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> groupList(@Valid GroupTypeDTO dto) { return ApiResp.respOK(coreRoleService.list( new QueryWrapper() .eq("type", dto.getType()) .orderByAsc("sort") )); } /** * 角色编辑 * * @param roleGroup * @return */ @PostMapping("/group/update") public ApiResp 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 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> userPage(PageDTO dto) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("cru.is_deleted", 0); dto.likeColumns(queryWrapper, "cru.ext1", "cru.ext2", "cru.ext3"); queryWrapper.orderByAsc("cru.user_id"); Page page = dto.pageObj(); coreRoleService.getUserMapper().page(page, queryWrapper); return ApiResp.respOK(page.getRecords(), page.getTotal()); } /** * 授权调整 * * @return */ @PostMapping("/user/update") public ApiResp 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 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() { return ApiResp.respOK(coreServerService.list()); } /** * 修改服务 * * @param coreService * @return */ @PostMapping("/server/update") public ApiResp update(@RequestBody CoreRoleServer coreService) { coreServerService.saveOrUpdate(coreService); return ApiResp.respOK(""); } /** * 权限-列出 * * @return */ @GetMapping("/authority/list") public ApiResp> menuList(@Valid ServiceDTO dto) { List list = coreServerService.getAuthorityMapper().selectList(new QueryWrapper() .eq(CoreRoleAuthority.IS_DELETED, 0) .eq("service", dto.getService()) .orderByAsc("sort") ); List collect = coreServerService.menuWrap(list, null); return ApiResp.respOK(collect); } /** * 权限-编辑 * * @return */ @PostMapping("/authority/update") public ApiResp menuUpdate(@RequestBody CoreRoleAuthority serviceMenu) { if (StrUtil.isEmpty(serviceMenu.getId())) { coreServerService.getAuthorityMapper().insert(serviceMenu); } else { serviceMenu.setNo(null); coreServerService.getAuthorityMapper().updateById(serviceMenu); } return ApiResp.respOK(""); } }