作者 唐旭辉

更新

@@ -29,6 +29,7 @@ type CreateOrderFromBestshop struct { @@ -29,6 +29,7 @@ type CreateOrderFromBestshop struct {
29 DeliveryTime string `json:"deliveryTime"` 29 DeliveryTime string `json:"deliveryTime"`
30 PartnerId int64 `json:"partnerId"` 30 PartnerId int64 `json:"partnerId"`
31 Goods []struct { 31 Goods []struct {
  32 + Id int64 `json:"id"`
32 //货品编号 33 //货品编号
33 Sn string `json:"sn"` 34 Sn string `json:"sn"`
34 //商品编号 35 //商品编号
@@ -43,25 +43,27 @@ func (s SyncOrderService) SyncOrderFromBestshop(cmd command.CreateOrderFromBests @@ -43,25 +43,27 @@ func (s SyncOrderService) SyncOrderFromBestshop(cmd command.CreateOrderFromBests
43 //检查账号是否存在 43 //检查账号是否存在
44 var ( 44 var (
45 orderBestshopDao *dao.OrderBestshopDao 45 orderBestshopDao *dao.OrderBestshopDao
  46 + orderExist bool
46 ) 47 )
47 if orderBestshopDao, err = factory.CreateOrderBestshopDao(map[string]interface{}{ 48 if orderBestshopDao, err = factory.CreateOrderBestshopDao(map[string]interface{}{
48 "transactionContext": transactionContext, 49 "transactionContext": transactionContext,
49 }); err != nil { 50 }); err != nil {
50 return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error()) 51 return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
51 } 52 }
52 - ok, err := orderBestshopDao.OrderExist(cmd.OrderCode) 53 + orderExist, err = orderBestshopDao.OrderExist(cmd.OrderCode)
53 if err != nil { 54 if err != nil {
54 return lib.ThrowError(lib.TRANSACTION_ERROR, "orderBestshopDao.OrderExist err:"+err.Error()) 55 return lib.ThrowError(lib.TRANSACTION_ERROR, "orderBestshopDao.OrderExist err:"+err.Error())
55 } 56 }
56 - if ok {  
57 - logs.Info("订单已存在,order_code=%s", cmd.OrderCode)  
58 - return nil  
59 - }  
60 err = transactionContext.CommitTransaction() 57 err = transactionContext.CommitTransaction()
61 if err != nil { 58 if err != nil {
62 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) 59 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
63 } 60 }
64 - err = s.CreateOrderFromBestshop(cmd) 61 + if orderExist {
  62 + err = s.UpdateOrderFromBestshop(cmd)
  63 + } else {
  64 + err = s.CreateOrderFromBestshop(cmd)
  65 + }
  66 +
65 return err 67 return err
66 } 68 }
67 69
@@ -115,6 +117,7 @@ func (s SyncOrderService) CreateOrderFromBestshop(cmd command.CreateOrderFromBes @@ -115,6 +117,7 @@ func (s SyncOrderService) CreateOrderFromBestshop(cmd command.CreateOrderFromBes
115 goods := []domain.OrderGoodBestShop{} 117 goods := []domain.OrderGoodBestShop{}
116 for i := range cmd.Goods { 118 for i := range cmd.Goods {
117 good := domain.OrderGoodBestShop{ 119 good := domain.OrderGoodBestShop{
  120 + Id: cmd.Goods[i].Id,
118 OrderId: order.Id, 121 OrderId: order.Id,
119 Sn: cmd.Goods[i].Sn, 122 Sn: cmd.Goods[i].Sn,
120 Bn: cmd.Goods[i].Bn, 123 Bn: cmd.Goods[i].Bn,
@@ -257,90 +260,92 @@ func (s SyncOrderService) copyOrderBestshopToOrderBase(orderBestshop *domain.Ord @@ -257,90 +260,92 @@ func (s SyncOrderService) copyOrderBestshopToOrderBase(orderBestshop *domain.Ord
257 return nil 260 return nil
258 } 261 }
259 262
260 -// func (s SyncOrderService) UpdateOrderFromBestshop(cmd command.CreateOrderFromBestshop) error {  
261 -// var (  
262 -// transactionContext, _ = factory.CreateTransactionContext(nil)  
263 -// err error  
264 -// )  
265 -// if err = transactionContext.StartTransaction(); err != nil {  
266 -// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
267 -// }  
268 -// defer func() {  
269 -// transactionContext.RollbackTransaction()  
270 -// }()  
271 -// var (  
272 -// orderBestshopRepository domain.OrderBestshopRepository  
273 -// orderGoodBestshopRepository domain.OrderGoodBestshopRepository  
274 -// )  
275 -// if orderBestshopRepository, err = factory.CreateOrderBestshopRepository(map[string]interface{}{  
276 -// "transactionContext": transactionContext,  
277 -// }); err != nil {  
278 -// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
279 -// }  
280 -// if orderGoodBestshopRepository, err = factory.CreateOrderGoodBestshopRepository(map[string]interface{}{  
281 -// "transactionContext": transactionContext,  
282 -// }); err != nil {  
283 -// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
284 -// }  
285 -// var (  
286 -// orderData *domain.OrderBestShop  
287 -// orderGoods []domain.OrderGoodBestShop  
288 -// )  
289 -// orderData, err = orderBestshopRepository.FindOne(domain.OrderBestshopFindOneQuery{  
290 -// OrderCode: cmd.OrderCode,  
291 -// })  
292 -// if err != nil {  
293 -// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "获取orderBestshop(order_code=%s)数据失败,err=%s", cmd.OrderCode, err.Error())  
294 -// }  
295 -  
296 -// orderData.OrderCode = cmd.OrderCode  
297 -// orderData.OrderTime = cmd.OrderTime  
298 -// orderData.OrderState = cmd.OrderState  
299 -// orderData.OrderCount = cmd.OrderCount  
300 -// orderData.OrderAmount = cmd.OrderAmount  
301 -// orderData.PartnerId = cmd.PartnerId  
302 -// orderData.BuyerName = cmd.BuyerName  
303 -// orderData.BuyerPhone = cmd.BuyerPhone  
304 -// orderData.BuyerAddress = cmd.BuyerAddress  
305 -// orderData.BuyerRemark = cmd.BuyerRemark  
306 -// orderData.BuyerId = cmd.BuyerId  
307 -// orderData.DeliveryState = cmd.DeliveryState  
308 -// orderData.DeliveryTime = cmd.DeliveryTime  
309 -// orderData.CompanyId = cmd.CompanyId  
310 -// err = orderBestshopRepository.Edit(orderData)  
311 -// if err != nil {  
312 -// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "添加order_bestshop失败:"+err.Error())  
313 -// }  
314 -// orderGoods, err = orderGoodBestshopRepository.Find(domain.OrderGoodBestshopFindQuery{  
315 -// OrderId: orderData.Id,  
316 -// })  
317 -// if err != nil {  
318 -// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "获取orderGoodBestshop(order_id=%s)数据失败,err=%s", orderData.Id, err.Error())  
319 -// } 263 +func (s SyncOrderService) UpdateOrderFromBestshop(cmd command.CreateOrderFromBestshop) error {
  264 + var (
  265 + transactionContext, _ = factory.CreateTransactionContext(nil)
  266 + err error
  267 + )
  268 + if err = transactionContext.StartTransaction(); err != nil {
  269 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  270 + }
  271 + defer func() {
  272 + transactionContext.RollbackTransaction()
  273 + }()
  274 + var (
  275 + orderBestshopRepository domain.OrderBestshopRepository
  276 + orderGoodBestshopRepository domain.OrderGoodBestshopRepository
  277 + )
  278 + if orderBestshopRepository, err = factory.CreateOrderBestshopRepository(map[string]interface{}{
  279 + "transactionContext": transactionContext,
  280 + }); err != nil {
  281 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  282 + }
  283 + if orderGoodBestshopRepository, err = factory.CreateOrderGoodBestshopRepository(map[string]interface{}{
  284 + "transactionContext": transactionContext,
  285 + }); err != nil {
  286 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  287 + }
  288 + var (
  289 + orderData *domain.OrderBestShop
  290 + orderGoods []domain.OrderGoodBestShop
  291 + )
  292 + orderData, err = orderBestshopRepository.FindOne(domain.OrderBestshopFindOneQuery{
  293 + OrderCode: cmd.OrderCode,
  294 + })
  295 + if err != nil {
  296 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "获取orderBestshop(order_code=%s)数据失败,err=%s", cmd.OrderCode, err.Error())
  297 + }
320 298
321 -// for i := range orderGoods {  
322 -// var updated bool  
323 -// for ii := range cmd.Goods {  
324 -// if cmd.Goods[ii].Sn != orderGoods[i].Sn {  
325 -// continue  
326 -// }  
327 -// good := orderGoods[i]  
328 -// good.Sn= cmd.Goods[i].Sn,  
329 -// good.Bn= cmd.Goods[i].Bn,  
330 -// good.Name= cmd.Goods[i].Name,  
331 -// good.Price cmd.Goods[i].Price,  
332 -// good.Nums: cmd.Goods[i].Nums,  
333 -// good.Amount: cmd.Goods[i].Amount,  
334 -// }  
335 -// err = orderGoodBestshopRepository.Edit(&orderGoods[i])  
336 -// if err != nil {  
337 -// logs.Error("更新order_good_bestshop失败:" + err.Error()) 299 + orderData.OrderCode = cmd.OrderCode
  300 + orderData.OrderTime = cmd.OrderTime
  301 + orderData.OrderState = cmd.OrderState
  302 + orderData.OrderCount = cmd.OrderCount
  303 + orderData.OrderAmount = cmd.OrderAmount
  304 + orderData.PartnerId = cmd.PartnerId
  305 + orderData.BuyerName = cmd.BuyerName
  306 + orderData.BuyerPhone = cmd.BuyerPhone
  307 + orderData.BuyerAddress = cmd.BuyerAddress
  308 + orderData.BuyerRemark = cmd.BuyerRemark
  309 + orderData.BuyerId = cmd.BuyerId
  310 + orderData.DeliveryState = cmd.DeliveryState
  311 + orderData.DeliveryTime = cmd.DeliveryTime
  312 + orderData.CompanyId = cmd.CompanyId
  313 + err = orderBestshopRepository.Edit(orderData)
  314 + if err != nil {
  315 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "添加order_bestshop失败:"+err.Error())
  316 + }
  317 + orderGoods, err = orderGoodBestshopRepository.Find(domain.OrderGoodBestshopFindQuery{
  318 + OrderId: orderData.Id,
  319 + })
  320 + if err != nil {
  321 + e := fmt.Sprintf("获取orderGoodBestshop(order_id=%d)数据失败,err=%s", orderData.Id, err.Error())
  322 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
  323 + }
338 324
339 -// }  
340 -// }  
341 -// err = transactionContext.CommitTransaction()  
342 -// if err != nil {  
343 -// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
344 -// }  
345 -// return nil  
346 -// } 325 + for i := range orderGoods {
  326 + var updated bool
  327 + for ii := range cmd.Goods {
  328 + if cmd.Goods[ii].Id != orderGoods[i].Id {
  329 + continue
  330 + }
  331 + orderGoods[i].Sn = cmd.Goods[ii].Sn
  332 + orderGoods[i].Bn = cmd.Goods[ii].Bn
  333 + orderGoods[i].Name = cmd.Goods[ii].Name
  334 + orderGoods[i].Price = cmd.Goods[ii].Price
  335 + orderGoods[i].Nums = cmd.Goods[ii].Nums
  336 + orderGoods[i].Amount = cmd.Goods[ii].Amount
  337 + updated = true
  338 + }
  339 + if updated {
  340 + err = orderGoodBestshopRepository.Edit(&orderGoods[i])
  341 + if err != nil {
  342 + logs.Error("更新order_good_bestshop失败:" + err.Error())
  343 + }
  344 + }
  345 + }
  346 + err = transactionContext.CommitTransaction()
  347 + if err != nil {
  348 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  349 + }
  350 + return nil
  351 +}