pg_business_bonus_dao.go 1.5 KB
package dao

import (
	"fmt"
	"github.com/go-pg/pg/v10"
	"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"
)

type BusinessBonusDao struct {
	transactionContext *transaction.TransactionContext
}

//订单分红统计
func (dao *BusinessBonusDao) OrderBonusStatics(option domain.OrderBonusQuery) (rsp domain.OrderBonusResponse, err error) {
	rsp = domain.OrderBonusResponse{}
	if option.PartnerId == 0 && option.CompanyId == 0 && len(option.InPartnerIds) == 0 {
		return
	}
	tx := dao.transactionContext.PgTx
	q := tx.Model(new(models.BusinessBonus))
	q.ColumnExpr("count(*) count")
	q.ColumnExpr("sum(bonus) bonus")
	q.ColumnExpr("sum(bonus_expense) bonus_expense")
	if option.PartnerId > 0 {
		q.Where(`"business_bonus".partner_info_id =?`, option.PartnerId)
	}
	if option.CompanyId > 0 {
		q.Where(`"business_bonus".company_id =?`, option.CompanyId)
	}
	if option.IsDisable > 0 {
		q.Where(`"business_bonus".is_disable =?`, option.IsDisable)
	}
	if len(option.InPartnerIds) > 0 {
		q.Where(`"business_bonus".partner_info_id in (?)`, pg.In(option.InPartnerIds))
	}
	err = q.Select(&rsp.Total, &rsp.Bonus, &rsp.BonusExpense)
	return
}

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