103 lines
3.9 KiB
Go
103 lines
3.9 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/sqlx"
|
||
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
||
|
|
)
|
||
|
|
|
||
|
|
var (
|
||
|
|
invInventoryLogFieldNames = builder.RawFieldNames(&InvInventoryLog{})
|
||
|
|
invInventoryLogRows = strings.Join(invInventoryLogFieldNames, ",")
|
||
|
|
invInventoryLogRowsExpectAutoSet = strings.Join(stringx.Remove(invInventoryLogFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||
|
|
invInventoryLogRowsWithPlaceHolder = strings.Join(stringx.Remove(invInventoryLogFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||
|
|
)
|
||
|
|
|
||
|
|
type (
|
||
|
|
invInventoryLogModel interface {
|
||
|
|
Insert(ctx context.Context, data *InvInventoryLog) (sql.Result, error)
|
||
|
|
FindOne(ctx context.Context, id int64) (*InvInventoryLog, error)
|
||
|
|
FindOneByLogId(ctx context.Context, logId string) (*InvInventoryLog, error)
|
||
|
|
Update(ctx context.Context, data *InvInventoryLog) error
|
||
|
|
Delete(ctx context.Context, id int64) error
|
||
|
|
}
|
||
|
|
|
||
|
|
defaultInvInventoryLogModel struct {
|
||
|
|
conn sqlx.SqlConn
|
||
|
|
table string
|
||
|
|
}
|
||
|
|
|
||
|
|
InvInventoryLog struct {
|
||
|
|
Id int64 `db:"id"`
|
||
|
|
LogId string `db:"log_id"`
|
||
|
|
TenantId string `db:"tenant_id"`
|
||
|
|
ProductId string `db:"product_id"`
|
||
|
|
ChangeQty float64 `db:"change_qty"`
|
||
|
|
BalanceAfter float64 `db:"balance_after"`
|
||
|
|
ChangeType int64 `db:"change_type"`
|
||
|
|
RefType string `db:"ref_type"`
|
||
|
|
RefId string `db:"ref_id"`
|
||
|
|
ContactId string `db:"contact_id"`
|
||
|
|
LogDate time.Time `db:"log_date"`
|
||
|
|
Operator string `db:"operator"`
|
||
|
|
Remark sql.NullString `db:"remark"`
|
||
|
|
CreatedAt time.Time `db:"created_at"`
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func newInvInventoryLogModel(conn sqlx.SqlConn) *defaultInvInventoryLogModel {
|
||
|
|
return &defaultInvInventoryLogModel{
|
||
|
|
conn: conn,
|
||
|
|
table: "`inv_inventory_log`",
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultInvInventoryLogModel) Insert(ctx context.Context, data *InvInventoryLog) (sql.Result, error) {
|
||
|
|
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, invInventoryLogRowsExpectAutoSet)
|
||
|
|
return m.conn.ExecCtx(ctx, query,
|
||
|
|
data.LogId, data.TenantId, data.ProductId, data.ChangeQty, data.BalanceAfter,
|
||
|
|
data.ChangeType, data.RefType, data.RefId, data.ContactId, data.LogDate, data.Operator, data.Remark)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultInvInventoryLogModel) FindOne(ctx context.Context, id int64) (*InvInventoryLog, error) {
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? LIMIT 1", invInventoryLogRows, m.table)
|
||
|
|
var resp InvInventoryLog
|
||
|
|
if err := m.conn.QueryRowCtx(ctx, &resp, query, id); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &resp, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultInvInventoryLogModel) FindOneByLogId(ctx context.Context, logId string) (*InvInventoryLog, error) {
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE `log_id` = ? LIMIT 1", invInventoryLogRows, m.table)
|
||
|
|
var resp InvInventoryLog
|
||
|
|
if err := m.conn.QueryRowCtx(ctx, &resp, query, logId); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &resp, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultInvInventoryLogModel) Update(ctx context.Context, data *InvInventoryLog) error {
|
||
|
|
query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, invInventoryLogRowsWithPlaceHolder)
|
||
|
|
_, err := m.conn.ExecCtx(ctx, query,
|
||
|
|
data.LogId, data.TenantId, data.ProductId, data.ChangeQty, data.BalanceAfter,
|
||
|
|
data.ChangeType, data.RefType, data.RefId, data.ContactId, data.LogDate, data.Operator, data.Remark, data.Id)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultInvInventoryLogModel) 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 *defaultInvInventoryLogModel) tableName() string {
|
||
|
|
return m.table
|
||
|
|
}
|