部门模块的迁移

This commit is contained in:
YunaiV
2020-07-14 19:31:27 +08:00
parent 244f248ad5
commit 846510c309
47 changed files with 1203 additions and 333 deletions

View File

@@ -0,0 +1,35 @@
package cn.iocoder.mall.systemservice.convert.admin;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.DepartmentDO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.DepartmentCreateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.DepartmentUpdateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.vo.DepartmentVO;
import cn.iocoder.mall.systemservice.service.admin.bo.DepartmentBO;
import cn.iocoder.mall.systemservice.service.admin.bo.DepartmentCreateBO;
import cn.iocoder.mall.systemservice.service.admin.bo.DepartmentUpdateBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface DepartmentConvert {
DepartmentConvert INSTANCE = Mappers.getMapper(DepartmentConvert.class);
DepartmentDO convert(DepartmentCreateBO bean);
DepartmentBO convert(DepartmentDO bean);
DepartmentDO convert(DepartmentUpdateBO bean);
DepartmentUpdateBO convert(DepartmentUpdateDTO bean);
DepartmentCreateBO convert(DepartmentCreateDTO bean);
DepartmentVO convert(DepartmentBO bean);
List<DepartmentBO> convertList(List<DepartmentDO> list);
List<DepartmentVO> convertList02(List<DepartmentBO> list);
}

View File

