优化常量

This commit is contained in:
zhuoda 2021-10-07 22:04:57 +08:00
parent b9a76dc2d1
commit 8ead192c44
10 changed files with 52 additions and 19 deletions

View File

@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst; import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.common.constant.StringConst; import net.lab1024.smartadmin.service.common.constant.StringConst;
import net.lab1024.smartadmin.service.module.business.category.constant.CategoryConst;
import net.lab1024.smartadmin.service.module.business.category.domain.CategoryEntity; import net.lab1024.smartadmin.service.module.business.category.domain.CategoryEntity;
import net.lab1024.smartadmin.service.module.business.category.domain.CategorySimpleDTO; import net.lab1024.smartadmin.service.module.business.category.domain.CategorySimpleDTO;
import net.lab1024.smartadmin.service.module.business.category.domain.CategoryTreeQueryDTO; import net.lab1024.smartadmin.service.module.business.category.domain.CategoryTreeQueryDTO;
@ -118,7 +119,7 @@ public class CategoryQueryService {
*/ */
public List<CategoryEntity> queryCategoryByParent(Long categoryId, Integer categoryType) { public List<CategoryEntity> queryCategoryByParent(Long categoryId, Integer categoryType) {
if (null == categoryId) { if (null == categoryId) {
return StringConst.EMPTY_LIST; return Collections.emptyList();
} }
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_SUB, getCacheId(categoryId, categoryType)); String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_SUB, getCacheId(categoryId, categoryType));
return cache.get(cacheKey); return cache.get(cacheKey);
@ -132,7 +133,7 @@ public class CategoryQueryService {
*/ */
public Map<Long, CategoryEntity> queryCategoryList(List<Long> categoryIdList) { public Map<Long, CategoryEntity> queryCategoryList(List<Long> categoryIdList) {
if (CollectionUtils.isEmpty(categoryIdList)) { if (CollectionUtils.isEmpty(categoryIdList)) {
return StringConst.EMPTY_MAP; return Collections.emptyMap();
} }
categoryIdList = categoryIdList.stream().distinct().collect(Collectors.toList()); categoryIdList = categoryIdList.stream().distinct().collect(Collectors.toList());
@ -161,7 +162,7 @@ public class CategoryQueryService {
*/ */
public List<Long> queryCategorySubId(List<Long> categoryIdList) { public List<Long> queryCategorySubId(List<Long> categoryIdList) {
if (CollectionUtils.isEmpty(categoryIdList)) { if (CollectionUtils.isEmpty(categoryIdList)) {
return StringConst.EMPTY_LIST; return Collections.emptyList();
} }
// 查询所有子类 // 查询所有子类
Map<Long, List<CategoryEntity>> subTypeMap = this.querySubCategoryFromCache(categoryIdList); Map<Long, List<CategoryEntity>> subTypeMap = this.querySubCategoryFromCache(categoryIdList);
@ -288,7 +289,7 @@ public class CategoryQueryService {
// 父级始终放在第一位 // 父级始终放在第一位
parentCategoryList.add(0, categoryEntity); parentCategoryList.add(0, categoryEntity);
Long parentId = categoryEntity.getParentId(); Long parentId = categoryEntity.getParentId();
if (Objects.equals(StringConst.DEFAULT_PARENT_ID, parentId)) { if (Objects.equals(CategoryConst.DEFAULT_PARENT_ID, parentId)) {
return parentCategoryList; return parentCategoryList;
} }
parentCategoryList.addAll(0, this.queryCategoryAndParent(parentId)); parentCategoryList.addAll(0, this.queryCategoryAndParent(parentId));

View File

@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import net.lab1024.smartadmin.service.common.code.UserErrorCode; import net.lab1024.smartadmin.service.common.code.UserErrorCode;
import net.lab1024.smartadmin.service.common.constant.StringConst; import net.lab1024.smartadmin.service.common.constant.StringConst;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO; import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
import net.lab1024.smartadmin.service.module.business.category.constant.CategoryConst;
import net.lab1024.smartadmin.service.module.business.category.domain.*; import net.lab1024.smartadmin.service.module.business.category.domain.*;
import net.lab1024.smartadmin.service.common.util.SmartBeanUtil; import net.lab1024.smartadmin.service.common.util.SmartBeanUtil;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
@ -43,9 +44,9 @@ public class CategoryService {
return res; return res;
} }
// 没有父类则使用默认父类 // 没有父类则使用默认父类
Long parentId = null == addDTO.getParentId() ? StringConst.DEFAULT_PARENT_ID : addDTO.getParentId(); Long parentId = null == addDTO.getParentId() ? CategoryConst.DEFAULT_PARENT_ID : addDTO.getParentId();
categoryEntity.setParentId(parentId); categoryEntity.setParentId(parentId);
categoryEntity.setSort(null == addDTO.getSort() ? StringConst.ZERO : addDTO.getSort()); categoryEntity.setSort(null == addDTO.getSort() ? 0 : addDTO.getSort());
categoryEntity.setDeletedFlag(false); categoryEntity.setDeletedFlag(false);
// 保存数据 // 保存数据
@ -106,7 +107,7 @@ public class CategoryService {
if (Objects.equals(categoryEntity.getCategoryId(), parentId)) { if (Objects.equals(categoryEntity.getCategoryId(), parentId)) {
return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "父级类目怎么和自己相同了"); return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "父级类目怎么和自己相同了");
} }
if (!Objects.equals(parentId, StringConst.DEFAULT_PARENT_ID)) { if (!Objects.equals(parentId, CategoryConst.DEFAULT_PARENT_ID)) {
Optional<CategoryEntity> optional = categoryQueryService.queryCategory(parentId); Optional<CategoryEntity> optional = categoryQueryService.queryCategory(parentId);
if (!optional.isPresent()) { if (!optional.isPresent()) {
return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST, "父级类目不存在~"); return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST, "父级类目不存在~");
@ -120,7 +121,7 @@ public class CategoryService {
} else { } else {
// 如果没有父类 使用默认父类 // 如果没有父类 使用默认父类
parentId = StringConst.DEFAULT_PARENT_ID; parentId = CategoryConst.DEFAULT_PARENT_ID;
} }
// 校验同父类下 名称是否重复 // 校验同父类下 名称是否重复
@ -169,7 +170,7 @@ public class CategoryService {
if (null == queryDTO.getCategoryType()) { if (null == queryDTO.getCategoryType()) {
return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "类目类型不能为空"); return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "类目类型不能为空");
} }
queryDTO.setParentId(StringConst.DEFAULT_PARENT_ID); queryDTO.setParentId(CategoryConst.DEFAULT_PARENT_ID);
} }
List<CategoryTreeVO> treeList = categoryQueryService.queryCategoryTree(queryDTO); List<CategoryTreeVO> treeList = categoryQueryService.queryCategoryTree(queryDTO);
return ResponseDTO.ok(treeList); return ResponseDTO.ok(treeList);

View File

@ -0,0 +1,13 @@
package net.lab1024.smartadmin.service.module.business.category.constant;
/**
* 分类类型 枚举
*
* @author listen
* @date 2021/08/05 15:26
*/
public class CategoryConst {
public static final long DEFAULT_PARENT_ID = 0;
}

View File

@ -0,0 +1,10 @@
package net.lab1024.smartadmin.service.module.system.department;
class DepartmentConst {
/**
* 默认的顶级部门的parent id
*/
static final long DEFAULT_PARENT_ID = 0;
}

View File

@ -173,7 +173,7 @@ public class DepartmentService {
List<DepartmentVO> deptList = departmentVOList.stream().filter(e -> e.getId().equals(parentId)).collect(Collectors.toList()); List<DepartmentVO> deptList = departmentVOList.stream().filter(e -> e.getId().equals(parentId)).collect(Collectors.toList());
for (DepartmentVO item : deptList) { for (DepartmentVO item : deptList) {
result.add(item); result.add(item);
if (item.getParentId() != StringConst.DEFAULT_PARENT_ID && item.getParentId() != null) { if (item.getParentId() != DepartmentConst.DEFAULT_PARENT_ID && item.getParentId() != null) {
result.addAll(getParentDepartment(departmentVOList, item.getParentId(), result)); result.addAll(getParentDepartment(departmentVOList, item.getParentId(), result));
} }
} }
@ -341,7 +341,7 @@ public class DepartmentService {
if (departmentEntity == null) { if (departmentEntity == null) {
return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST); return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
} }
if (departmentEntity.getParentId() == null || departmentEntity.getParentId().equals(StringConst.DEFAULT_PARENT_ID)) { if (departmentEntity.getParentId() == null || departmentEntity.getParentId().equals(DepartmentConst.DEFAULT_PARENT_ID)) {
return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "此部门已经是根节点无法移动"); return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "此部门已经是根节点无法移动");
} }
DepartmentEntity parentEntity = departmentDao.selectById(departmentEntity.getParentId()); DepartmentEntity parentEntity = departmentDao.selectById(departmentEntity.getParentId());
@ -453,7 +453,7 @@ public class DepartmentService {
*/ */
private void recursionFindParentDepartmentName(List<String> departmentNameList, List<DepartmentVO> departmentList, Long departmentId) { private void recursionFindParentDepartmentName(List<String> departmentNameList, List<DepartmentVO> departmentList, Long departmentId) {
Optional<DepartmentVO> findRes = departmentList.stream().filter(e -> e.getId().equals(departmentId)).findFirst(); Optional<DepartmentVO> findRes = departmentList.stream().filter(e -> e.getId().equals(departmentId)).findFirst();
if (!findRes.isPresent() || findRes.get().getParentId() == StringConst.DEFAULT_PARENT_ID) { if (!findRes.isPresent() || findRes.get().getParentId() == DepartmentConst.DEFAULT_PARENT_ID) {
return; return;
} }
DepartmentVO departmentVO = findRes.get(); DepartmentVO departmentVO = findRes.get();
@ -501,7 +501,7 @@ public class DepartmentService {
private DepartmentVO recursionFindSchoolDepartmentId(List<DepartmentVO> departmentList, Long departmentId) { private DepartmentVO recursionFindSchoolDepartmentId(List<DepartmentVO> departmentList, Long departmentId) {
Optional<DepartmentVO> findRes = departmentList.stream().filter(e -> e.getId().equals(departmentId)).findFirst(); Optional<DepartmentVO> findRes = departmentList.stream().filter(e -> e.getId().equals(departmentId)).findFirst();
// 如果查询不到 或者自己本身为最顶级 返回null // 如果查询不到 或者自己本身为最顶级 返回null
if (!findRes.isPresent() || findRes.get().getParentId() == StringConst.DEFAULT_PARENT_ID) { if (!findRes.isPresent() || findRes.get().getParentId() == DepartmentConst.DEFAULT_PARENT_ID) {
return null; return null;
} }
DepartmentVO departmentVO = findRes.get(); DepartmentVO departmentVO = findRes.get();
@ -512,7 +512,7 @@ public class DepartmentService {
return null; return null;
} }
// 若父级为最顶级 则返回本级ID // 若父级为最顶级 则返回本级ID
if (parentFindRes.get().getParentId() == StringConst.DEFAULT_PARENT_ID) { if (parentFindRes.get().getParentId() == DepartmentConst.DEFAULT_PARENT_ID) {
return departmentVO; return departmentVO;
} }
// 若父级不为最顶级 进入递归 // 若父级不为最顶级 进入递归

View File

@ -29,7 +29,7 @@ public class DepartmentTreeService {
if (CollectionUtils.isEmpty(voList)) { if (CollectionUtils.isEmpty(voList)) {
return Lists.newArrayList(); return Lists.newArrayList();
} }
List<DepartmentVO> rootList = voList.stream().filter(e -> e.getParentId() == null || e.getParentId() == StringConst.DEFAULT_PARENT_ID).collect(Collectors.toList()); List<DepartmentVO> rootList = voList.stream().filter(e -> e.getParentId() == null || e.getParentId() == DepartmentConst.DEFAULT_PARENT_ID).collect(Collectors.toList());
if (CollectionUtils.isEmpty(rootList)) { if (CollectionUtils.isEmpty(rootList)) {
return Lists.newArrayList(); return Lists.newArrayList();
} }

View File

@ -28,6 +28,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -402,7 +403,7 @@ public class EmployeeService {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString()); String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString());
List<EmployeeEntity> employeeEntityList = beanCache.get(cacheKey); List<EmployeeEntity> employeeEntityList = beanCache.get(cacheKey);
if (CollectionUtils.isEmpty(employeeEntityList)) { if (CollectionUtils.isEmpty(employeeEntityList)) {
return ResponseDTO.ok(StringConst.EMPTY_LIST); return ResponseDTO.ok(Collections.emptyList());
} }
List<EmployeeVO> voList = SmartBeanUtil.copyList(employeeEntityList, EmployeeVO.class); List<EmployeeVO> voList = SmartBeanUtil.copyList(employeeEntityList, EmployeeVO.class);
return ResponseDTO.ok(voList); return ResponseDTO.ok(voList);

View File

@ -0,0 +1,6 @@
package net.lab1024.smartadmin.service.module.system.menu;
public class MenuConst {
public static final long DEFAULT_PARENT_ID = 0;
}

View File

@ -231,7 +231,7 @@ public class MenuService {
List<MenuVO> menuVOList = menuDao.queryMenuList(Boolean.FALSE, disabledFlag, null); List<MenuVO> menuVOList = menuDao.queryMenuList(Boolean.FALSE, disabledFlag, null);
//根据ParentId进行分组 //根据ParentId进行分组
Map<Long, List<MenuVO>> parentMap = menuVOList.stream().collect(Collectors.groupingBy(MenuVO::getParentId, Collectors.toList())); Map<Long, List<MenuVO>> parentMap = menuVOList.stream().collect(Collectors.groupingBy(MenuVO::getParentId, Collectors.toList()));
List<MenuVO> filterMenuVOList = this.filterNoParentMenu(parentMap, StringConst.DEFAULT_PARENT_ID); List<MenuVO> filterMenuVOList = this.filterNoParentMenu(parentMap, MenuConst.DEFAULT_PARENT_ID);
return filterMenuVOList; return filterMenuVOList;
} }
@ -275,7 +275,7 @@ public class MenuService {
List<MenuVO> menuVOList = menuDao.queryMenuList(Boolean.FALSE, null, menuTypeList); List<MenuVO> menuVOList = menuDao.queryMenuList(Boolean.FALSE, null, menuTypeList);
//根据ParentId进行分组 //根据ParentId进行分组
Map<Long, List<MenuVO>> parentMap = menuVOList.stream().collect(Collectors.groupingBy(MenuVO::getParentId, Collectors.toList())); Map<Long, List<MenuVO>> parentMap = menuVOList.stream().collect(Collectors.groupingBy(MenuVO::getParentId, Collectors.toList()));
List<MenuTreeVO> menuTreeVOList = this.buildMenuTree(parentMap, StringConst.DEFAULT_PARENT_ID); List<MenuTreeVO> menuTreeVOList = this.buildMenuTree(parentMap, MenuConst.DEFAULT_PARENT_ID);
return ResponseDTO.ok(menuTreeVOList); return ResponseDTO.ok(menuTreeVOList);
} }

View File

@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import net.lab1024.smartadmin.service.common.code.UserErrorCode; import net.lab1024.smartadmin.service.common.code.UserErrorCode;
import net.lab1024.smartadmin.service.common.constant.StringConst; import net.lab1024.smartadmin.service.common.constant.StringConst;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO; import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
import net.lab1024.smartadmin.service.module.system.menu.MenuConst;
import net.lab1024.smartadmin.service.module.system.menu.MenuDao; import net.lab1024.smartadmin.service.module.system.menu.MenuDao;
import net.lab1024.smartadmin.service.module.system.menu.MenuEmployeeService; import net.lab1024.smartadmin.service.module.system.menu.MenuEmployeeService;
import net.lab1024.smartadmin.service.module.system.menu.domain.MenuSimpleTreeVO; import net.lab1024.smartadmin.service.module.system.menu.domain.MenuSimpleTreeVO;
@ -83,7 +84,7 @@ public class RoleMenuService {
//查询菜单权限 //查询菜单权限
List<MenuVO> menuVOList = menuDao.queryMenuList(Boolean.FALSE, Boolean.FALSE, null); List<MenuVO> menuVOList = menuDao.queryMenuList(Boolean.FALSE, Boolean.FALSE, null);
Map<Long, List<MenuVO>> parentMap = menuVOList.stream().collect(Collectors.groupingBy(MenuVO::getParentId, Collectors.toList())); Map<Long, List<MenuVO>> parentMap = menuVOList.stream().collect(Collectors.groupingBy(MenuVO::getParentId, Collectors.toList()));
List<MenuSimpleTreeVO> menuTreeList = this.buildMenuTree(parentMap, StringConst.DEFAULT_PARENT_ID); List<MenuSimpleTreeVO> menuTreeList = this.buildMenuTree(parentMap, MenuConst.DEFAULT_PARENT_ID);
res.setMenuTreeList(menuTreeList); res.setMenuTreeList(menuTreeList);
return ResponseDTO.ok(res); return ResponseDTO.ok(res);
} }