mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-10 08:57:14 +08:00
refactor: 优化部分代码
修复 Sonar、Codacy 扫描问题:补充部分泛型、调整部分 Boolean 类型判断、将部分不必要的(无集合长度变动) collect(Collectors.toList()); 转换为 toList()
This commit is contained in:
@@ -56,7 +56,6 @@ import top.charles7c.continew.starter.extension.crud.util.TreeUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 登录业务实现
|
||||
@@ -158,9 +157,7 @@ public class LoginServiceImpl implements LoginService {
|
||||
} else {
|
||||
roleCodeSet.forEach(roleCode -> menuSet.addAll(menuService.listByRoleCode(roleCode)));
|
||||
}
|
||||
List<MenuResp> menuList = menuSet.stream()
|
||||
.filter(m -> !MenuTypeEnum.BUTTON.equals(m.getType()))
|
||||
.collect(Collectors.toList());
|
||||
List<MenuResp> menuList = menuSet.stream().filter(m -> !MenuTypeEnum.BUTTON.equals(m.getType())).toList();
|
||||
// 构建路由树
|
||||
TreeField treeField = MenuResp.class.getDeclaredAnnotation(TreeField.class);
|
||||
TreeNodeConfig treeNodeConfig = TreeUtils.genTreeNodeConfig(treeField);
|
||||
@@ -175,7 +172,7 @@ public class LoginServiceImpl implements LoginService {
|
||||
MetaResp metaResp = new MetaResp();
|
||||
metaResp.setLocale(m.getTitle());
|
||||
metaResp.setIcon(m.getIcon());
|
||||
metaResp.setIgnoreCache(!m.getIsCache());
|
||||
metaResp.setIgnoreCache(Boolean.FALSE.equals(m.getIsCache()));
|
||||
metaResp.setHideInMenu(m.getIsHidden());
|
||||
metaResp.setOrder(m.getSort());
|
||||
tree.putExtra("meta", metaResp);
|
||||
|
@@ -21,6 +21,7 @@ import top.charles7c.continew.admin.common.model.resp.LabelValueResp;
|
||||
import top.charles7c.continew.admin.system.model.entity.DictItemDO;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.base.BaseMapper;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -37,5 +38,5 @@ public interface DictItemMapper extends BaseMapper<DictItemDO> {
|
||||
* @param dictCode 字典编码
|
||||
* @return 字典项列表
|
||||
*/
|
||||
List<LabelValueResp> listByDictCode(@Param("dictCode") String dictCode);
|
||||
List<LabelValueResp<Serializable>> listByDictCode(@Param("dictCode") String dictCode);
|
||||
}
|
@@ -25,6 +25,7 @@ import top.charles7c.continew.admin.system.model.resp.DictItemResp;
|
||||
import top.charles7c.continew.starter.extension.crud.service.BaseService;
|
||||
import top.charles7c.continew.starter.extension.crud.service.IService;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -49,7 +50,7 @@ public interface DictItemService extends BaseService<DictItemResp, DictItemDetai
|
||||
* @param dictCode 字典编码
|
||||
* @return 字典项列表
|
||||
*/
|
||||
List<LabelValueResp> listByDictCode(String dictCode);
|
||||
List<LabelValueResp<Serializable>> listByDictCode(String dictCode);
|
||||
|
||||
/**
|
||||
* 根据字典 ID 列表删除
|
||||
|
@@ -30,6 +30,7 @@ import top.charles7c.continew.starter.core.util.validate.CheckUtils;
|
||||
import top.charles7c.continew.starter.extension.crud.service.impl.BaseServiceImpl;
|
||||
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -66,7 +67,7 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LabelValueResp> listByDictCode(String dictCode) {
|
||||
public List<LabelValueResp<Serializable>> listByDictCode(String dictCode) {
|
||||
return baseMapper.listByDictCode(dictCode);
|
||||
}
|
||||
|
||||
|
@@ -16,17 +16,9 @@
|
||||
|
||||
package top.charles7c.continew.admin.system.service.impl;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import top.charles7c.continew.admin.common.enums.MessageTypeEnum;
|
||||
import top.charles7c.continew.admin.system.mapper.MessageUserMapper;
|
||||
import top.charles7c.continew.admin.system.model.entity.MessageUserDO;
|
||||
@@ -35,6 +27,10 @@ import top.charles7c.continew.admin.system.model.resp.MessageUnreadResp;
|
||||
import top.charles7c.continew.admin.system.service.MessageUserService;
|
||||
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 消息和用户关联业务实现
|
||||
*
|
||||
@@ -78,7 +74,7 @@ public class MessageUserServiceImpl implements MessageUserService {
|
||||
messageUser.setMessageId(messageId);
|
||||
messageUser.setIsRead(false);
|
||||
return messageUser;
|
||||
}).collect(Collectors.toList());
|
||||
}).toList();
|
||||
baseMapper.insertBatch(messageUserList);
|
||||
}
|
||||
|
||||
|
@@ -28,7 +28,6 @@ import top.charles7c.continew.admin.system.model.entity.RoleDeptDO;
|
||||
import top.charles7c.continew.admin.system.service.RoleDeptService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 角色和部门业务实现
|
||||
@@ -52,16 +51,14 @@ public class RoleDeptServiceImpl implements RoleDeptService {
|
||||
.list()
|
||||
.stream()
|
||||
.map(RoleDeptDO::getDeptId)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
if (CollUtil.isEmpty(CollUtil.disjunction(deptIds, oldDeptIdList))) {
|
||||
return false;
|
||||
}
|
||||
// 删除原有关联
|
||||
roleDeptMapper.lambdaUpdate().eq(RoleDeptDO::getRoleId, roleId).remove();
|
||||
// 保存最新关联
|
||||
List<RoleDeptDO> roleDeptList = deptIds.stream()
|
||||
.map(deptId -> new RoleDeptDO(roleId, deptId))
|
||||
.collect(Collectors.toList());
|
||||
List<RoleDeptDO> roleDeptList = deptIds.stream().map(deptId -> new RoleDeptDO(roleId, deptId)).toList();
|
||||
return roleDeptMapper.insertBatch(roleDeptList);
|
||||
}
|
||||
|
||||
|
@@ -16,21 +16,18 @@
|
||||
|
||||
package top.charles7c.continew.admin.system.service.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
|
||||
import top.charles7c.continew.admin.system.mapper.RoleMenuMapper;
|
||||
import top.charles7c.continew.admin.system.model.entity.RoleMenuDO;
|
||||
import top.charles7c.continew.admin.system.service.RoleMenuService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 角色和菜单业务实现
|
||||
*
|
||||
@@ -60,9 +57,7 @@ public class RoleMenuServiceImpl implements RoleMenuService {
|
||||
// 删除原有关联
|
||||
roleMenuMapper.lambdaUpdate().eq(RoleMenuDO::getRoleId, roleId).remove();
|
||||
// 保存最新关联
|
||||
List<RoleMenuDO> roleMenuList = menuIds.stream()
|
||||
.map(menuId -> new RoleMenuDO(roleId, menuId))
|
||||
.collect(Collectors.toList());
|
||||
List<RoleMenuDO> roleMenuList = menuIds.stream().map(menuId -> new RoleMenuDO(roleId, menuId)).toList();
|
||||
return roleMenuMapper.insertBatch(roleMenuList);
|
||||
}
|
||||
|
||||
|
@@ -133,7 +133,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
|
||||
Long roleId = detail.getId();
|
||||
if (SysConstants.ADMIN_ROLE_CODE.equals(detail.getCode())) {
|
||||
List<MenuResp> list = menuService.list(null, null);
|
||||
List<Long> menuIds = list.stream().map(MenuResp::getId).collect(Collectors.toList());
|
||||
List<Long> menuIds = list.stream().map(MenuResp::getId).toList();
|
||||
detail.setMenuIds(menuIds);
|
||||
} else {
|
||||
detail.setMenuIds(roleMenuService.listMenuIdByRoleIds(CollUtil.newArrayList(roleId)));
|
||||
@@ -152,7 +152,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
|
||||
@Override
|
||||
public List<String> listNameByIds(List<Long> ids) {
|
||||
List<RoleDO> roleList = baseMapper.lambdaQuery().select(RoleDO::getName).in(RoleDO::getId, ids).list();
|
||||
return roleList.stream().map(RoleDO::getName).collect(Collectors.toList());
|
||||
return roleList.stream().map(RoleDO::getName).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -167,10 +167,7 @@ public class StorageServiceImpl extends BaseServiceImpl<StorageMapper, StorageDO
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean isDefaultExists(Long id) {
|
||||
return baseMapper.lambdaQuery()
|
||||
.eq(StorageDO::getIsDefault, Boolean.TRUE)
|
||||
.ne(null != id, StorageDO::getId, id)
|
||||
.exists();
|
||||
return baseMapper.lambdaQuery().eq(StorageDO::getIsDefault, true).ne(null != id, StorageDO::getId, id).exists();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -28,7 +28,6 @@ import top.charles7c.continew.admin.system.model.entity.UserRoleDO;
|
||||
import top.charles7c.continew.admin.system.service.UserRoleService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 用户和角色业务实现
|
||||
@@ -52,16 +51,14 @@ public class UserRoleServiceImpl implements UserRoleService {
|
||||
.list()
|
||||
.stream()
|
||||
.map(UserRoleDO::getRoleId)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
if (CollUtil.isEmpty(CollUtil.disjunction(roleIds, oldRoleIdList))) {
|
||||
return false;
|
||||
}
|
||||
// 删除原有关联
|
||||
userRoleMapper.lambdaUpdate().eq(UserRoleDO::getUserId, userId).remove();
|
||||
// 保存最新关联
|
||||
List<UserRoleDO> userRoleList = roleIds.stream()
|
||||
.map(roleId -> new UserRoleDO(userId, roleId))
|
||||
.collect(Collectors.toList());
|
||||
List<UserRoleDO> userRoleList = roleIds.stream().map(roleId -> new UserRoleDO(userId, roleId)).toList();
|
||||
return userRoleMapper.insertBatch(userRoleList);
|
||||
}
|
||||
|
||||
|
@@ -75,6 +75,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
@Value("${avatar.support-suffix}")
|
||||
private String[] avatarSupportSuffix;
|
||||
private static final String CURRENT_PASSWORD_ERROR = "当前密码错误";
|
||||
|
||||
@Override
|
||||
public Long add(UserDO user) {
|
||||
@@ -85,12 +86,13 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
|
||||
@Override
|
||||
protected void beforeAdd(UserReq req) {
|
||||
final String errorMsgTemplate = "新增失败,[{}] 已存在";
|
||||
String username = req.getUsername();
|
||||
CheckUtils.throwIf(this.isNameExists(username, null), "新增失败,[{}] 已存在", username);
|
||||
CheckUtils.throwIf(this.isNameExists(username, null), errorMsgTemplate, username);
|
||||
String email = req.getEmail();
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), "新增失败,[{}] 已存在", email);
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), errorMsgTemplate, email);
|
||||
String phone = req.getPhone();
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), "新增失败,[{}] 已存在", phone);
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), errorMsgTemplate, phone);
|
||||
req.setStatus(DisEnableStatusEnum.ENABLE);
|
||||
req.setPassword(passwordEncoder.encode(req.getPassword()));
|
||||
}
|
||||
@@ -107,12 +109,13 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@CacheUpdate(key = "#id", value = "#req.nickname", name = CacheConstants.USER_KEY_PREFIX)
|
||||
public void update(UserReq req, Long id) {
|
||||
final String errorMsgTemplate = "修改失败,[{}] 已存在";
|
||||
String username = req.getUsername();
|
||||
CheckUtils.throwIf(this.isNameExists(username, id), "修改失败,[{}] 已存在", username);
|
||||
CheckUtils.throwIf(this.isNameExists(username, id), errorMsgTemplate, username);
|
||||
String email = req.getEmail();
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, id), "修改失败,[{}] 已存在", email);
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, id), errorMsgTemplate, email);
|
||||
String phone = req.getPhone();
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, id), "修改失败,[{}] 已存在", phone);
|
||||
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, id), errorMsgTemplate, phone);
|
||||
DisEnableStatusEnum newStatus = req.getStatus();
|
||||
CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(newStatus) && ObjectUtil.equal(id, LoginHelper
|
||||
.getUserId()), "不允许禁用当前用户");
|
||||
@@ -196,7 +199,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
UserDO user = super.getById(id);
|
||||
String password = user.getPassword();
|
||||
if (StrUtil.isNotBlank(password)) {
|
||||
CheckUtils.throwIf(!passwordEncoder.matches(oldPassword, password), "当前密码错误");
|
||||
CheckUtils.throwIf(!passwordEncoder.matches(oldPassword, password), CURRENT_PASSWORD_ERROR);
|
||||
}
|
||||
// 更新密码和密码重置时间
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
@@ -210,7 +213,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
@Override
|
||||
public void updatePhone(String newPhone, String currentPassword, Long id) {
|
||||
UserDO user = super.getById(id);
|
||||
CheckUtils.throwIf(!passwordEncoder.matches(currentPassword, user.getPassword()), "当前密码错误");
|
||||
CheckUtils.throwIf(!passwordEncoder.matches(currentPassword, user.getPassword()), CURRENT_PASSWORD_ERROR);
|
||||
Long count = baseMapper.lambdaQuery().eq(UserDO::getPhone, newPhone).count();
|
||||
CheckUtils.throwIf(count > 0, "手机号已绑定其他账号,请更换其他手机号");
|
||||
CheckUtils.throwIfEqual(newPhone, user.getPhone(), "新手机号不能与当前手机号相同");
|
||||
@@ -221,7 +224,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
|
||||
@Override
|
||||
public void updateEmail(String newEmail, String currentPassword, Long id) {
|
||||
UserDO user = super.getById(id);
|
||||
CheckUtils.throwIf(!passwordEncoder.matches(currentPassword, user.getPassword()), "当前密码错误");
|
||||
CheckUtils.throwIf(!passwordEncoder.matches(currentPassword, user.getPassword()), CURRENT_PASSWORD_ERROR);
|
||||
Long count = baseMapper.lambdaQuery().eq(UserDO::getEmail, newEmail).count();
|
||||
CheckUtils.throwIf(count > 0, "邮箱已绑定其他账号,请更换其他邮箱");
|
||||
CheckUtils.throwIfEqual(newEmail, user.getEmail(), "新邮箱不能与当前邮箱相同");
|
||||
|
Reference in New Issue
Block a user