作者 陈志颖

feat:共创合约仓储整合金额激励规则仓储

@@ -401,8 +401,7 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -401,8 +401,7 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
401 return nil, err 401 return nil, err
402 } 402 }
403 403
404 - //TODO /************************ 更新分红激励规则 **************************/  
405 - 404 + /************************ 更新分红激励规则 **************************/
406 // 获取分红激励规则列表 405 // 获取分红激励规则列表
407 var dividendsIncentivesRulesFetched []*models.DividendsIncentivesRule 406 var dividendsIncentivesRulesFetched []*models.DividendsIncentivesRule
408 dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRulesFetched) 407 dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRulesFetched)
@@ -485,14 +484,89 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai @@ -485,14 +484,89 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
485 return nil, err 484 return nil, err
486 } 485 }
487 486
488 - //TODO /********************** 更新金额激励规则 **************************/  
489 -  
490 - // 获取金额激励规则列表 487 + /********************** 更新金额激励规则 **************************/
491 var moneyIncentivesRulesFetched []*models.MoneyIncentivesRule 488 var moneyIncentivesRulesFetched []*models.MoneyIncentivesRule
492 moneyIncentivesRuleQuery := tx.Model(&moneyIncentivesRulesFetched) 489 moneyIncentivesRuleQuery := tx.Model(&moneyIncentivesRulesFetched)
493 if err := moneyIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil { 490 if err := moneyIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
494 return nil, err 491 return nil, err
495 } 492 }
  493 +
  494 + // 提取金额激励规则id
  495 + var moneyIncentivesRuleIdsFetched []int64
  496 + for _, moneyIncentivesRule := range moneyIncentivesRulesFetched {
  497 + moneyIncentivesRuleIdsFetched = append(moneyIncentivesRuleIdsFetched, moneyIncentivesRule.MoneyIncentivesRuleId)
  498 + }
  499 +
  500 + // 待更新金额激励规则
  501 + var moneyIncentivesRulesToUpdate []*domain.MoneyIncentivesRule
  502 + // 待添加金额激励规则
  503 + var moneyIncentivesRulesToAdd []*domain.MoneyIncentivesRule
  504 + for _, moneyIncentivesRule := range cooperationContract.MoneyIncentivesRules {
  505 + if moneyIncentivesRule.MoneyIncentivesRuleId != 0 {
  506 + moneyIncentivesRulesToUpdate = append(moneyIncentivesRulesToUpdate, moneyIncentivesRule)
  507 + } else {
  508 + moneyIncentivesRulesToAdd = append(moneyIncentivesRulesToAdd, moneyIncentivesRule)
  509 + }
  510 + }
  511 +
  512 + // 将待添加的金额激励规则领域模型转换为数据模型
  513 + var moneyIncentivesRulesToAddModels []*models.MoneyIncentivesRule
  514 + for _, moneyIncentivesRuleDomain := range moneyIncentivesRulesToAdd {
  515 + moneyIncentivesRulesToAddModels = append(moneyIncentivesRulesToAddModels, &models.MoneyIncentivesRule{
  516 + CooperationContractNumber: moneyIncentivesRuleDomain.CooperationContractNumber,
  517 + MoneyIncentivesAmount: moneyIncentivesRuleDomain.MoneyIncentivesAmount,
  518 + MoneyIncentivesStage: moneyIncentivesRuleDomain.MoneyIncentivesStage,
  519 + MoneyIncentivesStageEnd: moneyIncentivesRuleDomain.MoneyIncentivesStageEnd,
  520 + MoneyIncentivesStageStart: moneyIncentivesRuleDomain.MoneyIncentivesStageStart,
  521 + MoneyIncentivesTime: moneyIncentivesRuleDomain.MoneyIncentivesTime,
  522 + ReferrerPercentage: moneyIncentivesRuleDomain.ReferrerPercentage,
  523 + SalesmanPercentage: moneyIncentivesRuleDomain.SalesmanPercentage,
  524 + Org: moneyIncentivesRuleDomain.Org,
  525 + Company: moneyIncentivesRuleDomain.Company,
  526 + UpdatedAt: time.Time{},
  527 + DeletedAt: time.Time{},
  528 + CreatedAt: time.Now(),
  529 + })
  530 + }
  531 + // 添加金额激励规则
  532 + if _, err := tx.Model(&moneyIncentivesRulesToAddModels).Insert(); err != nil {
  533 + return nil, err
  534 + }
  535 +
  536 + // 待更新或者删除的ids
  537 + var moneyIncentivesRulesToUpdateOrDeleteIds []int64
  538 + for _, moneyIncentivesRuleToUpdate := range moneyIncentivesRulesToUpdate {
  539 + moneyIncentivesRulesToUpdateOrDeleteIds = append(moneyIncentivesRulesToUpdateOrDeleteIds, moneyIncentivesRuleToUpdate.MoneyIncentivesRuleId)
  540 + }
  541 +
  542 + // 待更新的金额激励规则id
  543 + moneyRuleIdsToUpdate := utils.Intersect(moneyIncentivesRuleIdsFetched, moneyIncentivesRulesToUpdateOrDeleteIds)
  544 + var moneyRuleModelsToUpdate []*models.MoneyIncentivesRule
  545 + for _, id := range moneyRuleIdsToUpdate {
  546 + for _, moneyIncentivesRuleModel := range moneyIncentivesRulesFetched {
  547 + if moneyIncentivesRuleModel.MoneyIncentivesRuleId == id {
  548 + moneyRuleModelsToUpdate = append(moneyRuleModelsToUpdate, moneyIncentivesRuleModel)
  549 + }
  550 + }
  551 + }
  552 + if _, err := tx.Model(&moneyRuleModelsToUpdate).WherePK().Update(); err != nil {
  553 + return nil, err
  554 + }
  555 +
  556 + // 待删除的金额激励规则id
  557 + moneyIncentivesRuleIdsToDelete := utils.Difference(moneyIncentivesRuleIdsFetched, moneyIncentivesRulesToUpdateOrDeleteIds)
  558 + var moneyIncentivesRuleModelsToDelete []*models.MoneyIncentivesRule
  559 + for _, id := range moneyIncentivesRuleIdsToDelete {
  560 + for _, moneyIncentivesRuleModel := range moneyIncentivesRulesFetched {
  561 + if moneyIncentivesRuleModel.MoneyIncentivesRuleId == id {
  562 + moneyIncentivesRuleModelsToDelete = append(moneyIncentivesRuleModelsToDelete, moneyIncentivesRuleModel)
  563 + }
  564 + }
  565 + }
  566 + if _, err := tx.Model(&moneyIncentivesRuleModelsToDelete).WherePK().Delete(); err != nil {
  567 + return nil, err
  568 + }
  569 +
496 } 570 }
497 return cooperationContract, nil 571 return cooperationContract, nil
498 } 572 }