Bläddra i källkod

租户优化修改

dongxin 1 år sedan
förälder
incheckning
71684481dc

+ 6 - 0
commons/src/main/java/com/jzg/commons/entity/po/SysRolePerms.java

@@ -1,5 +1,7 @@
 package com.jzg.commons.entity.po;
 
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
 
@@ -7,14 +9,18 @@ import lombok.Data;
 @Data
 public class SysRolePerms {
 
+    @TableId(value = "id")
     private String id;
 
     @Schema(description = "角色id")
+    @TableField(value = "role_id")
     private String roleId;
 
     @Schema(description = "权限id")
+    @TableField(value = "perms_id")
     private String permsId;
 
     @Schema(description = "权限标识")
+    @TableField(value = "perms")
     private String perms;
 }

+ 20 - 0
commons/src/main/java/com/jzg/commons/entity/vo/SysConfigVo.java

@@ -0,0 +1,20 @@
+package com.jzg.commons.entity.vo;
+
+import com.jzg.commons.entity.enums.ConfigTypeEnum;
+import io.swagger.v3.oas.annotations.media.Schema;
+import jakarta.validation.constraints.NotEmpty;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+@Schema(description = "系统配置实体")
+public class SysConfigVo {
+
+    @Schema(description = "系统code")
+    private String systemCode;
+
+    @Schema(description = "系统类型")
+    @NotEmpty(message = "系统类型不能为空")
+    private Integer type;
+}

+ 11 - 2
commons/src/main/java/com/jzg/commons/entity/vo/SysSystemPermsVo.java

@@ -25,10 +25,19 @@ public class SysSystemPermsVo {
     @Data
     public static class PermsVo{
         @Schema(description = "权限ID")
-        private String permsId;
+        private String id;
 
         @Schema(description = "权限标识")
-        private String perms;
+        private String auth;
+
+        @Schema(description = "标题")
+        private String title;
+
+        @Schema(description = "类型")
+        private String type;
+
+        @Schema(description = "子集")
+        private List<PermsVo> children;
     }
 
 }

+ 6 - 0
platform/src/main/java/com/jzg/PlatFormApplication.java

