This commit is contained in:
yandanyang 2021-10-11 20:06:42 +08:00
parent da046ba20e
commit d0086cf68d
25 changed files with 263 additions and 1312 deletions

View File

@ -25,10 +25,6 @@ public class CacheModuleConst {
public static class Department { public static class Department {
/**
* 部门树
*/
public static final String DEPARTMENT_CACHE = "department_cache";
/** /**
* 部门树 * 部门树
@ -38,7 +34,7 @@ public class CacheModuleConst {
/** /**
* 某个部门以及下级的id列表 * 某个部门以及下级的id列表
*/ */
public static final String DEPARTMENT_TREE_ID_CACHE = "department_tree_id_cache"; public static final String DEPARTMENT_SELF_CHILDREN_ID_CACHE = "department_self_children_id_cache";
} }

View File

@ -1,61 +0,0 @@
package net.lab1024.smartadmin.service.config;
import com.github.benmanes.caffeine.cache.LoadingCache;
import net.lab1024.smartadmin.service.module.support.beancache.cache.AbstractCaffeineCache;
import net.lab1024.smartadmin.service.module.support.beancache.cache.AbstractDisableCache;
import net.lab1024.smartadmin.service.module.support.beancache.cache.CacheLoadMethodRegister;
import net.lab1024.smartadmin.service.module.support.beancache.cache.IBeanCache;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* [ ]
*
* @author zhuoda
*/
@Configuration
public class CacheConfig {
@Value("${cache.maximumSize:5000}")
private Integer cacheMaximumSize;
@Value("${cache.expireDays:5}")
private Integer expireDays;
@Bean
public CacheLoadMethodRegister methodRegister(){
return new CacheLoadMethodRegister();
}
@Bean
@Primary
public IBeanCache beanCache(CacheLoadMethodRegister methodRegister) {
return new AbstractCaffeineCache() {
LoadingCache<String, CacheData> cache = this.initCache(cacheMaximumSize, expireDays);
@Override
public LoadingCache getCache() {
return cache;
}
@Override
public CacheLoadMethodRegister methodRegister() {
return methodRegister;
}
};
}
@Bean
@ConditionalOnMissingBean(IBeanCache.class)
public IBeanCache beanDisableCache(CacheLoadMethodRegister methodRegister) {
return new AbstractDisableCache() {
@Override
public CacheLoadMethodRegister methodRegister() {
return methodRegister;
}
};
}
}

View File

@ -0,0 +1,116 @@
package net.lab1024.smartadmin.service.module.business.category;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.common.constant.StringConst;
import net.lab1024.smartadmin.service.common.util.SmartBeanUtil;
import net.lab1024.smartadmin.service.module.business.category.domain.CategoryEntity;
import net.lab1024.smartadmin.service.module.business.category.domain.CategoryTreeVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 类目 查询 业务类
*
* @author 胡克
* @date 2021/1/20 16:26
*/
@Service
@Slf4j
public class CategoryCacheManager {
@Autowired
private CategoryDao categoryDao;
/**
* 根据类目id 移除缓存
*/
@CacheEvict(value = {CacheModuleConst.Category.CATEGORY, CacheModuleConst.Category.CATEGORY_SUB, CacheModuleConst.Category.CATEGORY_TREE}, allEntries = true)
public void removeCache() {
log.info("clear CATEGORY ,CATEGORY_SUB ,CATEGORY_TREE");
}
/**
* 查詢类目
*
* @param categoryId
* @return
*/
@Cacheable(CacheModuleConst.Category.CATEGORY)
public CategoryEntity queryCategory(Long categoryId) {
return categoryDao.selectById(categoryId);
}
/**
* 查询类目 子级
*
* @param categoryId
* @return
*/
@Cacheable(CacheModuleConst.Category.CATEGORY_SUB)
public List<CategoryEntity> querySubCategory(Long categoryId) {
return categoryDao.queryByParentId(Lists.newArrayList(categoryId), false);
}
/**
* 查询类目 层级树
* 优先查询缓存
*
* @return
*/
@Cacheable(CacheModuleConst.Category.CATEGORY_TREE)
public List<CategoryTreeVO> queryCategoryTree(Long parentId, Integer categoryType) {
List<CategoryEntity> allCategoryEntityList = categoryDao.queryByParentIdAndType(Lists.newArrayList(NumberUtils.LONG_ZERO), categoryType, false);
List<CategoryEntity> categoryEntityList = allCategoryEntityList.stream().filter(e -> e.getParentId().equals(parentId)).collect(Collectors.toList());
List<CategoryTreeVO> treeList = SmartBeanUtil.copyList(categoryEntityList, CategoryTreeVO.class);
treeList.forEach(e -> {
e.setLabel(e.getCategoryName());
e.setValue(e.getCategoryId());
e.setCategoryFullName(e.getCategoryName());
});
// 递归设置子类
this.queryAndSetSubCategory(treeList, allCategoryEntityList);
return treeList;
}
/**
* 递归查询设置类目子类
* 从缓存查询子类
*
* @param treeList
*/
private void queryAndSetSubCategory(List<CategoryTreeVO> treeList, List<CategoryEntity> allCategoryEntityList) {
if (CollectionUtils.isEmpty(treeList)) {
return;
}
List<Long> parentIdList = treeList.stream().map(CategoryTreeVO::getValue).collect(Collectors.toList());
List<CategoryEntity> categoryEntityList = allCategoryEntityList.stream().filter(e -> parentIdList.contains(e.getParentId())).collect(Collectors.toList());
Map<Long, List<CategoryEntity>> categorySubMap = categoryEntityList.stream().collect(Collectors.groupingBy(CategoryEntity::getCategoryId));
treeList.forEach(e -> {
List<CategoryEntity> childrenEntityList = categorySubMap.getOrDefault(e.getValue(), Lists.newArrayList());
List<CategoryTreeVO> childrenVOList = SmartBeanUtil.copyList(childrenEntityList, CategoryTreeVO.class);
childrenVOList.forEach(item -> {
item.setLabel(item.getCategoryName());
item.setValue(item.getCategoryId());
item.setCategoryFullName(e.getCategoryFullName() + StringConst.SEPARATOR_SLASH + item.getCategoryName());
});
// 递归查询
this.queryAndSetSubCategory(childrenVOList, allCategoryEntityList);
e.setChildren(childrenVOList);
});
}
}

View File

@ -23,11 +23,21 @@ public interface CategoryDao extends BaseMapper<CategoryEntity> {
* 根据父级id 类型 查询子类 * 根据父级id 类型 查询子类
* *
* @param parentIdList * @param parentIdList
* @param categoryType {@link CategoryTypeEnum}
* @param deletedFlag * @param deletedFlag
* @return * @return
*/ */
List<CategoryEntity> queryByParentId(@Param("parentIdList") List<Long> parentIdList, List<CategoryEntity> queryByParentId(@Param("parentIdList") List<Long> parentIdList,
@Param("deletedFlag") Boolean deletedFlag);
/**
* 根据父级id 类型 查询子类
*
* @param parentIdList
* @param categoryType {@link CategoryTypeEnum}
* @param deletedFlag
* @return
*/
List<CategoryEntity> queryByParentIdAndType(@Param("parentIdList") List<Long> parentIdList,
@Param("categoryType") Integer categoryType, @Param("categoryType") Integer categoryType,
@Param("deletedFlag") Boolean deletedFlag); @Param("deletedFlag") Boolean deletedFlag);

View File

@ -2,20 +2,8 @@ package net.lab1024.smartadmin.service.module.business.category;
import com.google.common.collect.Lists; 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.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.CategoryTreeQueryForm;
import net.lab1024.smartadmin.service.module.business.category.domain.CategoryTreeVO;
import net.lab1024.smartadmin.service.module.support.beancache.cache.IBeanCache;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.anno.CacheLoad;
import net.lab1024.smartadmin.service.common.util.SmartBeanUtil;
import net.lab1024.smartadmin.service.common.util.SmartStringUtil;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -37,61 +25,8 @@ public class CategoryQueryService {
private CategoryDao categoryDao; private CategoryDao categoryDao;
@Autowired @Autowired
private IBeanCache cache; private CategoryCacheManager categoryCacheManager;
/**
* 查詢类目
*
* @param cacheKey
* @return
*/
@CacheLoad(CacheModuleConst.Category.CATEGORY)
public CategoryEntity queryCategory(String cacheKey) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
return categoryDao.selectById(businessId);
}
/**
* 查询类目 子级
*
* @param cacheKey
* @return
*/
@CacheLoad(CacheModuleConst.Category.CATEGORY_SUB)
public List<CategoryEntity> querySubCategory(String cacheKey) {
/**
* 下划线 分隔 key
* 左边 categoryId 右边 type
*/
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
String[] split = businessId.split(StringConst.UNDERLINE);
Integer categoryType = split.length > 1 ? Integer.valueOf(split[1]) : null;
return categoryDao.queryByParentId(Lists.newArrayList(Long.valueOf(split[0])), categoryType, false);
}
/**
* 类目id+下划线+类型 作为缓存key
*
* @param categoryId
* @param categoryType
* @return
*/
private static String getCacheId(Long categoryId, Integer categoryType) {
return categoryId + StringConst.UNDERLINE + categoryType;
}
/**
* 批量查询类目 子级
*
* @param categoryIdList
* @return
*/
public Map<Long, List<CategoryEntity>> querySubCategoryFromCache(List<Long> categoryIdList) {
return categoryIdList.stream().collect(Collectors.toMap(Function.identity(), e -> {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_SUB, e.toString());
return cache.get(cacheKey);
}));
}
/** /**
* 根据 id 查询未删除的类目 * 根据 id 查询未删除的类目
@ -103,27 +38,13 @@ public class CategoryQueryService {
if (null == categoryId) { if (null == categoryId) {
return Optional.empty(); return Optional.empty();
} }
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString()); CategoryEntity entity = categoryCacheManager.queryCategory(categoryId);
CategoryEntity entity = cache.get(cacheKey);
if (null == entity || entity.getDeletedFlag()) { if (null == entity || entity.getDeletedFlag()) {
return Optional.empty(); return Optional.empty();
} }
return Optional.of(entity); return Optional.of(entity);
} }
/**
* 根据 类目id 查询未删除的子类
*
* @param categoryId
* @return 没有返回空集合
*/
public List<CategoryEntity> queryCategoryByParent(Long categoryId, Integer categoryType) {
if (null == categoryId) {
return Collections.emptyList();
}
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_SUB, getCacheId(categoryId, categoryType));
return cache.get(cacheKey);
}
/** /**
* 根据 类目id集合 查询未删除的类目集合 * 根据 类目id集合 查询未删除的类目集合
@ -138,20 +59,11 @@ public class CategoryQueryService {
categoryIdList = categoryIdList.stream().distinct().collect(Collectors.toList()); categoryIdList = categoryIdList.stream().distinct().collect(Collectors.toList());
return categoryIdList.stream().collect(Collectors.toMap(Function.identity(), e -> { return categoryIdList.stream().collect(Collectors.toMap(Function.identity(), e -> {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, e.toString()); CategoryEntity categoryEntity = categoryCacheManager.queryCategory(e);
return cache.get(cacheKey); return categoryEntity;
})); }));
} }
/**
* 根据类目id 移除缓存
*/
public void removeCache() {
cache.removeByModule(CacheModuleConst.Category.CATEGORY);
cache.removeByModule(CacheModuleConst.Category.CATEGORY_SUB);
// 移除整个类目树缓存
cache.removeByModule(CacheModuleConst.Category.CATEGORY_TREE);
}
/** /**
* 根据类目id 递归查询该id的所有子类id 递归查询 * 根据类目id 递归查询该id的所有子类id 递归查询
@ -164,11 +76,12 @@ public class CategoryQueryService {
if (CollectionUtils.isEmpty(categoryIdList)) { if (CollectionUtils.isEmpty(categoryIdList)) {
return Collections.emptyList(); return Collections.emptyList();
} }
// 查询所有子类 //所有子类
Map<Long, List<CategoryEntity>> subTypeMap = this.querySubCategoryFromCache(categoryIdList); List<CategoryEntity> categoryEntityList = Lists.newArrayList();
if (MapUtils.isEmpty(subTypeMap)) { categoryIdList.forEach(e -> {
return Lists.newArrayList(); categoryEntityList.addAll(categoryCacheManager.querySubCategory(e));
} });
Map<Long, List<CategoryEntity>> subTypeMap = categoryEntityList.stream().collect(Collectors.groupingBy(CategoryEntity::getCategoryId));
// 递归查询子类 // 递归查询子类
categoryIdList = subTypeMap.values().stream().flatMap(Collection::stream).map(CategoryEntity::getCategoryId).distinct().collect(Collectors.toList()); categoryIdList = subTypeMap.values().stream().flatMap(Collection::stream).map(CategoryEntity::getCategoryId).distinct().collect(Collectors.toList());
if (CollectionUtils.isEmpty(categoryIdList)) { if (CollectionUtils.isEmpty(categoryIdList)) {
@ -178,123 +91,6 @@ public class CategoryQueryService {
return categoryIdList; return categoryIdList;
} }
/**
* 查询自身以及所有子节点
* @param categoryIdList
* @return
*/
public List<Long> queryCategorySelfAndSubId(List<Long> categoryIdList) {
List<Long> subIdList = this.queryCategorySubId(categoryIdList);
subIdList.addAll(categoryIdList);
return subIdList;
}
/**
* 查询类目 层级树
* 优先查询缓存
*
* @return
*/
public List<CategoryTreeVO> queryCategoryTree(CategoryTreeQueryForm queryForm) {
// 查询缓存
Long parentId = queryForm.getParentId();
Integer categoryType = queryForm.getCategoryType();
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_TREE, getCacheId(parentId, categoryType));
List<CategoryTreeVO> treeList = cache.get(cacheKey);
if (null != treeList) {
return treeList;
}
// 查询一级类目
List<CategoryEntity> categoryEntityList = this.queryCategoryByParent(parentId, categoryType);
treeList = SmartBeanUtil.copyList(categoryEntityList, CategoryTreeVO.class);
treeList.forEach(e -> {
e.setLabel(e.getCategoryName());
e.setValue(e.getCategoryId());
e.setCategoryFullName(e.getCategoryName());
});
// 递归设置子类
this.queryAndSetSubCategory(treeList);
// 放入缓存
cache.put(cacheKey, treeList);
return treeList;
}
/**
* 递归查询设置类目子类
* 从缓存查询子类
*
* @param treeList
*/
private void queryAndSetSubCategory(List<CategoryTreeVO> treeList) {
if (CollectionUtils.isEmpty(treeList)) {
return;
}
List<Long> parentIdList = treeList.stream().map(CategoryTreeVO::getValue).collect(Collectors.toList());
Map<Long, List<CategoryEntity>> categorySubMap = this.querySubCategoryFromCache(parentIdList);
treeList.forEach(e -> {
List<CategoryEntity> childrenEntityList = categorySubMap.getOrDefault(e.getValue(), Lists.newArrayList());
List<CategoryTreeVO> childrenVOList = SmartBeanUtil.copyList(childrenEntityList, CategoryTreeVO.class);
childrenVOList.forEach(item -> {
item.setLabel(item.getCategoryName());
item.setValue(item.getCategoryId());
item.setCategoryFullName(e.getCategoryFullName() + StringConst.SEPARATOR_SLASH + item.getCategoryName());
});
// 递归查询
this.queryAndSetSubCategory(childrenVOList);
e.setChildren(childrenVOList);
});
}
/**
* 根据类目id 查询类目详情 包含类目全称 医考/医师资格/临床执业
*
* @param categoryId
* @return
*/
public CategorySimpleDTO queryCategoryInfo(Long categoryId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString());
CategoryEntity categoryEntity = cache.get(cacheKey);
if (null == categoryEntity || categoryEntity.getDeletedFlag()) {
return null;
}
// 递归查询分类和所有父级类目
List<CategoryEntity> parentCategoryList = this.queryCategoryAndParent(categoryId);
// 拼接父级类目名称 斜杠分隔返回
List<String> nameList = parentCategoryList.stream().map(CategoryEntity::getCategoryName).collect(Collectors.toList());
// 返回DTO
CategorySimpleDTO categoryDTO = new CategorySimpleDTO();
categoryDTO.setCategoryId(categoryId);
categoryDTO.setCategoryName(categoryEntity.getCategoryName());
categoryDTO.setCategoryFullName(SmartStringUtil.join(nameList, StringConst.SEPARATOR_SLASH));
categoryDTO.setParentId(categoryEntity.getParentId());
return categoryDTO;
}
/**
* 递归查询分类和所有父级类目 ps:特别注意返回的集合中 包含自己
*
* @param categoryId
* @return
*/
public List<CategoryEntity> queryCategoryAndParent(Long categoryId) {
List<CategoryEntity> parentCategoryList = Lists.newArrayList();
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString());
CategoryEntity categoryEntity = cache.get(cacheKey);
if (null == categoryEntity || categoryEntity.getDeletedFlag()) {
return parentCategoryList;
}
// 父级始终放在第一位
parentCategoryList.add(0, categoryEntity);
Long parentId = categoryEntity.getParentId();
if (Objects.equals(CategoryConst.DEFAULT_PARENT_ID, parentId)) {
return parentCategoryList;
}
parentCategoryList.addAll(0, this.queryCategoryAndParent(parentId));
return parentCategoryList;
}
/** /**
* 处理类目名称 * 处理类目名称
@ -323,8 +119,7 @@ public class CategoryQueryService {
* @return * @return
*/ */
public String queryCategoryName(Long categoryId) { public String queryCategoryName(Long categoryId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString()); CategoryEntity categoryEntity = categoryCacheManager.queryCategory(categoryId);
CategoryEntity categoryEntity = cache.get(cacheKey);
if (null == categoryEntity || categoryEntity.getDeletedFlag()) { if (null == categoryEntity || categoryEntity.getDeletedFlag()) {
return null; return null;
} }

View File

@ -3,10 +3,10 @@ package net.lab1024.smartadmin.service.module.business.category;
import com.google.common.collect.Lists; 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.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;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -29,6 +29,9 @@ public class CategoryService {
@Autowired @Autowired
private CategoryQueryService categoryQueryService; private CategoryQueryService categoryQueryService;
@Autowired
private CategoryCacheManager categoryCacheManager;
/** /**
* 添加类目 * 添加类目
* *
@ -43,7 +46,7 @@ public class CategoryService {
return res; return res;
} }
// 没有父类则使用默认父类 // 没有父类则使用默认父类
Long parentId = null == addForm.getParentId() ? CategoryConst.DEFAULT_PARENT_ID : addForm.getParentId(); Long parentId = null == addForm.getParentId() ? NumberUtils.LONG_ZERO : addForm.getParentId();
categoryEntity.setParentId(parentId); categoryEntity.setParentId(parentId);
categoryEntity.setSort(null == addForm.getSort() ? 0 : addForm.getSort()); categoryEntity.setSort(null == addForm.getSort() ? 0 : addForm.getSort());
categoryEntity.setDeletedFlag(false); categoryEntity.setDeletedFlag(false);
@ -52,7 +55,7 @@ public class CategoryService {
categoryDao.insert(categoryEntity); categoryDao.insert(categoryEntity);
// 更新缓存 // 更新缓存
categoryQueryService.removeCache(); categoryCacheManager.removeCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -87,7 +90,7 @@ public class CategoryService {
categoryDao.updateById(categoryEntity); categoryDao.updateById(categoryEntity);
// 更新缓存 // 更新缓存
categoryQueryService.removeCache(); categoryCacheManager.removeCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -106,7 +109,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, CategoryConst.DEFAULT_PARENT_ID)) { if (!Objects.equals(parentId, NumberUtils.LONG_ZERO)) {
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 +123,7 @@ public class CategoryService {
} else { } else {
// 如果没有父类 使用默认父类 // 如果没有父类 使用默认父类
parentId = CategoryConst.DEFAULT_PARENT_ID; parentId = NumberUtils.LONG_ZERO;
} }
// 校验同父类下 名称是否重复 // 校验同父类下 名称是否重复
@ -169,9 +172,9 @@ public class CategoryService {
if (null == queryForm.getCategoryType()) { if (null == queryForm.getCategoryType()) {
return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "类目类型不能为空"); return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "类目类型不能为空");
} }
queryForm.setParentId(CategoryConst.DEFAULT_PARENT_ID); queryForm.setParentId(NumberUtils.LONG_ZERO);
} }
List<CategoryTreeVO> treeList = categoryQueryService.queryCategoryTree(queryForm); List<CategoryTreeVO> treeList = categoryCacheManager.queryCategoryTree(queryForm.getParentId(),queryForm.getCategoryType());
return ResponseDTO.ok(treeList); return ResponseDTO.ok(treeList);
} }
@ -200,7 +203,7 @@ public class CategoryService {
categoryDao.updateById(categoryEntity); categoryDao.updateById(categoryEntity);
// 更新缓存 // 更新缓存
categoryQueryService.removeCache(); categoryCacheManager.removeCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }

View File

@ -1,13 +0,0 @@
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

@ -1,26 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.anno;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* [ ]
*
* @author 罗伊
* @date 2020/9/6 15:53
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CacheLoad {
String value();
/**
* 过期时间 默认不过期依据总体过期时间定义
* @return
*/
long expireSecond() default 0L;
}

View File

@ -1,227 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.cache;
import com.alibaba.fastjson.JSON;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheData;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKeyBuilder;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod;
import net.lab1024.smartadmin.service.third.SmartApplicationContext;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
/**
* [ ]
*
* @author 罗伊
* @date 2021/4/14 15:12
*/
public abstract class AbstractCaffeineCache implements IBeanCache {
/**
* 获取缓存
* @return
*/
@Override
public abstract LoadingCache<String, CacheData> getCache();
/**
* 缓存加载方法
* @return
*/
@Override
public abstract CacheLoadMethodRegister methodRegister();
/**
* 移除某个缓存
* @param key
*/
@Override
public void remove(String key) {
getCache().invalidate(key);
}
/**
* 清除所有
*/
@Override
public void clear() {
getCache().invalidateAll();
}
/**
* 获取某个缓存对象
* @param key
* @return
*/
@Override
public <T> T get(String key) {
LoadingCache<String, CacheData> cache = getCache();
CacheData cacheData = cache.get(key);
if(cacheData == null){
return null;
}
if(cacheData.getExpireTime() == null){
return (T)cacheData.getData();
}
long expireTime = cacheData.getExpireTime();
long current = System.currentTimeMillis();
if(expireTime > current){
return (T)cacheData.getData();
}
//缓存过期 移除key 重新获取
cache.invalidate(key);
return (T)cache.get(key).getData();
}
/**
* 刷新某个缓存key-异步刷新
* @param key
*/
@Override
public void refresh(String key){
LoadingCache<String, CacheData> cache = getCache();
cache.refresh(key);
}
/**
* 设置key->value
* @param key
* @param obj
*/
@Override
public void put(String key, Object obj) {
CacheData cacheData = new CacheData();
cacheData.setData(obj);
getCache().put(key, cacheData);
}
@Override
public void put(String key, Object obj, long expireSecond) {
CacheData cacheData = new CacheData();
long expireTime = System.currentTimeMillis() + expireSecond * 1000;
cacheData.setExpireTime(expireTime);
cacheData.setData(obj);
getCache().put(key, cacheData);
}
/**
* 判断是否包含某个key
* @param key
* @return
*/
@Override
public boolean containsKey(String key) {
ConcurrentMap concurrentMap = getCache().asMap();
return concurrentMap.containsKey(key);
}
/**
* 所有key集合
* @return
*/
@Override
public Set<String> keySet() {
ConcurrentMap concurrentMap = getCache().asMap();
return concurrentMap.keySet();
}
/**
* 移除某个模块的所有key
* @param module
*/
@Override
public void removeByModule(String module) {
ConcurrentMap concurrentMap = getCache().asMap();
List<String> removeKeys = Lists.newArrayList();
Set<String> keySet = concurrentMap.keySet();
keySet.forEach(e -> {
CacheKeyBuilder cacheKeyBuilder = JSON.parseObject(e, CacheKeyBuilder.class);
if (cacheKeyBuilder.getCacheModule().equals(module)){
removeKeys.add(e);
}
});
if (CollectionUtils.isNotEmpty(removeKeys)) {
getCache().invalidateAll(removeKeys);
}
}
/**
* 移除某个模块 某个分组下的缓存
* @param module
* @param group
*/
@Override
public void removeByModuleAndGroup(String module, String group){
ConcurrentMap concurrentMap = getCache().asMap();
List<String> removeKeys = Lists.newArrayList();
Set<String> keySet = concurrentMap.keySet();
keySet.forEach(e -> {
CacheKeyBuilder cacheKeyBuilder = JSON.parseObject(e, CacheKeyBuilder.class);
if (cacheKeyBuilder.getCacheModule().equals(module) && group.equals(cacheKeyBuilder.getGroup())){
removeKeys.add(e);
}
});
if (CollectionUtils.isNotEmpty(removeKeys)) {
getCache().invalidateAll(removeKeys);
}
}
/**
* 根据缓存个数和过期时间初始化缓存的方法
* @param expireDays
* @param maximumSize
* @param scanPath
* @return
*/
public LoadingCache<String, CacheData> initCache(Integer expireDays, Integer maximumSize) {
//构建缓存对象
Caffeine<Object, Object> builder = Caffeine.newBuilder();
if(maximumSize != null){
builder.maximumSize(maximumSize);
}
if(expireDays != null){
builder.expireAfterAccess(expireDays, TimeUnit.DAYS);
}
return builder.recordStats()
.build(key -> {
String cacheModule = CacheKey.getCacheModeByCacheKey(key);
CacheLoadMethodRegister methodRegister = this.methodRegister();
CacheLoadMethod loadMethod = methodRegister.getCacheLoadMethod(cacheModule);
if (loadMethod == null) {
return null;
}
Method method = loadMethod.getLoadMethod();
Object object = SmartApplicationContext.getBean(method.getDeclaringClass());
//方法返回值
Object result = null;
if (method.getParameterCount() == 0) {
result = method.invoke(object);
}else{
result = method.invoke(object, key);
}
if(result == null){
return null;
}
//指定咖啡因缓存返回值
Long expireTime = null;
if(loadMethod.getExpireSecond()>0){
expireTime = System.currentTimeMillis() + loadMethod.getExpireSecond() * 1000;
}
CacheData cacheData = new CacheData();
cacheData.setExpireTime(expireTime);
cacheData.setData(result);
return cacheData;
});
}
}

View File

@ -1,118 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.cache;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod;
import net.lab1024.smartadmin.service.third.SmartApplicationContext;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;
/**
* [ ]
*
* @author 罗伊
* @date 2021/4/14 15:27
*/
@Slf4j
public abstract class AbstractDisableCache implements IBeanCache {
/**
* 缓存加载方法
* @return
*/
@Override
public abstract CacheLoadMethodRegister methodRegister();
@Override
public Map<String, Object> getCache() {
log.warn("Cache is disable!");
return Maps.newHashMap();
}
@Override
public void remove(String key) {
log.warn("Cache is disable!");
return;
}
@Override
public void clear() {
log.warn("Cache is disable!");
return;
}
@Override
public <T> T get(String key) {
log.warn("Cache is disable!");
String cacheModule = CacheKey.getCacheModeByCacheKey(key);
CacheLoadMethodRegister methodRegister = this.methodRegister();
CacheLoadMethod loadMethod = methodRegister.getCacheLoadMethod(cacheModule);
if (loadMethod == null) {
throw null;
}
Method method = loadMethod.getLoadMethod();
Object object = SmartApplicationContext.getBean(method.getDeclaringClass());
Object result = null;
try {
if (method.getParameterCount() == 0) {
result = method.invoke(object);
}else{
result = method.invoke(object, key);
}
}catch (Exception e){
log.error("Cache get exception:{}",e);
}
return (T) result;
}
@Override
public void refresh(String key) {
log.warn("Cache is disable!");
return;
}
@Override
public void put(String key, Object obj) {
log.warn("Cache is disable!");
return;
}
@Override
public void put(String key, Object obj, long expireSecond) {
log.warn("Cache is disable!");
return;
}
@Override
public boolean containsKey(String key) {
log.warn("Cache is disable!");
return false;
}
@Override
public Set<String> keySet() {
log.warn("Cache is disable!");
return Sets.newHashSet();
}
@Override
public void removeByModule(String module) {
log.warn("Cache is disable!");
return;
}
@Override
public void removeByModuleAndGroup(String module, String group) {
log.warn("Cache is disable!");
return;
}
}

View File

@ -1,48 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.cache;
import net.lab1024.smartadmin.service.module.support.beancache.anno.CacheLoad;
import net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* [ ]
*
* @author yandanyang
* @date 2021/9/25 16:32
*/
public class CacheLoadMethodRegister implements BeanPostProcessor {
private Map<String, CacheLoadMethod> cacheLoadMethodMap = new HashMap<>();
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
if (methods == null) {
return bean;
}
for (Method method : methods) {
CacheLoad cacheLoad = method.getAnnotation(CacheLoad.class);
if (cacheLoad != null) {
String cacheModule = cacheLoad.value();
net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod cacheLoadMethod = new net.lab1024.smartadmin.service.module.support.beancache.domain.CacheLoadMethod();
cacheLoadMethod.setCacheModule(cacheModule);
cacheLoadMethod.setExpireSecond(cacheLoad.expireSecond());
cacheLoadMethod.setLoadMethod(method);
cacheLoadMethodMap.put(cacheModule, cacheLoadMethod);
}
}
return bean;
}
public CacheLoadMethod getCacheLoadMethod(String key){
return cacheLoadMethodMap.get(key);
}
}

