linwe
2024-05-29 c10d6358b9f014375a13821465bc978d0c0da22e
src/main/java/org/springblade/common/cache/SysCache.java
@@ -1,31 +1,19 @@
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.common.cache;
import org.apache.logging.log4j.util.Strings;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.modules.system.entity.*;
import org.springblade.modules.system.service.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.springblade.common.cache.CacheNames.ARTICLE_KEY;
import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
/**
@@ -42,6 +30,8 @@
   private static final String DEPT_NAMES_ID = "deptNames:id:";
   private static final String DEPT_CHILD_ID = "deptChild:id:";
   private static final String DEPT_CHILDIDS_ID = "deptChildIds:id:";
   private static final String REGION_CHILD_CODE = "regionChild:code:";
   private static final String REGION_CHILDCODES_CODE = "regionChildCodes:code:";
   private static final String POST_ID = "post:id:";
   private static final String POST_NAME = "post:name:";
   private static final String POST_NAME_FUZZY = "post:nameFuzzy:";
@@ -61,6 +51,7 @@
   private static final IDeptService deptService;
   private static final IPostService postService;
   private static final IRoleService roleService;
   private static final IRegionService regionService;
   private static final ITenantService tenantService;
   private static final ITenantPackageService tenantPackageService;
@@ -69,6 +60,7 @@
      deptService = SpringUtil.getBean(IDeptService.class);
      postService = SpringUtil.getBean(IPostService.class);
      roleService = SpringUtil.getBean(IRoleService.class);
      regionService = SpringUtil.getBean(IRegionService.class);
      tenantService = SpringUtil.getBean(ITenantService.class);
      tenantPackageService = SpringUtil.getBean(ITenantPackageService.class);
   }
@@ -149,25 +141,33 @@
   /**
    * 获取子部门ID集合
    *
    * @param deptId 主键
    * @param deptIds 主键
    * @return 子部门ID
    */
   public static List<Long> getDeptChildIds(Long deptId) {
      if (deptId == null) {
   public static List<Long> getDeptChildIds(String deptIds) {
      if (Strings.isBlank(deptIds)) {
         return null;
      }
      List<Long> deptIdList = CacheUtil.get(SYS_CACHE, DEPT_CHILDIDS_ID, deptId, List.class);
      if (deptIdList == null) {
         deptIdList = new ArrayList<>();
         List<Dept> deptChild = getDeptChild(deptId);
         if (deptChild != null) {
            List<Long> collect = deptChild.stream().map(Dept::getId).collect(Collectors.toList());
            deptIdList.addAll(collect);
      List<Long> list = new ArrayList<>();
      List<String> deptIdList = new ArrayList<>(Arrays.asList(deptIds.split(",")));
      for (String deptId : deptIdList) {
         List<Long> deptIdLists = CacheUtil.get(SYS_CACHE, DEPT_CHILDIDS_ID, deptId, List.class);
         if (deptIdLists == null) {
            deptIdLists = new ArrayList<>();
            List<Dept> deptChild = getDeptChild(Long.parseLong(deptId));
            if (deptChild != null) {
               List<Long> collect = deptChild.stream().map(Dept::getId).collect(Collectors.toList());
               deptIdLists.addAll(collect);
            }
            deptIdLists.add(Long.parseLong(deptId));
            CacheUtil.put(SYS_CACHE, DEPT_CHILDIDS_ID, deptId, deptIdLists);
         }
         deptIdList.add(deptId);
         CacheUtil.put(SYS_CACHE, DEPT_CHILDIDS_ID, deptId, deptIdList);
         list.addAll(deptIdLists);
      }
      return deptIdList;
      // 去重
      List<Long> collect = list.stream().distinct().collect(Collectors.toList());
      // 返回
      return collect;
   }
   /**
@@ -314,4 +314,207 @@
      return CacheUtil.get(SYS_CACHE, TENANT_PACKAGE_ID, tenantId, () -> tenantPackageService.getById(tenant.getPackageId()), Boolean.FALSE);
   }
   /**
    * 获取下级所有区域code
    *
    * @return regionCode
    */
   public static List<String> getRegionChildCodesByDeptId(String deptId,String communityCode) {
      //多个部门按逗号分割
      List<String> deptIdList = Arrays.asList(deptId.split(","));
      //所有行政区划code
      List<String> allRegionList = new ArrayList<>();
      deptIdList.forEach(id->{
         List<String> list = new ArrayList<>();
         // 查询对应的区域编号code
         Dept dept = deptService.getById(id);
         if (null!=dept && !Strings.isBlank(dept.getRegionCode()) && !AuthUtil.isAdministrator()){
            list = getRegionChildCodes(dept.getRegionCode(),communityCode);
            //行政区划不为空添加进集合
            if (list.size()>0){
               allRegionList.addAll(list);
            }
         }
      });
      // 去重
      List<String> collect = allRegionList.stream().distinct().collect(Collectors.toList());
      return collect;
   }
   /**
    * 获取民警(公安)下级所有区域code
    *
    * @return regionCode
    */
   public static List<String> getPoliceRegionChildCodesByDeptId(String deptId,String communityCode) {
      List<String> regionCodeList = new ArrayList<>();
      if (!Strings.isBlank(deptId)) {
         //多个部门按逗号分割
         List<String> deptIdList = Arrays.asList(deptId.split(","));
         //所有行政区划code
         List<String> allRegionList = new ArrayList<>();
         deptIdList.forEach(id -> {
            List<String> list = new ArrayList<>();
            // 查询对应的区域编号code
            Dept dept = deptService.getById(id);
            // 只取公安的
            if (dept.getDeptNature() == 1) {
               if (null != dept && !Strings.isBlank(dept.getRegionCode()) && !AuthUtil.isAdministrator()) {
                  list = getPoliceRegionChildCodes(dept.getRegionCode(),communityCode);
                  //行政区划不为空添加进集合
                  if (list.size() > 0) {
                     allRegionList.addAll(list);
                  }
               }
            }
         });
         // 去重
         regionCodeList = allRegionList.stream().distinct().collect(Collectors.toList());
      }
      return regionCodeList;
   }
   /**
    * 查询综治网格/公安相关的网格,社区编号集合
    * @param deptId
    * @param communityCode
    * @param roleName
    * @return
    */
   public static List<String> getGridRegionChildCodesByDeptId(String deptId, String communityCode,String roleName) {
      List<String> regionCodeList = new ArrayList<>();
      if (!Strings.isBlank(deptId)) {
         //多个部门按逗号分割
         List<String> deptIdList = Arrays.asList(deptId.split(","));
         List<String> finalRegionCodeList = regionCodeList;
         String key = AuthUtil.getUserId().toString();
         if (!Strings.isBlank(roleName)){
            key = key + ":" + roleName;
         }
         String finalKey = key;
         deptIdList.forEach(id -> {
            List<String> list = new ArrayList<>();
            // 查询对应的区域编号code
            Dept dept = deptService.getById(id);
            if (null != dept) {
               if (
                  (roleName.equals("mj") && dept.getDeptNature()==1) ||
                  (
                     (roleName.equals("wgy") || roleName.equals("wzcj")) && dept.getDeptNature()==2
                  )
               ){
                  if (!AuthUtil.isAdministrator()) {
                     if (dept.getRegionCode() == null) {
                        return;
                     }
                     list = CacheUtil.get(SYS_CACHE, REGION_CHILDCODES_CODE, finalKey, List.class);
                     if (list == null || list.size() == 0) {
                        list = new ArrayList<>();
                        List<Region> deptChild = getGridRegionChild(dept.getRegionCode(), communityCode, roleName);
                        if (deptChild != null) {
                           List<String> collect = deptChild.stream().map(Region::getCode).collect(Collectors.toList());
                           list.addAll(collect);
                        }
                     }
                     //行政区划不为空添加进集合
                     if (list.size() > 0) {
                        finalRegionCodeList.addAll(list);
                     }
                  }
               }
            }
         });
         // 去重
         regionCodeList = finalRegionCodeList.stream().distinct().collect(Collectors.toList());
         CacheUtil.put(SYS_CACHE, REGION_CHILDCODES_CODE, key, regionCodeList);
      }
      return regionCodeList;
   }
   /**
    * 获取下级所有区域code
    *
    * @return regionCode
    */
   public static List<String> getRegionChildCodes(String regionCode,String communityCode) {
      if (regionCode == null) {
         return null;
      }
      List<String> regionCodeList = CacheUtil.get(SYS_CACHE, REGION_CHILDCODES_CODE, regionCode, List.class);
      if (regionCodeList == null || regionCodeList.size()==0) {
         regionCodeList = new ArrayList<>();
         List<Region> deptChild = getRegionChild(regionCode,communityCode);
         if (deptChild != null) {
            List<String> collect = deptChild.stream().map(Region::getCode).collect(Collectors.toList());
            regionCodeList.addAll(collect);
         }
         regionCodeList.add(regionCode);
         CacheUtil.put(SYS_CACHE, REGION_CHILDCODES_CODE, regionCode, regionCodeList);
      }
      return regionCodeList;
   }
   /**
    * 获取公安下级所有区域code
    *
    * @return regionCode
    */
   public static List<String> getPoliceRegionChildCodes(String regionCode,String communityCode) {
      if (regionCode == null) {
         return null;
      }
      List<String> regionCodeList = CacheUtil.get(SYS_CACHE, REGION_CHILDCODES_CODE, regionCode, List.class);
      if (regionCodeList == null || regionCodeList.size()==0) {
         regionCodeList = new ArrayList<>();
         List<Region> deptChild = getPoliceRegionChild(regionCode,communityCode);
         if (deptChild != null) {
            List<String> collect = deptChild.stream().map(Region::getCode).collect(Collectors.toList());
            regionCodeList.addAll(collect);
         }
         regionCodeList.add(regionCode);
         CacheUtil.put(SYS_CACHE, REGION_CHILDCODES_CODE, regionCode, regionCodeList);
      }
      return regionCodeList;
   }
   /**
    * 获取综治下级区域
    * @param regionCode
    * @return
    */
   private static List<Region> getGridRegionChild(String regionCode, String communityCode,String roleName) {
      return CacheUtil.get(SYS_CACHE, REGION_CHILD_CODE, regionCode, () -> regionService.getGridRegionChild(regionCode,communityCode,roleName));
   }
   /**
    * 获取民警下级区域
    * @param regionCode
    * @return
    */
   private static List<Region> getPoliceRegionChild(String regionCode,String communityCode) {
      return CacheUtil.get(SYS_CACHE, REGION_CHILD_CODE, regionCode, () -> regionService.getPoliceRegionChild(regionCode,communityCode));
   }
   /**
    * 获取下级区域
    * @param regionCode
    * @return
    */
   private static List<Region> getRegionChild(String regionCode,String communityCode) {
      return CacheUtil.get(SYS_CACHE, REGION_CHILD_CODE, regionCode, () -> regionService.getRegionChild(regionCode,communityCode));
   }
   /**
    * 查询当前文章范围对应的社区编号字符串集合
    * @param articleRange
    * @return
    */
   public static String getAllCommunityNameListString(String articleRange,String id) {
      return CacheUtil.get(ARTICLE_KEY, "id", id ,() -> regionService.getAllCommunityNameListString(articleRange));
   }
}