作者 唐旭辉

更新

@@ -121,31 +121,55 @@ func (subscriber *OrderLogSubscriber) HandleEvent(domainEvent coreDomain.DomainE @@ -121,31 +121,55 @@ func (subscriber *OrderLogSubscriber) HandleEvent(domainEvent coreDomain.DomainE
121 err = orderLogRepository.Add(&orderLog) 121 err = orderLogRepository.Add(&orderLog)
122 break 122 break
123 // 支付订单中货品的分红 123 // 支付订单中货品的分红
124 - case event.PAY_ORDER_GOOD_BONUS_EVENT:  
125 - currentEvent := domainEvent.(event.PayOrderGoodBonus)  
126 - adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})  
127 - if err != nil {  
128 - return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())  
129 - } 124 + // case event.PAY_ORDER_GOOD_BONUS_EVENT:
  125 + // currentEvent := domainEvent.(event.PayOrderGoodBonus)
  126 + // adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
  127 + // if err != nil {
  128 + // return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  129 + // }
  130 + // orderLog := domain.OrderLog{
  131 + // OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
  132 + // OperatorId: currentEvent.AdminId,
  133 + // Operator: adminUser.Name,
  134 + // AlterTime: time.Now(),
  135 + // DataFrom: domain.ORDER_LOG_FROM,
  136 + // LogAction: "支付",
  137 + // OrderId: currentEvent.OrderId,
  138 + // GoodId: currentEvent.GoodId,
  139 + // Descript: []domain.OrderLogDescript{
  140 + // domain.OrderLogDescript{
  141 + // Title: "支付分红",
  142 + // Item: currentEvent.GoodName,
  143 + // Action: []string{
  144 + // fmt.Sprintf(`支付分红"¥%.2f"`, currentEvent.PartnerBonus),
  145 + // },
  146 + // },
  147 + // },
  148 + // }
  149 + // err = orderLogRepository.Add(&orderLog)
  150 + // break
  151 + case event.BATCH_PAY_ORDER_GOOD_BONUS_EVENT:
  152 + currentEvent := domainEvent.(event.BatchPayOrderGoodBonus)
130 orderLog := domain.OrderLog{ 153 orderLog := domain.OrderLog{
131 OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN, 154 OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
132 - OperatorId: currentEvent.AdminId, 155 + OperatorId: currentEvent.Admin.Id,
133 Operator: adminUser.Name, 156 Operator: adminUser.Name,
134 AlterTime: time.Now(), 157 AlterTime: time.Now(),
135 DataFrom: domain.ORDER_LOG_FROM, 158 DataFrom: domain.ORDER_LOG_FROM,
136 LogAction: "支付", 159 LogAction: "支付",
137 - OrderId: currentEvent.OrderId,  
138 - GoodId: currentEvent.GoodId,  
139 - Descript: []domain.OrderLogDescript{  
140 - domain.OrderLogDescript{ 160 + OrderId: currentEvent.OrderBase.Id,
  161 + Descript: []domain.OrderLogDescript{},
  162 + }
  163 + for i := range currentEvent.Goods {
  164 + m := domain.OrderLogDescript{
141 Title: "支付分红", 165 Title: "支付分红",
142 - Item: currentEvent.GoodName, 166 + Item: currentEvent.Goods[i].GoodName,
143 Action: []string{ 167 Action: []string{
144 - fmt.Sprintf(`支付分红"¥%.2f"`, currentEvent.PartnerBonus),  
145 - },  
146 - }, 168 + fmt.Sprintf(`支付分红"¥%.2f"`, currentEvent.Goods[i].GetCurrentPartnerBonus()),
147 }, 169 },
148 } 170 }
  171 + orderLog.Descript = append(orderLog.Descript, m)
  172 + }
