package com.stylefeng.guns.modular.code.controller; 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; import com.stylefeng.guns.core.common.constant.cache.Cache; import com.stylefeng.guns.core.common.constant.dictmap.RoleDict; import com.stylefeng.guns.core.common.constant.factory.ConstantFactory; import com.stylefeng.guns.core.common.exception.BizExceptionEnum; import com.stylefeng.guns.core.exception.GunsException; import com.stylefeng.guns.core.log.LogObjectHolder; import com.stylefeng.guns.core.mutidatasource.annotion.DataSource; 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 io.swagger.annotations.ApiOperation; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.CollectionUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @Controller @RequestMapping("/base/role") public class SysRoleController { @Autowired private IRoleService roleService; @Autowired private IMenuService menuService; @Autowired private IUserService userService; @GetMapping(value = "/list") @ApiOperation(value = "列表", tags = {"后台-角色管理"}) @ResponseBody public ResultUtil> list(DeptQuery req) { List> roles = this.roleService.selectRoles(req.getDeptName()); PageInfo 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, @RequestParam("ids") String ids,BindingResult result) { if (result.hasErrors()) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } role.setId(null); 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("添加成功"); } @RequestMapping(value = "/pre/edit/{roleId}") public Role preEdit(@PathVariable Integer roleId) { if (ToolUtil.isEmpty(roleId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } Role role = this.roleService.selectById(roleId); return role; } @PostMapping(value = "/edit") @BussinessLog(value = "修改角色", key = "name", dict = RoleDict.class) @ApiOperation(value = "修改角色", tags = {"后台-角色管理"}) @ResponseBody 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") @BussinessLog(value = "删除角色", key = "name", dict = RoleDict.class) @ApiOperation(value = "删除角色", tags = {"后台-角色管理"}) @ResponseBody public ResultUtil delete( String ids) { String[] split = ids.split(","); for (String id : split) { List roleid = userService.selectList(new EntityWrapper().eq("roleid", Integer.valueOf(id))); if (!roleid.isEmpty()){ return ResultUtil.error("当前角色已绑定用户,无法删除"); } if (ToolUtil.isEmpty(id)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } //不能删除超级管理员角色 if (id.equals(Const.ADMIN_ROLE_ID)) { throw new GunsException(BizExceptionEnum.CANT_DELETE_ADMIN); } //缓存被删除的角色名称 LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(Integer.valueOf(id))); Role role = roleService.selectById(id); this.roleService.delRoleById(Integer.valueOf(id)); //删除缓存 CacheKit.removeAll(Cache.CONSTANT); } return ResultUtil.success("删除成功"); } @DataSource(name = "dataSourceGuns") @GetMapping (value = "/menuTreeListByRoleId/{roleId}") @ApiOperation(value = "角色分配权限获取列表", tags = {"后台-角色管理"}) @ResponseBody public List menuTreeListByRoleId(@PathVariable Integer roleId) { List menuIds = this.menuService.getMenuIdsByRoleId(roleId); if (ToolUtil.isEmpty(menuIds)) { List roleTreeList = this.menuService.menuTreeList(); List root = ListToTreeUtil.toTree(roleTreeList, "root"); return root; }else { for (Long menuId : menuIds) { System.err.println("菜单id:"+menuId); } List roleTreeListByUserId = this.menuService.menuTreeListByMenuIds(menuIds); List root = ListToTreeUtil.toTree(roleTreeListByUserId, "root"); return root; } } @DataSource(name = "dataSourceGuns") @RequestMapping("/setAuthority") @BussinessLog(value = "配置权限操作", key = "roleId,ids", dict = RoleDict.class) @ResponseBody public ResultUtil setAuthority(@RequestParam("roleId") Integer roleId, @RequestParam("ids") String ids) { if (ToolUtil.isOneEmpty(roleId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } this.roleService.setAuthority(roleId, ids); Role role = roleService.selectById(roleId); return ResultUtil.success("分配成功"); } // private List getParent(List roleTreeListByUserId,List parent){ // List result = new ArrayList<>(); // for (ZTreeNode zTreeNode : parent) { // // List 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; // } @GetMapping(value = "/roleTreeList") @ApiOperation(value = "获取角色下拉框", tags = {"后台-角色管理"}) @ResponseBody public List roleTreeList() { // List roleTreeList = this.roleService.roleTreeList(); // roleTreeList.add(ZTreeNode.createParent()); return roleService.selectList(null); // return roleTreeList; } }