作者 陈志颖

refactor:共创合约仓储优化

@@ -195,7 +195,7 @@ func (cooperationContractService *CooperationContractService) CreateCooperationC @@ -195,7 +195,7 @@ func (cooperationContractService *CooperationContractService) CreateCooperationC
195 UserId: undertakerDomain.UserId, 195 UserId: undertakerDomain.UserId,
196 CooperationContractNumber: contractNumber, 196 CooperationContractNumber: contractNumber,
197 UserBaseId: undertakerDomain.UserBaseId, 197 UserBaseId: undertakerDomain.UserBaseId,
198 - Org: undertakerDomain.Org, 198 + Org: organization,
199 Orgs: undertakerDomain.Orgs, 199 Orgs: undertakerDomain.Orgs,
200 Department: undertakerDomain.Department, 200 Department: undertakerDomain.Department,
201 Roles: undertakerDomain.Roles, 201 Roles: undertakerDomain.Roles,
@@ -227,14 +227,14 @@ func (cooperationContractService *CooperationContractService) CreateCooperationC @@ -227,14 +227,14 @@ func (cooperationContractService *CooperationContractService) CreateCooperationC
227 CooperationContractNumber: contractNumber, 227 CooperationContractNumber: contractNumber,
228 UserId: relevantDomain.UserId, 228 UserId: relevantDomain.UserId,
229 UserBaseId: relevantDomain.UserBaseId, 229 UserBaseId: relevantDomain.UserBaseId,
230 - Org: relevantDomain.Org, 230 + Org: organization,
231 Orgs: relevantDomain.Orgs, 231 Orgs: relevantDomain.Orgs,
232 Department: relevantDomain.Department, 232 Department: relevantDomain.Department,
233 Roles: relevantDomain.Roles, 233 Roles: relevantDomain.Roles,
234 UserInfo: relevantDomain.UserInfo, 234 UserInfo: relevantDomain.UserInfo,
235 UserType: relevantDomain.UserType, 235 UserType: relevantDomain.UserType,
236 Status: relevantDomain.Status, 236 Status: relevantDomain.Status,
237 - Company: relevantDomain.Company, 237 + Company: company,
238 }) 238 })
239 } 239 }
240 240
@@ -276,37 +276,41 @@ func (repository *CooperationApplicationRepository) Find(queryOptions map[string @@ -276,37 +276,41 @@ func (repository *CooperationApplicationRepository) Find(queryOptions map[string
276 if count > 0 { 276 if count > 0 {
277 for _, cooperationApplicationModel := range cooperationApplicationModels { 277 for _, cooperationApplicationModel := range cooperationApplicationModels {
278 // 获取共创项目 278 // 获取共创项目
279 - cooperationProjectModel := new(models.CooperationProject)  
280 - cooperationProjectQuery := tx.Model(cooperationProjectModel)  
281 - cooperationProjectQuery.AllWithDeleted()  
282 - if err := cooperationProjectQuery. 279 + var cooperationProjectModels []*models.CooperationProject
  280 + cooperationProjectQuery := tx.Model(&cooperationProjectModels)
  281 + if countProject, err := cooperationProjectQuery.
283 Where("company->>'companyId' = '?'", cooperationApplicationModel.Company.CompanyId). 282 Where("company->>'companyId' = '?'", cooperationApplicationModel.Company.CompanyId).
284 Where("org->>'orgId' = '?'", cooperationApplicationModel.Org.OrgId). 283 Where("org->>'orgId' = '?'", cooperationApplicationModel.Org.OrgId).
285 Where("cooperation_project_number = ?", cooperationApplicationModel.CooperationProjectNumber). 284 Where("cooperation_project_number = ?", cooperationApplicationModel.CooperationProjectNumber).
286 - First(); err != nil { 285 + Limit(1).
  286 + SelectAndCount(); err != nil {
287 log.Logger.Error("申请关联的共创项目不存在", map[string]interface{}{ 287 log.Logger.Error("申请关联的共创项目不存在", map[string]interface{}{
288 "cooperationApplicationModel": cooperationApplicationModel, 288 "cooperationApplicationModel": cooperationApplicationModel,
289 }) 289 })
290 - return 0, nil, fmt.Errorf("申请关联的共创项目不存在")  
291 - }  
292 - // 获取共创模式  
293 - cooperationModeModel := new(models.CooperationMode)  
294 - cooperationModeQuery := tx.Model(cooperationModeModel)  
295 - cooperationModeQuery.AllWithDeleted()  
296 - if err := cooperationModeQuery.  
297 - Where("company->>'companyId' = '?'", cooperationApplicationModel.Company.CompanyId).  
298 - Where("org->>'orgId' = '?'", cooperationApplicationModel.Org.OrgId).  
299 - Where("cooperation_mode_number = ?", cooperationProjectModel.CooperationModeNumber).  
300 - First(); err != nil {  
301 - log.Logger.Error("申请关联的共创模式不存在", map[string]interface{}{  
302 - "cooperationApplicationModel": cooperationApplicationModel,  
303 - })  
304 - return 0, nil, fmt.Errorf("申请关联的共创模式不存在")  
305 - }  
306 - if cooperationApplication, err := transform.TransformToCooperationApplicationDomainModelFromPgModels(cooperationApplicationModel, cooperationProjectModel, cooperationModeModel); err != nil {  
307 - return 0, cooperationApplications, err  
308 } else { 290 } else {
309 - cooperationApplications = append(cooperationApplications, cooperationApplication) 291 + if countProject > 0 {
  292 + // 获取共创模式
  293 + var cooperationModeModels []*models.CooperationMode
  294 + cooperationModeQuery := tx.Model(&cooperationModeModels)
  295 + if countMode, err := cooperationModeQuery.
  296 + Where("company->>'companyId' = '?'", cooperationApplicationModel.Company.CompanyId).
  297 + Where("org->>'orgId' = '?'", cooperationApplicationModel.Org.OrgId).
  298 + Where("cooperation_mode_number = ?", cooperationProjectModels[0].CooperationModeNumber).
  299 + Limit(1).
  300 + SelectAndCount(); err != nil {
  301 + log.Logger.Error("申请关联的共创模式不存在", map[string]interface{}{
  302 + "cooperationApplicationModel": cooperationApplicationModel,
  303 + })
  304 + } else {
  305 + if countMode > 0 {
  306 + if cooperationApplication, err := transform.TransformToCooperationApplicationDomainModelFromPgModels(cooperationApplicationModel, cooperationProjectModels[0], cooperationModeModels[0]); err != nil {
  307 + return 0, cooperationApplications, err
  308 + } else {
  309 + cooperationApplications = append(cooperationApplications, cooperationApplication)
  310 + }
  311 + }
  312 + }
  313 + }
310 } 314 }
311 } 315 }
312 } 316 }
@@ -1099,65 +1099,71 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in @@ -1099,65 +1099,71 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in
1099 if count > 0 { 1099 if count > 0 {
1100 for _, cooperationContractModel := range cooperationContractModels { 1100 for _, cooperationContractModel := range cooperationContractModels {
1101 // 获取共创模式 1101 // 获取共创模式
1102 - cooperationModeModel := new(models.CooperationMode)  
1103 - cooperationModeQuery := tx.Model(cooperationModeModel)  
1104 - if err := cooperationModeQuery. 1102 + var cooperationModeModels []*models.CooperationMode
  1103 + cooperationModeQuery := tx.Model(&cooperationModeModels)
  1104 + if countMode, err := cooperationModeQuery.
1105 Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId). 1105 Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).
1106 Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId). 1106 Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).
1107 Where("cooperation_mode_number = ?", cooperationContractModel.CooperationModeNumber). 1107 Where("cooperation_mode_number = ?", cooperationContractModel.CooperationModeNumber).
1108 - First(); err != nil {  
1109 - return 0, nil, fmt.Errorf("合约关联的共创模式不存在")  
1110 - }  
1111 - // 获取分红激励规则列表  
1112 - var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule  
1113 - dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRuleModels)  
1114 - if err := dividendsIncentivesRuleQuery.  
1115 - Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).  
1116 - Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).  
1117 - Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).  
1118 - Select(); err != nil {  
1119 - return 0, nil, fmt.Errorf("合约关联的分红激励规则列表不存在")  
1120 - }  
1121 - // 获取金额激励规则列表  
1122 - var moneyIncentivesRuleModels []*models.MoneyIncentivesRule  
1123 - moneyIncentivesRuleQuery := tx.Model(&moneyIncentivesRuleModels)  
1124 - if err := moneyIncentivesRuleQuery.  
1125 - Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).  
1126 - Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).  
1127 - Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).  
1128 - Select(); err != nil {  
1129 - return 0, nil, fmt.Errorf("合约关联的金额激励规则列表不存在")  
1130 - }  
1131 - // 获取承接人列表  
1132 - var cooperationContractUndertakerModels []*models.CooperationContractUndertaker  
1133 - cooperationContractUndertakerQuery := tx.Model(&cooperationContractUndertakerModels)  
1134 - if err := cooperationContractUndertakerQuery.  
1135 - Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).  
1136 - Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).  
1137 - Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).  
1138 - Select(); err != nil {  
1139 - return 0, nil, fmt.Errorf("合约相关的承接人列表不存在")  
1140 - }  
1141 - // 获取相关人列表  
1142 - var cooperationContractRelevantModels []*models.CooperationContractRelevant  
1143 - cooperationContractRelevantQuery := tx.Model(&cooperationContractRelevantModels)  
1144 - if err := cooperationContractRelevantQuery.  
1145 - Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).  
1146 - Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).  
1147 - Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).  
1148 - Select(); err != nil {  
1149 - return 0, nil, fmt.Errorf("合约相关的相关人列表不存在")  
1150 - }  
1151 - if cooperationContract, err := transform.TransformToCooperationContractDomainModelFromPgModels(  
1152 - cooperationContractModel,  
1153 - cooperationModeModel,  
1154 - dividendsIncentivesRuleModels,  
1155 - moneyIncentivesRuleModels,  
1156 - cooperationContractRelevantModels,  
1157 - cooperationContractUndertakerModels); err != nil {  
1158 - return 0, cooperationContracts, err 1108 + Limit(1).
  1109 + SelectAndCount(); err != nil {
  1110 + log.Logger.Error("合约关联的共创模式不存在", map[string]interface{}{
  1111 + "cooperationContractModel": cooperationContractModel,
  1112 + })
1159 } else { 1113 } else {
1160 - cooperationContracts = append(cooperationContracts, cooperationContract) 1114 + if countMode > 0 {
  1115 + // 获取分红激励规则列表
  1116 + var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule
  1117 + dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRuleModels)
  1118 + if err := dividendsIncentivesRuleQuery.
  1119 + Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).
  1120 + Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).
  1121 + Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).
  1122 + Select(); err != nil {
  1123 + return 0, nil, fmt.Errorf("合约关联的分红激励规则列表不存在")
  1124 + }
  1125 + // 获取金额激励规则列表
  1126 + var moneyIncentivesRuleModels []*models.MoneyIncentivesRule
  1127 + moneyIncentivesRuleQuery := tx.Model(&moneyIncentivesRuleModels)
  1128 + if err := moneyIncentivesRuleQuery.
  1129 + Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).
  1130 + Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).
  1131 + Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).
  1132 + Select(); err != nil {
  1133 + return 0, nil, fmt.Errorf("合约关联的金额激励规则列表不存在")
  1134 + }
  1135 + // 获取承接人列表
  1136 + var cooperationContractUndertakerModels []*models.CooperationContractUndertaker
  1137 + cooperationContractUndertakerQuery := tx.Model(&cooperationContractUndertakerModels)
  1138 + if err := cooperationContractUndertakerQuery.
  1139 + Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).
  1140 + Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).
  1141 + Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).
  1142 + Select(); err != nil {
  1143 + return 0, nil, fmt.Errorf("合约相关的承接人列表不存在")
  1144 + }
  1145 + // 获取相关人列表
  1146 + var cooperationContractRelevantModels []*models.CooperationContractRelevant
  1147 + cooperationContractRelevantQuery := tx.Model(&cooperationContractRelevantModels)
  1148 + if err := cooperationContractRelevantQuery.
  1149 + Where("company->>'companyId' = '?'", cooperationContractModel.Company.CompanyId).
  1150 + Where("org->>'orgId' = '?'", cooperationContractModel.Org.OrgId).
  1151 + Where("cooperation_contract_number = ?", cooperationContractModel.CooperationContractNumber).
  1152 + Select(); err != nil {
  1153 + return 0, nil, fmt.Errorf("合约相关的相关人列表不存在")
  1154 + }
  1155 + if cooperationContract, err := transform.TransformToCooperationContractDomainModelFromPgModels(
  1156 + cooperationContractModel,
  1157 + cooperationModeModels[0],
  1158 + dividendsIncentivesRuleModels,
  1159 + moneyIncentivesRuleModels,
  1160 + cooperationContractRelevantModels,
  1161 + cooperationContractUndertakerModels); err != nil {
  1162 + return 0, cooperationContracts, err
  1163 + } else {
  1164 + cooperationContracts = append(cooperationContracts, cooperationContract)
  1165 + }
  1166 + }
