完成资源模块的改造

This commit is contained in:
YunaiV
2020-07-07 21:14:35 +08:00
parent 8af7b252aa
commit 4c4708cc8f
41 changed files with 1272 additions and 439 deletions

View File

@@ -1,52 +0,0 @@
package cn.iocoder.mall.system.biz.bo.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* 授权模块 - 资源信息 BO
*/
@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;
}

View File

@@ -16,15 +16,7 @@ public interface ResourceConvert {
ResourceConvert INSTANCE = Mappers.getMapper(ResourceConvert.class);
ResourceBO convert(ResourceDO bean);
@Mapping(source = "bean", target = "node")
ResourceTreeNodeBO convertTreeNode(ResourceDO bean);
List<ResourceBO> convertList(List<ResourceDO> beans);
ResourceDO convert(ResourceAddDTO bean);
ResourceDO convert(ResourceUpdateDTO bean);
}

View File

@@ -1,29 +0,0 @@
package cn.iocoder.mall.system.biz.dao.user;
import cn.iocoder.mall.mybatis.query.QueryWrapperX;
import cn.iocoder.mall.system.biz.dataobject.user.UserDO;
import cn.iocoder.mall.system.biz.dto.user.UserPageDTO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<UserDO> {
/**
* 根据条件分页查询用户列表
* @param userPageDTO
* @return
*/
default IPage<UserDO> selectUserPage(UserPageDTO userPageDTO) {
// TODO FROM 芋艿 to jwf1173看下 QueryWrapperX 噢,已经提供判空啦 [DONE]
return this.selectPage(new Page<>(userPageDTO.getPageNo(), userPageDTO.getPageSize()),
new QueryWrapperX<UserDO>()
.eq(StringUtils.isNotBlank(userPageDTO.getNickname()), "nickname", userPageDTO.getNickname())
.eq(null != userPageDTO.getStatus(), "status", userPageDTO.getStatus())
);
}
}

View File

@@ -1,47 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.system.biz.enums.authorization.ResourceTypeEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 资源模块 - 添加资源 DTO
*/
@Data
@Accessors(chain = true)
public class ResourceAddDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
@NotNull(message = "类型不能为空")
@InEnum(value = ResourceTypeEnum.class, message = "资源类型必须是 {value}")
private Integer type;
@NotNull(message = "类型不能为空")
private Integer sort;
@NotEmpty(message = "菜单名不能为空")
private String name;
@NotNull(message = "父级资源编号不能为空")
private Integer pid;
/**
* 前端路由
*/
private String route;
/**
* 图标
*/
private String icon;
/**
* 权限标识
*/
private String permission;
}

View File

@@ -1,23 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* 资源模块 - 删除资源 DTO
*/
@Data
@Accessors(chain = true)
public class ResourceDeleteDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
@ApiModelProperty(value = "资源编号", required = true, example = "1")
@NotNull(message = "资源编号不能为空")
private Integer id;
}

View File

@@ -1,50 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.system.biz.enums.authorization.ResourceTypeEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 资源模块 - 更新资源 DTO
*/
@Data
@Accessors(chain = true)
public class ResourceUpdateDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
@NotNull(message = "资源编号不能为空")
private Integer id;
@NotNull(message = "类型不能为空")
@InEnum(value = ResourceTypeEnum.class, message = "资源类型必须是 {value}")
private Integer type;
@NotNull(message = "类型不能为空")
private Integer sort;
@NotEmpty(message = "资源名字不能为空")
private String name;
@NotNull(message = "父级资源编号不能为空")
private Integer pid;
/**
* 前端路由
*/
private String route;
/**
* 图标
*/
private String icon;
/**
* 权限标识
*/
private String permission;
}

View File

@@ -1,27 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 角色模块 - 添加角色 DTO
*/
@Data
@Accessors(chain = true)
public class RoleAddDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
@NotEmpty(message = "角色名字不能为空")
private String name;
/**
* 角色编码
*/
private String code;
}

View File