149 err = orderLogRepository.Add(&orderLog) 173 err = orderLogRepository.Add(&orderLog)
150 break 174 break
151 } 175 }
@@ -158,5 +182,6 @@ func (subscriber *OrderLogSubscriber) SubscribedToEventTypes() []string { @@ -158,5 +182,6 @@ func (subscriber *OrderLogSubscriber) SubscribedToEventTypes() []string {
158 event.UPDATE_BONUS_BY_PARTENT_BONUS_PERCENT_EVENT, 182 event.UPDATE_BONUS_BY_PARTENT_BONUS_PERCENT_EVENT,
159 event.UPDATE_ORDER_REMARK, 183 event.UPDATE_ORDER_REMARK,
160 event.PAY_ORDER_GOOD_BONUS_EVENT, 184 event.PAY_ORDER_GOOD_BONUS_EVENT,
  185 + event.BATCH_PAY_ORDER_GOOD_BONUS_EVENT,
161 } 186 }
162 } 187 }
@@ -512,3 +512,38 @@ func (service OrderInfoService) UpdateBonusByPartnerBonusPercent(orderId int64, @@ -512,3 +512,38 @@ func (service OrderInfoService) UpdateBonusByPartnerBonusPercent(orderId int64,
512 } 512 }
513 return nil 513 return nil
514 } 514 }
  515 +
  516 +//BatchPayPartnerBonus 批量支付分红
  517 +func (service OrderInfoService) BatchPayPartnerBonus(orderId []int64, adminId int64) error {
  518 + var (
  519 + transactionContext, _ = factory.CreateTransactionContext(nil)
  520 + err error
  521 + )
  522 + if err = transactionContext.StartTransaction(); err != nil {
  523 + return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
  524 + }
  525 + defer func() {
  526 + transactionContext.RollbackTransaction()
  527 + }()
  528 + var (
  529 + orderBonuSrv domainService.OrderBonusService
  530 + )
  531 + orderBonuSrv, err = factory.CreateOrderBonusService(map[string]interface{}{
  532 + "transactionContext": transactionContext,
  533 + })
  534 + if err != nil {
  535 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  536 + }
  537 + orderBonuSrv.Subscribe(&subscriber.OrderLogSubscriber{
  538 + TransactionContext: transactionContext.(*transaction.TransactionContext),
  539 + })
  540 + err = orderBonuSrv.BatchPayPartnerBonus(orderId, adminId)
  541 + if err != nil {
  542 + return err
  543 + }
  544 + err = transactionContext.CommitTransaction()
  545 + if err != nil {
  546 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  547 + }
  548 + return nil
  549 +}
1 package event 1 package event
2 2
  3 +import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
  4 +
