| | |
| | | package com.sinata.system.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.sinata.common.constant.CacheConstants; |
| | | import com.sinata.common.exception.ServiceException; |
| | | import com.sinata.common.utils.BeanUtils; |
| | | import com.sinata.common.utils.CollUtils; |
| | | import com.sinata.common.utils.SecurityUtils; |
| | | import com.sinata.common.utils.StringUtils; |
| | | import com.sinata.system.domain.SysDepartment; |
| | | import com.sinata.system.domain.SysUserDepartment; |
| | | import com.sinata.system.domain.dto.SysDepartmentDTO; |
| | | import com.sinata.system.domain.vo.SysDepartmentVO; |
| | | import com.sinata.system.enums.DepartmentEnum; |
| | | import com.sinata.system.mapper.SysDepartmentMapper; |
| | | import com.sinata.system.service.SysDepartmentService; |
| | | import com.sinata.system.service.SysUserDepartmentService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.function.Function; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @RequiredArgsConstructor |
| | | public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> implements SysDepartmentService { |
| | | private final SysUserDepartmentService sysUserDepartmentService; |
| | | private final RedisTemplate<Object, Object> redisTemplate; |
| | | /** |
| | | * 获取区域树 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<SysDepartmentVO> listByType(Integer type) { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | List<SysDepartment> departmentList = baseMapper.getDepartmentListByType(userId, type); |
| | | if (CollUtils.isNotEmpty(departmentList)) { |
| | | Map<Long, SysDepartment> departmentMap = departmentList.stream().collect(Collectors.toMap(SysDepartment::getId, Function.identity())); |
| | | |
| | | List<SysDepartmentVO> root = new ArrayList<>(); |
| | | SysDepartment myDepartment = getMyDepartment(); |
| | | if (Objects.isNull(myDepartment)) { |
| | | return root; |
| | | } |
| | | return null; |
| | | Map<Long, List<SysDepartment>> childrenMap; |
| | | switch (type) { |
| | | case 1: |
| | | if (!myDepartment.getOrgType().equals(DepartmentEnum.REGION.getCode())) { |
| | | return root; |
| | | } |
| | | childrenMap = getChildrenDepartmentByOrgType(myDepartment, Collections.singletonList(DepartmentEnum.REGION.getCode())); |
| | | break; |
| | | case 2: |
| | | childrenMap = getChildrenDepartmentByOrgType(myDepartment, Arrays.asList(DepartmentEnum.REGION.getCode(), DepartmentEnum.MEDICAL_INSTITUTION.getCode())); |
| | | break; |
| | | case 3: |
| | | childrenMap = getChildrenDepartmentByOrgType(myDepartment, Arrays.asList(DepartmentEnum.REGION.getCode(), DepartmentEnum.DISPOSAL_UNIT.getCode())); |
| | | break; |
| | | case 4: |
| | | childrenMap = getChildrenDepartmentByOrgType(myDepartment, Arrays.asList(DepartmentEnum.REGION.getCode(), DepartmentEnum.REGULATORY_UNIT.getCode())); |
| | | break; |
| | | default: |
| | | childrenMap = getChildrenDepartmentByOrgType(myDepartment, null); |
| | | } |
| | | SysDepartmentVO sysDepartmentVO = fillChildrenTreeModel(myDepartment, childrenMap); |
| | | root.add(sysDepartmentVO); |
| | | return root; |
| | | } |
| | | |
| | | /** |
| | | * @param myDepartment |
| | | * @param orgTypes 查询区域类型列表 |
| | | * @return |
| | | */ |
| | | @NotNull |
| | | private Map<Long, List<SysDepartment>> getChildrenDepartmentByOrgType(SysDepartment myDepartment, List<Integer> orgTypes) { |
| | | List<SysDepartment> sysDepartmentList = this.lambdaQuery() |
| | | .likeRight(SysDepartment::getTreeCode, myDepartment.getTreeCode()) |
| | | .in(CollUtils.isNotEmpty(orgTypes), SysDepartment::getOrgType, orgTypes) |
| | | .orderByDesc(SysDepartment::getCreateTime) |
| | | .list(); |
| | | return sysDepartmentList.stream() |
| | | .collect(Collectors.groupingBy(SysDepartment::getParentId)); |
| | | } |
| | | |
| | | @Override |
| | | public SysDepartment getDepartmentByParentId(Long parentId) { |
| | | return this.lambdaQuery().eq(SysDepartment::getId, parentId).one(); |
| | | } |
| | | |
| | | /** |
| | | * 获取区域树 |
| | | * |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<SysDepartmentVO> getRegionTree(String keyword) { |
| | | List<SysDepartmentVO> root = new ArrayList<>(); |
| | | SysDepartment currentDepartment = getMyDepartment(); |
| | | if (Objects.isNull(currentDepartment)) { |
| | | return root; |
| | | } |
| | | if (!currentDepartment.getOrgType().equals(DepartmentEnum.REGION.getCode())) { |
| | | return root; |
| | | } |
| | | Map<Long, List<SysDepartment>> childrenMap = getChildrenDepartmentByOrgType(currentDepartment, Collections.singletonList(DepartmentEnum.REGION.getCode())); |
| | | SysDepartmentVO sysDepartmentVO = fillChildrenTreeModel(currentDepartment, childrenMap); |
| | | root.add(sysDepartmentVO); |
| | | if (StringUtils.isNotBlank(keyword)) { |
| | | treeMatch(root, keyword); |
| | | } |
| | | return root; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前登录用户所属区域 |
| | | * |
| | | * @return |
| | | */ |
| | | @Override |
| | | public SysDepartment getMyDepartment() { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | return baseMapper.getDepartmentByUserId(userId); |
| | | } |
| | | |
| | | /** |
| | | * 构建树结构 |
| | | * |
| | | * @param myDepartment |
| | | * @param childrenMap |
| | | * @return |
| | | */ |
| | | private SysDepartmentVO fillChildrenTreeModel(SysDepartment myDepartment, Map<Long, List<SysDepartment>> childrenMap) { |
| | | SysDepartmentVO currentNode = BeanUtils.copyBean(myDepartment, SysDepartmentVO.class); |
| | | // 使用部门映射直接获取子部门,避免了遍历整个列表 |
| | | List<SysDepartment> children = childrenMap.getOrDefault(currentNode.getId(), |
| | | Collections.emptyList()); |
| | | List<SysDepartmentVO> childrenList = children.stream().map(child -> fillChildrenTreeModel(child, childrenMap)).collect(Collectors.toList()); |
| | | currentNode.setChildren(childrenList.isEmpty() ? new ArrayList<>() : childrenList); |
| | | childrenMap.remove(currentNode.getId()); |
| | | return currentNode; |
| | | } |
| | | |
| | | /** |
| | | * 递归方法 |
| | | * |
| | | * @param tree 任意层级的目录集合 |
| | | * @param keyword 关键字 |
| | | */ |
| | | public static void treeMatch(List<SysDepartmentVO> tree, String keyword) { |
| | | Iterator<SysDepartmentVO> iter = tree.iterator(); |
| | | while (iter.hasNext()) { |
| | | // 获取当前遍历到的目录 |
| | | SysDepartmentVO department = iter.next(); |
| | | // 如果当前目录名称包含关键字,则什么也不做(不移除),否则就看下一级 |
| | | if (!department.getDepartmentName().contains(keyword)) { |
| | | // 取出下一级目录集合 |
| | | List<SysDepartmentVO> childrenSysDepartmentVOList = department.getChildren(); |
| | | // 递归 |
| | | if (!CollUtils.isEmpty(childrenSysDepartmentVOList)) { |
| | | treeMatch(childrenSysDepartmentVOList, keyword); |
| | | } |
| | | // 下一级目录看完了,如果下一级目录全部被移除,则移除当前目录 |
| | | if (CollUtils.isEmpty(department.getChildren())) { |
| | | iter.remove(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 新增区域 |
| | | * |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public void addRegion(SysDepartmentDTO dto) { |
| | | SysDepartment currentDepartment = getMyDepartment(); |
| | | if (Objects.isNull(currentDepartment)) { |
| | | throw new ServiceException("无操作权限"); |
| | | } |
| | | SysDepartment parent = this.getById(dto.getParentId()); |
| | | if (Objects.isNull(parent)) { |
| | | throw new ServiceException("找不到对应父级组织"); |
| | | } |
| | | if (!parent.getTreeCode().startsWith(currentDepartment.getTreeCode())) { |
| | | throw new ServiceException("无操作权限"); |
| | | } |
| | | Long count = this.lambdaQuery().eq(SysDepartment::getDepartmentName, dto.getDepartmentName()) |
| | | .eq(SysDepartment::getOrgType, DepartmentEnum.REGION.getCode()) |
| | | .count(); |
| | | if (count > 0) { |
| | | throw new ServiceException("区域已存在"); |
| | | } |
| | | SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class); |
| | | //获取部门树编码 |
| | | department.setOrgType(DepartmentEnum.REGION.getCode()); |
| | | department.setTreeCode(getTreeCode(dto.getParentId())); |
| | | department.setOrgCode(getOrgCode(dto.getParentId(), DepartmentEnum.REGION.getCode())); |
| | | save(department); |
| | | } |
| | | |
| | | /** |
| | | * 编辑区域 |
| | | * |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public void editRegion(SysDepartmentDTO dto) { |
| | | SysDepartment currentDepartment = getMyDepartment(); |
| | | if (Objects.isNull(currentDepartment)) { |
| | | throw new ServiceException("无操作权限"); |
| | | } |
| | | SysDepartment parent = this.getById(dto.getParentId()); |
| | | if (Objects.isNull(parent)) { |
| | | throw new ServiceException("找不到对应父级组织"); |
| | | } |
| | | if (!parent.getTreeCode().startsWith(currentDepartment.getTreeCode())) { |
| | | throw new ServiceException("无操作权限"); |
| | | } |
| | | Long count = this.lambdaQuery().eq(SysDepartment::getDepartmentName, dto.getDepartmentName()) |
| | | .eq(SysDepartment::getOrgType, DepartmentEnum.REGION.getCode()) |
| | | .ne(SysDepartment::getId, dto.getId()) |
| | | .count(); |
| | | if (count > 0) { |
| | | throw new ServiceException("区域已存在"); |
| | | } |
| | | SysDepartment department = BeanUtils.copyBean(dto, SysDepartment.class); |
| | | SysDepartment sysDepartment = getById(dto.getId()); |
| | | if (!dto.getParentId().equals(sysDepartment.getParentId())) { |
| | | //获取部门树编码 |
| | | department.setTreeCode(getTreeCode(dto.getParentId())); |
| | | } |
| | | updateById(department); |
| | | } |
| | | |
| | | /** |
| | | * 获取树编码 |
| | | * |
| | | * @param parentId |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String getTreeCode(Long parentId) { |
| | | |
| | | String treeId; |
| | | String preTreeCode = ""; |
| | | StringBuilder sb = new StringBuilder(); |
| | | |
| | | SysDepartment sysDepartment = getById(parentId); |
| | | if (sysDepartment != null) { |
| | | preTreeCode = sysDepartment.getTreeCode(); |
| | | } |
| | | //设置key |
| | | |
| | | String key = CacheConstants.SYS_DEPARTS_CACHE + parentId; |
| | | int count = 0; |
| | | //判断是否存在key |
| | | if (redisTemplate.hasKey(key)) { |
| | | count = (int) redisTemplate.opsForValue().get(key); |
| | | } |
| | | do { |
| | | count++; |
| | | treeId = sb.append(preTreeCode).append(StringUtils.leftPad(String.valueOf(count), 4, "0")).toString(); |
| | | SysDepartment department = getOne(new QueryWrapper<SysDepartment>().lambda().eq(SysDepartment::getTreeCode, treeId)); |
| | | if (department == null) { |
| | | break; |
| | | } |
| | | sb = new StringBuilder(); |
| | | } while (true); |
| | | //设置缓存过期时间 |
| | | redisTemplate.opsForValue().set(key, count, 365, TimeUnit.DAYS); |
| | | return treeId; |
| | | } |
| | | |
| | | /** |
| | | * 生成组织编码 |
| | | * 区域、医疗机构、处置单位、监管单位 4位 按组织类型不重复 |
| | | * |
| | | * @param parentId |
| | | * @param orgType |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String getOrgCode(Long parentId, Integer orgType) { |
| | | |
| | | String key = CacheConstants.SYS_DEPARTMENT_ORG_CODE + orgType; |
| | | int length = 4; |
| | | int count = 0; |
| | | //判断是否存在key |
| | | if (redisTemplate.hasKey(key)) { |
| | | count = (int) redisTemplate.opsForValue().get(key); |
| | | } |
| | | //组织编码 |
| | | String orgCode; |
| | | do { |
| | | count++; |
| | | orgCode = StringUtils.leftPad(String.valueOf(count), length, "0"); |
| | | SysDepartment department = getOne(new QueryWrapper<SysDepartment>().lambda().eq(SysDepartment::getOrgCode, orgCode).eq(SysDepartment::getOrgType, orgType).last("limit 1")); |
| | | if (department == null) { |
| | | break; |
| | | } |
| | | } while (true); |
| | | //设置缓存过期时间 |
| | | redisTemplate.opsForValue().set(key, count, 365, TimeUnit.DAYS); |
| | | return orgCode; |
| | | } |
| | | |
| | | @Override |
| | | public void deleteRegion(Long id) { |
| | | SysDepartment myDepartment = getMyDepartment(); |
| | | SysDepartment sysDepartment = getById(id); |
| | | if (!sysDepartment.getTreeCode().startsWith(myDepartment.getTreeCode())) { |
| | | throw new ServiceException("无操作权限"); |
| | | } |
| | | List<SysDepartment> sysDepartmentList = this.lambdaQuery() |
| | | .likeRight(SysDepartment::getTreeCode, sysDepartment.getTreeCode()) |
| | | .eq(SysDepartment::getOrgType, DepartmentEnum.REGION.getCode()) |
| | | .orderByDesc(SysDepartment::getCreateTime) |
| | | .list(); |
| | | List<Long> departmentIds = sysDepartmentList.stream().map(SysDepartment::getId).collect(Collectors.toList()); |
| | | Long count = sysUserDepartmentService.lambdaQuery().in(SysUserDepartment::getDepartmentId, departmentIds).count(); |
| | | if (count > 0) { |
| | | throw new ServiceException("该区域已存在用户,无法删除"); |
| | | } |
| | | removeById(id); |
| | | } |
| | | } |