package com.stylefeng.guns.modular.code.controller;
|
|
import com.stylefeng.guns.core.base.tips.Tip;
|
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.model.Role;
|
import com.stylefeng.guns.modular.system.service.IMenuService;
|
import com.stylefeng.guns.modular.system.service.IRoleService;
|
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.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.ui.Model;
|
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;
|
@Controller
|
@RequestMapping("/base/role")
|
public class SysRoleController {
|
|
@Autowired
|
private IRoleService roleService;
|
@Autowired
|
private IMenuService menuService;
|
|
@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;
|
}
|
|
|
@PostMapping(value = "/add")
|
@BussinessLog(value = "添加角色", key = "name", dict = RoleDict.class)
|
@ApiOperation(value = "添加角色", tags = {"后台-角色管理"})
|
@ResponseBody
|
public ResultUtil add(@Valid Role role, BindingResult result) {
|
if (result.hasErrors()) {
|
throw new GunsException(BizExceptionEnum.REQUEST_NULL);
|
}
|
role.setId(null);
|
role.setInsetTime(new Date());
|
this.roleService.insert(role);
|
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(@Valid Role role, BindingResult result) {
|
if (result.hasErrors()) {
|
throw new GunsException(BizExceptionEnum.REQUEST_NULL);
|
}
|
this.roleService.updateById(role);
|
return ResultUtil.success("修改成功");
|
}
|
|
|
@PostMapping(value = "/delete/{id}")
|
@BussinessLog(value = "删除角色", key = "name", dict = RoleDict.class)
|
@ApiOperation(value = "删除角色", tags = {"后台-角色管理"})
|
@ResponseBody
|
public ResultUtil delete(@PathVariable Integer id) {
|
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(id));
|
Role role = roleService.selectById(id);
|
this.roleService.delRoleById(id);
|
//删除缓存
|
CacheKit.removeAll(Cache.CONSTANT);
|
return ResultUtil.success("删除成功");
|
}
|
|
|
|
|
@DataSource(name = "dataSourceGuns")
|
@GetMapping (value = "/menuTreeListByRoleId/{roleId}")
|
@ApiOperation(value = "角色分配权限获取列表", tags = {"后台-角色管理"})
|
@ResponseBody
|
public List<ZTreeNode> menuTreeListByRoleId(@PathVariable Integer roleId) {
|
|
List<Long> menuIds = this.menuService.getMenuIdsByRoleId(roleId);
|
if (ToolUtil.isEmpty(menuIds)) {
|
List<ZTreeNode> roleTreeList = this.menuService.menuTreeList();
|
return roleTreeList;
|
}else {
|
List<ZTreeNode> roleTreeListByUserId = this.menuService.menuTreeListByMenuIds(menuIds);
|
return roleTreeListByUserId;
|
}
|
}
|
|
@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("分配成功");
|
}
|
|
|
|
@GetMapping(value = "/roleTreeList")
|
@ApiOperation(value = "获取角色树", tags = {"后台-角色管理"})
|
@ResponseBody
|
public List<ZTreeNode> roleTreeList() {
|
List<ZTreeNode> roleTreeList = this.roleService.roleTreeList();
|
roleTreeList.add(ZTreeNode.createParent());
|
return roleTreeList;
|
}
|
}
|