cooperation_contract.go 14.3 KB
package service

import (
	"strconv"
	"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) {

	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, createCooperationContractCommand.Operator.OrgId), err

}

// 暂停恢复共创合约
func (cooperationContractService *CooperationContractService) EnableCooperationContract(enableCooperationContractCommand *command.EnableCooperationContractCommand) (interface{}, 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) {

	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, getCooperationContractQuery.Operator.OrgId), nil
}

// 返回共创合约管理列表
func (cooperationContractService *CooperationContractService) ListCooperationContract(
	listCooperationContractQuery *query.ListCooperationContractQuery) (int, interface{}, 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,
		OrgIds:                    listCooperationContractQuery.Operator.OrgIds,
	})
	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], listCooperationContractQuery.Operator.OrgId)
		data = append(data, *item)
	}

	return result.Grid.Total, data, nil
}

// 更新共创合约管理
func (cooperationContractService *CooperationContractService) UpdateCooperationContract(updateCooperationContractCommand *command.UpdateCooperationContractCommand) (interface{}, error) {

	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(updateCooperationContractCommand.Operator)
	rules1 := []allied_creation_cooperation.DividendsIncentivesRule{}
	for _, v := range updateCooperationContractCommand.DividendsIncentivesRules {
		id, _ := strconv.Atoi(v.DividendsIncentivesRuleId)
		r := allied_creation_cooperation.DividendsIncentivesRule{
			DividendsIncentivesRuleId:     id,
			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 updateCooperationContractCommand.MoneyIncentivesRules {
		id, _ := strconv.Atoi(v.MoneyIncentivesRuleId)
		r := allied_creation_cooperation.MoneyIncentivesRule{
			MoneyIncentivesRuleId:     id,
			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 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, updateCooperationContractCommand.Operator.OrgId), 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)
	result, err := creationCooperationGateway.CooperationContractsSearchByUndertaker(
		allied_creation_cooperation.ReqCooperationContractSearchByUndertaker{
			CooperationContractName: queryParam.CooperationContractName,
			SponsorName:             queryParam.ContractSponsor,
			PageNumber:              queryParam.PageNumber,
			PageSize:                queryParam.PageSize,
			CompanyId:               queryParam.Operator.CompanyId,
			OrgIds:                  queryParam.Operator.OrgIds,
		})

	return result.Grid.Total, result.Grid.List, 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
}

// 分红单,分红退货单 返回共创合约下拉选择列表
func (cooperationContractService *CooperationContractService) ContractSelectorForDividendsOrder(
	listCooperationContractQuery *query.ListContractSelectorQuery) (int, interface{}, 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,
		OrgId:                     listCooperationContractQuery.Operator.OrgId,
		Status:                    1,
		IncentivesType:            1,
	})
	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], listCooperationContractQuery.Operator.OrgId)
		data = append(data, *item)
	}

	return result.Grid.Total, data, nil
}