order.go
11.6 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package service
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/order/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//OrderService 管理员相关服务
type OrderService struct {
}
func NewOrderService(option map[string]interface{}) *OrderService {
newAdminUserService := new(OrderService)
return newAdminUserService
}
func (service OrderService) PageListOrder(listOrderQuery query.ListOrderQuery) ([]domain.Order, int, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
orderRepository domain.OrderRepository
orders []domain.Order
cnt int
)
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
query := domain.OrderFindQuery{
PartnerId: listOrderQuery.PartnerId,
OrderCode: listOrderQuery.OrderCode,
Offset: listOrderQuery.Offset,
Limit: listOrderQuery.Limit,
OrderType: listOrderQuery.OrderType,
}
orders, err = orderRepository.Find(query)
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
cnt, err = orderRepository.CountAll(query)
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range orders {
var partnerData *domain.PartnerInfo
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: orders[i].PartnerInfo.Id})
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orders[i].PartnerInfo = partnerData.Partner
}
return orders, cnt, nil
}
func (service OrderService) GetOrder(getOrderQuery query.GetOrderQuery) (*domain.Order, error) {
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
orderRepository domain.OrderRepository
order *domain.Order
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
order, err = orderRepository.FindOne(domain.OrderFindOneQuery{
OrderId: getOrderQuery.OrderId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var partnerData *domain.PartnerInfo
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: order.PartnerInfo.Id})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
order.PartnerInfo = partnerData.Partner
err = transactionContext.CommitTransaction()
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return order, nil
}
//CreateOrder 创建意向单
func (service OrderService) CreateOrder(command command.CreateOrderCommand) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = command.ValidateCommand(); err != nil {
return lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var partnerData *domain.PartnerInfo
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: command.PartnerId})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var orderRepository domain.OrderRepository
if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
newOrder := &domain.Order{
//订单类型
OrderType: command.OrderType,
//订单编号
OrderCode: command.OrderCode,
//订单名称
OrderName: command.OrderName,
//订单状态
OrderStatus: domain.OrderStatusReserve,
//数量
OrderCount: command.OrderCount,
//实际数量
OrderActualCount: command.OrderActualCount,
//订单金额
OrderAmount: command.OrderAmount,
//实际订单金额
OrderActualAmount: command.OrderActualAmount,
//订单已支付分红金额(货款)
OrderPaymentAmount: 0,
//订单区域信息
OrderRegionInfo: domain.RegionInfo{
RegionName: command.OrderRegion,
},
//买家
Buyer: domain.Buyer{
BuyerName: command.BuyerName,
ShippingAddress: command.BuyerAddress,
ContactInfo: command.BuyerPhone,
},
PartnerInfo: partnerData.Partner,
//合伙人分红百分比
PartnerBonusPercent: command.PartnerBonusPercent,
//业务员分红百分比
SalesmanBonusPercent: command.SalesmanBonusPercent,
}
err = orderRepository.Save(newOrder)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
//UpdateOrderPurpose 更新意向单
func (service OrderService) UpdateOrderPurpose(command command.UpdateOrderCommand) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = command.ValidateCommand(); err != nil {
return lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
orderRepository domain.OrderRepository
orderData *domain.Order
)
if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderData, err = orderRepository.FindOne(domain.OrderFindOneQuery{
OrderId: command.Id,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderData.OrderType != domain.OrderIntention {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单不是意向单")
}
err = orderData.Update(map[string]interface{}{
//订单编号
"orderCode": command.OrderCode,
"oderName": command.OrderName,
"orderCount": command.OrderCount,
"orderAmount": command.OrderAmount,
"buyer": domain.Buyer{
BuyerName: orderData.Buyer.BuyerName,
ContactInfo: command.BuyerPhone,
ShippingAddress: command.BuyerAddress,
},
"orderRegion": domain.RegionInfo{
RegionName: command.OrderRegion,
},
"partnerBonusPercent": command.PartnerBonusPercent,
"salesmanBonusPercent": command.SalesmanBonusPercent,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
err = orderRepository.Save(orderData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
//RemoveOrder 删除意向单
func (service OrderService) RemoveOrder(id int64) error {
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
orderRepository domain.OrderRepository
order *domain.Order
)
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
order, err = orderRepository.FindOne(domain.OrderFindOneQuery{
OrderId: id,
})
if err != nil {
return lib.ThrowError(lib.RES_NO_FIND_ERROR, err.Error())
}
if order.OrderType != domain.OrderIntention {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单不是意向单")
}
err = orderRepository.Remove(order.Id)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
//UpdateOrderReal 更新为实发单
func (service OrderService) UpdateOrderReal(command command.UpdateOrderRealCommand) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = command.ValidateCommand(); err != nil {
return lib.ThrowError(lib.ARG_ERROR, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
orderRepository domain.OrderRepository
orderData *domain.Order
)
if orderRepository, err = factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderData, err = orderRepository.FindOne(domain.OrderFindOneQuery{
OrderId: command.Id,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = orderData.Update(map[string]interface{}{
//订单编号
"orderCode": command.OrderCode,
"oderName": command.OrderName,
"orderActualCount": command.OrderActualCount,
"orderActualAmount": command.OrderActualAmount,
"buyer": domain.Buyer{
BuyerName: orderData.Buyer.BuyerName,
ContactInfo: command.BuyerPhone,
ShippingAddress: command.BuyerAddress,
},
"orderRegion": domain.RegionInfo{
RegionName: command.OrderRegion,
},
"partnerBonusPercent": command.PartnerBonusPercent,
"salesmanBonusPercent": command.SalesmanBonusPercent,
"orderStatus": command.OrderStatus,
"orderType": domain.OrderReal,
})
if err != nil {
return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
}
err = orderRepository.Save(orderData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}