cooperation_contract.go 13.2 KB
package service

import (
	"time"

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

// 共创合约管理
type CooperationContractService struct {
}

// 创建共创合约管理
func (cooperationContractService *CooperationContractService) CreateCooperationContract(createCooperationContractCommand *command.CreateCooperationContractCommand) (interface{}, error) {
	if err := createCooperationContractCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(createCooperationContractCommand.Operator)
	rules1 := []allied_creation_cooperation.DividendsIncentivesRule{}
	for _, v := range createCooperationContractCommand.DividendsIncentivesRules {
		r := allied_creation_cooperation.DividendsIncentivesRule{
			CooperationContractNumber:     v.CooperationContractNumber,
			ReferrerPercentage:            v.ReferrerPercentage,
			SalesmanPercentage:            v.SalesmanPercentage,
			DividendsIncentivesPercentage: v.DividendsPercentage,
			DividendsIncentivesStage:      v.DividendsStage,
			DividendsIncentivesStageEnd:   time.Unix(int64(v.DividendsStageEnd/1e3), 0),
			DividendsIncentivesStageStart: time.Unix(int64(v.DividendsStageStart/1e3), 0),
			Remarks:                       v.Remarks,
		}
		rules1 = append(rules1, r)
	}
	rules2 := []allied_creation_cooperation.MoneyIncentivesRule{}
	for _, v := range createCooperationContractCommand.MoneyIncentivesRules {
		r := allied_creation_cooperation.MoneyIncentivesRule{
			MoneyIncentivesRuleId:     0,
			CooperationContractNumber: v.CooperationContractNumber,
			MoneyIncentivesAmount:     v.MoneyIncentivesAmount,
			MoneyIncentivesStage:      v.MoneyIncentivesStage,
			MoneyIncentivesTime:       time.Unix(int64(v.MoneyIncentivesTime/1e3), 0),
			ReferrerPercentage:        v.ReferrerPercentage,
			SalesmanPercentage:        v.SalesmanPercentage,
			Remarks:                   v.Remarks,
		}
		rules2 = append(rules2, r)
	}
	underTakers := []allied_creation_cooperation.Undertaker{}
	for _, v := range createCooperationContractCommand.Undertakers {
		u := allied_creation_cooperation.Undertaker{
			UndertakerId:       v.UndertakerId,
			UserId:             v.UserId,
			ReferrerId:         v.RerferrerId,
			SalesmanId:         v.SalesmanId,
			ContractAttachment: v.Attachment,
		}
		underTakers = append(underTakers, u)
	}
	result, err := creationCooperationGateway.CooperationContractAdd(allied_creation_cooperation.ReqCooperationContractAdd{
		MoneyIncentivesRules:              rules2,
		DividendsIncentivesRules:          rules1,
		Undertakers:                       underTakers,
		RelevantIds:                       createCooperationContractCommand.RelationUser,
		CooperationContractDescription:    createCooperationContractCommand.CooperationContract.CooperationContractDescription,
		CooperationProjectNumber:          createCooperationContractCommand.CooperationContract.CooperationProjectNumber,
		DepartmentId:                      createCooperationContractCommand.CooperationContract.DepartmentId,
		CooperationContractUndertakerType: createCooperationContractCommand.CooperationContract.CooperationContractUndertakerType,
		CooperationContractName:           createCooperationContractCommand.CooperationContract.CooperationContractName,
		CooperationModeNumber:             createCooperationContractCommand.CooperationContract.CooperationModeNumber,
		SponsorUid:                        createCooperationContractCommand.CooperationContract.SponsorUserId,
		CompanyId:                         int(createCooperationContractCommand.Operator.CompanyId),
		UserId:                            int(createCooperationContractCommand.Operator.UserId),
		UserBaseId:                        int(createCooperationContractCommand.Operator.UserBaseId),
		OrgId:                             int(createCooperationContractCommand.Operator.OrgId),
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return dto.ToCooperationContractInfo(&result.CooperationContract), err
}

// 暂停恢复共创合约
func (cooperationContractService *CooperationContractService) EnableCooperationContract(enableCooperationContractCommand *command.EnableCooperationContractCommand) (interface{}, error) {
	if err := enableCooperationContractCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(enableCooperationContractCommand.Operator)
	_, err := creationCooperationGateway.CooperationContractsBatchOperate(allied_creation_cooperation.ReqCooperationContractsBatchOperate{
		CooperationContractIds: enableCooperationContractCommand.CooperationContractId,
		Action:                 enableCooperationContractCommand.Status,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return enableCooperationContractCommand, nil
}

// 返回共创合约详情
func (cooperationContractService *CooperationContractService) GetCooperationContract(getCooperationContractQuery *query.GetCooperationContractQuery) (interface{}, error) {
	if err := getCooperationContractQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(getCooperationContractQuery.Operator)
	result, err := creationCooperationGateway.CooperationContractGet(allied_creation_cooperation.ReqCooperationContractGet{
		CooperationContractId: getCooperationContractQuery.CooperationContractId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	return dto.ToCooperationContractInfo(&result.CooperationContract), nil
}

// 返回共创合约管理列表
func (cooperationContractService *CooperationContractService) ListCooperationContract(
	listCooperationContractQuery *query.ListCooperationContractQuery) (int, interface{}, error) {
	if err := listCooperationContractQuery.ValidateQuery(); err != nil {
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(listCooperationContractQuery.Operator)
	result, err := creationCooperationGateway.CooperationContractSearch(allied_creation_cooperation.ReqCooperationContractSearch{
		PageNumber:                listCooperationContractQuery.PageNumber,
		PageSize:                  listCooperationContractQuery.PageSize,
		SponsorName:               listCooperationContractQuery.SponsorName,
		CooperationContractNumber: listCooperationContractQuery.CooperationContractNumber,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	data := []dto.CooperationContractItem{}
	for i := range result.Grid.List {
		item := dto.ToCooperationContractItem(&result.Grid.List[i])
		data = append(data, *item)
	}

	return result.Grid.Total, data, nil
}

// 更新共创合约管理
func (cooperationContractService *CooperationContractService) UpdateCooperationContract(updateCooperationContractCommand *command.UpdateCooperationContractCommand) (interface{}, error) {
	if err := updateCooperationContractCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(updateCooperationContractCommand.Operator)
	rules1 := []allied_creation_cooperation.DividendsIncentivesRule{}
	for _, v := range updateCooperationContractCommand.DividendsIncentivesRules {
		r := allied_creation_cooperation.DividendsIncentivesRule{
			CooperationContractNumber:     v.CooperationContractNumber,
			ReferrerPercentage:            v.ReferrerPercentage,
			SalesmanPercentage:            v.SalesmanPercentage,
			DividendsIncentivesPercentage: v.DividendsPercentage,
			DividendsIncentivesStage:      v.DividendsStage,
			DividendsIncentivesStageEnd:   time.Unix(int64(v.DividendsStageEnd/1e3), 0),
			DividendsIncentivesStageStart: time.Unix(int64(v.DividendsStageStart/1e3), 0),
		}
		rules1 = append(rules1, r)
	}
	rules2 := []allied_creation_cooperation.MoneyIncentivesRule{}
	for _, v := range updateCooperationContractCommand.MoneyIncentivesRules {
		r := allied_creation_cooperation.MoneyIncentivesRule{
			MoneyIncentivesRuleId:     0,
			CooperationContractNumber: v.CooperationContractNumber,
			MoneyIncentivesAmount:     v.MoneyIncentivesAmount,
			MoneyIncentivesStage:      v.MoneyIncentivesStage,
			MoneyIncentivesTime:       time.Unix(int64(v.MoneyIncentivesTime/1e3), 0),
			ReferrerPercentage:        v.ReferrerPercentage,
			SalesmanPercentage:        v.SalesmanPercentage,
		}
		rules2 = append(rules2, r)
	}
	underTakers := []allied_creation_cooperation.Undertaker{}
	for _, v := range updateCooperationContractCommand.Undertakers {
		u := allied_creation_cooperation.Undertaker{
			UndertakerId:       v.UndertakerId,
			UserId:             v.UserId,
			ReferrerId:         v.RerferrerId,
			SalesmanId:         v.SalesmanId,
			ContractAttachment: v.Attachment,
		}
		underTakers = append(underTakers, u)
	}
	result, err := creationCooperationGateway.CooperationContractUpdate(allied_creation_cooperation.ReqCooperationContractUpdate{
		CooperationContractId:          updateCooperationContractCommand.CooperationContract.CooperationContractId,
		MoneyIncentivesRules:           rules2,
		DividendsIncentivesRules:       rules1,
		Undertakers:                    underTakers,
		RelevantIds:                    updateCooperationContractCommand.RelationUser,
		CooperationContractDescription: updateCooperationContractCommand.CooperationContract.CooperationContractDescription,
		// CooperationContractNumber:         updateCooperationContractCommand.CooperationContract.CooperationContractNumber,
		CooperationProjectNumber:          updateCooperationContractCommand.CooperationContract.CooperationProjectNumber,
		DepartmentId:                      updateCooperationContractCommand.CooperationContract.DepartmentId,
		CooperationContractUndertakerType: updateCooperationContractCommand.CooperationContract.CooperationContractUndertakerType,
		CooperationContractName:           updateCooperationContractCommand.CooperationContract.CooperationContractName,
		CooperationModeNumber:             updateCooperationContractCommand.CooperationContract.CooperationModeNumber,
		SponsorUid:                        updateCooperationContractCommand.CooperationContract.SponsorUserId,
		CompanyId:                         int(updateCooperationContractCommand.Operator.CompanyId),
		UserId:                            int(updateCooperationContractCommand.Operator.UserId),
		UserBaseId:                        int(updateCooperationContractCommand.Operator.UserBaseId),
		OrgId:                             int(updateCooperationContractCommand.Operator.OrgId),
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return dto.ToCooperationContractInfo(&result.CooperationContract), nil
}

func NewCooperationContractService(options map[string]interface{}) *CooperationContractService {
	newCooperationContractService := &CooperationContractService{}
	return newCooperationContractService
}

func (cooperationContractService *CooperationContractService) SearchCooperationContractsByUndertake(queryParam *query.SearchCooperationContractsByUndertake) (
	int, interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(queryParam.Operator)
	_, err := creationCooperationGateway.CooperationContractsSearchByUndertaker(
		allied_creation_cooperation.ReqCooperationContractSearchByUndertaker{
			CooperationContractName: queryParam.CooperationContractName,
			SponsorName:             queryParam.ContractSponsor,
			PageNumber:              queryParam.PageNumber,
			PageIndex:               queryParam.PageSize,
			CompanyId:               queryParam.Operator.CompanyId,
		})
	data := []dto.CooperationContractUndertake{{}}
	return 10, data, err
}

// 移除共创合约详情
func (cooperationContractService *CooperationContractService) RemoveCooperationContract(removeCooperationContractCommand *command.RemoveCooperationContractCommand) (interface{}, error) {
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(removeCooperationContractCommand.Operator)
	_, err := creationCooperationGateway.CooperationContractBatchRemove(allied_creation_cooperation.ReqCooperationContractBatchRemove{
		CooperationContractIds: removeCooperationContractCommand.CooperationContractId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	return removeCooperationContractCommand, nil
}