dividend.go 5.1 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)
		BusinessBonusRepository, _ = factory.CreateBusinessBonusRepository(transactionContext)
	)
	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, OrderTypes: domain.UserOrderTypes(domain.Career)}))
	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)

	// 业务分红统计
	if bonus, e := BusinessBonusRepository.FindOne(map[string]interface{}{"partner_id": header.UserId, "isDisable": 1}); e == nil {
		bonusAll.Receivable += bonus.Bonus
		bonusAll.Received += bonus.BonusHas
		bonusAll.Outstanding += bonus.BonusNot
		if bonus.UpdateAt.Unix() >= (request.StartTime/1000) && bonus.UpdateAt.Unix() < (request.EndTime/1000) {
			q := quarter(bonus.UpdateAt)
			bonusQuarters[q].Receivable += bonus.Bonus
			bonusQuarters[q].Received += bonus.BonusHas
			bonusQuarters[q].Outstanding += bonus.BonusNot
		}
	}

	rsp = &protocol.DividendStatisticsResponse{}
	rsp.Statistics = protocol.DividendStatistics{
		Received:    bonusAll.Received,
		Outstanding: bonusAll.Outstanding,
		Receivable:  bonusAll.Receivable,
		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]
		static := o.OrderBonusStatic()
		bonus.Received = utils.Decimal(bonus.Received + static.OrderBonusReceive())
		bonus.Outstanding = utils.Decimal(bonus.Outstanding + static.OrderBonusWait())
		bonus.Receivable = utils.Decimal(bonus.Receivable + static.OrderTotalReceivable())
	}
	return
}

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

// 季度
func quarter(t time.Time) int {
	q := t.Month()
	switch q {
	case 4, 5, 6:
		return 0
	case 7, 8, 9:
		return 1
	case 10, 11, 12:
		return 2
	case 1, 2, 3:
		return 3
	}
	return 0
}

// 分红订单
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{
		OrderTypes:     domain.UserOrderTypes(domain.Career),
		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]
		s := o.OrderBonusStatic()
		item := &protocol.DividendOrderListItem{
			Id:                 o.Id,
			DeliveryNo:         o.DeliveryCode,
			OrderAmount:        s.PlanOrderAmount, //s.OrderAmount(),
			DividendReceivable: utils.Decimal(s.OrderTotalReceivable()),
		}
		rsp.List = append(rsp.List, item)
	}
	rsp.Total = count

	err = transactionContext.CommitTransaction()
	return
}