3 const ( 5 const (
4 //支付订单中货品的分红 6 //支付订单中货品的分红
5 PAY_ORDER_GOOD_BONUS_EVENT string = "PayOrderGoodBonus" 7 PAY_ORDER_GOOD_BONUS_EVENT string = "PayOrderGoodBonus"
  8 + BATCH_PAY_ORDER_GOOD_BONUS_EVENT string = "BatchPayOrderGoodBonus"
6 ) 9 )
7 10
8 //PayOrderGoodBonus 11 //PayOrderGoodBonus
9 //事件:支付订单中货品的分红 12 //事件:支付订单中货品的分红
10 -type PayOrderGoodBonus struct {  
11 - //订单id  
12 - OrderId int64  
13 - //货品名称  
14 - GoodName string  
15 - //订单中的货品id  
16 - GoodId int64  
17 - //管理员id  
18 - AdminId int64  
19 - //分红额度  
20 - PartnerBonus float64 13 +// type PayOrderGoodBonus struct {
  14 +// //订单id
  15 +// OrderId int64
  16 +// //货品名称
  17 +// GoodName string
  18 +// //订单中的货品id
  19 +// GoodId int64
  20 +// //管理员id
  21 +// AdminId int64
  22 +// //分红额度
  23 +// PartnerBonus float64
  24 +// }
  25 +
  26 +// func (p PayOrderGoodBonus) EventType() string {
  27 +// return PAY_ORDER_GOOD_BONUS_EVENT
  28 +// }
  29 +
  30 +//PayOrderGoodBonus
  31 +//事件:批量支付一个订单中货品的分红
  32 +type BatchPayOrderGoodBonus struct {
  33 + OrderBase *domain.OrderBase
  34 + Goods []domain.OrderGood
  35 + Admin *domain.Users
21 } 36 }
22 37
23 -func (p PayOrderGoodBonus) EventType() string {  
24 - return PAY_ORDER_GOOD_BONUS_EVENT 38 +func (p BatchPayOrderGoodBonus) EventType() string {
  39 + return BATCH_PAY_ORDER_GOOD_BONUS_EVENT
25 } 40 }
@@ -10,4 +10,5 @@ type OrderBonusService interface { @@ -10,4 +10,5 @@ type OrderBonusService interface {
10 UpdateBounsByPartnerBonusPercent(orderId int64, adminId int64, goodId int64, partnerPercent float64, reason string) error 10 UpdateBounsByPartnerBonusPercent(orderId int64, adminId int64, goodId int64, partnerPercent float64, reason string) error
11 UpdateOrderRemarkBonus(orderId int64, adminId int64, remark string) error 11 UpdateOrderRemarkBonus(orderId int64, adminId int64, remark string) error
12 PayOrderGoodBonus(orderId int64, goodId int64, adminId int64) error 12 PayOrderGoodBonus(orderId int64, goodId int64, adminId int64) error
  13 + BatchPayPartnerBonus(orderIds []int64, adminId int64) error
13 } 14 }
@@ -306,12 +306,12 @@ func (serve *OrderBonusService) PayOrderGoodBonus(orderId int64, goodId int64, a @@ -306,12 +306,12 @@ func (serve *OrderBonusService) PayOrderGoodBonus(orderId int64, goodId int64, a
306 return fmt.Errorf("更新订单数据失败,%s", err) 306 return fmt.Errorf("更新订单数据失败,%s", err)
307 } 307 }
308 // 308 //
309 - payEvent := event.PayOrderGoodBonus{  
310 - OrderId: orderId,  
311 - GoodId: goodId,  
312 - AdminId: adminId,  
313 - GoodName: updateGood.GoodName,  
314 - PartnerBonus: updateGood.GetCurrentPartnerBonus(), 309 + payEvent := event.BatchPayOrderGoodBonus{
  310 + OrderBase: oldOrder,
  311 + Goods: []domain.OrderGood{
  312 + updateGood,
  313 + },
  314 + Admin: &adminUser,
315 } 315 }
316 if err = serve.Publish(payEvent); err != nil { 316 if err = serve.Publish(payEvent); err != nil {
317 return err 317 return err
@@ -368,3 +368,87 @@ func (serve *OrderBonusService) UpdateOrderRemarkBonus(orderId int64, adminId in @@ -368,3 +368,87 @@ func (serve *OrderBonusService) UpdateOrderRemarkBonus(orderId int64, adminId in
368 } 368 }
369 return nil 369 return nil
370 } 370 }
  371 +
  372 +//BatchPayPartnerBonus 批量支付订单中货品的分红
  373 +func (serve *OrderBonusService) BatchPayPartnerBonus(orderIds []int64, adminId int64) error {
  374 + var (
  375 + userRepository domain.UsersRepository
  376 + orderBaseReponsitory domain.OrderBaseRepository
  377 + orderGoodRepository domain.OrderGoodRepository
  378 +
  379 + adminUser domain.Users
  380 + err error
  381 + )
  382 + if orderGoodRepository, err = repository.NewOrderGoodRepository(serve.transactionContext); err != nil {
  383 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  384 + }
  385 + if orderBaseReponsitory, err = repository.NewOrderBaseRepository(serve.transactionContext); err != nil {
  386 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  387 + }
  388 + if userRepository, err = repository.NewUsersRepository(serve.transactionContext); err != nil {
  389 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  390 + }
  391 + adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: adminId})
  392 + if err != nil {
  393 + e := fmt.Sprintf("获取管理员用户(id=%d)数据失败,%s", adminId, err)
  394 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
  395 + }
  396 + for _, orderId := range orderIds {
  397 + var oldOrder *domain.OrderBase
  398 + oldOrder, err = orderBaseReponsitory.FindOne(domain.OrderBaseFindOneQuery{OrderId: orderId})
  399 + if err != nil {
  400 + e := fmt.Sprintf("获取订单(id=%d)数据失败,%s", orderId, err)
  401 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
  402 + }
  403 + oldOrder.Goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{OrderId: orderId})
  404 + if err != nil {
  405 + e := fmt.Sprintf("获取订单中(id=%d)的货品数据失败,%s", orderId, err)
  406 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
  407 + }
  408 +
  409 + if ok := adminUser.InCompany(oldOrder.CompanyId); !ok {
  410 + return lib.ThrowError(lib.BUSINESS_ERROR, "用户不能更新非自己公司的订单")
  411 + }
  412 + var updateGoods []domain.OrderGood
  413 + for i := range oldOrder.Goods {
  414 + thisGood := oldOrder.Goods[i]
  415 + if thisGood.BonusStatus == domain.OrderGoodHasPay {
  416 + //已支付
  417 + continue
  418 + }
  419 + if thisGood.PartnerBonusPercent < 0 {
  420 + //未设置合伙人分红比例
  421 + continue
  422 + }
  423 + thisGood.CurrentBonusStatus.PayPartnerBonus(&thisGood)
  424 + oldOrder.Goods[i] = thisGood
  425 + //货品更新
  426 + updateGoods = append(updateGoods, thisGood)
  427 + }
  428 + err = oldOrder.Compute()
  429 + if err != nil {
  430 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "核算订单数据失败")
  431 + }
  432 + //更新订单货品
  433 + if len(updateGoods) > 0 {
  434 + err = orderBaseReponsitory.Save(oldOrder)
  435 + if err != nil {
  436 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  437 + }
  438 + err = orderGoodRepository.Save(updateGoods)
  439 + if err != nil {
  440 + return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
  441 + }
  442 + }
  443 + //构建事件
  444 + payEvent := event.BatchPayOrderGoodBonus{
  445 + OrderBase: oldOrder,
  446 + Goods: updateGoods,
  447 + Admin: &adminUser,
  448 + }
  449 + if err = serve.Publish(payEvent); err != nil {
  450 + return err
  451 + }
  452 + }
  453 + return nil
  454 +}
