muyu-apiserver/model/proinboundrecordmodel.go

57 lines
1.8 KiB
Go
Raw Normal View History

package model
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ ProInboundRecordModel = (*customProInboundRecordModel)(nil)
type (
ProInboundRecordModel interface {
proInboundRecordModel
FindByPlanId(ctx context.Context, planId string, page, pageSize int64) ([]*ProInboundRecord, int64, error)
SumQuantityByPlan(ctx context.Context, planId string) (float64, int64, error)
}
customProInboundRecordModel struct {
*defaultProInboundRecordModel
}
)
func NewProInboundRecordModel(conn sqlx.SqlConn) ProInboundRecordModel {
return &customProInboundRecordModel{
defaultProInboundRecordModel: newProInboundRecordModel(conn),
}
}
func (m *customProInboundRecordModel) FindByPlanId(ctx context.Context, planId string, page, pageSize int64) ([]*ProInboundRecord, int64, error) {
var total int64
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE plan_id = ?", m.table)
if err := m.conn.QueryRowCtx(ctx, &total, countQuery, planId); err != nil {
return nil, 0, err
}
var list []*ProInboundRecord
query := fmt.Sprintf("SELECT %s FROM %s WHERE plan_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?",
proInboundRecordRows, m.table)
if err := m.conn.QueryRowsCtx(ctx, &list, query, planId, pageSize, (page-1)*pageSize); err != nil {
return nil, 0, err
}
return list, total, nil
}
func (m *customProInboundRecordModel) SumQuantityByPlan(ctx context.Context, planId string) (float64, int64, error) {
var result struct {
TotalQty float64 `db:"total_qty"`
TotalRolls int64 `db:"total_rolls"`
}
query := fmt.Sprintf("SELECT COALESCE(SUM(quantity),0) as total_qty, COALESCE(SUM(rolls),0) as total_rolls FROM %s WHERE plan_id = ?", m.table)
if err := m.conn.QueryRowCtx(ctx, &result, query, planId); err != nil {
return 0, 0, err
}
return result.TotalQty, result.TotalRolls, nil
}