diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/common/constant/CacheModuleConst.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/common/constant/CacheModuleConst.java index 8d5e2c5a..e8251469 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/common/constant/CacheModuleConst.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/common/constant/CacheModuleConst.java @@ -25,10 +25,6 @@ public class CacheModuleConst { public static class Department { - /** - * 部门树 - */ - public static final String DEPARTMENT_CACHE = "department_cache"; /** * 部门树 @@ -38,7 +34,7 @@ public class CacheModuleConst { /** * 某个部门以及下级的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"; } diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/config/CacheConfig.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/config/CacheConfig.java deleted file mode 100644 index 502a03a0..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/config/CacheConfig.java +++ /dev/null @@ -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 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; - } - }; - } -} - diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryCacheManager.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryCacheManager.java new file mode 100644 index 00000000..a746698a --- /dev/null +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryCacheManager.java @@ -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 querySubCategory(Long categoryId) { + return categoryDao.queryByParentId(Lists.newArrayList(categoryId), false); + } + + + /** + * 查询类目 层级树 + * 优先查询缓存 + * + * @return + */ + @Cacheable(CacheModuleConst.Category.CATEGORY_TREE) + public List queryCategoryTree(Long parentId, Integer categoryType) { + List allCategoryEntityList = categoryDao.queryByParentIdAndType(Lists.newArrayList(NumberUtils.LONG_ZERO), categoryType, false); + + List categoryEntityList = allCategoryEntityList.stream().filter(e -> e.getParentId().equals(parentId)).collect(Collectors.toList()); + List 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 treeList, List allCategoryEntityList) { + if (CollectionUtils.isEmpty(treeList)) { + return; + } + List parentIdList = treeList.stream().map(CategoryTreeVO::getValue).collect(Collectors.toList()); + List categoryEntityList = allCategoryEntityList.stream().filter(e -> parentIdList.contains(e.getParentId())).collect(Collectors.toList()); + Map> categorySubMap = categoryEntityList.stream().collect(Collectors.groupingBy(CategoryEntity::getCategoryId)); + treeList.forEach(e -> { + List childrenEntityList = categorySubMap.getOrDefault(e.getValue(), Lists.newArrayList()); + List 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); + }); + } + + +} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryDao.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryDao.java index 9fdd7464..80be6223 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryDao.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryDao.java @@ -23,11 +23,21 @@ public interface CategoryDao extends BaseMapper { * 根据父级id 类型 查询子类 * * @param parentIdList - * @param categoryType {@link CategoryTypeEnum} * @param deletedFlag * @return */ List queryByParentId(@Param("parentIdList") List parentIdList, + @Param("deletedFlag") Boolean deletedFlag); + + /** + * 根据父级id 类型 查询子类 + * + * @param parentIdList + * @param categoryType {@link CategoryTypeEnum} + * @param deletedFlag + * @return + */ + List queryByParentIdAndType(@Param("parentIdList") List parentIdList, @Param("categoryType") Integer categoryType, @Param("deletedFlag") Boolean deletedFlag); diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryQueryService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryQueryService.java index 9bbc736e..08f49461 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryQueryService.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryQueryService.java @@ -2,20 +2,8 @@ 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.module.business.category.constant.CategoryConst; 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.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -37,61 +25,8 @@ public class CategoryQueryService { private CategoryDao categoryDao; @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 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> querySubCategoryFromCache(List 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 查询未删除的类目 @@ -103,27 +38,13 @@ public class CategoryQueryService { if (null == categoryId) { return Optional.empty(); } - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString()); - CategoryEntity entity = cache.get(cacheKey); + CategoryEntity entity = categoryCacheManager.queryCategory(categoryId); if (null == entity || entity.getDeletedFlag()) { return Optional.empty(); } return Optional.of(entity); } - /** - * 根据 类目id 查询未删除的子类 - * - * @param categoryId - * @return 没有返回空集合 - */ - public List 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集合 查询未删除的类目集合 @@ -138,20 +59,11 @@ public class CategoryQueryService { categoryIdList = categoryIdList.stream().distinct().collect(Collectors.toList()); return categoryIdList.stream().collect(Collectors.toMap(Function.identity(), e -> { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, e.toString()); - return cache.get(cacheKey); + CategoryEntity categoryEntity = categoryCacheManager.queryCategory(e); + 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 递归查询 @@ -164,11 +76,12 @@ public class CategoryQueryService { if (CollectionUtils.isEmpty(categoryIdList)) { return Collections.emptyList(); } - // 查询所有子类 - Map> subTypeMap = this.querySubCategoryFromCache(categoryIdList); - if (MapUtils.isEmpty(subTypeMap)) { - return Lists.newArrayList(); - } + //所有子类 + List categoryEntityList = Lists.newArrayList(); + categoryIdList.forEach(e -> { + categoryEntityList.addAll(categoryCacheManager.querySubCategory(e)); + }); + Map> subTypeMap = categoryEntityList.stream().collect(Collectors.groupingBy(CategoryEntity::getCategoryId)); // 递归查询子类 categoryIdList = subTypeMap.values().stream().flatMap(Collection::stream).map(CategoryEntity::getCategoryId).distinct().collect(Collectors.toList()); if (CollectionUtils.isEmpty(categoryIdList)) { @@ -178,123 +91,6 @@ public class CategoryQueryService { return categoryIdList; } - /** - * 查询自身以及所有子节点 - * @param categoryIdList - * @return - */ - public List queryCategorySelfAndSubId(List categoryIdList) { - List subIdList = this.queryCategorySubId(categoryIdList); - subIdList.addAll(categoryIdList); - return subIdList; - } - - /** - * 查询类目 层级树 - * 优先查询缓存 - * - * @return - */ - public List queryCategoryTree(CategoryTreeQueryForm queryForm) { - // 查询缓存 - Long parentId = queryForm.getParentId(); - Integer categoryType = queryForm.getCategoryType(); - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY_TREE, getCacheId(parentId, categoryType)); - List treeList = cache.get(cacheKey); - if (null != treeList) { - return treeList; - } - // 查询一级类目 - List 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 treeList) { - if (CollectionUtils.isEmpty(treeList)) { - return; - } - List parentIdList = treeList.stream().map(CategoryTreeVO::getValue).collect(Collectors.toList()); - Map> categorySubMap = this.querySubCategoryFromCache(parentIdList); - treeList.forEach(e -> { - List childrenEntityList = categorySubMap.getOrDefault(e.getValue(), Lists.newArrayList()); - List 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 parentCategoryList = this.queryCategoryAndParent(categoryId); - // 拼接父级类目名称 斜杠分隔返回 - List 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 queryCategoryAndParent(Long categoryId) { - List 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 */ public String queryCategoryName(Long categoryId) { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Category.CATEGORY, categoryId.toString()); - CategoryEntity categoryEntity = cache.get(cacheKey); + CategoryEntity categoryEntity = categoryCacheManager.queryCategory(categoryId); if (null == categoryEntity || categoryEntity.getDeletedFlag()) { return null; } diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryService.java index 28f70750..43a75ef5 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryService.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/CategoryService.java @@ -3,10 +3,10 @@ package net.lab1024.smartadmin.service.module.business.category; import com.google.common.collect.Lists; import net.lab1024.smartadmin.service.common.code.UserErrorCode; 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.common.util.SmartBeanUtil; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.math.NumberUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -29,6 +29,9 @@ public class CategoryService { @Autowired private CategoryQueryService categoryQueryService; + @Autowired + private CategoryCacheManager categoryCacheManager; + /** * 添加类目 * @@ -43,7 +46,7 @@ public class CategoryService { 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.setSort(null == addForm.getSort() ? 0 : addForm.getSort()); categoryEntity.setDeletedFlag(false); @@ -52,7 +55,7 @@ public class CategoryService { categoryDao.insert(categoryEntity); // 更新缓存 - categoryQueryService.removeCache(); + categoryCacheManager.removeCache(); return ResponseDTO.ok(); } @@ -87,7 +90,7 @@ public class CategoryService { categoryDao.updateById(categoryEntity); // 更新缓存 - categoryQueryService.removeCache(); + categoryCacheManager.removeCache(); return ResponseDTO.ok(); } @@ -106,7 +109,7 @@ public class CategoryService { if (Objects.equals(categoryEntity.getCategoryId(), parentId)) { return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "父级类目怎么和自己相同了"); } - if (!Objects.equals(parentId, CategoryConst.DEFAULT_PARENT_ID)) { + if (!Objects.equals(parentId, NumberUtils.LONG_ZERO)) { Optional optional = categoryQueryService.queryCategory(parentId); if (!optional.isPresent()) { return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST, "父级类目不存在~"); @@ -120,7 +123,7 @@ public class CategoryService { } else { // 如果没有父类 使用默认父类 - parentId = CategoryConst.DEFAULT_PARENT_ID; + parentId = NumberUtils.LONG_ZERO; } // 校验同父类下 名称是否重复 @@ -169,9 +172,9 @@ public class CategoryService { if (null == queryForm.getCategoryType()) { return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "类目类型不能为空"); } - queryForm.setParentId(CategoryConst.DEFAULT_PARENT_ID); + queryForm.setParentId(NumberUtils.LONG_ZERO); } - List treeList = categoryQueryService.queryCategoryTree(queryForm); + List treeList = categoryCacheManager.queryCategoryTree(queryForm.getParentId(),queryForm.getCategoryType()); return ResponseDTO.ok(treeList); } @@ -200,7 +203,7 @@ public class CategoryService { categoryDao.updateById(categoryEntity); // 更新缓存 - categoryQueryService.removeCache(); + categoryCacheManager.removeCache(); return ResponseDTO.ok(); } diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/constant/CategoryConst.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/constant/CategoryConst.java deleted file mode 100644 index f63ff0a4..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/business/category/constant/CategoryConst.java +++ /dev/null @@ -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; -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/anno/CacheLoad.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/anno/CacheLoad.java deleted file mode 100644 index c3101a82..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/anno/CacheLoad.java +++ /dev/null @@ -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; - -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/AbstractCaffeineCache.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/AbstractCaffeineCache.java deleted file mode 100644 index c8f1ac76..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/AbstractCaffeineCache.java +++ /dev/null @@ -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 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 get(String key) { - LoadingCache 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 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 keySet() { - ConcurrentMap concurrentMap = getCache().asMap(); - return concurrentMap.keySet(); - } - - /** - * 移除某个模块的所有key - * @param module - */ - @Override - public void removeByModule(String module) { - ConcurrentMap concurrentMap = getCache().asMap(); - List removeKeys = Lists.newArrayList(); - Set 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 removeKeys = Lists.newArrayList(); - Set 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 initCache(Integer expireDays, Integer maximumSize) { - - //构建缓存对象 - Caffeine 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; - }); - } -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/AbstractDisableCache.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/AbstractDisableCache.java deleted file mode 100644 index 0146cfc3..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/AbstractDisableCache.java +++ /dev/null @@ -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 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 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 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; - } -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/CacheLoadMethodRegister.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/CacheLoadMethodRegister.java deleted file mode 100644 index 7b110cd3..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/CacheLoadMethodRegister.java +++ /dev/null @@ -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 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); - } - -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/IBeanCache.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/IBeanCache.java deleted file mode 100644 index 13213421..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/cache/IBeanCache.java +++ /dev/null @@ -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 getCache(); - - /** - * 移除某个缓存 - * - * @param key - */ - void remove(String key); - - /** - * 清除所有 - */ - void clear(); - - /** - * 获取某个缓存对象 - * - * @param key - * @return - */ - 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 keySet(); - - /** - * 移除某个模块的所有key - * - * @param module - */ - void removeByModule(String module); - - /** - * 移除某个模块 某个分组下的缓存 - * - * @param module - * @param group - */ - void removeByModuleAndGroup(String module, String group); - -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/domain/CacheData.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/domain/CacheData.java deleted file mode 100644 index 205fcc5a..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/domain/CacheData.java +++ /dev/null @@ -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 { - - /** - * 过期时间 - */ - private Long expireTime; - - /** - * 缓存数据 - */ - private T data; -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/domain/CacheLoadMethod.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/domain/CacheLoadMethod.java deleted file mode 100644 index bb4e88c3..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/domain/CacheLoadMethod.java +++ /dev/null @@ -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; -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/key/CacheKey.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/key/CacheKey.java deleted file mode 100644 index 93dd05e0..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/key/CacheKey.java +++ /dev/null @@ -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(); - } - -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/key/CacheKeyBuilder.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/key/CacheKeyBuilder.java deleted file mode 100644 index c186af4b..00000000 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/support/beancache/key/CacheKeyBuilder.java +++ /dev/null @@ -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; -} diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentCacheService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentCacheManager.java similarity index 55% rename from admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentCacheService.java rename to admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentCacheManager.java index de342e17..82a97923 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentCacheService.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentCacheManager.java @@ -2,11 +2,11 @@ package net.lab1024.smartadmin.service.module.system.department; import lombok.extern.slf4j.Slf4j; 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.DepartmentVO; 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; @@ -17,7 +17,7 @@ import java.util.List; */ @Slf4j @Service -public class DepartmentCacheService { +public class DepartmentCacheManager { @Autowired private DepartmentDao departmentDao; @@ -25,27 +25,29 @@ public class DepartmentCacheService { @Autowired private DepartmentTreeService departmentTreeService; + /** - * 缓存部门结构 - * cacheKey = CacheKeyConst.DEPARTMENT_TREE_CACHE - * 无businessId - * - * @return + * 清除自身以及下级的id列表缓存 */ - @CacheLoad(CacheModuleConst.Department.DEPARTMENT_CACHE) - public List departmentCache() { - List departmentVOList = departmentDao.listAll(); - return departmentVOList; + @CacheEvict(CacheModuleConst.Department.DEPARTMENT_SELF_CHILDREN_ID_CACHE) + public void clearSelfAndChildrenIdCache() { + log.info("clear DEPARTMENT_SELF_CHILDREN_ID_CACHE"); + } + + /** + * 清除树结构缓存 + */ + @CacheEvict(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE) + public void clearTreeCache() { + log.info("clear DEPARTMENT_TREE_CACHE"); } /** * 缓存部门树结构 - * cacheKey = CacheKeyConst.DEPARTMENT_TREE_CACHE - * 无businessId * * @return */ - @CacheLoad(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE) + @Cacheable(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE) public List departmentTreeCache() { List departmentVOList = departmentDao.listAll(); List treeList = departmentTreeService.buildTree(departmentVOList); @@ -54,16 +56,12 @@ public class DepartmentCacheService { /** * 缓存某个部门的下级id列表 - * cacheKey = CacheKeyConst.DEPARTMENT_TREE_ID_CACHE - * businessId = departmentId * - * @param cacheKey + * @param departmentId * @return */ - @CacheLoad(CacheModuleConst.Department.DEPARTMENT_TREE_ID_CACHE) - public List departmentTreeCache(String cacheKey) { - String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey); - Long departmentId = Long.valueOf(businessId); + @Cacheable(CacheModuleConst.Department.DEPARTMENT_SELF_CHILDREN_ID_CACHE) + public List departmentSelfAndChildrenIdCache(Long departmentId) { List departmentVOList = departmentDao.listAll(); List idList = departmentTreeService.selfAndChildrenIdList(departmentId, departmentVOList); return idList; diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentService.java index 1415cb73..d6abc0a0 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentService.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/department/DepartmentService.java @@ -1,11 +1,7 @@ package net.lab1024.smartadmin.service.module.system.department; 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.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.DepartmentUpdateDTO; 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.employee.EmployeeDao; 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 org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.utils.Lists; @@ -46,7 +41,7 @@ public class DepartmentService { private DepartmentTreeService departmentTreeService; @Autowired - protected IBeanCache beanCache; + protected DepartmentCacheManager departmentCacheManager; /** * 获取部门树形结构 @@ -54,8 +49,7 @@ public class DepartmentService { * @return */ public ResponseDTO> departmentTree() { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE); - List treeVOList = beanCache.get(cacheKey); + List treeVOList = departmentCacheManager.departmentTreeCache(); return ResponseDTO.ok(treeVOList); } @@ -66,8 +60,7 @@ public class DepartmentService { * @return */ public List selfAndChildrenIdList(Long departmentId) { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_ID_CACHE, departmentId.toString()); - return beanCache.get(cacheKey); + return departmentCacheManager.departmentSelfAndChildrenIdCache(departmentId); } /** @@ -76,8 +69,7 @@ public class DepartmentService { * @return */ public ResponseDTO> departmentEmployeeTree() { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_TREE_CACHE); - List treeVOList = beanCache.get(cacheKey); + List treeVOList = departmentCacheManager.departmentTreeCache(); if (CollectionUtils.isEmpty(treeVOList)) { return ResponseDTO.ok(Lists.newArrayList()); } @@ -192,8 +184,8 @@ public class DepartmentService { departmentEntity.setSort(0L); DepartmentService departmentService = (DepartmentService) AopContext.currentProxy(); departmentService.addDepartmentHandle(departmentEntity); - this.clearTreeCache(); - this.clearSelfAndChildrenIdCache(); + departmentCacheManager.clearTreeCache(); + departmentCacheManager.clearSelfAndChildrenIdCache(); return ResponseDTO.ok(); } @@ -228,7 +220,7 @@ public class DepartmentService { DepartmentEntity departmentEntity = SmartBeanUtil.copy(updateDTO, DepartmentEntity.class); departmentEntity.setSort(entity.getSort()); departmentDao.updateById(departmentEntity); - this.clearTreeCache(); + departmentCacheManager.clearTreeCache(); return ResponseDTO.ok(); } @@ -258,8 +250,8 @@ public class DepartmentService { } departmentDao.deleteById(deptId); // 清除缓存 - this.clearTreeCache(); - this.clearSelfAndChildrenIdCache(); + departmentCacheManager.clearTreeCache(); + departmentCacheManager.clearSelfAndChildrenIdCache(); return ResponseDTO.ok(); } @@ -314,7 +306,7 @@ public class DepartmentService { DepartmentService departmentService = (DepartmentService) AopContext.currentProxy(); departmentService.upOrDownUpdate(updateEntity, swapEntity); //清除缓存 - this.clearTreeCache(); + departmentCacheManager.clearTreeCache(); return ResponseDTO.ok(); } @@ -351,7 +343,7 @@ public class DepartmentService { updateEntity.setParentId(parentEntity.getParentId()); departmentDao.updateById(updateEntity); //清除缓存 - this.clearTreeCache(); + departmentCacheManager.clearTreeCache(); return ResponseDTO.ok(); } @@ -376,37 +368,11 @@ public class DepartmentService { updateEntity.setParentId(preEntity.getId()); departmentDao.updateById(updateEntity); //清除缓存 - this.clearTreeCache(); + departmentCacheManager.clearTreeCache(); 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> departmentTree = departmentTree(); - List data = departmentTree.getData(); - return data.get(0).getId(); - } - /** * 获取校区列表 即第二级部门列表 * @@ -429,94 +395,5 @@ public class DepartmentService { return ResponseDTO.ok(resList); } - /** - * 根据部门ID获取父级名称 - * - * @param departmentId - * @return - */ - public String getParentNameTreeByDepartmentId(Long departmentId) { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_CACHE); - List departmentList = beanCache.get(cacheKey); - //递归寻找上级直到校区(第二级) - List departmentNameList = Lists.newArrayList(); - this.recursionFindParentDepartmentName(departmentNameList, departmentList, departmentId); - return StringUtils.join(departmentNameList, "/"); - } - - /** - * 递归查询父级部门名称 到校区(第二级) - * - * @param departmentNameList - * @param departmentList - * @param departmentId - */ - private void recursionFindParentDepartmentName(List departmentNameList, List departmentList, Long departmentId) { - Optional 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 departmentList = beanCache.get(cacheKey); - //递归寻找校区(第二级) - return this.recursionFindSchoolDepartmentId(departmentList, departmentId); - } - - /** - * 寻找校区ID - * - * @param departmentList - * @param departmentId - * @return - */ - private DepartmentVO recursionFindSchoolDepartmentId(List departmentList, Long departmentId) { - Optional 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 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()); - } } diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeCacheService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeCacheManager.java similarity index 56% rename from admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeCacheService.java rename to admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeCacheManager.java index ac95aa18..7653daac 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeCacheService.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeCacheManager.java @@ -2,13 +2,13 @@ package net.lab1024.smartadmin.service.module.system.employee; import lombok.extern.slf4j.Slf4j; 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.role.roleemployee.RoleEmployeeDao; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.utils.Lists; 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; @@ -19,25 +19,37 @@ import java.util.List; */ @Slf4j @Service -public class EmployeeCacheService { +public class EmployeeCacheManager { @Autowired private EmployeeDao employeeDao; @Autowired 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 */ - @CacheLoad(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE) - public List departmentEmployeeCache(String cacheKey) { - String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey); - Long departmentId = Long.valueOf(businessId); + @Cacheable(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE) + public List departmentEmployeeCache(Long departmentId) { List employeeEntityList = employeeDao.selectByDepartmentId(departmentId, false, false); return employeeEntityList; } @@ -45,13 +57,11 @@ public class EmployeeCacheService { /** * 单个员工的缓存 * - * @param cacheKey + * @param employeeId * @return */ - @CacheLoad(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE) - public EmployeeEntity singleEmployeeCache(String cacheKey) { - String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey); - Long employeeId = Long.valueOf(businessId); + @Cacheable(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE) + public EmployeeEntity singleEmployeeCache(Long employeeId) { EmployeeEntity employeeEntity = employeeDao.selectById(employeeId); if (null == employeeEntity) { return null; @@ -63,15 +73,13 @@ public class EmployeeCacheService { } /** - * 单个员工的缓存 + * 单个员工的角色缓存 * - * @param cacheKey + * @param employeeId * @return */ - @CacheLoad(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE) - public List singleEmployeeRoleCache(String cacheKey) { - String businessId = CacheKey.getBusinessIdByCacheKey(cacheKey); - Long employeeId = Long.valueOf(businessId); + @Cacheable(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE) + public List singleEmployeeRoleCache(Long employeeId) { List roleIdList = roleEmployeeDao.selectRoleIdByEmployeeId(employeeId); if (CollectionUtils.isEmpty(roleIdList)) { return Lists.newArrayList(); diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeController.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeController.java index 9f2a25dd..d902345b 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeController.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeController.java @@ -109,10 +109,5 @@ public class EmployeeController extends SystemBaseController { return employeeService.resetPassword(employeeId); } - @ApiOperation(value = "查询员工-根据校区id", notes = "@author 善逸") - @GetMapping("/employee/query/school/{deptId}") - public ResponseDTO> listBySchoolId(@PathVariable Long deptId) { - return employeeService.getEmployeeBySchoolId(deptId); - } } diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeService.java index 5f82088a..121bf653 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeService.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/employee/EmployeeService.java @@ -3,14 +3,12 @@ package net.lab1024.smartadmin.service.module.system.employee; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; 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.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.common.util.SmartBeanUtil; +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.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.entity.EmployeeEntity; 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.role.roleemployee.RoleEmployeeDao; 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.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -58,7 +54,7 @@ public class EmployeeService { private RoleEmployeeDao roleEmployeeDao; @Autowired - protected IBeanCache beanCache; + private EmployeeCacheManager employeeCacheManager; /** * 获取员工登录信息 @@ -67,11 +63,8 @@ public class EmployeeService { * @return */ public EmployeeLoginInfoDTO getById(Long employeeId) { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString()); - EmployeeEntity employeeEntity = beanCache.get(cacheKey); - //获取员工角色缓存 - String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString()); - List roleIdList = beanCache.get(roleCacheKey); + EmployeeEntity employeeEntity = employeeCacheManager.singleEmployeeCache(employeeId); + List roleIdList = employeeCacheManager.singleEmployeeRoleCache(employeeId); if (employeeEntity != null) { Boolean isSuperman = menuEmployeeService.isSuperman(employeeId); EmployeeLoginInfoDTO loginDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginInfoDTO.class); @@ -90,11 +83,8 @@ public class EmployeeService { * @return */ public EmployeeLoginBO getBoById(Long employeeId) { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_CACHE, employeeId.toString()); - EmployeeEntity employeeEntity = beanCache.get(cacheKey); - //获取员工角色缓存 - String roleCacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.SINGLE_EMPLOYEE_ROLE_CACHE, employeeId.toString()); - List roleIdList = beanCache.get(roleCacheKey); + EmployeeEntity employeeEntity = employeeCacheManager.singleEmployeeCache(employeeId); + List roleIdList = employeeCacheManager.singleEmployeeRoleCache(employeeId); if (employeeEntity != null) { Boolean isSuperman = menuEmployeeService.isSuperman(employeeId); EmployeeLoginBO loginDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginBO.class); @@ -112,7 +102,7 @@ public class EmployeeService { * @param queryDTO * @return */ - public ResponseDTO> queryEmployeeList(EmployeeQueryForm queryDTO) { + public ResponseDTO> queryEmployeeList(EmployeeQueryForm queryDTO) { queryDTO.setDeletedFlag(false); Page pageParam = SmartPageUtil.convert2PageQuery(queryDTO); List employeeList = employeeDao.queryEmployee(pageParam, queryDTO); @@ -163,7 +153,7 @@ public class EmployeeService { // 保存数据 employeeManager.saveEmployee(entity, addDTO.getRoleIdList()); - this.clearCacheByDepartmentId(departmentId); + employeeCacheManager.clearCacheByDepartmentId(departmentId); return ResponseDTO.ok(); } @@ -202,8 +192,8 @@ public class EmployeeService { employeeManager.updateEmployee(entity, updateDTO.getRoleIdList()); // 清除缓存 - this.clearCacheByEmployeeId(employeeId); - this.clearCacheByDepartmentId(departmentId); + employeeCacheManager.clearCacheByEmployeeId(employeeId); + employeeCacheManager.clearCacheByDepartmentId(departmentId); return ResponseDTO.ok(); } @@ -225,8 +215,8 @@ public class EmployeeService { employeeDao.updateDisableFlag(employeeId, !employeeEntity.getDisabledFlag()); - this.clearCacheByEmployeeId(employeeId); - this.clearCacheByDepartmentId(employeeEntity.getDepartmentId()); + employeeCacheManager.clearCacheByEmployeeId(employeeId); + employeeCacheManager.clearCacheByDepartmentId(employeeEntity.getDepartmentId()); return ResponseDTO.ok(); } @@ -247,8 +237,8 @@ public class EmployeeService { // 清除缓存 employeeEntityList.forEach(e -> { - this.clearCacheByEmployeeId(e.getId()); - this.clearCacheByDepartmentId(e.getDepartmentId()); + employeeCacheManager.clearCacheByEmployeeId(e.getId()); + employeeCacheManager.clearCacheByDepartmentId(e.getDepartmentId()); }); return ResponseDTO.ok(); } @@ -278,8 +268,8 @@ public class EmployeeService { // 清除缓存 employeeEntityList.forEach(e -> { - this.clearCacheByEmployeeId(e.getId()); - this.clearCacheByDepartmentId(e.getDepartmentId()); + employeeCacheManager.clearCacheByEmployeeId(e.getId()); + employeeCacheManager.clearCacheByDepartmentId(e.getDepartmentId()); }); return ResponseDTO.ok(); } @@ -301,8 +291,8 @@ public class EmployeeService { updateEmployee.setDeletedFlag(true); employeeDao.updateById(updateEmployee); - this.clearCacheByEmployeeId(employeeId); - this.clearCacheByDepartmentId(employeeEntity.getDepartmentId()); + employeeCacheManager.clearCacheByEmployeeId(employeeId); + employeeCacheManager.clearCacheByDepartmentId(employeeEntity.getDepartmentId()); return ResponseDTO.ok(); } @@ -330,8 +320,8 @@ public class EmployeeService { // 清除缓存 employeeEntityList.forEach(e -> { - this.clearCacheByEmployeeId(e.getId()); - this.clearCacheByDepartmentId(e.getDepartmentId()); + employeeCacheManager.clearCacheByEmployeeId(e.getId()); + employeeCacheManager.clearCacheByDepartmentId(e.getDepartmentId()); }); return ResponseDTO.ok(); } @@ -399,8 +389,7 @@ public class EmployeeService { * @return */ public ResponseDTO> getEmployeeByDeptId(Long departmentId) { - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Employee.DEPARTMENT_EMPLOYEE_CACHE, departmentId.toString()); - List employeeEntityList = beanCache.get(cacheKey); + List employeeEntityList = employeeCacheManager.departmentEmployeeCache(departmentId); if (CollectionUtils.isEmpty(employeeEntityList)) { return ResponseDTO.ok(Collections.emptyList()); } @@ -408,44 +397,6 @@ public class EmployeeService { return ResponseDTO.ok(voList); } - /** - * 获取某个校区的员工信息 - * - * @param departmentId - * @return - */ - public ResponseDTO> getEmployeeBySchoolId(Long departmentId) { - // 查询部门下所有部门包含子部门 - String cacheKey = CacheKey.cacheKey(CacheModuleConst.Department.DEPARTMENT_CACHE); - List departmentList = beanCache.get(cacheKey); - // 先查询本部门的员工 - ResponseDTO> employeeByDeptId = getEmployeeByDeptId(departmentId); - List schoolEmployeeList = employeeByDeptId.getData(); - // 进入递归 - List employeeList = Lists.newArrayList(schoolEmployeeList); - recursionFindEmployee(employeeList, departmentList, departmentId); - return ResponseDTO.ok(employeeList); - } - - /** - * 递归查询员工 - * - * @param employeeList - * @param departmentList - */ - private void recursionFindEmployee(List employeeList, List departmentList, Long parentId) { - // 寻找子集部门 - List childDepartmentList = departmentList.stream().filter(e -> e.getParentId().equals(parentId)).collect(Collectors.toList()); - if (CollectionUtils.isEmpty(childDepartmentList)) { - return; - } - for (DepartmentVO departmentVO : childDepartmentList) { - // 查询本级部门下包含的员工 - ResponseDTO> employeeByDeptId = getEmployeeByDeptId(departmentVO.getId()); - employeeList.addAll(employeeByDeptId.getData()); - recursionFindEmployee(employeeList, departmentList, departmentVO.getId()); - } - } /** * 重置密码 @@ -459,28 +410,6 @@ public class EmployeeService { 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); - } - /** * 获取 加密后 的密码 * diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/login/EmployeeLoginService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/login/EmployeeLoginService.java index d5a7bf45..01872516 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/login/EmployeeLoginService.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/login/EmployeeLoginService.java @@ -4,12 +4,11 @@ import lombok.extern.slf4j.Slf4j; import net.lab1024.smartadmin.service.common.code.UserErrorCode; import net.lab1024.smartadmin.service.common.constant.StringConst; 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.domain.CaptchaForm; 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.vo.DepartmentVO; 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.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.menu.MenuEmployeeService; 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.stereotype.Service; @@ -37,9 +35,6 @@ public class EmployeeLoginService { @Autowired private DepartmentDao departmentDao; - @Autowired - private DepartmentService departmentService; - @Autowired private EmployeeLoginTokenService employeeLoginTokenService; @@ -91,10 +86,6 @@ public class EmployeeLoginService { // 查询部门 DepartmentEntity deptEntity = departmentDao.selectById(employeeEntity.getDepartmentId()); String deptName = null == deptEntity ? StringConst.EMPTY_STR : deptEntity.getName(); - - // 查询所在校区 - DepartmentVO schoolIdByDepartment = departmentService.getSchoolIdByDepartment(employeeEntity.getDepartmentId()); - // 返回登录结果 EmployeeLoginVO loginResultDTO = SmartBeanUtil.copy(employeeEntity, EmployeeLoginVO.class); loginResultDTO.setEmployeeId(employeeEntity.getId()); @@ -103,10 +94,6 @@ public class EmployeeLoginService { loginResultDTO.setDepartmentName(deptName); loginResultDTO.setToken(token); loginResultDTO.setIsSuperMan(isSuperman); - if (schoolIdByDepartment != null) { - loginResultDTO.setSchoolId(schoolIdByDepartment.getId()); - loginResultDTO.setSchoolName(schoolIdByDepartment.getName()); - } return ResponseDTO.ok(loginResultDTO); } @@ -117,7 +104,6 @@ public class EmployeeLoginService { * @return */ public ResponseDTO logoutByToken(Long employeeId) { - employeeService.clearCacheByEmployeeId(employeeId); return ResponseDTO.ok(); } diff --git a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/role/roleemployee/RoleEmployeeService.java b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/role/roleemployee/RoleEmployeeService.java index 58352430..744fe401 100644 --- a/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/role/roleemployee/RoleEmployeeService.java +++ b/admin-api/java-api/src/main/java/net/lab1024/smartadmin/service/module/system/role/roleemployee/RoleEmployeeService.java @@ -2,13 +2,13 @@ package net.lab1024.smartadmin.service.module.system.role.roleemployee; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.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.common.util.SmartBeanUtil; +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.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.vo.EmployeeVO; 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.roleemployee.domain.RoleEmployeeBatchDTO; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -49,7 +47,7 @@ public class RoleEmployeeService { private RoleEmployeeManager roleEmployeeManager; @Autowired - protected IBeanCache beanCache; + protected EmployeeCacheManager employeeCacheManager; /** * 通过角色id,分页获取成员员工列表 @@ -87,7 +85,7 @@ public class RoleEmployeeService { return ResponseDTO.error(UserErrorCode.PARAM_ERROR); } roleEmployeeDao.deleteByEmployeeIdRoleId(employeeId, roleId); - this.clearCacheByEmployeeId(employeeId); + employeeCacheManager.clearCacheByEmployeeId(employeeId); return ResponseDTO.ok(); } @@ -100,7 +98,7 @@ public class RoleEmployeeService { public ResponseDTO batchRemoveEmployeeRole(RoleEmployeeBatchDTO removeDTO) { roleEmployeeDao.batchDeleteEmployeeRole(removeDTO.getRoleId(), removeDTO.getEmployeeIdList()); for (Long employeeId : removeDTO.getEmployeeIdList()) { - this.clearCacheByEmployeeId(employeeId); + employeeCacheManager.clearCacheByEmployeeId(employeeId); } return ResponseDTO.ok(); } @@ -118,13 +116,13 @@ public class RoleEmployeeService { List roleEmployeeList = null; if (CollectionUtils.isNotEmpty(employeeIdList)) { roleEmployeeList = employeeIdList.stream() - .map(employeeId -> new RoleEmployeeEntity(roleId, employeeId)) - .collect(Collectors.toList()); + .map(employeeId -> new RoleEmployeeEntity(roleId, employeeId)) + .collect(Collectors.toList()); } // 保存数据 roleEmployeeManager.saveRoleEmployee(roleId, roleEmployeeList); for (Long employeeId : employeeIdList) { - this.clearCacheByEmployeeId(employeeId); + employeeCacheManager.clearCacheByEmployeeId(employeeId); } return ResponseDTO.ok(); } @@ -142,14 +140,4 @@ public class RoleEmployeeService { result.stream().forEach(item -> item.setSelected(roleIds.contains(item.getId()))); 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); - } } diff --git a/admin-api/java-api/src/main/resources/dev/application.properties b/admin-api/java-api/src/main/resources/dev/application.properties index 7904d39c..ef7393d5 100644 --- a/admin-api/java-api/src/main/resources/dev/application.properties +++ b/admin-api/java-api/src/main/resources/dev/application.properties @@ -22,9 +22,9 @@ spring.jackson.time-zone=GMT+8 spring.jackson.serialization.write-dates-as-timestamps=false ######################### 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.password=11024Lab +spring.datasource.password=root spring.datasource.initial-size=2 spring.datasource.min-idle=1 spring.datasource.max-active=10 @@ -40,14 +40,14 @@ spring.datasource.druid.service.scanner=net.lab1024.smartadmin.module..*Service. ######################### redis ####################################### 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.min-idle=5 spring.redis.lettuce.pool.max-idle=10 spring.redis.lettuce.pool.max-wait=30000ms -spring.redis.port=21024 +spring.redis.port=1234 spring.redis.timeout=10000ms -spring.redis.password=21024Lab +spring.redis.password=1234 ######################### swagger ######################### swagger.apiGroupName=smartAdmin @@ -87,6 +87,5 @@ access-control-allow-origin=* heart-beat.intervalTime=300000 ######################### cache config ######################### -cache.maximumSize=5000 -cache.expireDays=10 +spring.cache.type=caffeine diff --git a/admin-api/java-api/src/main/resources/mapper/business/category/CategoryMapper.xml b/admin-api/java-api/src/main/resources/mapper/business/category/CategoryMapper.xml index c3c73f90..3207edad 100644 --- a/admin-api/java-api/src/main/resources/mapper/business/category/CategoryMapper.xml +++ b/admin-api/java-api/src/main/resources/mapper/business/category/CategoryMapper.xml @@ -9,9 +9,19 @@ WHERE parent_id IN #{id} - - AND category_type = #{categoryType} - + AND deleted_flag = #{deletedFlag} + ORDER BY sort ASC + + + + +