pg_confirm_money_incentives_estimate_service.go 6.5 KB
package domain_service

import (
	"fmt"
	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/dao"
	"time"
)

type ConfirmMoneyIncentivesEstimateService struct {
	coreDomain.BaseEventPublisher
	transactionContext *pgTransaction.TransactionContext
}

func (domainService *ConfirmMoneyIncentivesEstimateService) Confirm(contract *domain.CooperationContract, stage int32, undertakerUIDs []int64) ([]*service.DividendsEstimateDetail, error) {
	//var dividendsEstimateRepository domain.DividendsEstimateRepository // 分红预算单仓储
	var dividendsEstimateDao *dao.DividendsEstimateDao // 分红预算DAO

	// 分红预算单仓储初始化
	//if repo, err := repository.NewDividendsEstimateRepository(domainService.transactionContext); err != nil {
	//	return nil, err
	//} else {
	//	dividendsEstimateRepository = repo
	//}

	// 分红预算DAO初始化
	if estimateDao, err := dao.NewDividendsEstimateDao(domainService.transactionContext); err != nil {
		return nil, err
	} else {
		dividendsEstimateDao = estimateDao
	}

	// 金额激励详情
	var dividendsEstimateDetails []*service.DividendsEstimateDetail

	// 匹配金额激励规则
	var moneyIncentivesRuleMatched *domain.MoneyIncentivesRule
	for _, moneyIncentivesRule := range contract.MoneyIncentivesRules {
		if moneyIncentivesRule.MoneyIncentivesStage == stage {
			moneyIncentivesRuleMatched = moneyIncentivesRule
			break
		}
	}
	if moneyIncentivesRuleMatched == nil {
		return nil, fmt.Errorf("未匹配到金额激励规则")
	}

	// 判断金额激励阶段是否合法
	currentTime := time.Now()
	if moneyIncentivesRuleMatched.MoneyIncentivesStageStart.After(currentTime) {
		return nil, fmt.Errorf("还未到分红时间")
	}

	// 金额激励预算
	for _, undertaker := range contract.Undertakers {
		//	判断承接人在当前阶段是否已经分红
		undertakerEstimated, err := dividendsEstimateDao.UserEstimated(map[string]interface{}{
			"undertakerUid": undertaker.UserId,
			"companyId":     contract.Company.CompanyId,
			"orgId":         contract.Org.OrgId,
		})
		if err != nil {
			return nil, err
		}
		if undertakerEstimated {
			return nil, fmt.Errorf("用户 " + undertaker.UserName + " 已分红")
		} else {
			undertakerDividendsAmount := moneyIncentivesRuleMatched.MoneyIncentivesAmount * (1 - (moneyIncentivesRuleMatched.SalesmanPercentage+moneyIncentivesRuleMatched.ReferrerPercentage)/100)
			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.MONEY_INCENTIVES,
				DividendsStage:           stage,
				DividendsAmount:          undertakerDividendsAmount,
			})
		}
		//	判断业务员在当前阶段是否已经分红
		salesmanEstimated, err := dividendsEstimateDao.UserEstimated(map[string]interface{}{
			"undertakerUid": undertaker.Salesman.UserId,
			"companyId":     contract.Company.CompanyId,
			"orgId":         contract.Org.OrgId,
		})
		if err != nil {
			return nil, err
		}
		if salesmanEstimated {
			return nil, fmt.Errorf("业务员 " + undertaker.Salesman.UserName + " 已分红")
		} else {
			undertakerDividendsAmount := moneyIncentivesRuleMatched.MoneyIncentivesAmount * (moneyIncentivesRuleMatched.SalesmanPercentage / 100)
			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.MONEY_INCENTIVES,
				DividendsStage:           stage,
				DividendsAmount:          undertakerDividendsAmount,
			})
		}
		//	判断关联业务员在当前阶段是否已经分红
		referrerEstimated, err := dividendsEstimateDao.UserEstimated(map[string]interface{}{
			"undertakerUid": undertaker.Referrer.UserId,
			"companyId":     contract.Company.CompanyId,
			"orgId":         contract.Org.OrgId,
		})
		if err != nil {
			return nil, err
		}
		if referrerEstimated {
			return nil, fmt.Errorf("推荐人 " + undertaker.Salesman.UserName + " 已分红")
		} else {
			undertakerDividendsAmount := moneyIncentivesRuleMatched.MoneyIncentivesAmount * (moneyIncentivesRuleMatched.ReferrerPercentage / 100)
			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.MONEY_INCENTIVES,
				DividendsStage:           stage,
				DividendsAmount:          undertakerDividendsAmount,
			})
		}
	}
	return dividendsEstimateDetails, nil
}

func NewConfirmMoneyIncentivesEstimateService(transactionContext *pgTransaction.TransactionContext) (*ConfirmMoneyIncentivesEstimateService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为空")
	} else {
		return &ConfirmMoneyIncentivesEstimateService{
			transactionContext: transactionContext,
		}, nil
	}
}