dividend.go 4.0 KB
package dividend

import (
	"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/log"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	"time"
)

// 分红统计
func Statistics(header *protocol.RequestHeader, request *protocol.DividendStatisticsRequest) (rsp *protocol.DividendStatisticsResponse, err error) {
	var (
		transactionContext, _    = factory.CreateTransactionContext(nil)
		OrderBaseResponsitory, _ = factory.CreateOrderBaseRepository(transactionContext)
		//beginTime             = time.Unix(request.StartTime/1000, 0)
		//endTime               = time.Unix(request.EndTime/1000, 0)
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	_, orderAll, e := OrderBaseResponsitory.Find(utils.ObjectJsonToMap(domain.OrderQueryOption{PartnerId: header.UserId, EndTime: time.Now(), SortByCreateTime: domain.DESC, OrderType: domain.OrderReal, IsDisable: "0"}))
	if e != nil {
		log.Error(e)
	}
	var orderBetween []*domain.OrderBase
	for i := range orderAll {
		if orderAll[i].CreateTime.Unix() >= (request.StartTime/1000) && orderAll[i].CreateTime.Unix() < (request.EndTime/1000) {
			orderBetween = append(orderBetween, orderAll[i])
		}
	}
	bonusAll := AllBonusStatics(orderAll, 0)
	bonusQuarters := QuartersBonusStatics(orderBetween, 0)
	rsp = &protocol.DividendStatisticsResponse{}
	rsp.Statistics = protocol.DividendStatistics{
		Received:    bonusAll.Received,
		Outstanding: bonusAll.Outstanding,
		Quarters:    bonusQuarters,
	}
	err = transactionContext.CommitTransaction()
	return
}

// 累计分红
func AllBonusStatics(orders []*domain.OrderBase, action int) (bonus protocol.Bonus) {
	bonus = protocol.Bonus{}
	for i := range orders {
		o := orders[i]
		bonus.Received = utils.Decimal(bonus.Received + o.OrderBonusReceive())
		bonus.Outstanding = utils.Decimal(bonus.Outstanding + o.OrderBonusWait())
	}
	return
}

// 订单-季度分红统计
func QuartersBonusStatics(orders []*domain.OrderBase, action int) (bonus []protocol.Bonus) {
	bonus = make([]protocol.Bonus, 4)
	for i := range orders {
		o := orders[i]
		quarter := (o.CreateTime.Month() - 1) / 3
		bonus[quarter].Received = utils.Decimal(bonus[quarter].Received + o.OrderBonusReceive())
		bonus[quarter].Outstanding = utils.Decimal(bonus[quarter].Outstanding + o.OrderBonusWait())
		bonus[quarter].Expend = utils.Decimal(bonus[quarter].Expend + o.OrderBonusOutstanding())
	}
	return
}

// 分红订单
func OrderList(header *protocol.RequestHeader, request *protocol.DividendOrdersRequest) (rsp *protocol.DividendOrdersResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		OrderDao, _           = factory.CreateOrderBaseDao(transactionContext)
		orders                []*domain.OrderBase
		count                 int
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	rsp = &protocol.DividendOrdersResponse{List: make([]*protocol.DividendOrderListItem, 0)}

	count, orders, err = OrderDao.DividendOrders(&domain.DividendOrdersQueryOption{
		OrderType:        domain.OrderReal,
		PartnerId:        header.UserId,
		DetailAction:     request.DetailAction,
		DividendAction:   request.DividendAction,
		IsDisable:        "0",
		StartTime:        request.StartTime,
		EndTime:          request.EndTime,
		Offset:           request.PageIndex * request.PageSize,
		Limit:            request.PageSize,
		SortByUpdateTime: domain.DESC,
	})
	if err != nil {
		return
	}

	for i := range orders {
		o := orders[i]
		item := &protocol.DividendOrderListItem{
			Id:                 o.Id,
			DeliveryNo:         o.DeliveryCode,
			OrderAmount:        o.OrderAmount(),
			DividendReceivable: utils.Decimal(o.OrderTotalBonus()),
		}
		rsp.List = append(rsp.List, item)
	}
	rsp.Total = count

	err = transactionContext.CommitTransaction()
	return
}