作者 陈志颖

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/partnermg into v0.8.0-dev

1 package command 1 package command
2 2
  3 +import "errors"
  4 +
3 //创建订单 5 //创建订单
4 type CreateOrderCommand struct { 6 type CreateOrderCommand struct {
5 //订单类型 7 //订单类型
@@ -22,3 +24,30 @@ type CreateOrderCommand struct { @@ -22,3 +24,30 @@ type CreateOrderCommand struct {
22 24
23 PartnerCategory int64 `json:"partner_category"` 25 PartnerCategory int64 `json:"partner_category"`
24 } 26 }
  27 +
  28 +func (postData *CreateOrderCommand) Valid() error {
  29 + if len(postData.OrderCode) == 0 {
  30 + return errors.New("订单编号必填")
  31 + }
  32 + if len(postData.BuyerName) == 0 {
  33 + return errors.New("买家信息必填")
  34 + }
  35 + if postData.PartnerId == 0 {
  36 + return errors.New("合伙人信息必填")
  37 + }
  38 + if len(postData.OrderRegion) == 0 {
  39 + return errors.New("订单区域必填")
  40 + }
  41 + if len(postData.Goods) == 0 {
  42 + return errors.New("货品列表必填")
  43 + }
  44 + if len(postData.Goods) > 50 {
  45 + return errors.New("货品列表最多50项")
  46 + }
  47 + for i := range postData.Goods {
  48 + if err := postData.Goods[i].Valid(); err != nil {
  49 + return err
  50 + }
  51 + }
  52 + return nil
  53 +}
1 package command 1 package command
2 2
  3 +import (
  4 + "errors"
  5 + "fmt"
  6 + "regexp"
  7 + "unicode/utf8"
  8 +)
  9 +
3 type OrderGoodData struct { 10 type OrderGoodData struct {
4 //货品id 11 //货品id
5 Id int64 `json:"id"` 12 Id int64 `json:"id"`
@@ -14,3 +21,30 @@ type OrderGoodData struct { @@ -14,3 +21,30 @@ type OrderGoodData struct {
14 //备注信息 21 //备注信息
15 Remark string `json:"remark"` 22 Remark string `json:"remark"`
16 } 23 }
  24 +
  25 +func (postData OrderGoodData) Valid() error {
  26 + lenProductName := utf8.RuneCountInString(postData.GoodName)
  27 + if lenProductName == 0 {
  28 + return errors.New("商品名称必填")
  29 + }
  30 + if lenProductName > 50 {
  31 + return errors.New("商品名称最多50位")
  32 + }
  33 + if postData.PlanGoodNumber >= 1e16 {
  34 + return errors.New("商品数量最多16位")
  35 + }
  36 + if postData.Price >= 1e16 {
  37 + return errors.New("商品单价最多16位")
  38 + }
  39 + if postData.PartnerBonusPercent > 100 {
  40 + return errors.New("合伙人分红比例超额")
  41 + }
  42 + partnerRatio := fmt.Sprint(postData.PartnerBonusPercent)
  43 + regexpStr := `^(100|[1-9]\d|\d)(.\d{1,2})?$`
  44 + ok := regexp.MustCompile(regexpStr).MatchString(partnerRatio)
  45 + if !ok {
  46 + return errors.New("合伙人分红比例精确到小数点2位")
  47 + }
  48 +
  49 + return nil
  50 +}
@@ -186,7 +186,9 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) ( @@ -186,7 +186,9 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (
186 transactionContext, _ = factory.CreateTransactionContext(nil) 186 transactionContext, _ = factory.CreateTransactionContext(nil)
187 err error 187 err error
188 ) 188 )
189 - 189 + if err = cmd.Valid(); err != nil {
  190 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
  191 + }
190 if err = transactionContext.StartTransaction(); err != nil { 192 if err = transactionContext.StartTransaction(); err != nil {
191 return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error()) 193 return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
192 } 194 }
@@ -232,7 +234,15 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) ( @@ -232,7 +234,15 @@ func (service OrderInfoService) CreateNewOrder(cmd command.CreateOrderCommand) (
232 } else if ok { 234 } else if ok {
233 return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单已存在") 235 return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单已存在")
234 } 236 }
235 - 237 + //检查货品数据
  238 + var goodMap = map[string]int{}
  239 + for i := range cmd.Goods {
  240 + goodname := cmd.Goods[i].GoodName
  241 + if _, ok := goodMap[goodname]; ok {
  242 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单中货品重复已存在")
  243 + }
  244 + goodMap[goodname] = 1
  245 + }
236 newOrder := &domain.OrderBase{ 246 newOrder := &domain.OrderBase{
237 OrderType: cmd.OrderType, OrderCode: cmd.OrderCode, 247 OrderType: cmd.OrderType, OrderCode: cmd.OrderCode,
238 DeliveryCode: cmd.DeliveryCode, 248 DeliveryCode: cmd.DeliveryCode,
@@ -435,6 +445,15 @@ func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand) @@ -435,6 +445,15 @@ func (service OrderInfoService) UpdateOrderData(cmd command.UpdateOrderCommand)
435 } else if ok { 445 } else if ok {
436 return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单已存在") 446 return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单已存在")
437 } 447 }
  448 + //检查货品数据
  449 + var goodMap = map[string]int{}
  450 + for i := range cmd.Goods {
  451 + goodname := cmd.Goods[i].GoodName
  452 + if _, ok := goodMap[goodname]; ok {
  453 + return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单中货品重复已存在")
  454 + }
  455 + goodMap[goodname] = 1
  456 + }
438 //获取旧的订单中的商品 457 //获取旧的订单中的商品
439 oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{ 458 oldOrderGoods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{
440 OrderId: cmd.Id, 459 OrderId: cmd.Id,