luo
2023-12-25 23c4967b4cb8dbce8277f830f7152d315c5a4a57
guns-management/src/main/java/com/stylefeng/guns/modular/code/controller/SysRoleController.java
@@ -1,6 +1,7 @@
package com.stylefeng.guns.modular.code.controller;
import com.stylefeng.guns.core.base.tips.Tip;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.gson.Gson;
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 +15,26 @@
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.TreeBean;
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.util.*;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/base/role")
public class SysRoleController {
@@ -38,6 +43,8 @@
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IUserService userService;
    @GetMapping(value = "/list")
    @ApiOperation(value = "列表", tags = {"后台-角色管理"})
@@ -57,7 +64,12 @@
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        role.setId(null);
        role.setInsetTime(new Date());
        role.setDeptid(0);
        role.setPid(0);
        role.setInsertTime(new Date());
        role.setCreateBy(Objects.requireNonNull(ShiroKit.getUser()).id);
        this.roleService.insert(role);
        return ResultUtil.success("添加成功");
    }
@@ -80,17 +92,27 @@
        if (result.hasErrors()) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        role.setDeptid(0);
        role.setPid(0);
        this.roleService.updateById(role);
        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);
        }
@@ -100,11 +122,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("删除成功");
    }
@@ -115,17 +139,39 @@
    @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<ZTreeNode> parent = roleTreeList.stream().filter(e -> e.getpId() == 0).collect(Collectors.toList());
            List<TreeBean> root = ListToTreeUtil.toTree(roleTreeList, "root");
            return root;
        }else {
            List<ZTreeNode> roleTreeListByUserId = this.menuService.menuTreeListByMenuIds(menuIds);
            return roleTreeListByUserId;
//            List<ZTreeNode> parent = roleTreeListByUserId.stream().filter(e -> e.getpId() == 0).collect(Collectors.toList());
            List<TreeBean> root = ListToTreeUtil.toTree(roleTreeListByUserId, "root");
            return root;
        }
    }
    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;
    }
    @DataSource(name = "dataSourceGuns")
    @RequestMapping("/setAuthority")
@@ -143,11 +189,12 @@
    @GetMapping(value = "/roleTreeList")
    @ApiOperation(value = "获取角色树", tags = {"后台-角色管理"})
    @ApiOperation(value = "获取角色下拉框", tags = {"后台-角色管理"})
    @ResponseBody
    public List<ZTreeNode> roleTreeList() {
        List<ZTreeNode> roleTreeList = this.roleService.roleTreeList();
        roleTreeList.add(ZTreeNode.createParent());
        return roleTreeList;
    public List<Role> roleTreeList() {
//        List<ZTreeNode> roleTreeList = this.roleService.roleTreeList();
//        roleTreeList.add(ZTreeNode.createParent());
        return roleService.selectList(null);
//        return roleTreeList;
    }
}