作者 唐旭辉

订单详情里各种操作,完成

package subscriber
import (
"github.com/linmadan/egglib-go/core/domain"
"fmt"
"time"
coreDomain "github.com/linmadan/egglib-go/core/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain/event"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//订单数据修改触发的订阅事件
type OrderLogSubscriber struct {
transactionContext *transaction.TransactionContext
TransactionContext *transaction.TransactionContext
}
var _ domain.DomainEventSubscriber = (*OrderLogSubscriber)(nil)
var _ coreDomain.DomainEventSubscriber = (*OrderLogSubscriber)(nil)
func (subscriber *OrderLogSubscriber) HandleEvent(domainEvent coreDomain.DomainEvent) error {
var (
orderLogRepository domain.OrderLogRepository
userRepository domain.UsersRepository
adminUser domain.Users
err error
)
if orderLogRepository, err = repository.NewOrderLogRepository(subscriber.TransactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
if userRepository, err = repository.NewUsersRepository(subscriber.TransactionContext); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
func (subscriber *OrderLogSubscriber) HandleEvent(domainEvent domain.DomainEvent) error {
switch domainEvent.EventType() {
//订单分红因为货品的数量变动而发送改变
case event.UPDATE_BONUS_BY_GOOD_NUMBER_EVENT:
currentEvent := domainEvent.(event.UpdateBonusByGoodNumber)
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("购买数量由%s调整为%s", currentEvent.FormerNumber, currentEvent.NewNumber),
fmt.Sprintf("商品总价由¥%s调整为¥%s", currentEvent.FormerAmount, currentEvent.NewAmount),
},
},
},
}
err = orderLogRepository.Add(&orderLog)
break
//订单分红因为合伙人分红比例变动而发送改变
case event.UPDATE_BONUS_BY_PARTENT_BONUS_PERCENT_EVENT:
currentEvent := domainEvent.(event.UpdateBounsByPartnerBonusPercent)
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("分红比例由%s调整为%s", currentEvent.FormerPartnerBonusPercent, currentEvent.NewPartnerBonusPercent),
fmt.Sprintf("应收分红由¥%s调整为¥%s", currentEvent.FormerPartnerBonus, currentEvent.NewPartnerBonus),
},
},
},
}
err = orderLogRepository.Add(&orderLog)
break
//更新订单的备注
case event.UPDATE_ORDER_REMARK:
currentEvent := domainEvent.(event.UpdateOrderRemark)
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: 0,
Descript: []domain.OrderLogDescript{
domain.OrderLogDescript{
Title: "编辑备注",
Item: currentEvent.NewRemark,
Action: []string{},
},
},
}
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())
}
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
}
return nil
return err
}
func (subscriber *OrderLogSubscriber) SubscribedToEventTypes() []string {
... ...
... ... @@ -13,3 +13,11 @@ func CreateBusinessBonusService(options map[string]interface{}) (service.Busines
}
return domainService.NewBusinessBonusService(transactionContext), nil
}
func CreateOrderBonusService(options map[string]interface{}) (service.OrderBonusService, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return domainService.NewOrderBonusService(transactionContext), nil
}
... ...
... ... @@ -7,12 +7,15 @@ import (
"github.com/astaxie/beego/logs"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/event/subscriber"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/orderinfo/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
domainService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain/service"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
... ... @@ -976,23 +979,39 @@ func (service OrderInfoService) buildOrderBestshopInfoData(orderBase *domain.Ord
"order": order,
"product": product,
"modify": modifyLog,
"remark": orderBase.Remark.RemarkBonus,
}
return result
}
//UpdateBounsWithGoodNumber 分红时,因修改订单中商品的数量发生分红变动
func (service OrderInfoService) UpdateBonusByGoodNumber() error {
func (service OrderInfoService) UpdateBonusByGoodNumber(orderId int64, goodId int64, adminId int64, goodNumber int, reason string) error {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
err error
)
if err = transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
//TODO
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.UpdateBounsByGoodNumber(orderId, goodId, adminId, goodNumber, reason)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -1001,18 +1020,103 @@ func (service OrderInfoService) UpdateBonusByGoodNumber() error {
}
//UpdateBounsByPartnerBonusPercent 分红时,因修改订单中商品的合伙人分行比例发生分红变动
func (service OrderInfoService) UpdateBonusByPartnerBonusPercent() error {
func (service OrderInfoService) UpdateBonusByPartnerBonusPercent(orderId int64, goodId int64, adminId int64, percent float64, reason string) 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.UpdateBounsByPartnerBonusPercent(orderId, goodId, adminId, percent, reason)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
//PayPartnerBonusWithOrderBestshop 支付分红,海鲜干货的订单 (orderType=domain.OrderTypeBestShop)
func (service OrderInfoService) PayPartnerBonusWithOrderBestshop(orderId int64, goodId 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.PayOrderGoodBonus(orderId, goodId, adminId)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return nil
}
//PayPartnerBonusWithOrderBestshop 支付分红,海鲜干货的订单 (orderType=domain.OrderTypeBestShop)
func (service OrderInfoService) UpdateOrderRemarkBonus(orderId int64, adminId int64, remark string) 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()
}()
//TODO
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.UpdateOrderRemarkBonus(orderId, adminId, remark)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
err = transactionContext.CommitTransaction()
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
... ...
... ... @@ -10,10 +10,14 @@ const (
type PayOrderGoodBonus struct {
//订单id
OrderId int64
//货品名称
GoodName string
//订单中的货品id
GoodId int64
//管理员id
AdminId int64
//
PartnerBonus float64
}
func (p PayOrderGoodBonus) EventType() string {
... ...
... ... @@ -24,8 +24,9 @@ type OrderLogDescript struct {
//OrderLog 订单修改记录
type OrderLog struct {
Id int64
OrderId int64 `json:"order_id"` //订单id
AlterTime time.Time `json:"alter_time"` //时间
OrderId int64 `json:"orderId"` //订单id
GoodId int64 `json:"goodId"`
AlterTime time.Time `json:"alterTime"` //时间
Operator string `json:"operator"` //操作人员
OperatorId int64 `json:"operatorId"` //操作人员Id
OperatorType string `json:"operatorType"` //操作人员的类型
... ...
package service
import (
coreDomain "github.com/linmadan/egglib-go/core/domain"
)
type OrderBonusService interface {
UpdateBounsByGoodNumber(orderId int64, adminId int64, goodWithNumber int) error
UpdateBounsByPartnerBonusPercent(orderId int64, adminId int64, partnerPercent float64) error
UpdateOrderRemark(orderId int64, adminId int64, remark string) error
coreDomain.DomainEventPublisher
UpdateBounsByGoodNumber(orderId int64, adminId int64, goodid int64, goodWithNumber int, reason string) error
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
}
... ...
package domainService
import (
"errors"
"fmt"
coreDomain "github.com/linmadan/egglib-go/core/domain"
... ... @@ -13,15 +14,22 @@ import (
)
//OrderBonusServices 处理订单分红的相关操作
type OrderBonusServices struct {
type OrderBonusService struct {
coreDomain.BaseEventPublisher
transactionContext *transaction.TransactionContext
}
var _ service.OrderBonusService = (*OrderBonusServices)(nil)
var _ service.OrderBonusService = (*OrderBonusService)(nil)
func NewOrderBonusService(tcx *transaction.TransactionContext) *OrderBonusService {
return &OrderBonusService{
transactionContext: tcx,
}
}
//UpdateBounsWithGoodNumber 分红时,因修改订单中商品的数量发生分红变动
func (serve *OrderBonusServices) UpdateBounsByGoodNumber(orderId int64, adminId int64, goodWithNumber int) error {
//目前只处理 海鲜干货的订单 即 order_type = OrderTypeBestShop (3)
func (serve *OrderBonusService) UpdateBounsByGoodNumber(orderId int64, adminId int64, goodId int64, goodWithNumber int, reason string) error {
var (
userRepository domain.UsersRepository
orderBaseReponsitory domain.OrderBaseRepository
... ... @@ -44,6 +52,9 @@ func (serve *OrderBonusServices) UpdateBounsByGoodNumber(orderId int64, adminId
e := fmt.Sprintf("获取订单(id=%d)数据失败,%s", orderId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if oldOrder.OrderType != domain.OrderTypeBestShop {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单类型错误")
}
oldOrder.Goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单(id=%d)中的货品数据失败,%s", orderId, err)
... ... @@ -54,22 +65,69 @@ func (serve *OrderBonusServices) UpdateBounsByGoodNumber(orderId int64, adminId
e := fmt.Sprintf("获取管理员用户(id=%d)数据失败,%s", adminId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
_ = adminUser
//TODO数据更新操作
//事件发布
// modifyEvent := event.OrderModify{
// OrderId: oldOrderGoods.OrderId,
// AdminId: adminId,
// //
// }
// if err = serve.Publish(modifyEvent); err != nil {
// return err
// }
if ok := adminUser.InCompany(oldOrder.CompanyId); !ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "用户不能更新非自己公司的订单")
}
var (
updateGood domain.OrderGood
formerNumber int
formerAmount float64
newAmount float64
goodExist bool
)
for i := range oldOrder.Goods {
if oldOrder.Goods[i].Id != goodId {
continue
}
updateGood = oldOrder.Goods[i]
formerNumber = updateGood.GetCurrentGoodNumber()
formerAmount = updateGood.GetCurrentAmount()
updateGood.UseGoodNumber = goodWithNumber
updateGood.RemarkReason.ModifyGoodNumber = reason
//计算
if err := updateGood.Compute(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "核算商品数据失败"+err.Error())
}
newAmount = updateGood.GetCurrentAmount()
goodExist = true
oldOrder.Goods[i] = updateGood
}
if !goodExist {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "未找到指定的货品")
}
if err := oldOrder.Compute(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "核算订单数据失败"+err.Error())
}
//更新商品数据
err = orderGoodRepository.Save([]domain.OrderGood{updateGood})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "更新商品数据失败,"+err.Error())
}
//更新订单数据
err = orderBaseReponsitory.Save(oldOrder)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "更新订单数据失败,"+err.Error())
}
// 事件发布
modifyEvent := event.UpdateBonusByGoodNumber{
OrderId: oldOrder.Id,
AdminId: adminId,
GoodId: oldOrder.Id,
GoodName: updateGood.GoodName,
FormerNumber: fmt.Sprint(formerNumber),
NewNumber: fmt.Sprint(goodWithNumber),
FormerAmount: fmt.Sprint(formerAmount),
NewAmount: fmt.Sprint(newAmount),
}
if err = serve.Publish(modifyEvent); err != nil {
return err
}
return nil
}
//UpdateBounsByPartnerBonusPercent 分红时,因修改订单中商品的合伙人分行比例发生分红变动
func (serve *OrderBonusServices) UpdateBounsByPartnerBonusPercent(orderId int64, adminId int64, partnerPercent float64) error {
////目前只处理 海鲜干货的订单 即 order_type = OrderTypeBestShop (3)
func (serve *OrderBonusService) UpdateBounsByPartnerBonusPercent(orderId int64, adminId int64, goodId int64, partnerPercent float64, reason string) error {
var (
userRepository domain.UsersRepository
orderBaseReponsitory domain.OrderBaseRepository
... ... @@ -92,6 +150,9 @@ func (serve *OrderBonusServices) UpdateBounsByPartnerBonusPercent(orderId int64,
e := fmt.Sprintf("获取订单(id=%d)数据失败,%s", orderId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if oldOrder.OrderType != domain.OrderTypeBestShop {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单类型错误")
}
oldOrder.Goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单中(id=%d)的货品数据失败,%s", orderId, err)
... ... @@ -102,22 +163,69 @@ func (serve *OrderBonusServices) UpdateBounsByPartnerBonusPercent(orderId int64,
e := fmt.Sprintf("获取管理员用户(id=%d)数据失败,%s", adminId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
_ = adminUser
//TODO数据更新操作
if ok := adminUser.InCompany(oldOrder.CompanyId); !ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "用户不能更新非自己公司的订单")
}
var (
updateGood domain.OrderGood
formerPartnerBonusPercent float64
formerPartnerBonus float64
newPartnerBonus float64
goodExist bool
)
for i := range oldOrder.Goods {
if oldOrder.Goods[i].Id != goodId {
continue
}
updateGood = oldOrder.Goods[i]
formerPartnerBonusPercent = updateGood.PartnerBonusPercent
formerPartnerBonus = updateGood.GetCurrentPartnerBonus()
updateGood.PartnerBonusPercent = partnerPercent
updateGood.RemarkReason.ModifyPartnerBonusPercent = reason
//计算
if err := updateGood.Compute(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "核算商品数据失败"+err.Error())
}
newPartnerBonus = updateGood.GetCurrentPartnerBonus()
goodExist = true
oldOrder.Goods[i] = updateGood
}
if !goodExist {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "未找到指定的货品")
}
if err := oldOrder.Compute(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "核算订单数据失败"+err.Error())
}
//更新商品数据
err = orderGoodRepository.Save([]domain.OrderGood{updateGood})
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "更新商品数据失败,"+err.Error())
}
//更新订单数据
err = orderBaseReponsitory.Save(oldOrder)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "更新订单数据失败,"+err.Error())
}
//事件发布
// modifyEvent := event.OrderModify{
// OrderId: oldOrderGoods.OrderId,
// AdminId: adminId,
// //
// }
// if err = serve.Publish(modifyEvent); err != nil {
// return err
// }
modifyEvent := event.UpdateBounsByPartnerBonusPercent{
OrderId: oldOrder.Id,
AdminId: adminUser.Id,
GoodId: updateGood.Id,
GoodName: updateGood.GoodName,
FormerPartnerBonusPercent: fmt.Sprint(formerPartnerBonusPercent),
NewPartnerBonusPercent: fmt.Sprint(partnerPercent),
FormerPartnerBonus: fmt.Sprint(formerPartnerBonus),
NewPartnerBonus: fmt.Sprint(newPartnerBonus),
}
if err = serve.Publish(modifyEvent); err != nil {
return err
}
return nil
}
//PayOrderGoodBonus 支付订单中货品的分红
func (serve *OrderBonusServices) PayOrderGoodBonus(orderId int64, goodId int64, adminId int64) error {
//目前只处理 海鲜干货的订单 即 order_type = OrderTypeBestShop (3)
func (serve *OrderBonusService) PayOrderGoodBonus(orderId int64, goodId int64, adminId int64) error {
var (
userRepository domain.UsersRepository
orderBaseReponsitory domain.OrderBaseRepository
... ... @@ -140,6 +248,9 @@ func (serve *OrderBonusServices) PayOrderGoodBonus(orderId int64, goodId int64,
e := fmt.Sprintf("获取订单(id=%d)数据失败,%s", orderId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
if oldOrder.OrderType != domain.OrderTypeBestShop {
return lib.ThrowError(lib.BUSINESS_ERROR, "订单类型错误")
}
oldOrder.Goods, _, err = orderGoodRepository.Find(domain.OrderGoodFindQuery{OrderId: orderId})
if err != nil {
e := fmt.Sprintf("获取订单中(id=%d)的货品数据失败,%s", orderId, err)
... ... @@ -150,11 +261,50 @@ func (serve *OrderBonusServices) PayOrderGoodBonus(orderId int64, goodId int64,
e := fmt.Sprintf("获取管理员用户(id=%d)数据失败,%s", adminId, err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
_ = adminUser
//TODO
if ok := adminUser.InCompany(oldOrder.CompanyId); !ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "用户不能更新非自己公司的订单")
}
var (
updateGood domain.OrderGood
goodExist bool
)
for i := range oldOrder.Goods {
if oldOrder.Goods[i].Id != goodId {
continue
}
updateGood = oldOrder.Goods[i]
updateGood.CurrentBonusStatus.PayPartnerBonus(&updateGood)
//计算
if err := updateGood.Compute(); err != nil {
return errors.New("核算商品数据失败" + err.Error())
}
goodExist = true
oldOrder.Goods[i] = updateGood
}
if !goodExist {
return errors.New("未找到指定的货品")
}
//计算订单的总数据
if err := oldOrder.Compute(); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, "核算订单数据失败"+err.Error())
}
//更新商品数据
err = orderGoodRepository.Save([]domain.OrderGood{updateGood})
if err != nil {
return fmt.Errorf("更新商品数据失败,%s", err)
}
//更新订单数据
err = orderBaseReponsitory.Save(oldOrder)
if err != nil {
return fmt.Errorf("更新订单数据失败,%s", err)
}
//
payEvent := event.PayOrderGoodBonus{
GoodId: goodId,
AdminId: adminId,
OrderId: orderId,
GoodId: goodId,
AdminId: adminId,
GoodName: updateGood.GoodName,
PartnerBonus: updateGood.GetCurrentPartnerBonus(),
}
if err = serve.Publish(payEvent); err != nil {
return err
... ... @@ -162,6 +312,54 @@ func (serve *OrderBonusServices) PayOrderGoodBonus(orderId int64, goodId int64,
return nil
}
func (serve *OrderBonusServices) UpdateOrderRemark(orderId int64, adminId int64, remark string) error {
//UpdateOrderRemarkBonus 更新备注
//目前只处理 海鲜干货的订单 即 order_type = OrderTypeBestShop (3)
func (serve *OrderBonusService) UpdateOrderRemarkBonus(orderId int64, adminId int64, remark string) error {
var (
userRepository domain.UsersRepository
orderBaseReponsitory domain.OrderBaseRepository
oldOrder *domain.OrderBase
adminUser domain.Users
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())
}
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)
}
if oldOrder.OrderType != domain.OrderTypeBestShop {
return lib.ThrowError(lib.BUSINESS_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)
}
if ok := adminUser.InCompany(oldOrder.CompanyId); !ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "用户不能更新非自己公司的订单")
}
formerRemark := oldOrder.Remark.RemarkBonus
oldOrder.Remark.RemarkBonus = remark
//更新订单数据
err = orderBaseReponsitory.Save(oldOrder)
if err != nil {
return fmt.Errorf("更新订单数据失败,%s", err)
}
//事件发布
modifyEvent := event.UpdateOrderRemark{
OrderId: oldOrder.Id,
AdminId: adminUser.Id,
FormerRemark: formerRemark,
NewRemark: remark,
}
if err = serve.Publish(modifyEvent); err != nil {
return err
}
return nil
}
... ...
... ... @@ -305,3 +305,117 @@ func (c *OrderDividendController) OrderDividendDetailForBestshop() {
c.ResponseData(respData)
return
}
//EditOrderForBestshop 编辑海鲜干货的订单 中的 货品数量和分红比例
func (c *OrderDividendController) EditOrderDividendForBestshop() {
type Parameter struct {
State int `json:"state"`
OrderId string `json:"id"`
ProductId string `json:"id"`
Reason string `json:"reason"`
GoodNumber int `json:"contents"`
PartnerBonusPercent float64 `json:"partnerBonusPercent"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
orderid, _ := strconv.ParseInt(param.OrderId, 10, 64)
if orderid == 0 {
c.ResponseError(errors.New("参数错误"))
return
}
productId, _ := strconv.ParseInt(param.ProductId, 10, 64)
if productId == 0 {
c.ResponseError(errors.New("参数错误"))
return
}
adminId := c.GetUserId()
orderSrv := orderService.NewOrderInfoService(nil)
switch param.State {
case 1:
err = orderSrv.UpdateBonusByGoodNumber(orderid, productId, adminId, param.GoodNumber, param.Reason)
case 2:
err = orderSrv.UpdateBonusByPartnerBonusPercent(orderid, productId, adminId, param.PartnerBonusPercent, param.Reason)
}
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//PayOrderGoodBonusForBestshop 支付海鲜干货订单中的分红
func (c *OrderDividendController) PayOrderGoodBonusForBestshop() {
type Parameter struct {
OrderId string `json:"id"`
ProductId string `json:"id"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
orderid, _ := strconv.ParseInt(param.OrderId, 10, 64)
if orderid == 0 {
c.ResponseError(errors.New("参数错误"))
return
}
productId, _ := strconv.ParseInt(param.ProductId, 10, 64)
if productId == 0 {
c.ResponseError(errors.New("参数错误"))
return
}
adminId := c.GetUserId()
orderSrv := orderService.NewOrderInfoService(nil)
err = orderSrv.PayPartnerBonusWithOrderBestshop(orderid, productId, adminId)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//EditOrderRemarkBonusForBestshop 编辑海鲜干货订单中的备注
func (c *OrderDividendController) EditOrderRemarkBonusForBestshop() {
type Parameter struct {
OrderId string `json:"id"`
Remark string `json:"remark"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
orderid, _ := strconv.ParseInt(param.OrderId, 10, 64)
if orderid == 0 {
c.ResponseError(errors.New("参数错误"))
return
}
adminId := c.GetUserId()
orderSrv := orderService.NewOrderInfoService(nil)
err = orderSrv.UpdateOrderRemarkBonus(orderid, adminId, param.Remark)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
... ...