statistics_v2.go
10.9 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package dividend
import (
"fmt"
"time"
"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"
)
// 分红统计
func StatisticsV2(header *protocol.RequestHeader, request *protocol.DividendStatisticsRequest) (rsp *protocol.DividendStatisticsV2Response, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderBaseResponsitory, _ = factory.CreateOrderBaseRepository(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)
}
err = transactionContext.CommitTransaction()
if err != nil {
return nil, err
}
//获取营销年
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)
// 请求开始时间和结束时间都为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
}
}
fmt.Println("StartTime: ", request.StartTime)
fmt.Println("EndTime: ", request.EndTime)
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])
}
}
//0:全部分类,
//Career int = 1 //事业
//Business int = 2 //业务
//Develop int = 3 //研发
//App int = 4 //业务产品-应用
//累计分红
var (
bonusAll protocol.Bonus
bonusAllCareer protocol.Bonus
bonusAllBusiness protocol.Bonus
bonusAllDevelop protocol.Bonus
bonusAllApp protocol.Bonus
)
for _, val := range orderAll {
static := val.OrderBonusStatic()
bonusAll.Received = utils.Decimal(bonusAll.Received + static.OrderBonusReceive())
bonusAll.Outstanding = utils.Decimal(bonusAll.Outstanding + static.OrderBonusWait())
bonusAll.Receivable = utils.Decimal(bonusAll.Receivable + static.OrderTotalReceivable())
if val.PartnerCategory == nil {
continue
}
switch val.PartnerCategory.Id {
case int64(domain.Career):
bonusAllCareer.Received = utils.Decimal(bonusAllCareer.Received + static.OrderBonusReceive())
bonusAllCareer.Outstanding = utils.Decimal(bonusAllCareer.Outstanding + static.OrderBonusWait())
bonusAllCareer.Receivable = utils.Decimal(bonusAllCareer.Receivable + static.OrderTotalReceivable())
case int64(domain.Business):
bonusAllBusiness.Received = utils.Decimal(bonusAllBusiness.Received + static.OrderBonusReceive())
bonusAllBusiness.Outstanding = utils.Decimal(bonusAllBusiness.Outstanding + static.OrderBonusWait())
bonusAllBusiness.Receivable = utils.Decimal(bonusAllBusiness.Receivable + static.OrderTotalReceivable())
case int64(domain.App):
bonusAllApp.Received = utils.Decimal(bonusAllApp.Received + static.OrderBonusReceive())
bonusAllApp.Outstanding = utils.Decimal(bonusAllApp.Outstanding + static.OrderBonusWait())
bonusAllApp.Receivable = utils.Decimal(bonusAllApp.Receivable + static.OrderTotalReceivable())
case int64(domain.Develop):
bonusAllDevelop.Received = utils.Decimal(bonusAllDevelop.Received + static.OrderBonusReceive())
bonusAllDevelop.Outstanding = utils.Decimal(bonusAllDevelop.Outstanding + static.OrderBonusWait())
bonusAllDevelop.Receivable = utils.Decimal(bonusAllDevelop.Receivable + static.OrderTotalReceivable())
}
}
//0:全部分类,
//Career int = 1 //事业
//Business int = 2 //业务
//Develop int = 3 //研发
//App int = 4 //业务产品-应用
//季度分红
var (
bonusQuarters = [4]protocol.Bonus{}
bonusQuartersCareer = [4]protocol.Bonus{}
bonusQuartersBusiness = [4]protocol.Bonus{}
bonusQuartersDevelop = [4]protocol.Bonus{}
bonusQuartersApp = [4]protocol.Bonus{}
)
//月度分红
var (
bonusMonths = [12]protocol.Bonus{}
bonusMonthsCareer = [12]protocol.Bonus{}
bonusMonthsBusiness = [12]protocol.Bonus{}
bonusMonthsDevelop = [12]protocol.Bonus{}
bonusMonthsApp = [12]protocol.Bonus{}
)
var (
quarterNum int // 对应季度在数组中的位置
monthNum int //对应月份在数组中的位置
)
for _, val := range orderBetween {
quarterNum = quarter(val.SaleDate)
monthNum = int(val.SaleDate.Month()) - 1
static := val.OrderBonusStatic()
bonusQuarters[quarterNum].Receivable = utils.Decimal(bonusQuarters[quarterNum].Receivable + static.OrderTotalReceivable())
bonusQuarters[quarterNum].Received = utils.Decimal(bonusQuarters[quarterNum].Received + static.OrderBonusReceive())
bonusQuarters[quarterNum].Outstanding = utils.Decimal(bonusQuarters[quarterNum].Outstanding + static.OrderBonusWait())
bonusMonths[monthNum].Receivable = utils.Decimal(bonusMonths[monthNum].Receivable + static.OrderTotalReceivable())
bonusMonths[monthNum].Received = utils.Decimal(bonusMonths[monthNum].Received + static.OrderBonusReceive())
bonusMonths[monthNum].Outstanding = utils.Decimal(bonusMonths[monthNum].Outstanding + static.OrderBonusWait())
if val.PartnerCategory == nil {
continue
}
// 分类合并计数
switch val.PartnerCategory.Id {
case int64(domain.Career):
bonusQuartersCareer[quarterNum].Receivable = utils.Decimal(bonusQuartersCareer[quarterNum].Receivable + static.OrderTotalReceivable())
bonusQuartersCareer[quarterNum].Received = utils.Decimal(bonusQuartersCareer[quarterNum].Received + static.OrderBonusReceive())
bonusQuartersCareer[quarterNum].Outstanding = utils.Decimal(bonusQuartersCareer[quarterNum].Outstanding + static.OrderBonusWait())
bonusMonthsCareer[monthNum].Receivable = utils.Decimal(bonusMonthsCareer[monthNum].Receivable + static.OrderTotalReceivable())
bonusMonthsCareer[monthNum].Received = utils.Decimal(bonusMonthsCareer[monthNum].Received + static.OrderBonusReceive())
bonusMonthsCareer[monthNum].Outstanding = utils.Decimal(bonusMonthsCareer[monthNum].Outstanding + static.OrderBonusWait())
case int64(domain.Business):
bonusQuartersBusiness[quarterNum].Receivable = utils.Decimal(bonusQuartersBusiness[quarterNum].Receivable + static.OrderTotalReceivable())
bonusQuartersBusiness[quarterNum].Received = utils.Decimal(bonusQuartersBusiness[quarterNum].Received + static.OrderBonusReceive())
bonusQuartersBusiness[quarterNum].Outstanding = utils.Decimal(bonusQuartersBusiness[quarterNum].Outstanding + static.OrderBonusWait())
bonusMonthsBusiness[monthNum].Receivable = utils.Decimal(bonusMonthsBusiness[monthNum].Receivable + static.OrderTotalReceivable())
bonusMonthsBusiness[monthNum].Received = utils.Decimal(bonusMonthsBusiness[monthNum].Received + static.OrderBonusReceive())
bonusMonthsBusiness[monthNum].Outstanding = utils.Decimal(bonusMonthsBusiness[monthNum].Outstanding + static.OrderBonusWait())
case int64(domain.App):
bonusQuartersApp[quarterNum].Receivable = utils.Decimal(bonusQuartersApp[quarterNum].Receivable + static.OrderTotalReceivable())
bonusQuartersApp[quarterNum].Received = utils.Decimal(bonusQuartersApp[quarterNum].Received + static.OrderBonusReceive())
bonusQuartersApp[quarterNum].Outstanding = utils.Decimal(bonusQuartersApp[quarterNum].Outstanding + static.OrderBonusWait())
bonusMonthsApp[monthNum].Receivable = utils.Decimal(bonusMonthsApp[monthNum].Receivable + static.OrderTotalReceivable())
bonusMonthsApp[monthNum].Received = utils.Decimal(bonusMonthsApp[monthNum].Received + static.OrderBonusReceive())
bonusMonthsApp[monthNum].Outstanding = utils.Decimal(bonusMonthsApp[monthNum].Outstanding + static.OrderBonusWait())
case int64(domain.Develop):
bonusQuartersDevelop[quarterNum].Receivable = utils.Decimal(bonusQuartersDevelop[quarterNum].Receivable + static.OrderTotalReceivable())
bonusQuartersDevelop[quarterNum].Received = utils.Decimal(bonusQuartersDevelop[quarterNum].Received + static.OrderBonusReceive())
bonusQuartersDevelop[quarterNum].Outstanding = utils.Decimal(bonusQuartersDevelop[quarterNum].Outstanding + static.OrderBonusWait())
bonusMonthsDevelop[monthNum].Receivable = utils.Decimal(bonusMonthsDevelop[monthNum].Receivable + static.OrderTotalReceivable())
bonusMonthsDevelop[monthNum].Received = utils.Decimal(bonusMonthsDevelop[monthNum].Received + static.OrderBonusReceive())
bonusMonthsDevelop[monthNum].Outstanding = utils.Decimal(bonusMonthsDevelop[monthNum].Outstanding + static.OrderBonusWait())
}
}
// 整理输出数据
rsp = &protocol.DividendStatisticsV2Response{
Statistics: protocol.DividendStatistics{
Received: bonusAll.Received,
Outstanding: bonusAll.Outstanding,
Receivable: bonusAll.Receivable,
Quarters: bonusQuarters,
Months: bonusMonths,
},
StatisticsCareer: protocol.DividendStatistics{
Received: bonusAllCareer.Receivable,
Outstanding: bonusAllCareer.Outstanding,
Receivable: bonusAllCareer.Receivable,
Quarters: bonusQuartersCareer,
Months: bonusMonthsCareer,
},
StatisticsBusiness: protocol.DividendStatistics{
Received: bonusAllBusiness.Receivable,
Outstanding: bonusAllBusiness.Outstanding,
Receivable: bonusAllBusiness.Receivable,
Quarters: bonusQuartersBusiness,
Months: bonusMonthsBusiness,
},
StatisticsDevelop: protocol.DividendStatistics{
Received: bonusAllDevelop.Received,
Outstanding: bonusAllDevelop.Outstanding,
Receivable: bonusAllDevelop.Receivable,
Quarters: bonusQuartersDevelop,
Months: bonusMonthsDevelop,
},
StatisticsApp: protocol.DividendStatistics{
Received: bonusAllApp.Received,
Outstanding: bonusAllApp.Outstanding,
Receivable: bonusAllApp.Receivable,
Quarters: bonusQuartersApp,
Months: bonusMonthsApp,
},
Timestamp: 0,
}
rsp.Timestamp = time.Now().Unix() * 1000
return rsp, nil
}