作者 陈志颖

fix:共创项目名称唯一性校验

@@ -175,11 +175,20 @@ func (cooperationProjectService *CooperationProjectService) CreateCooperationPro @@ -175,11 +175,20 @@ func (cooperationProjectService *CooperationProjectService) CreateCooperationPro
175 "orgId": createCooperationProjectCommand.OrgId, 175 "orgId": createCooperationProjectCommand.OrgId,
176 "cooperationProjectNumber": projectNumber, 176 "cooperationProjectNumber": projectNumber,
177 }) 177 })
178 - // TODO 校验共创项目名称是否唯一  
179 -  
180 if !numberAvailable { 178 if !numberAvailable {
181 return nil, application.ThrowError(application.TRANSACTION_ERROR, "新增共创项目异常") 179 return nil, application.ThrowError(application.TRANSACTION_ERROR, "新增共创项目异常")
182 } 180 }
  181 +
  182 + // 校验共创项目名称是否唯一
  183 + nameAvailable, _ := cooperationProjectDao.CheckProjectNameAvailable(map[string]interface{}{
  184 + "companyId": createCooperationProjectCommand.CompanyId,
  185 + "orgId": createCooperationProjectCommand.OrgId,
  186 + "cooperationProjectName": createCooperationProjectCommand.CooperationProjectName,
  187 + })
  188 + if !nameAvailable {
  189 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "共创项目名称重复")
  190 + }
  191 +
183 newCooperationProject := &domain.CooperationProject{ 192 newCooperationProject := &domain.CooperationProject{
184 CooperationProjectNumber: projectNumber, 193 CooperationProjectNumber: projectNumber,
185 CooperationProjectName: createCooperationProjectCommand.CooperationProjectName, 194 CooperationProjectName: createCooperationProjectCommand.CooperationProjectName,
@@ -489,6 +498,16 @@ func (cooperationProjectService *CooperationProjectService) UpdateCooperationPro @@ -489,6 +498,16 @@ func (cooperationProjectService *CooperationProjectService) UpdateCooperationPro
489 operator = data 498 operator = data
490 } 499 }
491 500
  501 + // 共创项目DAO初始化
  502 + var cooperationProjectDao *dao.CooperationProjectDao
  503 + if value, err := factory.CreateCooperationProjectDao(map[string]interface{}{
  504 + "transactionContext": transactionContext,
  505 + }); err != nil {
  506 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  507 + } else {
  508 + cooperationProjectDao = value
  509 + }
  510 +
492 // 获取发起人 511 // 获取发起人
493 var sponsor *domain.User 512 var sponsor *domain.User
494 // sponsorUid类型转换 513 // sponsorUid类型转换
@@ -520,6 +539,18 @@ func (cooperationProjectService *CooperationProjectService) UpdateCooperationPro @@ -520,6 +539,18 @@ func (cooperationProjectService *CooperationProjectService) UpdateCooperationPro
520 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateCooperationProjectCommand.CooperationProjectId))) 539 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateCooperationProjectCommand.CooperationProjectId)))
521 } 540 }
522 541
  542 + // 校验共创项目名称是否唯一
  543 + if cooperationProject.CooperationProjectName != updateCooperationProjectCommand.CooperationProjectName {
  544 + nameAvailable, _ := cooperationProjectDao.CheckProjectNameAvailable(map[string]interface{}{
  545 + "companyId": updateCooperationProjectCommand.CompanyId,
  546 + "orgId": updateCooperationProjectCommand.OrgId,
  547 + "cooperationProjectName": updateCooperationProjectCommand.CooperationProjectName,
  548 + })
  549 + if !nameAvailable {
  550 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "共创项目名称重复")
  551 + }
  552 + }
  553 +
523 applicantTypes := make(map[int32]interface{}) 554 applicantTypes := make(map[int32]interface{})
524 555
525 // 获取所有申请人 556 // 获取所有申请人
@@ -104,6 +104,24 @@ func (dao *CooperationProjectDao) CheckProjectNumberAvailable(queryOptions map[s @@ -104,6 +104,24 @@ func (dao *CooperationProjectDao) CheckProjectNumberAvailable(queryOptions map[s
104 return !ok, err 104 return !ok, err
105 } 105 }
106 106
  107 +// CheckProjectNameAvailable 校验共创项目名称唯一
  108 +func (dao *CooperationProjectDao) CheckProjectNameAvailable(queryOptions map[string]interface{}) (bool, error) {
  109 + tx := dao.transactionContext.PgTx
  110 + var cooperationProjectModels []*models.CooperationProject
  111 + query := tx.Model(&cooperationProjectModels)
  112 + if cooperationProjectName, ok := queryOptions["cooperationProjectName"]; ok && cooperationProjectName != "" {
  113 + query = query.Where("cooperation_project_name = ?", cooperationProjectName)
  114 + }
  115 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  116 + query = query.Where(`cooperation_project.company @> '{"companyId":"?"}'`, companyId)
  117 + }
  118 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  119 + query = query.Where(`cooperation_project.org @> '{"orgId":"?"}'`, orgId)
  120 + }
  121 + ok, err := query.Exists()
  122 + return !ok, err
  123 +}
  124 +
107 func NewCooperationProjectDao(transactionContext *pgTransaction.TransactionContext) (*CooperationProjectDao, error) { 125 func NewCooperationProjectDao(transactionContext *pgTransaction.TransactionContext) (*CooperationProjectDao, error) {
108 if transactionContext == nil { 126 if transactionContext == nil {
109 return nil, fmt.Errorf("transactionContext参数不能为nil") 127 return nil, fmt.Errorf("transactionContext参数不能为nil")