!132 优化查询是否存在的方法,替换魔法值为已定义的常量

* fix 替换查询所有子部门数中魔法值为已定义的常量
* fix 1.修改查询是否存在的方法改为baseMapper.exists()方法查询。 2.将部分魔法值改为已定义的常量
This commit is contained in:
KonBAI 2022-01-22 07:00:58 +00:00 committed by 疯狂的狮子Li
parent 0f75f789d8
commit d52ece745e
7 changed files with 31 additions and 37 deletions

View File

@ -1,6 +1,7 @@
package com.ruoyi.system.mapper; package com.ruoyi.system.mapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDictData; import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.mapper.BaseMapperPlus; import com.ruoyi.common.core.mapper.BaseMapperPlus;
@ -16,7 +17,7 @@ public interface SysDictDataMapper extends BaseMapperPlus<SysDictDataMapper, Sys
default List<SysDictData> selectDictDataByType(String dictType) { default List<SysDictData> selectDictDataByType(String dictType) {
return selectList( return selectList(
new LambdaQueryWrapper<SysDictData>() new LambdaQueryWrapper<SysDictData>()
.eq(SysDictData::getStatus, "0") .eq(SysDictData::getStatus, UserConstants.DICT_NORMAL)
.eq(SysDictData::getDictType, dictType) .eq(SysDictData::getDictType, dictType)
.orderByAsc(SysDictData::getDictSort)); .orderByAsc(SysDictData::getDictSort));
} }

View File

@ -44,7 +44,7 @@ public interface ISysDeptService {
SysDept selectDeptById(Long deptId); SysDept selectDeptById(Long deptId);
/** /**
* 根据ID查询所有子部门正常状态 * 根据ID查询所有子部门正常状态
* *
* @param deptId 部门ID * @param deptId 部门ID
* @return 子部门数 * @return 子部门数

View File

@ -92,7 +92,7 @@ public class SysDeptServiceImpl implements ISysDeptService {
} }
/** /**
* 根据ID查询所有子部门正常状态 * 根据ID查询所有子部门正常状态
* *
* @param deptId 部门ID * @param deptId 部门ID
* @return 子部门数 * @return 子部门数
@ -100,7 +100,7 @@ public class SysDeptServiceImpl implements ISysDeptService {
@Override @Override
public long selectNormalChildrenDeptById(Long deptId) { public long selectNormalChildrenDeptById(Long deptId) {
return baseMapper.selectCount(new LambdaQueryWrapper<SysDept>() return baseMapper.selectCount(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getStatus, 0) .eq(SysDept::getStatus, UserConstants.DEPT_NORMAL)
.apply("find_in_set({0}, ancestors)", deptId)); .apply("find_in_set({0}, ancestors)", deptId));
} }
@ -136,12 +136,11 @@ public class SysDeptServiceImpl implements ISysDeptService {
*/ */
@Override @Override
public String checkDeptNameUnique(SysDept dept) { public String checkDeptNameUnique(SysDept dept) {
Long deptId = ObjectUtil.isNull(dept.getDeptId()) ? -1L : dept.getDeptId(); boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysDept>()
boolean count = baseMapper.exists(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getDeptName, dept.getDeptName()) .eq(SysDept::getDeptName, dept.getDeptName())
.eq(SysDept::getParentId, dept.getParentId()) .eq(SysDept::getParentId, dept.getParentId())
.ne(SysDept::getDeptId, deptId)); .ne(ObjectUtil.isNotNull(dept.getDeptId()), SysDept::getDeptId, dept.getDeptId()));
if (count) { if (exist) {
return UserConstants.NOT_UNIQUE; return UserConstants.NOT_UNIQUE;
} }
return UserConstants.UNIQUE; return UserConstants.UNIQUE;
@ -199,7 +198,7 @@ public class SysDeptServiceImpl implements ISysDeptService {
} }
int result = baseMapper.updateById(dept); int result = baseMapper.updateById(dept);
if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors()) if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
&& !StringUtils.equals("0", dept.getAncestors())) { && !StringUtils.equals(UserConstants.DEPT_NORMAL, dept.getAncestors())) {
// 如果该部门是启用状态则启用该部门的所有上级部门 // 如果该部门是启用状态则启用该部门的所有上级部门
updateParentDeptStatusNormal(dept); updateParentDeptStatusNormal(dept);
} }
@ -215,7 +214,7 @@ public class SysDeptServiceImpl implements ISysDeptService {
String ancestors = dept.getAncestors(); String ancestors = dept.getAncestors();
Long[] deptIds = Convert.toLongArray(ancestors); Long[] deptIds = Convert.toLongArray(ancestors);
baseMapper.update(null, new LambdaUpdateWrapper<SysDept>() baseMapper.update(null, new LambdaUpdateWrapper<SysDept>()
.set(SysDept::getStatus, "0") .set(SysDept::getStatus, UserConstants.DEPT_NORMAL)
.in(SysDept::getDeptId, Arrays.asList(deptIds))); .in(SysDept::getDeptId, Arrays.asList(deptIds)));
} }