@@ -1,21 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotNull;
/**
* 资源模块 - 删除资源 DTO
*/
@Data
@Accessors(chain = true)
public class RoleDeleteDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
@NotNull(message = "角色编号不能为空")
private Integer id;
}

View File

@@ -1,22 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Collection;
/**
* 角色模块 - 获得角色列表 DTO
*/
@Data
@Accessors(chain = true)
public class RoleGetListDTO {
/**
* 角色编号数组
*
* 如果传入空,则不进行角色编号的过滤
*/
private Collection<Integer> ids;
}

View File

@@ -1,21 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import cn.iocoder.common.framework.vo.PageParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* 角色模块 - 角色分页 DTO
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class RolePageDTO extends PageParam {
/**
* 角色名,模糊匹配
*/
private String name;
}

View File

@@ -1,30 +0,0 @@
package cn.iocoder.mall.system.biz.dto.authorization;
import lombok.Data;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 角色模块 - 修改角色 DTO
*/
@Data
@Accessors(chain = true)
public class RoleUpdateDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
@NotNull(message = "角色编号不能为空")
private Integer id;
@NotEmpty(message = "角色名字不能为空")
private String name;
/**
* 角色编码
*/
private String code;
}

View File

@@ -1,6 +0,0 @@
/**
* Spring 事件机制
*
* 不了解的胖友,可以阅读 http://www.iocoder.cn/Spring-Boot/Event/?onemall 文章
*/
package cn.iocoder.mall.system.biz.event;

View File

@@ -1,9 +1,10 @@
package cn.iocoder.mall.system.biz.service.authorization;
import cn.iocoder.common.framework.exception.ServiceException;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceBO;
import cn.iocoder.mall.system.biz.bo.authorization.ResourceTreeNodeBO;
import cn.iocoder.mall.system.biz.dto.authorization.*;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceCountDTO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceGetListDTO;
import cn.iocoder.mall.system.biz.dto.authorization.ResourceGetTreeDTO;
import java.util.Collection;
import java.util.List;
@@ -27,20 +28,4 @@ public interface ResourceService {
*/
List<ResourceTreeNodeBO> getResourceTree(ResourceGetTreeDTO getTreeDTO);
Integer addResource(ResourceAddDTO addDTO);
/**
* 更新资源。如果更新失败,则抛出 {@link ServiceException} 异常
*
* @param updateDTO 更新资源
*/
void updateResource(ResourceUpdateDTO updateDTO);
/**
* 删除资源。如果删除失败,则抛出 {@link ServiceException} 异常
*
* @param deleteDTO 删除资源
*/
void deleteResource(ResourceDeleteDTO deleteDTO);
}

View File

