完成资源模块的改造

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

@ -0,0 +1,33 @@
### /resource/create 成功
POST {{baseUrl}}/resource/create
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{accessToken}}
name=测试菜单&permission=resource:add&type=1&sort=1&pid=0&route=/resource/list&icon=test
### /admin/update 成功
POST {{baseUrl}}/resource/update
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{accessToken}}
id=61&name=测试菜单2&permission=resource:add&type=1&sort=1&pid=0&route=/resource/list&icon=test
### /resource/delete 成功
POST {{baseUrl}}/resource/delete
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{accessToken}}
resourceId=61
### /resource/get 成功
GET {{baseUrl}}/resource/get?resourceId=61
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{accessToken}}
### /resource/list 成功
GET {{baseUrl}}/resource/list?resourceIds=61,63
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer {{accessToken}}
###

View File

@ -0,0 +1,67 @@
package cn.iocoder.mall.managementweb.controller.permission;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.managementweb.controller.permission.dto.ResourceCreateDTO;
import cn.iocoder.mall.managementweb.controller.permission.dto.ResourceUpdateDTO;
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceVO;
import cn.iocoder.mall.managementweb.manager.permission.ResourceManager;
import cn.iocoder.mall.security.admin.core.context.AdminSecurityContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* 资源 Controller
*/
@RestController
@RequestMapping("/resource")
@Api(tags = "资源")
@Validated
public class ResourceController {
@Autowired
private ResourceManager resourceManager;
@PostMapping("/create")
@ApiOperation("创建资源")
public CommonResult<Integer> createResource(@Valid ResourceCreateDTO createDTO) {
return success(resourceManager.createResource(createDTO, AdminSecurityContextHolder.getAdminId()));
}
@PostMapping("/update")
@ApiOperation("更新资源")
public CommonResult<Boolean> updateResource(@Valid ResourceUpdateDTO updateDTO) {
resourceManager.updateResource(updateDTO);
return success(true);
}
@PostMapping("/delete")
@ApiOperation("删除资源")
@ApiImplicitParam(name = "resourceId", value = "资源编号", required = true)
public CommonResult<Boolean> deleteResource(@RequestParam("resourceId") Integer resourceId) {
resourceManager.deleteResource(resourceId);
return success(true);
}
@GetMapping("/get")
@ApiOperation("获得资源")
public CommonResult<ResourceVO> getResource(@RequestParam("resourceId") Integer resourceId) {
return success(resourceManager.getResource(resourceId));
}
@GetMapping("/list")
@ApiOperation("获得资源列表")
@ApiImplicitParam(name = "resourceId", value = "资源编号列表", required = true)
public CommonResult<List<ResourceVO>> getResources(@RequestParam("resourceIds") List<Integer> resourceIds) {
return success(resourceManager.listResource(resourceIds));
}
}

View File

@ -0,0 +1,36 @@
package cn.iocoder.mall.managementweb.controller.permission.dto;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@ApiModel("资源创建 DTO")
@Data
public class ResourceCreateDTO {
@ApiModelProperty(value = "菜单名", required = true, example = "商品管理")
@NotEmpty(message = "菜单名不能为空")
private String name;
@ApiModelProperty(value = "权限标识", example = "resource:add")
private String permission;
@ApiModelProperty(value = "资源类型", required = true, example = "1")
@NotNull(message = "资源类型不能为空")
@InEnum(value = ResourceTypeEnum.class, message = "资源类型必须是 {value}")
private Integer type;
@ApiModelProperty(value = "排序", required = true, example = "1")
@NotNull(message = "排序不能为空")
private Integer sort;
@ApiModelProperty(value = "父级资源编号", required = true, example = "1", notes = "如果无父资源,则值为 0")
@NotNull(message = "父级资源编号不能为空")
private Integer pid;
@ApiModelProperty(value = "前端路由", example = "/resource/list")
private String route;
@ApiModelProperty(value = "菜单图标", example = "add")
private String icon;
}

View File

