pg_cancel_dividends_estimate_service.go
4.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
package domain_service
import (
"fmt"
coreDomain "github.com/linmadan/egglib-go/core/domain"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
)
type CancelDividendsEstimateService struct {
coreDomain.BaseEventPublisher
transactionContext *pgTransaction.TransactionContext
}
func (domainService *CancelDividendsEstimateService) CancelEstimate(dividendsEstimateIds []int64, operator *domain.User) ([]*domain.DividendsEstimate, error) {
var dividendsEstimateRepository domain.DividendsEstimateRepository // 分红预算单仓储
var dividendsOrderRepository domain.DividendsOrderRepository // 分红订单仓储
var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository // 分红退货单仓储
// 分红预算单仓储初始化
if repo, err := repository.NewDividendsEstimateRepository(domainService.transactionContext); err != nil {
return nil, err
} else {
dividendsEstimateRepository = repo
}
// 分红订单仓储初始化
if repo, err := repository.NewDividendsOrderRepository(domainService.transactionContext); err != nil {
return nil, err
} else {
dividendsOrderRepository = repo
}
// 分红退后单仓储初始化
if repo, err := repository.NewDividendsReturnedOrderRepository(domainService.transactionContext); err != nil {
return nil, err
} else {
dividendsReturnedOrderRepository = repo
}
// 获取分红预算单
if count, dividendsEstimates, err := dividendsEstimateRepository.Find(map[string]interface{}{
"dividendsEstimateIds": dividendsEstimateIds,
}); err != nil {
return nil, err
} else {
if count > 0 {
var dividendsEstimatesCanceled []*domain.DividendsEstimate
var orderOrReturnOrderNums []string
for _, dividendsEstimate := range dividendsEstimates {
// 根据当前分红预算单的分红订单号或退货单号获取分红预算单
if countRelative, dividendsEstimatesRelative, err2 := dividendsEstimateRepository.Find(map[string]interface{}{
"orderOrReturnedOrderNum": dividendsEstimate.OrderOrReturnedOrderNum,
}); err2 != nil {
return nil, err2
} else {
if countRelative > 0 {
for i, _ := range dividendsEstimatesRelative {
dividendsEstimatesRelative[i].IsCanceled = true
dividendsEstimatesRelative[i].Operator = operator
// 获取相关订单/退货单号
orderOrReturnOrderNums = append(orderOrReturnOrderNums, dividendsEstimatesRelative[i].OrderOrReturnedOrderNum)
}
dividendsEstimatesRelativeCanceled, err3 := dividendsEstimateRepository.UpdateMany(dividendsEstimatesRelative)
if err3 != nil {
return nil, err3
}
dividendsEstimatesCanceled = append(dividendsEstimatesCanceled, dividendsEstimatesRelativeCanceled...)
}
}
}
// 分红订单或分红退货单状态变更
var orderNums []string
var returnedOrderNums []string
// 订单/退货单号数组去重
orderOrReturnOrderNumsWithoutDup := utils.RemoveDuplication(orderOrReturnOrderNums)
for _, orderNullNum := range orderOrReturnOrderNumsWithoutDup {
if orderNullNum[:2] == "RE" { // 分红退后单
returnedOrderNums = append(returnedOrderNums, orderNullNum)
} else if orderNullNum[:2] == "SL" { // 分红订单
orderNums = append(orderNums, orderNullNum)
}
}
// 获取分红订单
if countDividendsOrder, orders, err4 := dividendsOrderRepository.Find(map[string]interface{}{
"dividendsOrderNumbers": orderNums,
}); err4 != nil {
return nil, err4
} else {
if countDividendsOrder > 0 {
for i, _ := range orders {
orders[i].DividendStatus = 1
}
// 保存订单状态
if _, err6 := dividendsOrderRepository.UpdateMany(orders); err6 != nil {
return nil, err6
}
}
}
// 获取分红退货单
if countDividendsReturnedOrder, returnedOrders, err5 := dividendsReturnedOrderRepository.Find(map[string]interface{}{
"dividendsReturnedOrderNumbers": returnedOrderNums,
}); err5 != nil {
return nil, err5
} else {
if countDividendsReturnedOrder > 0 {
for i, _ := range returnedOrders {
returnedOrders[i].DividendStatus = 1
}
// 保存退货单状态
if _, err7 := dividendsReturnedOrderRepository.UpdateMany(returnedOrders); err7 != nil {
return nil, err7
}
}
}
return dividendsEstimatesCanceled, nil
}
}
return nil, nil
}
func NewCancelDividendsEstimateService(transactionContext *pgTransaction.TransactionContext) (*CancelDividendsEstimateService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CancelDividendsEstimateService{
transactionContext: transactionContext,
}, nil
}
}