@@ -79,119 +79,4 @@ public class ResourceServiceImpl implements ResourceService {
.collect(Collectors.toList());
}
@Override
public Integer addResource(ResourceAddDTO addDTO) {
// 校验父资源存在
checkParentResource(addDTO.getPid(), null);
// 校验资源(自己)
checkResource(addDTO.getPid(), addDTO.getName(), null);
// 存储到数据库
ResourceDO resource = ResourceConvert.INSTANCE.convert(addDTO);
initResourceProperty(resource);
resource.setCreateTime(new Date());
resource.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
resourceMapper.insert(resource);
// TODO 操作日志
// 返回成功
return resource.getId();
}
@Override
public void updateResource(ResourceUpdateDTO updateDTO) {
// 校验更新的资源是否存在
if (resourceMapper.selectById(updateDTO.getId()) == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.RESOURCE_NOT_EXISTS);
}
// 校验父资源存在
checkParentResource(updateDTO.getPid(), updateDTO.getId());
// 校验资源(自己)
checkResource(updateDTO.getPid(), updateDTO.getName(), updateDTO.getId());
// 更新到数据库
ResourceDO resource = ResourceConvert.INSTANCE.convert(updateDTO);
initResourceProperty(resource);
resourceMapper.updateById(resource);
// TODO 操作日志
}
@Override
@Transactional
public void deleteResource(ResourceDeleteDTO deleteDTO) {
// 校验更新的资源是否存在
if (resourceMapper.selectById(deleteDTO.getId()) == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.RESOURCE_NOT_EXISTS);
}
// 校验是否还有子资源
if (resourceMapper.selectCountByPid(deleteDTO.getId()) > 0) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.RESOURCE_EXISTS_CHILDREN);
}
// 更新到数据库
resourceMapper.deleteById(deleteDTO.getId());
// 发布资源删除事件,方便清理关联表
eventPublisher.publishEvent(new ResourceDeleteEvent(this, deleteDTO.getId()));
}
/**
* 校验父资源是否合法
*
* 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(SystemErrorCodeEnum.RESOURCE_PARENT_ERROR);
}
ResourceDO resource = resourceMapper.selectById(pid);
if (resource == null) { // 父资源不存在
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.RESOURCE_PARENT_NOT_EXISTS);
}
if (!ResourceTypeEnum.MENU.getType().equals(resource.getType())) { // 父资源必须是菜单类型
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.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(SystemErrorCodeEnum.RESOURCE_NAME_DUPLICATE);
}
if (!resource.getId().equals(id)) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.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);
}
}
}

View File

@@ -31,16 +31,6 @@ public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper roleMapper;
@Override
public RoleBO getRole(Integer id) {
return RoleConvert.INSTANCE.convert(roleMapper.selectById(id));
}
@Override
public List<RoleBO> getRoleList(RoleGetListDTO getListDTO) {
return RoleConvert.INSTANCE.convertList(roleMapper.selectListByIds(getListDTO.getIds()));
}
@Override
public PageResult<RoleBO> getRolePage(RolePageDTO pageDTO) {
return RoleConvert.INSTANCE.convertPage(roleMapper.selectPage(pageDTO));
@@ -57,84 +47,4 @@ public class RoleServiceImpl implements RoleService {
return false;
}
@Override
public Integer addRole(RoleAddDTO roleAddDTO) {
// 校验角色
checkDuplicateRole(roleAddDTO.getName(), roleAddDTO.getCode(), null);
// 保存到数据库
RoleDO role = RoleConvert.INSTANCE.convert(roleAddDTO);
role.setType(RoleTypeEnum.CUSTOM.getType());
role.setCreateTime(new Date());
role.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
roleMapper.insert(role);
// TODO 插入操作日志
// 返回成功
return role.getId();
}
@Override
public void updateRole(RoleUpdateDTO roleUpdateDTO) {
// 校验角色是否存在
RoleDO roleDO = roleMapper.selectById(roleUpdateDTO.getId());
if (roleDO == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ROLE_NOT_EXISTS);
}
// 内置角色,不允许修改
if (RoleTypeEnum.SYSTEM.getType().equals(roleDO.getType())) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
}
// 校验角色的唯一字段是否重复
checkDuplicateRole(roleUpdateDTO.getName(), roleUpdateDTO.getCode(), roleUpdateDTO.getId());
// 更新到数据库
RoleDO updateRole = RoleConvert.INSTANCE.convert(roleUpdateDTO);
roleMapper.updateById(updateRole);
// TODO 插入操作日志
}
@Override
@Transactional
public void deleteRole(RoleDeleteDTO roleDeleteDTO) {
// 校验角色是否存在
RoleDO roleDO = roleMapper.selectById(roleDeleteDTO.getId());
if (roleDO == null) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ROLE_NOT_EXISTS);
}
// 内置角色,不允许删除
if (RoleTypeEnum.SYSTEM.getType().equals(roleDO.getType())) {
throw ServiceExceptionUtil.exception(SystemErrorCodeEnum.ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE);
}
// 更新到数据库,标记删除
roleMapper.deleteById(roleDeleteDTO.getId());
// 发布角色删除事件,方便清理关联表
eventPublisher.publishEvent(new ResourceDeleteEvent(this, roleDeleteDTO.getId()));
// TODO 插入操作日志
}
/**
* 校验角色的唯一字段是否重复
*
* 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(SystemErrorCodeEnum.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(SystemErrorCodeEnum.ROLE_CODE_DUPLICATE, name);
}
}
}