From 16b704d18a875d1fb63827aaa507790ba2bef5be Mon Sep 17 00:00:00 2001
From: 44323 <443237572@qq.com>
Date: 星期二, 23 四月 2024 11:44:13 +0800
Subject: [PATCH] JK最终代码提交

---
 guns-management/src/main/java/com/stylefeng/guns/modular/code/controller/SysRoleController.java |   98 ++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 72 insertions(+), 26 deletions(-)

diff --git a/guns-management/src/main/java/com/stylefeng/guns/modular/code/controller/SysRoleController.java b/guns-management/src/main/java/com/stylefeng/guns/modular/code/controller/SysRoleController.java
index 1098cb8..ef0e731 100644
--- a/guns-management/src/main/java/com/stylefeng/guns/modular/code/controller/SysRoleController.java
+++ b/guns-management/src/main/java/com/stylefeng/guns/modular/code/controller/SysRoleController.java
@@ -1,6 +1,8 @@
 package com.stylefeng.guns.modular.code.controller;
 
-import com.stylefeng.guns.core.base.tips.Tip;
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
 import com.stylefeng.guns.core.cache.CacheKit;
 import com.stylefeng.guns.core.common.annotion.BussinessLog;
 import com.stylefeng.guns.core.common.constant.Const;
@@ -14,22 +16,29 @@
 import com.stylefeng.guns.core.node.ZTreeNode;
 import com.stylefeng.guns.core.shiro.ShiroKit;
 import com.stylefeng.guns.core.util.ToolUtil;
+import com.stylefeng.guns.modular.system.dto.DeptQuery;
+import com.stylefeng.guns.modular.system.dto.TreeBean;
+import com.stylefeng.guns.modular.system.model.Dept;
 import com.stylefeng.guns.modular.system.model.Role;
+import com.stylefeng.guns.modular.system.model.User;
 import com.stylefeng.guns.modular.system.service.IMenuService;
 import com.stylefeng.guns.modular.system.service.IRoleService;
+import com.stylefeng.guns.modular.system.service.IUserService;
+import com.stylefeng.guns.modular.system.util.ListToTreeUtil;
 import com.stylefeng.guns.modular.system.util.ResultUtil;
-import com.stylefeng.guns.modular.system.warpper.RoleWarpper;
 import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