View File

@ -129,8 +129,8 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService, DictService
public void deleteDictTypeByIds(Long[] dictIds) { public void deleteDictTypeByIds(Long[] dictIds) {
for (Long dictId : dictIds) { for (Long dictId : dictIds) {
SysDictType dictType = selectDictTypeById(dictId); SysDictType dictType = selectDictTypeById(dictId);
if (dictDataMapper.selectCount(new LambdaQueryWrapper<SysDictData>() if (dictDataMapper.exists(new LambdaQueryWrapper<SysDictData>()
.eq(SysDictData::getDictType, dictType.getDictType())) > 0) { .eq(SysDictData::getDictType, dictType.getDictType()))) {
throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName())); throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
} }
RedisUtils.deleteObject(getCacheKey(dictType.getDictType())); RedisUtils.deleteObject(getCacheKey(dictType.getDictType()));
@ -144,7 +144,7 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService, DictService
@Override @Override
public void loadingDictCache() { public void loadingDictCache() {
List<SysDictData> dictDataList = dictDataMapper.selectList( List<SysDictData> dictDataList = dictDataMapper.selectList(
new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getStatus, "0")); new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getStatus, UserConstants.DICT_NORMAL));
Map<String, List<SysDictData>> dictDataMap = dictDataList.stream().collect(Collectors.groupingBy(SysDictData::getDictType)); Map<String, List<SysDictData>> dictDataMap = dictDataList.stream().collect(Collectors.groupingBy(SysDictData::getDictType));
dictDataMap.forEach((k,v) -> { dictDataMap.forEach((k,v) -> {
String dictKey = getCacheKey(k); String dictKey = getCacheKey(k);
@ -217,11 +217,10 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService, DictService
*/ */
@Override @Override
public String checkDictTypeUnique(SysDictType dict) { public String checkDictTypeUnique(SysDictType dict) {
Long dictId = ObjectUtil.isNull(dict.getDictId()) ? -1L : dict.getDictId(); boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysDictType>()
long count = baseMapper.selectCount(new LambdaQueryWrapper<SysDictType>()
.eq(SysDictType::getDictType, dict.getDictType()) .eq(SysDictType::getDictType, dict.getDictType())
.ne(SysDictType::getDictId, dictId)); .ne(ObjectUtil.isNotNull(dict.getDictId()), SysDictType::getDictId, dict.getDictId()));
if (count > 0) { if (exist) {
return UserConstants.NOT_UNIQUE; return UserConstants.NOT_UNIQUE;
} }
return UserConstants.UNIQUE; return UserConstants.UNIQUE;

View File

@ -263,12 +263,11 @@ public class SysMenuServiceImpl implements ISysMenuService {
*/ */
@Override @Override
public String checkMenuNameUnique(SysMenu menu) { public String checkMenuNameUnique(SysMenu menu) {
Long menuId = ObjectUtil.isNull(menu.getMenuId()) ? -1L : menu.getMenuId(); boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysMenu>()
boolean count = baseMapper.exists(new LambdaQueryWrapper<SysMenu>()
.eq(SysMenu::getMenuName, menu.getMenuName()) .eq(SysMenu::getMenuName, menu.getMenuName())
.eq(SysMenu::getParentId, menu.getParentId()) .eq(SysMenu::getParentId, menu.getParentId())
.ne(SysMenu::getMenuId, menuId)); .ne(ObjectUtil.isNotNull(menu.getMenuId()), SysMenu::getMenuId, menu.getMenuId()));
if (count) { if (exist) {
return UserConstants.NOT_UNIQUE; return UserConstants.NOT_UNIQUE;
} }
return UserConstants.UNIQUE; return UserConstants.UNIQUE;

View File

@ -95,11 +95,10 @@ public class SysPostServiceImpl implements ISysPostService {
*/ */
@Override @Override
public String checkPostNameUnique(SysPost post) { public String checkPostNameUnique(SysPost post) {
Long postId = ObjectUtil.isNull(post.getPostId()) ? -1L : post.getPostId(); boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysPost>()
boolean count = baseMapper.exists(new LambdaQueryWrapper<SysPost>()
.eq(SysPost::getPostName, post.getPostName()) .eq(SysPost::getPostName, post.getPostName())
.ne(SysPost::getPostId, postId)); .ne(ObjectUtil.isNotNull(post.getPostId()), SysPost::getPostId, post.getPostId()));
if (count) { if (exist) {
return UserConstants.NOT_UNIQUE; return UserConstants.NOT_UNIQUE;
} }
return UserConstants.UNIQUE; return UserConstants.UNIQUE;
@ -113,11 +112,10 @@ public class SysPostServiceImpl implements ISysPostService {
*/ */
@Override @Override
public String checkPostCodeUnique(SysPost post) { public String checkPostCodeUnique(SysPost post) {
Long postId = ObjectUtil.isNull(post.getPostId()) ? -1L : post.getPostId(); boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysPost>()
boolean count = baseMapper.exists(new LambdaQueryWrapper<SysPost>()
.eq(SysPost::getPostCode, post.getPostCode()) .eq(SysPost::getPostCode, post.getPostCode())
.ne(SysPost::getPostId, postId)); .ne(ObjectUtil.isNotNull(post.getPostId()), SysPost::getPostId, post.getPostId()));
if (count) { if (exist) {
return UserConstants.NOT_UNIQUE; return UserConstants.NOT_UNIQUE;
} }
return UserConstants.UNIQUE; return UserConstants.UNIQUE;

View File

@ -136,11 +136,10 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/ */
@Override @Override
public String checkRoleNameUnique(SysRole role) { public String checkRoleNameUnique(SysRole role) {
Long roleId = ObjectUtil.isNull(role.getRoleId()) ? -1L : role.getRoleId(); boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysRole>()
boolean count = baseMapper.exists(new LambdaQueryWrapper<SysRole>()
.eq(SysRole::getRoleName, role.getRoleName()) .eq(SysRole::getRoleName, role.getRoleName())
.ne(SysRole::getRoleId, roleId)); .ne(ObjectUtil.isNotNull(role.getRoleId()), SysRole::getRoleId, role.getRoleId()));
if (count) { if (exist) {
return UserConstants.NOT_UNIQUE; return UserConstants.NOT_UNIQUE;
} }
return UserConstants.UNIQUE; return UserConstants.UNIQUE;
@ -154,11 +153,10 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/ */
@Override @Override
public String checkRoleKeyUnique(SysRole role) { public String checkRoleKeyUnique(SysRole role) {
Long roleId = ObjectUtil.isNull(role.getRoleId()) ? -1L : role.getRoleId(); boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysRole>()
boolean count = baseMapper.exists(new LambdaQueryWrapper<SysRole>()
.eq(SysRole::getRoleKey, role.getRoleKey()) .eq(SysRole::getRoleKey, role.getRoleKey())
.ne(SysRole::getRoleId, roleId)); .ne(ObjectUtil.isNotNull(role.getRoleId()), SysRole::getRoleId, role.getRoleId()));
if (count) { if (exist) {
return UserConstants.NOT_UNIQUE; return UserConstants.NOT_UNIQUE;
} }
return UserConstants.UNIQUE; return UserConstants.UNIQUE;