@@ -8,6 +8,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 @SpringBootApplication
 public class PlatFormApplication {
     public static void main(String[] args) {
+//        /*
+//         * 指定使用的日志框架,否则将会告警
+//         * RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.InternalThreadLocalMap).
+//         * RocketMQLog:WARN Please initialize the logger system properly.
+//         */
+//        System.setProperty("rocketmq.client.logUseSlf4j", "true");
         SpringApplication.run(PlatFormApplication.class, args);
     }
 

+ 9 - 7
platform/src/main/java/com/jzg/controller/SysSystemController.java

@@ -7,6 +7,7 @@ import com.jzg.commons.entity.dto.SysSystemEditParam;
 import com.jzg.commons.entity.dto.SysSystemParam;
 import com.jzg.commons.entity.enums.YesNoEnum;
 import com.jzg.commons.entity.po.SysConfig;
+import com.jzg.commons.entity.vo.SysConfigVo;
 import com.jzg.commons.entity.vo.SysSystemPermsVo;
 import com.jzg.commons.entity.vo.SysSystemVo;
 import com.jzg.commons.entity.vo.UpAndDownVo;
@@ -35,16 +36,17 @@ public class SysSystemController {
     private SysSystemService sysSystemService;
 
     /**
-     * 获取初始化配置
+     * 获取平台初始化配置
      * @return
      */
-    @GetMapping("/getSystemConfig")
-    @Operation(summary = "获取初始化配置")
-    public HttpResult getSystemConfig(){
-        return HttpResult.ok(sysSystemService.getSystemConfig());
+    @GetMapping("/getInitConfig")
+    @Operation(summary = "获取平台初始化配置")
+    public HttpResult getInitConfig(SysConfigVo sysConfigVo){
+        return StringUtils.isNull(sysSystemService.getSystemConfig(sysConfigVo))?
+                HttpResult.error("获取配置失败!")
+                : HttpResult.error("获取配置成功!");
     }
 
-
     /**
      * 更新初始化配置
      * @param sysConfig
@@ -76,7 +78,7 @@ public class SysSystemController {
      * @param systemCode
      * @return
      */
-    @PostMapping("/getSystemPerms")
+    @GetMapping("/getSystemPerms")
     @Operation(summary = "获取租户权限")
     public HttpResult getSystemPerms(String systemCode){
         return HttpResult.ok(sysSystemService.getSystemPerms(systemCode));

+ 10 - 0
platform/src/main/java/com/jzg/mapper/SysRolePermsMapper.java

@@ -3,7 +3,17 @@ package com.jzg.mapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.jzg.commons.entity.po.SysRolePerms;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 @Mapper
 public interface SysRolePermsMapper extends BaseMapper<SysRolePerms> {
+
+    /**
+     * 根据角色id获取权限信息
+     * @param id
+     * @return
+     */
+    List<SysRolePerms> getByRoleId(@Param("id") String id);
 }

+ 11 - 4
platform/src/main/java/com/jzg/service/SysSystemService.java

@@ -6,9 +6,8 @@ import com.jzg.commons.entity.dto.SysSystemAddParam;
 import com.jzg.commons.entity.dto.SysSystemEditParam;
 import com.jzg.commons.entity.dto.SysSystemParam;
 import com.jzg.commons.entity.po.SysConfig;
-import com.jzg.commons.entity.po.SysRolePerms;
 import com.jzg.commons.entity.po.SysSystem;
-import com.jzg.commons.entity.po.SysSystemPerms;
+import com.jzg.commons.entity.vo.SysConfigVo;
 import com.jzg.commons.entity.vo.SysSystemPermsVo;
 import com.jzg.commons.entity.vo.SysSystemVo;
 import com.jzg.commons.entity.vo.UpAndDownVo;
@@ -32,7 +31,7 @@ public interface SysSystemService extends IService<SysSystem> {
      * 获取初始化配置
      * @return
      */
-    public SysConfig getSystemConfig();
+    public SysConfig getSystemConfig(SysConfigVo sysConfigVo);
 
     /**
      * 更新租户权限
@@ -40,12 +39,20 @@ public interface SysSystemService extends IService<SysSystem> {
      */
     public boolean updatePerms(SysSystemPermsVo sysSystemPermsVo);
 
+    /**
+     * 保存配置信息
+     * @param configStr 配置json
+     * @param type 1.全部菜单
+     * @param sysCode 系统code
+     */
+    public void saveConfig(String configStr, Integer type, String sysCode);
+
     /**
      * 获取租户权限
      * @param systemCode
      * @return
      */
-    public List<SysRolePerms> getSystemPerms(String systemCode);
+    public List<String> getSystemPerms(String systemCode);
 
     /**
      * 根据SystemCode 查询 System对象

+ 99 - 33
platform/src/main/java/com/jzg/service/impl/SysSystemServiceImpl.java

@@ -6,11 +6,13 @@ import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.jzg.commons.constants.SystemConstants;
 import com.jzg.commons.entity.dto.SysSystemAddParam;
 import com.jzg.commons.entity.dto.SysSystemEditParam;
 import com.jzg.commons.entity.dto.SysSystemParam;
 import com.jzg.commons.entity.enums.YesNoEnum;
 import com.jzg.commons.entity.po.*;
+import com.jzg.commons.entity.vo.SysConfigVo;
 import com.jzg.commons.entity.vo.SysSystemPermsVo;
 import com.jzg.commons.entity.vo.SysSystemVo;
 import com.jzg.commons.entity.vo.UpAndDownVo;
@@ -18,7 +20,6 @@ import com.jzg.commons.util.MinioUtils;
 import com.jzg.commons.util.StringUtils;
 import com.jzg.mapper.*;
 import com.jzg.service.SysSystemService;
-import com.jzg.commons.util.MinioUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -61,22 +62,12 @@ public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem
     private String bucketName;
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public boolean updatePerms(SysSystemPermsVo sysSystemPermsVo) {
-        String permsStr = JSON.toJSONString(sysSystemPermsVo.getPerms());
-        List<SysConfig> configs = sysConfigMapper.selectList(new LambdaQueryWrapper<SysConfig>()
-                .eq(SysConfig::getType, 1).eq(SysConfig::getSystemCode,sysSystemPermsVo.getSystemCode()));
-        SysConfig config;
-        if(CollUtil.isEmpty(configs)){
-            config = new SysConfig();
-            config.setSysConfig(permsStr);
-            config.setType(1);
-            config.setSystemCode(sysSystemPermsVo.getSystemCode());
-            sysConfigMapper.insert(config);
-        }else{
-            config = configs.get(0);
-            config.setSysConfig(permsStr);
-            sysConfigMapper.updateById(config);
-        }
+        // 保存勾选菜单配置
+        String permsStr = sysSystemPermsVo.getPermsStr();
+        this.saveConfig(permsStr, 1, sysSystemPermsVo.getSystemCode());
+
         // 获取租户管理员角色id
         LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(SysRole::getSystemCode, sysSystemPermsVo.getSystemCode())
@@ -86,34 +77,71 @@ public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem
         LambdaQueryWrapper<SysRolePerms> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(SysRolePerms::getRoleId,roles.get(0).getId());
         sysRolePermsMapper.delete(wrapper);
-        List<SysRolePerms> perms = new ArrayList<>();
-        sysSystemPermsVo.getPerms().forEach(per->{
-            SysRolePerms sysSystemPerms = new SysRolePerms();
-            sysSystemPerms.setRoleId(roles.get(0).getId());
-            sysSystemPerms.setPermsId(per.getPermsId());
-            sysSystemPerms.setPerms(per.getPerms());
-            perms.add(sysSystemPerms);
-        });
-        return !sysRolePermsMapper.insert(perms).isEmpty();
+
+        List<SysSystemPermsVo.PermsVo> permsList = sysSystemPermsVo.getPerms();
+        return this.savePerms(permsList, roles.get(0).getId());
+    }
+
+    /**
+     * 保存配置信息
+     * @param configStr 配置json
+     * @param type 1.全部菜单
+     * @param sysCode 系统code
+     */
+    public void saveConfig(String configStr, Integer type, String sysCode){
+        List<SysConfig> checkConfigs = sysConfigMapper.selectList(new LambdaQueryWrapper<SysConfig>()
+                .eq(SysConfig::getType, type).eq(SysConfig::getSystemCode,sysCode));
+        SysConfig config;
+        if(CollUtil.isEmpty(checkConfigs)){
+            config = new SysConfig();
+            config.setSysConfig(configStr);
+            config.setType(2);
+            config.setSystemCode(sysCode);
+            sysConfigMapper.insert(config);
+        }else{
+            config = checkConfigs.get(0);
+            config.setSysConfig(configStr);
+            sysConfigMapper.updateById(config);
+        }
+    }
+
+    /**
+
+     * 递归把传过来的tree转为list
+     *
+     * @param result     结果集
+     * @param root       当前对象
+     */
+    public void recursionTreeToList(List<SysSystemPermsVo.PermsVo> result, SysSystemPermsVo.PermsVo root) {
+        result.add(root);
+        if (CollUtil.isEmpty(root.getChildren())) {
+            return;
+        }
+        for (SysSystemPermsVo.PermsVo child : root.getChildren()) {
+            this.recursionTreeToList(result, child);
+        }
     }
 
     @Override
-    public List<SysRolePerms> getSystemPerms(String systemCode) {
+    public List<String> getSystemPerms(String systemCode) {
         LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(SysRole::getSystemCode, systemCode)
                 .like(SysRole::getName,"管理员");
         List<SysRole> roles = sysRoleMapper.selectList(queryWrapper);
-        List<SysRolePerms> sysSystemPerms = sysRolePermsMapper.selectList(new LambdaQueryWrapper<SysRolePerms>()
-                .eq(SysRolePerms::getRoleId, roles.get(0).getId()));
-        return sysSystemPerms;
+        List<SysRolePerms> sysSystemPerms = sysRolePermsMapper.getByRoleId(roles.get(0).getId());
+        List<String> permsIds = sysSystemPerms.stream().map(SysRolePerms::getPermsId).collect(Collectors.toList());
+        return permsIds;
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public boolean updateConfig(SysConfig config) {
         List<SysConfig> configs = sysConfigMapper.selectList(new LambdaQueryWrapper<SysConfig>()
-                .eq(SysConfig::getType, 0));
+                .eq(SysConfig::getType, 0)
+                .eq(SysConfig::getSystemCode, SystemConstants.SYSTEM_PLATFORM.getCode()));
         if(CollUtil.isEmpty(configs)){
             config.setType(0);
+            config.setSystemCode(SystemConstants.SYSTEM_PLATFORM.getCode());
             sysConfigMapper.insert(config);
         }else{
             config.setId(configs.get(0).getId());
@@ -123,9 +151,13 @@ public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem
     }
 
     @Override
-    public SysConfig getSystemConfig() {
+    public SysConfig getSystemConfig(SysConfigVo sysConfigVo) {
         List<SysConfig> configs = sysConfigMapper.selectList(new LambdaQueryWrapper<SysConfig>()
-                .eq(SysConfig::getType, 0));
+                .eq(SysConfig::getType, sysConfigVo.getType())
+                .eq(StringUtils.isNotEmpty(sysConfigVo.getSystemCode()),SysConfig::getSystemCode, sysConfigVo.getSystemCode()));
+        if(CollUtil.isEmpty(configs)){
+            return null;
+        }
         return configs.get(0);
     }
 
@@ -135,7 +167,7 @@ public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem
      * @return
      */
     @Override
-    public SysSystem getBySystemCode(String systemCode ) {
+    public SysSystem getBySystemCode(String systemCode) {
         return this.getOne(new LambdaQueryWrapper<SysSystem>()
                 .eq(SysSystem::getSysCode, systemCode)
                 .eq(SysSystem::getAvailable, YesNoEnum.YES.getCode())
@@ -252,11 +284,45 @@ public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem
         userRole.setUserId(userId);
         userRole.setRoleId(role.getId());
         sysUserRoleMapper.insert(userRole);
+        // 初始化权限信息
+        // 1.初始化配置的保存
+        List<SysConfig> configs = sysConfigMapper.selectList(new LambdaQueryWrapper<SysConfig>()
+                .eq(SysConfig::getType, 0)
+                .eq(SysConfig::getSystemCode, SystemConstants.SYSTEM_PLATFORM.getCode()));
+        this.saveConfig(configs.get(0).getSysConfig(), 0, SystemConstants.SYSTEM_PLATFORM.getCode());
+
+        List<SysSystemPermsVo.PermsVo> permsList = JSON.parseArray(configs.get(0).getSysConfig(),SysSystemPermsVo.PermsVo.class);;
+
+        this.savePerms(permsList, role.getId());
         // 租户绑定管理员信息
         sysSystem.setUserId(userId);
         this.updateById(sysSystem);
     }
 
+    /**
+     * 租户权限保存
+     * @param permsList 树形租户权限
+     * @param roleId 角色id
+     */
+    public boolean savePerms(List<SysSystemPermsVo.PermsVo> permsList, String roleId){
+        List<SysSystemPermsVo.PermsVo> all = new ArrayList<>();
+        for (SysSystemPermsVo.PermsVo permsVo : permsList) {
+            List<SysSystemPermsVo.PermsVo> result = new ArrayList<>();
+            this.recursionTreeToList(result, permsVo);
+            all.addAll(result);
+        }
+
+        List<SysRolePerms> perms = new ArrayList<>();
+        all.forEach(per->{
+            SysRolePerms sysSystemPerms = new SysRolePerms();
+            sysSystemPerms.setRoleId(roleId);
+            sysSystemPerms.setPermsId(per.getId());
+            sysSystemPerms.setPerms(per.getAuth());
+            perms.add(sysSystemPerms);
+        });
+        return !sysRolePermsMapper.insert(perms).isEmpty();
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void sysEdit(SysSystemEditParam param) {

+ 14 - 0
platform/src/main/resources/mapper/SysRolePermsMapper.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.jzg.mapper.SysRolePermsMapper">
+
+    <select id="getByRoleId" resultType="com.jzg.commons.entity.po.SysRolePerms">
+        select id, role_id as roleId, perms_id as permsId, perms
+        from sys_role_perms
+        <where>
+            <if test="id != null and id != ''">
+                role_id = #{id}
+            </if>
+        </where>
+    </select>
+</mapper>