pg_order_bonus_service.go 14.1 KB
package domainService

import (
	"errors"
	"fmt"

	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/domain/service"
	"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"
)

//OrderBonusServices 处理订单分红的相关操作
type OrderBonusService struct {
	coreDomain.BaseEventPublisher
	transactionContext *transaction.TransactionContext
}

var _ service.OrderBonusService = (*OrderBonusService)(nil)

func NewOrderBonusService(tcx *transaction.TransactionContext) *OrderBonusService {
	return &OrderBonusService{
		transactionContext: tcx,
	}
}

//UpdateBounsWithGoodNumber 分红时,因修改订单中商品的数量发生分红变动
//目前只处理 海鲜干货的订单 即 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
		orderGoodRepository  domain.OrderGoodRepository
		oldOrder             *domain.OrderBase
		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())
	}
	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, "订单类型错误")
	}
	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)
	}
	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, "用户不能更新非自己公司的订单")
	}
	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()
		err := new(domain.OrderGoodWithBestshop).UpdateBonusByGoodNumber(&updateGood, goodWithNumber)
		if err != nil {
			return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
		}
		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 分红时,因修改订单中商品的合伙人分行比例发生分红变动
////目前只处理 海鲜干货的订单 即 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
		orderGoodRepository  domain.OrderGoodRepository
		oldOrder             *domain.OrderBase
		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())
	}
	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, "订单类型错误")
	}
	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)
	}
	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, "用户不能更新非自己公司的订单")
	}
	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()
		err := new(domain.OrderGoodWithBestshop).UpdateBonusByPertnerBonusPercent(&updateGood, partnerPercent)
		if err != nil {
			return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
		}
		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.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 支付订单中货品的分红
//目前只处理 海鲜干货的订单 即 order_type = OrderTypeBestShop (3)
func (serve *OrderBonusService) PayOrderGoodBonus(orderId int64, goodId int64, adminId int64) error {
	var (
		userRepository       domain.UsersRepository
		orderBaseReponsitory domain.OrderBaseRepository
		orderGoodRepository  domain.OrderGoodRepository
		oldOrder             *domain.OrderBase
		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())
	}
	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, "订单类型错误")
	}
	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)
	}
	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, "用户不能更新非自己公司的订单")
	}
	var (
		updateGood domain.OrderGood
		goodExist  bool
	)
	for i := range oldOrder.Goods {
		if oldOrder.Goods[i].Id != goodId {
			continue
		}
		updateGood = oldOrder.Goods[i]
		err := new(domain.OrderGoodWithBestshop).PayPartnerBonus(&updateGood)
		if err != nil {
			return lib.ThrowError(lib.BUSINESS_ERROR, err.Error())
		}
		//计算
		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{
		OrderId:      orderId,
		GoodId:       goodId,
		AdminId:      adminId,
		GoodName:     updateGood.GoodName,
		PartnerBonus: updateGood.GetCurrentPartnerBonus(),
	}
	if err = serve.Publish(payEvent); err != nil {
		return err
	}
	return nil
}

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