168 lines
7.3 KiB
Go
168 lines
7.3 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/builder"
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
)
|
|
|
|
var (
|
|
proProductionPlanFieldNames = builder.RawFieldNames(&ProProductionPlan{})
|
|
proProductionPlanRows = strings.Join(proProductionPlanFieldNames, ",")
|
|
proProductionPlanRowsExpectAutoSet = strings.Join(stringx.Remove(proProductionPlanFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
|
proProductionPlanRowsWithPlaceHolder = strings.Join(stringx.Remove(proProductionPlanFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
|
|
|
cacheProProductionPlanIdPrefix = "cache:proProductionPlan:id:"
|
|
cacheProProductionPlanPlanIdPrefix = "cache:proProductionPlan:planId:"
|
|
)
|
|
|
|
type (
|
|
proProductionPlanModel interface {
|
|
Insert(ctx context.Context, data *ProProductionPlan) (sql.Result, error)
|
|
FindOne(ctx context.Context, id int64) (*ProProductionPlan, error)
|
|
FindOneByPlanId(ctx context.Context, planId string) (*ProProductionPlan, error)
|
|
Update(ctx context.Context, data *ProProductionPlan) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
defaultProProductionPlanModel struct {
|
|
sqlc.CachedConn
|
|
table string
|
|
}
|
|
|
|
ProProductionPlan struct {
|
|
Id int64 `db:"id"`
|
|
PlanId string `db:"plan_id"`
|
|
TenantId string `db:"tenant_id"`
|
|
PlanCode string `db:"plan_code"`
|
|
ProductId string `db:"product_id"`
|
|
ProductName string `db:"product_name"`
|
|
Color string `db:"color"`
|
|
FabricCode string `db:"fabric_code"`
|
|
ColorCode string `db:"color_code"`
|
|
TargetQuantity float64 `db:"target_quantity"`
|
|
CompletedQuantity float64 `db:"completed_quantity"`
|
|
YarnUsagePerMeter float64 `db:"yarn_usage_per_meter"`
|
|
ProductionPrice float64 `db:"production_price"`
|
|
SupplierId string `db:"supplier_id"`
|
|
Status int64 `db:"status"`
|
|
Remark sql.NullString `db:"remark"`
|
|
StartTime sql.NullTime `db:"start_time"`
|
|
Creator string `db:"creator"`
|
|
Operator string `db:"operator"`
|
|
CreatedAt time.Time `db:"created_at"`
|
|
UpdatedAt time.Time `db:"updated_at"`
|
|
}
|
|
)
|
|
|
|
func newProProductionPlanModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultProProductionPlanModel {
|
|
return &defaultProProductionPlanModel{
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
|
table: "`pro_production_plan`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultProProductionPlanModel) Insert(ctx context.Context, data *ProProductionPlan) (sql.Result, error) {
|
|
proProductionPlanIdKey := fmt.Sprintf("%s%v", cacheProProductionPlanIdPrefix, data.Id)
|
|
proProductionPlanPlanIdKey := fmt.Sprintf("%s%v", cacheProProductionPlanPlanIdPrefix, data.PlanId)
|
|
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, proProductionPlanRowsExpectAutoSet)
|
|
return conn.ExecCtx(ctx, query,
|
|
data.PlanId, data.TenantId, data.PlanCode, data.ProductId, data.ProductName,
|
|
data.Color, data.FabricCode, data.ColorCode, data.TargetQuantity, data.CompletedQuantity,
|
|
data.YarnUsagePerMeter, data.ProductionPrice, data.SupplierId, data.Status, data.Remark,
|
|
data.StartTime, data.Creator, data.Operator)
|
|
}, proProductionPlanIdKey, proProductionPlanPlanIdKey)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultProProductionPlanModel) FindOne(ctx context.Context, id int64) (*ProProductionPlan, error) {
|
|
proProductionPlanIdKey := fmt.Sprintf("%s%v", cacheProProductionPlanIdPrefix, id)
|
|
var resp ProProductionPlan
|
|
err := m.QueryRowCtx(ctx, &resp, proProductionPlanIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? LIMIT 1", proProductionPlanRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, id)
|
|
})
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultProProductionPlanModel) FindOneByPlanId(ctx context.Context, planId string) (*ProProductionPlan, error) {
|
|
proProductionPlanPlanIdKey := fmt.Sprintf("%s%v", cacheProProductionPlanPlanIdPrefix, planId)
|
|
var resp ProProductionPlan
|
|
err := m.QueryRowIndexCtx(ctx, &resp, proProductionPlanPlanIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE `plan_id` = ? LIMIT 1", proProductionPlanRows, m.table)
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, planId); err != nil {
|
|
return nil, err
|
|
}
|
|
return resp.Id, nil
|
|
}, m.queryPrimary)
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultProProductionPlanModel) Update(ctx context.Context, newData *ProProductionPlan) error {
|
|
data, err := m.FindOne(ctx, newData.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
proProductionPlanIdKey := fmt.Sprintf("%s%v", cacheProProductionPlanIdPrefix, data.Id)
|
|
proProductionPlanPlanIdKey := fmt.Sprintf("%s%v", cacheProProductionPlanPlanIdPrefix, data.PlanId)
|
|
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, proProductionPlanRowsWithPlaceHolder)
|
|
return conn.ExecCtx(ctx, query,
|
|
newData.PlanId, newData.TenantId, newData.PlanCode, newData.ProductId, newData.ProductName,
|
|
newData.Color, newData.FabricCode, newData.ColorCode, newData.TargetQuantity, newData.CompletedQuantity,
|
|
newData.YarnUsagePerMeter, newData.ProductionPrice, newData.SupplierId, newData.Status, newData.Remark,
|
|
newData.StartTime, newData.Creator, newData.Operator, newData.Id)
|
|
}, proProductionPlanIdKey, proProductionPlanPlanIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultProProductionPlanModel) Delete(ctx context.Context, id int64) error {
|
|
data, err := m.FindOne(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
proProductionPlanIdKey := fmt.Sprintf("%s%v", cacheProProductionPlanIdPrefix, id)
|
|
proProductionPlanPlanIdKey := fmt.Sprintf("%s%v", cacheProProductionPlanPlanIdPrefix, data.PlanId)
|
|
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("DELETE FROM %s WHERE `id` = ?", m.table)
|
|
return conn.ExecCtx(ctx, query, id)
|
|
}, proProductionPlanIdKey, proProductionPlanPlanIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultProProductionPlanModel) formatPrimary(primary any) string {
|
|
return fmt.Sprintf("%s%v", cacheProProductionPlanIdPrefix, primary)
|
|
}
|
|
|
|
func (m *defaultProProductionPlanModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? LIMIT 1", proProductionPlanRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
|
}
|
|
|
|
func (m *defaultProProductionPlanModel) tableName() string {
|
|
return m.table
|
|
}
|