审查视图

pkg/application/web/cooperationContract/service/cooperation_contract.go 13.0 KB
tangxuhui authored
1 2 3
package service

import (
tangxuhui authored
4 5
	"time"
tangxuhui authored
6 7
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/cooperationContract/command"
tangxuhui authored
8
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/cooperationContract/dto"
tangxuhui authored
9
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/cooperationContract/query"
tangxuhui authored
10
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
tangxuhui authored
11 12 13 14 15 16 17 18 19 20 21
)

// 共创合约管理
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())
	}
tangxuhui authored
22
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(createCooperationContractCommand.Operator)
tangxuhui authored
23 24 25 26 27 28
	rules1 := []allied_creation_cooperation.DividendsIncentivesRule{}
	for _, v := range createCooperationContractCommand.DividendsIncentivesRules {
		r := allied_creation_cooperation.DividendsIncentivesRule{
			CooperationContractNumber:     v.CooperationContractNumber,
			ReferrerPercentage:            v.ReferrerPercentage,
			SalesmanPercentage:            v.SalesmanPercentage,
tangxuhui authored
29 30 31 32
			DividendsIncentivesPercentage: v.DividendsPercentage,
			DividendsIncentivesStage:      v.DividendsStage,
			DividendsIncentivesStageEnd:   time.Unix(int64(v.DividendsStageEnd/1e3), 0),
			DividendsIncentivesStageStart: time.Unix(int64(v.DividendsStageStart/1e3), 0),
tangxuhui authored
33 34 35
		}
		rules1 = append(rules1, r)
	}
tangxuhui authored
36 37 38 39 40 41 42
	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,
tangxuhui authored
43
			MoneyIncentivesTime:       time.Unix(int64(v.MoneyIncentivesTime/1e3), 0),
tangxuhui authored
44 45 46 47 48 49 50 51
			ReferrerPercentage:        v.ReferrerPercentage,
			SalesmanPercentage:        v.SalesmanPercentage,
		}
		rules2 = append(rules2, r)
	}
	underTakers := []allied_creation_cooperation.Undertaker{}
	for _, v := range createCooperationContractCommand.Undertakers {
		u := allied_creation_cooperation.Undertaker{
tangxuhui authored
52 53 54 55 56
			UndertakerId: v.UndertakerId,
			UserId:       v.UserId,
			ReferrerId:   v.RerferrerId,
			SalesmanId:   v.SalesmanId,
			Attachment:   v.Attachment,
tangxuhui authored
57 58 59
		}
		underTakers = append(underTakers, u)
	}
tangxuhui authored
60
	result, err := creationCooperationGateway.CooperationContractAdd(allied_creation_cooperation.ReqCooperationContractAdd{
tangxuhui authored
61 62 63 64
		MoneyIncentivesRules:              rules2,
		DividendsIncentivesRules:          rules1,
		Undertakers:                       underTakers,
		RelevantIds:                       createCooperationContractCommand.RelevantIds,
65 66 67 68 69 70
		CooperationContractDescription:    createCooperationContractCommand.CooperationContract.CooperationContractDescription,
		CooperationProjectNumber:          createCooperationContractCommand.CooperationContract.CooperationProjectNumber,
		DepartmentId:                      createCooperationContractCommand.CooperationContract.DepartmentId,
		CooperationContractUndertakerType: createCooperationContractCommand.CooperationContract.CooperationContractUndertakerType,
		CooperationContractName:           createCooperationContractCommand.CooperationContract.CooperationContractName,
		CooperationModeNumber:             createCooperationContractCommand.CooperationContract.CooperationModeNumber,
tangxuhui authored
71
		SponsorUid:                        createCooperationContractCommand.CooperationContract.SponsorUserId,
tangxuhui authored
72 73 74 75
		CompanyId:                         int(createCooperationContractCommand.Operator.CompanyId),
		UserId:                            int(createCooperationContractCommand.Operator.UserId),
		UserBaseId:                        int(createCooperationContractCommand.Operator.UserBaseId),
		OrgId:                             int(createCooperationContractCommand.Operator.OrgId),
tangxuhui authored
76
	})
tangxuhui authored
77 78 79
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
tangxuhui authored
80
	return dto.ToCooperationContractInfo(&result.CooperationContract), err
tangxuhui authored
81 82 83 84 85 86 87
}

// 暂停恢复共创合约
func (cooperationContractService *CooperationContractService) EnableCooperationContract(enableCooperationContractCommand *command.EnableCooperationContractCommand) (interface{}, error) {
	if err := enableCooperationContractCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
tangxuhui authored
88 89 90 91 92 93 94 95 96
	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
tangxuhui authored
97 98
}
tangxuhui authored
99
// 返回共创合约详情
tangxuhui authored
100 101 102 103
func (cooperationContractService *CooperationContractService) GetCooperationContract(getCooperationContractQuery *query.GetCooperationContractQuery) (interface{}, error) {
	if err := getCooperationContractQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
tangxuhui authored
104 105 106 107 108 109 110
	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())
	}
tangxuhui authored
111 112

	return dto.ToCooperationContractInfo(&result.CooperationContract), nil
