package model import ( "context" "database/sql" "fmt" "strings" "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" ) var ( invProductBatchFieldNames = builder.RawFieldNames(&InvProductBatch{}) invProductBatchRows = strings.Join(invProductBatchFieldNames, ",") invProductBatchRowsExpectAutoSet = strings.Join(stringx.Remove(invProductBatchFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") invProductBatchRowsWithPlaceHolder = strings.Join(stringx.Remove(invProductBatchFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" ) type ( invProductBatchModel interface { Insert(ctx context.Context, data *InvProductBatch) (sql.Result, error) FindOne(ctx context.Context, id int64) (*InvProductBatch, error) FindOneByBatchId(ctx context.Context, batchId string) (*InvProductBatch, error) Update(ctx context.Context, data *InvProductBatch) error Delete(ctx context.Context, id int64) error } defaultInvProductBatchModel struct { conn sqlx.SqlConn table string } InvProductBatch struct { Id int64 `db:"id"` BatchId string `db:"batch_id"` TenantId string `db:"tenant_id"` ProductId string `db:"product_id"` BatchNo string `db:"batch_no"` YarnRatio sql.NullString `db:"yarn_ratio"` WarpWeightGM float64 `db:"warp_weight_g_m"` WeftWeightGM float64 `db:"weft_weight_g_m"` ProductionProcess sql.NullString `db:"production_process"` Remark sql.NullString `db:"remark"` Status int64 `db:"status"` CreatedAt time.Time `db:"created_at"` UpdatedAt time.Time `db:"updated_at"` DeletedAt sql.NullTime `db:"deleted_at"` } ) func newInvProductBatchModel(conn sqlx.SqlConn) *defaultInvProductBatchModel { return &defaultInvProductBatchModel{ conn: conn, table: "`inv_product_batch`", } } func (m *defaultInvProductBatchModel) Insert(ctx context.Context, data *InvProductBatch) (sql.Result, error) { query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, invProductBatchRowsExpectAutoSet) return m.conn.ExecCtx(ctx, query, data.BatchId, data.TenantId, data.ProductId, data.BatchNo, data.YarnRatio, data.WarpWeightGM, data.WeftWeightGM, data.ProductionProcess, data.Remark, data.Status, data.DeletedAt) } func (m *defaultInvProductBatchModel) FindOne(ctx context.Context, id int64) (*InvProductBatch, error) { query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? LIMIT 1", invProductBatchRows, m.table) var resp InvProductBatch err := m.conn.QueryRowCtx(ctx, &resp, query, id) if err != nil { return nil, err } return &resp, nil } func (m *defaultInvProductBatchModel) FindOneByBatchId(ctx context.Context, batchId string) (*InvProductBatch, error) { query := fmt.Sprintf("SELECT %s FROM %s WHERE `batch_id` = ? AND `deleted_at` IS NULL LIMIT 1", invProductBatchRows, m.table) var resp InvProductBatch err := m.conn.QueryRowCtx(ctx, &resp, query, batchId) if err != nil { return nil, err } return &resp, nil } func (m *defaultInvProductBatchModel) Update(ctx context.Context, data *InvProductBatch) error { query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, invProductBatchRowsWithPlaceHolder) _, err := m.conn.ExecCtx(ctx, query, data.BatchId, data.TenantId, data.ProductId, data.BatchNo, data.YarnRatio, data.WarpWeightGM, data.WeftWeightGM, data.ProductionProcess, data.Remark, data.Status, data.DeletedAt, data.Id) return err } func (m *defaultInvProductBatchModel) Delete(ctx context.Context, id int64) error { query := fmt.Sprintf("DELETE FROM %s WHERE `id` = ?", m.table) _, err := m.conn.ExecCtx(ctx, query, id) return err } func (m *defaultInvProductBatchModel) tableName() string { return m.table }