package model import ( "context" "fmt" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var _ PurPurchaseReceiptModel = (*customPurPurchaseReceiptModel)(nil) type ( PurPurchaseReceiptModel interface { purPurchaseReceiptModel FindByOrderId(ctx context.Context, orderId string) ([]*PurPurchaseReceipt, error) FindList(ctx context.Context, tenantId string, page, pageSize int64, orderId string, status int64, startDate, endDate string) ([]*PurPurchaseReceipt, int64, error) CountTodayReceipts(ctx context.Context, tenantId, dateStr string) (int64, error) } customPurPurchaseReceiptModel struct { *defaultPurPurchaseReceiptModel } ) func NewPurPurchaseReceiptModel(conn sqlx.SqlConn) PurPurchaseReceiptModel { return &customPurPurchaseReceiptModel{ defaultPurPurchaseReceiptModel: newPurPurchaseReceiptModel(conn), } } func (m *customPurPurchaseReceiptModel) FindByOrderId(ctx context.Context, orderId string) ([]*PurPurchaseReceipt, error) { var list []*PurPurchaseReceipt query := fmt.Sprintf("SELECT %s FROM %s WHERE order_id = ? ORDER BY created_at DESC", purPurchaseReceiptRows, m.table) if err := m.conn.QueryRowsCtx(ctx, &list, query, orderId); err != nil { return nil, err } return list, nil } func (m *customPurPurchaseReceiptModel) FindList(ctx context.Context, tenantId string, page, pageSize int64, orderId string, status int64, startDate, endDate string) ([]*PurPurchaseReceipt, int64, error) { where := "WHERE tenant_id = ?" args := []interface{}{tenantId} if orderId != "" { where += " AND order_id = ?" args = append(args, orderId) } if status != -1 { where += " AND status = ?" args = append(args, status) } if startDate != "" { where += " AND receipt_date >= ?" args = append(args, startDate) } if endDate != "" { where += " AND receipt_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 []*PurPurchaseReceipt query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", purPurchaseReceiptRows, 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 *customPurPurchaseReceiptModel) CountTodayReceipts(ctx context.Context, tenantId, dateStr string) (int64, error) { var count int64 query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE tenant_id = ? AND DATE(created_at) = ?", m.table) if err := m.conn.QueryRowCtx(ctx, &count, query, tenantId, dateStr); err != nil { return 0, err } return count, nil }