作者 陈志颖

feat:共创模式删除时增加业务数据校验

@@ -92,6 +92,7 @@ func (cooperationModeService *CooperationModeService) CreateCooperationMode(crea @@ -92,6 +92,7 @@ func (cooperationModeService *CooperationModeService) CreateCooperationMode(crea
92 } else { 92 } else {
93 cooperationModeDao = value 93 cooperationModeDao = value
94 } 94 }
  95 +
95 // 校验共创模式编码唯一性 96 // 校验共创模式编码唯一性
96 numberAvailable, _ := cooperationModeDao.CheckModeNumberAvailable(map[string]interface{}{ 97 numberAvailable, _ := cooperationModeDao.CheckModeNumberAvailable(map[string]interface{}{
97 "companyId": createCooperationModeCommand.CompanyId, 98 "companyId": createCooperationModeCommand.CompanyId,
@@ -231,6 +232,8 @@ func (cooperationModeService *CooperationModeService) RemoveCooperationMode(remo @@ -231,6 +232,8 @@ func (cooperationModeService *CooperationModeService) RemoveCooperationMode(remo
231 defer func() { 232 defer func() {
232 _ = transactionContext.RollbackTransaction() 233 _ = transactionContext.RollbackTransaction()
233 }() 234 }()
  235 +
  236 + // 共创模式仓储初始化
234 var cooperationModeRepository domain.CooperationModeRepository 237 var cooperationModeRepository domain.CooperationModeRepository
235 if value, err := factory.CreateCooperationModeRepository(map[string]interface{}{ 238 if value, err := factory.CreateCooperationModeRepository(map[string]interface{}{
236 "transactionContext": transactionContext, 239 "transactionContext": transactionContext,
@@ -239,6 +242,18 @@ func (cooperationModeService *CooperationModeService) RemoveCooperationMode(remo @@ -239,6 +242,18 @@ func (cooperationModeService *CooperationModeService) RemoveCooperationMode(remo
239 } else { 242 } else {
240 cooperationModeRepository = value 243 cooperationModeRepository = value
241 } 244 }
  245 +
  246 + // 共创模式DAO初始化
  247 + var cooperationModeDao *dao.CooperationModeDao
  248 + if value, err := factory.CreateCooperationModeDao(map[string]interface{}{
  249 + "transactionContext": transactionContext,
  250 + }); err != nil {
  251 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  252 + } else {
  253 + cooperationModeDao = value
  254 + }
  255 +
  256 + // 获取共创模式
242 cooperationMode, err := cooperationModeRepository.FindOne(map[string]interface{}{"cooperationModeId": removeCooperationModeCommand.CooperationModeId}) 257 cooperationMode, err := cooperationModeRepository.FindOne(map[string]interface{}{"cooperationModeId": removeCooperationModeCommand.CooperationModeId})
243 if err != nil { 258 if err != nil {
244 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "共创模式不存在") 259 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "共创模式不存在")
@@ -246,13 +261,25 @@ func (cooperationModeService *CooperationModeService) RemoveCooperationMode(remo @@ -246,13 +261,25 @@ func (cooperationModeService *CooperationModeService) RemoveCooperationMode(remo
246 if cooperationMode == nil { 261 if cooperationMode == nil {
247 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(removeCooperationModeCommand.CooperationModeId, 10))) 262 return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(removeCooperationModeCommand.CooperationModeId, 10)))
248 } 263 }
249 - if cooperationMode, err := cooperationModeRepository.Remove(cooperationMode); err != nil { 264 +
  265 + // 校验共创模式是否可以删除
  266 + deleteAvailable, _ := cooperationModeDao.CheckModeIsDeleteAvailable(map[string]interface{}{
  267 + "companyId": removeCooperationModeCommand.CompanyId,
  268 + "orgId": removeCooperationModeCommand.OrgId,
  269 + "cooperationModeNumber": cooperationMode.CooperationModeNumber,
  270 + })
  271 + if !deleteAvailable {
  272 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "当前共创模式存在业务数据,不可删除")
  273 + }
  274 +
  275 + // 移除共创模式
  276 + if cooperationModeRemoved, err := cooperationModeRepository.Remove(cooperationMode); err != nil {
250 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 277 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
251 } else { 278 } else {
252 if err := transactionContext.CommitTransaction(); err != nil { 279 if err := transactionContext.CommitTransaction(); err != nil {
253 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 280 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
254 } 281 }
255 - return cooperationMode, nil 282 + return cooperationModeRemoved, nil
256 } 283 }
257 } 284 }
258 285
@@ -271,6 +298,8 @@ func (cooperationModeService *CooperationModeService) BatchRemoveCooperationMode @@ -271,6 +298,8 @@ func (cooperationModeService *CooperationModeService) BatchRemoveCooperationMode
271 defer func() { 298 defer func() {
272 _ = transactionContext.RollbackTransaction() 299 _ = transactionContext.RollbackTransaction()
273 }() 300 }()
  301 +
  302 + // 共创模式仓储初始化
274 var cooperationModeRepository domain.CooperationModeRepository 303 var cooperationModeRepository domain.CooperationModeRepository
275 if value, err := factory.CreateCooperationModeRepository(map[string]interface{}{ 304 if value, err := factory.CreateCooperationModeRepository(map[string]interface{}{
276 "transactionContext": transactionContext, 305 "transactionContext": transactionContext,
@@ -279,13 +308,41 @@ func (cooperationModeService *CooperationModeService) BatchRemoveCooperationMode @@ -279,13 +308,41 @@ func (cooperationModeService *CooperationModeService) BatchRemoveCooperationMode
279 } else { 308 } else {
280 cooperationModeRepository = value 309 cooperationModeRepository = value
281 } 310 }
282 - cooperationModeIds, _ := utils.SliceAtoi(batchRemoveCooperationModeCommand.CooperationModeIds) 311 +
  312 + // 共创模式ID列表类型转换
  313 + cooperationModeIds, err := utils.SliceAtoi(batchRemoveCooperationModeCommand.CooperationModeIds)
  314 + if err != nil {
  315 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "共创模式类型错误")
  316 + }
  317 +
  318 + // 共创模式DAO初始化
  319 + var cooperationModeDao *dao.CooperationModeDao
  320 + if value, err := factory.CreateCooperationModeDao(map[string]interface{}{
  321 + "transactionContext": transactionContext,
  322 + }); err != nil {
  323 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  324 + } else {
  325 + cooperationModeDao = value
  326 + }
  327 +
  328 + // 获取共创模式
283 if count, cooperationModes, err := cooperationModeRepository.Find(map[string]interface{}{ 329 if count, cooperationModes, err := cooperationModeRepository.Find(map[string]interface{}{
284 "cooperationModeIds": cooperationModeIds, 330 "cooperationModeIds": cooperationModeIds,
285 }); err != nil { 331 }); err != nil {
286 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 332 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
287 } else { 333 } else {
288 if count > 0 { 334 if count > 0 {
  335 + for _, cooperationMode := range cooperationModes {
  336 + // 校验共创模式是否可以删除
  337 + deleteAvailable, _ := cooperationModeDao.CheckModeIsDeleteAvailable(map[string]interface{}{
  338 + "companyId": batchRemoveCooperationModeCommand.CompanyId,
  339 + "orgId": batchRemoveCooperationModeCommand.OrgId,
  340 + "cooperationModeNumber": cooperationMode.CooperationModeNumber,
  341 + })
  342 + if !deleteAvailable {
  343 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "所选共创模式存在业务数据,不可删除")
  344 + }
  345 + }
289 if cooperationMode, err := cooperationModeRepository.BatchRemove(cooperationModes); err != nil { 346 if cooperationMode, err := cooperationModeRepository.BatchRemove(cooperationModes); err != nil {
290 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 347 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
291 } else { 348 } else {
@@ -49,6 +49,24 @@ func (dao *CooperationModeDao) CheckModeNumberAvailable(queryOptions map[string] @@ -49,6 +49,24 @@ func (dao *CooperationModeDao) CheckModeNumberAvailable(queryOptions map[string]
49 return !ok, err 49 return !ok, err
50 } 50 }
51 51
  52 +// CheckModeIsDeleteAvailable 校验模式是否有项目数据关联
  53 +func (dao *CooperationModeDao) CheckModeIsDeleteAvailable(queryOptions map[string]interface{}) (bool, error) {
  54 + tx := dao.transactionContext.PgTx
  55 + var cooperationProjectModels []*models.CooperationProject
  56 + query := tx.Model(&cooperationProjectModels)
  57 + if cooperationModeNumber, ok := queryOptions["cooperationModeNumber"]; ok && cooperationModeNumber != "" {
  58 + query = query.Where("cooperation_mode_number = ?", cooperationModeNumber)
  59 + }
  60 + if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
  61 + query = query.Where(`cooperation_project.company @> '{"companyId":"?"}'`, companyId)
  62 + }
  63 + if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
  64 + query = query.Where(`cooperation_project.org @> '{"orgId":"?"}'`, orgId)
  65 + }
  66 + ok, err := query.Exists()
  67 + return !ok, err
  68 +}
  69 +
52 // UpdateCooperationModeSlice 更新多个共创模式 70 // UpdateCooperationModeSlice 更新多个共创模式
53 func (dao *CooperationModeDao) UpdateCooperationModeSlice(modeIds []int64, status int32) ([]*domain.CooperationMode, error) { 71 func (dao *CooperationModeDao) UpdateCooperationModeSlice(modeIds []int64, status int32) ([]*domain.CooperationMode, error) {
54 tx := dao.transactionContext.PgTx 72 tx := dao.transactionContext.PgTx