作者 陈志颖

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

... ... @@ -401,8 +401,7 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
return nil, err
}
//TODO /************************ 更新分红激励规则 **************************/
/************************ 更新分红激励规则 **************************/
// 获取分红激励规则列表
var dividendsIncentivesRulesFetched []*models.DividendsIncentivesRule
dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRulesFetched)
... ... @@ -485,14 +484,89 @@ func (repository *CooperationContractRepository) Save(cooperationContract *domai
return nil, err
}
//TODO /********************** 更新金额激励规则 **************************/
// 获取金额激励规则列表
/********************** 更新金额激励规则 **************************/
var moneyIncentivesRulesFetched []*models.MoneyIncentivesRule
moneyIncentivesRuleQuery := tx.Model(&moneyIncentivesRulesFetched)
if err := moneyIncentivesRuleQuery.Where("cooperation_contract_number = ?", cooperationContract.CooperationContractNumber).Select(); err != nil {
return nil, err
}
// 提取金额激励规则id
var moneyIncentivesRuleIdsFetched []int64
for _, moneyIncentivesRule := range moneyIncentivesRulesFetched {
moneyIncentivesRuleIdsFetched = append(moneyIncentivesRuleIdsFetched, moneyIncentivesRule.MoneyIncentivesRuleId)
}
// 待更新金额激励规则
var moneyIncentivesRulesToUpdate []*domain.MoneyIncentivesRule
// 待添加金额激励规则
var moneyIncentivesRulesToAdd []*domain.MoneyIncentivesRule
for _, moneyIncentivesRule := range cooperationContract.MoneyIncentivesRules {
if moneyIncentivesRule.MoneyIncentivesRuleId != 0 {
moneyIncentivesRulesToUpdate = append(moneyIncentivesRulesToUpdate, moneyIncentivesRule)
} else {
moneyIncentivesRulesToAdd = append(moneyIncentivesRulesToAdd, moneyIncentivesRule)
}
}
// 将待添加的金额激励规则领域模型转换为数据模型
var moneyIncentivesRulesToAddModels []*models.MoneyIncentivesRule
for _, moneyIncentivesRuleDomain := range moneyIncentivesRulesToAdd {
moneyIncentivesRulesToAddModels = append(moneyIncentivesRulesToAddModels, &models.MoneyIncentivesRule{
CooperationContractNumber: moneyIncentivesRuleDomain.CooperationContractNumber,
MoneyIncentivesAmount: moneyIncentivesRuleDomain.MoneyIncentivesAmount,
MoneyIncentivesStage: moneyIncentivesRuleDomain.MoneyIncentivesStage,
MoneyIncentivesStageEnd: moneyIncentivesRuleDomain.MoneyIncentivesStageEnd,
MoneyIncentivesStageStart: moneyIncentivesRuleDomain.MoneyIncentivesStageStart,
MoneyIncentivesTime: moneyIncentivesRuleDomain.MoneyIncentivesTime,
ReferrerPercentage: moneyIncentivesRuleDomain.ReferrerPercentage,
SalesmanPercentage: moneyIncentivesRuleDomain.SalesmanPercentage,
Org: moneyIncentivesRuleDomain.Org,
Company: moneyIncentivesRuleDomain.Company,
UpdatedAt: time.Time{},
DeletedAt: time.Time{},
CreatedAt: time.Now(),
})
}
// 添加金额激励规则
if _, err := tx.Model(&moneyIncentivesRulesToAddModels).Insert(); err != nil {
return nil, err
}
// 待更新或者删除的ids
var moneyIncentivesRulesToUpdateOrDeleteIds []int64
for _, moneyIncentivesRuleToUpdate := range moneyIncentivesRulesToUpdate {
moneyIncentivesRulesToUpdateOrDeleteIds = append(moneyIncentivesRulesToUpdateOrDeleteIds, moneyIncentivesRuleToUpdate.MoneyIncentivesRuleId)
}
// 待更新的金额激励规则id
moneyRuleIdsToUpdate := utils.Intersect(moneyIncentivesRuleIdsFetched, moneyIncentivesRulesToUpdateOrDeleteIds)
var moneyRuleModelsToUpdate []*models.MoneyIncentivesRule
for _, id := range moneyRuleIdsToUpdate {
for _, moneyIncentivesRuleModel := range moneyIncentivesRulesFetched {
if moneyIncentivesRuleModel.MoneyIncentivesRuleId == id {
moneyRuleModelsToUpdate = append(moneyRuleModelsToUpdate, moneyIncentivesRuleModel)
}
}
}
if _, err := tx.Model(&moneyRuleModelsToUpdate).WherePK().Update(); err != nil {
return nil, err
}
// 待删除的金额激励规则id
moneyIncentivesRuleIdsToDelete := utils.Difference(moneyIncentivesRuleIdsFetched, moneyIncentivesRulesToUpdateOrDeleteIds)
var moneyIncentivesRuleModelsToDelete []*models.MoneyIncentivesRule
for _, id := range moneyIncentivesRuleIdsToDelete {
for _, moneyIncentivesRuleModel := range moneyIncentivesRulesFetched {
if moneyIncentivesRuleModel.MoneyIncentivesRuleId == id {
moneyIncentivesRuleModelsToDelete = append(moneyIncentivesRuleModelsToDelete, moneyIncentivesRuleModel)
}
}
}
if _, err := tx.Model(&moneyIncentivesRuleModelsToDelete).WherePK().Delete(); err != nil {
return nil, err
}
}
return cooperationContract, nil
}
... ...