@@ -514,3 +514,37 @@ func (c *OrderDividendController) ListOrderBonusForExcel() { @@ -514,3 +514,37 @@ func (c *OrderDividendController) ListOrderBonusForExcel() {
514 c.ResponseExcelByFile(c.Ctx, excelMaker) 514 c.ResponseExcelByFile(c.Ctx, excelMaker)
515 return 515 return
516 } 516 }
  517 +
  518 +//BatchPayOrderGoodBonus 批量支付订单中的分红
  519 +func (c *OrderDividendController) BatchPayOrderGoodBonus() {
  520 + type Parameter struct {
  521 + OrderIds []string `json:"orderIds"`
  522 + }
  523 + var (
  524 + param Parameter
  525 + err error
  526 + )
  527 + if err = c.BindJsonData(&param); err != nil {
  528 + logs.Error(err)
  529 + c.ResponseError(errors.New("json数据解析失败"))
  530 + return
  531 + }
  532 + var orderids []int64
  533 + for _, v := range param.OrderIds {
  534 + orderid, _ := strconv.ParseInt(v, 10, 64)
  535 + if orderid == 0 {
  536 + c.ResponseError(errors.New("参数错误"))
  537 + return
  538 + }
  539 + orderids = append(orderids, orderid)
  540 + }
  541 + adminId := c.GetUserId()
  542 + orderSrv := orderService.NewOrderInfoService(nil)
  543 + err = orderSrv.BatchPayPartnerBonus(orderids, adminId)
  544 + if err != nil {
  545 + c.ResponseError(err)
  546 + return
  547 + }
  548 + c.ResponseData(nil)
  549 + return
  550 +}
@@ -32,6 +32,7 @@ func init() { @@ -32,6 +32,7 @@ func init() {
32 // beego.NSRouter("/mini-program/detail", &controllers.OrderDividendController{}, "POST:OrderDividendDetailForBestshop"), 32 // beego.NSRouter("/mini-program/detail", &controllers.OrderDividendController{}, "POST:OrderDividendDetailForBestshop"),
33 //beego.NSRouter("/mini-program/modify", &controllers.OrderDividendController{}, "POST:EditOrderDividendForBestshop"), 33 //beego.NSRouter("/mini-program/modify", &controllers.OrderDividendController{}, "POST:EditOrderDividendForBestshop"),
34 beego.NSRouter("/payDividends", &controllers.OrderDividendController{}, "POST:PayOrderGoodBonus"), 34 beego.NSRouter("/payDividends", &controllers.OrderDividendController{}, "POST:PayOrderGoodBonus"),
  35 + beego.NSRouter("/payDividends/batch", &controllers.OrderDividendController{}, "POST:BatchPayOrderGoodBonus"),
35 beego.NSRouter("/remarks", &controllers.OrderDividendController{}, "POST:EditOrderRemarkBonus"), 36 beego.NSRouter("/remarks", &controllers.OrderDividendController{}, "POST:EditOrderRemarkBonus"),
36 beego.NSRouter("/list/excel", &controllers.OrderDividendController{}, "POST:ListOrderBonusForExcel"), 37 beego.NSRouter("/list/excel", &controllers.OrderDividendController{}, "POST:ListOrderBonusForExcel"),
37 // beego.NSRouter("/business/detail", &controllers.BusinessBonusController{}, "POST:GetBusinessBonus"), 38 // beego.NSRouter("/business/detail", &controllers.BusinessBonusController{}, "POST:GetBusinessBonus"),