pg_order_base_dao.go 4.6 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"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
	"time"
)

type OrderBaseDao struct {
	transactionContext *transaction.TransactionContext
}

//订单统计
func (dao *OrderBaseDao) OrderStatics(option *domain.OrderStaticQuery) (count int, amount float64, err error) {
	tx := dao.transactionContext.PgTx
	order := new(models.OrderBase)
	var orderAmount float64
	q := tx.Model(order)
	q.ColumnExpr("count(*) count")
	//q.ColumnExpr("sum(case when use_order_amount >0 THEN use_order_amount else plan_order_amount END) total_order_actual_amount")
	q.ColumnExpr("sum(plan_order_amount) total_order_actual_amount")
	q.ColumnExpr("sum(use_order_amount) total_order_amount")
	if option.PartnerId > 0 {
		q.Where(`"order_base".partner_id =?`, option.PartnerId)
	}
	if option.OrderType > 0 {
		q.Where(`"order_base".order_type =?`, option.OrderType)
	}
	if option.BeginTime > 0 {
		q.Where(`"order_base".create_time >=?`, time.Unix(option.BeginTime/1000, 0))
	}
	if option.EndTime > 0 {
		q.Where(`"order_base".create_time <?`, time.Unix(option.EndTime/1000, 0))
	}
	//q.Where(`"order_base".is_disable =?`, 0)
	err = q.Select(&count, &amount, &orderAmount)
	if option.OrderType == domain.OrderIntention { //意向订单值只有 order_amount  order_actual_amount还未赋值
		amount = orderAmount
	}
	return
}

//订单分红统计
func (dao *OrderBaseDao) 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.OrderBase))
	q.ColumnExpr("count(*) count")
	q.ColumnExpr("sum(plan_partner_bonus) bonus")
	q.ColumnExpr("sum(partner_bonus_expense) bonus_expense")
	if option.PartnerId > 0 {
		q.Where(`"order_base".partner_id =?`, option.PartnerId)
	}
	if option.CompanyId > 0 {
		q.Where(`"order_base".company_id =?`, option.CompanyId)
	}
	if len(option.InPartnerIds) > 0 {
		q.Where(`"order_base".partner_id in (?)`, pg.In(option.InPartnerIds))
	}
	err = q.Select(&rsp.Total, &rsp.Bonus, &rsp.BonusExpense)
	return
}

//分红订单
func (dao *OrderBaseDao) DividendOrders(option *domain.DividendOrdersQueryOption) (count int, domainOrders []*domain.OrderBase, err error) {
	tx := dao.transactionContext.PgTx
	var orders []*models.OrderBase
	q := tx.Model(&orders)
	//q.Column(`order_base.id`, `order_base.order_code`, `order_base.order_actual_amount`, `order_base.partner_bonus_percent`)
	if option.OrderType > 0 {
		q.Where(`"order_base".order_type=?`, option.OrderType)
	}
	if option.PartnerId > 0 {
		q.Where(`"order_base".partner_id=?`, option.PartnerId)
	}
	if option.StartTime > 0 {
		q.Where(`"order_base".create_time >=?`, time.Unix(option.StartTime/1000, 0))
	}
	if option.EndTime > 0 {
		q.Where(`"order_base".create_time <?`, time.Unix(option.EndTime/1000, 0))
	}
	//if len(option.IsDisable) > 0 {
	//	value, _ := strconv.Atoi(option.IsDisable)
	//	q.Where(`"order_base".is_disable =?`, value)
	//}
	if option.DividendAction == 0 { //累计分红
		if option.DetailAction == 0 { //已收明细
			q.Where(`"order_base".partner_bonus_has>0`)
		} else if option.DetailAction == 1 { //未收明细  //实际金额>已支付金额
			q.Where(`"order_base".partner_bonus_not>0`)
		}
	} else if option.DividendAction == 1 { //分红支出
		q.Where(`"order_base".partner_bonus_expense>0`)
	}
	if option.Limit > 0 {
		q.Limit(option.Limit)
	}
	if option.Offset > 0 {
		q.Offset(option.Offset)
	}
	if len(option.SortByUpdateTime) > 0 {
		q.Order(fmt.Sprintf("order_base.id %v", option.SortByUpdateTime))
	}
	count, err = q.Distinct().SelectAndCount()
	for i := range orders {
		var domainOrder *domain.OrderBase
		err = utils.GobModelTransform(&domainOrder, &orders[i])
		if err != nil {
			return
		}
		domainOrders = append(domainOrders, domainOrder)
	}
	return
}

func (dao *OrderBaseDao) UpdateLastViewTime(id int64, lastViewTime time.Time) (err error) {
	tx := dao.transactionContext.PgTx
	order := new(models.OrderBase)
	q := tx.Model(order).Set("last_view_time = ?", lastViewTime)
	q.Where("id=?", id)
	_, err = q.Update()
	return
}

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