You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-spms-java/src/main/java/com/glxp/api/controller/auth/SysRoleController.java

239 lines
8.2 KiB
Java

package com.glxp.api.controller.auth;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.pagehelper.PageInfo;
import com.glxp.api.annotation.Log;
import com.glxp.api.constant.BusinessType;
import com.glxp.api.constant.Constant;
import com.glxp.api.controller.BaseController;
import com.glxp.api.entity.auth.AuthAdmin;
import com.glxp.api.entity.auth.SysRole;
import com.glxp.api.entity.auth.SysUserRole;
import com.glxp.api.req.auth.FilterAuthUserRequest;
import com.glxp.api.req.auth.FilterRoleRequest;
import com.glxp.api.res.PageSimpleResponse;
import com.glxp.api.service.auth.AuthAdminService;
import com.glxp.api.service.auth.ISysRoleService;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {
@Resource
ISysRoleService roleService;
@Resource
AuthAdminService userService;
/**
* 获取角色信息列表
*/
@GetMapping("/list")
public BaseResponse list(FilterRoleRequest filterRoleRequest) {
List<SysRole> sysRoles = roleService.selectPageRoleList(filterRoleRequest);
PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoles);
PageSimpleResponse<SysRole> pageSimpleResponse = new PageSimpleResponse<>();
pageSimpleResponse.setTotal(pageInfo.getTotal());
pageSimpleResponse.setList(sysRoles);
return ResultVOUtils.success(pageSimpleResponse);
}
/**
* 根据角色编号获取详细信息
*
* @param roleId 角色ID
*/
@GetMapping(value = "/{roleId}")
public BaseResponse getInfo(@PathVariable Long roleId) {
roleService.checkRoleDataScope(roleId);
return ResultVOUtils.success(roleService.selectRoleById(roleId));
}
/**
* 新增角色
*/
@PostMapping
@Log(title = "用户管理", businessType = BusinessType.INSERT)
public BaseResponse add(@Validated @RequestBody SysRole role) {
role.setRoleKey(UUID.randomUUID() + "");
if (Constant.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
return ResultVOUtils.error(500, "新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
} else if (Constant.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
return ResultVOUtils.error(500, "新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
}
role.setCreateBy(getUser().getId() + "");
role.setCreateTime(new Date());
int i = roleService.insertRole(role);
return ResultVOUtils.success("新增成功!");
}
/**
* 修改保存角色
*/
@PutMapping
@Log(title = "用户管理", businessType = BusinessType.UPDATE)
public BaseResponse edit(@Validated @RequestBody SysRole role) {
roleService.checkRoleAllowed(role);
roleService.checkRoleDataScope(role.getRoleId());
if (Constant.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
return ResultVOUtils.error(500, "修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
} else if (Constant.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
return ResultVOUtils.error(500, "修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
}
role.setUpdateTime(new Date());
role.setUpdateBy(getUser().getId() + "");
if (roleService.updateRole(role) > 0) {
// 更新缓存用户权限
return ResultVOUtils.success("修改成功!");
}
return ResultVOUtils.error(500, "修改角色'" + role.getRoleName() + "'失败,请联系管理员");
}
/**
* 修改保存数据权限
*/
@PutMapping("/dataScope")
@Log(title = "用户管理", businessType = BusinessType.UPDATE)
public BaseResponse dataScope(@RequestBody SysRole role) {
roleService.checkRoleAllowed(role);
roleService.checkRoleDataScope(role.getRoleId());
int i = roleService.authDataScope(role);
return ResultVOUtils.success("修改成功!");
}
/**
* 状态修改
*/
@PutMapping("/changeStatus")
@Log(title = "用户管理", businessType = BusinessType.UPDATE)
public BaseResponse changeStatus(@RequestBody SysRole role) {
roleService.checkRoleAllowed(role);
roleService.checkRoleDataScope(role.getRoleId());
int i = roleService.updateRoleStatus(role);
return ResultVOUtils.success("修改成功!");
}
/**
* 删除角色
*
* @param roleIds 角色ID串
*/
@DeleteMapping("/{roleIds}")
@Log(title = "用户管理", businessType = BusinessType.DELETE)
public BaseResponse remove(@PathVariable Long[] roleIds) {
int i = roleService.deleteRoleByIds(roleIds);
if(i>0){
return ResultVOUtils.success("修改成功!");
}
return ResultVOUtils.error(500,"已绑定用户,不能删除!");
}
/**
* 获取角色选择框列表
*/
@SaCheckPermission("system:role:query")
@GetMapping("/optionselect")
public BaseResponse optionselect() {
return ResultVOUtils.success(roleService.selectRoleAll());
}
/**
* 查询已分配用户角色列表
*/
@GetMapping("/authUser/allocatedList")
public BaseResponse allocatedList(FilterAuthUserRequest filterAuthUserRequest) {
List<AuthAdmin> authAdminList = userService.selectAllocatedList(filterAuthUserRequest);
PageInfo<AuthAdmin> pageInfo = new PageInfo<>(authAdminList);
PageSimpleResponse<AuthAdmin> pageSimpleResponse = new PageSimpleResponse<>();
pageSimpleResponse.setTotal(pageInfo.getTotal());
pageSimpleResponse.setList(authAdminList);
return ResultVOUtils.success(pageSimpleResponse);
}
/**
* 查询未分配用户角色列表
*/
@GetMapping("/authUser/unallocatedList")
public BaseResponse unallocatedList(FilterAuthUserRequest filterAuthUserRequest) {
List<AuthAdmin> authAdminList = userService.selectUnallocatedList(filterAuthUserRequest);
PageInfo<AuthAdmin> pageInfo = new PageInfo<>(authAdminList);
PageSimpleResponse<AuthAdmin> pageSimpleResponse = new PageSimpleResponse<>();
pageSimpleResponse.setTotal(pageInfo.getTotal());
pageSimpleResponse.setList(authAdminList);
return ResultVOUtils.success(pageSimpleResponse);
}
/**
* 取消授权用户
*/
@PutMapping("/authUser/cancel")
@Log(title = "用户管理", businessType = BusinessType.DELETE)
public BaseResponse cancelAuthUser(@RequestBody SysUserRole userRole) {
int i = roleService.deleteAuthUser(userRole);
return ResultVOUtils.success("取消成功!");
}
/**
* 批量取消授权用户
*
* @param roleId 角色ID
* @param userIds 用户ID串
*/
@SaCheckPermission("system:role:edit")
@PutMapping("/authUser/cancelAll")
@Log(title = "用户管理", businessType = BusinessType.DELETE)
public BaseResponse cancelAuthUserAll(Long roleId, Long[] userIds) {
int i = roleService.deleteAuthUsers(roleId, userIds);
return ResultVOUtils.success("取消成功!");
}
/**
* 批量选择用户授权
*
* @param roleId 角色ID
* @param userIds 用户ID串
*/
@SaCheckPermission("system:role:edit")
@PutMapping("/authUser/selectAll")
public BaseResponse selectAuthUserAll(Long roleId, Long[] userIds) {
roleService.checkRoleDataScope(roleId);
roleService.insertAuthUsers(roleId, userIds);
return ResultVOUtils.success("授权成功!");
}
/**
* 获取供应商角色信息列表
*/
@GetMapping("/customerlist")
public BaseResponse customerlist() {
List<SysRole> sysRoles = roleService.selectCustomer();
return ResultVOUtils.success(sysRoles);
}
}