muyu-apiserver/model/invproductbatchmodel.go
kae_mihara e3f6fa236d
feat:improve product yarn ratio workflow (#5)
* feat: support product yarn batches

* feat: improve product yarn ratio workflow

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-07-05 08:06:28 +09:00

82 lines
3.0 KiB
Go

package model
import (
"context"
"fmt"
"time"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ InvProductBatchModel = (*customInvProductBatchModel)(nil)
type (
InvProductBatchModel interface {
invProductBatchModel
FindByProductId(ctx context.Context, tenantId, productId string) ([]*InvProductBatch, error)
FindLatestByProductId(ctx context.Context, tenantId, productId string) (*InvProductBatch, error)
FindOneByProductAndBatchId(ctx context.Context, tenantId, productId, batchId string) (*InvProductBatch, error)
NextBatchNo(ctx context.Context, tenantId, productCode string) (string, error)
SoftDeleteByBatchId(ctx context.Context, batchId string) error
}
customInvProductBatchModel struct {
*defaultInvProductBatchModel
}
)
func NewInvProductBatchModel(conn sqlx.SqlConn) InvProductBatchModel {
return &customInvProductBatchModel{
defaultInvProductBatchModel: newInvProductBatchModel(conn),
}
}
func (m *customInvProductBatchModel) FindByProductId(ctx context.Context, tenantId, productId string) ([]*InvProductBatch, error) {
var list []*InvProductBatch
query := fmt.Sprintf("SELECT %s FROM %s WHERE tenant_id = ? AND product_id = ? AND deleted_at IS NULL ORDER BY batch_no", invProductBatchRows, m.table)
if err := m.conn.QueryRowsCtx(ctx, &list, query, tenantId, productId); err != nil {
return nil, err
}
return list, nil
}
func (m *customInvProductBatchModel) FindLatestByProductId(ctx context.Context, tenantId, productId string) (*InvProductBatch, error) {
var resp InvProductBatch
query := fmt.Sprintf("SELECT %s FROM %s WHERE tenant_id = ? AND product_id = ? AND deleted_at IS NULL AND status = 1 ORDER BY batch_no DESC LIMIT 1", invProductBatchRows, m.table)
if err := m.conn.QueryRowCtx(ctx, &resp, query, tenantId, productId); err != nil {
return nil, err
}
return &resp, nil
}
func (m *customInvProductBatchModel) FindOneByProductAndBatchId(ctx context.Context, tenantId, productId, batchId string) (*InvProductBatch, error) {
var resp InvProductBatch
query := fmt.Sprintf("SELECT %s FROM %s WHERE tenant_id = ? AND product_id = ? AND batch_id = ? AND deleted_at IS NULL LIMIT 1", invProductBatchRows, m.table)
if err := m.conn.QueryRowCtx(ctx, &resp, query, tenantId, productId, batchId); err != nil {
return nil, err
}
return &resp, nil
}
func (m *customInvProductBatchModel) NextBatchNo(ctx context.Context, tenantId, productCode string) (string, error) {
var next int64
query := fmt.Sprintf(`SELECT COALESCE(MAX(CAST(SUBSTRING(batch_no, CHAR_LENGTH(?) + 3) AS UNSIGNED)), 0) + 1
FROM %s
WHERE tenant_id = ? AND batch_no LIKE CONCAT(?, '-B%%')`, m.table)
if err := m.conn.QueryRowCtx(ctx, &next, query, productCode, tenantId, productCode); err != nil {
return "", err
}
return fmt.Sprintf("%s-B%03d", productCode, next), nil
}
func (m *customInvProductBatchModel) SoftDeleteByBatchId(ctx context.Context, batchId string) error {
batch, err := m.FindOneByBatchId(ctx, batchId)
if err != nil {
return err
}
batch.Status = 0
batch.DeletedAt.Time = time.Now()
batch.DeletedAt.Valid = true
return m.Update(ctx, batch)
}