View File

@ -1,95 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.cache;
import java.util.Set;
/**
* [ ]
*
* @author 罗伊
* @date 2020/9/6 15:55
*/
public interface IBeanCache {
CacheLoadMethodRegister methodRegister();
/**
* 获取缓存
*
* @return
*/
<T> T getCache();
/**
* 移除某个缓存
*
* @param key
*/
void remove(String key);
/**
* 清除所有
*/
void clear();
/**
* 获取某个缓存对象
*
* @param key
* @return
*/
<T> T get(String key);
/**
* 刷新某个缓存key-异步刷新
*
* @param key
*/
void refresh(String key);
/**
* 设置key->value
*
* @param key
* @param obj
*/
void put(String key, Object obj);
/**
* 待过期时间get
*
* @param key
* @param obj
* @param expireSecond
*/
void put(String key, Object obj, long expireSecond);
/**
* 判断是否包含某个key
*
* @param key
* @return
*/
boolean containsKey(String key);
/**
* 所有key集合
*
* @return
*/
Set<String> keySet();
/**
* 移除某个模块的所有key
*
* @param module
*/
void removeByModule(String module);
/**
* 移除某个模块 某个分组下的缓存
*
* @param module
* @param group
*/
void removeByModuleAndGroup(String module, String group);
}