+import org.springframework.util.CollectionUtils;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
+
 @Controller
 @RequestMapping("/base/role")
 public class SysRoleController {
@@ -38,21 +47,24 @@
     private IRoleService roleService;
     @Autowired
     private IMenuService menuService;
+    @Autowired
+    private IUserService userService;
 
     @GetMapping(value = "/list")
     @ApiOperation(value = "列表", tags = {"后台-角色管理"})
     @ResponseBody
-    public Object list(@RequestParam(required = false) String roleName) {
-        List<Map<String, Object>> roles = this.roleService.selectRoles(roleName);
-        return roles;
+    public ResultUtil<PageInfo<Map>> list(DeptQuery req) {
+//        PageHelper.startPage(req.getPageNum(),req.getPageSize());
+        List<Map<String, Object>> roles = this.roleService.selectRoles(req.getDeptName());
+        PageInfo<Map> info=new PageInfo<>(roles);
+        return ResultUtil.success(info);
     }
-
 
     @PostMapping(value = "/add")
     @BussinessLog(value = "添加角色", key = "name", dict = RoleDict.class)
     @ApiOperation(value = "添加角色", tags = {"后台-角色管理"})
     @ResponseBody
-    public ResultUtil add(@Valid Role role, BindingResult result) {
+    public ResultUtil add(@Valid Role role, @RequestParam("ids") String ids,BindingResult result) {
         if (result.hasErrors()) {
             throw new GunsException(BizExceptionEnum.REQUEST_NULL);
         }
@@ -60,7 +72,9 @@
         role.setDeptid(0);
         role.setPid(0);
         role.setInsertTime(new Date());
+        role.setCreateBy(Objects.requireNonNull(ShiroKit.getUser()).id);
         this.roleService.insert(role);
+        this.roleService.setAuthority(role.getId(), ids);
         return ResultUtil.success("添加成功");
     }
 
@@ -78,23 +92,31 @@
     @BussinessLog(value = "修改角色", key = "name", dict = RoleDict.class)
     @ApiOperation(value = "修改角色", tags = {"后台-角色管理"})
     @ResponseBody
-    public ResultUtil edit(@Valid Role role, BindingResult result) {
-        if (result.hasErrors()) {
-            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
-        }
+    public ResultUtil edit(@RequestParam("id") Integer roleId, @RequestParam("name") String name,@RequestParam("ids") String ids) {
+
+        Role role = roleService.selectById(roleId);
         role.setDeptid(0);
+        role.setName(name);
         role.setPid(0);
         this.roleService.updateById(role);
+        this.roleService.setAuthority(role.getId(), ids);
         return ResultUtil.success("修改成功");
     }
 
-
-    @PostMapping(value = "/delete/{id}")
+    @PostMapping(value = "/delete")
     @BussinessLog(value = "删除角色", key = "name", dict = RoleDict.class)
     @ApiOperation(value = "删除角色", tags = {"后台-角色管理"})
     @ResponseBody
-    public ResultUtil delete(@PathVariable Integer id) {
-        if (ToolUtil.isEmpty(id)) {
+    public ResultUtil delete( String ids) {
+        String[] split = ids.split(",");
+        for (String id : split) {
+            List<User> roleid = userService.selectList(new EntityWrapper<User>().eq("roleid", Integer.valueOf(id)));
+            if (!roleid.isEmpty()){
+                return ResultUtil.error("当前角色已绑定用户,无法删除");
+            }
+
+
+            if (ToolUtil.isEmpty(id)) {
             throw new GunsException(BizExceptionEnum.REQUEST_NULL);
         }
 
@@ -104,11 +126,13 @@
         }
 
         //缓存被删除的角色名称
-        LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(id));
+        LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(Integer.valueOf(id)));
         Role role = roleService.selectById(id);
-        this.roleService.delRoleById(id);
+        this.roleService.delRoleById(Integer.valueOf(id));
         //删除缓存
         CacheKit.removeAll(Cache.CONSTANT);
+        }
+
         return ResultUtil.success("删除成功");
     }
 
@@ -119,18 +143,21 @@
     @GetMapping (value = "/menuTreeListByRoleId/{roleId}")
     @ApiOperation(value = "角色分配权限获取列表", tags = {"后台-角色管理"})
     @ResponseBody
-    public List<ZTreeNode> menuTreeListByRoleId(@PathVariable Integer roleId) {
-
+    public  List<TreeBean> menuTreeListByRoleId(@PathVariable Integer roleId) {
         List<Long> menuIds = this.menuService.getMenuIdsByRoleId(roleId);
         if (ToolUtil.isEmpty(menuIds)) {
             List<ZTreeNode> roleTreeList = this.menuService.menuTreeList();
-            return roleTreeList;
+            List<TreeBean> root = ListToTreeUtil.toTree(roleTreeList, "root");
+            return root;
         }else {
+            for (Long menuId : menuIds) {
+                System.err.println("菜单id:"+menuId);
+            }
             List<ZTreeNode> roleTreeListByUserId = this.menuService.menuTreeListByMenuIds(menuIds);
-            return roleTreeListByUserId;
+            List<TreeBean> root = ListToTreeUtil.toTree(roleTreeListByUserId, "root");
+            return root;
         }
     }
-
     @DataSource(name = "dataSourceGuns")
     @RequestMapping("/setAuthority")
     @BussinessLog(value = "配置权限操作", key = "roleId,ids", dict = RoleDict.class)
@@ -143,6 +170,25 @@
         Role role = roleService.selectById(roleId);
         return ResultUtil.success("分配成功");
     }
+    private List<ZTreeNode> getParent(List<ZTreeNode> roleTreeListByUserId,List<ZTreeNode> parent){
+        List<ZTreeNode> result = new ArrayList<>();
+        for (ZTreeNode zTreeNode : parent) {
+
+            List<ZTreeNode> children = roleTreeListByUserId.stream().filter(e -> e.getpId().equals(zTreeNode.getId())).collect(Collectors.toList());
+            if(CollectionUtils.isEmpty(children)){
+                zTreeNode.setChildren(children);
+            }else {
+                getParent(roleTreeListByUserId,children);
+            }
+            ZTreeNode zTreeNode1 = new ZTreeNode();
+            BeanUtils.copyProperties(zTreeNode,zTreeNode1);
+            result.add(zTreeNode1);
+        }
+        return result;
+    }
+
+
+
 
 
 

--
Gitblit v1.7.1