pg_order_bonus_service.go 6.5 KB
package domainService

import (
	"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 OrderBonusServices struct {
	coreDomain.BaseEventPublisher
	transactionContext *transaction.TransactionContext
}

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

//UpdateBounsWithGoodNumber 分红时,因修改订单中商品的数量发生分红变动
func (serve *OrderBonusServices) UpdateBounsByGoodNumber(orderId int64, adminId int64, goodWithNumber int) 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)
	}
	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)
	}
	_ = adminUser
	//TODO数据更新操作
	//事件发布
	// modifyEvent := event.OrderModify{
	// 	OrderId: oldOrderGoods.OrderId,
	// 	AdminId: adminId,
	// 	//
	// }
	// if err = serve.Publish(modifyEvent); err != nil {
	// 	return err
	// }
	return nil
}

//UpdateBounsByPartnerBonusPercent 分红时,因修改订单中商品的合伙人分行比例发生分红变动
func (serve *OrderBonusServices) UpdateBounsByPartnerBonusPercent(orderId int64, adminId int64, partnerPercent float64) 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)
	}
	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)
	}
	_ = adminUser
	//TODO数据更新操作
	//事件发布
	// modifyEvent := event.OrderModify{
	// 	OrderId: oldOrderGoods.OrderId,
	// 	AdminId: adminId,
	// 	//
	// }
	// if err = serve.Publish(modifyEvent); err != nil {
	// 	return err
	// }
	return nil
}

//PayOrderGoodBonus 支付订单中货品的分红
func (serve *OrderBonusServices) 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)
	}
	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)
	}
	_ = adminUser
	//TODO
	payEvent := event.PayOrderGoodBonus{
		GoodId:  goodId,
		AdminId: adminId,
	}
	if err = serve.Publish(payEvent); err != nil {
		return err
	}
	return nil
}

func (serve *OrderBonusServices) UpdateOrderRemark(orderId int64, adminId int64, remark string) error {
	return nil
}