@@ -18,6 +18,12 @@ public interface AdminMapper extends BaseMapper<AdminDO> {
);
}
default Integer selectCountByDepartmentId(Integer departmentId) {
return selectCount(new QueryWrapper<AdminDO>()
.eq("department_id", departmentId)
);
}
default IPage<AdminDO> selectPage(AdminPageBO pageBO) {
return selectPage(new Page<>(pageBO.getPageNo(), pageBO.getPageSize()),
new QueryWrapperX<AdminDO>().likeIfPresent("name", pageBO.getName())

View File

@@ -0,0 +1,16 @@
package cn.iocoder.mall.systemservice.dal.mysql.mapper.admin;
import cn.iocoder.mall.mybatis.query.QueryWrapperX;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.DepartmentDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface DepartmentMapper extends BaseMapper<DepartmentDO> {
default DepartmentDO selectByPidAndName(Integer pid, String name) {
return selectOne(new QueryWrapperX<DepartmentDO>().eqIfPresent("pid", pid)
.eqIfPresent("name", name));
}
}

View File

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
@Repository
@@ -19,6 +20,10 @@ public interface AdminRoleMapper extends BaseMapper<AdminRoleDO> {
return selectList(new QueryWrapper<AdminRoleDO>().eq("admin_id", adminId));
}
default List<AdminRoleDO> selectListByAdminIds(Collection<Integer> adminIds) {
return selectList(new QueryWrapper<AdminRoleDO>().in("admin_id", adminIds));
}
default int deleteByAdminId(Integer adminId) {
return delete(new QueryWrapper<AdminRoleDO>().eq("admin_id", adminId));
}

View File

@@ -0,0 +1,85 @@
package cn.iocoder.mall.systemservice.manager.admin;
import cn.iocoder.mall.systemservice.convert.admin.DepartmentConvert;
import cn.iocoder.mall.systemservice.rpc.admin.dto.DepartmentCreateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.DepartmentUpdateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.vo.DepartmentVO;
import cn.iocoder.mall.systemservice.service.admin.DepartmentService;
import cn.iocoder.mall.systemservice.service.admin.bo.DepartmentBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
/**
* 部门 Manager
*/
@Service
public class DepartmentManager {
@Autowired
private DepartmentService departmentService;
/**
* 创建部门
*
* @param createDTO 创建部门 DTO
* @return 部门
*/
public Integer createDepartment(DepartmentCreateDTO createDTO) {
DepartmentBO departmentBO = departmentService.createDepartment(DepartmentConvert.INSTANCE.convert(createDTO));
return departmentBO.getId();
}
/**
* 更新部门
*
* @param updateDTO 更新部门 DTO
*/
public void updateDepartment(DepartmentUpdateDTO updateDTO) {
departmentService.updateDepartment(DepartmentConvert.INSTANCE.convert(updateDTO));
}
/**
* 删除部门
*
* @param departmentId 部门编号
*/
public void deleteDepartment(Integer departmentId) {
departmentService.deleteDepartment(departmentId);
}
/**
* 获得部门
*
* @param departmentId 部门编号
* @return 部门
*/
public DepartmentVO getDepartment(Integer departmentId) {
DepartmentBO departmentBO = departmentService.getDepartment(departmentId);
return DepartmentConvert.INSTANCE.convert(departmentBO);
}
/**
* 获得部门列表
*
* @param departmentIds 部门编号列表
* @return 部门列表
*/
public List<DepartmentVO> listDepartments(Collection<Integer> departmentIds) {
List<DepartmentBO> departmentBOs = departmentService.listDepartments(departmentIds);
return DepartmentConvert.INSTANCE.convertList02(departmentBOs);
}
/**
* 获得部门全列表
*
* @return 资源列表
*/
public List<DepartmentVO> listDepartments() {
List<DepartmentBO> departmentBOs = departmentService.listDepartments();
return DepartmentConvert.INSTANCE.convertList02(departmentBOs);
}
}

View File

@@ -13,7 +13,9 @@ import cn.iocoder.mall.systemservice.service.permission.bo.ResourceBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.AUTHORIZATION_PERMISSION_DENY;
@@ -65,6 +67,17 @@ public class PermissionManager {
return permissionService.listAdminRoleIds(adminId);
}
/**
* 获得每个管理员拥有的角色编号
* 返回的结果key 为管理员编号
*
* @param adminIds 管理员编号列表
* @return 每个管理员拥有的角色编号
*/
public Map<Integer, Set<Integer>> mapAdminRoleIds(Collection<Integer> adminIds) {
return permissionService.mapAdminRoleIds(adminIds);
}
/**
* 赋予管理员角色
*

View File

@@ -11,6 +11,7 @@ import cn.iocoder.mall.systemservice.service.permission.bo.RoleBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@@ -79,7 +80,7 @@ public class RoleManager {
* @param roleIds 角色编号列表
* @return 角色列表
*/
public List<RoleVO> listRoles(List<Integer> roleIds) {
public List<RoleVO> listRoles(Collection<Integer> roleIds) {
List<RoleBO> roleBOs = roleService.listRole(roleIds);
return RoleConvert.INSTANCE.convertList02(roleBOs);
}

View File

@@ -0,0 +1,57 @@
package cn.iocoder.mall.systemservice.rpc.admin;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.manager.admin.DepartmentManager;
import cn.iocoder.mall.systemservice.rpc.admin.dto.DepartmentCreateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.dto.DepartmentUpdateDTO;
import cn.iocoder.mall.systemservice.rpc.admin.vo.DepartmentVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* 部门 Rpc 实现类
*/
@Service(version = "${dubbo.provider.DepartmentRpc.version}", validation = "false")
public class DepartmentRpcImpl implements DepartmentRpc {
@Autowired
private DepartmentManager departmentManager;
@Override
public CommonResult<Integer> createDepartment(DepartmentCreateDTO createDTO) {
return success(departmentManager.createDepartment(createDTO));
}
@Override
public CommonResult<Boolean> updateDepartment(DepartmentUpdateDTO updateDTO) {
departmentManager.updateDepartment(updateDTO);
return success(true);
}
@Override
public CommonResult<Boolean> deleteDepartment(Integer departmentId) {
departmentManager.deleteDepartment(departmentId);
return success(true);
}
@Override
public CommonResult<DepartmentVO> getDepartment(Integer departmentId) {
return success(departmentManager.getDepartment(departmentId));
}
@Override
public CommonResult<List<DepartmentVO>> listDepartments(Collection<Integer> departmentIds) {
return success(departmentManager.listDepartments(departmentIds));
}
@Override
public CommonResult<List<DepartmentVO>> listDepartments() {
return success(departmentManager.listDepartments());
}
}

View File

@@ -8,6 +8,8 @@ import cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionCheckDTO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@@ -37,6 +39,11 @@ public class PermissionRpcImpl implements PermissionRpc {
return success(permissionManager.listAdminRoleIds(adminId));
}
@Override
public CommonResult<Map<Integer, Set<Integer>>> mapAdminRoleIds(Collection<Integer> adminIds) {
return success(permissionManager.mapAdminRoleIds(adminIds));
}
@Override
public CommonResult<Boolean> assignAdminRole(PermissionAssignAdminRoleDTO assignAdminRoleDTO) {
permissionManager.assignAdminRole(assignAdminRoleDTO);

View File

@@ -10,6 +10,7 @@ import cn.iocoder.mall.systemservice.rpc.permission.vo.RoleVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@@ -52,7 +53,7 @@ public class RoleRpcImpl implements RoleRpc {
}
@Override
public CommonResult<List<RoleVO>> listRoles(List<Integer> roleIds) {
public CommonResult<List<RoleVO>> listRoles(Collection<Integer> roleIds) {
return success(roleManager.listRoles(roleIds));
}

View File

@@ -0,0 +1,168 @@
package cn.iocoder.mall.systemservice.service.admin;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.mall.systemservice.convert.admin.DepartmentConvert;
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.admin.DepartmentDO;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.admin.AdminMapper;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.admin.DepartmentMapper;
import cn.iocoder.mall.systemservice.enums.admin.DepartmentIdEnum;
import cn.iocoder.mall.systemservice.service.admin.bo.DepartmentBO;
import cn.iocoder.mall.systemservice.service.admin.bo.DepartmentCreateBO;
import cn.iocoder.mall.systemservice.service.admin.bo.DepartmentUpdateBO;
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.Collection;
import java.util.List;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*;
/**
* 部门 Service
*/
@Service
@Validated
public class DepartmentService {
@Autowired
private DepartmentMapper departmentMapper;
@Autowired
private AdminMapper adminMapper;
/**
* 创建部门
*
* @param createBO 创建部门 BO
* @return 部门
*/
public DepartmentBO createDepartment(@Valid DepartmentCreateBO createBO) {
// 校验父部门存在
checkParentDepartment(createBO.getPid(), null);
// 校验部门(自己)
checkDepartment(createBO.getPid(), createBO.getName(), null);
// 插入到数据库
DepartmentDO departmentDO = DepartmentConvert.INSTANCE.convert(createBO);
departmentMapper.insert(departmentDO);
// 返回
return DepartmentConvert.INSTANCE.convert(departmentDO);
}
/**
* 更新部门
*
* @param updateBO 更新部门 BO
*/
public void updateDepartment(@Valid DepartmentUpdateBO updateBO) {
// 校验更新的部门是否存在
if (departmentMapper.selectById(updateBO.getId()) == null) {
throw ServiceExceptionUtil.exception(DEPARTMENT_NOT_FOUND);
}
// 校验父部门存在
checkParentDepartment(updateBO.getPid(), updateBO.getId());
// 校验部门(自己)
checkDepartment(updateBO.getPid(), updateBO.getName(), updateBO.getId());
// 更新到数据库
DepartmentDO updateObject = DepartmentConvert.INSTANCE.convert(updateBO);
departmentMapper.updateById(updateObject);
}
/**
* 删除部门
*
* @param departmentId 部门编号
*/
public void deleteDepartment(Integer departmentId) {
// 校验删除的部门是否存在
if (departmentMapper.selectById(departmentId) == null) {
throw ServiceExceptionUtil.exception(DEPARTMENT_NOT_FOUND);
}
// 校验部门里没管理员
if (adminMapper.selectCountByDepartmentId(departmentId) > 0) {
throw ServiceExceptionUtil.exception(DEPARTMENT_NOT_FOUND);
}
// 标记删除
departmentMapper.deleteById(departmentId);
}
/**
* 获得部门
*
* @param departmentId 部门编号
* @return 部门
*/
public DepartmentBO getDepartment(Integer departmentId) {
DepartmentDO departmentDO = departmentMapper.selectById(departmentId);
return DepartmentConvert.INSTANCE.convert(departmentDO);
}
/**
* 获得部门列表
*
* @param departmentIds 部门编号列表
* @return 部门列表
*/
public List<DepartmentBO> listDepartments(Collection<Integer> departmentIds) {
List<DepartmentDO> departmentDOs = departmentMapper.selectBatchIds(departmentIds);
return DepartmentConvert.INSTANCE.convertList(departmentDOs);
}
/**
* 获得部门全列表
*
* @return 资源列表
*/
public List<DepartmentBO> listDepartments() {
List<DepartmentDO> departmentDOs = departmentMapper.selectList(null);
return DepartmentConvert.INSTANCE.convertList(departmentDOs);
}
/**
* 校验父部门是否合法
*
* 1. 不能设置自己为父部门
* 2. 父部门不存在
*
* @param pid 父部门编号
* @param childId 当前部门编号
*/
private void checkParentDepartment(Integer pid, Integer childId) {
if (pid == null || DepartmentIdEnum.ROOT.getId().equals(pid)) {
return;
}
// 不能设置自己为父部门
if (pid.equals(childId)) {
throw ServiceExceptionUtil.exception(DEPARTMENT_PARENT_ERROR);
}
DepartmentDO resource = departmentMapper.selectById(pid);
// 父部门不存在
if (resource == null) {
throw ServiceExceptionUtil.exception(DEPARTMENT_PARENT_NOT_EXITS);
}
}
/**
* 校验部门是否合法
*
* 1. 校验相同父部门编号下,是否存在相同的部门名
*
* @param name 部门名字
* @param pid 父部门编号
* @param id 部门编号
*/
private void checkDepartment(Integer pid, String name, Integer id) {
DepartmentDO resource = departmentMapper.selectByPidAndName(pid, name);
if (resource == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同 id 的部门
if (id == null) {
throw ServiceExceptionUtil.exception(DEPARTMENT_NAME_DUPLICATE);
}
if (!resource.getId().equals(id)) {
throw ServiceExceptionUtil.exception(DEPARTMENT_NAME_DUPLICATE);
}
}
}

View File

@@ -0,0 +1,44 @@
package cn.iocoder.mall.systemservice.service.admin.bo;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* 部门 BO
*/
@Data
@Accessors(chain = true)
public class DepartmentBO {
/**
* 部门编号
*/
private Integer id;
/**
* 部门名称
*/
private String name;
/**
* 排序字段
*/
private Integer sort;
/**
* 父级部门编号
*/
private Integer pid;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 删除标记
*/
private Integer deleted;
}

View File

@@ -0,0 +1,32 @@
package cn.iocoder.mall.systemservice.service.admin.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 DepartmentCreateBO {
/**
* 部门名称
*/
@NotEmpty(message = "部门名称不能为空")
private String name;
/**
* 排序字段
*/
@NotNull(message = "排序字段不能为空")
private Integer sort;
/**
* 父级部门编号
*/
@NotNull(message = "父级部门编号不能为空")
private Integer pid;
}

View File

@@ -0,0 +1,37 @@
package cn.iocoder.mall.systemservice.service.admin.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 DepartmentUpdateBO {
/**
* 部门编号
*/
@NotNull(message = "部门编号不能为空")
private Integer id;
/**
* 部门名称
*/
@NotEmpty(message = "部门名称不能为空")
private String name;
/**
* 排序字段
*/
@NotNull(message = "排序字段不能为空")
private Integer sort;
/**
* 父级部门编号
*/
@NotNull(message = "父级部门编号不能为空")
private Integer pid;
}

View File

@@ -18,10 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeEnum.*;
@@ -127,6 +124,21 @@ public class PermissionService {
return CollectionUtils.convertSet(adminRoleDOs, AdminRoleDO::getRoleId);
}
/**
* 获得每个管理员拥有的角色编号
* 返回的结果key 为管理员编号
*
* @param adminIds 管理员编号列表
* @return 每个管理员拥有的角色编号
*/
public Map<Integer, Set<Integer>> mapAdminRoleIds(Collection<Integer> adminIds) {
List<AdminRoleDO> adminRoleDOs = adminRoleMapper.selectListByAdminIds(adminIds);
if (CollectionUtils.isEmpty(adminRoleDOs)) {
return Collections.emptyMap();
}
return CollectionUtils.convertMultiMap2(adminRoleDOs, AdminRoleDO::getAdminId, AdminRoleDO::getRoleId);
}
public void checkPermission(Collection<Integer> roleIds, Collection<String> permissions) {
// 查询权限对应资源
List<ResourceDO> resourceBOs = resourceMapper.selectListByPermissions(permissions);

View File

@@ -95,6 +95,8 @@ public class ResourceService {
}
// 标记删除
resourceMapper.deleteById(resourceId);
// 删除授予给角色的权限
roleResourceMapper.deleteByResourceId(resourceId);
}
/**
@@ -160,7 +162,7 @@ public class ResourceService {
/**
* 校验父资源是否合法
*
* 1. 不能舌质红自己为父资源
* 1. 不能设置自己为父资源
* 2. 父资源不存在
* 3. 父资源必须是 {@link ResourceTypeEnum#MENU} 菜单类型
*

View File

@@ -8,6 +8,7 @@ import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.AdminRoleDO
import cn.iocoder.mall.systemservice.dal.mysql.dataobject.permission.RoleDO;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.AdminRoleMapper;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleMapper;
import cn.iocoder.mall.systemservice.dal.mysql.mapper.permission.RoleResourceMapper;
import cn.iocoder.mall.systemservice.enums.permission.RoleCodeEnum;
import cn.iocoder.mall.systemservice.enums.permission.RoleTypeEnum;
import cn.iocoder.mall.systemservice.service.permission.bo.RoleBO;
@@ -38,6 +39,8 @@ public class RoleService {
private RoleMapper roleMapper;
@Autowired
private AdminRoleMapper adminRoleMapper;
@Autowired
private RoleResourceMapper roleResourceMapper;
/**
* 创建角色
@@ -95,8 +98,10 @@ public class RoleService {
}
// 标记删除
roleMapper.deleteById(roleId);
// // 发布角色删除事件,方便清理关联表 TODO 芋艿,需要实现
// eventPublisher.publishEvent(new ResourceDeleteEvent(this, roleDeleteDTO.getId()));
// 标记删除 RoleResource
roleResourceMapper.deleteByRoleId(roleId);
// 标记删除 AdminRole
adminRoleMapper.deleteByRoleId(roleId);
}
/**
@@ -126,7 +131,7 @@ public class RoleService {
* @param roleIds 角色编号列表
* @return 角色列表
*/
public List<RoleBO> listRole(List<Integer> roleIds) {
public List<RoleBO> listRole(Collection<Integer> roleIds) {
List<RoleDO> roleDOs = roleMapper.selectBatchIds(roleIds);
return RoleConvert.INSTANCE.convertList(roleDOs);
}