Commit c8f1e60b authored by hongguangwu's avatar hongguangwu

数据权限-缓存保存111

parent ff8c24dc
......@@ -64,4 +64,13 @@ public interface CacheConstants {
*/
String PARAMS_DETAILS = "params_details";
/**
* 数据权限信息缓存
* data_auth_details0:用户组
* data_auth_details1:用户
* hgw
* 2022-6-9 17:29:49
*/
String DATA_AUTH_DETAILS = "data_auth_details";
}
......@@ -123,4 +123,10 @@ public interface CommonConstants {
*/
String THREE_String = "3";
/**
* 下划线
* hgw 2022-6-9 17:36:35
*/
String DOWN_LINE = "_";
}
......@@ -29,6 +29,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.TimeoutException;
/**
* @Description: 数据权限控制器
......
......@@ -24,6 +24,7 @@ import com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysDataAuthVO;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import java.util.List;
import java.util.concurrent.TimeoutException;
/**
* @Description: 数据权限配置接口
......
......@@ -23,14 +23,19 @@ import com.yifu.cloud.plus.v1.yifu.admin.api.entity.*;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysDataAuthVO;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysDataAuthMapper;
import com.yifu.cloud.plus.v1.yifu.admin.service.*;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CacheConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.security.service.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -54,6 +59,9 @@ public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDa
// 自定义sql-菜单
private final SysDiySqlMenuRelService diySqlMenuRelService;
// 缓存信息
private final CacheManager cacheManager;
/**
* @Description: 分页查询
* @Author: hgw
......@@ -99,18 +107,26 @@ public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDa
} else {
List<SysDataAuthMenuRel> saveMenuDeptList = new ArrayList<>();
// 更新缓存
Cache cache = cacheManager.getCache(CacheConstants.DATA_AUTH_DETAILS + sysDataAuth.getLinkType());
if (cache == null) {
return R.failed("获取Redis缓存失败!");
}
// 编辑
String mainId = sysDataAuth.getId();
R<String> failed = null;
R<String> failed;
if (mainId == null) {
// 新增
sysDataAuth.setCreateBy(user.getId());
sysDataAuth.setCreateName(user.getNickname());
sysDataAuth.setCreateTime(LocalDateTime.now());
this.save(sysDataAuth);
mainId = sysDataAuth.getId();
} else {
// 删除之前的关系
this.deleteSubInfoByMainId(mainId);
}
failed = doCore(sysDataAuth, saveMenuDeptList, sysDataAuthVO, mainId);
failed = this.doCore(sysDataAuth, saveMenuDeptList, sysDataAuthVO, mainId, cache);
if (failed != null) {
return failed;
}
......@@ -119,6 +135,7 @@ public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDa
if (!saveMenuDeptList.isEmpty()) {
authMenuRelService.saveOrUpdateBatch(saveMenuDeptList);
}
return R.ok("保存成功");
}
} else {
......@@ -133,7 +150,8 @@ public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDa
* @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.String>
**/
private R<String> doCore(SysDataAuth sysDataAuth, List<SysDataAuthMenuRel> saveMenuDeptList
, SysDataAuthVO sysDataAuthVO, String mainId) {
, SysDataAuthVO sysDataAuthVO, String mainId, Cache cache) {
List<SysDataAuthDeptRel> authDeptList = sysDataAuthVO.getAuthDeptList();
List<SysDataAuthDiySql> authSqlList = sysDataAuthVO.getAuthSqlList();
......@@ -141,28 +159,49 @@ public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDa
List<SysDataAuthMenuRel> menuCreateList = sysDataAuthVO.getMenuCreateList();
List<SysDataAuthMenuRel> menuSettleList = sysDataAuthVO.getMenuSettleList();
List<SysDataAuthMenuRel> menuDeptList = sysDataAuthVO.getMenuDeptList();
// 存储sql的map,用作缓存 键:linkId + CommonConstants.DOWN_LINE + menu.getMenuId()
Map<String, String> authSqlMap = new HashMap<>();
StringBuilder sql = new StringBuilder();
String mapSql;
StringBuilder nowMapSql;
// Map里的Key
String linkId = sysDataAuth.getLinkId();
// 处理权限部门
if (sysDataAuth.getIsDeptAuth() == 1 || sysDataAuth.getIsDeptAuth() == 2) {
if (menuDeptList == null || menuDeptList.isEmpty()) {
return R.failed("请选择部门关联的菜单!");
} else if (sysDataAuth.getIsDeptAuth() == 2) {
sql.append(" or dept.dept_id in ('0' ");
for (SysDataAuthDeptRel dept : authDeptList) {
sql.append(", '").append(dept.getDeptId()).append("'");
dept.setSysDataAuthId(mainId);
}
sql.append(")");
authDeptRelService.saveOrUpdateBatch(authDeptList);
} else {
sql.append(" or dept.dept_id = #deptId ");
}
for (SysDataAuthMenuRel menu : menuDeptList) {
authSqlMap.put(linkId + CommonConstants.DOWN_LINE + menu.getMenuId(), sql.toString());
menu.setSysDataAuthId(mainId);
menu.setType(3);
}
saveMenuDeptList.addAll(menuSettleList);
}
String nowSql;
// 处理创建人
if (menuCreateList != null && !menuCreateList.isEmpty()) {
nowSql = " or a.create_by = #create_by ";
sysDataAuth.setIsCreateAuth(1);
for (SysDataAuthMenuRel menu : menuCreateList) {
mapSql = authSqlMap.get(linkId + CommonConstants.DOWN_LINE + menu.getMenuId());
if (!Common.isEmpty(mapSql)) {
nowMapSql = new StringBuilder(nowSql).append(mapSql);
} else {
nowMapSql = new StringBuilder(nowSql);
}
authSqlMap.put(linkId + CommonConstants.DOWN_LINE + menu.getMenuId(), nowMapSql.toString());
menu.setType(1);
menu.setSysDataAuthId(mainId);
}
......@@ -171,8 +210,16 @@ public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDa
// 处理结算主体
if (menuSettleList != null && !menuSettleList.isEmpty()) {
nowSql = " or a.settleDomainId in ('0'#settleDomainId) ";
sysDataAuth.setIsSettleAuth(1);
for (SysDataAuthMenuRel menu : menuSettleList) {
mapSql = authSqlMap.get(linkId + CommonConstants.DOWN_LINE + menu.getMenuId());
if (!Common.isEmpty(mapSql)) {
nowMapSql = new StringBuilder(nowSql).append(mapSql);
} else {
nowMapSql = new StringBuilder(nowSql);
}
authSqlMap.put(linkId + CommonConstants.DOWN_LINE + menu.getMenuId(), nowMapSql.toString());
menu.setType(2);
menu.setSysDataAuthId(mainId);
}
......@@ -183,14 +230,22 @@ public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDa
if (authSqlList != null && !authSqlList.isEmpty()) {
sysDataAuth.setIsDiySql(1);
List<SysDiySqlMenuRel> menuSqlList;
for (SysDataAuthDiySql sql : authSqlList) {
menuSqlList = sql.getSqlMenuList();
for (SysDataAuthDiySql sqls : authSqlList) {
nowSql = sqls.getDiySql();
menuSqlList = sqls.getSqlMenuList();
if (menuSqlList != null && !menuSqlList.isEmpty()) {
sql.setSysDataAuthId(mainId);
diySqlService.save(sql);
sqls.setSysDataAuthId(mainId);
diySqlService.save(sqls);
for (SysDiySqlMenuRel menuSql : menuSqlList) {
mapSql = authSqlMap.get(linkId + CommonConstants.DOWN_LINE + menuSql.getMenuId());
if (!Common.isEmpty(mapSql)) {
nowMapSql = new StringBuilder(nowSql).append(mapSql);
} else {
nowMapSql = new StringBuilder(nowSql);
}
authSqlMap.put(linkId + CommonConstants.DOWN_LINE + menuSql.getMenuId(), nowMapSql.toString());
menuSql.setSysDataAuthId(mainId);
menuSql.setSysDiySqlId(sql.getId());
menuSql.setSysDiySqlId(sqls.getId());
}
diySqlMenuRelService.saveBatch(menuSqlList);
} else {
......@@ -198,6 +253,18 @@ public class SysDataAuthServiceImpl extends ServiceImpl<SysDataAuthMapper, SysDa
}
}
}
// 更新缓存
// 缓存里放入 id_菜单id, sql
if (!authSqlMap.isEmpty()) {
// 前缀
String prefix = " and (1=1 ";
// 后缀
String suffix = " ) ";
for (Map.Entry<String, String> authSql : authSqlMap.entrySet()) {
cache.put(authSql.getKey(), prefix + authSql.getValue() + suffix);
}
}
return null;
}
......
......@@ -28,6 +28,7 @@
<result property="isSettleAuth" column="is_settle_auth"/>
<result property="isDiySql" column="is_diy_sql"/>
<result property="createBy" column="create_by"/>
<result property="createName" column="create_name"/>
<result property="createTime" column="create_time"/>
<result property="updateBy" column="update_by"/>
<result property="updateTime" column="update_time"/>
......@@ -43,6 +44,7 @@
a.is_settle_auth,
a.is_diy_sql,
a.create_by,
a.create_name,
a.create_time,
a.update_by,
a.update_time
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment