|
|
|
1
|
+package service
|
|
|
|
2
|
+
|
|
|
|
3
|
+import (
|
|
|
|
4
|
+ "fmt"
|
|
|
|
5
|
+ "time"
|
|
|
|
6
|
+
|
|
|
|
7
|
+ "github.com/astaxie/beego/logs"
|
|
|
|
8
|
+
|
|
|
|
9
|
+ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
|
|
|
|
10
|
+ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/syncOrder/command"
|
|
|
|
11
|
+ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
|
|
|
|
12
|
+ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
|
|
|
|
13
|
+)
|
|
|
|
14
|
+
|
|
|
|
15
|
+//从其他系统接收订单数据
|
|
|
|
16
|
+const (
|
|
|
|
17
|
+ BEST_SHOP_UNIONID string = "gh_5268964adfe3" //海鲜干货小程序原始id
|
|
|
|
18
|
+)
|
|
|
|
19
|
+
|
|
|
|
20
|
+type SyncOrderService struct {
|
|
|
|
21
|
+}
|
|
|
|
22
|
+
|
|
|
|
23
|
+func NewOrderInfoService(option map[string]interface{}) *SyncOrderService {
|
|
|
|
24
|
+ newService := new(SyncOrderService)
|
|
|
|
25
|
+ return newService
|
|
|
|
26
|
+}
|
|
|
|
27
|
+
|
|
|
|
28
|
+//SyncOrderFromBestshop 接收来源于海鲜干货小程序的订单数据
|
|
|
|
29
|
+func (s SyncOrderService) SyncOrderFromBestshop(cmd command.CreateOrderFromBestshop) error {
|
|
|
|
30
|
+ var (
|
|
|
|
31
|
+ transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
|
32
|
+ err error
|
|
|
|
33
|
+ )
|
|
|
|
34
|
+ if err = transactionContext.StartTransaction(); err != nil {
|
|
|
|
35
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
36
|
+ }
|
|
|
|
37
|
+ defer func() {
|
|
|
|
38
|
+ transactionContext.RollbackTransaction()
|
|
|
|
39
|
+ }()
|
|
|
|
40
|
+ var (
|
|
|
|
41
|
+ orderBestshopRepository domain.OrderBestshopRepository
|
|
|
|
42
|
+ orderGoodBestshopRepository domain.OrderGoodBestshopRepository
|
|
|
|
43
|
+ )
|
|
|
|
44
|
+ if orderBestshopRepository, err = factory.CreateOrderBestshopRepository(map[string]interface{}{
|
|
|
|
45
|
+ "transactionContext": transactionContext,
|
|
|
|
46
|
+ }); err != nil {
|
|
|
|
47
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
48
|
+ }
|
|
|
|
49
|
+ if orderGoodBestshopRepository, err = factory.CreateOrderGoodBestshopRepository(map[string]interface{}{
|
|
|
|
50
|
+ "transactionContext": transactionContext,
|
|
|
|
51
|
+ }); err != nil {
|
|
|
|
52
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
53
|
+ }
|
|
|
|
54
|
+ order := domain.OrderBestShop{
|
|
|
|
55
|
+ OrderCode: cmd.OrderCode,
|
|
|
|
56
|
+ OrderTime: cmd.OrderTime,
|
|
|
|
57
|
+ OrderState: cmd.OrderState,
|
|
|
|
58
|
+ OrderCount: cmd.OrderCount,
|
|
|
|
59
|
+ OrderAmount: cmd.OrderAmount,
|
|
|
|
60
|
+ CreateTime: time.Now(),
|
|
|
|
61
|
+ PartnerId: cmd.PartnerId,
|
|
|
|
62
|
+ BuyerName: cmd.BuyerName,
|
|
|
|
63
|
+ BuyerPhone: cmd.BuyerPhone,
|
|
|
|
64
|
+ BuyerAddress: cmd.BuyerAddress,
|
|
|
|
65
|
+ BuyerRemark: cmd.BuyerRemark,
|
|
|
|
66
|
+ BuyerId: cmd.BuyerId,
|
|
|
|
67
|
+ DeliveryState: cmd.DeliveryState,
|
|
|
|
68
|
+ DeliveryTime: cmd.DeliveryTime,
|
|
|
|
69
|
+ IsCopy: false,
|
|
|
|
70
|
+ }
|
|
|
|
71
|
+ err = orderBestshopRepository.Add(&order)
|
|
|
|
72
|
+ if err != nil {
|
|
|
|
73
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "添加order_bestshop失败:"+err.Error())
|
|
|
|
74
|
+ }
|
|
|
|
75
|
+ goods := []domain.OrderGoodBestShop{}
|
|
|
|
76
|
+ for i := range cmd.Goods {
|
|
|
|
77
|
+ good := domain.OrderGoodBestShop{
|
|
|
|
78
|
+ OrderId: order.Id,
|
|
|
|
79
|
+ Sn: cmd.Goods[i].Sn,
|
|
|
|
80
|
+ Bn: cmd.Goods[i].Bn,
|
|
|
|
81
|
+ Name: cmd.Goods[i].Name,
|
|
|
|
82
|
+ Price: cmd.Goods[i].Price,
|
|
|
|
83
|
+ Nums: cmd.Goods[i].Nums,
|
|
|
|
84
|
+ Amount: cmd.Goods[i].Amount,
|
|
|
|
85
|
+ }
|
|
|
|
86
|
+ err = orderGoodBestshopRepository.Add(&good)
|
|
|
|
87
|
+ if err != nil {
|
|
|
|
88
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "添加order_good失败:"+err.Error())
|
|
|
|
89
|
+ }
|
|
|
|
90
|
+ goods = append(goods, good)
|
|
|
|
91
|
+ }
|
|
|
|
92
|
+ order.Goods = goods
|
|
|
|
93
|
+ err = transactionContext.CommitTransaction()
|
|
|
|
94
|
+ if err != nil {
|
|
|
|
95
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
96
|
+ }
|
|
|
|
97
|
+ err = s.copyOrderBestshopToOrderBase(&order)
|
|
|
|
98
|
+ return err
|
|
|
|
99
|
+}
|
|
|
|
100
|
+
|
|
|
|
101
|
+//copyOrderBestshopToOrderBase 复制数据
|
|
|
|
102
|
+func (s SyncOrderService) copyOrderBestshopToOrderBase(orderBestshop *domain.OrderBestShop) error {
|
|
|
|
103
|
+ var (
|
|
|
|
104
|
+ transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
|
105
|
+ err error
|
|
|
|
106
|
+ )
|
|
|
|
107
|
+ if err = transactionContext.StartTransaction(); err != nil {
|
|
|
|
108
|
+ return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
|
|
|
|
109
|
+ }
|
|
|
|
110
|
+ defer func() {
|
|
|
|
111
|
+ transactionContext.RollbackTransaction()
|
|
|
|
112
|
+ }()
|
|
|
|
113
|
+ var (
|
|
|
|
114
|
+ orderBaseRepository domain.OrderBaseRepository
|
|
|
|
115
|
+ orderGoodRepository domain.OrderGoodRepository
|
|
|
|
116
|
+ orderBestshopRepository domain.OrderBestshopRepository
|
|
|
|
117
|
+ partnerRepository domain.PartnerInfoRepository
|
|
|
|
118
|
+ companyRepository domain.CompanyRepository
|
|
|
|
119
|
+ )
|
|
|
|
120
|
+ if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
|
|
|
|
121
|
+ "transactionContext": transactionContext,
|
|
|
|
122
|
+ }); err != nil {
|
|
|
|
123
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
124
|
+ }
|
|
|
|
125
|
+ if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
|
|
|
|
126
|
+ "transactionContext": transactionContext,
|
|
|
|
127
|
+ }); err != nil {
|
|
|
|
128
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
129
|
+ }
|
|
|
|
130
|
+ if partnerRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
|
131
|
+ "transactionContext": transactionContext,
|
|
|
|
132
|
+ }); err != nil {
|
|
|
|
133
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
134
|
+ }
|
|
|
|
135
|
+ if companyRepository, err = factory.CreateCompanyRepository(map[string]interface{}{
|
|
|
|
136
|
+ "transactionContext": transactionContext,
|
|
|
|
137
|
+ }); err != nil {
|
|
|
|
138
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
139
|
+ }
|
|
|
|
140
|
+ if orderBestshopRepository, err = factory.CreateOrderBestshopRepository(map[string]interface{}{
|
|
|
|
141
|
+ "transactionContext": transactionContext,
|
|
|
|
142
|
+ }); err != nil {
|
|
|
|
143
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
144
|
+ }
|
|
|
|
145
|
+ var (
|
|
|
|
146
|
+ partnerData *domain.PartnerInfo
|
|
|
|
147
|
+ companyData domain.Company
|
|
|
|
148
|
+ canCopyOrder bool
|
|
|
|
149
|
+ )
|
|
|
|
150
|
+ partnerData, err = partnerRepository.FindOne(domain.PartnerFindOneQuery{UserId: orderBestshop.Id})
|
|
|
|
151
|
+ if err != nil {
|
|
|
|
152
|
+ e := fmt.Sprintf("未找到指定的合伙人(id=%d)数据,%s", orderBestshop.PartnerId, err)
|
|
|
|
153
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
|
|
|
|
154
|
+ }
|
|
|
|
155
|
+ companyData, err = companyRepository.FindOne(domain.CompanyFindOneOptions{
|
|
|
|
156
|
+ Id: partnerData.CompanyId,
|
|
|
|
157
|
+ })
|
|
|
|
158
|
+ if err != nil {
|
|
|
|
159
|
+ e := fmt.Sprintf("未找到指定的合伙人的公司(partner_id=%d,company_id=%d)数据,%s", orderBestshop.PartnerId, partnerData.CompanyId, err)
|
|
|
|
160
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
|
|
|
|
161
|
+ }
|
|
|
|
162
|
+ for _, v := range companyData.Applets {
|
|
|
|
163
|
+ if v.Id == BEST_SHOP_UNIONID {
|
|
|
|
164
|
+ canCopyOrder = true
|
|
|
|
165
|
+ }
|
|
|
|
166
|
+ }
|
|
|
|
167
|
+ if !canCopyOrder {
|
|
|
|
168
|
+ logs.Warning("公司未设置海鲜干货的小程序原始id; order_bestshop.id=%d", orderBestshop.Id)
|
|
|
|
169
|
+ return nil
|
|
|
|
170
|
+ }
|
|
|
|
171
|
+ var (
|
|
|
|
172
|
+ orderbase domain.OrderBase
|
|
|
|
173
|
+ ordergoods []domain.OrderGood
|
|
|
|
174
|
+ )
|
|
|
|
175
|
+ //TODO 添加orderBase
|
|
|
|
176
|
+ err = orderBaseRepository.Save(&orderbase)
|
|
|
|
177
|
+ if err != nil {
|
|
|
|
178
|
+ e := fmt.Sprintf("添加order_base数据失败%s", err)
|
|
|
|
179
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
|
|
|
|
180
|
+ }
|
|
|
|
181
|
+ //TODO 添加goods
|
|
|
|
182
|
+ err = orderGoodRepository.Save(ordergoods)
|
|
|
|
183
|
+ if err != nil {
|
|
|
|
184
|
+ e := fmt.Sprintf("添加order_good数据失败%s", err)
|
|
|
|
185
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
|
|
|
|
186
|
+ }
|
|
|
|
187
|
+ //TODO 更新isCopy
|
|
|
|
188
|
+ err = orderBestshopRepository.Edit(orderBestshop)
|
|
|
|
189
|
+ if err != nil {
|
|
|
|
190
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
191
|
+ }
|
|
|
|
192
|
+ err = transactionContext.CommitTransaction()
|
|
|
|
193
|
+ if err != nil {
|
|
|
|
194
|
+ return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
|
195
|
+ }
|
|
|
|
196
|
+ return nil
|
|
|
|
197
|
+} |