完成资源模块的改造
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package cn.iocoder.mall.systemservice.convert.permission;
|
||||
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.ResourceDO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceCreateDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceUpdateDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.ResourceCreateBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.ResourceUpdateBO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ResourceConvert {
|
||||
|
||||
ResourceConvert INSTANCE = Mappers.getMapper(ResourceConvert.class);
|
||||
|
||||
ResourceDO convert(ResourceCreateBO bean);
|
||||
|
||||
ResourceBO convert(ResourceDO bean);
|
||||
|
||||
ResourceDO convert(ResourceUpdateBO bean);
|
||||
|
||||
ResourceCreateBO convert(ResourceCreateDTO bean);
|
||||
|
||||
ResourceVO convert(ResourceBO bean);
|
||||
|
||||
ResourceUpdateBO convert(ResourceUpdateDTO bean);
|
||||
|
||||
List<ResourceBO> convertList(List<ResourceDO> list);
|
||||
List<ResourceVO> convertList02(List<ResourceBO> list);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.iocoder.mall.systemservice.convert.permission;
|
||||
|
||||
import cn.iocoder.common.framework.vo.PageResult;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleDO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RoleBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RoleCreateBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RoleUpdateBO;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface RoleConvert {
|
||||
|
||||
RoleConvert INSTANCE = Mappers.getMapper(RoleConvert.class);
|
||||
|
||||
RoleDO convert(RoleCreateBO bean);
|
||||
|
||||
RoleBO convert(RoleDO bean);
|
||||
|
||||
RoleDO convert(RoleUpdateBO bean);
|
||||
|
||||
List<RoleBO> convertList(List<RoleDO> list);
|
||||
|
||||
PageResult<RoleBO> convertPage(IPage<RoleDO> page);
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission;
|
||||
|
||||
import cn.iocoder.mall.mybatis.dataobject.DeletableDO;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.AdminDO;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
@@ -64,4 +65,11 @@ public class ResourceDO extends DeletableDO {
|
||||
*/
|
||||
private String icon;
|
||||
|
||||
/**
|
||||
* 创建管理员编号
|
||||
*
|
||||
* 外键 {@link AdminDO#getId()}
|
||||
*/
|
||||
private Integer createAdminId;
|
||||
|
||||
}
|
||||
|
||||
@@ -18,10 +18,10 @@ public interface AdminMapper extends BaseMapper<AdminDO> {
|
||||
);
|
||||
}
|
||||
|
||||
default IPage<AdminDO> selectPage(AdminPageBO adminPageBO) {
|
||||
return selectPage(new Page<>(adminPageBO.getPageNo(), adminPageBO.getPageSize()),
|
||||
new QueryWrapperX<AdminDO>().likeIfPresent("name", adminPageBO.getName())
|
||||
.eqIfPresent("department_id", adminPageBO.getDepartmentId()));
|
||||
default IPage<AdminDO> selectPage(AdminPageBO pageBO) {
|
||||
return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
|
||||
new QueryWrapperX<AdminDO>().likeIfPresent("name", pageBO.getName())
|
||||
.eqIfPresent("department_id", pageBO.getDepartmentId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,24 +2,24 @@ package cn.iocoder.mall.systemservice.dal.mysql.mapper.permission;
|
||||
|
||||
import cn.iocoder.mall.mybatis.query.QueryWrapperX;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleDO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RolePageBO;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface RoleMapper extends BaseMapper<RoleDO> {
|
||||
|
||||
// default IPage<RoleDO> selectPage(RolePageDTO rolePageDTO) {
|
||||
// return selectPage(new Page<>(rolePageDTO.getPageNo(), rolePageDTO.getPageSize()),
|
||||
// new QueryWrapperX<RoleDO>().likeIfPresent("name", rolePageDTO.getName()));
|
||||
// }
|
||||
|
||||
default List<RoleDO> selectListByIds(Collection<Integer> ids) {
|
||||
return selectList(new QueryWrapperX<RoleDO>().inIfPresent("id", ids));
|
||||
default IPage<RoleDO> selectPage(RolePageBO pageBO) {
|
||||
return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
|
||||
new QueryWrapperX<RoleDO>().likeIfPresent("name", pageBO.getName()));
|
||||
}
|
||||
|
||||
// default List<RoleDO> selectListByIds(Collection<Integer> ids) {
|
||||
// return selectList(new QueryWrapperX<RoleDO>().inIfPresent("id", ids));
|
||||
// }
|
||||
|
||||
default RoleDO selectByName(String name) {
|
||||
return selectOne(new QueryWrapperX<RoleDO>().eqIfPresent("name", name));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package cn.iocoder.mall.systemservice.manager.permission;
|
||||
|
||||
import cn.iocoder.mall.systemservice.convert.permission.ResourceConvert;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceCreateDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceUpdateDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.ResourceService;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 资源 Manager
|
||||
*/
|
||||
@Service
|
||||
public class ResourceManager {
|
||||
|
||||
@Autowired
|
||||
private ResourceService resourceService;
|
||||
|
||||
/**
|
||||
* 创建资源
|
||||
*
|
||||
* @param createDTO 创建资源 DTO
|
||||
* @return 资源
|
||||
*/
|
||||
public Integer createResource(ResourceCreateDTO createDTO) {
|
||||
ResourceBO resourceBO = resourceService.createResource(ResourceConvert.INSTANCE.convert(createDTO));
|
||||
return resourceBO.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新资源
|
||||
*
|
||||
* @param updateDTO 更新资源 DTO
|
||||
*/
|
||||
public void updateResource(ResourceUpdateDTO updateDTO) {
|
||||
resourceService.updateResource(ResourceConvert.INSTANCE.convert(updateDTO));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除资源
|
||||
*
|
||||
* @param resourceId 资源编号
|
||||
*/
|
||||
public void deleteResource(Integer resourceId) {
|
||||
resourceService.deleteResource(resourceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得资源
|
||||
*
|
||||
* @param resourceId 资源编号
|
||||
* @return 资源
|
||||
*/
|
||||
public ResourceVO getResource(Integer resourceId) {
|
||||
ResourceBO resourceBO = resourceService.getResource(resourceId);
|
||||
return ResourceConvert.INSTANCE.convert(resourceBO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得资源列表
|
||||
*
|
||||
* @param resourceIds 资源编号列表
|
||||
* @return 资源列表
|
||||
*/
|
||||
public List<ResourceVO> listResource(List<Integer> resourceIds) {
|
||||
List<ResourceBO> resourceBOs = resourceService.listResource(resourceIds);
|
||||
return ResourceConvert.INSTANCE.convertList02(resourceBOs);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package cn.iocoder.mall.systemservice.rpc.permission;
|
||||
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.systemservice.manager.permission.ResourceManager;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceCreateDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceUpdateDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO;
|
||||
import org.apache.dubbo.config.annotation.Service;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.common.framework.vo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* 资源 Rpc 实现类
|
||||
*/
|
||||
@Service(version = "${dubbo.provider.ResourceRpc.version}", validation = "false")
|
||||
public class ResourceRpcImpl implements ResourceRpc {
|
||||
|
||||
@Autowired
|
||||
private ResourceManager resourceManager;
|
||||
|
||||
@Override
|
||||
public CommonResult<Integer> createResource(ResourceCreateDTO createDTO) {
|
||||
return success(resourceManager.createResource(createDTO));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateResource(ResourceUpdateDTO updateDTO) {
|
||||
resourceManager.updateResource(updateDTO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> deleteResource(Integer resourceId) {
|
||||
resourceManager.deleteResource(resourceId);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<ResourceVO> getResource(Integer resourceId) {
|
||||
return success(resourceManager.getResource(resourceId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<ResourceVO>> listResource(List<Integer> resourceIds) {
|
||||
return success(resourceManager.listResource(resourceIds));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,14 @@ public class AdminService {
|
||||
@Autowired
|
||||
private AdminMapper adminMapper;
|
||||
|
||||
/**
|
||||
* 校验登陆的账号密码是否正确
|
||||
*
|
||||
* @param username 账号
|
||||
* @param password 密码
|
||||
* @param ip 登陆 IP
|
||||
* @return 管理员信息
|
||||
*/
|
||||
public AdminBO verifyPassword(String username, String password, String ip) {
|
||||
AdminDO adminDO = adminMapper.selectByUsername(username);
|
||||
if (adminDO == null) {
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission;
|
||||
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.mall.systemservice.convert.permission.ResourceConvert;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.ResourceDO;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.ResourceMapper;
|
||||
import cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceIdEnum;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.ResourceCreateBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.ResourceUpdateBO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*;
|
||||
|
||||
/**
|
||||
* 资源 Service
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
public class ResourceService {
|
||||
|
||||
@Autowired
|
||||
private ResourceMapper resourceMapper;
|
||||
|
||||
/**
|
||||
* 创建资源
|
||||
*
|
||||
* @param createBO 创建资源 BO
|
||||
* @return 资源
|
||||
*/
|
||||
public ResourceBO createResource(@Valid ResourceCreateBO createBO) {
|
||||
// 校验父资源存在
|
||||
checkParentResource(createBO.getPid(), null);
|
||||
// 校验资源(自己)
|
||||
checkResource(createBO.getPid(), createBO.getName(), null);
|
||||
// 插入数据库
|
||||
ResourceDO resourceDO = ResourceConvert.INSTANCE.convert(createBO);
|
||||
initResourceProperty(resourceDO);
|
||||
resourceMapper.insert(resourceDO);
|
||||
// 返回
|
||||
return ResourceConvert.INSTANCE.convert(resourceDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新资源
|
||||
*
|
||||
* @param updateBO 更新资源 BO
|
||||
*/
|
||||
public void updateResource(@Valid ResourceUpdateBO updateBO) {
|
||||
// 校验更新的资源是否存在
|
||||
if (resourceMapper.selectById(updateBO.getId()) == null) {
|
||||
throw ServiceExceptionUtil.exception(RESOURCE_NOT_EXISTS);
|
||||
}
|
||||
// 校验父资源存在
|
||||
checkParentResource(updateBO.getPid(), updateBO.getId());
|
||||
// 校验资源(自己)
|
||||
checkResource(updateBO.getPid(), updateBO.getName(), updateBO.getId());
|
||||
// 更新到数据库
|
||||
ResourceDO updateObject = ResourceConvert.INSTANCE.convert(updateBO);
|
||||
initResourceProperty(updateObject);
|
||||
resourceMapper.updateById(updateObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除资源
|
||||
*
|
||||
* @param resourceId 资源编号
|
||||
*/
|
||||
public void deleteResource(Integer resourceId) {
|
||||
// 校验更新的资源是否存在
|
||||
if (resourceMapper.selectById(resourceId) == null) {
|
||||
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.RESOURCE_NOT_EXISTS);
|
||||
}
|
||||
// 校验是否还有子资源
|
||||
if (resourceMapper.selectCountByPid(resourceId) > 0) {
|
||||
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.RESOURCE_EXISTS_CHILDREN);
|
||||
}
|
||||
// 校验删除的资源是否存在
|
||||
if (resourceMapper.selectById(resourceId) == null) {
|
||||
throw ServiceExceptionUtil.exception(RESOURCE_NOT_EXISTS);
|
||||
}
|
||||
// 标记删除
|
||||
resourceMapper.deleteById(resourceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得资源
|
||||
*
|
||||
* @param resourceId 资源编号
|
||||
* @return 资源
|
||||
*/
|
||||
public ResourceBO getResource(Integer resourceId) {
|
||||
ResourceDO resourceDO = resourceMapper.selectById(resourceId);
|
||||
return ResourceConvert.INSTANCE.convert(resourceDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得资源列表
|
||||
*
|
||||
* @param resourceIds 资源编号列表
|
||||
* @return 资源列表
|
||||
*/
|
||||
public List<ResourceBO> listResource(List<Integer> resourceIds) {
|
||||
List<ResourceDO> resourceDOs = resourceMapper.selectBatchIds(resourceIds);
|
||||
return ResourceConvert.INSTANCE.convertList(resourceDOs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验父资源是否合法
|
||||
*
|
||||
* 1. 不能舌质红自己为父资源
|
||||
* 2. 父资源不存在
|
||||
* 3. 父资源必须是 {@link ResourceTypeEnum#MENU} 菜单类型
|
||||
*
|
||||
* @param pid 父资源编号
|
||||
* @param childId 当前资源编号
|
||||
*/
|
||||
private void checkParentResource(Integer pid, Integer childId) {
|
||||
if (pid == null || ResourceIdEnum.ROOT.getId().equals(pid)) {
|
||||
return;
|
||||
}
|
||||
// 不能设置自己为父资源
|
||||
if (pid.equals(childId)) {
|
||||
throw ServiceExceptionUtil.exception(RESOURCE_PARENT_ERROR);
|
||||
}
|
||||
ResourceDO resource = resourceMapper.selectById(pid);
|
||||
// 父资源不存在
|
||||
if (resource == null) {
|
||||
throw ServiceExceptionUtil.exception(RESOURCE_PARENT_NOT_EXISTS);
|
||||
}
|
||||
// 父资源必须是菜单类型
|
||||
if (!ResourceTypeEnum.MENU.getType().equals(resource.getType())) {
|
||||
throw ServiceExceptionUtil.exception(RESOURCE_PARENT_NOT_MENU);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验资源是否合法
|
||||
*
|
||||
* 1. 校验相同父资源编号下,是否存在相同的资源名
|
||||
*
|
||||
* @param name 资源名字
|
||||
* @param pid 父资源编号
|
||||
* @param id 资源编号
|
||||
*/
|
||||
private void checkResource(Integer pid, String name, Integer id) {
|
||||
ResourceDO resource = resourceMapper.selectByPidAndName(pid, name);
|
||||
if (resource == null) {
|
||||
return;
|
||||
}
|
||||
// 如果 id 为空,说明不用比较是否为相同 id 的资源
|
||||
if (id == null) {
|
||||
throw ServiceExceptionUtil.exception(RESOURCE_NAME_DUPLICATE);
|
||||
}
|
||||
if (!resource.getId().equals(id)) {
|
||||
throw ServiceExceptionUtil.exception(RESOURCE_NAME_DUPLICATE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化资源的通用属性。
|
||||
*
|
||||
* 例如说,只有菜单类型的资源,才设置 icon
|
||||
*
|
||||
* @param resource 资源
|
||||
*/
|
||||
private void initResourceProperty(ResourceDO resource) {
|
||||
// 初始化资源为按钮类型时,无需 route 和 icon 属性
|
||||
if (ResourceTypeEnum.BUTTON.getType().equals(resource.getType())) {
|
||||
resource.setRoute(null);
|
||||
resource.setIcon(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission;
|
||||
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.PageResult;
|
||||
import cn.iocoder.mall.systemservice.convert.permission.RoleConvert;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleDO;
|
||||
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleMapper;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.RoleTypeEnum;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RoleBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RoleCreateBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RolePageBO;
|
||||
import cn.iocoder.mall.systemservice.service.permission.bo.RoleUpdateBO;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*;
|
||||
|
||||
/**
|
||||
* 角色 Service
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
public class RoleService {
|
||||
|
||||
@Autowired
|
||||
private RoleMapper roleMapper;
|
||||
|
||||
/**
|
||||
* 创建角色
|
||||
*
|
||||
* @param createBO 创建角色 BO
|
||||
* @return 角色
|
||||
*/
|
||||
public RoleBO createRole(@Valid RoleCreateBO createBO) {
|
||||
// 校验角色
|
||||
checkDuplicateRole(createBO.getName(), createBO.getCode(), null);
|
||||
// 插入到数据库
|
||||
RoleDO roleDO = RoleConvert.INSTANCE.convert(createBO);
|
||||
roleMapper.insert(roleDO);
|
||||
// 返回
|
||||
return RoleConvert.INSTANCE.convert(roleDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新角色
|
||||
*
|
||||
* @param updateBO 更新角色 BO
|
||||
*/
|
||||
public void updateRole(@Valid RoleUpdateBO updateBO) {
|
||||
// 校验更新的角色是否存在
|
||||
RoleDO roleDO = roleMapper.selectById(updateBO.getId());
|
||||
if (roleMapper.selectById(updateBO.getId()) == null) {
|
||||
throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS);
|
||||
}
|
||||
// 内置角色,不允许修改
|
||||
if (RoleTypeEnum.SYSTEM.getType().equals(roleDO.getType())) {
|
||||
throw ServiceExceptionUtil.exception(ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
|
||||
}
|
||||
// 校验角色的唯一字段是否重复
|
||||
checkDuplicateRole(updateBO.getName(), updateBO.getCode(), updateBO.getId());
|
||||
// 更新到数据库
|
||||
RoleDO updateObject = RoleConvert.INSTANCE.convert(updateBO);
|
||||
roleMapper.updateById(updateObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*
|
||||
* @param roleId 角色编号
|
||||
*/
|
||||
public void deleteRole(Integer roleId) {
|
||||
// 校验删除的角色是否存在
|
||||
RoleDO roleDO = roleMapper.selectById(roleId);
|
||||
if (roleMapper.selectById(roleId) == null) {
|
||||
throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS);
|
||||
}
|
||||
// 内置角色,不允许删除
|
||||
if (RoleTypeEnum.SYSTEM.getType().equals(roleDO.getType())) {
|
||||
throw ServiceExceptionUtil.exception(ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE);
|
||||
}
|
||||
// 标记删除
|
||||
roleMapper.deleteById(roleId);
|
||||
// // 发布角色删除事件,方便清理关联表 TODO 芋艿,需要实现
|
||||
// eventPublisher.publishEvent(new ResourceDeleteEvent(this, roleDeleteDTO.getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得角色
|
||||
*
|
||||
* @param roleId 角色编号
|
||||
* @return 角色
|
||||
*/
|
||||
public RoleBO getRole(Integer roleId) {
|
||||
RoleDO roleDO = roleMapper.selectById(roleId);
|
||||
return RoleConvert.INSTANCE.convert(roleDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得角色列表
|
||||
*
|
||||
* @param roleIds 角色编号列表
|
||||
* @return 角色列表
|
||||
*/
|
||||
public List<RoleBO> listRole(List<Integer> roleIds) {
|
||||
List<RoleDO> roleDOs = roleMapper.selectBatchIds(roleIds);
|
||||
return RoleConvert.INSTANCE.convertList(roleDOs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得角色分页
|
||||
*
|
||||
* @param pageBO 角色分页查询
|
||||
* @return 角色分页结果
|
||||
*/
|
||||
public PageResult<RoleBO> pageRole(RolePageBO pageBO) {
|
||||
IPage<RoleDO> roleDOPage = roleMapper.selectPage(pageBO);
|
||||
return RoleConvert.INSTANCE.convertPage(roleDOPage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验角色的唯一字段是否重复
|
||||
*
|
||||
* 1. 是否存在相同名字的角色
|
||||
* 2. 是否存在相同编码的角色
|
||||
*
|
||||
* @param name 角色名字
|
||||
* @param code 角色额编码
|
||||
* @param id 角色编号
|
||||
*/
|
||||
private void checkDuplicateRole(String name, String code, Integer id) {
|
||||
// 1. 该 name 名字被其它角色所使用
|
||||
RoleDO role = roleMapper.selectByName(name);
|
||||
if (role != null && !role.getId().equals(id)) {
|
||||
throw ServiceExceptionUtil.exception(ROLE_NAME_DUPLICATE, name);
|
||||
}
|
||||
// 2. 是否存在相同编码的角色
|
||||
if (!StringUtils.hasText(code)) {
|
||||
return;
|
||||
}
|
||||
// 该 code 编码被其它角色所使用
|
||||
role = roleMapper.selectByCode(code);
|
||||
if (role != null && !role.getId().equals(id)) {
|
||||
throw ServiceExceptionUtil.exception(ROLE_CODE_DUPLICATE, name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission.bo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ResourceBO {
|
||||
|
||||
/**
|
||||
* 资源编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 菜单名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 权限标识
|
||||
*/
|
||||
private String permission;
|
||||
/**
|
||||
* 资源类型
|
||||
*/
|
||||
private Integer type;
|
||||
/**
|
||||
* 排序
|
||||
*/
|
||||
private Integer sort;
|
||||
/**
|
||||
* 父级资源编号
|
||||
*/
|
||||
private Integer pid;
|
||||
/**
|
||||
* 前端路由
|
||||
*/
|
||||
private String route;
|
||||
/**
|
||||
* 菜单图标
|
||||
*/
|
||||
private String icon;
|
||||
/**
|
||||
* 添加时间
|
||||
*/
|
||||
private Date createTime;
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission.bo;
|
||||
|
||||
import cn.iocoder.common.framework.validator.InEnum;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 资源创建 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ResourceCreateBO {
|
||||
|
||||
/**
|
||||
* 菜单名
|
||||
*/
|
||||
@NotEmpty(message = "菜单名不能为空")
|
||||
private String name;
|
||||
/**
|
||||
* 权限标识
|
||||
*/
|
||||
private String permission;
|
||||
/**
|
||||
* 资源类型
|
||||
*/
|
||||
@NotNull(message = "资源类型不能为空")
|
||||
@InEnum(value = ResourceTypeEnum.class, message = "资源类型必须是 {value}")
|
||||
private Integer type;
|
||||
/**
|
||||
* 排序
|
||||
*/
|
||||
@NotNull(message = "排序不能为空")
|
||||
private Integer sort;
|
||||
/**
|
||||
* 父级资源编号
|
||||
*/
|
||||
@NotNull(message = "父级资源编号不能为空")
|
||||
private Integer pid;
|
||||
/**
|
||||
* 前端路由
|
||||
*/
|
||||
private String route;
|
||||
/**
|
||||
* 菜单图标
|
||||
*/
|
||||
private String icon;
|
||||
|
||||
/**
|
||||
* 创建管理员编号
|
||||
*/
|
||||
@NotNull(message = "创建管理员编号不能为空")
|
||||
private Integer createAdminId;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission.bo;
|
||||
|
||||
import cn.iocoder.common.framework.validator.InEnum;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class ResourceUpdateBO {
|
||||
|
||||
/**
|
||||
* 菜单编号
|
||||
*/
|
||||
@NotNull(message = "菜单编号不能为空")
|
||||
private Integer id;
|
||||
/**
|
||||
* 菜单名
|
||||
*/
|
||||
@NotEmpty(message = "菜单名不能为空")
|
||||
private String name;
|
||||
/**
|
||||
* 权限标识
|
||||
*/
|
||||
private String permission;
|
||||
/**
|
||||
* 资源类型
|
||||
*/
|
||||
@NotNull(message = "资源类型不能为空")
|
||||
@InEnum(value = ResourceTypeEnum.class, message = "资源类型必须是 {value}")
|
||||
private Integer type;
|
||||
/**
|
||||
* 排序
|
||||
*/
|
||||
@NotNull(message = "排序不能为空")
|
||||
private Integer sort;
|
||||
/**
|
||||
* 父级资源编号
|
||||
*/
|
||||
@NotNull(message = "父级资源编号不能为空")
|
||||
private Integer pid;
|
||||
/**
|
||||
* 前端路由
|
||||
*/
|
||||
private String route;
|
||||
/**
|
||||
* 菜单图标
|
||||
*/
|
||||
private String icon;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission.bo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 角色 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class RoleBO {
|
||||
|
||||
/**
|
||||
* 角色编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 角色名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 角色编码
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* 角色类型
|
||||
*/
|
||||
private Integer type;
|
||||
/**
|
||||
* 创建管理员编号
|
||||
*/
|
||||
private Integer createAdminId;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission.bo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 角色创建 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class RoleCreateBO {
|
||||
|
||||
/**
|
||||
* 角色名
|
||||
*/
|
||||
@NotEmpty(message = "角色名不能为空")
|
||||
private String name;
|
||||
/**
|
||||
* 角色编码
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* 创建管理员编号
|
||||
*/
|
||||
@NotNull(message = "创建管理员编号不能为空")
|
||||
private Integer createAdminId;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission.bo;
|
||||
|
||||
import cn.iocoder.common.framework.vo.PageParam;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* 角色分页 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class RolePageBO extends PageParam {
|
||||
|
||||
/**
|
||||
* 角色名,模糊匹配
|
||||
*/
|
||||
private String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.iocoder.mall.systemservice.service.permission.bo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 角色更新 BO
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class RoleUpdateBO {
|
||||
|
||||
/**
|
||||
* 角色编号
|
||||
*/
|
||||
@NotNull(message = "角色编号不能为空")
|
||||
private Integer id;
|
||||
/**
|
||||
* 角色名
|
||||
*/
|
||||
@NotEmpty(message = "角色名不能为空")
|
||||
private String name;
|
||||
/**
|
||||
* 角色编码
|
||||
*/
|
||||
private String code;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user