Browse Source

增加编辑角色

david 2 years ago
parent
commit
8057b2f51b

BIN
out/artifacts/roma_api_jar/roma-api.jar


+ 14 - 5
src/main/java/com/roma/romaapi/controller/ApiController.java

@@ -110,9 +110,9 @@ public class ApiController {
     @RequestMapping("/api/pageDetail")
     public Map pageDetail(HttpServletRequest request, @RequestBody String data){
         Map maps = commonUtil.analysisRequestParams(request, data); // 参数
-        Map detailInfo = apiService.pageDetailById(maps);
+        Map detailInfo = apiService.roleDetailById(maps);
 
-        return detailInfo;
+        return CustomResponse.formatResponse(detailInfo);
     }
 
     // 页面管理-新增页面
@@ -157,10 +157,19 @@ public class ApiController {
 
     // 角色管理-获取全部角色
     @RequestMapping("/api/roleList")
-    public Map roleList(HttpServletRequest request) {
+    public Map roleList(HttpServletRequest request, @RequestBody String data) {
+        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
         Map<String, Object> ret = new HashMap<>();
-        List allPermiss = apiService.roleList();
-        ret.put("options", allPermiss);
+        ret = apiService.roleList(maps);
+
+        return CustomResponse.formatResponse(ret);
+    }
+
+    // 角色管理-获取角色详情
+    @RequestMapping("/api/roleDetailed")
+    public Map roleDetailed(HttpServletRequest request, @RequestBody String data) {
+        Map maps = commonUtil.analysisRequestParams(request, data); // 参数
+        Map ret = apiService.roleDetailById(maps);
 
         return CustomResponse.formatResponse(ret);
     }

+ 66 - 0
src/main/java/com/roma/romaapi/dao/ApiDao.java

@@ -1,5 +1,6 @@
 package com.roma.romaapi.dao;
 
+import com.roma.romaapi.utils.CommonUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.EmptyResultDataAccessException;
 import org.springframework.jdbc.core.PreparedStatementCreator;
@@ -18,6 +19,8 @@ import java.util.Map;
 public class ApiDao {
     @Autowired
     private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
+    @Autowired
+    private CommonUtil commonUtil;
 
     // 通过code,获取要执行的sql
     public Map<String, Object> adminInfoQueryForMap(String adminName) {
@@ -75,6 +78,16 @@ public class ApiDao {
         }
     }
 
+    // 通过角色id,获取角色下全部的权限信息
+    public List<Map<String, Object>> getAllPermissionByRoleId(String id){
+        String sql = " SELECT * FROM `roma_permission_role` WHERE role_id=:id ";
+        Map<String, Object> bindValue = new HashMap<>();
+        bindValue.put("id", id);
+        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, bindValue);
+
+        return resMap2;
+    }
+
     // 添加角色
     public Integer addRole(Map<String, Object> bindValue) {
         String insertSql = " INSERT INTO `roma_roles` (`role_name`,`role_description`,`is_enable`,`created_at`)  " +
@@ -86,6 +99,29 @@ public class ApiDao {
         return k;
     }
 
+    // 编辑角色信息
+    public Integer editRole(Map<String, Object> bindValue) {
+        String editSql = " UPDATE `roma_roles` SET `role_name`=:roleName,`role_description`=:roleDesc,`is_enable`=:isEnable WHERE id=:id";
+        Integer row = namedParameterJdbcTemplate.update(editSql, bindValue);
+
+        return row;
+    }
+
+    // 编辑角色权限
+    public Integer editRolePermission(String info, String id) {
+        // 先删除此角色的所有权限
+        Map<String, Object> delParams = new HashMap<>();
+        delParams.put("id", id);
+        String delRolePermissionSql = " DELETE FROM `roma_permission_role` WHERE role_id=:id ";
+        Integer row = namedParameterJdbcTemplate.update(delRolePermissionSql, delParams);
+        // 增加此角色的权限信息
+        Map addMap = new HashMap<>();
+        String insertSql = " INSERT INTO `roma_permission_role` (`role_id`,`permission_id`) VALUES  " + info;
+        Integer rowAdd = namedParameterJdbcTemplate.update(insertSql, addMap);
+
+        return rowAdd;
+    }
+
     // 插入角色和权限关联表数据
     public Integer addRolePermission(String info){
         Map map = new HashMap<>();
@@ -115,4 +151,34 @@ public class ApiDao {
             return row;
         }
     }
+
+    // Role角色管理-获取总数
+    public Integer getAllRoleListHasPagingCount(Map bindValue) {
+        // 获取总数
+        String countInnerSql = " SELECT * FROM `roma_roles` WHERE 1=1 ";
+        String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + countInnerSql + " ) AS ROMA";
+        Integer count = namedParameterJdbcTemplate.queryForObject(countSql, bindValue, Integer.class);
+
+        return count;
+    }
+
+    // Page页面管理-获取带分页的列表数据
+    public List<Map<String, Object>> getAllRoleListHasPaging(Map bindValue, String page, String perPage, Integer count) {
+        // 计算分页
+        String limitAfter = commonUtil.calcPagingString(count, page, perPage);
+        String listSql = "SELECT  *  FROM `roma_roles` WHERE 1=1 ORDER BY `id` DESC " + " LIMIT " + limitAfter;
+
+        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(listSql, bindValue);
+
+        return resMap2;
+    }
+
+    //获取全部角色
+    public List<Map<String, Object>> getAllRoleForList(){
+        Map<String, Object> paramMap = new HashMap<>();
+        String sql = " SELECT  * FROM `roma_roles`";
+        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);
+
+        return resMap2;
+    }
 }

+ 0 - 9
src/main/java/com/roma/romaapi/dao/PageDao.java

@@ -64,15 +64,6 @@ public class PageDao {
         return resMap2;
     }
 
-    //获取全部角色
-    public List<Map<String, Object>> getAllRoleForList(){
-        Map<String, Object> paramMap = new HashMap<>();
-        String sql = " SELECT  * FROM `roma_roles`";
-        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);
-
-        return resMap2;
-    }
-
     // 获取全部的页面
     public List<Map<String, Object>> getAllPageList() {
         Map<String, Object> paramMap = new HashMap<>();

+ 75 - 9
src/main/java/com/roma/romaapi/service/ApiService.java

@@ -111,11 +111,43 @@ public class ApiService {
     }
 
     // 获取全部角色
-    public List roleList() {
-        Map<String, Object> map = new HashMap<>();
-        List<Map<String, Object>> menuData = pageDao.getAllRoleForList(); // 获取全部角色
+    public Map roleList(Map<String, String> maps) {
+        String page = "1"; // 页数
+        String perPage = "15"; // 显示多少条
+        Map res = new HashMap<>();
+        Map bindData = commonUtil.filterApiBindParams(maps);
+        if(maps.containsKey("page")) {
+            page = maps.get("page");
+        }
+        if(maps.containsKey("perPage")) {
+            perPage = maps.get("perPage");
+        }
+        Integer count = apiDao.getAllRoleListHasPagingCount(bindData);
+        List info = apiDao.getAllRoleListHasPaging(bindData, page, perPage, count);
+        res.put("count", count);
+        res.put("rows", info);
 
-        return menuData;
+        return res;
+    }
+
+    // 获取角色详情
+    public Map roleDetailById(Map params) {
+        String id = params.get("id").toString();
+        Map info = apiDao.getDetailsInfoByIdAndType("roma_roles", id);
+        // 获取此角色下的全部详情数据,并用,分割成字符串 permission_info
+        List<Map<String, Object>> roleAllPermission = apiDao.getAllPermissionByRoleId(id);
+        String permissionAllString = "";
+        if (roleAllPermission.size() > 0) {
+            for (int i = 0; i < roleAllPermission.size(); i++) {
+                Map map = roleAllPermission.get(i);
+                String permissionId = map.get("permission_id").toString();
+                permissionAllString += permissionId + ",";
+            }
+            permissionAllString = commonUtil.trimFirstAndLastChar(permissionAllString, ",");
+        }
+        info.put("permission_info", permissionAllString);
+
+        return info;
     }
 
     // 获取全部page_details
@@ -195,21 +227,49 @@ public class ApiService {
 
     // 添加角色
     public Map addRole(Map maps) {
+        Map retInfo = new HashMap<>(); // 返回信息
+        // 添加和编辑公用此方法
+        String id = "";
+        if (maps.containsKey("id")) {
+            id = maps.get("id").toString();
+        }
         // 角色名,是否启用,是必填的
         String isEnable = maps.get("is_enable").toString();
         String roleName = maps.get("role_name").toString();
         String roleDesc = "";
         if (maps.containsKey("role_desc")) {
             roleDesc = maps.get("role_desc").toString();
+        } else if (maps.containsKey("role_description")) {
+            roleDesc = maps.get("role_description").toString();
         }
         String createdAt = commonUtil.getNowYYMMDDHHIISS();
         Map<String, Object> addRolePamars = new HashMap<>();
         addRolePamars.put("roleName", roleName);
         addRolePamars.put("isEnable", isEnable);
         addRolePamars.put("roleDesc", roleDesc);
-        addRolePamars.put("createdAt", createdAt);
-        // 添加角色
-        Integer roleId = apiDao.addRole(addRolePamars);
+        if (id.length()>0) {
+            // 如果是编辑角色,则增加id参数
+            addRolePamars.put("id", id);
+        } else {
+            // 如果是添加角色,则增加创建时间字段
+            addRolePamars.put("createdAt", createdAt);
+        }
+        Integer roleId = 0;
+        if (id.length() > 0) {
+            // 编辑角色
+            Integer editRoleRet = apiDao.editRole(addRolePamars);
+            if (editRoleRet < 1) {
+                // 如果角色信息编辑失败,则直接返回错误,不进行权限编辑
+                retInfo.put("sysErrorCode", "500");
+
+                return retInfo;
+            }
+            roleId = Integer.valueOf(id);
+        } else {
+            // 添加角色
+            roleId = apiDao.addRole(addRolePamars);
+        }
+
         // 判断是否给角色勾选的权限
         Boolean hasPermission = maps.containsKey("permission_info");
         Integer addRolePermissionRet = 0;
@@ -226,9 +286,15 @@ public class ApiService {
                 values += " ("+roleId+","+val+"),";
             }
             values = commonUtil.trimFirstAndLastChar(values, ",");
-            addRolePermissionRet = apiDao.addRolePermission(values);
+            if (id.length() > 0) {
+                // 编辑权限
+                addRolePermissionRet = apiDao.editRolePermission(values, id);
+            } else {
+                // 添加权限
+                addRolePermissionRet = apiDao.addRolePermission(values);
+            }
         }
-        Map retInfo = new HashMap<>();
+
         if((hasPermission && addRolePermissionRet>0 && roleId>0) || (!hasPermission && roleId>0)) {
 
         } else {