系统管理接口开发

This commit is contained in:
haoxr 2020-09-11 19:56:39 +08:00
parent fd18d60dff
commit 81c94215ae
9 changed files with 184 additions and 56 deletions

View File

@ -5,5 +5,7 @@ package com.youlai.admin.common;
*/ */
public interface AdminConstant { public interface AdminConstant {
int ROOT_DEPT_ID = 0; int ROOT_DEPT_ID = 0; // 根部门ID
int ROOT_ROLE_ID =1; // 超级管理员角色ID
} }

View File

@ -55,7 +55,7 @@ public class SysDictController {
@ApiOperation(value = "字典详情", httpMethod = "GET") @ApiOperation(value = "字典详情", httpMethod = "GET")
@ApiImplicitParam(name = "id", value = "字典id", required = true, paramType = "path", dataType = "Integer") @ApiImplicitParam(name = "id", value = "字典id", required = true, paramType = "path", dataType = "Integer")
@GetMapping("/{id}") @GetMapping("/{id}")
public Result detail(@PathVariable Long id) { public Result detail(@PathVariable Integer id) {
SysDict sysDict = iSysDictService.getById(id); SysDict sysDict = iSysDictService.getById(id);
return Result.success(sysDict); return Result.success(sysDict);
} }
@ -75,7 +75,7 @@ public class SysDictController {
}) })
@PutMapping(value = "/{id}") @PutMapping(value = "/{id}")
public Result update( public Result update(
@PathVariable Long id, @PathVariable Integer id,
@RequestBody SysDict sysDict) { @RequestBody SysDict sysDict) {
sysDict.setUpdateTime(new Date()); sysDict.setUpdateTime(new Date());
boolean status = iSysDictService.updateById(sysDict); boolean status = iSysDictService.updateById(sysDict);

View File

@ -54,7 +54,7 @@ public class SysDictTypeController {
@ApiOperation(value = "字典类型详情", httpMethod = "GET") @ApiOperation(value = "字典类型详情", httpMethod = "GET")
@ApiImplicitParam(name = "id", value = "字典类型id", required = true, paramType = "path", dataType = "Integer") @ApiImplicitParam(name = "id", value = "字典类型id", required = true, paramType = "path", dataType = "Integer")
@GetMapping("/{id}") @GetMapping("/{id}")
public Result detail(@PathVariable Long id) { public Result detail(@PathVariable Integer id) {
SysDictType sysDictType = iSysDictTypeService.getById(id); SysDictType sysDictType = iSysDictTypeService.getById(id);
return Result.success(sysDictType); return Result.success(sysDictType);
} }
@ -74,7 +74,7 @@ public class SysDictTypeController {
}) })
@PutMapping(value = "/{id}") @PutMapping(value = "/{id}")
public Result update( public Result update(
@PathVariable Long id, @PathVariable Integer id,
@RequestBody SysDictType sysDictType) { @RequestBody SysDictType sysDictType) {
boolean status = iSysDictTypeService.updateById(sysDictType); boolean status = iSysDictTypeService.updateById(sysDictType);
return Result.status(status); return Result.status(status);

View File

@ -4,10 +4,14 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.admin.common.AdminConstant;
import com.youlai.admin.domain.entity.SysMenu; import com.youlai.admin.domain.entity.SysMenu;
import com.youlai.admin.domain.entity.SysMenu; import com.youlai.admin.domain.entity.SysMenu;
import com.youlai.admin.domain.entity.SysMenu; import com.youlai.admin.domain.entity.SysMenu;
import com.youlai.admin.domain.entity.SysRoleMenu;
import com.youlai.admin.domain.vo.TreeSelectVO;
import com.youlai.admin.service.ISysMenuService; import com.youlai.admin.service.ISysMenuService;
import com.youlai.admin.service.ISysRoleMenuService;
import com.youlai.common.result.PageResult; import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result; import com.youlai.common.result.Result;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -18,8 +22,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Date; import java.util.*;
import java.util.List; import java.util.stream.Collectors;
@Api(tags = "菜单接口") @Api(tags = "菜单接口")
@RestController @RestController
@ -30,13 +34,18 @@ public class SysMenuController {
@Autowired @Autowired
private ISysMenuService iSysMenuService; private ISysMenuService iSysMenuService;
@Autowired
private ISysRoleMenuService iSysRoleMenuService;
@ApiOperation(value = "列表分页", httpMethod = "GET") @ApiOperation(value = "列表分页", httpMethod = "GET")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "mode", value = "查询模式: 1-表格数据 2-树形下拉", paramType = "query", dataType = "Integer") @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "mode", value = "查询模式: 1-表格数据 2-树形数据", paramType = "query", dataType = "Integer")
}) })
@GetMapping @GetMapping
public Result list(String name,Integer mode) { public Result list(String name, Integer roleId, Integer mode) {
LambdaQueryWrapper<SysMenu> baseQuery = new LambdaQueryWrapper<SysMenu>() LambdaQueryWrapper<SysMenu> baseQuery = new LambdaQueryWrapper<SysMenu>()
.orderByAsc(SysMenu::getSort) .orderByAsc(SysMenu::getSort)
.orderByDesc(SysMenu::getUpdateTime) .orderByDesc(SysMenu::getUpdateTime)
@ -45,8 +54,24 @@ public class SysMenuController {
if (mode.equals(1)) { // 表格数据 if (mode.equals(1)) { // 表格数据
baseQuery = baseQuery.like(StrUtil.isNotBlank(name), SysMenu::getName, name); baseQuery = baseQuery.like(StrUtil.isNotBlank(name), SysMenu::getName, name);
list = iSysMenuService.listForTableData(baseQuery); list = iSysMenuService.listForTableData(baseQuery);
} else if (mode.equals(2)) { // tree-select 树形下拉数据 } else if (mode.equals(2)) { // 树形数据
list = iSysMenuService.listForTreeSelect(baseQuery); list = iSysMenuService.listForTreeSelect(baseQuery);
if (roleId != null) { // 菜单树形 + 角色权限
Map<String, Object> map = new HashMap<>();
map.put("menus", list);
List<Integer> checkedKeys;
if (roleId.equals(AdminConstant.ROOT_ROLE_ID)) { // 超级管理员拥有所有的菜单权限
checkedKeys = (List<Integer>) list.stream().map(item -> ((TreeSelectVO) item).getId()).collect(Collectors.toList());
} else {
checkedKeys = iSysRoleMenuService.list(new LambdaQueryWrapper<SysRoleMenu>()
.eq(SysRoleMenu::getRoleId, roleId))
.stream()
.map(item -> item.getMenuId())
.collect(Collectors.toList());
}
map.put("checkedKeys", checkedKeys);
return Result.success(map);
}
} else { } else {
list = iSysMenuService.list(baseQuery); list = iSysMenuService.list(baseQuery);
} }
@ -56,7 +81,7 @@ public class SysMenuController {
@ApiOperation(value = "菜单详情", httpMethod = "GET") @ApiOperation(value = "菜单详情", httpMethod = "GET")
@ApiImplicitParam(name = "id", value = "菜单id", required = true, paramType = "path", dataType = "Integer") @ApiImplicitParam(name = "id", value = "菜单id", required = true, paramType = "path", dataType = "Integer")
@GetMapping("/{id}") @GetMapping("/{id}")
public Result detail(@PathVariable Long id) { public Result detail(@PathVariable Integer id) {
SysMenu sysMenu = iSysMenuService.getById(id); SysMenu sysMenu = iSysMenuService.getById(id);
return Result.success(sysMenu); return Result.success(sysMenu);
} }
@ -76,7 +101,7 @@ public class SysMenuController {
}) })
@PutMapping(value = "/{id}") @PutMapping(value = "/{id}")
public Result update( public Result update(
@PathVariable Long id, @PathVariable Integer id,
@RequestBody SysMenu sysMenu) { @RequestBody SysMenu sysMenu) {
sysMenu.setUpdateTime(new Date()); sysMenu.setUpdateTime(new Date());
boolean status = iSysMenuService.updateById(sysMenu); boolean status = iSysMenuService.updateById(sysMenu);
@ -101,7 +126,7 @@ public class SysMenuController {
LambdaUpdateWrapper<SysMenu> luw = new LambdaUpdateWrapper<SysMenu>().eq(SysMenu::getId, id); LambdaUpdateWrapper<SysMenu> luw = new LambdaUpdateWrapper<SysMenu>().eq(SysMenu::getId, id);
if (menu.getStatus() != null) { // 状态更新 if (menu.getStatus() != null) { // 状态更新
luw.set(SysMenu::getStatus, menu.getStatus()); luw.set(SysMenu::getStatus, menu.getStatus());
} else { } else {
return Result.success(); return Result.success();
} }
boolean update = iSysMenuService.update(luw); boolean update = iSysMenuService.update(luw);

View File

@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.admin.domain.entity.SysRole; import com.youlai.admin.domain.entity.SysRole;
import com.youlai.admin.domain.entity.SysRole;
import com.youlai.admin.service.ISysRoleService; import com.youlai.admin.service.ISysRoleService;
import com.youlai.common.result.PageResult; import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result; import com.youlai.common.result.Result;
@ -13,21 +12,19 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List; import java.util.List;
@Api(tags = "角色接口") @Api(tags = "角色接口")
@RestController @RestController
@RequestMapping("/roles") @RequestMapping("/roles")
@Slf4j @Slf4j
@AllArgsConstructor
public class SysRoleController { public class SysRoleController {
@Autowired
private ISysRoleService iSysRoleService; private ISysRoleService iSysRoleService;
@ApiOperation(value = "列表分页", httpMethod = "GET") @ApiOperation(value = "列表分页", httpMethod = "GET")
@ -44,7 +41,7 @@ public class SysRoleController {
.orderByDesc(SysRole::getCreateTime); .orderByDesc(SysRole::getCreateTime);
if (page != null && limit != null) { if (page != null && limit != null) {
Page<SysRole> result = iSysRoleService.page(new Page<>(page, limit) ,queryWrapper); Page<SysRole> result = iSysRoleService.page(new Page<>(page, limit), queryWrapper);
return PageResult.success(result.getRecords(), result.getTotal()); return PageResult.success(result.getRecords(), result.getTotal());
} else if (limit != null) { } else if (limit != null) {
queryWrapper.last("LIMIT " + limit); queryWrapper.last("LIMIT " + limit);
@ -56,38 +53,37 @@ public class SysRoleController {
@ApiOperation(value = "角色详情", httpMethod = "GET") @ApiOperation(value = "角色详情", httpMethod = "GET")
@ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Integer") @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Integer")
@GetMapping("/{id}") @GetMapping("/{id}")
public Result detail(@PathVariable Long id) { public Result detail(@PathVariable Integer id) {
SysRole sysRole = iSysRoleService.getById(id); SysRole sysRole = iSysRoleService.getById(id);
return Result.success(sysRole); return Result.success(sysRole);
} }
@ApiOperation(value = "新增角色", httpMethod = "POST") @ApiOperation(value = "新增角色", httpMethod = "POST")
@ApiImplicitParam(name = "sysRole", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysRole") @ApiImplicitParam(name = "role", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysRole")
@PostMapping @PostMapping
public Result add(@RequestBody SysRole sysRole) { public Result add(@RequestBody SysRole role) {
boolean status = iSysRoleService.save(sysRole); boolean status = iSysRoleService.add(role);
return Result.status(status); return Result.status(status);
} }
@ApiOperation(value = "修改角色", httpMethod = "PUT") @ApiOperation(value = "修改角色", httpMethod = "PUT")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Integer"), @ApiImplicitParam(name = "id", value = "角色id", required = true, paramType = "path", dataType = "Integer"),
@ApiImplicitParam(name = "sysRole", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysRole") @ApiImplicitParam(name = "role", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysRole")
}) })
@PutMapping(value = "/{id}") @PutMapping(value = "/{id}")
public Result update( public Result update(
@PathVariable Long id, @PathVariable Integer id,
@RequestBody SysRole sysRole) { @RequestBody SysRole role) {
sysRole.setUpdateTime(new Date()); boolean status = iSysRoleService.update(role);
boolean status = iSysRoleService.updateById(sysRole);
return Result.status(status); return Result.status(status);
} }
@ApiOperation(value = "删除角色", httpMethod = "DELETE") @ApiOperation(value = "删除角色", httpMethod = "DELETE")
@ApiImplicitParam(name = "ids[]", value = "id集合", required = true, paramType = "query", allowMultiple = true, dataType = "Integer") @ApiImplicitParam(name = "ids[]", value = "id集合", required = true, paramType = "query", allowMultiple = true, dataType = "Integer")
@DeleteMapping @DeleteMapping
public Result delete(@RequestParam("ids") List<Long> ids) { public Result delete(@RequestParam("ids") List<Integer> ids) {
boolean status = iSysRoleService.removeByIds(ids); boolean status = iSysRoleService.delete(ids);
return Result.status(status); return Result.status(status);
} }
@ -102,11 +98,11 @@ public class SysRoleController {
LambdaUpdateWrapper<SysRole> luw = new LambdaUpdateWrapper<SysRole>().eq(SysRole::getId, id); LambdaUpdateWrapper<SysRole> luw = new LambdaUpdateWrapper<SysRole>().eq(SysRole::getId, id);
if (role.getStatus() != null) { // 状态更新 if (role.getStatus() != null) { // 状态更新
luw.set(SysRole::getStatus, role.getStatus()); luw.set(SysRole::getStatus, role.getStatus());
} else { } else {
return Result.success(); return Result.error("未发生任何更新");
} }
boolean update = iSysRoleService.update(luw); boolean status = iSysRoleService.update(luw);
return Result.success(update); return Result.status(status);
} }
} }