tangxuhui authored
113 114 115
}

// 返回共创合约管理列表
tangxuhui authored
116 117
func (cooperationContractService *CooperationContractService) ListCooperationContract(
	listCooperationContractQuery *query.ListCooperationContractQuery) (int, interface{}, error) {
tangxuhui authored
118
	if err := listCooperationContractQuery.ValidateQuery(); err != nil {
tangxuhui authored
119
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
tangxuhui authored
120
	}
tangxuhui authored
121
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(listCooperationContractQuery.Operator)
tangxuhui authored
122
	result, err := creationCooperationGateway.CooperationContractSearch(allied_creation_cooperation.ReqCooperationContractSearch{
tangxuhui authored
123 124 125 126
		PageNumber:                listCooperationContractQuery.PageNumber,
		PageSize:                  listCooperationContractQuery.PageSize,
		SponsorName:               listCooperationContractQuery.SponsorName,
		CooperationContractNumber: listCooperationContractQuery.CooperationContractNumber,
tangxuhui authored
127
	})
tangxuhui authored
128
	if err != nil {
tangxuhui authored
129
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
tangxuhui authored
130
	}
tangxuhui authored
131 132 133 134 135 136 137
	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
tangxuhui authored
138 139 140 141 142 143 144
}

// 更新共创合约管理
func (cooperationContractService *CooperationContractService) UpdateCooperationContract(updateCooperationContractCommand *command.UpdateCooperationContractCommand) (interface{}, error) {
	if err := updateCooperationContractCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
tangxuhui authored
145 146 147 148 149 150 151
	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,
tangxuhui authored
152 153 154 155
			DividendsIncentivesPercentage: v.DividendsPercentage,
			DividendsIncentivesStage:      v.DividendsStage,
			DividendsIncentivesStageEnd:   time.Unix(int64(v.DividendsStageEnd/1e3), 0),
			DividendsIncentivesStageStart: time.Unix(int64(v.DividendsStageStart/1e3), 0),
tangxuhui authored
156 157 158 159 160 161 162 163 164 165
		}
		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,
tangxuhui authored
166
			MoneyIncentivesTime:       time.Unix(int64(v.MoneyIncentivesTime/1e3), 0),
tangxuhui authored
167 168 169 170 171 172 173 174
			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{
tangxuhui authored
175 176 177 178 179
			UndertakerId: v.UndertakerId,
			UserId:       v.UserId,
			ReferrerId:   v.RerferrerId,
			SalesmanId:   v.SalesmanId,
			Attachment:   v.Attachment,
tangxuhui authored
180 181 182
		}
		underTakers = append(underTakers, u)
	}
tangxuhui authored
183
	result, err := creationCooperationGateway.CooperationContractUpdate(allied_creation_cooperation.ReqCooperationContractUpdate{
184
		CooperationContractId:             updateCooperationContractCommand.CooperationContract.CooperationContractId,
tangxuhui authored
185 186 187 188
		MoneyIncentivesRules:              rules2,
		DividendsIncentivesRules:          rules1,
		Undertakers:                       underTakers,
		RelevantIds:                       updateCooperationContractCommand.RelevantIds,
189 190 191 192 193 194 195
		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,
tangxuhui authored
196
		SponsorUid:                        updateCooperationContractCommand.CooperationContract.SponsorUserId,
tangxuhui authored
197 198 199 200
		CompanyId:                         int(updateCooperationContractCommand.Operator.CompanyId),
		UserId:                            int(updateCooperationContractCommand.Operator.UserId),
		UserBaseId:                        int(updateCooperationContractCommand.Operator.UserBaseId),
		OrgId:                             int(updateCooperationContractCommand.Operator.OrgId),
tangxuhui authored
201 202 203 204
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
tangxuhui authored
205
	return dto.ToCooperationContractInfo(&result.CooperationContract), nil
tangxuhui authored
206 207 208 209 210 211
}

func NewCooperationContractService(options map[string]interface{}) *CooperationContractService {
	newCooperationContractService := &CooperationContractService{}
	return newCooperationContractService
}
tangxuhui authored
212
tangxuhui authored
213 214
func (cooperationContractService *CooperationContractService) SearchCooperationContractsByUndertake(queryParam *query.SearchCooperationContractsByUndertake) (
	int, interface{}, error) {
tangxuhui authored
215
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(queryParam.Operator)
tangxuhui authored
216
	_, err := creationCooperationGateway.CooperationContractsSearchByUndertaker(
tangxuhui authored
217 218 219 220 221 222
		allied_creation_cooperation.ReqCooperationContractSearchByUndertaker{
			CooperationContractName: queryParam.CooperationContractName,
			SponsorName:             queryParam.ContractSponsor,
			PageNumber:              queryParam.PageNumber,
			PageIndex:               queryParam.PageSize,
		})
tangxuhui authored
223 224
	data := []dto.CooperationContractUndertake{{}}
	return 10, data, err
tangxuhui authored
225
}
tangxuhui authored
226 227 228 229 230 231 232 233 234 235 236 237 238

// 移除共创合约详情
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
}