order_v2.go 3.2 KB
package order

import (
	"github.com/tiptok/gocomm/sync/task"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
)

// 订单统计
func StatisticsV2(header *protocol.RequestHeader, request *protocol.OrderStatisticsRequest) (rsp *protocol.OrderStatisticsResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		OrderDao, _           = factory.CreateOrderBaseDao(transactionContext)
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	rsp = &protocol.OrderStatisticsResponse{}

	//事业分红/业务分红
	var total float64
	if bonus, e := OrderDao.OrderBonusStatics(domain.OrderBonusQuery{PartnerId: request.PartnerId, OrderTypes: domain.UserOrderTypes(domain.Career), EndTime: utils.GetDayEnd().Unix() * 1000}); e == nil {
		total += bonus.Bonus
	}
	calcOrderStatic := func(catId int, total float64) protocol.OrderStatic {
		todayRealQuantity, todayRealMoney, _ := OrderDao.OrderStatics(&domain.OrderStaticQuery{
			BeginTime:         utils.GetDayBegin().Unix() * 1000,
			EndTime:           utils.GetDayEnd().Unix() * 1000,
			OrderTypes:        domain.UserOrderTypes(domain.Career),
			PartnerId:         request.PartnerId,
			PartnerCategoryId: catId,
		})
		cumulativeQuantity, cumulativeMoney, _ := OrderDao.OrderStatics(&domain.OrderStaticQuery{
			EndTime:           utils.GetDayEnd().Unix() * 1000,
			OrderTypes:        domain.UserOrderTypes(domain.Career),
			PartnerId:         request.PartnerId,
			PartnerCategoryId: catId,
		})
		bonus, _ := OrderDao.OrderBonusStatics(domain.OrderBonusQuery{PartnerId: request.PartnerId, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: catId, EndTime: utils.GetDayEnd().Unix() * 1000})
		var percent float64 = 0
		if bonus.Bonus != 0 {
			percent = utils.DecimalToNumber(bonus.Bonus * 100 / total)
		}
		return protocol.OrderStatic{
			Percent:            percent,
			TodayRealQuantity:  todayRealQuantity,
			TodayRealMoney:     utils.Decimal(todayRealMoney),
			CumulativeQuantity: cumulativeQuantity,
			CumulativeMoney:    utils.Decimal(cumulativeMoney),
		}
	}
	task.Parallel(
		func() { rsp.Career = calcOrderStatic(domain.Career, total) },
		func() { rsp.Business = calcOrderStatic(domain.Business, total) },
		func() { rsp.Develop = calcOrderStatic(domain.Develop, total) },
		func() { rsp.App = calcOrderStatic(domain.App, total) },
	)

	err = transactionContext.CommitTransaction()
	// 合并全部的
	rsp.All.CumulativeMoney = rsp.Career.CumulativeMoney + rsp.Business.CumulativeMoney + rsp.Develop.CumulativeMoney + rsp.App.CumulativeMoney
	rsp.All.CumulativeQuantity = rsp.Career.CumulativeQuantity + rsp.Business.CumulativeQuantity + rsp.Develop.CumulativeQuantity + rsp.App.CumulativeQuantity
	rsp.All.Percent = 100
	rsp.All.TodayRealMoney = rsp.Career.TodayRealMoney + rsp.Business.TodayRealMoney + rsp.Develop.TodayRealMoney + rsp.App.TodayRealMoney
	rsp.All.TodayRealQuantity = rsp.Career.TodayRealQuantity + rsp.Business.TodayRealQuantity + rsp.Develop.TodayRealQuantity + rsp.App.TodayRealQuantity
	return
}