作者 tangxvhui

校验订单编号的唯一性

... ... @@ -132,6 +132,14 @@ func (service OrderService) CreateOrder(command command.CreateOrderCommand) erro
transactionContext.RollbackTransaction()
}()
orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
ok, err := orderDao.OrderCodeIsExist(command.OrderCode, 0)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")
}
var PartnerInfoRepository domain.PartnerInfoRepository
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
... ... @@ -210,6 +218,14 @@ func (service OrderService) UpdateOrderPurpose(command command.UpdateOrderComman
defer func() {
transactionContext.RollbackTransaction()
}()
orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
ok, err := orderDao.OrderCodeIsExist(command.OrderCode, command.Id)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")
}
var (
orderRepository domain.OrderRepository
orderData *domain.Order
... ... @@ -318,6 +334,14 @@ func (service OrderService) UpdateOrderReal(command command.UpdateOrderRealComma
defer func() {
transactionContext.RollbackTransaction()
}()
orderDao, _ := factory.CreateOrderDao(map[string]interface{}{"transactionContext": transactionContext})
ok, err := orderDao.OrderCodeIsExist(command.OrderCode, command.Id)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单编号已存在")
}
var (
orderRepository domain.OrderRepository
orderData *domain.Order
... ...
... ... @@ -28,11 +28,11 @@ func (dao *OrderDao) Update(options map[string]interface{}) (err error) {
return
}
func (dao *OrderDao) OrderCodeIsExist(code string, orderType int) (bool, error) {
func (dao *OrderDao) OrderCodeIsExist(code string, notId int64) (bool, error) {
tx := dao.transactionContext.PgDd
ok, err := tx.Model(new(models.Order)).
Where("order_code=?", code).
Where("order_type=?", orderType).
Where("id<>?", notId).
Exists()
return ok, err
}
... ...
... ... @@ -70,9 +70,6 @@ func (repository OrderRepository) Save(orderInfo *domain.Order) error {
PartnerId: orderInfo.PartnerInfo.Id,
PartnerBonusPercent: orderInfo.PartnerBonusPercent,
SalesmanBonusPercent: orderInfo.SalesmanBonusPercent,
// CreateAt: orderInfo.CreateAt,
// UpdateAt: orderInfo.UpdateAt,
// LastViewTime: orderInfo.LastViewTime,
}
if m.Id == 0 {
err = tx.Insert(m)
... ...