muyu-apiserver/model/invproductboltmodel.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

147 lines
5.1 KiB
Go

package model
import (
"context"
"fmt"
"strings"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ InvProductBoltModel = (*customInvProductBoltModel)(nil)
// BoltListItem is the flat view row for the bolt-dimension panel.
// Color comes from the bolt itself, spec is omitted (product-level only).
type BoltListItem struct {
BoltId string `db:"bolt_id"`
PanId string `db:"pan_id"`
LengthM float64 `db:"length_m"`
Color string `db:"color"`
SortOrder int64 `db:"sort_order"`
PanName string `db:"pan_name"`
Position string `db:"position"`
ProductId string `db:"product_id"`
ProductName string `db:"product_name"`
}
type (
InvProductBoltModel interface {
invProductBoltModel
FindByPanId(ctx context.Context, panId string) ([]*InvProductBolt, error)
FindByProductId(ctx context.Context, productId string) ([]*InvProductBolt, error)
DeleteByPanId(ctx context.Context, panId string) error
DeleteByProductId(ctx context.Context, productId string) error
BulkReplace(ctx context.Context, panId, tenantId string, bolts []*InvProductBolt) error
FindBoltList(ctx context.Context, tenantId string, page, pageSize int64, productName, position string) ([]*BoltListItem, int64, error)
}
customInvProductBoltModel struct {
*defaultInvProductBoltModel
}
)
func NewInvProductBoltModel(conn sqlx.SqlConn) InvProductBoltModel {
return &customInvProductBoltModel{
defaultInvProductBoltModel: newInvProductBoltModel(conn),
}
}
func (m *customInvProductBoltModel) FindByPanId(ctx context.Context, panId string) ([]*InvProductBolt, error) {
var list []*InvProductBolt
query := fmt.Sprintf("SELECT %s FROM %s WHERE `pan_id` = ? ORDER BY `sort_order`", invProductBoltRows, m.table)
err := m.conn.QueryRowsCtx(ctx, &list, query, panId)
if err != nil {
return nil, err
}
return list, nil
}
func (m *customInvProductBoltModel) FindByProductId(ctx context.Context, productId string) ([]*InvProductBolt, error) {
var list []*InvProductBolt
query := fmt.Sprintf(
"SELECT b.`id`, b.`bolt_id`, b.`pan_id`, b.`length_m`, b.`color`, b.`sort_order`, b.`tenant_id`, b.`created_at`, b.`updated_at` "+
"FROM %s b INNER JOIN `inv_product_pan` p ON b.`pan_id` = p.`pan_id` "+
"WHERE p.`product_id` = ? ORDER BY p.`sort_order`, b.`sort_order`", m.table)
err := m.conn.QueryRowsCtx(ctx, &list, query, productId)
if err != nil {
return nil, err
}
return list, nil
}
func (m *customInvProductBoltModel) DeleteByPanId(ctx context.Context, panId string) error {
query := fmt.Sprintf("DELETE FROM %s WHERE `pan_id` = ?", m.table)
_, err := m.conn.ExecCtx(ctx, query, panId)
return err
}
func (m *customInvProductBoltModel) DeleteByProductId(ctx context.Context, productId string) error {
query := fmt.Sprintf(
"DELETE b FROM %s b INNER JOIN `inv_product_pan` p ON b.`pan_id` = p.`pan_id` WHERE p.`product_id` = ?", m.table)
_, err := m.conn.ExecCtx(ctx, query, productId)
return err
}
func (m *customInvProductBoltModel) FindBoltList(ctx context.Context, tenantId string, page, pageSize int64, productName, position string) ([]*BoltListItem, int64, error) {
where := "WHERE b.tenant_id = ? AND p.deleted_at IS NULL"
args := []interface{}{tenantId}
if productName != "" {
where += " AND p.product_name LIKE ?"
args = append(args, "%"+productName+"%")
}
if position != "" {
where += " AND pan.position LIKE ?"
args = append(args, "%"+position+"%")
}
countQuery := fmt.Sprintf(`SELECT COUNT(*)
FROM %s b
JOIN inv_product_pan pan ON pan.pan_id = b.pan_id
JOIN inv_product p ON p.product_id = pan.product_id
%s`, m.table, where)
var total int64
if err := m.conn.QueryRowCtx(ctx, &total, countQuery, args...); err != nil {
return nil, 0, err
}
query := fmt.Sprintf(`SELECT b.bolt_id, b.pan_id, b.length_m, b.color, b.sort_order,
pan.name AS pan_name, pan.position,
p.product_id, p.product_name
FROM %s b
JOIN inv_product_pan pan ON pan.pan_id = b.pan_id
JOIN inv_product p ON p.product_id = pan.product_id
%s
ORDER BY b.created_at DESC
LIMIT ? OFFSET ?`, m.table, where)
args = append(args, pageSize, (page-1)*pageSize)
var list []*BoltListItem
if err := m.conn.QueryRowsCtx(ctx, &list, query, args...); err != nil {
return nil, 0, err
}
return list, total, nil
}
func (m *customInvProductBoltModel) BulkReplace(ctx context.Context, panId, tenantId string, bolts []*InvProductBolt) error {
return m.conn.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
delQuery := fmt.Sprintf("DELETE FROM %s WHERE `pan_id` = ?", m.table)
if _, err := session.ExecCtx(ctx, delQuery, panId); err != nil {
return err
}
if len(bolts) == 0 {
return nil
}
placeholder := "(?, ?, ?, ?, ?, ?)"
placeholders := make([]string, len(bolts))
args := make([]interface{}, 0, len(bolts)*6)
for i, b := range bolts {
placeholders[i] = placeholder
args = append(args, b.BoltId, panId, b.LengthM, b.Color, b.SortOrder, tenantId)
}
insQuery := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", m.table, invProductBoltRowsExpectAutoSet, strings.Join(placeholders, ","))
_, err := session.ExecCtx(ctx, insQuery, args...)
return err
})
}