View File

@ -1,23 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.domain;
import lombok.Data;
/**
* [ ]
*
* @author 罗伊
* @date 2021/4/22 17:21
*/
@Data
public class CacheData<T> {
/**
* 过期时间
*/
private Long expireTime;
/**
* 缓存数据
*/
private T data;
}

View File

@ -1,29 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.domain;
import lombok.Data;
import java.lang.reflect.Method;
/**
* [ ]
*
* @author 罗伊
* @date 2021/4/22 17:28
*/
@Data
public class CacheLoadMethod {
/**
* 缓存模块
*/
private String cacheModule;
/**
* 缓存过期时间
*/
private long expireSecond;
/**
* 缓存加载方法
*/
private Method loadMethod;
}

View File

@ -1,94 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.key;
import com.alibaba.fastjson.JSON;
/**
* [ ]
*
* @author 罗伊
* @date 2020/9/6 17:20
*/
public class CacheKey {
/**
* 构建缓存key信息
*
* @param cacheModule
* @param group
* @param businessId
* @return
*/
public static String cacheKey(String cacheModule, String group, String businessId) {
CacheKeyBuilder cacheKeyBuilder = CacheKeyBuilder.builder()
.cacheModule(cacheModule)
.group(group)
.businessId(businessId).build();
return cacheKey(cacheKeyBuilder);
}
public static String cacheKeyGroup(String cacheModule, String group) {
CacheKeyBuilder cacheKeyBuilder = CacheKeyBuilder.builder()
.cacheModule(cacheModule)
.group(group).build();
return cacheKey(cacheKeyBuilder);
}
/**
* 构建缓存key信息
*
* @param cacheModule
* @param businessId
* @return
*/
public static String cacheKey(String cacheModule, String businessId) {
CacheKeyBuilder cacheKeyBuilder = CacheKeyBuilder.builder()
.cacheModule(cacheModule)
.businessId(businessId).build();
return cacheKey(cacheKeyBuilder);
}
public static String cacheKey(String cacheModule) {
CacheKeyBuilder cacheKeyBuilder = CacheKeyBuilder.builder()
.cacheModule(cacheModule).build();
return cacheKey(cacheKeyBuilder);
}
/**
* 构建缓存key信息
*
* @param cacheKeyBuilder
* @return
*/
public static String cacheKey(CacheKeyBuilder cacheKeyBuilder) {
return JSON.toJSONString(cacheKeyBuilder);
}
/**
* 通过缓存key获取对应的method key
*
* @param cacheKey
* @return
*/
public static String getCacheModeByCacheKey(String cacheKey) {
CacheKeyBuilder cacheKeyBuilder = JSON.parseObject(cacheKey, CacheKeyBuilder.class);
return cacheKeyBuilder.getCacheModule();
}
/**
* 通过缓存key获取对应的businessId
*
* @param cacheKey
* @return
*/
public static String getBusinessIdByCacheKey(String cacheKey) {
CacheKeyBuilder cacheKeyBuilder = JSON.parseObject(cacheKey, CacheKeyBuilder.class);
return cacheKeyBuilder.getBusinessId();
}
public static String getGroupIdByCacheKey(String cacheKey) {
CacheKeyBuilder cacheKeyBuilder = JSON.parseObject(cacheKey, CacheKeyBuilder.class);
return cacheKeyBuilder.getGroup();
}
}

