作者 陈志颖

feat:修改共创合约仓储承接人部分

@@ -10,7 +10,7 @@ var LOG_FILE = "app.log" @@ -10,7 +10,7 @@ var LOG_FILE = "app.log"
10 var LOG_PREFIX = "[allied-creation-cooperation]" 10 var LOG_PREFIX = "[allied-creation-cooperation]"
11 11
12 // 用户信息模块地址 12 // 用户信息模块地址
13 -var USER_MODULE_HOST = "" 13 +var USER_MODULE_HOST = "http://127.0.0.1:8081"
14 14
15 func init() { 15 func init() {
16 if os.Getenv("LOG_LEVEL") != "" { 16 if os.Getenv("LOG_LEVEL") != "" {
@@ -14,10 +14,6 @@ type CooperationContract struct { @@ -14,10 +14,6 @@ type CooperationContract struct {
14 CooperationContractName string `json:"cooperationContractName"` 14 CooperationContractName string `json:"cooperationContractName"`
15 // 共创合约编号 15 // 共创合约编号
16 CooperationContractNumber string `json:"cooperationContractNumber"` 16 CooperationContractNumber string `json:"cooperationContractNumber"`
17 - // 合约推荐人  
18 - CooperationContractReferrer *User `json:"cooperationContractReferrer"`  
19 - // 共创合约业务员  
20 - CooperationContractSalesman *User `json:"cooperationContractSalesman"`  
21 // 共创合约承接对象,1员工,2共创用户,3公开 17 // 共创合约承接对象,1员工,2共创用户,3公开
22 CooperationContractUndertakerTypes []int32 `json:"cooperationContractUndertakerType"` 18 CooperationContractUndertakerTypes []int32 `json:"cooperationContractUndertakerType"`
23 // 共创合约发起人 19 // 共创合约发起人
@@ -36,10 +32,10 @@ type CooperationContract struct { @@ -36,10 +32,10 @@ type CooperationContract struct {
36 DividendsIncentivesRules []*DividendsIncentivesRule `json:"dividendsIncentivesRules"` 32 DividendsIncentivesRules []*DividendsIncentivesRule `json:"dividendsIncentivesRules"`
37 // 金额激励规则 33 // 金额激励规则
38 MoneyIncentivesRules []*MoneyIncentivesRule `json:"moneyIncentivesRules"` 34 MoneyIncentivesRules []*MoneyIncentivesRule `json:"moneyIncentivesRules"`
39 - // 共创合约相关人列表  
40 - RelevantPeople []*Relevant `json:"relevantPeople"`  
41 // 共创承接人列表 35 // 共创承接人列表
42 Undertakers []*Undertaker `json:"undertakers"` 36 Undertakers []*Undertaker `json:"undertakers"`
  37 + // 共创合约相关人列表
  38 + RelevantPeople []*Relevant `json:"relevantPeople"`
43 // 操作时间 39 // 操作时间
44 OperateTime time.Time `json:"operateTime"` 40 OperateTime time.Time `json:"operateTime"`
45 // 创建时间 41 // 创建时间
@@ -74,18 +70,6 @@ func (cooperationContract *CooperationContract) Update(data map[string]interface @@ -74,18 +70,6 @@ func (cooperationContract *CooperationContract) Update(data map[string]interface
74 if cooperationContractNumber, ok := data["cooperationContractNumber"]; ok { 70 if cooperationContractNumber, ok := data["cooperationContractNumber"]; ok {
75 cooperationContract.CooperationContractNumber = cooperationContractNumber.(string) 71 cooperationContract.CooperationContractNumber = cooperationContractNumber.(string)
76 } 72 }
77 - if userId, ok := data["userId"]; ok {  
78 - cooperationContract.CooperationContractReferrer.UserId = userId.(int64)  
79 - }  
80 - if userBaseId, ok := data["userBaseId"]; ok {  
81 - cooperationContract.CooperationContractReferrer.UserBaseId = userBaseId.(int64)  
82 - }  
83 - if orgs, ok := data["orgs"]; ok {  
84 - cooperationContract.CooperationContractReferrer.Orgs = orgs.([]*Org)  
85 - }  
86 - if status, ok := data["status"]; ok {  
87 - cooperationContract.CooperationContractReferrer.Status = status.(int32)  
88 - }  
89 if cooperationContractUndertakerType, ok := data["cooperationContractUndertakerType"]; ok { 73 if cooperationContractUndertakerType, ok := data["cooperationContractUndertakerType"]; ok {
90 cooperationContract.CooperationContractUndertakerTypes = cooperationContractUndertakerType.([]int32) 74 cooperationContract.CooperationContractUndertakerTypes = cooperationContractUndertakerType.([]int32)
91 } 75 }
@@ -2,8 +2,12 @@ package domain @@ -2,8 +2,12 @@ package domain
2 2
3 // Undertaker 共创合约承接方值对象 3 // Undertaker 共创合约承接方值对象
4 type Undertaker struct { 4 type Undertaker struct {
  5 + // 承接人id
  6 + UndertakerId int64 `json:"relevantId"`
5 // 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员 7 // 用户ID,通过集成REST上下文获取,可翻译成发起人、承接人、推荐人、业务员
6 UserId int64 `json:"userId,string"` 8 UserId int64 `json:"userId,string"`
  9 + // 共创合约编号
  10 + CooperationContractNumber string `json:"cooperationContractNumber"`
7 // 用户基本id 11 // 用户基本id
8 UserBaseId int64 `json:"userBaseId,string"` 12 UserBaseId int64 `json:"userBaseId,string"`
9 // 用户所属组织机构 13 // 用户所属组织机构
@@ -15,10 +15,6 @@ type CooperationContract struct { @@ -15,10 +15,6 @@ type CooperationContract struct {
15 CooperationContractName string `comment:"共创合约名称"` 15 CooperationContractName string `comment:"共创合约名称"`
16 // 共创合约编号 16 // 共创合约编号
17 CooperationContractNumber string `comment:"共创合约编号"` 17 CooperationContractNumber string `comment:"共创合约编号"`
18 - // 合约推荐人  
19 - CooperationContractReferrer *domain.User `comment:"合约推荐人"`  
20 - // 共创合约业务员  
21 - CooperationContractSalesman *domain.User `comment:"共创合约业务员"`  
22 // 共创合约承接对象,1员工,2共创用户,3公开 18 // 共创合约承接对象,1员工,2共创用户,3公开
23 CooperationContractUndertakerTypes []int32 `comment:"共创合约承接对象,1员工,2共创用户,3公开" pg:",array"` 19 CooperationContractUndertakerTypes []int32 `comment:"共创合约承接对象,1员工,2共创用户,3公开" pg:",array"`
24 // 共创合约发起人 20 // 共创合约发起人
@@ -71,17 +71,17 @@ func TransformToCooperationContractDomainModelFromPgModels( @@ -71,17 +71,17 @@ func TransformToCooperationContractDomainModelFromPgModels(
71 var undertakersDomain []*domain.Undertaker 71 var undertakersDomain []*domain.Undertaker
72 for _, undertaker := range undertakers { 72 for _, undertaker := range undertakers {
73 undertakersDomain = append(undertakersDomain, &domain.Undertaker{ 73 undertakersDomain = append(undertakersDomain, &domain.Undertaker{
74 - UserId: undertaker.Undertaker.UserId,  
75 - UserBaseId: undertaker.Undertaker.UserBaseId,  
76 - Org: undertaker.Undertaker.Org,  
77 - Orgs: undertaker.Undertaker.Orgs,  
78 - Department: undertaker.Undertaker.Department,  
79 - Role: undertaker.Undertaker.Role,  
80 - UserInfo: undertaker.Undertaker.UserInfo,  
81 - UserType: undertaker.Undertaker.UserType,  
82 - Status: undertaker.Undertaker.Status,  
83 - Company: undertaker.Undertaker.Company,  
84 - ContractAttachment: undertaker.Undertaker.ContractAttachment, 74 + UserId: undertaker.UserId,
  75 + UserBaseId: undertaker.UserBaseId,
  76 + Org: undertaker.Org,
  77 + Orgs: undertaker.Orgs,
  78 + Department: undertaker.Department,
  79 + Role: undertaker.Role,
  80 + UserInfo: undertaker.UserInfo,
  81 + UserType: undertaker.UserType,
  82 + Status: undertaker.Status,
  83 + Company: undertaker.Company,
  84 + ContractAttachment: undertaker.ContractAttachment,
85 }) 85 })
86 } 86 }
87 return &domain.CooperationContract{ 87 return &domain.CooperationContract{
@@ -89,8 +89,6 @@ func TransformToCooperationContractDomainModelFromPgModels( @@ -89,8 +89,6 @@ func TransformToCooperationContractDomainModelFromPgModels(
89 CooperationContractDescription: cooperationContractModel.CooperationContractDescription, 89 CooperationContractDescription: cooperationContractModel.CooperationContractDescription,
90 CooperationContractName: cooperationContractModel.CooperationContractName, 90 CooperationContractName: cooperationContractModel.CooperationContractName,
91 CooperationContractNumber: cooperationContractModel.CooperationContractNumber, 91 CooperationContractNumber: cooperationContractModel.CooperationContractNumber,
92 - CooperationContractReferrer: cooperationContractModel.CooperationContractReferrer,  
93 - CooperationContractSalesman: cooperationContractModel.CooperationContractSalesman,  
94 CooperationContractUndertakerTypes: cooperationContractModel.CooperationContractUndertakerTypes, 92 CooperationContractUndertakerTypes: cooperationContractModel.CooperationContractUndertakerTypes,
95 CooperationContractSponsor: cooperationContractModel.CooperationContractSponsor, 93 CooperationContractSponsor: cooperationContractModel.CooperationContractSponsor,
96 CooperationMode: &domain.CooperationMode{ 94 CooperationMode: &domain.CooperationMode{
@@ -64,8 +64,6 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -64,8 +64,6 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
64 &cooperationContract.CooperationContractDescription, 64 &cooperationContract.CooperationContractDescription,
65 &cooperationContract.CooperationContractName, 65 &cooperationContract.CooperationContractName,
66 &cooperationContract.CooperationContractNumber, 66 &cooperationContract.CooperationContractNumber,
67 - &cooperationContract.CooperationContractReferrer,  
68 - &cooperationContract.CooperationContractSalesman,  
69 pg.Array(&cooperationContract.CooperationContractUndertakerTypes), 67 pg.Array(&cooperationContract.CooperationContractUndertakerTypes),
70 &cooperationContract.CooperationContractSponsor, 68 &cooperationContract.CooperationContractSponsor,
71 &cooperationContract.CooperationMode.CooperationModeNumber, 69 &cooperationContract.CooperationMode.CooperationModeNumber,
@@ -83,8 +81,6 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -83,8 +81,6 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
83 cooperationContract.CooperationContractDescription, 81 cooperationContract.CooperationContractDescription,
84 cooperationContract.CooperationContractName, 82 cooperationContract.CooperationContractName,
85 cooperationContract.CooperationContractNumber, 83 cooperationContract.CooperationContractNumber,
86 - cooperationContract.CooperationContractReferrer,  
87 - cooperationContract.CooperationContractSalesman,  
88 pg.Array(cooperationContract.CooperationContractUndertakerTypes), 84 pg.Array(cooperationContract.CooperationContractUndertakerTypes),
89 cooperationContract.CooperationContractSponsor, 85 cooperationContract.CooperationContractSponsor,
90 cooperationContract.CooperationMode.CooperationModeNumber, 86 cooperationContract.CooperationMode.CooperationModeNumber,
@@ -200,8 +196,6 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -200,8 +196,6 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
200 &cooperationContract.CooperationContractDescription, 196 &cooperationContract.CooperationContractDescription,
201 &cooperationContract.CooperationContractName, 197 &cooperationContract.CooperationContractName,
202 &cooperationContract.CooperationContractNumber, 198 &cooperationContract.CooperationContractNumber,
203 - &cooperationContract.CooperationContractReferrer,  
204 - &cooperationContract.CooperationContractSalesman,  
205 pg.Array(&cooperationContract.CooperationContractUndertakerTypes), 199 pg.Array(&cooperationContract.CooperationContractUndertakerTypes),
206 &cooperationContract.CooperationContractSponsor, 200 &cooperationContract.CooperationContractSponsor,
207 &cooperationContract.CooperationMode.CooperationModeNumber, 201 &cooperationContract.CooperationMode.CooperationModeNumber,
@@ -219,8 +213,6 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -219,8 +213,6 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
219 cooperationContract.CooperationContractDescription, 213 cooperationContract.CooperationContractDescription,
220 cooperationContract.CooperationContractName, 214 cooperationContract.CooperationContractName,
221 cooperationContract.CooperationContractNumber, 215 cooperationContract.CooperationContractNumber,
222 - cooperationContract.CooperationContractReferrer,  
223 - cooperationContract.CooperationContractSalesman,  
224 pg.Array(cooperationContract.CooperationContractUndertakerTypes), 216 pg.Array(cooperationContract.CooperationContractUndertakerTypes),
225 cooperationContract.CooperationContractSponsor, 217 cooperationContract.CooperationContractSponsor,
226 cooperationContract.CooperationMode.CooperationModeNumber, 218 cooperationContract.CooperationMode.CooperationModeNumber,
@@ -237,8 +229,7 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -237,8 +229,7 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
237 return cooperationContract, err 229 return cooperationContract, err
238 } 230 }
239 231
240 - // 更新相关人  
241 - 232 + /******************************* 更新相关人 ****************************/
242 // 查找相关人列表 233 // 查找相关人列表
243 var cooperationContractRelevantModelsFetched []*models.CooperationContractRelevant 234 var cooperationContractRelevantModelsFetched []*models.CooperationContractRelevant
244 cooperationContractRelevantQuery := tx.Model(&cooperationContractRelevantModelsFetched) 235 cooperationContractRelevantQuery := tx.Model(&cooperationContractRelevantModelsFetched)
@@ -324,16 +315,93 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -324,16 +315,93 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
324 return nil, err 315 return nil, err
325 } 316 }
326 317
327 - //TODO 更新承接人  
328 - 318 + /************************* 更新承接人 ****************************/
329 // 获取承接人列表 319 // 获取承接人列表
330 - var cooperationContractUndertakersFetched []*models.CooperationContractUndertaker  
331 - cooperationContractUndertakerQuery := tx.Model(&cooperationContractUndertakersFetched) 320 + var cooperationContractUndertakersModelsFetched []*models.CooperationContractUndertaker
  321 + cooperationContractUndertakerQuery := tx.Model(&cooperationContractUndertakersModelsFetched)
332 if err := cooperationContractUndertakerQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil { 322 if err := cooperationContractUndertakerQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
333 return nil, err 323 return nil, err
334 } 324 }
335 325
336 - //TODO 更新分红激励规则 326 + // 提取承接人id
  327 + var cooperationContractUndertakersIdsFetched []int64
  328 + for _, cooperationContractUndertaker := range cooperationContractUndertakersModelsFetched {
  329 + cooperationContractUndertakersIdsFetched = append(cooperationContractUndertakersIdsFetched, cooperationContractUndertaker.CooperationContractUndertakerId)
  330 + }
  331 +
  332 + // 待更新相关人
  333 + var cooperationContractUndertakersToUpdate []*domain.Undertaker
  334 +
  335 + // 待添加相关人
  336 + var cooperationContractUndertakersToAdd []*domain.Undertaker
  337 + for _, undertaker := range cooperationContract.Undertakers {
  338 + if undertaker.UndertakerId != 0 {
  339 + cooperationContractUndertakersToUpdate = append(cooperationContractUndertakersToUpdate, undertaker)
  340 + } else {
  341 + cooperationContractUndertakersToAdd = append(cooperationContractUndertakersToAdd, undertaker)
  342 + }
  343 + }
  344 +
  345 + // 将待添加的相关人领域模型转换为数据模型
  346 + var cooperationContractUndertakersToAddModels []*models.CooperationContractUndertaker
  347 + for _, undertakerDomain := range cooperationContractUndertakersToAdd {
  348 + cooperationContractUndertakersToAddModels = append(cooperationContractUndertakersToAddModels, &models.CooperationContractUndertaker{
  349 + CooperationContractNumber: undertakerDomain.CooperationContractNumber,
  350 + UserId: undertakerDomain.UserId,
  351 + UserBaseId: undertakerDomain.UserBaseId,
  352 + Org: undertakerDomain.Org,
  353 + Orgs: undertakerDomain.Orgs,
  354 + Department: undertakerDomain.Department,
  355 + Role: undertakerDomain.Role,
  356 + UserInfo: undertakerDomain.UserInfo,
  357 + UserType: undertakerDomain.UserType,
  358 + Status: undertakerDomain.Status,
  359 + Company: undertakerDomain.Company,
  360 + UpdatedAt: time.Time{},
  361 + DeletedAt: time.Time{},
  362 + CreatedAt: time.Now(),
  363 + })
  364 + }
  365 + // 添加承接人
  366 + if _, err := tx.Model(&cooperationContractUndertakersToAddModels).Insert(); err != nil {
  367 + return nil, err
  368 + }
  369 +
  370 + // 待更新或者删除的ids
  371 + var cooperationContractUndertakersToUpdateOrDeleteIds []int64
  372 + for _, cooperationContractUndertakerToUpdate := range cooperationContractUndertakersToUpdate {
  373 + cooperationContractUndertakersToUpdateOrDeleteIds = append(cooperationContractUndertakersToUpdateOrDeleteIds, cooperationContractUndertakerToUpdate.UndertakerId)
  374 + }
  375 +
  376 + // 待更新的承接人id
  377 + undertakerIdsToUpdate := utils.Intersect(cooperationContractUndertakersIdsFetched, cooperationContractUndertakersToUpdateOrDeleteIds)
  378 + var undertakerModelsToUpdate []*models.CooperationContractUndertaker
  379 + for _, id := range undertakerIdsToUpdate {
  380 + for _, undertakerModel := range cooperationContractUndertakersModelsFetched {
  381 + if undertakerModel.CooperationContractUndertakerId == id {
  382 + undertakerModelsToUpdate = append(undertakerModelsToUpdate, undertakerModel)
  383 + }
  384 + }
  385 + }
  386 + if _, err := tx.Model(&undertakerModelsToUpdate).WherePK().Update(); err != nil {
  387 + return nil, err
  388 + }
  389 +
  390 + // 待删除的相关人id
  391 + undertakerIdsToDelete := utils.Difference(cooperationContractUndertakersIdsFetched, cooperationContractUndertakersToUpdateOrDeleteIds)
  392 + var undertakerModelsToDelete []*models.CooperationContractUndertaker
  393 + for _, id := range undertakerIdsToDelete {
  394 + for _, undertakerModel := range cooperationContractUndertakersModelsFetched {
  395 + if undertakerModel.CooperationContractUndertakerId == id {
  396 + undertakerModelsToDelete = append(undertakerModelsToDelete, undertakerModel)
  397 + }
  398 + }
  399 + }
  400 + if _, err := tx.Model(&relevantModelsToDelete).WherePK().Delete(); err != nil {
  401 + return nil, err
  402 + }
  403 +
  404 + //TODO /************************ 更新分红激励规则 **************************/
337 405
338 // 获取分红激励规则列表 406 // 获取分红激励规则列表
339 var dividendsIncentivesRulesFetched []*models.DividendsIncentivesRule 407 var dividendsIncentivesRulesFetched []*models.DividendsIncentivesRule
@@ -342,7 +410,7 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -342,7 +410,7 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
342 return nil, err 410 return nil, err
343 } 411 }
344 412
345 - //TODO 更新金额激励规则 413 + //TODO /********************** 更新金额激励规则 **************************/
346 414
347 // 获取金额激励规则列表 415 // 获取金额激励规则列表
348 var moneyIncentivesRulesFetched []*models.MoneyIncentivesRule 416 var moneyIncentivesRulesFetched []*models.MoneyIncentivesRule