作者 tangxvhui

更新:检查订单号,发货号的是否重复

@@ -20,3 +20,11 @@ func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao, @@ -20,3 +20,11 @@ func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao,
20 } 20 }
21 return dao.NewPartnerInfoDao(transactionContext) 21 return dao.NewPartnerInfoDao(transactionContext)
22 } 22 }
  23 +
  24 +func CreateOrderBaseDao(options map[string]interface{}) (*dao.OrderBaseDao, error) {
  25 + var transactionContext *transaction.TransactionContext
  26 + if value, ok := options["transactionContext"]; ok {
  27 + transactionContext = value.(*transaction.TransactionContext)
  28 + }
  29 + return dao.NewOrderBaseDao(transactionContext)
  30 +}
@@ -8,6 +8,7 @@ import ( @@ -8,6 +8,7 @@ import (
8 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/command" 8 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/command"
9 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query" 9 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query"
10 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain" 10 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
11 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib" 12 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
12 ) 13 )
13 14
@@ -162,6 +163,7 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) ( @@ -162,6 +163,7 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (
162 var ( 163 var (
163 orderBaseRepository domain.OrderBaseRepository 164 orderBaseRepository domain.OrderBaseRepository
164 orderGoodRepository domain.OrderGoodRepository 165 orderGoodRepository domain.OrderGoodRepository
  166 + orderBaseDao *dao.OrderBaseDao
165 ) 167 )
166 if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{ 168 if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
167 "transactionContext": transactionContext, 169 "transactionContext": transactionContext,
@@ -173,6 +175,25 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) ( @@ -173,6 +175,25 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (
173 }); err != nil { 175 }); err != nil {
174 return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) 176 return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
175 } 177 }
  178 + if orderBaseDao, err = factory.CreateOrderBaseDao(map[string]interface{}{
  179 + "transactionContext": transactionContext,
  180 + }); err != nil {
  181 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  182 + }
  183 + //检查order_code是否重复
  184 + if ok, err := orderBaseDao.OrderCodeExist(cmd.OrderCode); err != nil {
  185 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  186 + } else if ok {
  187 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单号已存在")
  188 + }
  189 + //检查delivery_code是否重复
  190 + if len(cmd.DeliveryCode) > 0 {
  191 + if ok, err := orderBaseDao.DeliveryCodeExist(cmd.DeliveryCode); err != nil {
  192 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  193 + } else if ok {
  194 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "发货号已存在")
  195 + }
  196 + }
176 newOrder := &domain.OrderBase{ 197 newOrder := &domain.OrderBase{
177 OrderType: cmd.OrderType, OrderCode: cmd.OrderCode, 198 OrderType: cmd.OrderType, OrderCode: cmd.OrderCode,
178 DeliveryCode: cmd.DeliveryCode, 199 DeliveryCode: cmd.DeliveryCode,
@@ -302,6 +323,7 @@ func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand) @@ -302,6 +323,7 @@ func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand)
302 oldOrderGoods []domain.OrderGood 323 oldOrderGoods []domain.OrderGood
303 newOrderGoods []domain.OrderGood 324 newOrderGoods []domain.OrderGood
304 delGoods []int64 325 delGoods []int64
  326 + orderBaseDao *dao.OrderBaseDao
305 ) 327 )
306 if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{ 328 if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
307 "transactionContext": transactionContext, 329 "transactionContext": transactionContext,
@@ -324,6 +346,25 @@ func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand) @@ -324,6 +346,25 @@ func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand)
324 if oldOrderData.OrderType != cmd.OrderType { 346 if oldOrderData.OrderType != cmd.OrderType {
325 return nil, lib.ThrowError(lib.BUSINESS_ERROR, fmt.Sprintf("操作失败,待更新的订单的类型已变更")) 347 return nil, lib.ThrowError(lib.BUSINESS_ERROR, fmt.Sprintf("操作失败,待更新的订单的类型已变更"))
326 } 348 }
  349 + if orderBaseDao, err = factory.CreateOrderBaseDao(map[string]interface{}{
  350 + "transactionContext": transactionContext,
  351 + }); err != nil {
  352 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  353 + }
  354 + //检查order_code是否重复
  355 + if ok, err := orderBaseDao.OrderCodeExist(cmd.OrderCode, cmd.Id); err != nil {
  356 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  357 + } else if ok {
  358 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单号已存在")
  359 + }
  360 + //检查delivery_code是否重复
  361 + if len(cmd.DeliveryCode) > 0 {
  362 + if ok, err := orderBaseDao.DeliveryCodeExist(cmd.DeliveryCode, cmd.Id); err != nil {
  363 + return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  364 + } else if ok {
  365 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "发货号已存在")
  366 + }
  367 + }
