order.go
8.7 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package order
import (
"github.com/tiptok/gocomm/xtime"
"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 Detail(header *protocol.RequestHeader, request *protocol.OrderDetailRequest) (rsp *protocol.OrderDetailResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderResponsitory, _ = factory.CreateOrderBaseRepository(transactionContext)
OrderDao, _ = factory.CreateOrderBaseDao(transactionContext)
order *domain.OrderBase
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
rsp = &protocol.OrderDetailResponse{}
// 订单详情
if order, err = OrderResponsitory.FindOne(utils.ObjectJsonToMap(request)); err != nil {
log.Error(err)
return
}
rsp.Order = protocol.OrderDetail{
Id: order.Id,
OrderNo: order.OrderCode,
DeliveryNo: order.DeliveryCode,
OrderStatus: orderStatus(order),
CreateTime: order.CreateTime.Unix() * 1000,
UpdateTime: order.UpdateTime.Unix() * 1000,
OrderDistrict: map[string]interface{}{"id": order.RegionInfo.RegionId, "name": order.RegionInfo.RegionName},
Customer: protocol.Customer{Uname: order.Buyer.BuyerName, Phone: order.Buyer.ContactInfo},
Products: orderProducts(order),
Total: orderTotalStatic(order),
}
// 更新已读(关联事件)
if header.UserId == order.PartnerId && order.UpdateTime.After(order.LastViewTime) {
if err = OrderDao.UpdateLastViewTime(order.Id, time.Now()); err != nil {
return
}
}
err = transactionContext.CommitTransaction()
return
}
func orderStatus(order *domain.OrderBase) int {
var hasBonus = false
for i := range order.OrderGood {
good := order.OrderGood[i]
if good.BonusStatus == domain.BonusPaid {
hasBonus = true
break
}
}
if hasBonus {
if order.UseOrderCount >= 0 && order.UseOrderCount < order.PlanOrderCount {
return 3 // 已支付退货
}
return 2 // 已支付分红
}
if !hasBonus {
if order.UseOrderCount >= 0 && order.UseOrderCount < order.PlanOrderCount {
return 4 // 待支付退货
}
return 1
}
return 1
}
func orderProducts(order *domain.OrderBase) interface{} {
var products []map[string]interface{}
for i := range order.OrderGood {
good := order.OrderGood[i]
static := good.OrderBonusStatic()
item := make(map[string]interface{})
item["productName"] = good.GoodName
item["orderCount"] = good.PlanGoodNumber
item["orderAmount"] = good.PlanAmount
item["dividendPercent"] = good.PartnerBonusPercent
item["dividendReceivable"] = static.OrderTotalReceivable() //应收分红
item["dividendReceived"] = static.OrderBonusReceive() //已收分红
item["dividendUnReceive"] = static.OrderBonusWait() // 未收分红
item["dividendExpend"] = static.OrderBonusOutstanding() //分红支出
if len(good.Remark) > 0 {
item["orderUpdateReason"] = good.Remark
}
item["dividendStatus"] = good.Status()
if good.Status() > 2 {
item["countAdjust"] = good.UseGoodNumber
item["amountAdjust"] = good.UseAmount
}
products = append(products, item)
}
return products
}
func orderTotalStatic(order *domain.OrderBase) interface{} {
item := make(map[string]interface{})
static := order.OrderBonusStatic()
item["totalCount"] = order.PlanOrderCount
item["totalAmount"] = order.PlanOrderAmount
item["totalDividendReceivable"] = static.OrderTotalReceivable()
item["totalReceived"] = static.OrderBonusReceive()
item["totalUnReceive"] = static.OrderBonusWait()
item["totalExpend"] = static.OrderBonusOutstanding()
if order.UseOrderCount > 0 {
item["totalCountAdjust"] = order.UseOrderCount
item["totalAmountAdjust"] = order.UseOrderAmount
}
return item
}
// 订单统计
func Statistics(header *protocol.RequestHeader, request *protocol.OrderStatisticsRequest) (rsp *protocol.OrderStatisticsResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderDao, _ = factory.CreateOrderBaseDao(transactionContext)
BusinessBonusRepository, _ = factory.CreateBusinessBonusRepository(transactionContext)
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
rsp = &protocol.OrderStatisticsResponse{Statistics: &protocol.OrderStatics{}}
// 当天订单/累计订单
if rsp.Statistics.TodayRealQuantity, rsp.Statistics.TodayRealMoney, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
BeginTime: utils.GetDayBegin().Unix() * 1000,
EndTime: utils.GetDayEnd().Unix() * 1000,
OrderTypes: domain.UserOrderTypes(domain.Career),
PartnerId: header.UserId,
}); err != nil {
return
}
if rsp.Statistics.CumulativeQuantity, rsp.Statistics.CumulativeMoney, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
EndTime: time.Now().Unix() * 1000,
OrderTypes: domain.UserOrderTypes(domain.Career),
PartnerId: header.UserId,
}); err != nil {
return
}
rsp.Statistics.TodayRealMoney = utils.Decimal(rsp.Statistics.TodayRealMoney)
rsp.Statistics.CumulativeMoney = utils.Decimal(rsp.Statistics.CumulativeMoney)
//事业分红/业务分红
var careerBonus, businessBonus, total float64 //,developBonus,appBonus
if bonus, e := OrderDao.OrderBonusStatics(domain.OrderBonusQuery{PartnerId: header.UserId, OrderTypes: domain.UserOrderTypes(domain.Career)}); e == nil {
careerBonus = bonus.Bonus
total += careerBonus
}
if bonus, e := BusinessBonusRepository.FindOne(map[string]interface{}{"partner_id": header.UserId, "isDisable": 1}); e == nil {
businessBonus = bonus.Bonus
total += businessBonus
rsp.Statistics.BusinessMoney = bonus.Bonus
}
if total != 0 {
rsp.Statistics.CareerPercent = utils.DecimalToNumber(careerBonus * 100 / total)
rsp.Statistics.BusinessPercent = utils.DecimalToNumber(businessBonus * 100 / total)
}
err = transactionContext.CommitTransaction()
return
}
// 订单列表
func List(header *protocol.RequestHeader, request *protocol.OrderListRequest) (rsp *protocol.OrderListResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
OrderResponsitory, _ = factory.CreateOrderBaseRepository(transactionContext)
OrderDao, _ = factory.CreateOrderBaseDao(transactionContext)
orders []*domain.OrderBase
total int64
)
rsp = &protocol.OrderListResponse{
List: make([]*protocol.OrderListItem, 0),
}
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
// 查询订单列表
queryOption := &domain.OrderQueryOption{
Limit: request.PageSize,
Offset: request.PageSize * request.PageIndex,
SortByUpdateTime: domain.DESC,
PartnerId: request.PartnerId,
PartnerCategoryId: request.JoinWay,
}
if request.StartTime > 0 {
queryOption.BeginTime = time.Unix(request.StartTime/1000, 0)
}
if request.EndTime > 0 {
queryOption.EndTime = time.Unix(request.EndTime/1000, 0)
}
queryOption.OrderType = request.OrderType
queryOption.OrderTypes = request.OrderTypes
total, orders, _ = OrderResponsitory.Find(utils.ObjectJsonToMap(queryOption))
if len(orders) != 0 {
for i := range orders {
rsp.List = append(rsp.List, DomainOrderToOrderListItem(orders[i]))
}
}
// 查询统计数据
if request.PageIndex == 0 {
var (
cumulativeQuantity int
)
//累计实发订单
cumulativeQuantity, _, err = OrderDao.OrderStatics(&domain.OrderStaticQuery{
EndTime: time.Now().Unix() * 1000,
OrderTypes: domain.UserOrderTypes(domain.Career),
PartnerId: request.PartnerId,
PartnerCategoryId: request.JoinWay,
})
rsp.Total = cumulativeQuantity
}
// 今天累计数量特殊处理,不需要返回所有订单总数(因为要复用rsp.Total字段)
if request.StartTime > 0 && request.EndTime > 0 {
zero := time.Time(xtime.XTime(time.Now()).DayBefore(0)).Unix()
if request.StartTime == (zero * 1000) {
rsp.Total = int(total)
}
}
err = transactionContext.CommitTransaction()
return
}
func DomainOrderToOrderListItem(order *domain.OrderBase) *protocol.OrderListItem {
bonusStatic := order.OrderBonusStatic()
return &protocol.OrderListItem{
Id: order.Id,
OrderType: order.OrderType,
OrderNo: order.OrderCode,
DeliveryNo: order.DeliveryCode,
OrderAmount: order.PlanOrderAmount,
UpdateTime: order.UpdateTime.Unix() * 1000,
MyDividend: bonusStatic.OrderTotalReceivable(),
IsRead: order.IsRead(),
}
}