作者 唐旭辉

更新

... ... @@ -121,30 +121,54 @@ func (subscriber *OrderLogSubscriber) HandleEvent(domainEvent coreDomain.DomainE
err = orderLogRepository.Add(&orderLog)
break
// 支付订单中货品的分红
case event.PAY_ORDER_GOOD_BONUS_EVENT:
currentEvent := domainEvent.(event.PayOrderGoodBonus)
adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
// case event.PAY_ORDER_GOOD_BONUS_EVENT:
// currentEvent := domainEvent.(event.PayOrderGoodBonus)
// adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: currentEvent.AdminId})
// if err != nil {
// return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
// }
// orderLog := domain.OrderLog{
// OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
// OperatorId: currentEvent.AdminId,
// Operator: adminUser.Name,
// AlterTime: time.Now(),
// DataFrom: domain.ORDER_LOG_FROM,
// LogAction: "支付",
// OrderId: currentEvent.OrderId,
// GoodId: currentEvent.GoodId,
// Descript: []domain.OrderLogDescript{
// domain.OrderLogDescript{
// Title: "支付分红",
// Item: currentEvent.GoodName,
// Action: []string{
// fmt.Sprintf(`支付分红"¥%.2f"`, currentEvent.PartnerBonus),
// },
// },
// },
// }
// err = orderLogRepository.Add(&orderLog)
// break
case event.BATCH_PAY_ORDER_GOOD_BONUS_EVENT:
currentEvent := domainEvent.(event.BatchPayOrderGoodBonus)
orderLog := domain.OrderLog{
OperatorType: domain.ORDER_LOG_OPERATOR_ADMIN,
OperatorId: currentEvent.AdminId,
OperatorId: currentEvent.Admin.Id,
Operator: adminUser.Name,
AlterTime: time.Now(),
DataFrom: domain.ORDER_LOG_FROM,
LogAction: "支付",
OrderId: currentEvent.OrderId,
GoodId: currentEvent.GoodId,
Descript: []domain.OrderLogDescript{
domain.OrderLogDescript{
Title: "支付分红",
Item: currentEvent.GoodName,
Action: []string{
fmt.Sprintf(`支付分红"¥%.2f"`, currentEvent.PartnerBonus),
},
OrderId: currentEvent.OrderBase.Id,
Descript: []domain.OrderLogDescript{},
}
for i := range currentEvent.Goods {
m := domain.OrderLogDescript{
Title: "支付分红",
Item: currentEvent.Goods[i].GoodName,
Action: []string{
fmt.Sprintf(`支付分红"¥%.2f"`, currentEvent.Goods[i].GetCurrentPartnerBonus()),
},
},
}
orderLog.Descript = append(orderLog.Descript, m)
}
err = orderLogRepository.Add(&orderLog)
break
... ... @@ -158,5 +182,6 @@ func (subscriber *OrderLogSubscriber) SubscribedToEventTypes() []string {
event.UPDATE_BONUS_BY_PARTENT_BONUS_PERCENT_EVENT,
event.UPDATE_ORDER_REMARK,
event.PAY_ORDER_GOOD_BONUS_EVENT,
event.BATCH_PAY_ORDER_GOOD_BONUS_EVENT,
}
}
... ...
... ... @@ -512,3 +512,38 @@ func (service OrderInfoService) UpdateBonusByPartnerBonusPercent(orderId int64,
}
return nil
}
//BatchPayPartnerBonus 批量支付分红
func (service OrderInfoService) BatchPayPartnerBonus(orderId []int64, adminId int64) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
orderBonuSrv domainService.OrderBonusService
)
orderBonuSrv, err = factory.CreateOrderBonusService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
orderBonuSrv.Subscribe(&subscriber.OrderLogSubscriber{
TransactionContext: transactionContext.(*transaction.TransactionContext),
})
err = orderBonuSrv.BatchPayPartnerBonus(orderId, adminId)
if err != nil {
return err
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
... ...
package event
import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
const (
//支付订单中货品的分红
PAY_ORDER_GOOD_BONUS_EVENT string = "PayOrderGoodBonus"
PAY_ORDER_GOOD_BONUS_EVENT string = "PayOrderGoodBonus"
BATCH_PAY_ORDER_GOOD_BONUS_EVENT string = "BatchPayOrderGoodBonus"
)
//PayOrderGoodBonus
//事件:支付订单中货品的分红
type PayOrderGoodBonus struct {
//订单id
OrderId int64
//货品名称
GoodName string
//订单中的货品id
GoodId int64
//管理员id
AdminId int64
//分红额度
PartnerBonus float64
// type PayOrderGoodBonus struct {
// //订单id
// OrderId int64
// //货品名称
// GoodName string
// //订单中的货品id
// GoodId int64
// //管理员id
// AdminId int64
// //分红额度
// PartnerBonus float64
// }
// func (p PayOrderGoodBonus) EventType() string {
// return PAY_ORDER_GOOD_BONUS_EVENT
// }
//PayOrderGoodBonus
//事件:批量支付一个订单中货品的分红
type BatchPayOrderGoodBonus struct {
OrderBase *domain.OrderBase
Goods []domain.OrderGood
Admin *domain.Users
}
func (p PayOrderGoodBonus) EventType() string {
return PAY_ORDER_GOOD_BONUS_EVENT
func (p BatchPayOrderGoodBonus) EventType() string {
return BATCH_PAY_ORDER_GOOD_BONUS_EVENT
}
... ...
... ... @@ -10,4 +10,5 @@ type OrderBonusService interface {
UpdateBounsByPartnerBonusPercent(orderId int64, adminId int64, goodId int64, partnerPercent float64, reason string) error
UpdateOrderRemarkBonus(orderId int64, adminId int64, remark string) error
PayOrderGoodBonus(orderId int64, goodId int64, adminId int64) error
BatchPayPartnerBonus(orderIds []int64, adminId int64) error
}
... ...
... ... @@ -306,12 +306,12 @@ func (serve *OrderBonusService) PayOrderGoodBonus(orderId int64, goodId int64, a
return fmt.Errorf("更新订单数据失败,%s", err)
}
//
payEvent := event.PayOrderGoodBonus{
OrderId: orderId,
GoodId: goodId,
AdminId: adminId,
GoodName: updateGood.GoodName,
PartnerBonus: updateGood.GetCurrentPartnerBonus(),
payEvent := event.BatchPayOrderGoodBonus{
OrderBase: oldOrder,
Goods: []domain.OrderGood{
updateGood,
},
Admin: &adminUser,
}
if err = serve.Publish(payEvent); err != nil {
return err
... ... @@ -368,3 +368,87 @@ func (serve *OrderBonusService) UpdateOrderRemarkBonus(orderId int64, adminId in
}
return nil
}
//BatchPayPartnerBonus 批量支付订单中货品的分红
func (serve *OrderBonusService) BatchPayPartnerBonus(orderIds []int64, adminId int64) error {
var (
userRepository domain.UsersRepository
orderBaseReponsitory domain.OrderBaseRepository
orderGoodRepository domain.OrderGoodRepository
adminUser domain.Users
err error
)
if orderGoodRepository, err = repository.NewOrderGoodRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if orderBaseReponsitory, err = repository.NewOrderBaseRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRepository, err = repository.NewUsersRepository(serve.transactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
adminUser, err = userRepository.FindOne(domain.UsersFindOneQuery{Id: adminId})
if err != nil {
e := fmt.Sprintf("获取管理员用户(id=%d)数据失败,%s", adminId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
for _, orderId := range orderIds {
var oldOrder *domain.OrderBase
oldOrder, err = orderBaseReponsitory.FindOne(domain.OrderBaseFindOneQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单(id=%d)数据失败,%s", orderId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
oldOrder.Goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单中(id=%d)的货品数据失败,%s", orderId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if ok := adminUser.InCompany(oldOrder.CompanyId); !ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "用户不能更新非自己公司的订单")
}
var updateGoods []domain.OrderGood
for i := range oldOrder.Goods {
thisGood := oldOrder.Goods[i]
if thisGood.BonusStatus == domain.OrderGoodHasPay {
//已支付
continue
}
if thisGood.PartnerBonusPercent < 0 {
//未设置合伙人分红比例
continue
}
thisGood.CurrentBonusStatus.PayPartnerBonus(&thisGood)
oldOrder.Goods[i] = thisGood
//货品更新
updateGoods = append(updateGoods, thisGood)
}
err = oldOrder.Compute()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "核算订单数据失败")
}
//更新订单货品
if len(updateGoods) > 0 {
err = orderBaseReponsitory.Save(oldOrder)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = orderGoodRepository.Save(updateGoods)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
}
//构建事件
payEvent := event.BatchPayOrderGoodBonus{
OrderBase: oldOrder,
Goods: updateGoods,
Admin: &adminUser,
}
if err = serve.Publish(payEvent); err != nil {
return err
}
}
return nil
}
... ...
... ... @@ -514,3 +514,37 @@ func (c *OrderDividendController) ListOrderBonusForExcel() {
c.ResponseExcelByFile(c.Ctx, excelMaker)
return
}
//BatchPayOrderGoodBonus 批量支付订单中的分红
func (c *OrderDividendController) BatchPayOrderGoodBonus() {
type Parameter struct {
OrderIds []string `json:"orderIds"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
var orderids []int64
for _, v := range param.OrderIds {
orderid, _ := strconv.ParseInt(v, 10, 64)
if orderid == 0 {
c.ResponseError(errors.New("参数错误"))
return
}
orderids = append(orderids, orderid)
}
adminId := c.GetUserId()
orderSrv := orderService.NewOrderInfoService(nil)
err = orderSrv.BatchPayPartnerBonus(orderids, adminId)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
... ...
... ... @@ -32,6 +32,7 @@ func init() {
// beego.NSRouter("/mini-program/detail", &controllers.OrderDividendController{}, "POST:OrderDividendDetailForBestshop"),
//beego.NSRouter("/mini-program/modify", &controllers.OrderDividendController{}, "POST:EditOrderDividendForBestshop"),
beego.NSRouter("/payDividends", &controllers.OrderDividendController{}, "POST:PayOrderGoodBonus"),
beego.NSRouter("/payDividends/batch", &controllers.OrderDividendController{}, "POST:BatchPayOrderGoodBonus"),
beego.NSRouter("/remarks", &controllers.OrderDividendController{}, "POST:EditOrderRemarkBonus"),
beego.NSRouter("/list/excel", &controllers.OrderDividendController{}, "POST:ListOrderBonusForExcel"),
// beego.NSRouter("/business/detail", &controllers.BusinessBonusController{}, "POST:GetBusinessBonus"),
... ...