View File

@ -1,25 +0,0 @@
package net.lab1024.smartadmin.service.module.support.beancache.key;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* [ ]
*
* @author 罗伊
* @date 2021/1/23 17:41
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CacheKeyBuilder {
private String cacheModule;
private String group;
private String businessId;
}

View File

@ -2,11 +2,11 @@ package net.lab1024.smartadmin.service.module.system.department;
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.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.anno.CacheLoad;
import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentTreeVO; import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentTreeVO;
import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentVO; import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentVO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -17,7 +17,7 @@ import java.util.List;
*/ */
@Slf4j @Slf4j
@Service @Service
public class DepartmentCacheService { public class DepartmentCacheManager {
@Autowired @Autowired
private DepartmentDao departmentDao; private DepartmentDao departmentDao;
@ -25,27 +25,29 @@ public class DepartmentCacheService {
@Autowired @Autowired
private DepartmentTreeService departmentTreeService; private DepartmentTreeService departmentTreeService;
/** /**
* 缓存部门结构 * 清除自身以及下级的id列表缓存
* cacheKey = CacheKeyConst.DEPARTMENT_TREE_CACHE
* 无businessId
*
* @return
*/ */
@CacheLoad(CacheModuleConst.Department.DEPARTMENT_CACHE) @CacheEvict(CacheModuleConst.Department.DEPARTMENT_SELF_CHILDREN_ID_CACHE)
public List<DepartmentVO> departmentCache() { public void clearSelfAndChildrenIdCache() {
List<DepartmentVO> departmentVOList = departmentDao.listAll(); log.info("clear DEPARTMENT_SELF_CHILDREN_ID_CACHE");
return departmentVOList; }
/**
* 清除树结构缓存
*/
@CacheEvict(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE)
public void clearTreeCache() {
log.info("clear DEPARTMENT_TREE_CACHE");
} }
/** /**
* 缓存部门树结构 * 缓存部门树结构
* cacheKey = CacheKeyConst.DEPARTMENT_TREE_CACHE
* 无businessId
* *
* @return * @return
*/ */
@CacheLoad(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE) @Cacheable(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE)
public List<DepartmentTreeVO> departmentTreeCache() { public List<DepartmentTreeVO> departmentTreeCache() {
List<DepartmentVO> departmentVOList = departmentDao.listAll(); List<DepartmentVO> departmentVOList = departmentDao.listAll();
List<DepartmentTreeVO> treeList = departmentTreeService.buildTree(departmentVOList); List<DepartmentTreeVO> treeList = departmentTreeService.buildTree(departmentVOList);
@ -54,16 +56,12 @@ public class DepartmentCacheService {
/** /**
* 缓存某个部门的下级id列表 * 缓存某个部门的下级id列表
* cacheKey = CacheKeyConst.DEPARTMENT_TREE_ID_CACHE
* businessId = departmentId
* *
* @param cacheKey * @param departmentId
* @return * @return
*/ */
@CacheLoad(CacheModuleConst.Department.DEPARTMENT_TREE_ID_CACHE) @Cacheable(CacheModuleConst.Department.DEPARTMENT_SELF_CHILDREN_ID_CACHE)
public List<Long> departmentTreeCache(String cacheKey) { public List<Long> departmentSelfAndChildrenIdCache(Long departmentId) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
Long departmentId = Long.valueOf(businessId);
List<DepartmentVO> departmentVOList = departmentDao.listAll(); List<DepartmentVO> departmentVOList = departmentDao.listAll();
List<Long> idList = departmentTreeService.selfAndChildrenIdList(departmentId, departmentVOList); List<Long> idList = departmentTreeService.selfAndChildrenIdList(departmentId, departmentVOList);
return idList; return idList;

View File

@ -1,11 +1,7 @@
package net.lab1024.smartadmin.service.module.system.department; package net.lab1024.smartadmin.service.module.system.department;
import net.lab1024.smartadmin.service.common.code.UserErrorCode; import net.lab1024.smartadmin.service.common.code.UserErrorCode;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
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.support.beancache.cache.IBeanCache;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.system.department.domain.dto.DepartmentCreateDTO; import net.lab1024.smartadmin.service.module.system.department.domain.dto.DepartmentCreateDTO;
import net.lab1024.smartadmin.service.module.system.department.domain.dto.DepartmentUpdateDTO; import net.lab1024.smartadmin.service.module.system.department.domain.dto.DepartmentUpdateDTO;
import net.lab1024.smartadmin.service.module.system.department.domain.entity.DepartmentEntity; import net.lab1024.smartadmin.service.module.system.department.domain.entity.DepartmentEntity;
@ -14,7 +10,6 @@ import net.lab1024.smartadmin.service.module.system.department.domain.vo.Departm
import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentVO; import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentVO;
import net.lab1024.smartadmin.service.module.system.employee.EmployeeDao; import net.lab1024.smartadmin.service.module.system.employee.EmployeeDao;
import net.lab1024.smartadmin.service.module.system.employee.domain.dto.EmployeeDTO; import net.lab1024.smartadmin.service.module.system.employee.domain.dto.EmployeeDTO;
import net.lab1024.smartadmin.service.module.system.employee.domain.entity.EmployeeEntity;
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;
import org.apache.commons.compress.utils.Lists; import org.apache.commons.compress.utils.Lists;
@ -46,7 +41,7 @@ public class DepartmentService {
private DepartmentTreeService departmentTreeService; private DepartmentTreeService departmentTreeService;
@Autowired @Autowired
protected IBeanCache beanCache; protected DepartmentCacheManager departmentCacheManager;
/** /**
* 获取部门树形结构 * 获取部门树形结构
@ -54,8 +49,7 @@ public class DepartmentService {
* @return * @return
*/ */
public ResponseDTO<List<DepartmentTreeVO>> departmentTree() { public ResponseDTO<List<DepartmentTreeVO>> departmentTree() {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE); List<DepartmentTreeVO> treeVOList = departmentCacheManager.departmentTreeCache();
List<DepartmentTreeVO> treeVOList = beanCache.get(cacheKey);
return ResponseDTO.ok(treeVOList); return ResponseDTO.ok(treeVOList);
} }
@ -66,8 +60,7 @@ public class DepartmentService {
* @return * @return
*/ */
public List<Long> selfAndChildrenIdList(Long departmentId) { public List<Long> selfAndChildrenIdList(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_ID_CACHE, departmentId.toString()); return departmentCacheManager.departmentSelfAndChildrenIdCache(departmentId);
return beanCache.get(cacheKey);
} }
/** /**
@ -76,8 +69,7 @@ public class DepartmentService {
* @return * @return
*/ */
public ResponseDTO<List<DepartmentEmployeeTreeVO>> departmentEmployeeTree() { public ResponseDTO<List<DepartmentEmployeeTreeVO>> departmentEmployeeTree() {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE); List<DepartmentTreeVO> treeVOList = departmentCacheManager.departmentTreeCache();
List<DepartmentTreeVO> treeVOList = beanCache.get(cacheKey);
if (CollectionUtils.isEmpty(treeVOList)) { if (CollectionUtils.isEmpty(treeVOList)) {
return ResponseDTO.ok(Lists.newArrayList()); return ResponseDTO.ok(Lists.newArrayList());
} }
@ -192,8 +184,8 @@ public class DepartmentService {
departmentEntity.setSort(0L); departmentEntity.setSort(0L);
DepartmentService departmentService = (DepartmentService) AopContext.currentProxy(); DepartmentService departmentService = (DepartmentService) AopContext.currentProxy();
departmentService.addDepartmentHandle(departmentEntity); departmentService.addDepartmentHandle(departmentEntity);
this.clearTreeCache(); departmentCacheManager.clearTreeCache();
this.clearSelfAndChildrenIdCache(); departmentCacheManager.clearSelfAndChildrenIdCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -228,7 +220,7 @@ public class DepartmentService {
DepartmentEntity departmentEntity = SmartBeanUtil.copy(updateDTO, DepartmentEntity.class); DepartmentEntity departmentEntity = SmartBeanUtil.copy(updateDTO, DepartmentEntity.class);
departmentEntity.setSort(entity.getSort()); departmentEntity.setSort(entity.getSort());
departmentDao.updateById(departmentEntity); departmentDao.updateById(departmentEntity);
this.clearTreeCache(); departmentCacheManager.clearTreeCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -258,8 +250,8 @@ public class DepartmentService {
} }
departmentDao.deleteById(deptId); departmentDao.deleteById(deptId);
// 清除缓存 // 清除缓存
this.clearTreeCache(); departmentCacheManager.clearTreeCache();
this.clearSelfAndChildrenIdCache(); departmentCacheManager.clearSelfAndChildrenIdCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -314,7 +306,7 @@ public class DepartmentService {
DepartmentService departmentService = (DepartmentService) AopContext.currentProxy(); DepartmentService departmentService = (DepartmentService) AopContext.currentProxy();
departmentService.upOrDownUpdate(updateEntity, swapEntity); departmentService.upOrDownUpdate(updateEntity, swapEntity);
//清除缓存 //清除缓存
this.clearTreeCache(); departmentCacheManager.clearTreeCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -351,7 +343,7 @@ public class DepartmentService {
updateEntity.setParentId(parentEntity.getParentId()); updateEntity.setParentId(parentEntity.getParentId());
departmentDao.updateById(updateEntity); departmentDao.updateById(updateEntity);
//清除缓存 //清除缓存
this.clearTreeCache(); departmentCacheManager.clearTreeCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -376,37 +368,11 @@ public class DepartmentService {
updateEntity.setParentId(preEntity.getId()); updateEntity.setParentId(preEntity.getId());
departmentDao.updateById(updateEntity); departmentDao.updateById(updateEntity);
//清除缓存 //清除缓存
this.clearTreeCache(); departmentCacheManager.clearTreeCache();
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
/**
* 清除自身以及下级的id列表缓存
*/
private void clearSelfAndChildrenIdCache() {
beanCache.removeByModule(CacheModuleConst.Department.DEPARTMENT_TREE_ID_CACHE);
}
/**
* 清除树结构缓存
*/
private void clearTreeCache() {
String treeCacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE);
beanCache.remove(treeCacheKey);
}
/**
* 获取最顶级部门id
*
* @return
*/
public Long queryTopDeptId() {
ResponseDTO<List<DepartmentTreeVO>> departmentTree = departmentTree();
List<DepartmentTreeVO> data = departmentTree.getData();
return data.get(0).getId();
}
/** /**
* 获取校区列表 即第二级部门列表 * 获取校区列表 即第二级部门列表
* *
@ -429,94 +395,5 @@ public class DepartmentService {
return ResponseDTO.ok(resList); return ResponseDTO.ok(resList);
} }
/**
* 根据部门ID获取父级名称
*
* @param departmentId
* @return
*/
public String getParentNameTreeByDepartmentId(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_CACHE);
List<DepartmentVO> departmentList = beanCache.get(cacheKey);
//递归寻找上级直到校区(第二级)
List<String> departmentNameList = Lists.newArrayList();
this.recursionFindParentDepartmentName(departmentNameList, departmentList, departmentId);
return StringUtils.join(departmentNameList, "/");
}
/**
* 递归查询父级部门名称 到校区第二级
*
* @param departmentNameList
* @param departmentList
* @param departmentId
*/
private void recursionFindParentDepartmentName(List<String> departmentNameList, List<DepartmentVO> departmentList, Long departmentId) {
Optional<DepartmentVO> findRes = departmentList.stream().filter(e -> e.getId().equals(departmentId)).findFirst();
if (!findRes.isPresent() || findRes.get().getParentId() == DepartmentConst.DEFAULT_PARENT_ID) {
return;
}
DepartmentVO departmentVO = findRes.get();
departmentNameList.add(0, departmentVO.getName());
this.recursionFindParentDepartmentName(departmentNameList, departmentList, departmentVO.getParentId());
}
/**
* 寻找员工所属校区
*
* @param employeeId
* @return
*/
public Long getSchoolIdByEmployeeId(Long employeeId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
EmployeeEntity employeeEntity = beanCache.get(cacheKey);
Long departmentId = employeeEntity.getDepartmentId();
DepartmentVO schoolIdByDepartment = this.getSchoolIdByDepartment(departmentId);
if (schoolIdByDepartment != null) {
return schoolIdByDepartment.getId();
}
return null;
}
/**
* 寻找部门所属校区
*
* @param departmentId
* @return
*/
public DepartmentVO getSchoolIdByDepartment(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_CACHE);
List<DepartmentVO> departmentList = beanCache.get(cacheKey);
//递归寻找校区(第二级)
return this.recursionFindSchoolDepartmentId(departmentList, departmentId);
}
/**
* 寻找校区ID
*
* @param departmentList
* @param departmentId
* @return
*/
private DepartmentVO recursionFindSchoolDepartmentId(List<DepartmentVO> departmentList, Long departmentId) {
Optional<DepartmentVO> findRes = departmentList.stream().filter(e -> e.getId().equals(departmentId)).findFirst();
// 如果查询不到 或者自己本身为最顶级 返回null
if (!findRes.isPresent() || findRes.get().getParentId() == DepartmentConst.DEFAULT_PARENT_ID) {
return null;
}
DepartmentVO departmentVO = findRes.get();
// 寻找父级 判断父级的父级是不是最顶级
Optional<DepartmentVO> parentFindRes = departmentList.stream().filter(e -> e.getId().equals(departmentVO.getParentId())).findFirst();
// 若找不到父级则返回null
if (!parentFindRes.isPresent()) {
return null;
}
// 若父级为最顶级 则返回本级ID
if (parentFindRes.get().getParentId() == DepartmentConst.DEFAULT_PARENT_ID) {
return departmentVO;
}
// 若父级不为最顶级 进入递归
return this.recursionFindSchoolDepartmentId(departmentList, departmentVO.getParentId());
}
} }

View File

@ -2,13 +2,13 @@ package net.lab1024.smartadmin.service.module.system.employee;
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.module.support.beancache.key.CacheKey;
import net.lab1024.smartadmin.service.module.support.beancache.anno.CacheLoad;
import net.lab1024.smartadmin.service.module.system.employee.domain.entity.EmployeeEntity; import net.lab1024.smartadmin.service.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.smartadmin.service.module.system.role.roleemployee.RoleEmployeeDao; import net.lab1024.smartadmin.service.module.system.role.roleemployee.RoleEmployeeDao;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists; import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -19,25 +19,37 @@ import java.util.List;
*/ */
@Slf4j @Slf4j
@Service @Service
public class EmployeeCacheService { public class EmployeeCacheManager {
@Autowired @Autowired
private EmployeeDao employeeDao; private EmployeeDao employeeDao;
@Autowired @Autowired
private RoleEmployeeDao roleEmployeeDao; private RoleEmployeeDao roleEmployeeDao;
/**
* 清除businessId为员工id的缓存信息
*
* @param employeeId
*/
@CacheEvict({CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE})
public void clearCacheByEmployeeId(Long employeeId) {
log.info("clear SINGLE_EMPLOYEE_CACHE and SINGLE_EMPLOYEE_ROLE_CACHE {}",employeeId);
}
@CacheEvict({CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE})
public void clearCacheByDepartmentId(Long departmentId) {
log.info("clear DEPARTMENT_EMPLOYEE_CACHE {}",departmentId);
}
/** /**
* 缓存某个部门下的员工 * 缓存某个部门下的员工
* cacheKey = CacheKeyConst.Employee
* businessId = departmentId
* *
* @param cacheKey * @param departmentId
* @return * @return
*/ */
@CacheLoad(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE) @Cacheable(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE)
public List<EmployeeEntity> departmentEmployeeCache(String cacheKey) { public List<EmployeeEntity> departmentEmployeeCache(Long departmentId) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
Long departmentId = Long.valueOf(businessId);
List<EmployeeEntity> employeeEntityList = employeeDao.selectByDepartmentId(departmentId, false, false); List<EmployeeEntity> employeeEntityList = employeeDao.selectByDepartmentId(departmentId, false, false);
return employeeEntityList; return employeeEntityList;
} }
@ -45,13 +57,11 @@ public class EmployeeCacheService {
/** /**
* 单个员工的缓存 * 单个员工的缓存
* *
* @param cacheKey * @param employeeId
* @return * @return
*/ */
@CacheLoad(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE) @Cacheable(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE)
public EmployeeEntity singleEmployeeCache(String cacheKey) { public EmployeeEntity singleEmployeeCache(Long employeeId) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
Long employeeId = Long.valueOf(businessId);
EmployeeEntity employeeEntity = employeeDao.selectById(employeeId); EmployeeEntity employeeEntity = employeeDao.selectById(employeeId);
if (null == employeeEntity) { if (null == employeeEntity) {
return null; return null;
@ -63,15 +73,13 @@ public class EmployeeCacheService {
} }
/** /**
* 单个员工的缓存 * 单个员工的角色缓存
* *
* @param cacheKey * @param employeeId
* @return * @return
*/ */
@CacheLoad(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE) @Cacheable(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE)
public List<Long> singleEmployeeRoleCache(String cacheKey) { public List<Long> singleEmployeeRoleCache(Long employeeId) {
String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey);
Long employeeId = Long.valueOf(businessId);
List<Long> roleIdList = roleEmployeeDao.selectRoleIdByEmployeeId(employeeId); List<Long> roleIdList = roleEmployeeDao.selectRoleIdByEmployeeId(employeeId);
if (CollectionUtils.isEmpty(roleIdList)) { if (CollectionUtils.isEmpty(roleIdList)) {
return Lists.newArrayList(); return Lists.newArrayList();

View File

@ -109,10 +109,5 @@ public class EmployeeController extends SystemBaseController {
return employeeService.resetPassword(employeeId); return employeeService.resetPassword(employeeId);
} }
@ApiOperation(value = "查询员工-根据校区id", notes = "@author 善逸")
@GetMapping("/employee/query/school/{deptId}")
public ResponseDTO<List<EmployeeVO>> listBySchoolId(@PathVariable Long deptId) {
return employeeService.getEmployeeBySchoolId(deptId);
}
} }

View File

@ -3,14 +3,12 @@ package net.lab1024.smartadmin.service.module.system.employee;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists; 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.CacheModuleConst;
import net.lab1024.smartadmin.service.common.domain.PageResultDTO; import net.lab1024.smartadmin.service.common.domain.PageResultDTO;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO; import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
import net.lab1024.smartadmin.service.module.support.beancache.cache.IBeanCache; import net.lab1024.smartadmin.service.common.util.SmartBeanUtil;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey; import net.lab1024.smartadmin.service.common.util.SmartPageUtil;
import net.lab1024.smartadmin.service.module.system.department.DepartmentDao; import net.lab1024.smartadmin.service.module.system.department.DepartmentDao;
import net.lab1024.smartadmin.service.module.system.department.domain.entity.DepartmentEntity; import net.lab1024.smartadmin.service.module.system.department.domain.entity.DepartmentEntity;
import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentVO;
import net.lab1024.smartadmin.service.module.system.employee.domain.dto.*; import net.lab1024.smartadmin.service.module.system.employee.domain.dto.*;
import net.lab1024.smartadmin.service.module.system.employee.domain.entity.EmployeeEntity; import net.lab1024.smartadmin.service.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.smartadmin.service.module.system.employee.domain.vo.EmployeeVO; import net.lab1024.smartadmin.service.module.system.employee.domain.vo.EmployeeVO;
@ -19,8 +17,6 @@ import net.lab1024.smartadmin.service.module.system.login.domain.EmployeeLoginIn
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.role.roleemployee.RoleEmployeeDao; import net.lab1024.smartadmin.service.module.system.role.roleemployee.RoleEmployeeDao;
import net.lab1024.smartadmin.service.module.system.role.roleemployee.domain.RoleEmployeeEntity; import net.lab1024.smartadmin.service.module.system.role.roleemployee.domain.RoleEmployeeEntity;
import net.lab1024.smartadmin.service.common.util.SmartBeanUtil;
import net.lab1024.smartadmin.service.common.util.SmartPageUtil;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -58,7 +54,7 @@ public class EmployeeService {
private RoleEmployeeDao roleEmployeeDao; private RoleEmployeeDao roleEmployeeDao;
@Autowired @Autowired
protected IBeanCache beanCache; private EmployeeCacheManager employeeCacheManager;
/** /**
* 获取员工登录信息 * 获取员工登录信息
@ -67,11 +63,8 @@ public class EmployeeService {
* @return * @return
*/ */
public EmployeeLoginInfoDTO getById(Long employeeId) { public EmployeeLoginInfoDTO getById(Long employeeId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString()); EmployeeEntity employeeEntity = employeeCacheManager.singleEmployeeCache(employeeId);
EmployeeEntity employeeEntity = beanCache.get(cacheKey); List<Long> roleIdList = employeeCacheManager.singleEmployeeRoleCache(employeeId);
//获取员工角色缓存
String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
List<Long> roleIdList = beanCache.get(roleCacheKey);
if (employeeEntity != null) { if (employeeEntity != null) {
Boolean isSuperman = menuEmployeeService.isSuperman(employeeId); Boolean isSuperman = menuEmployeeService.isSuperman(employeeId);
EmployeeLoginInfoDTO loginDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginInfoDTO.class); EmployeeLoginInfoDTO loginDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginInfoDTO.class);
@ -90,11 +83,8 @@ public class EmployeeService {
* @return * @return
*/ */
public EmployeeLoginBO getBoById(Long employeeId) { public EmployeeLoginBO getBoById(Long employeeId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString()); EmployeeEntity employeeEntity = employeeCacheManager.singleEmployeeCache(employeeId);
EmployeeEntity employeeEntity = beanCache.get(cacheKey); List<Long> roleIdList = employeeCacheManager.singleEmployeeRoleCache(employeeId);
//获取员工角色缓存
String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
List<Long> roleIdList = beanCache.get(roleCacheKey);
if (employeeEntity != null) { if (employeeEntity != null) {
Boolean isSuperman = menuEmployeeService.isSuperman(employeeId); Boolean isSuperman = menuEmployeeService.isSuperman(employeeId);
EmployeeLoginBO loginDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginBO.class); EmployeeLoginBO loginDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginBO.class);
@ -112,7 +102,7 @@ public class EmployeeService {
* @param queryDTO * @param queryDTO
* @return * @return
*/ */
public ResponseDTO<PageResultDTO<EmployeeVO>> queryEmployeeList(EmployeeQueryForm queryDTO) { public ResponseDTO<PageResultDTO<EmployeeVO>> queryEmployeeList(EmployeeQueryForm queryDTO) {
queryDTO.setDeletedFlag(false); queryDTO.setDeletedFlag(false);
Page pageParam = SmartPageUtil.convert2PageQuery(queryDTO); Page pageParam = SmartPageUtil.convert2PageQuery(queryDTO);
List<EmployeeVO> employeeList = employeeDao.queryEmployee(pageParam, queryDTO); List<EmployeeVO> employeeList = employeeDao.queryEmployee(pageParam, queryDTO);
@ -163,7 +153,7 @@ public class EmployeeService {
// 保存数据 // 保存数据
employeeManager.saveEmployee(entity, addDTO.getRoleIdList()); employeeManager.saveEmployee(entity, addDTO.getRoleIdList());
this.clearCacheByDepartmentId(departmentId); employeeCacheManager.clearCacheByDepartmentId(departmentId);
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -202,8 +192,8 @@ public class EmployeeService {
employeeManager.updateEmployee(entity, updateDTO.getRoleIdList()); employeeManager.updateEmployee(entity, updateDTO.getRoleIdList());
// 清除缓存 // 清除缓存
this.clearCacheByEmployeeId(employeeId); employeeCacheManager.clearCacheByEmployeeId(employeeId);
this.clearCacheByDepartmentId(departmentId); employeeCacheManager.clearCacheByDepartmentId(departmentId);
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -225,8 +215,8 @@ public class EmployeeService {
employeeDao.updateDisableFlag(employeeId, !employeeEntity.getDisabledFlag()); employeeDao.updateDisableFlag(employeeId, !employeeEntity.getDisabledFlag());
this.clearCacheByEmployeeId(employeeId); employeeCacheManager.clearCacheByEmployeeId(employeeId);
this.clearCacheByDepartmentId(employeeEntity.getDepartmentId()); employeeCacheManager.clearCacheByDepartmentId(employeeEntity.getDepartmentId());
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -247,8 +237,8 @@ public class EmployeeService {
// 清除缓存 // 清除缓存
employeeEntityList.forEach(e -> { employeeEntityList.forEach(e -> {
this.clearCacheByEmployeeId(e.getId()); employeeCacheManager.clearCacheByEmployeeId(e.getId());
this.clearCacheByDepartmentId(e.getDepartmentId()); employeeCacheManager.clearCacheByDepartmentId(e.getDepartmentId());
}); });
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -278,8 +268,8 @@ public class EmployeeService {
// 清除缓存 // 清除缓存
employeeEntityList.forEach(e -> { employeeEntityList.forEach(e -> {
this.clearCacheByEmployeeId(e.getId()); employeeCacheManager.clearCacheByEmployeeId(e.getId());
this.clearCacheByDepartmentId(e.getDepartmentId()); employeeCacheManager.clearCacheByDepartmentId(e.getDepartmentId());
}); });
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -301,8 +291,8 @@ public class EmployeeService {
updateEmployee.setDeletedFlag(true); updateEmployee.setDeletedFlag(true);
employeeDao.updateById(updateEmployee); employeeDao.updateById(updateEmployee);
this.clearCacheByEmployeeId(employeeId); employeeCacheManager.clearCacheByEmployeeId(employeeId);
this.clearCacheByDepartmentId(employeeEntity.getDepartmentId()); employeeCacheManager.clearCacheByDepartmentId(employeeEntity.getDepartmentId());
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -330,8 +320,8 @@ public class EmployeeService {
// 清除缓存 // 清除缓存
employeeEntityList.forEach(e -> { employeeEntityList.forEach(e -> {
this.clearCacheByEmployeeId(e.getId()); employeeCacheManager.clearCacheByEmployeeId(e.getId());
this.clearCacheByDepartmentId(e.getDepartmentId()); employeeCacheManager.clearCacheByDepartmentId(e.getDepartmentId());
}); });
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -399,8 +389,7 @@ public class EmployeeService {
* @return * @return
*/ */
public ResponseDTO<List<EmployeeVO>> getEmployeeByDeptId(Long departmentId) { public ResponseDTO<List<EmployeeVO>> getEmployeeByDeptId(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString()); List<EmployeeEntity> employeeEntityList = employeeCacheManager.departmentEmployeeCache(departmentId);
List<EmployeeEntity> employeeEntityList = beanCache.get(cacheKey);
if (CollectionUtils.isEmpty(employeeEntityList)) { if (CollectionUtils.isEmpty(employeeEntityList)) {
return ResponseDTO.ok(Collections.emptyList()); return ResponseDTO.ok(Collections.emptyList());
} }
@ -408,44 +397,6 @@ public class EmployeeService {
return ResponseDTO.ok(voList); return ResponseDTO.ok(voList);
} }
/**
* 获取某个校区的员工信息
*
* @param departmentId
* @return
*/
public ResponseDTO<List<EmployeeVO>> getEmployeeBySchoolId(Long departmentId) {
// 查询部门下所有部门包含子部门
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_CACHE);
List<DepartmentVO> departmentList = beanCache.get(cacheKey);
// 先查询本部门的员工
ResponseDTO<List<EmployeeVO>> employeeByDeptId = getEmployeeByDeptId(departmentId);
List<EmployeeVO> schoolEmployeeList = employeeByDeptId.getData();
// 进入递归
List<EmployeeVO> employeeList = Lists.newArrayList(schoolEmployeeList);
recursionFindEmployee(employeeList, departmentList, departmentId);
return ResponseDTO.ok(employeeList);
}
/**
* 递归查询员工
*
* @param employeeList
* @param departmentList
*/
private void recursionFindEmployee(List<EmployeeVO> employeeList, List<DepartmentVO> departmentList, Long parentId) {
// 寻找子集部门
List<DepartmentVO> childDepartmentList = departmentList.stream().filter(e -> e.getParentId().equals(parentId)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(childDepartmentList)) {
return;
}
for (DepartmentVO departmentVO : childDepartmentList) {
// 查询本级部门下包含的员工
ResponseDTO<List<EmployeeVO>> employeeByDeptId = getEmployeeByDeptId(departmentVO.getId());
employeeList.addAll(employeeByDeptId.getData());
recursionFindEmployee(employeeList, departmentList, departmentVO.getId());
}
}
/** /**
* 重置密码 * 重置密码
@ -459,28 +410,6 @@ public class EmployeeService {
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
/**
* 清除businessId为员工id的缓存信息
*
* @param employeeId
*/
public void clearCacheByEmployeeId(Long employeeId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString());
beanCache.remove(cacheKey);
String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
beanCache.remove(roleCacheKey);
}
/**
* 清除businessId为部门id的缓存信息
*
* @param departmentId
*/
private void clearCacheByDepartmentId(Long departmentId) {
String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString());
beanCache.remove(cacheKey);
}
/** /**
* 获取 加密后 的密码 * 获取 加密后 的密码
* *

View File

@ -4,12 +4,11 @@ import lombok.extern.slf4j.Slf4j;
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.common.util.SmartBeanUtil;
import net.lab1024.smartadmin.service.module.support.captcha.CaptchaService; import net.lab1024.smartadmin.service.module.support.captcha.CaptchaService;
import net.lab1024.smartadmin.service.module.support.captcha.domain.CaptchaForm; import net.lab1024.smartadmin.service.module.support.captcha.domain.CaptchaForm;
import net.lab1024.smartadmin.service.module.system.department.DepartmentDao; import net.lab1024.smartadmin.service.module.system.department.DepartmentDao;
import net.lab1024.smartadmin.service.module.system.department.DepartmentService;
import net.lab1024.smartadmin.service.module.system.department.domain.entity.DepartmentEntity; import net.lab1024.smartadmin.service.module.system.department.domain.entity.DepartmentEntity;
import net.lab1024.smartadmin.service.module.system.department.domain.vo.DepartmentVO;
import net.lab1024.smartadmin.service.module.system.employee.EmployeeDao; import net.lab1024.smartadmin.service.module.system.employee.EmployeeDao;
import net.lab1024.smartadmin.service.module.system.employee.EmployeeService; import net.lab1024.smartadmin.service.module.system.employee.EmployeeService;
import net.lab1024.smartadmin.service.module.system.employee.domain.dto.EmployeeLoginDTO; import net.lab1024.smartadmin.service.module.system.employee.domain.dto.EmployeeLoginDTO;
@ -18,7 +17,6 @@ import net.lab1024.smartadmin.service.module.system.login.domain.EmployeeLoginIn
import net.lab1024.smartadmin.service.module.system.login.domain.EmployeeLoginVO; import net.lab1024.smartadmin.service.module.system.login.domain.EmployeeLoginVO;
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.MenuLoginBO; import net.lab1024.smartadmin.service.module.system.menu.domain.MenuLoginBO;
import net.lab1024.smartadmin.service.common.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -37,9 +35,6 @@ public class EmployeeLoginService {
@Autowired @Autowired
private DepartmentDao departmentDao; private DepartmentDao departmentDao;
@Autowired
private DepartmentService departmentService;
@Autowired @Autowired
private EmployeeLoginTokenService employeeLoginTokenService; private EmployeeLoginTokenService employeeLoginTokenService;
@ -91,10 +86,6 @@ public class EmployeeLoginService {
// 查询部门 // 查询部门
DepartmentEntity deptEntity = departmentDao.selectById(employeeEntity.getDepartmentId()); DepartmentEntity deptEntity = departmentDao.selectById(employeeEntity.getDepartmentId());
String deptName = null == deptEntity ? StringConst.EMPTY_STR : deptEntity.getName(); String deptName = null == deptEntity ? StringConst.EMPTY_STR : deptEntity.getName();
// 查询所在校区
DepartmentVO schoolIdByDepartment = departmentService.getSchoolIdByDepartment(employeeEntity.getDepartmentId());
// 返回登录结果 // 返回登录结果
EmployeeLoginVO loginResultDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginVO.class); EmployeeLoginVO loginResultDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginVO.class);
loginResultDTO.setEmployeeId(employeeEntity.getId()); loginResultDTO.setEmployeeId(employeeEntity.getId());
@ -103,10 +94,6 @@ public class EmployeeLoginService {
loginResultDTO.setDepartmentName(deptName); loginResultDTO.setDepartmentName(deptName);
loginResultDTO.setToken(token); loginResultDTO.setToken(token);
loginResultDTO.setIsSuperMan(isSuperman); loginResultDTO.setIsSuperMan(isSuperman);
if (schoolIdByDepartment != null) {
loginResultDTO.setSchoolId(schoolIdByDepartment.getId());
loginResultDTO.setSchoolName(schoolIdByDepartment.getName());
}
return ResponseDTO.ok(loginResultDTO); return ResponseDTO.ok(loginResultDTO);
} }
@ -117,7 +104,6 @@ public class EmployeeLoginService {
* @return * @return
*/ */
public ResponseDTO<String> logoutByToken(Long employeeId) { public ResponseDTO<String> logoutByToken(Long employeeId) {
employeeService.clearCacheByEmployeeId(employeeId);
return ResponseDTO.ok(); return ResponseDTO.ok();
} }

View File

@ -2,13 +2,13 @@ package net.lab1024.smartadmin.service.module.system.role.roleemployee;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.smartadmin.service.common.code.UserErrorCode; import net.lab1024.smartadmin.service.common.code.UserErrorCode;
import net.lab1024.smartadmin.service.common.constant.CacheModuleConst;
import net.lab1024.smartadmin.service.common.domain.PageResultDTO; import net.lab1024.smartadmin.service.common.domain.PageResultDTO;
import net.lab1024.smartadmin.service.common.domain.ResponseDTO; import net.lab1024.smartadmin.service.common.domain.ResponseDTO;
import net.lab1024.smartadmin.service.module.support.beancache.cache.IBeanCache; import net.lab1024.smartadmin.service.common.util.SmartBeanUtil;
import net.lab1024.smartadmin.service.module.support.beancache.key.CacheKey; import net.lab1024.smartadmin.service.common.util.SmartPageUtil;
import net.lab1024.smartadmin.service.module.system.department.DepartmentDao; import net.lab1024.smartadmin.service.module.system.department.DepartmentDao;
import net.lab1024.smartadmin.service.module.system.department.domain.entity.DepartmentEntity; import net.lab1024.smartadmin.service.module.system.department.domain.entity.DepartmentEntity;
import net.lab1024.smartadmin.service.module.system.employee.EmployeeCacheManager;
import net.lab1024.smartadmin.service.module.system.employee.domain.dto.EmployeeDTO; import net.lab1024.smartadmin.service.module.system.employee.domain.dto.EmployeeDTO;
import net.lab1024.smartadmin.service.module.system.employee.domain.vo.EmployeeVO; import net.lab1024.smartadmin.service.module.system.employee.domain.vo.EmployeeVO;
import net.lab1024.smartadmin.service.module.system.role.basic.RoleDao; import net.lab1024.smartadmin.service.module.system.role.basic.RoleDao;
@ -17,8 +17,6 @@ import net.lab1024.smartadmin.service.module.system.role.basic.domain.entity.Rol
import net.lab1024.smartadmin.service.module.system.role.basic.domain.vo.RoleSelectedVO; import net.lab1024.smartadmin.service.module.system.role.basic.domain.vo.RoleSelectedVO;
import net.lab1024.smartadmin.service.module.system.role.roleemployee.domain.RoleEmployeeBatchDTO; import net.lab1024.smartadmin.service.module.system.role.roleemployee.domain.RoleEmployeeBatchDTO;
import net.lab1024.smartadmin.service.module.system.role.roleemployee.domain.RoleEmployeeEntity; import net.lab1024.smartadmin.service.module.system.role.roleemployee.domain.RoleEmployeeEntity;
import net.lab1024.smartadmin.service.common.util.SmartBeanUtil;
import net.lab1024.smartadmin.service.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -49,7 +47,7 @@ public class RoleEmployeeService {
private RoleEmployeeManager roleEmployeeManager; private RoleEmployeeManager roleEmployeeManager;
@Autowired @Autowired
protected IBeanCache beanCache; protected EmployeeCacheManager employeeCacheManager;
/** /**
* 通过角色id分页获取成员员工列表 * 通过角色id分页获取成员员工列表
@ -87,7 +85,7 @@ public class RoleEmployeeService {
return ResponseDTO.error(UserErrorCode.PARAM_ERROR); return ResponseDTO.error(UserErrorCode.PARAM_ERROR);
} }
roleEmployeeDao.deleteByEmployeeIdRoleId(employeeId, roleId); roleEmployeeDao.deleteByEmployeeIdRoleId(employeeId, roleId);
this.clearCacheByEmployeeId(employeeId); employeeCacheManager.clearCacheByEmployeeId(employeeId);
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -100,7 +98,7 @@ public class RoleEmployeeService {
public ResponseDTO<String> batchRemoveEmployeeRole(RoleEmployeeBatchDTO removeDTO) { public ResponseDTO<String> batchRemoveEmployeeRole(RoleEmployeeBatchDTO removeDTO) {
roleEmployeeDao.batchDeleteEmployeeRole(removeDTO.getRoleId(), removeDTO.getEmployeeIdList()); roleEmployeeDao.batchDeleteEmployeeRole(removeDTO.getRoleId(), removeDTO.getEmployeeIdList());
for (Long employeeId : removeDTO.getEmployeeIdList()) { for (Long employeeId : removeDTO.getEmployeeIdList()) {
this.clearCacheByEmployeeId(employeeId); employeeCacheManager.clearCacheByEmployeeId(employeeId);
} }
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -118,13 +116,13 @@ public class RoleEmployeeService {
List<RoleEmployeeEntity> roleEmployeeList = null; List<RoleEmployeeEntity> roleEmployeeList = null;
if (CollectionUtils.isNotEmpty(employeeIdList)) { if (CollectionUtils.isNotEmpty(employeeIdList)) {
roleEmployeeList = employeeIdList.stream() roleEmployeeList = employeeIdList.stream()
.map(employeeId -> new RoleEmployeeEntity(roleId, employeeId)) .map(employeeId -> new RoleEmployeeEntity(roleId, employeeId))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
// 保存数据 // 保存数据
roleEmployeeManager.saveRoleEmployee(roleId, roleEmployeeList); roleEmployeeManager.saveRoleEmployee(roleId, roleEmployeeList);
for (Long employeeId : employeeIdList) { for (Long employeeId : employeeIdList) {
this.clearCacheByEmployeeId(employeeId); employeeCacheManager.clearCacheByEmployeeId(employeeId);
} }
return ResponseDTO.ok(); return ResponseDTO.ok();
} }
@ -142,14 +140,4 @@ public class RoleEmployeeService {
result.stream().forEach(item -> item.setSelected(roleIds.contains(item.getId()))); result.stream().forEach(item -> item.setSelected(roleIds.contains(item.getId())));
return ResponseDTO.ok(result); return ResponseDTO.ok(result);
} }
/**
* 清除businessId为员工id的缓存信息
*
* @param employeeId
*/
public void clearCacheByEmployeeId(Long employeeId) {
String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString());
beanCache.remove(roleCacheKey);
}
} }

View File

@ -22,9 +22,9 @@ spring.jackson.time-zone=GMT+8
spring.jackson.serialization.write-dates-as-timestamps=false spring.jackson.serialization.write-dates-as-timestamps=false
######################### database ######################### ######################### database #########################
spring.datasource.url=jdbc:p6spy:mysql://115.29.150.222:11024/smart_admin_v2?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai spring.datasource.url=jdbc:p6spy:mysql://127.0.0.1:3306/smart_admin_v2?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=11024Lab spring.datasource.password=root
spring.datasource.initial-size=2 spring.datasource.initial-size=2
spring.datasource.min-idle=1 spring.datasource.min-idle=1
spring.datasource.max-active=10 spring.datasource.max-active=10
@ -40,14 +40,14 @@ spring.datasource.druid.service.scanner=net.lab1024.smartadmin.module..*Service.
######################### redis ####################################### ######################### redis #######################################
spring.redis.database=1 spring.redis.database=1
spring.redis.host=115.29.150.222 spring.redis.host=127.0.0.1
spring.redis.lettuce.pool.max-active=100 spring.redis.lettuce.pool.max-active=100
spring.redis.lettuce.pool.min-idle=5 spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10 spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-wait=30000ms spring.redis.lettuce.pool.max-wait=30000ms
spring.redis.port=21024 spring.redis.port=1234
spring.redis.timeout=10000ms spring.redis.timeout=10000ms
spring.redis.password=21024Lab spring.redis.password=1234
######################### swagger ######################### ######################### swagger #########################
swagger.apiGroupName=smartAdmin swagger.apiGroupName=smartAdmin
@ -87,6 +87,5 @@ access-control-allow-origin=*
heart-beat.intervalTime=300000 heart-beat.intervalTime=300000
######################### cache config ######################### ######################### cache config #########################
cache.maximumSize=5000 spring.cache.type=caffeine
cache.expireDays=10

View File

@ -9,9 +9,19 @@
WHERE WHERE
parent_id IN parent_id IN
<foreach collection="parentIdList" open="(" separator="," close=")" item="id">#{id}</foreach> <foreach collection="parentIdList" open="(" separator="," close=")" item="id">#{id}</foreach>
<if test="categoryType != null"> AND deleted_flag = #{deletedFlag}
AND category_type = #{categoryType} ORDER BY sort ASC
</if> </select>
<!-- 根据父级id 查询子类 -->
<select id="queryByParentIdAndType"
resultType="net.lab1024.smartadmin.service.module.business.category.domain.CategoryEntity">
SELECT * FROM t_category
WHERE
parent_id IN
<foreach collection="parentIdList" open="(" separator="," close=")" item="id">#{id}</foreach>
AND category_type = #{categoryType}
AND deleted_flag = #{deletedFlag} AND deleted_flag = #{deletedFlag}
ORDER BY sort ASC ORDER BY sort ASC
</select> </select>