83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
|
|
package model
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||
|
|
)
|
||
|
|
|
||
|
|
var _ InvYarnModel = (*customInvYarnModel)(nil)
|
||
|
|
|
||
|
|
type (
|
||
|
|
InvYarnModel interface {
|
||
|
|
invYarnModel
|
||
|
|
FindList(ctx context.Context, tenantId string, page, pageSize int64, yarnName, supplierId string, status int64) ([]*InvYarn, int64, error)
|
||
|
|
FindOneByUnique(ctx context.Context, tenantId, yarnName, color, supplierId string) (*InvYarn, error)
|
||
|
|
SoftDeleteByYarnId(ctx context.Context, yarnId string) error
|
||
|
|
}
|
||
|
|
|
||
|
|
customInvYarnModel struct {
|
||
|
|
*defaultInvYarnModel
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func NewInvYarnModel(conn sqlx.SqlConn) InvYarnModel {
|
||
|
|
return &customInvYarnModel{
|
||
|
|
defaultInvYarnModel: newInvYarnModel(conn),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customInvYarnModel) FindList(ctx context.Context, tenantId string, page, pageSize int64, yarnName, supplierId string, status int64) ([]*InvYarn, int64, error) {
|
||
|
|
where := "WHERE tenant_id = ? AND deleted_at IS NULL"
|
||
|
|
args := []interface{}{tenantId}
|
||
|
|
|
||
|
|
if yarnName != "" {
|
||
|
|
where += " AND yarn_name LIKE ?"
|
||
|
|
args = append(args, "%"+yarnName+"%")
|
||
|
|
}
|
||
|
|
if supplierId != "" {
|
||
|
|
where += " AND supplier_id = ?"
|
||
|
|
args = append(args, supplierId)
|
||
|
|
}
|
||
|
|
if status >= 0 {
|
||
|
|
where += " AND status = ?"
|
||
|
|
args = append(args, status)
|
||
|
|
}
|
||
|
|
|
||
|
|
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 []*InvYarn
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", invYarnRows, 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 *customInvYarnModel) FindOneByUnique(ctx context.Context, tenantId, yarnName, color, supplierId string) (*InvYarn, error) {
|
||
|
|
var resp InvYarn
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE tenant_id = ? AND yarn_name = ? AND color = ? AND supplier_id = ? AND deleted_at IS NULL LIMIT 1", invYarnRows, m.table)
|
||
|
|
if err := m.conn.QueryRowCtx(ctx, &resp, query, tenantId, yarnName, color, supplierId); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &resp, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customInvYarnModel) SoftDeleteByYarnId(ctx context.Context, yarnId string) error {
|
||
|
|
yarn, err := m.FindOneByYarnId(ctx, yarnId)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
yarn.Status = 0
|
||
|
|
yarn.DeletedAt.Time = time.Now()
|
||
|
|
yarn.DeletedAt.Valid = true
|
||
|
|
return m.Update(ctx, yarn)
|
||
|
|
}
|