@ -0,0 +1,39 @@
package cn.iocoder.mall.managementweb.controller.permission.dto;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@ApiModel("资源创建 DTO")
@Data
public class ResourceUpdateDTO {
@ApiModelProperty(value = "资源编号", required = true, example = "1")
@NotNull(message = "资源编号不能为空")
private Integer id;
@ApiModelProperty(value = "菜单名", required = true, example = "商品管理")
@NotEmpty(message = "菜单名不能为空")
private String name;
@ApiModelProperty(value = "权限标识", example = "resource:add")
private String permission;
@ApiModelProperty(value = "资源类型", required = true, example = "1")
@NotNull(message = "资源类型不能为空")
@InEnum(value = ResourceTypeEnum.class, message = "资源类型必须是 {value}")
private Integer type;
@ApiModelProperty(value = "排序", required = true, example = "1")
@NotNull(message = "排序不能为空")
private Integer sort;
@ApiModelProperty(value = "父级资源编号", required = true, example = "1", notes = "如果无父资源,则值为 0")
@NotNull(message = "父级资源编号不能为空")
private Integer pid;
@ApiModelProperty(value = "前端路由", example = "/resource/list")
private String route;
@ApiModelProperty(value = "菜单图标", example = "add")
private String icon;
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.mall.managementweb.controller.permission.vo;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
import lombok.*;
import io.swagger.annotations.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;
@ApiModel("资源 VO")
@Data
public class ResourceVO {
@ApiModelProperty(value = "资源编号", required = true, example = "1")
@NotNull(message = "资源编号不能为空")
private Integer id;
@ApiModelProperty(value = "菜单名", required = true, example = "商品管理")
@NotEmpty(message = "菜单名不能为空")
private String name;
@ApiModelProperty(value = "权限标识", example = "resource:add")
private String permission;
@ApiModelProperty(value = "资源类型", required = true, example = "1")
@NotNull(message = "资源类型不能为空")
@InEnum(value = ResourceTypeEnum.class, message = "资源类型必须是 {value}")
private Integer type;
@ApiModelProperty(value = "排序", required = true, example = "1")
@NotNull(message = "排序不能为空")
private Integer sort;
@ApiModelProperty(value = "父级资源编号", required = true, example = "1", notes = "如果无父资源,则值为 0")
@NotNull(message = "父级资源编号不能为空")
private Integer pid;
@ApiModelProperty(value = "前端路由", example = "/resource/list")
private String route;
@ApiModelProperty(value = "菜单图标", example = "add")
private String icon;
@ApiModelProperty(value = "添加时间", required = true)
private Date createTime;
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.mall.managementweb.convert.permission;
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceVO;
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceCreateDTO;
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceUpdateDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ResourceConvert {
ResourceConvert INSTANCE = Mappers.getMapper(ResourceConvert.class);
ResourceCreateDTO convert(cn.iocoder.mall.managementweb.controller.permission.dto.ResourceCreateDTO bean);
ResourceUpdateDTO convert(cn.iocoder.mall.managementweb.controller.permission.dto.ResourceUpdateDTO bean);
ResourceVO convert(cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO bean);
List<ResourceVO> convertList(List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO> list);
}

View File

@ -0,0 +1,79 @@
package cn.iocoder.mall.managementweb.manager.permission;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.managementweb.controller.permission.dto.ResourceCreateDTO;
import cn.iocoder.mall.managementweb.controller.permission.dto.ResourceUpdateDTO;
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceVO;
import cn.iocoder.mall.managementweb.convert.permission.ResourceConvert;
import cn.iocoder.mall.systemservice.rpc.permission.ResourceRpc;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 资源 Manager
*/
@Service
public class ResourceManager {
@Reference(version = "${dubbo.consumer.ResourceRpc.version}", validation = "false")
private ResourceRpc resourceRpc;
/**
* 创建资源
*
* @param createDTO 创建资源 DTO
* @return 资源
*/
public Integer createResource(ResourceCreateDTO createDTO, Integer createAdminId) {
CommonResult<Integer> createResourceResult = resourceRpc.createResource(ResourceConvert.INSTANCE.convert(createDTO)
.setCreateAdminId(createAdminId));
createResourceResult.checkError();
return createResourceResult.getData();
}
/**
* 更新资源
*
* @param updateDTO 更新资源 DTO
*/
public void updateResource(ResourceUpdateDTO updateDTO) {
CommonResult<Boolean> updateResourceResult = resourceRpc.updateResource(ResourceConvert.INSTANCE.convert(updateDTO));
updateResourceResult.checkError();
}
/**
* 删除资源
*
* @param resourceId 资源编号
*/
public void deleteResource(Integer resourceId) {
CommonResult<Boolean> deleteResourceResult = resourceRpc.deleteResource(resourceId);
deleteResourceResult.checkError();
}
/**
* 获得资源
*
* @param resourceId 资源编号
* @return 资源
*/
public ResourceVO getResource(Integer resourceId) {
CommonResult<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO> getResourceResult = resourceRpc.getResource(resourceId);
getResourceResult.checkError();
return ResourceConvert.INSTANCE.convert(getResourceResult.getData());
}
/**
* 获得资源列表
*
* @param resourceIds 资源编号列表
* @return 资源列表
*/
public List<ResourceVO> listResource(List<Integer> resourceIds) {
CommonResult<List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO>> listResourceResult = resourceRpc.listResource(resourceIds);
return ResourceConvert.INSTANCE.convertList(listResourceResult.getData());
}
}

View File

@ -30,6 +30,8 @@ dubbo:
version: 1.0.0
SystemLogRPC:
version: 1.0.0
ResourceRpc:
version: 1.0.0
# Swagger 配置项
swagger:

View File

@ -0,0 +1,53 @@
package cn.iocoder.mall.systemservice.rpc.permission;
import cn.iocoder.common.framework.vo.CommonResult;
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 java.util.List;
/**
* 资源 Rpc 接口
*/
public interface ResourceRpc {
/**
* 创建资源
*
* @param createDTO 创建资源 DTO
* @return 资源
*/
CommonResult<Integer> createResource(ResourceCreateDTO createDTO);
/**
* 更新资源
*
* @param updateDTO 更新资源 DTO
*/
CommonResult<Boolean> updateResource(ResourceUpdateDTO updateDTO);
/**
* 删除资源
*
* @param resourceId 资源编号
*/
CommonResult<Boolean> deleteResource(Integer resourceId);
/**
* 获得资源
*
* @param resourceId 资源编号
* @return 资源
*/
CommonResult<ResourceVO> getResource(Integer resourceId);
/**
* 获得资源列表
*
* @param resourceIds 资源编号列表
* @return 资源列表
*/
CommonResult<List<ResourceVO>> listResource(List<Integer> resourceIds);
}

View File

@ -0,0 +1,59 @@
package cn.iocoder.mall.systemservice.rpc.permission.dto;
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;
import java.io.Serializable;
/**
* 资源创建 DTO
*/
@Data
@Accessors(chain = true)
public class ResourceCreateDTO implements Serializable {
/**
* 菜单名
*/
@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;
}

View File

@ -1,50 +1,58 @@
package cn.iocoder.mall.system.biz.dto.authorization;
package cn.iocoder.mall.systemservice.rpc.permission.dto;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.system.biz.enums.authorization.ResourceTypeEnum;
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;
import java.io.Serializable;
/**
* 资源模块 - 更新资源 DTO
*/
* 资源更新 DTO
*/
@Data
@Accessors(chain = true)
public class ResourceUpdateDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
public class ResourceUpdateDTO implements Serializable {
/**
* 资源编号
*/
@NotNull(message = "资源编号不能为空")
private Integer id;
@NotNull(message = "类型不能为空")
/**
* 菜单名
*/
@NotEmpty(message = "菜单名不能为空")
private String name;
/**
* 权限标识
*/
private String permission;
/**
* 资源类型
*/
@NotNull(message = "资源类型不能为空")
@InEnum(value = ResourceTypeEnum.class, message = "资源类型必须是 {value}")
private Integer type;
@NotNull(message = "类型不能为空")
/**
* 排序
*/
@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

@ -0,0 +1,53 @@
package cn.iocoder.mall.systemservice.rpc.permission.vo;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* 资源 VO
*/
@Data
@Accessors(chain = true)
public class ResourceVO implements Serializable {
/**
* 资源编号
*/
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

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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()));
}
}

