完成资源模块的改造

pull/3/head
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())
// );
// }
}