正在显示
6 个修改的文件
包含
286 行增加
和
7 行删除
1 | +package command | ||
2 | + | ||
3 | +import "time" | ||
4 | + | ||
5 | +//接收海鲜干货的订单 | ||
6 | +type CreateOrderFromBestshop struct { | ||
7 | + //订单编号 | ||
8 | + OrderCode string `json:"orderCode"` | ||
9 | + //下单时间 | ||
10 | + OrderTime string `json:"orderTime"` | ||
11 | + //订单状态 | ||
12 | + OrderState int8 `json:"orderState"` | ||
13 | + //发货状态 | ||
14 | + DeliveryState int8 `json:"deliveryState"` | ||
15 | + //买家名称 | ||
16 | + BuyerName string `json:"buyerName"` | ||
17 | + BuyerId int64 `json:"buyerId"` | ||
18 | + //买家电话 | ||
19 | + BuyerPhone string `json:"buyerPhone"` | ||
20 | + //买家地址 | ||
21 | + BuyerAddress string `json:"buyerAddress"` | ||
22 | + //买家备注 | ||
23 | + BuyerRemark string `json:"buyerRemark"` | ||
24 | + //商品总数 | ||
25 | + OrderCount int `json:"orderCount"` | ||
26 | + //d订单总额 | ||
27 | + OrderAmount float64 `json:"orderAmount"` | ||
28 | + //发货时间 | ||
29 | + DeliveryTime time.Time `json:"deliveryTime"` | ||
30 | + PartnerId int64 `json:"partnerId"` | ||
31 | + Goods []struct { | ||
32 | + //货品编号 | ||
33 | + Sn string `json:"sn"` | ||
34 | + //商品编号 | ||
35 | + Bn string `json:"bn"` | ||
36 | + //货品名称 | ||
37 | + Name string `json:"name"` | ||
38 | + //单价 | ||
39 | + Price float64 `json:"price"` | ||
40 | + //货品数量 | ||
41 | + Nums int `json:"nums"` | ||
42 | + //订单总价 | ||
43 | + Amount float64 `json:"amount"` | ||
44 | + } `json:"goods"` | ||
45 | +} |
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 | +} |
pkg/application/syncOrder/sync_order.go
已删除
100644 → 0
@@ -10,9 +10,9 @@ type OrderBestShop struct { | @@ -10,9 +10,9 @@ type OrderBestShop struct { | ||
10 | //下单时间 | 10 | //下单时间 |
11 | OrderTime string `json:"orderTime"` | 11 | OrderTime string `json:"orderTime"` |
12 | //订单状态 | 12 | //订单状态 |
13 | - OrderState int8 `json:"order_state"` | 13 | + OrderState int8 `json:"orderState"` |
14 | //发货状态 | 14 | //发货状态 |
15 | - DeliveryState int8 `json:"delivery_state"` | 15 | + DeliveryState int8 `json:"deliveryState"` |
16 | //买家名称 | 16 | //买家名称 |
17 | BuyerName string `json:"buyerName"` | 17 | BuyerName string `json:"buyerName"` |
18 | //买家电话 | 18 | //买家电话 |
@@ -21,6 +21,8 @@ type OrderBestShop struct { | @@ -21,6 +21,8 @@ type OrderBestShop struct { | ||
21 | BuyerAddress string `json:"buyerAddress"` | 21 | BuyerAddress string `json:"buyerAddress"` |
22 | //买家备注 | 22 | //买家备注 |
23 | BuyerRemark string `json:"buyerRemark"` | 23 | BuyerRemark string `json:"buyerRemark"` |
24 | + // | ||
25 | + BuyerId int64 `json:"buyerId"` | ||
24 | //商品总数 | 26 | //商品总数 |
25 | OrderCount int `json:"orderCount"` | 27 | OrderCount int `json:"orderCount"` |
26 | //d订单总额 | 28 | //d订单总额 |
@@ -29,7 +31,10 @@ type OrderBestShop struct { | @@ -29,7 +31,10 @@ type OrderBestShop struct { | ||
29 | DeliveryTime time.Time `json:"deliveryTime"` | 31 | DeliveryTime time.Time `json:"deliveryTime"` |
30 | //创建时间 | 32 | //创建时间 |
31 | CreateTime time.Time `json:"createTime"` | 33 | CreateTime time.Time `json:"createTime"` |
34 | + PartnerId int64 `json:"partnerId"` | ||
32 | Goods []OrderGoodBestShop `json:"goods"` | 35 | Goods []OrderGoodBestShop `json:"goods"` |
36 | + //是否将数据同步到 order_base ,order_good | ||
37 | + IsCopy bool `json:"isCopy"` | ||
33 | } | 38 | } |
34 | 39 | ||
35 | func (order OrderBestShop) CopyToOrderBase(o *OrderBase) { | 40 | func (order OrderBestShop) CopyToOrderBase(o *OrderBase) { |
@@ -56,6 +61,7 @@ type OrderBestshopFindOneQuery struct { | @@ -56,6 +61,7 @@ type OrderBestshopFindOneQuery struct { | ||
56 | 61 | ||
57 | type OrderBestshopRepository interface { | 62 | type OrderBestshopRepository interface { |
58 | Add(order *OrderBestShop) error | 63 | Add(order *OrderBestShop) error |
64 | + Edit(order *OrderBestShop) error | ||
59 | FindOne(qureyOptions OrderBestshopFindOneQuery) (*OrderBestShop, error) | 65 | FindOne(qureyOptions OrderBestshopFindOneQuery) (*OrderBestShop, error) |
60 | } | 66 | } |
61 | 67 |
@@ -21,6 +21,8 @@ type OrderBestshop struct { | @@ -21,6 +21,8 @@ type OrderBestshop struct { | ||
21 | BuyerAddress string | 21 | BuyerAddress string |
22 | //买家备注 | 22 | //买家备注 |
23 | BuyerRemark string | 23 | BuyerRemark string |
24 | + // | ||
25 | + BuyerId int64 | ||
24 | //订单总数 | 26 | //订单总数 |
25 | OrderCount int | 27 | OrderCount int |
26 | //d订单总额 | 28 | //d订单总额 |
@@ -29,4 +31,7 @@ type OrderBestshop struct { | @@ -29,4 +31,7 @@ type OrderBestshop struct { | ||
29 | DeliveryTime time.Time | 31 | DeliveryTime time.Time |
30 | //创建时间 | 32 | //创建时间 |
31 | CreateTime time.Time | 33 | CreateTime time.Time |
34 | + PartnerId int64 | ||
35 | + //是否将数据同步到 order_base ,order_good | ||
36 | + IsCopy bool `pg:",use_zero"` | ||
32 | } | 37 | } |
@@ -35,10 +35,13 @@ func (respository OrderBestshopRepository) transformPgModelToDomainModel(orderMo | @@ -35,10 +35,13 @@ func (respository OrderBestshopRepository) transformPgModelToDomainModel(orderMo | ||
35 | BuyerPhone: orderModel.BuyerPhone, | 35 | BuyerPhone: orderModel.BuyerPhone, |
36 | BuyerAddress: orderModel.BuyerAddress, | 36 | BuyerAddress: orderModel.BuyerAddress, |
37 | BuyerRemark: orderModel.BuyerRemark, | 37 | BuyerRemark: orderModel.BuyerRemark, |
38 | + BuyerId: orderModel.BuyerId, | ||
38 | OrderCount: orderModel.OrderCount, | 39 | OrderCount: orderModel.OrderCount, |
39 | OrderAmount: orderModel.OrderAmount, | 40 | OrderAmount: orderModel.OrderAmount, |
40 | DeliveryTime: orderModel.DeliveryTime, | 41 | DeliveryTime: orderModel.DeliveryTime, |
41 | CreateTime: orderModel.CreateTime, | 42 | CreateTime: orderModel.CreateTime, |
43 | + PartnerId: orderModel.PartnerId, | ||
44 | + IsCopy: orderModel.IsCopy, | ||
42 | }, nil | 45 | }, nil |
43 | } | 46 | } |
44 | 47 | ||
@@ -53,16 +56,44 @@ func (respository OrderBestshopRepository) Add(order *domain.OrderBestShop) erro | @@ -53,16 +56,44 @@ func (respository OrderBestshopRepository) Add(order *domain.OrderBestShop) erro | ||
53 | BuyerPhone: order.BuyerPhone, | 56 | BuyerPhone: order.BuyerPhone, |
54 | BuyerAddress: order.BuyerAddress, | 57 | BuyerAddress: order.BuyerAddress, |
55 | BuyerRemark: order.BuyerRemark, | 58 | BuyerRemark: order.BuyerRemark, |
59 | + BuyerId: order.BuyerId, | ||
56 | OrderCount: order.OrderCount, | 60 | OrderCount: order.OrderCount, |
57 | OrderAmount: order.OrderAmount, | 61 | OrderAmount: order.OrderAmount, |
58 | DeliveryTime: order.DeliveryTime, | 62 | DeliveryTime: order.DeliveryTime, |
59 | CreateTime: time.Now(), | 63 | CreateTime: time.Now(), |
64 | + PartnerId: order.PartnerId, | ||
65 | + IsCopy: order.IsCopy, | ||
60 | } | 66 | } |
61 | _, err := tx.Model(&m).Insert() | 67 | _, err := tx.Model(&m).Insert() |
62 | order.Id = m.Id | 68 | order.Id = m.Id |
63 | return err | 69 | return err |
64 | } | 70 | } |
65 | 71 | ||
72 | +func (respository OrderBestshopRepository) Edit(order *domain.OrderBestShop) error { | ||
73 | + tx := respository.transactionContext.GetDB() | ||
74 | + m := models.OrderBestshop{ | ||
75 | + Id: order.Id, | ||
76 | + OrderCode: order.OrderCode, | ||
77 | + OrderTime: order.OrderTime, | ||
78 | + OrderState: order.OrderState, | ||
79 | + DeliveryState: order.DeliveryState, | ||
80 | + BuyerName: order.BuyerName, | ||
81 | + BuyerPhone: order.BuyerPhone, | ||
82 | + BuyerAddress: order.BuyerAddress, | ||
83 | + BuyerRemark: order.BuyerRemark, | ||
84 | + BuyerId: order.BuyerId, | ||
85 | + OrderCount: order.OrderCount, | ||
86 | + OrderAmount: order.OrderAmount, | ||
87 | + DeliveryTime: order.DeliveryTime, | ||
88 | + CreateTime: order.CreateTime, | ||
89 | + PartnerId: order.PartnerId, | ||
90 | + IsCopy: order.IsCopy, | ||
91 | + } | ||
92 | + _, err := tx.Model(&m).Where("id=?", order.Id).Update() | ||
93 | + order.Id = m.Id | ||
94 | + return err | ||
95 | +} | ||
96 | + | ||
66 | func (respository OrderBestshopRepository) FindOne(queryOption domain.OrderBestshopFindOneQuery) (*domain.OrderBestShop, error) { | 97 | func (respository OrderBestshopRepository) FindOne(queryOption domain.OrderBestshopFindOneQuery) (*domain.OrderBestShop, error) { |
67 | tx := respository.transactionContext.GetDB() | 98 | tx := respository.transactionContext.GetDB() |
68 | m := models.OrderBestshop{} | 99 | m := models.OrderBestshop{} |
-
请 注册 或 登录 后发表评论