package model import ( "context" "fmt" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var _ InvInventoryLogModel = (*customInvInventoryLogModel)(nil) type ( InvInventoryLogModel interface { invInventoryLogModel FindList(ctx context.Context, tenantId string, page, pageSize int64, productId, refType string, changeType int64, startDate, endDate string) ([]*InvInventoryLog, int64, error) FindByRefId(ctx context.Context, refId string) ([]*InvInventoryLog, error) } customInvInventoryLogModel struct { *defaultInvInventoryLogModel } ) func NewInvInventoryLogModel(conn sqlx.SqlConn) InvInventoryLogModel { return &customInvInventoryLogModel{ defaultInvInventoryLogModel: newInvInventoryLogModel(conn), } } func (m *customInvInventoryLogModel) FindList(ctx context.Context, tenantId string, page, pageSize int64, productId, refType string, changeType int64, startDate, endDate string) ([]*InvInventoryLog, int64, error) { where := "WHERE tenant_id = ?" args := []interface{}{tenantId} if productId != "" { where += " AND product_id = ?" args = append(args, productId) } if refType != "" { where += " AND ref_type = ?" args = append(args, refType) } if changeType > 0 { where += " AND change_type = ?" args = append(args, changeType) } if startDate != "" { where += " AND log_date >= ?" args = append(args, startDate) } if endDate != "" { where += " AND log_date <= ?" args = append(args, endDate) } var total int64 countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s %s", m.table, where) if err := m.conn.QueryRowCtx(ctx, &total, countQuery, args...); err != nil { return nil, 0, err } var list []*InvInventoryLog query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", invInventoryLogRows, m.table, where) args = append(args, pageSize, (page-1)*pageSize) if err := m.conn.QueryRowsCtx(ctx, &list, query, args...); err != nil { return nil, 0, err } return list, total, nil } func (m *customInvInventoryLogModel) FindByRefId(ctx context.Context, refId string) ([]*InvInventoryLog, error) { var list []*InvInventoryLog query := fmt.Sprintf("SELECT %s FROM %s WHERE ref_id = ? ORDER BY created_at DESC", invInventoryLogRows, m.table) if err := m.conn.QueryRowsCtx(ctx, &list, query, refId); err != nil { return nil, err } return list, nil }