package model import ( "context" "fmt" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var _ ProProductionPlanModel = (*customProProductionPlanModel)(nil) type ( ProProductionPlanModel interface { proProductionPlanModel FindList(ctx context.Context, tenantId string, page, pageSize int64, status int64, productName, planCode string) ([]*ProProductionPlan, int64, error) CountTodayPlans(ctx context.Context, tenantId, dateStr string) (int64, error) UpdateCompletedQuantity(ctx context.Context, planId string, operator string) error UpdateStatus(ctx context.Context, planId string, status int64) error } customProProductionPlanModel struct { *defaultProProductionPlanModel } ) func NewProProductionPlanModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) ProProductionPlanModel { return &customProProductionPlanModel{ defaultProProductionPlanModel: newProProductionPlanModel(conn, c, opts...), } } func (m *customProProductionPlanModel) FindList(ctx context.Context, tenantId string, page, pageSize int64, status int64, productName, planCode string) ([]*ProProductionPlan, int64, error) { where := "WHERE tenant_id = ?" args := []interface{}{tenantId} if status != -1 { where += " AND status = ?" args = append(args, status) } if productName != "" { where += " AND product_name LIKE ?" args = append(args, "%"+productName+"%") } if planCode != "" { where += " AND plan_code LIKE ?" args = append(args, "%"+planCode+"%") } var total int64 countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s %s", m.table, where) if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, args...); err != nil { return nil, 0, err } var list []*ProProductionPlan query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", proProductionPlanRows, m.table, where) args = append(args, pageSize, (page-1)*pageSize) if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil { return nil, 0, err } return list, total, nil } func (m *customProProductionPlanModel) CountTodayPlans(ctx context.Context, tenantId, dateStr string) (int64, error) { var count int64 query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE tenant_id = ? AND DATE(created_at) = ?", m.table) if err := m.QueryRowNoCacheCtx(ctx, &count, query, tenantId, dateStr); err != nil { return 0, err } return count, nil } func (m *customProProductionPlanModel) UpdateCompletedQuantity(ctx context.Context, planId string, operator string) error { // Use SUM to recalculate, not increment — prevents drift query := fmt.Sprintf(`UPDATE %s SET completed_quantity = COALESCE((SELECT SUM(quantity) FROM pro_inbound_record WHERE plan_id = ?), 0), operator = ? WHERE plan_id = ?`, m.table) _, err := m.ExecNoCacheCtx(ctx, query, planId, operator, planId) return err } func (m *customProProductionPlanModel) UpdateStatus(ctx context.Context, planId string, status int64) error { query := fmt.Sprintf("UPDATE %s SET status = ? WHERE plan_id = ?", m.table) _, err := m.ExecNoCacheCtx(ctx, query, status, planId) return err }