327 //获取旧的订单中的商品 368 //获取旧的订单中的商品
328 oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{ 369 oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
329 OrderId: cmd.Id, 370 OrderId: cmd.Id,
@@ -418,7 +459,9 @@ func (service OrderInfoService) Delivery(cmd command.OrderDeliveryCommand) error @@ -418,7 +459,9 @@ func (service OrderInfoService) Delivery(cmd command.OrderDeliveryCommand) error
418 orderGoodRepository domain.OrderGoodRepository 459 orderGoodRepository domain.OrderGoodRepository
419 oldOrderData *domain.OrderBase 460 oldOrderData *domain.OrderBase
420 oldOrderGoods []domain.OrderGood 461 oldOrderGoods []domain.OrderGood
  462 + orderBaseDao *dao.OrderBaseDao
421 ) 463 )
  464 +
422 if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{ 465 if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
423 "transactionContext": transactionContext, 466 "transactionContext": transactionContext,
424 }); err != nil { 467 }); err != nil {
@@ -436,6 +479,20 @@ func (service OrderInfoService) Delivery(cmd command.OrderDeliveryCommand) error @@ -436,6 +479,20 @@ func (service OrderInfoService) Delivery(cmd command.OrderDeliveryCommand) error
436 if err != nil { 479 if err != nil {
437 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单:%s", err)) 480 return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("未找到指定的订单:%s", err))
438 } 481 }
  482 + if oldOrderData.OrderType != domain.OrderIntention {
  483 + return lib.ThrowError(lib.BUSINESS_ERROR, "订单类型已发生变更")
  484 + }
  485 + if orderBaseDao, err = factory.CreateOrderBaseDao(map[string]interface{}{
  486 + "transactionContext": transactionContext,
  487 + }); err != nil {
  488 + return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  489 + }
  490 + //检查delivery_code是否重复
  491 + if ok, err := orderBaseDao.DeliveryCodeExist(cmd.DeliveryCode, cmd.OrderId); err != nil {
  492 + return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  493 + } else if ok {
  494 + return lib.ThrowError(lib.BUSINESS_ERROR, "发货号已存在")
  495 + }
439 //获取旧的订单中的商品 496 //获取旧的订单中的商品
440 oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{ 497 oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
441 OrderId: cmd.OrderId, 498 OrderId: cmd.OrderId,
  1 +package dao
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
  7 + "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
  8 +)
  9 +
  10 +type OrderBaseDao struct {
  11 + transactionContext *transaction.TransactionContext
  12 +}
  13 +
  14 +func NewOrderBaseDao(transactionContext *transaction.TransactionContext) (*OrderBaseDao, error) {
  15 + if transactionContext == nil {
  16 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  17 + } else {
  18 + return &OrderBaseDao{
  19 + transactionContext: transactionContext,
  20 + }, nil
  21 + }
  22 +}
  23 +
  24 +func (dao OrderBaseDao) OrderCodeExist(code string, notId ...int64) (bool, error) {
  25 + tx := dao.transactionContext.PgDd
  26 + m := &models.OrderBase{}
  27 + query := tx.Model(m).Where("order_code=?", code)
  28 + if len(notId) > 0 {
  29 + query = query.WhereIn("id not in(?)", notId)
  30 + }
  31 + ok, err := query.Exists()
  32 + return ok, err
  33 +}
  34 +
  35 +func (dao OrderBaseDao) DeliveryCodeExist(code string, notId ...int64) (bool, error) {
  36 + tx := dao.transactionContext.PgDd
  37 + m := &models.OrderBase{}
  38 + query := tx.Model(m).Where("delivery_code=?", code)
  39 + if len(notId) > 0 {
  40 + query = query.WhereIn("id not in(?)", notId)
  41 + }
  42 + ok, err := query.Exists()
  43 + return ok, err
  44 +}