package model import ( "context" "fmt" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var _ PurPurchasePaymentModel = (*customPurPurchasePaymentModel)(nil) type ( PurPurchasePaymentModel interface { purPurchasePaymentModel FindByOrderId(ctx context.Context, orderId string) ([]*PurPurchasePayment, error) FindList(ctx context.Context, tenantId string, page, pageSize int64, orderId, supplierId string, startDate, endDate string) ([]*PurPurchasePayment, int64, error) CountTodayPayments(ctx context.Context, tenantId, dateStr string) (int64, error) } customPurPurchasePaymentModel struct { *defaultPurPurchasePaymentModel } ) func NewPurPurchasePaymentModel(conn sqlx.SqlConn) PurPurchasePaymentModel { return &customPurPurchasePaymentModel{ defaultPurPurchasePaymentModel: newPurPurchasePaymentModel(conn), } } func (m *customPurPurchasePaymentModel) FindByOrderId(ctx context.Context, orderId string) ([]*PurPurchasePayment, error) { var list []*PurPurchasePayment query := fmt.Sprintf("SELECT %s FROM %s WHERE order_id = ? ORDER BY created_at DESC", purPurchasePaymentRows, m.table) if err := m.conn.QueryRowsCtx(ctx, &list, query, orderId); err != nil { return nil, err } return list, nil } func (m *customPurPurchasePaymentModel) FindList(ctx context.Context, tenantId string, page, pageSize int64, orderId, supplierId string, startDate, endDate string) ([]*PurPurchasePayment, int64, error) { where := "WHERE tenant_id = ?" args := []interface{}{tenantId} if orderId != "" { where += " AND order_id = ?" args = append(args, orderId) } if supplierId != "" { where += " AND supplier_id = ?" args = append(args, supplierId) } if startDate != "" { where += " AND payment_date >= ?" args = append(args, startDate) } if endDate != "" { where += " AND payment_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 []*PurPurchasePayment query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", purPurchasePaymentRows, 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 *customPurPurchasePaymentModel) CountTodayPayments(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 }