dividends_estimate.go 8.0 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/dividendsEstimate/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/dividendsEstimate/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/dividendsEstimate/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
)

// 分红预算管理
type DividendsEstimateService struct {
}

//piliao  取消分红预算
func (dividendsEstimateService *DividendsEstimateService) CancelDividendsEstimate(cancelDividendsEstimateCommand *command.CancelDividendsEstimateCommand) (interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(cancelDividendsEstimateCommand.Operator)
	_, err := creationCooperationGateway.DividendsEstimatesBatchCancel(allied_creation_cooperation.ReqDividendsEstimateBatchCancel{
		DividendsEstimateIds: cancelDividendsEstimateCommand.DividendsEstimateId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return cancelDividendsEstimateCommand, nil
}

// 确定预算分红激励
func (dividendsEstimateService *DividendsEstimateService) EstimateDividendsIncentives(estimateDividendsIncentivesCommand *command.EstimateDividendsIncentivesCommand) (interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		estimateDividendsIncentivesCommand.Operator)
	_, err := creationCooperationGateway.DividendsEstimateDividendsIncentives(allied_creation_cooperation.ReqDividendsEstimateDividendsIncentives{
		CooperationContractNumber: estimateDividendsIncentivesCommand.CooperationContractNumber,
		OrderOrReturnedOrderNum:   estimateDividendsIncentivesCommand.OrderOrReturnedOrderNum,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return estimateDividendsIncentivesCommand, nil
}

// 确定预算金额激励分红
func (dividendsEstimateService *DividendsEstimateService) EstimateMoneyIncentives(estimateMoneyIncentivesCommand *command.EstimateMoneyIncentivesCommand) (interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		estimateMoneyIncentivesCommand.Operator)
	result, err := creationCooperationGateway.DividendsEstimatesEstimateMoneys(allied_creation_cooperation.ReqDividendsEstimateMoneyIncentives{
		CooperationContractNumber: estimateMoneyIncentivesCommand.CooperationContractNumber,
		DividendsIncentivesStage:  estimateMoneyIncentivesCommand.DividendsIncentivesStage,
		UndertakerUid:             estimateMoneyIncentivesCommand.UndertakerUid,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 查询分红预算单列表
func (dividendsEstimateService *DividendsEstimateService) SearchDividendsEstimate(searchDividendsEstimateQuery *query.SearchDividendsEstimateQuery) (interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		searchDividendsEstimateQuery.Operator)
	result, err := creationCooperationGateway.DividendsEstimatesSearch(allied_creation_cooperation.ReqDividendsEstimateSearch{
		DividendsEstimateOrderNumber: searchDividendsEstimateQuery.DividendsEstimateOrderNumber, //承接人分红预算单号
		DividendsType:                searchDividendsEstimateQuery.DividendsType,                //分红类型,1订单分红,2退货冲销,3金额激励
		PageNumber:                   searchDividendsEstimateQuery.PageNumber,
		PageSize:                     searchDividendsEstimateQuery.PageSize,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 查询业绩分红
func (dividendsEstimateService *DividendsEstimateService) SearchDividendsIncentives(searchDividendsIncentivesQuery *query.SearchDividendsIncentivesQuery) (
	int, interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		searchDividendsIncentivesQuery.Operator)
	result, err := creationCooperationGateway.DividendsEstimateSearchDividends(
		allied_creation_cooperation.ReqDividendsEstimateSearchDividend{
			PageNumber:                searchDividendsIncentivesQuery.PageNumber,
			PageSize:                  searchDividendsIncentivesQuery.PageSize,
			CooperationContractNumber: searchDividendsIncentivesQuery.CooperationContractNumber,
			OrderOrReturnedOrderNum:   searchDividendsIncentivesQuery.OrderOrReturnedOrderNum,
		})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	listdata := dto.ToDividendsEstimateDividendItem(result)
	return result.Grid.Total, listdata, nil
}

// 查询金额激励分红
func (dividendsEmmateService *DividendsEstimateService) SearchMoneyIncentives(searchMoneyIncentivesQuery *query.SearchMoneyIncentivesQuery) (
	int, interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		searchMoneyIncentivesQuery.Operator)
	result, err := creationCooperationGateway.DividendsEstimatesSearchMoney(allied_creation_cooperation.ReqDividendsEstimateSearchMoney{
		PageNumber:              searchMoneyIncentivesQuery.PageNumber,
		PageSize:                searchMoneyIncentivesQuery.PageSize,
		CooperationContractName: searchMoneyIncentivesQuery.CooperationContractName,
		DepartmentName:          searchMoneyIncentivesQuery.DepartmentName,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	dataList := dto.ToDividendsEstimateMoneyItem(result)
	return result.Grid.Total, dataList, nil
}

func NewDividendsEstimateService(options map[string]interface{}) *DividendsEstimateService {
	newDividendsEstimateService := &DividendsEstimateService{}
	return newDividendsEstimateService
}

func (dividendsEmmateService *DividendsEstimateService) MoneyIncentivesSelector(queryParam query.MoneyIncentiveSelectorQuery) (interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(
		queryParam.Operator)
	resultContract, err := creationCooperationGateway.CooperationContractSearch(allied_creation_cooperation.ReqCooperationContractSearch{
		CooperationContractNumber: queryParam.CooperationContractNumber,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if len(resultContract.Grid.List) == 0 {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, "未找到对应的合约数据")
	}
	contractData := resultContract.Grid.List[0]
	resultDividendsEstimate, err := creationCooperationGateway.DividendsEstimatesSearch(allied_creation_cooperation.ReqDividendsEstimateSearch{
		CooperationContractNumber: queryParam.CooperationContractNumber,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	rules := []dto.MoneyIncentivesRule{}
	for _, v := range contractData.MoneyIncentivesRules {
		r := dto.MoneyIncentivesRule{
			MoneyIncentivesStage:      v.MoneyIncentivesStage,
			MoneyIncentivesStageStart: int(v.MoneyIncentivesTime.Unix()),
		}
		rules = append(rules, r)
	}
	contractUndertaker := []dto.ContractUndertaker{}
	for _, v := range contractData.Undertakers {

		ruleStage := []int{}
		for _, vv := range resultDividendsEstimate.Grid.List {
			if v.UserId == vv.DividendsUser.UserId {
				ruleStage = append(ruleStage, vv.DividendsIncentivesStage)
			}
		}
		temp := dto.ContractUndertaker{
			UserId:                  v.UserId,
			UsersName:               v.UserName,
			ContractIncentivesRules: ruleStage,
		}
		contractUndertaker = append(contractUndertaker, temp)
	}
	data := map[string]interface{}{
		"contractUndertaker":  contractUndertaker,
		"moneyIncentivesRule": rules,
	}
	return data, nil
}