dividend.go
7.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package dividend
import (
"fmt"
"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: request.PartnerId, EndTime: utils.GetDayEnd(), SortBySalesTime: domain.DESC, OrderTypes: domain.UserOrderTypes(domain.Career)}))
if e != nil {
log.Error(e)
}
//获取营销年
t := time.Now()
var last time.Time
var first time.Time
boundDate := time.Date(t.Year(), time.March, 31, 23, 59, 59, 0, time.Local) // 营销年界限
fmt.Println("营销年界限:", boundDate)
if t.Before(boundDate) || t.Equal(boundDate) {
first = time.Date(t.Year()-1, time.April, 1, 0, 0, 0, 0, time.Local)
last = time.Date(t.Year(), time.March, 31, 23, 59, 59, 0, time.Local)
} else if t.After(boundDate) {
first = time.Date(t.Year(), time.April, 1, 0, 0, 0, 0, time.Local)
last = time.Date(t.Year()+1, time.March, 31, 23, 59, 59, 0, time.Local)
}
fmt.Println("当前营销年起始:", first, last)
//last := time.Date(t.Year(), time.December, 31, 23, 59, 59, 0, time.Local)
//first := time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, time.Local)
//last := first.AddDate(1, 0, 0)
//last := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local)
//last := utils.GetDayEnd()
// 请求开始时间和结束时间都为0时,默认从营销年开始统计
if request.StartTime == 0 && request.EndTime == 0 {
request.StartTime = first.Unix() * 1000
request.EndTime = last.Unix() * 1000
} else if request.StartTime > 0 && request.EndTime > 0 { // 判断结束时间是否超过今天,超过今天的结束时间到今天为止
currentDayEnd := utils.GetDayEnd().Unix() * 1000
if request.EndTime >= currentDayEnd {
request.EndTime = currentDayEnd
}
}
//if request.EndTime > last.Unix()*1000 {
// request.EndTime = last.Unix() * 1000
//}
fmt.Print("StartTime: ", request.StartTime, "\n")
fmt.Print("EndTime: ", request.EndTime, "\n")
var orderBetween []*domain.OrderBase
for i := range orderAll {
if orderAll[i].SaleDate.Unix() >= (request.StartTime/1000) && orderAll[i].SaleDate.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": request.PartnerId, "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,
}
rsp.Timestamp = time.Now().Unix() * 1000
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.SaleDate)
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)}
// 判断时间是否超过今天
currentDayEnd := utils.GetDayEnd().Unix() * 1000
if request.EndTime >= currentDayEnd {
request.EndTime = currentDayEnd
}
count, orders, err = OrderDao.DividendOrders(&domain.DividendOrdersQueryOption{
OrderTypes: domain.UserOrderTypes(domain.Career),
PartnerId: request.PartnerId,
DetailAction: request.DetailAction,
DividendAction: request.DividendAction,
StartTime: request.StartTime,
EndTime: request.EndTime,
Offset: request.PageIndex * request.PageSize,
Limit: request.PageSize,
SortBySalesTime: domain.DESC,
JoinWays: request.JoinWays,
})
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,
DividendReceivable: utils.Decimal(s.OrderTotalReceivable()),
}
rsp.List = append(rsp.List, item)
}
rsp.Total = count
err = transactionContext.CommitTransaction()
return
}