作者 唐旭辉

更新

... ... @@ -29,6 +29,7 @@ type CreateOrderFromBestshop struct {
DeliveryTime string `json:"deliveryTime"`
PartnerId int64 `json:"partnerId"`
Goods []struct {
Id int64 `json:"id"`
//货品编号
Sn string `json:"sn"`
//商品编号
... ...
... ... @@ -43,25 +43,27 @@ func (s SyncOrderService) SyncOrderFromBestshop(cmd command.CreateOrderFromBests
//检查账号是否存在
var (
orderBestshopDao *dao.OrderBestshopDao
orderExist bool
)
if orderBestshopDao, err = factory.CreateOrderBestshopDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
ok, err := orderBestshopDao.OrderExist(cmd.OrderCode)
orderExist, err = orderBestshopDao.OrderExist(cmd.OrderCode)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, "orderBestshopDao.OrderExist err:"+err.Error())
}
if ok {
logs.Info("订单已存在,order_code=%s", cmd.OrderCode)
return nil
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = s.CreateOrderFromBestshop(cmd)
if orderExist {
err = s.UpdateOrderFromBestshop(cmd)
} else {
err = s.CreateOrderFromBestshop(cmd)
}
return err
}
... ... @@ -115,6 +117,7 @@ func (s SyncOrderService) CreateOrderFromBestshop(cmd command.CreateOrderFromBes
goods := []domain.OrderGoodBestShop{}
for i := range cmd.Goods {
good := domain.OrderGoodBestShop{
Id: cmd.Goods[i].Id,
OrderId: order.Id,
Sn: cmd.Goods[i].Sn,
Bn: cmd.Goods[i].Bn,
... ... @@ -257,90 +260,92 @@ func (s SyncOrderService) copyOrderBestshopToOrderBase(orderBestshop *domain.Ord
return nil
}
// func (s SyncOrderService) UpdateOrderFromBestshop(cmd command.CreateOrderFromBestshop) error {
// var (
// transactionContext, _ = factory.CreateTransactionContext(nil)
// err error
// )
// if err = transactionContext.StartTransaction(); err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
// var (
// orderBestshopRepository domain.OrderBestshopRepository
// orderGoodBestshopRepository domain.OrderGoodBestshopRepository
// )
// if orderBestshopRepository, err = factory.CreateOrderBestshopRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// }); err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// if orderGoodBestshopRepository, err = factory.CreateOrderGoodBestshopRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// }); err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// var (
// orderData *domain.OrderBestShop
// orderGoods []domain.OrderGoodBestShop
// )
// orderData, err = orderBestshopRepository.FindOne(domain.OrderBestshopFindOneQuery{
// OrderCode: cmd.OrderCode,
// })
// if err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "获取orderBestshop(order_code=%s)数据失败,err=%s", cmd.OrderCode, err.Error())
// }
// orderData.OrderCode = cmd.OrderCode
// orderData.OrderTime = cmd.OrderTime
// orderData.OrderState = cmd.OrderState
// orderData.OrderCount = cmd.OrderCount
// orderData.OrderAmount = cmd.OrderAmount
// orderData.PartnerId = cmd.PartnerId
// orderData.BuyerName = cmd.BuyerName
// orderData.BuyerPhone = cmd.BuyerPhone
// orderData.BuyerAddress = cmd.BuyerAddress
// orderData.BuyerRemark = cmd.BuyerRemark
// orderData.BuyerId = cmd.BuyerId
// orderData.DeliveryState = cmd.DeliveryState
// orderData.DeliveryTime = cmd.DeliveryTime
// orderData.CompanyId = cmd.CompanyId
// err = orderBestshopRepository.Edit(orderData)
// if err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "添加order_bestshop失败:"+err.Error())
// }
// orderGoods, err = orderGoodBestshopRepository.Find(domain.OrderGoodBestshopFindQuery{
// OrderId: orderData.Id,
// })
// if err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "获取orderGoodBestshop(order_id=%s)数据失败,err=%s", orderData.Id, err.Error())
// }
func (s SyncOrderService) UpdateOrderFromBestshop(cmd command.CreateOrderFromBestshop) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
orderBestshopRepository domain.OrderBestshopRepository
orderGoodBestshopRepository domain.OrderGoodBestshopRepository
)
if orderBestshopRepository, err = factory.CreateOrderBestshopRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderGoodBestshopRepository, err = factory.CreateOrderGoodBestshopRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var (
orderData *domain.OrderBestShop
orderGoods []domain.OrderGoodBestShop
)
orderData, err = orderBestshopRepository.FindOne(domain.OrderBestshopFindOneQuery{
OrderCode: cmd.OrderCode,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "获取orderBestshop(order_code=%s)数据失败,err=%s", cmd.OrderCode, err.Error())
}
// for i := range orderGoods {
// var updated bool
// for ii := range cmd.Goods {
// if cmd.Goods[ii].Sn != orderGoods[i].Sn {
// continue
// }
// good := orderGoods[i]
// good.Sn= cmd.Goods[i].Sn,
// good.Bn= cmd.Goods[i].Bn,
// good.Name= cmd.Goods[i].Name,
// good.Price cmd.Goods[i].Price,
// good.Nums: cmd.Goods[i].Nums,
// good.Amount: cmd.Goods[i].Amount,
// }
// err = orderGoodBestshopRepository.Edit(&orderGoods[i])
// if err != nil {
// logs.Error("更新order_good_bestshop失败:" + err.Error())
orderData.OrderCode = cmd.OrderCode
orderData.OrderTime = cmd.OrderTime
orderData.OrderState = cmd.OrderState
orderData.OrderCount = cmd.OrderCount
orderData.OrderAmount = cmd.OrderAmount
orderData.PartnerId = cmd.PartnerId
orderData.BuyerName = cmd.BuyerName
orderData.BuyerPhone = cmd.BuyerPhone
orderData.BuyerAddress = cmd.BuyerAddress
orderData.BuyerRemark = cmd.BuyerRemark
orderData.BuyerId = cmd.BuyerId
orderData.DeliveryState = cmd.DeliveryState
orderData.DeliveryTime = cmd.DeliveryTime
orderData.CompanyId = cmd.CompanyId
err = orderBestshopRepository.Edit(orderData)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "添加order_bestshop失败:"+err.Error())
}
orderGoods, err = orderGoodBestshopRepository.Find(domain.OrderGoodBestshopFindQuery{
OrderId: orderData.Id,
})
if err != nil {
e := fmt.Sprintf("获取orderGoodBestshop(order_id=%d)数据失败,err=%s", orderData.Id, err.Error())
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
// }
// }
// err = transactionContext.CommitTransaction()
// if err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// return nil
// }
for i := range orderGoods {
var updated bool
for ii := range cmd.Goods {
if cmd.Goods[ii].Id != orderGoods[i].Id {
continue
}
orderGoods[i].Sn = cmd.Goods[ii].Sn
orderGoods[i].Bn = cmd.Goods[ii].Bn
orderGoods[i].Name = cmd.Goods[ii].Name
orderGoods[i].Price = cmd.Goods[ii].Price
orderGoods[i].Nums = cmd.Goods[ii].Nums
orderGoods[i].Amount = cmd.Goods[ii].Amount
updated = true
}
if updated {
err = orderGoodBestshopRepository.Edit(&orderGoods[i])
if err != nil {
logs.Error("更新order_good_bestshop失败:" + err.Error())
}
}
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
... ...