View File

@ -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));
}

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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) {

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -1,13 +1,10 @@
package cn.iocoder.mall.system.biz.bo.authorization;
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 ResourceBO {
@ -45,8 +42,12 @@ public class ResourceBO {
*/
private String icon;
/**
* 创建时间
* 添加时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
}

View File

@ -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;
}

View File

@ -1,47 +1,54 @@
package cn.iocoder.mall.system.biz.dto.authorization;
package cn.iocoder.mall.systemservice.service.permission.bo;
import cn.iocoder.common.framework.validator.InEnum;
import cn.iocoder.mall.system.biz.enums.authorization.ResourceTypeEnum;
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;
/**
* 资源模块 - 添加资源 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;
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;
/**
* 权限标识
*/
private String permission;
}

View File

@ -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;
}

View File

@ -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;
}

View File

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

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.system.biz.dto.authorization;
package cn.iocoder.mall.systemservice.service.permission.bo;
import lombok.Data;
import lombok.experimental.Accessors;
@ -7,21 +7,22 @@ import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 角色模块 - 修改角色 DTO
* 角色更新 BO
*/
@Data
@Accessors(chain = true)
public class RoleUpdateDTO {
@NotNull(message = "管理员编号不能为空")
private Integer adminId;
public class RoleUpdateBO {
/**
* 角色编号
*/
@NotNull(message = "角色编号不能为空")
private Integer id;
@NotEmpty(message = "角色名字不能为空")
/**
* 角色名
*/
@NotEmpty(message = "角色名不能为空")
private String name;
/**
* 角色编码
*/

View File

@ -39,3 +39,5 @@ dubbo:
version: 1.0.0
SystemLogRPC:
version: 1.0.0
ResourceRpc:
version: 1.0.0

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,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,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,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);
}
}
}

View File

@ -14,4 +14,18 @@ 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())
// );
// }
}