View File

@ -51,42 +51,42 @@ public class SysUserController {
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer"), @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer"), @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Integer"),
@ApiImplicitParam(name = "sysUser", value = "用户信息", paramType = "query", dataType = "SysUser"), @ApiImplicitParam(name = "user", value = "用户信息", paramType = "query", dataType = "SysUser"),
}) })
@GetMapping @GetMapping
public Result list(Integer page, Integer limit, SysUser sysUser) { public Result list(Integer page, Integer limit, SysUser user) {
IPage<SysUser> result =iSysUserService.list(new Page<>(page, limit),sysUser); IPage<SysUser> result =iSysUserService.list(new Page<>(page, limit),user);
return PageResult.success(result.getRecords(), result.getTotal()); return PageResult.success(result.getRecords(), result.getTotal());
} }
@ApiOperation(value = "用户详情", httpMethod = "GET") @ApiOperation(value = "用户详情", httpMethod = "GET")
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Integer") @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Integer")
@GetMapping("/{id}") @GetMapping("/{id}")
public Result detail(@PathVariable Long id) { public Result detail(@PathVariable Integer id) {
SysUser sysUser = iSysUserService.getById(id); SysUser user = iSysUserService.getById(id);
return Result.success(sysUser); return Result.success(user);
} }
@ApiOperation(value = "新增用户", httpMethod = "POST") @ApiOperation(value = "新增用户", httpMethod = "POST")
@ApiImplicitParam(name = "sysUser", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysUser") @ApiImplicitParam(name = "user", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysUser")
@PostMapping @PostMapping
public Result add(@RequestBody SysUser sysUser) { public Result add(@RequestBody SysUser user) {
boolean status = iSysUserService.save(sysUser); boolean status = iSysUserService.save(user);
return Result.status(status); return Result.status(status);
} }
@ApiOperation(value = "修改用户", httpMethod = "PUT") @ApiOperation(value = "修改用户", httpMethod = "PUT")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Integer"), @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Integer"),
@ApiImplicitParam(name = "sysUser", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysUser") @ApiImplicitParam(name = "user", value = "实体JSON对象", required = true, paramType = "body", dataType = "SysUser")
}) })
@PutMapping(value = "/{id}") @PutMapping(value = "/{id}")
public Result update( public Result update(
@PathVariable Long id, @PathVariable Integer id,
@RequestBody SysUser sysUser) { @RequestBody SysUser user) {
sysUser.setUpdateTime(new Date()); user.setUpdateTime(new Date());
boolean status = iSysUserService.updateById(sysUser); boolean status = iSysUserService.updateById(user);
return Result.status(status); return Result.status(status);
} }
@ -102,14 +102,14 @@ public class SysUserController {
@ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "query", dataType = "String") @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "query", dataType = "String")
@GetMapping("/loadUserByUsername") @GetMapping("/loadUserByUsername")
public UserDTO loadUserByUsername(@RequestParam String username) { public UserDTO loadUserByUsername(@RequestParam String username) {
SysUser sysUser = iSysUserService.getOne(new LambdaQueryWrapper<SysUser>() SysUser user = iSysUserService.getOne(new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getUsername, username)); .eq(SysUser::getUsername, username));
UserDTO userDTO = new UserDTO(); UserDTO userDTO = new UserDTO();
if (sysUser != null) { if (user != null) {
BeanUtil.copyProperties(sysUser, userDTO); BeanUtil.copyProperties(user, userDTO);
List<Integer> roleIds = iSysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>() List<Integer> roleIds = iSysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getUserId, sysUser.getId()) .eq(SysUserRole::getUserId, user.getId())
).stream().map(item -> item.getRoleId()).collect(Collectors.toList()); ).stream().map(item -> item.getRoleId()).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(roleIds)) { if (CollectionUtil.isNotEmpty(roleIds)) {
List<String> roles = iSysRoleService.listByIds(roleIds).stream() List<String> roles = iSysRoleService.listByIds(roleIds).stream()
@ -127,11 +127,11 @@ public class SysUserController {
String payload = request.getHeader(AuthConstant.USER_TOKEN_HEADER); String payload = request.getHeader(AuthConstant.USER_TOKEN_HEADER);
JSONObject jsonObject = JSONUtil.parseObj(payload); JSONObject jsonObject = JSONUtil.parseObj(payload);
Integer id = jsonObject.getInt("id"); Integer id = jsonObject.getInt("id");
SysUser sysUser = iSysUserService.getById(id); SysUser user = iSysUserService.getById(id);
UserVO userVO = new UserVO(); UserVO userVO = new UserVO();
BeanUtil.copyProperties(sysUser, userVO); BeanUtil.copyProperties(user, userVO);
List<Integer> roleIds = iSysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>() List<Integer> roleIds = iSysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getUserId, sysUser.getId()) .eq(SysUserRole::getUserId, user.getId())
).stream().map(item -> item.getRoleId()).collect(Collectors.toList()); ).stream().map(item -> item.getRoleId()).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(roleIds)) { if (CollectionUtil.isNotEmpty(roleIds)) {
List<String> roles = iSysRoleService.listByIds(roleIds).stream() List<String> roles = iSysRoleService.listByIds(roleIds).stream()

View File

@ -1,10 +1,13 @@
package com.youlai.admin.domain.entity; package com.youlai.admin.domain.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.youlai.common.core.entity.BaseEntity; import com.youlai.common.core.entity.BaseEntity;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class SysRole extends BaseEntity { public class SysRole extends BaseEntity {
@ -23,5 +26,8 @@ public class SysRole extends BaseEntity {
private String remark; private String remark;
@TableField(exist = false)
private List<Integer> menuIds;
} }

View File

@ -4,5 +4,13 @@ package com.youlai.admin.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.admin.domain.entity.SysRole; import com.youlai.admin.domain.entity.SysRole;
import java.util.List;
public interface ISysRoleService extends IService<SysRole> { public interface ISysRoleService extends IService<SysRole> {
boolean update(SysRole role);
boolean delete(List<Integer> ids);
boolean add(SysRole role);
} }

View File

@ -1,12 +1,103 @@
package com.youlai.admin.service.impl; package com.youlai.admin.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.admin.domain.entity.SysRole; import com.youlai.admin.domain.entity.SysRole;
import com.youlai.admin.domain.entity.SysRoleMenu;
import com.youlai.admin.domain.entity.SysUserRole;
import com.youlai.admin.mapper.SysRoleMapper; import com.youlai.admin.mapper.SysRoleMapper;
import com.youlai.admin.service.ISysRoleMenuService;
import com.youlai.admin.service.ISysRoleService; import com.youlai.admin.service.ISysRoleService;
import com.youlai.admin.service.ISysUserRoleService;
import com.youlai.common.web.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service @Service
@AllArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService { public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
private ISysRoleMenuService iSysRoleMenuService;
private ISysUserRoleService iSysUserRoleService;
@Override
public boolean add(SysRole role) {
List<Integer> menuIds = role.getMenuIds();
List<SysRoleMenu> roleMenus = new ArrayList<>();
Optional.ofNullable(menuIds)
.orElse(new ArrayList<>())
.forEach(menuId -> {
SysRoleMenu roleMenu = new SysRoleMenu();
roleMenu.setRoleId(role.getId());
roleMenu.setMenuId(menuId);
roleMenus.add(roleMenu);
});
iSysRoleMenuService.saveBatch(roleMenus);
return this.save(role);
}
@Override
public boolean update(SysRole role) {
List<Integer> menuIds = iSysRoleMenuService.list(new LambdaQueryWrapper<SysRoleMenu>()
.eq(SysRoleMenu::getRoleId, role.getId()))
.stream()
.map(item -> item.getMenuId())
.collect(Collectors.toList()); // 数据库角色拥有菜单权限ID
List<Integer> updatedMenuIds = role.getMenuIds(); // 修改后的角色拥有菜单权限ID
// 删除的角色菜单ID集合
List<Integer> removeMenuIds = Optional.ofNullable(menuIds)
.orElse(new ArrayList<>())
.stream()
.filter(menuId -> !Optional.ofNullable(updatedMenuIds).orElse(new ArrayList<>()).contains(menuId))
.collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(removeMenuIds)) {
iSysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().in(SysRoleMenu::getMenuId,removeMenuIds));
}
// 新增的角色菜单ID集合
List<Integer> addMenuIds = Optional.ofNullable(updatedMenuIds)
.orElse(new ArrayList<>())
.stream()
.filter(menuId -> !Optional.ofNullable(menuIds).orElse(new ArrayList<>()).contains(menuId))
.collect(Collectors.toList());
List<SysRoleMenu> roleMenus = new ArrayList<>();
Optional.ofNullable(addMenuIds)
.orElse(new ArrayList<>())
.forEach(menuId -> {
SysRoleMenu roleMenu = new SysRoleMenu();
roleMenu.setRoleId(role.getId());
roleMenu.setMenuId(menuId);
roleMenus.add(roleMenu);
});
iSysRoleMenuService.saveBatch(roleMenus); // 修改角色菜单
return this.updateById(role);
}
@Override
public boolean delete(List<Integer> ids) {
Optional.ofNullable(ids).orElse(new ArrayList<>()).forEach(id -> {
int count = iSysUserRoleService.count(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, id));
if (count > 0) {
throw new BusinessException("该角色已分配用户,无法删除");
}
// 删除角色菜单
iSysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id));
});
return this.removeByIds(ids);
}
} }