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

This commit is contained in:
YunaiV 2025-06-15 10:04:04 +08:00
parent 1c0c6710bb
commit d7dbb4817b
12 changed files with 23 additions and 191 deletions

View File

@ -175,8 +175,6 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@Transactional(rollbackFor = Exception.class)
#end
public void delete${simpleClassName}ListByIds(List<${primaryColumn.javaType}> ids) {
// 校验存在
validate${simpleClassName}Exists(ids);
// 删除
${classNameVar}Mapper.deleteByIds(ids);
## 特殊:主子表专属逻辑
@ -193,12 +191,6 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
#end
}
private void validate${simpleClassName}Exists(List<${primaryColumn.javaType}> ids) {
List<${table.className}DO> list = ${classNameVar}Mapper.selectByIds(ids);
if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
throw exception(${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS);
}
}
#end
private void validate${simpleClassName}Exists(${primaryColumn.javaType} id) {

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.system.service.mail;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO;
@ -71,20 +70,17 @@ public class MailAccountServiceImpl implements MailAccountService {
}
@Override
@CacheEvict(value = RedisKeyConstants.MAIL_ACCOUNT, allEntries = true)
@CacheEvict(value = RedisKeyConstants.MAIL_ACCOUNT,
allEntries = true) // allEntries 清空所有缓存因为 Spring Cache 不支持按照 ids 批量删除
public void deleteMailAccountList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验是否存在
validateMailAccountsExists(ids);
// 校验是否存在关联模版
// 1. 校验是否存在关联模版
for (Long id : ids) {
if (mailTemplateService.getMailTemplateCountByAccountId(id) > 0) {
throw exception(MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS);
}
}
// 批量删除
// 2. 批量删除
mailAccountMapper.deleteByIds(ids);
}
@ -94,17 +90,6 @@ public class MailAccountServiceImpl implements MailAccountService {
}
}
private void validateMailAccountsExists(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
List<MailAccountDO> accounts = mailAccountMapper.selectByIds(ids);
if (CollUtil.isEmpty(accounts) || accounts.size() != ids.size()) {
throw exception(MAIL_ACCOUNT_NOT_EXISTS);
}
}
@Override
public MailAccountDO getMailAccount(Long id) {
return mailAccountMapper.selectById(id);

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.system.service.mail;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
@ -102,12 +101,6 @@ public class MailTemplateServiceImpl implements MailTemplateService {
@CacheEvict(cacheNames = RedisKeyConstants.MAIL_TEMPLATE,
allEntries = true) // allEntries 清空所有缓存因为 id 不是直接的缓存 code不好清理
public void deleteMailTemplateList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
validateMailTemplatesExists(ids);
// 批量删除
mailTemplateMapper.deleteByIds(ids);
}
@ -117,17 +110,6 @@ public class MailTemplateServiceImpl implements MailTemplateService {
}
}
private void validateMailTemplatesExists(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
List<MailTemplateDO> templates = mailTemplateMapper.selectByIds(ids);
if (CollUtil.isEmpty(templates) || templates.size() != ids.size()) {
throw exception(MAIL_TEMPLATE_NOT_EXISTS);
}
}
@Override
public MailTemplateDO getMailTemplate(Long id) {return mailTemplateMapper.selectById(id);}

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.system.service.notice;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.NoticePageReqVO;
@ -53,9 +52,6 @@ public class NoticeServiceImpl implements NoticeService {
@Override
public void deleteNoticeList(List<Long> ids) {
// 校验是否存在
validateNoticesExists(ids);
// 批量删除通知公告
noticeMapper.deleteByIds(ids);
}
@ -80,15 +76,4 @@ public class NoticeServiceImpl implements NoticeService {
}
}
private void validateNoticesExists(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
List<NoticeDO> notices = noticeMapper.selectByIds(ids);
if (CollUtil.isEmpty(notices) || notices.size() != ids.size()) {
throw exception(NOTICE_NOT_FOUND);
}
}
}

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.system.service.notify;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
@ -90,23 +89,9 @@ public class NotifyTemplateServiceImpl implements NotifyTemplateService {
@CacheEvict(cacheNames = RedisKeyConstants.NOTIFY_TEMPLATE,
allEntries = true) // allEntries 清空所有缓存因为 id 不是直接的缓存 code不好清理
public void deleteNotifyTemplateList(List<Long> ids) {
// 校验存在
validateNotifyTemplatesExists(ids);
// 批量删除
notifyTemplateMapper.deleteByIds(ids);
}
private void validateNotifyTemplatesExists(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
List<NotifyTemplateDO> templates = notifyTemplateMapper.selectByIds(ids);
if (CollUtil.isEmpty(templates) || templates.size() != ids.size()) {
throw exception(NOTIFY_TEMPLATE_NOT_EXISTS);
}
}
private void validateNotifyTemplateExists(Long id) {
if (notifyTemplateMapper.selectById(id) == null) {
throw exception(NOTIFY_TEMPLATE_NOT_EXISTS);

View File

@ -77,12 +77,6 @@ public class OAuth2ClientServiceImpl implements OAuth2ClientService {
@CacheEvict(cacheNames = RedisKeyConstants.OAUTH_CLIENT,
allEntries = true) // allEntries 清空所有缓存因为 id 不是直接的缓存 key不好清理
public void deleteOAuth2ClientList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
validateOAuth2ClientBatchExists(ids);
// 批量删除
oauth2ClientMapper.deleteByIds(ids);
}
@ -92,13 +86,6 @@ public class OAuth2ClientServiceImpl implements OAuth2ClientService {
}
}
private void validateOAuth2ClientBatchExists(List<Long> ids) {
List<OAuth2ClientDO> clients = oauth2ClientMapper.selectByIds(ids);
if (clients.size() != ids.size()) {
throw exception(OAUTH2_CLIENT_NOT_EXISTS);
}
}
@VisibleForTesting
void validateClientIdExists(Long id, String clientId) {
OAuth2ClientDO client = oauth2ClientMapper.selectByClientId(clientId);

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.system.service.sms;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO;
@ -68,18 +67,14 @@ public class SmsChannelServiceImpl implements SmsChannelService {
@Override
public void deleteSmsChannelList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
validateSmsChannelBatchExists(ids);
// 校验是否有在使用该账号的模版
for (Long id : ids) {
// 1. 校验是否有在使用该账号的模版
ids.forEach(id -> {
if (smsTemplateService.getSmsTemplateCountByChannelId(id) > 0) {
throw exception(SMS_CHANNEL_HAS_CHILDREN);
}
}
// 批量删除
});
// 2. 批量删除
smsChannelMapper.deleteByIds(ids);
}
@ -91,13 +86,6 @@ public class SmsChannelServiceImpl implements SmsChannelService {
return channel;
}
private void validateSmsChannelBatchExists(List<Long> ids) {
List<SmsChannelDO> channels = smsChannelMapper.selectByIds(ids);
if (channels.size() != ids.size()) {
throw exception(SMS_CHANNEL_NOT_EXISTS);
}
}
@Override
public SmsChannelDO getSmsChannel(Long id) {
return smsChannelMapper.selectById(id);

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.system.service.sms;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReUtil;
@ -105,12 +104,6 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
@CacheEvict(cacheNames = RedisKeyConstants.SMS_TEMPLATE,
allEntries = true) // allEntries 清空所有缓存因为 id 不是直接的缓存 code不好清理
public void deleteSmsTemplateList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
validateSmsTemplateListExists(ids);
// 批量删除
smsTemplateMapper.deleteByIds(ids);
}
@ -120,13 +113,6 @@ public class SmsTemplateServiceImpl implements SmsTemplateService {
}
}
private void validateSmsTemplateListExists(List<Long> ids) {
List<SmsTemplateDO> templates = smsTemplateMapper.selectByIds(ids);
if (templates.size() != ids.size()) {
throw exception(SMS_TEMPLATE_NOT_EXISTS);
}
}
@Override
public SmsTemplateDO getSmsTemplate(Long id) {
return smsTemplateMapper.selectById(id);

View File

@ -468,12 +468,6 @@ public class SocialClientServiceImpl implements SocialClientService {
@Override
public void deleteSocialClientList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
validateSocialClientBatchExists(ids);
// 批量删除
socialClientMapper.deleteByIds(ids);
}
@ -483,13 +477,6 @@ public class SocialClientServiceImpl implements SocialClientService {
}
}
private void validateSocialClientBatchExists(List<Long> ids) {
List<SocialClientDO> clients = socialClientMapper.selectByIds(ids);
if (clients.size() != ids.size()) {
throw exception(SOCIAL_CLIENT_NOT_EXISTS);
}
}
/**
* 校验社交应用是否重复需要保证 userType + socialType 唯一
* 原因是不同端userType选择某个社交登录socialType需要通过 {@link #buildAuthRequest(Integer, Integer)} 构建对应的请求

View File

@ -78,14 +78,14 @@ public class TenantPackageServiceImpl implements TenantPackageService {
@Override
public void deleteTenantPackageList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
// 1. 校验是否有租户正在使用该套餐
for (Long id : ids) {
if (tenantService.getTenantCountByPackageId(id) > 0) {
throw exception(TENANT_PACKAGE_USED);
}
}
// 校验存在
ids.forEach(this::validateTenantPackageExists);
// 校验正在使用
validateTenantUsedBatch(ids);
// 批量删除
// 2. 批量删除
tenantPackageMapper.deleteByIds(ids);
}
@ -103,20 +103,6 @@ public class TenantPackageServiceImpl implements TenantPackageService {
}
}
/**
* 校验租户套餐是否被使用 - 批量
*
* @param ids 租户套餐编号数组
*/
private void validateTenantUsedBatch(List<Long> ids) {
// 查询是否有租户正在使用该套餐
for (Long id : ids) {
if (tenantService.getTenantCountByPackageId(id) > 0) {
throw exception(TENANT_PACKAGE_USED);
}
}
}
@Override
public TenantPackageDO getTenantPackage(Long id) {
return tenantPackageMapper.selectById(id);

View File

@ -227,12 +227,10 @@ public class TenantServiceImpl implements TenantService {
@Override
public void deleteTenantList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 校验存在
validateUpdateTenantBatch(ids);
// 批量删除
// 1. 校验存在
ids.forEach(this::validateUpdateTenant);
// 2. 批量删除
tenantMapper.deleteByIds(ids);
}
@ -248,26 +246,6 @@ public class TenantServiceImpl implements TenantService {
return tenant;
}
/**
* 校验租户是否可以更新 - 批量
*
* @param ids 租户编号数组
*/
private void validateUpdateTenantBatch(List<Long> ids) {
// 查询租户
List<TenantDO> tenants = tenantMapper.selectByIds(ids);
if (tenants.size() != ids.size()) {
throw exception(TENANT_NOT_EXISTS);
}
// 校验是否有系统内置租户
tenants.forEach(tenant -> {
if (isSystemTenant(tenant)) {
throw exception(TENANT_CAN_NOT_UPDATE_SYSTEM);
}
});
}
@Override
public TenantDO getTenant(Long id) {
return tenantMapper.selectById(id);

View File

@ -251,19 +251,10 @@ public class AdminUserServiceImpl implements AdminUserService {
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteUserList(List<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 1. 校验用户存在
List<AdminUserDO> users = userMapper.selectByIds(ids);
if (CollUtil.isEmpty(users)) {
return;
}
// 2. 批量删除用户
// 1. 批量删除用户
userMapper.deleteByIds(ids);
// 3. 批量删除用户关联数据
// 2. 批量删除用户关联数据
ids.forEach(id -> {
permissionService.processUserDeleted(id);
userPostMapper.deleteByUserId(id);