mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-09 20:57:21 +08:00
优化:使用 MyBatis Plus ChainWrapper 优化部分 QueryWrapper 的写法
This commit is contained in:
@@ -26,8 +26,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
|
||||
@@ -62,8 +60,8 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long create(DeptRequest request) {
|
||||
String deptName = request.getDeptName();
|
||||
boolean isExist = this.checkNameExist(deptName, request.getParentId(), request.getDeptId());
|
||||
CheckUtils.throwIf(() -> isExist, String.format("新增失败,'%s'已存在", deptName));
|
||||
boolean isExists = this.checkNameExists(deptName, request.getParentId(), request.getDeptId());
|
||||
CheckUtils.throwIf(() -> isExists, String.format("新增失败,'%s'已存在", deptName));
|
||||
|
||||
// 保存信息
|
||||
request.setStatus(DisEnableStatusEnum.ENABLE);
|
||||
@@ -74,8 +72,8 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(DeptRequest request) {
|
||||
String deptName = request.getDeptName();
|
||||
boolean isExist = this.checkNameExist(deptName, request.getParentId(), request.getDeptId());
|
||||
CheckUtils.throwIf(() -> isExist, String.format("新增失败,'%s'已存在", deptName));
|
||||
boolean isExists = this.checkNameExists(deptName, request.getParentId(), request.getDeptId());
|
||||
CheckUtils.throwIf(() -> isExists, String.format("修改失败,'%s'已存在", deptName));
|
||||
|
||||
super.update(request);
|
||||
}
|
||||
@@ -85,7 +83,7 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
public void delete(List<Long> ids) {
|
||||
CheckUtils.throwIf(() -> userService.countByDeptIds(ids) > 0, "所选部门存在用户关联,请解除关联后重试");
|
||||
super.delete(ids);
|
||||
baseMapper.delete(Wrappers.<DeptDO>lambdaQuery().in(DeptDO::getParentId, ids));
|
||||
super.lambdaUpdate().in(DeptDO::getParentId, ids).remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,18 +107,18 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
*/
|
||||
private List<DeptVO> deDuplication(List<DeptVO> list) {
|
||||
List<DeptVO> deDuplicationList = new ArrayList<>();
|
||||
for (DeptVO outerDept : list) {
|
||||
for (DeptVO outer : list) {
|
||||
boolean flag = true;
|
||||
for (DeptVO innerDept : list) {
|
||||
for (DeptVO inner : list) {
|
||||
// 忽略重复子列表
|
||||
if (innerDept.getDeptId().equals(outerDept.getParentId())) {
|
||||
if (inner.getDeptId().equals(outer.getParentId())) {
|
||||
flag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
deDuplicationList.add(outerDept);
|
||||
deDuplicationList.add(outer);
|
||||
}
|
||||
}
|
||||
return deDuplicationList;
|
||||
@@ -153,17 +151,17 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
/**
|
||||
* 检查名称是否存在
|
||||
*
|
||||
* @param deptName
|
||||
* 部门名称
|
||||
* @param name
|
||||
* 名称
|
||||
* @param parentId
|
||||
* 上级部门 ID
|
||||
* @param deptId
|
||||
* 部门 ID
|
||||
* 上级 ID
|
||||
* @param id
|
||||
* ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean checkNameExist(String deptName, Long parentId, Long deptId) {
|
||||
return baseMapper.exists(Wrappers.<DeptDO>lambdaQuery().eq(DeptDO::getDeptName, deptName)
|
||||
.eq(DeptDO::getParentId, parentId).ne(deptId != null, DeptDO::getDeptId, deptId));
|
||||
private boolean checkNameExists(String name, Long parentId, Long id) {
|
||||
return super.lambdaQuery().eq(DeptDO::getDeptName, name).eq(DeptDO::getParentId, parentId)
|
||||
.ne(id != null, DeptDO::getDeptId, id).exists();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -23,8 +23,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
@@ -54,7 +52,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long create(RoleRequest request) {
|
||||
String roleName = request.getRoleName();
|
||||
boolean isExist = this.checkNameExist(roleName, request.getRoleId());
|
||||
boolean isExist = this.checkNameExists(roleName, request.getRoleId());
|
||||
CheckUtils.throwIf(() -> isExist, String.format("新增失败,'%s'已存在", roleName));
|
||||
|
||||
// 保存信息
|
||||
@@ -66,7 +64,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(RoleRequest request) {
|
||||
String roleName = request.getRoleName();
|
||||
boolean isExist = this.checkNameExist(roleName, request.getRoleId());
|
||||
boolean isExist = this.checkNameExists(roleName, request.getRoleId());
|
||||
CheckUtils.throwIf(() -> isExist, String.format("修改失败,'%s'已存在", roleName));
|
||||
|
||||
super.update(request);
|
||||
@@ -82,14 +80,13 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
|
||||
/**
|
||||
* 检查名称是否存在
|
||||
*
|
||||
* @param roleName
|
||||
* 角色名称
|
||||
* @param roleId
|
||||
* 角色 ID
|
||||
* @param name
|
||||
* 名称
|
||||
* @param id
|
||||
* ID
|
||||
* @return 是否存在
|
||||
*/
|
||||
private boolean checkNameExist(String roleName, Long roleId) {
|
||||
return baseMapper.exists(Wrappers.<RoleDO>lambdaQuery().eq(RoleDO::getRoleName, roleName).ne(roleId != null,
|
||||
RoleDO::getRoleId, roleId));
|
||||
private boolean checkNameExists(String name, Long id) {
|
||||
return super.lambdaQuery().eq(RoleDO::getRoleName, name).ne(id != null, RoleDO::getRoleId, id).exists();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user