Commit d7dbb4817bbac448a46e2117375535be9f9b011e

Authored by YunaiV
1 parent 1c0c6710

review:【System 系统管理】批量删除接口

yudao-module-infra/src/main/resources/codegen/java/service/serviceImpl.vm
... ... @@ -175,8 +175,6 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
175 175 @Transactional(rollbackFor = Exception.class)
176 176 #end
177 177 public void delete${simpleClassName}ListByIds(List<${primaryColumn.javaType}> ids) {
178   - // 校验存在
179   - validate${simpleClassName}Exists(ids);
180 178 // 删除
181 179 ${classNameVar}Mapper.deleteByIds(ids);
182 180 ## 特殊:主子表专属逻辑
... ... @@ -193,12 +191,6 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
193 191 #end
194 192 }
195 193  
196   - private void validate${simpleClassName}Exists(List<${primaryColumn.javaType}> ids) {
197   - List<${table.className}DO> list = ${classNameVar}Mapper.selectByIds(ids);
198   - if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
199   - throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
200   - }
201   - }
202 194 #end
203 195  
204 196 private void validate${simpleClassName}Exists(${primaryColumn.javaType} id) {
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailAccountServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.mail;
2 2  
3   -import cn.hutool.core.collection.CollUtil;
4 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 4 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
6 5 import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO;
... ... @@ -71,20 +70,17 @@ public class MailAccountServiceImpl implements MailAccountService {
71 70 }
72 71  
73 72 @Override
74   - @CacheEvict(value = RedisKeyConstants.MAIL_ACCOUNT, allEntries = true)
  73 + @CacheEvict(value = RedisKeyConstants.MAIL_ACCOUNT,
  74 + allEntries = true) // allEntries 清空所有缓存,因为 Spring Cache 不支持按照 ids 批量删除
75 75 public void deleteMailAccountList(List<Long> ids) {
76   - if (CollUtil.isEmpty(ids)) {
77   - return;
78   - }
79   - // 校验是否存在
80   - validateMailAccountsExists(ids);
81   - // 校验是否存在关联模版
  76 + // 1. 校验是否存在关联模版
82 77 for (Long id : ids) {
83 78 if (mailTemplateService.getMailTemplateCountByAccountId(id) > 0) {
84 79 throw exception(MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS);
85 80 }
86 81 }
87   - // 批量删除
  82 +
  83 + // 2. 批量删除
88 84 mailAccountMapper.deleteByIds(ids);
89 85 }
90 86  
... ... @@ -94,17 +90,6 @@ public class MailAccountServiceImpl implements MailAccountService {
94 90 }
95 91 }
96 92  
97   - private void validateMailAccountsExists(List<Long> ids) {
98   - if (CollUtil.isEmpty(ids)) {
99   - return;
100   - }
101   - // 校验存在
102   - List<MailAccountDO> accounts = mailAccountMapper.selectByIds(ids);
103   - if (CollUtil.isEmpty(accounts) || accounts.size() != ids.size()) {
104   - throw exception(MAIL_ACCOUNT_NOT_EXISTS);
105   - }
106   - }
107   -
108 93 @Override
109 94 public MailAccountDO getMailAccount(Long id) {
110 95 return mailAccountMapper.selectById(id);
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.mail;
2 2  
3   -import cn.hutool.core.collection.CollUtil;
4 3 import cn.hutool.core.util.ObjUtil;
5 4 import cn.hutool.core.util.ReUtil;
6 5 import cn.hutool.core.util.StrUtil;
... ... @@ -102,12 +101,6 @@ public class MailTemplateServiceImpl implements MailTemplateService {
102 101 @CacheEvict(cacheNames = RedisKeyConstants.MAIL_TEMPLATE,
103 102 allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 code,不好清理
104 103 public void deleteMailTemplateList(List<Long> ids) {
105   - if (CollUtil.isEmpty(ids)) {
106   - return;
107   - }
108   - // 校验存在
109   - validateMailTemplatesExists(ids);
110   - // 批量删除
111 104 mailTemplateMapper.deleteByIds(ids);
112 105 }
113 106  
... ... @@ -117,17 +110,6 @@ public class MailTemplateServiceImpl implements MailTemplateService {
117 110 }
118 111 }
119 112  
120   - private void validateMailTemplatesExists(List<Long> ids) {
121   - if (CollUtil.isEmpty(ids)) {
122   - return;
123   - }
124   - // 校验存在
125   - List<MailTemplateDO> templates = mailTemplateMapper.selectByIds(ids);
126   - if (CollUtil.isEmpty(templates) || templates.size() != ids.size()) {
127   - throw exception(MAIL_TEMPLATE_NOT_EXISTS);
128   - }
129   - }
130   -
131 113 @Override
132 114 public MailTemplateDO getMailTemplate(Long id) {return mailTemplateMapper.selectById(id);}
133 115  
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notice/NoticeServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.notice;
2 2  
3   -import cn.hutool.core.collection.CollUtil;
4 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 4 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
6 5 import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO;
... ... @@ -53,9 +52,6 @@ public class NoticeServiceImpl implements NoticeService {
53 52  
54 53 @Override
55 54 public void deleteNoticeList(List<Long> ids) {
56   - // 校验是否存在
57   - validateNoticesExists(ids);
58   - // 批量删除通知公告
59 55 noticeMapper.deleteByIds(ids);
60 56 }
61 57  
... ... @@ -80,15 +76,4 @@ public class NoticeServiceImpl implements NoticeService {
80 76 }
81 77 }
82 78  
83   - private void validateNoticesExists(List<Long> ids) {
84   - if (CollUtil.isEmpty(ids)) {
85   - return;
86   - }
87   - // 校验存在
88   - List<NoticeDO> notices = noticeMapper.selectByIds(ids);
89   - if (CollUtil.isEmpty(notices) || notices.size() != ids.size()) {
90   - throw exception(NOTICE_NOT_FOUND);
91   - }
92   - }
93   -
94 79 }
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyTemplateServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.notify;
2 2  
3   -import cn.hutool.core.collection.CollUtil;
4 3 import cn.hutool.core.util.ReUtil;
5 4 import cn.hutool.core.util.StrUtil;
6 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
... ... @@ -90,23 +89,9 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
90 89 @CacheEvict(cacheNames = RedisKeyConstants.NOTIFY_TEMPLATE,
91 90 allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 code,不好清理
92 91 public void deleteNotifyTemplateList(List<Long> ids) {
93   - // 校验存在
94   - validateNotifyTemplatesExists(ids);
95   - // 批量删除
96 92 notifyTemplateMapper.deleteByIds(ids);
97 93 }
98 94  
99   - private void validateNotifyTemplatesExists(List<Long> ids) {
100   - if (CollUtil.isEmpty(ids)) {
101   - return;
102   - }
103   - // 校验存在
104   - List<NotifyTemplateDO> templates = notifyTemplateMapper.selectByIds(ids);
105   - if (CollUtil.isEmpty(templates) || templates.size() != ids.size()) {
106   - throw exception(NOTIFY_TEMPLATE_NOT_EXISTS);
107   - }
108   - }
109   -
110 95 private void validateNotifyTemplateExists(Long id) {
111 96 if (notifyTemplateMapper.selectById(id) == null) {
112 97 throw exception(NOTIFY_TEMPLATE_NOT_EXISTS);
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2ClientServiceImpl.java
... ... @@ -77,12 +77,6 @@ public class OAuth2ClientServiceImpl implements OAuth2ClientService {
77 77 @CacheEvict(cacheNames = RedisKeyConstants.OAUTH_CLIENT,
78 78 allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 key,不好清理
79 79 public void deleteOAuth2ClientList(List<Long> ids) {
80   - if (CollUtil.isEmpty(ids)) {
81   - return;
82   - }
83   - // 校验存在
84   - validateOAuth2ClientBatchExists(ids);
85   - // 批量删除
86 80 oauth2ClientMapper.deleteByIds(ids);
87 81 }
88 82  
... ... @@ -92,13 +86,6 @@ public class OAuth2ClientServiceImpl implements OAuth2ClientService {
92 86 }
93 87 }
94 88  
95   - private void validateOAuth2ClientBatchExists(List<Long> ids) {
96   - List<OAuth2ClientDO> clients = oauth2ClientMapper.selectByIds(ids);
97   - if (clients.size() != ids.size()) {
98   - throw exception(OAUTH2_CLIENT_NOT_EXISTS);
99   - }
100   - }
101   -
102 89 @VisibleForTesting
103 90 void validateClientIdExists(Long id, String clientId) {
104 91 OAuth2ClientDO client = oauth2ClientMapper.selectByClientId(clientId);
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsChannelServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.sms;
2 2  
3   -import cn.hutool.core.collection.CollUtil;
4 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 4 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
6 5 import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO;
... ... @@ -68,18 +67,14 @@ public class SmsChannelServiceImpl implements SmsChannelService {
68 67  
69 68 @Override
70 69 public void deleteSmsChannelList(List<Long> ids) {
71   - if (CollUtil.isEmpty(ids)) {
72   - return;
73   - }
74   - // 校验存在
75   - validateSmsChannelBatchExists(ids);
76   - // 校验是否有在使用该账号的模版
77   - for (Long id : ids) {
  70 + // 1. 校验是否有在使用该账号的模版
  71 + ids.forEach(id -> {
78 72 if (smsTemplateService.getSmsTemplateCountByChannelId(id) > 0) {
79 73 throw exception(SMS_CHANNEL_HAS_CHILDREN);
80 74 }
81   - }
82   - // 批量删除
  75 + });
  76 +
  77 + // 2. 批量删除
83 78 smsChannelMapper.deleteByIds(ids);
84 79 }
85 80  
... ... @@ -91,13 +86,6 @@ public class SmsChannelServiceImpl implements SmsChannelService {
91 86 return channel;
92 87 }
93 88  
94   - private void validateSmsChannelBatchExists(List<Long> ids) {
95   - List<SmsChannelDO> channels = smsChannelMapper.selectByIds(ids);
96   - if (channels.size() != ids.size()) {
97   - throw exception(SMS_CHANNEL_NOT_EXISTS);
98   - }
99   - }
100   -
101 89 @Override
102 90 public SmsChannelDO getSmsChannel(Long id) {
103 91 return smsChannelMapper.selectById(id);
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.sms;
2 2  
3   -import cn.hutool.core.collection.CollUtil;
4 3 import cn.hutool.core.exceptions.ExceptionUtil;
5 4 import cn.hutool.core.lang.Assert;
6 5 import cn.hutool.core.util.ReUtil;
... ... @@ -105,12 +104,6 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
105 104 @CacheEvict(cacheNames = RedisKeyConstants.SMS_TEMPLATE,
106 105 allEntries = true) // allEntries 清空所有缓存,因为 id 不是直接的缓存 code,不好清理
107 106 public void deleteSmsTemplateList(List<Long> ids) {
108   - if (CollUtil.isEmpty(ids)) {
109   - return;
110   - }
111   - // 校验存在
112   - validateSmsTemplateListExists(ids);
113   - // 批量删除
114 107 smsTemplateMapper.deleteByIds(ids);
115 108 }
116 109  
... ... @@ -120,13 +113,6 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
120 113 }
121 114 }
122 115  
123   - private void validateSmsTemplateListExists(List<Long> ids) {
124   - List<SmsTemplateDO> templates = smsTemplateMapper.selectByIds(ids);
125   - if (templates.size() != ids.size()) {
126   - throw exception(SMS_TEMPLATE_NOT_EXISTS);
127   - }
128   - }
129   -
130 116 @Override
131 117 public SmsTemplateDO getSmsTemplate(Long id) {
132 118 return smsTemplateMapper.selectById(id);
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java
... ... @@ -468,12 +468,6 @@ public class SocialClientServiceImpl implements SocialClientService {
468 468  
469 469 @Override
470 470 public void deleteSocialClientList(List<Long> ids) {
471   - if (CollUtil.isEmpty(ids)) {
472   - return;
473   - }
474   - // 校验存在
475   - validateSocialClientBatchExists(ids);
476   - // 批量删除
477 471 socialClientMapper.deleteByIds(ids);
478 472 }
479 473  
... ... @@ -483,13 +477,6 @@ public class SocialClientServiceImpl implements SocialClientService {
483 477 }
484 478 }
485 479  
486   - private void validateSocialClientBatchExists(List<Long> ids) {
487   - List<SocialClientDO> clients = socialClientMapper.selectByIds(ids);
488   - if (clients.size() != ids.size()) {
489   - throw exception(SOCIAL_CLIENT_NOT_EXISTS);
490   - }
491   - }
492   -
493 480 /**
494 481 * 校验社交应用是否重复,需要保证 userType + socialType 唯一
495 482 * 原因是,不同端(userType)选择某个社交登录(socialType)时,需要通过 {@link #buildAuthRequest(Integer, Integer)} 构建对应的请求
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageServiceImpl.java
... ... @@ -78,14 +78,14 @@ public class TenantPackageServiceImpl implements TenantPackageService {
78 78  
79 79 @Override
80 80 public void deleteTenantPackageList(List<Long> ids) {
81   - if (CollUtil.isEmpty(ids)) {
82   - return;
  81 + // 1. 校验是否有租户正在使用该套餐
  82 + for (Long id : ids) {
  83 + if (tenantService.getTenantCountByPackageId(id) > 0) {
  84 + throw exception(TENANT_PACKAGE_USED);
  85 + }
83 86 }
84   - // 校验存在
85   - ids.forEach(this::validateTenantPackageExists);
86   - // 校验正在使用
87   - validateTenantUsedBatch(ids);
88   - // 批量删除
  87 +
  88 + // 2. 批量删除
89 89 tenantPackageMapper.deleteByIds(ids);
90 90 }
91 91  
... ... @@ -103,20 +103,6 @@ public class TenantPackageServiceImpl implements TenantPackageService {
103 103 }
104 104 }
105 105  
106   - /**
107   - * 校验租户套餐是否被使用 - 批量
108   - *
109   - * @param ids 租户套餐编号数组
110   - */
111   - private void validateTenantUsedBatch(List<Long> ids) {
112   - // 查询是否有租户正在使用该套餐
113   - for (Long id : ids) {
114   - if (tenantService.getTenantCountByPackageId(id) > 0) {
115   - throw exception(TENANT_PACKAGE_USED);
116   - }
117   - }
118   - }
119   -
120 106 @Override
121 107 public TenantPackageDO getTenantPackage(Long id) {
122 108 return tenantPackageMapper.selectById(id);
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java
... ... @@ -227,12 +227,10 @@ public class TenantServiceImpl implements TenantService {
227 227  
228 228 @Override
229 229 public void deleteTenantList(List<Long> ids) {
230   - if (CollUtil.isEmpty(ids)) {
231   - return;
232   - }
233   - // 校验存在
234   - validateUpdateTenantBatch(ids);
235   - // 批量删除
  230 + // 1. 校验存在
  231 + ids.forEach(this::validateUpdateTenant);
  232 +
  233 + // 2. 批量删除
236 234 tenantMapper.deleteByIds(ids);
237 235 }
238 236  
... ... @@ -248,26 +246,6 @@ public class TenantServiceImpl implements TenantService {
248 246 return tenant;
249 247 }
250 248  
251   - /**
252   - * 校验租户是否可以更新 - 批量
253   - *
254   - * @param ids 租户编号数组
255   - */
256   - private void validateUpdateTenantBatch(List<Long> ids) {
257   - // 查询租户
258   - List<TenantDO> tenants = tenantMapper.selectByIds(ids);
259   - if (tenants.size() != ids.size()) {
260   - throw exception(TENANT_NOT_EXISTS);
261   - }
262   -
263   - // 校验是否有系统内置租户
264   - tenants.forEach(tenant -> {
265   - if (isSystemTenant(tenant)) {
266   - throw exception(TENANT_CAN_NOT_UPDATE_SYSTEM);
267   - }
268   - });
269   - }
270   -
271 249 @Override
272 250 public TenantDO getTenant(Long id) {
273 251 return tenantMapper.selectById(id);
... ...
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
... ... @@ -251,19 +251,10 @@ public class AdminUserServiceImpl implements AdminUserService {
251 251 @Override
252 252 @Transactional(rollbackFor = Exception.class)
253 253 public void deleteUserList(List<Long> ids) {
254   - if (CollUtil.isEmpty(ids)) {
255   - return;
256   - }
257   - // 1. 校验用户存在
258   - List<AdminUserDO> users = userMapper.selectByIds(ids);
259   - if (CollUtil.isEmpty(users)) {
260   - return;
261   - }
262   -
263   - // 2. 批量删除用户
  254 + // 1. 批量删除用户
264 255 userMapper.deleteByIds(ids);
265 256  
266   - // 3. 批量删除用户关联数据
  257 + // 2. 批量删除用户关联数据
267 258 ids.forEach(id -> {
268 259 permissionService.processUserDeleted(id);
269 260 userPostMapper.deleteByUserId(id);
... ...