1161 } 1167 }
1162 } 1168 }
1163 } 1169 }
@@ -284,22 +284,25 @@ func (repository *CooperationProjectRepository) Find(queryOptions map[string]int @@ -284,22 +284,25 @@ func (repository *CooperationProjectRepository) Find(queryOptions map[string]int
284 if count > 0 { 284 if count > 0 {
285 for _, cooperationProjectModel := range cooperationProjectModels { 285 for _, cooperationProjectModel := range cooperationProjectModels {
286 // 获取共创模式 286 // 获取共创模式
287 - cooperationModeModel := new(models.CooperationMode)  
288 - cooperationModeQuery := tx.Model(cooperationModeModel)  
289 - if err := cooperationModeQuery. 287 + var cooperationModeModels []*models.CooperationMode
  288 + cooperationModeQuery := tx.Model(&cooperationModeModels)
  289 + if countMode, err := cooperationModeQuery.
290 Where("company->>'companyId' = '?'", cooperationProjectModel.Company.CompanyId). 290 Where("company->>'companyId' = '?'", cooperationProjectModel.Company.CompanyId).
291 Where("org->>'orgId' = '?'", cooperationProjectModel.Org.OrgId). 291 Where("org->>'orgId' = '?'", cooperationProjectModel.Org.OrgId).
292 Where("cooperation_mode_number = ?", cooperationProjectModel.CooperationModeNumber). 292 Where("cooperation_mode_number = ?", cooperationProjectModel.CooperationModeNumber).
293 - First(); err != nil { 293 + Limit(1).
  294 + SelectAndCount(); err != nil {
294 log.Logger.Error("共创项目关联的共创模式不存在", map[string]interface{}{ 295 log.Logger.Error("共创项目关联的共创模式不存在", map[string]interface{}{
295 "cooperationProjectModel": cooperationProjectModel, 296 "cooperationProjectModel": cooperationProjectModel,
296 }) 297 })
297 - return 0, nil, fmt.Errorf("共创项目关联的共创模式不存在")  
298 - }  
299 - if cooperationProject, err := transform.TransformToCooperationProjectDomainModelFromPgModels(cooperationProjectModel, cooperationModeModel); err != nil {  
300 - return 0, cooperationProjects, err  
301 } else { 298 } else {
302 - cooperationProjects = append(cooperationProjects, cooperationProject) 299 + if countMode > 0 {
  300 + if cooperationProject, err := transform.TransformToCooperationProjectDomainModelFromPgModels(cooperationProjectModel, cooperationModeModels[0]); err != nil {
  301 + return 0, cooperationProjects, err
  302 + } else {
  303 + cooperationProjects = append(cooperationProjects, cooperationProject)
  304 + }
  305 + }
303 } 306 }
304 } 307 }
305 } 308 }