|
|
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
|
|
|
} |
...
|
...
|
|