作者 陈志颖

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,40 +276,44 @@ func (repository *CooperationApplicationRepository) Find(queryOptions map[string @@ -276,40 +276,44 @@ 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 - } 290 + } else {
  291 + if countProject > 0 {
292 // 获取共创模式 292 // 获取共创模式
293 - cooperationModeModel := new(models.CooperationMode)  
294 - cooperationModeQuery := tx.Model(cooperationModeModel)  
295 - cooperationModeQuery.AllWithDeleted()  
296 - if err := cooperationModeQuery. 293 + var cooperationModeModels []*models.CooperationMode
  294 + cooperationModeQuery := tx.Model(&cooperationModeModels)
  295 + if countMode, err := cooperationModeQuery.
297 Where("company->>'companyId' = '?'", cooperationApplicationModel.Company.CompanyId). 296 Where("company->>'companyId' = '?'", cooperationApplicationModel.Company.CompanyId).
298 Where("org->>'orgId' = '?'", cooperationApplicationModel.Org.OrgId). 297 Where("org->>'orgId' = '?'", cooperationApplicationModel.Org.OrgId).
299 - Where("cooperation_mode_number = ?", cooperationProjectModel.CooperationModeNumber).  
300 - First(); err != nil { 298 + Where("cooperation_mode_number = ?", cooperationProjectModels[0].CooperationModeNumber).
  299 + Limit(1).
  300 + SelectAndCount(); err != nil {
301 log.Logger.Error("申请关联的共创模式不存在", map[string]interface{}{ 301 log.Logger.Error("申请关联的共创模式不存在", map[string]interface{}{
302 "cooperationApplicationModel": cooperationApplicationModel, 302 "cooperationApplicationModel": cooperationApplicationModel,
303 }) 303 })
304 - return 0, nil, fmt.Errorf("申请关联的共创模式不存在")  
305 - }  
306 - if cooperationApplication, err := transform.TransformToCooperationApplicationDomainModelFromPgModels(cooperationApplicationModel, cooperationProjectModel, cooperationModeModel); err != nil { 304 + } else {
  305 + if countMode > 0 {
  306 + if cooperationApplication, err := transform.TransformToCooperationApplicationDomainModelFromPgModels(cooperationApplicationModel, cooperationProjectModels[0], cooperationModeModels[0]); err != nil {
307 return 0, cooperationApplications, err 307 return 0, cooperationApplications, err
308 } else { 308 } else {
309 cooperationApplications = append(cooperationApplications, cooperationApplication) 309 cooperationApplications = append(cooperationApplications, cooperationApplication)
310 } 310 }
311 } 311 }
312 } 312 }
  313 + }
  314 + }
  315 + }
  316 + }
313 return int64(count), cooperationApplications, nil 317 return int64(count), cooperationApplications, nil
314 } 318 }
315 } 319 }
@@ -1099,15 +1099,19 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in @@ -1099,15 +1099,19 @@ 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 - } 1108 + Limit(1).
  1109 + SelectAndCount(); err != nil {
  1110 + log.Logger.Error("合约关联的共创模式不存在", map[string]interface{}{
  1111 + "cooperationContractModel": cooperationContractModel,
  1112 + })
  1113 + } else {
  1114 + if countMode > 0 {
1111 // 获取分红激励规则列表 1115 // 获取分红激励规则列表
1112 var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule 1116 var dividendsIncentivesRuleModels []*models.DividendsIncentivesRule
1113 dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRuleModels) 1117 dividendsIncentivesRuleQuery := tx.Model(&dividendsIncentivesRuleModels)
@@ -1150,7 +1154,7 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in @@ -1150,7 +1154,7 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in
1150 } 1154 }
1151 if cooperationContract, err := transform.TransformToCooperationContractDomainModelFromPgModels( 1155 if cooperationContract, err := transform.TransformToCooperationContractDomainModelFromPgModels(
1152 cooperationContractModel, 1156 cooperationContractModel,
1153 - cooperationModeModel, 1157 + cooperationModeModels[0],
1154 dividendsIncentivesRuleModels, 1158 dividendsIncentivesRuleModels,
1155 moneyIncentivesRuleModels, 1159 moneyIncentivesRuleModels,
1156 cooperationContractRelevantModels, 1160 cooperationContractRelevantModels,
@@ -1161,6 +1165,8 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in @@ -1161,6 +1165,8 @@ func (repository *CooperationContractRepository) Find(queryOptions map[string]in
1161 } 1165 }
1162 } 1166 }
1163 } 1167 }
  1168 + }
  1169 + }
1164 return int64(count), cooperationContracts, nil 1170 return int64(count), cooperationContracts, nil
1165 } 1171 }
1166 } 1172 }
@@ -284,25 +284,28 @@ func (repository *CooperationProjectRepository) Find(queryOptions map[string]int @@ -284,25 +284,28 @@ 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 { 298 + } else {
  299 + if countMode > 0 {
  300 + if cooperationProject, err := transform.TransformToCooperationProjectDomainModelFromPgModels(cooperationProjectModel, cooperationModeModels[0]); err != nil {
300 return 0, cooperationProjects, err 301 return 0, cooperationProjects, err
301 } else { 302 } else {
302 cooperationProjects = append(cooperationProjects, cooperationProject) 303 cooperationProjects = append(cooperationProjects, cooperationProject)
303 } 304 }
304 } 305 }
305 } 306 }
  307 + }
  308 + }
306 return int64(count), cooperationProjects, nil 309 return int64(count), cooperationProjects, nil
307 } 310 }
308 } 311 }