Files
tiesheng-springboot/springboot-role/src/main/java/com/tiesheng/role/controller/RoleController.java
2024-06-24 19:15:47 +08:00

210 lines
5.7 KiB
Java

package com.tiesheng.role.controller;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiesheng.role.pojos.dao.CoreRoleAuthority;
import com.tiesheng.role.pojos.dao.CoreRoleGroup;
import com.tiesheng.role.pojos.dao.CoreRoleServer;
import com.tiesheng.role.pojos.dao.CoreRoleUser;
import com.tiesheng.role.pojos.dto.ServiceDTO;
import com.tiesheng.role.pojos.vo.GroupTypeDTO;
import com.tiesheng.role.pojos.vo.RoleUserPageVO;
import com.tiesheng.role.pojos.vo.ServiceMenuVO;
import com.tiesheng.role.service.CoreRoleService;
import com.tiesheng.role.service.CoreServerService;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.pojos.ApiResp;
import com.tiesheng.util.pojos.IdDTO;
import com.tiesheng.util.pojos.PageDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping("/role")
public class RoleController {
@Resource
CoreServerService coreServerService;
@Resource
CoreRoleService coreRoleService;
/**
* 角色列表
*
* @return
*/
@GetMapping("/group/list")
public ApiResp<List<CoreRoleGroup>> groupList(@Valid GroupTypeDTO dto) {
return ApiResp.respOK(coreRoleService.list(
new QueryWrapper<CoreRoleGroup>()
.eq("type", dto.getType())
.orderByAsc("sort")
));
}
/**
* 角色编辑
*
* @param roleGroup
* @return
*/
@PostMapping("/group/update")
public ApiResp<String> groupUpdate(@RequestBody CoreRoleGroup roleGroup) {
if (StrUtil.isNotEmpty(roleGroup.getId())) {
roleGroup.setType(null);
roleGroup.setIsSystem(null);
}
coreRoleService.saveOrUpdate(roleGroup);
return ApiResp.respOK("");
}
/**
* 角色-删除
*
* @return
*/
@PostMapping("/group/deleted")
public ApiResp<String> groupDeleted(@RequestBody @Valid IdDTO dto) {
CoreRoleGroup byId = coreRoleService.getById(dto.getId());
if (byId == null || byId.getIsDeleted() != 0) {
throw new ApiException("角色不存在或已删除");
}
if (byId.getIsSystem() == 1) {
throw new ApiException(StrUtil.format("该{}无法删除",
Objects.equals(byId.getType(), "role") ? "角色" : "职位"));
}
CoreRoleGroup coreServiceMenu = new CoreRoleGroup();
coreServiceMenu.setId(dto.getId());
coreServiceMenu.setIsDeleted(1);
coreRoleService.updateById(coreServiceMenu);
return ApiResp.respOK("");
}
/**
* 授权列表
*
* @return
*/
@GetMapping("/user/page")
public ApiResp<List<RoleUserPageVO>> userPage(PageDTO dto) {
QueryWrapper<CoreRoleUser> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("cru.is_deleted", 0);
dto.likeColumns(queryWrapper, "cru.ext1", "cru.ext2", "cru.ext3");
queryWrapper.orderByAsc("cru.user_id");
Page<RoleUserPageVO> page = dto.pageObj();
coreRoleService.getUserMapper().page(page, queryWrapper);
return ApiResp.respOK(page.getRecords(), page.getTotal());
}
/**
* 授权调整
*
* @return
*/
@PostMapping("/user/update")
public ApiResp<String> userUpdate(@RequestBody CoreRoleUser roleUser) {
if (StrUtil.isNotEmpty(roleUser.getId())) {
coreRoleService.getUserMapper().updateById(roleUser);
} else {
coreRoleService.getUserMapper().insert(roleUser);
}
return ApiResp.respOK("");
}
/**
* 授权-删除
*
* @return
*/
@PostMapping("/user/deleted")
public ApiResp<String> userDeleted(@RequestBody @Valid IdDTO dto) {
CoreRoleUser coreRoleUser = new CoreRoleUser();
coreRoleUser.setId(dto.getId());
coreRoleUser.setIsDeleted(1);
coreRoleService.getUserMapper().updateById(coreRoleUser);
return ApiResp.respOK("");
}
/**
* 获取服务列表
*
* @return
*/
@GetMapping("/server/list")
public ApiResp<List<CoreRoleServer>> list() {
return ApiResp.respOK(coreServerService.list());
}
/**
* 修改服务
*
* @param coreService
* @return
*/
@PostMapping("/server/update")
public ApiResp<String> update(@RequestBody CoreRoleServer coreService) {
coreServerService.saveOrUpdate(coreService);
return ApiResp.respOK("");
}
/**
* 权限-列出
*
* @return
*/
@GetMapping("/authority/list")
public ApiResp<List<ServiceMenuVO>> menuList(@Valid ServiceDTO dto) {
List<CoreRoleAuthority> list = coreServerService.getAuthorityMapper().selectList(new QueryWrapper<CoreRoleAuthority>()
.eq(CoreRoleAuthority.IS_DELETED, 0)
.eq("service", dto.getService())
.orderByAsc("sort")
);
List<ServiceMenuVO> collect = coreServerService.menuWrap(list, null);
return ApiResp.respOK(collect);
}
/**
* 权限-编辑
*
* @return
*/
@PostMapping("/authority/update")
public ApiResp<String> menuUpdate(@RequestBody CoreRoleAuthority serviceMenu) {
if (StrUtil.isEmpty(serviceMenu.getId())) {
coreServerService.getAuthorityMapper().insert(serviceMenu);
} else {
serviceMenu.setNo(null);
coreServerService.getAuthorityMapper().updateById(serviceMenu);
}
return ApiResp.respOK("");
}
}