...
|
...
|
@@ -2,9 +2,12 @@ package domain_service |
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
coreDomain "github.com/linmadan/egglib-go/core/domain"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/repository"
|
|
|
)
|
|
|
|
|
|
type ConfirmDividendsIncentivesEstimateService struct {
|
...
|
...
|
@@ -12,9 +15,215 @@ type ConfirmDividendsIncentivesEstimateService struct { |
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
// Confirm TODO 确认业绩分红预算
|
|
|
func (c ConfirmDividendsIncentivesEstimateService) Confirm(orderGoods []*domain.OrderGood) (float64, error) {
|
|
|
panic("implement me")
|
|
|
// Confirm 确认业绩分红预算
|
|
|
func (domainService *ConfirmDividendsIncentivesEstimateService) Confirm(orderGoods []*domain.OrderGood) ([]*service.DividendsEstimateDetail, error) {
|
|
|
var cooperationContractRepository domain.CooperationContractRepository // 共创合约仓储
|
|
|
var dividendsOrderRepository domain.DividendsOrderRepository // 分红订单仓储
|
|
|
var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository // 分红退货单仓储
|
|
|
// 共创合约仓储初始化
|
|
|
if repo, err := repository.NewCooperationContractRepository(domainService.transactionContext); err != nil {
|
|
|
return nil, err
|
|
|
} else {
|
|
|
cooperationContractRepository = repo
|
|
|
}
|
|
|
// 分红订单仓储初始化
|
|
|
if repo, err := repository.NewDividendsOrderRepository(domainService.transactionContext); err != nil {
|
|
|
return nil, err
|
|
|
} else {
|
|
|
dividendsOrderRepository = repo
|
|
|
}
|
|
|
// 分红退后单仓储初始化
|
|
|
if repo, err := repository.NewDividendsReturnedOrderRepository(domainService.transactionContext); err != nil {
|
|
|
return nil, err
|
|
|
} else {
|
|
|
dividendsReturnedOrderRepository = repo
|
|
|
}
|
|
|
var dividendsEstimateDetails []*service.DividendsEstimateDetail
|
|
|
for _, orderGood := range orderGoods {
|
|
|
// 获取合约
|
|
|
cooperationContract, err := cooperationContractRepository.FindOne(map[string]interface{}{"cooperationContractNumber": orderGood.CooperationContractNumber})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if cooperationContract == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", orderGood.CooperationContractNumber))
|
|
|
}
|
|
|
if orderGood.DividendsOrderNumber != "" {
|
|
|
// 获取分红订单
|
|
|
dividendsOrder, err2 := dividendsOrderRepository.FindOne(map[string]interface{}{
|
|
|
"dividendsOrderNumber": orderGood.DividendsOrderNumber,
|
|
|
})
|
|
|
if err2 != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if dividendsOrder == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", orderGood.DividendsOrderNumber))
|
|
|
}
|
|
|
// 匹配分红规则
|
|
|
var dividendsIncentivesRuleMatched *domain.DividendsIncentivesRule
|
|
|
for _, dividendsIncentivesRule := range cooperationContract.DividendsIncentivesRules {
|
|
|
if dividendsOrder.OrderTime.After(dividendsIncentivesRule.DividendsIncentivesStageStart) && dividendsOrder.OrderTime.Before(dividendsIncentivesRule.DividendsIncentivesStageEnd) {
|
|
|
dividendsIncentivesRuleMatched = dividendsIncentivesRule
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
// 计算分红
|
|
|
for _, undertaker := range cooperationContract.Undertakers {
|
|
|
// 添加承接人分红预算信息详情
|
|
|
undertakerDividendsAmount := (orderGood.OrderGoodAmount - orderGood.OrderGoodExpense) * dividendsIncentivesRuleMatched.DividendsIncentivesPercentage
|
|
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
|
|
DividendsUser: &domain.User{
|
|
|
UserId: undertaker.UserId,
|
|
|
UserBaseId: undertaker.UserBaseId,
|
|
|
Org: undertaker.Org,
|
|
|
Orgs: undertaker.Orgs,
|
|
|
Department: undertaker.Department,
|
|
|
Roles: undertaker.Roles,
|
|
|
UserInfo: undertaker.UserInfo,
|
|
|
UserType: undertaker.UserType,
|
|
|
UserName: undertaker.UserName,
|
|
|
UserPhone: undertaker.UserPhone,
|
|
|
Status: undertaker.Status,
|
|
|
Company: undertaker.Company,
|
|
|
},
|
|
|
DividendsParticipateType: domain.UNDERTAKER,
|
|
|
DividendsStage: int32(dividendsIncentivesRuleMatched.DividendsIncentivesStage),
|
|
|
DividendsAmount: undertakerDividendsAmount,
|
|
|
})
|
|
|
// 添加推荐人分红预算信息详情
|
|
|
if undertaker.Referrer != nil {
|
|
|
referrerDividendsAmount := (orderGood.OrderGoodAmount - orderGood.OrderGoodExpense) * dividendsIncentivesRuleMatched.ReferrerPercentage
|
|
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
|
|
DividendsUser: &domain.User{
|
|
|
UserId: undertaker.Referrer.UserId,
|
|
|
UserBaseId: undertaker.Referrer.UserBaseId,
|
|
|
Org: undertaker.Referrer.Org,
|
|
|
Orgs: undertaker.Referrer.Orgs,
|
|
|
Department: undertaker.Referrer.Department,
|
|
|
Roles: undertaker.Referrer.Roles,
|
|
|
UserInfo: undertaker.Referrer.UserInfo,
|
|
|
UserType: undertaker.Referrer.UserType,
|
|
|
UserName: undertaker.Referrer.UserName,
|
|
|
UserPhone: undertaker.Referrer.UserPhone,
|
|
|
Company: undertaker.Referrer.Company,
|
|
|
},
|
|
|
DividendsParticipateType: domain.REFERRER,
|
|
|
DividendsStage: int32(dividendsIncentivesRuleMatched.DividendsIncentivesStage),
|
|
|
DividendsAmount: referrerDividendsAmount,
|
|
|
})
|
|
|
}
|
|
|
// 添加关联业务员分红预算信息详情
|
|
|
if undertaker.Salesman != nil {
|
|
|
salesmanDividendsAmount := (orderGood.OrderGoodAmount - orderGood.OrderGoodExpense) * dividendsIncentivesRuleMatched.SalesmanPercentage
|
|
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
|
|
DividendsUser: &domain.User{
|
|
|
UserId: undertaker.Salesman.UserId,
|
|
|
UserBaseId: undertaker.Salesman.UserBaseId,
|
|
|
Org: undertaker.Salesman.Org,
|
|
|
Orgs: undertaker.Salesman.Orgs,
|
|
|
Department: undertaker.Salesman.Department,
|
|
|
Roles: undertaker.Salesman.Roles,
|
|
|
UserInfo: undertaker.Salesman.UserInfo,
|
|
|
UserType: undertaker.Salesman.UserType,
|
|
|
UserName: undertaker.Salesman.UserName,
|
|
|
UserPhone: undertaker.Salesman.UserPhone,
|
|
|
Company: undertaker.Salesman.Company,
|
|
|
},
|
|
|
DividendsParticipateType: domain.SALESMAN,
|
|
|
DividendsStage: int32(dividendsIncentivesRuleMatched.DividendsIncentivesStage),
|
|
|
DividendsAmount: salesmanDividendsAmount,
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
} else if orderGood.DividendsReturnedOrderNumber != "" { // 获取分红退货单
|
|
|
dividendsReturnedOrder, err3 := dividendsReturnedOrderRepository.FindOne(map[string]interface{}{
|
|
|
"dividendsReturnedOrderNumber": orderGood.DividendsReturnedOrderNumber,
|
|
|
})
|
|
|
if err3 != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
if dividendsReturnedOrder == nil {
|
|
|
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", orderGood.DividendsReturnedOrderNumber))
|
|
|
}
|
|
|
// 匹配分红规则
|
|
|
var dividendsIncentivesRuleMatched *domain.DividendsIncentivesRule
|
|
|
for _, dividendsIncentivesRule := range cooperationContract.DividendsIncentivesRules {
|
|
|
if dividendsReturnedOrder.OrderTime.After(dividendsIncentivesRule.DividendsIncentivesStageStart) && dividendsReturnedOrder.OrderTime.Before(dividendsIncentivesRule.DividendsIncentivesStageEnd) {
|
|
|
dividendsIncentivesRuleMatched = dividendsIncentivesRule
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
// 计算分红
|
|
|
for _, undertaker := range cooperationContract.Undertakers {
|
|
|
// 添加承接人分红退货预算信息详情
|
|
|
undertakerDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.DividendsIncentivesPercentage
|
|
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
|
|
DividendsUser: &domain.User{
|
|
|
UserId: undertaker.UserId,
|
|
|
UserBaseId: undertaker.UserBaseId,
|
|
|
Org: undertaker.Org,
|
|
|
Orgs: undertaker.Orgs,
|
|
|
Department: undertaker.Department,
|
|
|
Roles: undertaker.Roles,
|
|
|
UserInfo: undertaker.UserInfo,
|
|
|
UserType: undertaker.UserType,
|
|
|
UserName: undertaker.UserName,
|
|
|
UserPhone: undertaker.UserPhone,
|
|
|
Status: undertaker.Status,
|
|
|
Company: undertaker.Company,
|
|
|
},
|
|
|
DividendsParticipateType: domain.UNDERTAKER,
|
|
|
DividendsStage: int32(dividendsIncentivesRuleMatched.DividendsIncentivesStage),
|
|
|
DividendsAmount: undertakerDividendsAmount,
|
|
|
})
|
|
|
// 添加推荐人分红退货预算信息详情
|
|
|
if undertaker.Referrer != nil {
|
|
|
referrerDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.ReferrerPercentage
|
|
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
|
|
DividendsUser: &domain.User{
|
|
|
UserId: undertaker.Referrer.UserId,
|
|
|
UserBaseId: undertaker.Referrer.UserBaseId,
|
|
|
Org: undertaker.Referrer.Org,
|
|
|
Orgs: undertaker.Referrer.Orgs,
|
|
|
Department: undertaker.Referrer.Department,
|
|
|
Roles: undertaker.Referrer.Roles,
|
|
|
UserInfo: undertaker.Referrer.UserInfo,
|
|
|
UserType: undertaker.Referrer.UserType,
|
|
|
UserName: undertaker.Referrer.UserName,
|
|
|
UserPhone: undertaker.Referrer.UserPhone,
|
|
|
Company: undertaker.Referrer.Company,
|
|
|
},
|
|
|
DividendsParticipateType: domain.REFERRER,
|
|
|
DividendsStage: int32(dividendsIncentivesRuleMatched.DividendsIncentivesStage),
|
|
|
DividendsAmount: referrerDividendsAmount,
|
|
|
})
|
|
|
}
|
|
|
// 添加关联业务员分红退货预算信息详情
|
|
|
if undertaker.Salesman != nil {
|
|
|
salesmanDividendsAmount := -orderGood.OrderGoodAmount * dividendsIncentivesRuleMatched.SalesmanPercentage
|
|
|
dividendsEstimateDetails = append(dividendsEstimateDetails, &service.DividendsEstimateDetail{
|
|
|
DividendsUser: &domain.User{
|
|
|
UserId: undertaker.Salesman.UserId,
|
|
|
UserBaseId: undertaker.Salesman.UserBaseId,
|
|
|
Org: undertaker.Salesman.Org,
|
|
|
Orgs: undertaker.Salesman.Orgs,
|
|
|
Department: undertaker.Salesman.Department,
|
|
|
Roles: undertaker.Salesman.Roles,
|
|
|
UserInfo: undertaker.Salesman.UserInfo,
|
|
|
UserType: undertaker.Salesman.UserType,
|
|
|
UserName: undertaker.Salesman.UserName,
|
|
|
UserPhone: undertaker.Salesman.UserPhone,
|
|
|
Company: undertaker.Salesman.Company,
|
|
|
},
|
|
|
DividendsParticipateType: domain.SALESMAN,
|
|
|
DividendsStage: int32(dividendsIncentivesRuleMatched.DividendsIncentivesStage),
|
|
|
DividendsAmount: salesmanDividendsAmount,
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return dividendsEstimateDetails, nil
|
|
|
}
|
|
|
|
|
|
func NewConfirmDividendsIncentivesEstimateService(transactionContext *pgTransaction.TransactionContext) (*ConfirmDividendsIncentivesEstimateService, error) {
|
...
|
...
|
|