pg_order_dao.go 1.3 KB
package dao

import (
	"fmt"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	"time"
)

type OrderDao struct {
	transactionContext *transaction.TransactionContext
}

func (dao *OrderDao) OrderStatics(option *domain.OrderStaticQuery) (count int, amount float64, err error) {
	tx := dao.transactionContext.PgTx
	order := new(models.Order)
	q := tx.Model(order)
	q.ColumnExpr("count(*) count")
	q.ColumnExpr("sum(order_amount) total_order_amount")
	if option.PartnerId > 0 {
		q.Where(`"order".partner_id =?`, option.PartnerId)
	}
	if option.OrderType > 0 {
		q.Where(`"order".order_type =?`, option.OrderType)
	}
	if option.OrderStatus > 0 {
		q.Where(`"order".order_status =?`, option.OrderStatus)
	}
	if option.BeginTime > 0 {
		q.Where(`"order".create_at >=?`, time.Unix(option.BeginTime/1000, 0))
	}
	if option.EndTime > 0 {
		q.Where(`"order".create_at <?`, time.Unix(option.EndTime/1000, 0))
	}
	err = q.Select(&count, &amount)
	return
}

func NewOrderDao(transactionContext *transaction.TransactionContext) (*OrderDao, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &OrderDao{
			transactionContext: transactionContext,
		}, nil
	}
}