dividend.go
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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/pg/models"
"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 DividendStatistics(header *protocol.RequestHeader, request *protocol.DividendStatisticsRequest) (rsp *protocol.DividendStatisticsResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderResponsitory, _ = factory.CreateOrderRepository(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 := OrderResponsitory.Find(utils.ObjectJsonToMap(domain.OrderQueryOption{PartnerId: header.UserId, EndTime: time.Now(), SortByCreateTime: domain.DESC}))
if e != nil {
log.Error(e, beginTime, endTime)
}
var orderBetween []*domain.Order
for i := range orderAll {
if orderAll[i].CreateAt.After(beginTime) && orderAll[i].CreateAt.Before(endTime) {
orderBetween = append(orderBetween, orderAll[i])
}
}
bonusAll := AllBonusStatics(orderAll, request.DividendAction)
bonusQuarters := QuartersBonusStatics(orderBetween, request.DividendAction)
rsp = &protocol.DividendStatisticsResponse{}
rsp.Statistics = protocol.DividendStatistics{
Received: bonusAll.Received,
Outstanding: bonusAll.Outstanding,
Quarters: bonusQuarters,
}
err = transactionContext.CommitTransaction()
return
}
//累计分红
func AllBonusStatics(orders []*domain.Order, action int) (bonus protocol.Bonus) {
bonus = protocol.Bonus{}
for i := range orders {
o := orders[i]
if action == protocol.BonusTotal {
bonus.Received += o.OrderBonusReceive()
bonus.Outstanding += o.OrderBonusWait()
}
if action == protocol.BonusOutstanding {
bonus.Outstanding += o.OrderBonusOutstanding()
}
}
return
}
//订单-季度分红统计
func QuartersBonusStatics(orders []*domain.Order, action int) (bonus []protocol.Bonus) {
bonus = make([]protocol.Bonus, 4)
for i := range orders {
o := orders[i]
quarter := o.CreateAt.Month() / 4
if action == protocol.BonusTotal {
bonus[quarter].Received += o.OrderBonusReceive()
bonus[quarter].Outstanding += o.OrderBonusWait()
}
if action == protocol.BonusOutstanding {
bonus[quarter].Expend += o.OrderBonusOutstanding()
}
}
return
}
//分红订单
func DividendOrders(header *protocol.RequestHeader, request *protocol.DividendOrdersRequest) (rsp *protocol.DividendOrdersResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderDao, _ = factory.CreateOrderDao(transactionContext)
orders []*models.Order
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{
PartnerId: header.UserId,
DetailAction: request.DetailAction,
DividendAction: request.DividendAction,
StartTime: request.StartTime,
EndTime: request.EndTime,
Offset: request.PageIndex * request.PageSize,
Limit: request.PageSize,
})
if err != nil {
return
}
for i := range orders {
o := orders[i]
item := &protocol.DividendOrderListItem{
Id: o.Id,
OrderNo: o.OrderCode,
OrderAmount: o.OrderActualAmount,
MyDividend: utils.Decimal(o.OrderActualAmount * (o.PartnerBonusPercent / 100.0)),
}
rsp.List = append(rsp.List, item)
}
rsp.Total = count
err = transactionContext.CommitTransaction()
return
}