Browse Source

Merge branch 'develop' of gitadmin/tuoheng_freeway into release

pull/380/head
suqin 9 months ago
parent
commit
824ff9bb87
8 changed files with 1493 additions and 0 deletions
  1. +136
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/DeptServiceImpl.java
  2. +294
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/add/AddDeptService.java
  3. +155
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/delete/DeleteDeptService.java
  4. +87
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/query/QueryChildListService.java
  5. +231
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/query/QueryDeptInfoService.java
  6. +47
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/query/QueryListByTenantIdService.java
  7. +105
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/query/QueryListTreeByDeptIdService.java
  8. +438
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/update/UpdateDeptService.java

+ 136
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/DeptServiceImpl.java View File

@@ -0,0 +1,136 @@
package com.tuoheng.admin.service.dept;

import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.request.dept.AddDeptRequest;
import com.tuoheng.admin.request.dept.EditDeptRequest;
import com.tuoheng.admin.service.dept.add.AddDeptService;
import com.tuoheng.admin.service.dept.delete.DeleteDeptService;
import com.tuoheng.admin.service.dept.query.*;
import com.tuoheng.admin.service.dept.update.UpdateDeptService;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 部门业务层处理
*
* @author wanjing
* @team tuoheng
* @date 2022-11-16
*/
@Slf4j
@Service
public class DeptServiceImpl implements IDeptService {

@Autowired
private DeptMapper deptMapper;

@Autowired
private QueryListTreeService queryListTreeService;

@Autowired
private QueryListTreeByDeptIdService queryListTreeByDeptIdService;

@Autowired
private QueryChildListService queryChildListService;

@Autowired
private QueryDeptInfoService queryDeptInfoService;

@Autowired
private AddDeptService addDeptService;

@Autowired
private UpdateDeptService updateDeptService;

@Autowired
private DeleteDeptService deleteDeptService;

@Autowired
private QueryListByTenantIdService queryListByTenantIdService;

/**
* 查询部门树形列表
*
* @return 部门
*/
@Override
public JsonResult getListTree() {
return queryListTreeService.getListTree();
}

/**
* 查询当前部门树形列表
*
* @return 部门
*/
@Override
public JsonResult getListTreeByDeptId(String deptId) {
return queryListTreeByDeptIdService.getListTree(deptId);
}

/**
* 根据id查询子部门列表
*
* @return 部门
*/
@Override
public JsonResult getChildList(String id) {
return queryChildListService.getChildList(id);
}

/**
* 查询部门
*
* @param id 部门主键
* @return 部门
*/
@Override
public JsonResult getDeptInfo(String id) {
return queryDeptInfoService.getDeptInfo(id);
}

/**
* 新增部门
*
* @param addDeptRequest 新增部门对象
* @return 结果
*/
@Override
public JsonResult insert(AddDeptRequest addDeptRequest) {
return addDeptService.add(addDeptRequest);
}

/**
* 修改部门
*
* @param oldEditDeptRequest 原部门对象
* @param newEditDeptRequest 新部门对象
* @return 结果
*/
@Override
public JsonResult update(EditDeptRequest oldEditDeptRequest, EditDeptRequest newEditDeptRequest) {
return updateDeptService.update(oldEditDeptRequest, newEditDeptRequest);
}

/**
* 删除部门信息
*
* @param id 部门主键
* @return 结果
*/
@Override
public JsonResult deleteById(String id) {
return deleteDeptService.deleteById(id);
}

/**
* 根据当前租户id获取部门列表
* @return
*/
@Override
public JsonResult getListByTenantId() {
return queryListByTenantIdService.getListByTenantId();
}
}

+ 294
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/add/AddDeptService.java View File

@@ -0,0 +1,294 @@
package com.tuoheng.admin.service.dept.add;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.dto.RoadSectionDto;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.dept.AddDeptCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.RoadDeptMapper;
import com.tuoheng.admin.mapper.SectionDeptMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.request.dept.AddDeptRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
* 添加部门业务层处理
*
* @author wanjing
* @team tuoheng
* @date 2022-11-17
*/
@Slf4j
@Service
public class AddDeptService {

@Autowired
private DeptMapper deptMapper;

@Autowired
private UserMapper userMapper;

@Autowired
private RoadDeptMapper roadDeptMapper;

@Autowired
private SectionDeptMapper sectionDeptMapper;

/**
* 添加部门数据
*
* @return
*/
@Transactional
public JsonResult add(AddDeptRequest addDeptRequest) {
log.info("进入添加部门业务接口");
User user = CurrentUserUtil.getUserInfo();
String userId = user.getId();
String tenantId = user.getTenantId();
JsonResult result = this.check(tenantId, addDeptRequest);
if (0 != result.getCode()) {
log.info("添加部门业务接口:校验失败:{}", result.getMsg());
return result;
}

Dept dept = new Dept();
dept.setTenantId(tenantId);
dept.setName(addDeptRequest.getName());
dept.setPid(addDeptRequest.getPid());
dept.setCreateUser(CurrentUserUtil.getUserId());
dept.setCreateTime(DateUtils.now());
Integer rowId = deptMapper.insert(dept);
log.info("新增部门, 返回结果: deptId={}", dept.getId());

if (rowId <= 0) {
log.info("添加部门业务接口:添加部门失败:{}", result.getMsg());
return JsonResult.error(AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getCode(), AddDeptCodeEnum.ADD_DEPT_IS_FAILED.getMsg());
}

this.updateSuperAdminUser(userId, tenantId, dept);

// 新增公路/路段与部门数据
this.addRoadAndSectionToDept(tenantId, dept.getId(), addDeptRequest.getRoadSectionDtoList());

log.info("添加部门业务接口:添加部门成功:{}");
return JsonResult.success(dept);
}

/**
* 检查参数
*
* @param tenantId
* @param addDeptRequest
* @return
*/
private JsonResult check(String tenantId, AddDeptRequest addDeptRequest) {
if (null == addDeptRequest.getPid()) {
return JsonResult.error(AddDeptCodeEnum.DEPT_PID_IS_NULL.getCode(), AddDeptCodeEnum.DEPT_PID_IS_NULL.getMsg());
}

if (StringUtils.isEmpty(addDeptRequest.getName())) {
return JsonResult.error(AddDeptCodeEnum.DEPT_NAME_IS_NULL.getCode(), AddDeptCodeEnum.DEPT_NAME_IS_NULL.getMsg());
}

if (SystemConstant.ROOT_DEPT_PID.equals(addDeptRequest.getPid())) {
Integer count = deptMapper.selectCount(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getPid, addDeptRequest.getPid())
.eq(Dept::getMark, MarkEnum.VALID.getCode()));

// 顶级部门只能创建一个
if (count > 0) {
return JsonResult.error(AddDeptCodeEnum.ROOT_DEPT_IS_EXIST.getCode(), AddDeptCodeEnum.ROOT_DEPT_IS_EXIST.getMsg());
}
} else {
// 判断上级部门是否存在
Integer count = deptMapper.selectCount(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getId, addDeptRequest.getPid())
.eq(Dept::getMark, MarkEnum.VALID.getCode()));
if (count <= 0) {
return JsonResult.error(AddDeptCodeEnum.PARENT_DEPT_IS_NOT_EXIST.getCode(), AddDeptCodeEnum.PARENT_DEPT_IS_NOT_EXIST.getMsg());
}

// 判断是否已存在该部门名称,同部门下,部门名称不能重复
count = deptMapper.selectCount(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getPid, addDeptRequest.getPid())
.eq(Dept::getName, addDeptRequest.getName())
.eq(Dept::getMark, MarkEnum.VALID.getCode()));

// 系统中已存在
if (count > 0) {
return JsonResult.error(AddDeptCodeEnum.DEPT_NAME_IS_ALREADY_EXIST.getCode(), AddDeptCodeEnum.DEPT_NAME_IS_ALREADY_EXIST.getMsg());
}
}

if (CollectionUtil.isNotEmpty(addDeptRequest.getRoadSectionDtoList())) {
List<Section> sectionList;
List<RoadSectionDto> roadSectionDtoList = addDeptRequest.getRoadSectionDtoList();
for (RoadSectionDto roadSectionDto : roadSectionDtoList) {
sectionList = roadSectionDto.getSectionList();
if (CollectionUtils.isEmpty(sectionList)) {
return JsonResult.error(AddDeptCodeEnum.SECTION_IS_NULL.getCode(), AddDeptCodeEnum.SECTION_IS_NULL.getMsg());
}
}
}
return JsonResult.success();
}

/**
* 新增公路/路段与部门数据
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @param roadSectionDtoList 公路/路段集合
* @return 结果
*/
private void addRoadAndSectionToDept(String tenantId, String deptId, List<RoadSectionDto> roadSectionDtoList) {
if (CollectionUtil.isEmpty(roadSectionDtoList)) {
log.info("新增部门公路/路段数据为空");
return;
}
List<RoadInformation> roadInformationList = new ArrayList<>();
List<Section> sectionList = new ArrayList<>();
RoadInformation road;
List<Section> sectionListTmp;
for (RoadSectionDto roadSectionDto : roadSectionDtoList) {
road = roadSectionDto.getRoad();
sectionListTmp = roadSectionDto.getSectionList();
roadInformationList.add(road);
sectionList.addAll(sectionListTmp);
}

// 添加部门和公路数据
// 去重,防止前端提交了重复的数据
roadInformationList = roadInformationList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(RoadInformation::getId))), ArrayList::new));
this.addRoadToDept(tenantId, deptId, roadInformationList);

// 添加部门和路段数据
// 去重,防止前端提交了重复的数据
sectionList = sectionList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Section::getId))), ArrayList::new));
this.addSectionToDept(tenantId, deptId, sectionList);
}

/**
* 新增公路部门数据
* 1、先删除原有记录数据
* 2、新增此次提交的数据
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @param roadInformationList 公路集合
* @return 结果
*/
private void addRoadToDept(String tenantId, String deptId, List<RoadInformation> roadInformationList) {
List<String> roadIdList = roadInformationList.stream().map(o -> o.getId()).collect(Collectors.toList());
Map<String, Object> map = new HashMap<>();
map.put("tenantId", tenantId);
map.put("deptId", deptId);
map.put("roadIdList", roadIdList);
roadDeptMapper.deleteBatchByMap(map);

List<RoadDept> list = new ArrayList<>();
RoadDept roadDept;
for (RoadInformation roadInformation : roadInformationList) {
Integer count = roadDeptMapper.selectCount(new LambdaQueryWrapper<RoadDept>()
.eq(RoadDept::getTenantId, tenantId)
.eq(RoadDept::getDeptId, deptId)
.eq(RoadDept::getRoadId, roadInformation.getId()));
if (count > 0) {
// 已经存在,不用在添加,防止出现冗余数据
continue;
}
roadDept = new RoadDept();
roadDept.setTenantId(tenantId);
roadDept.setDeptId(deptId);
roadDept.setRoadId(roadInformation.getId());
list.add(roadDept);
}

if (!CollectionUtils.isEmpty(list)) {
roadDeptMapper.insertBatch(list);
}
}

/**
* 新增路段部门数据
* 1、先删除原有记录数据
* 2、新增此次提交的数据
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @param sectionList 路段集合
* @return 结果
*/
private void addSectionToDept(String tenantId, String deptId, List<Section> sectionList) {
List<String> sectionIdList = sectionList.stream().map(o -> o.getId()).collect(Collectors.toList());
Map<String, Object> map = new HashMap<>();
map.put("tenantId", tenantId);
map.put("deptId", deptId);
map.put("sectionIdList", sectionIdList);
sectionDeptMapper.deleteBatchByMap(map);
List<SectionDept> list = new ArrayList<>();
SectionDept sectionDept;
for (Section section : sectionList) {
Integer count = sectionDeptMapper.selectCount(new LambdaQueryWrapper<SectionDept>()
.eq(SectionDept::getTenantId, tenantId)
.eq(SectionDept::getDeptId, deptId)
.eq(SectionDept::getSectionId, section.getId()));
if (count > 0) {
// 已经存在,不用在添加,防止出现冗余数据
continue;
}

sectionDept = new SectionDept();
sectionDept.setTenantId(tenantId);
sectionDept.setDeptId(deptId);
sectionDept.setSectionId(section.getId());
list.add(sectionDept);
}
if (!CollectionUtils.isEmpty(list)) {
sectionDeptMapper.insertBatch(list);
}
}

/**
* 如果创建的是顶级部门,将该部门与超级管理员关联
*/
private void updateSuperAdminUser(String userId, String tenantId, Dept dept) {
if (!SystemConstant.ROOT_DEPT_PID.equals(dept.getPid())) {
return;
}
List<User> userList = userMapper.selectList(new LambdaQueryWrapper<User>()
.eq(User::getTenantId, tenantId)
.eq(User::getRoleId, 1)
.eq(User::getMark, MarkEnum.VALID.getCode()));
if (CollectionUtils.isEmpty(userList)) {
log.info("用户为空");
return;
}
List<String> userIdList = userList.stream().map(o -> o.getId()).collect(Collectors.toList());
Map<String, Object> map = new HashMap<>();
map.put("deptId", dept.getId());
map.put("idList", userIdList);
map.put("updateUser", userId);
map.put("updateTime", DateUtils.now());
userMapper.updateDeptIdByIdList(map);
}

}

+ 155
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/delete/DeleteDeptService.java View File

@@ -0,0 +1,155 @@
package com.tuoheng.admin.service.dept.delete;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.dept.DeleteDeptCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.RoadDeptMapper;
import com.tuoheng.admin.mapper.SectionDeptMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 删除部门业务层处理
*
* @author wanjing
* @team tuoheng
* @date 2022-11-19
*/
@Slf4j
@Service
public class DeleteDeptService {

@Autowired
private DeptMapper deptMapper;

@Autowired
private UserMapper userMapper;

@Autowired
private RoadDeptMapper roadDeptMapper;

@Autowired
private SectionDeptMapper sectionDeptMapper;


public JsonResult deleteById(String id) {
log.info("进入删除部门业务接口");
String tenantId = CurrentUserUtil.getTenantId();
JsonResult result = this.check(tenantId, id);
if (0 != result.getCode()) {
log.info("删除部门业务接口:校验失败:{}", result.getMsg());
return result;
}

// 在校验中,判断部门是否存在时,若部门存在,直接将部门信息返回,减少数据库查询次数
Dept dept = (Dept) result.getData();
// dept.setUpdateUser(SecurityUtils.getUserId());
dept.setUpdateTime(DateUtils.now());

Integer rowId = deptMapper.deleteById(id);

log.info("删除部门, 返回结果: deptId={}", rowId);

if (rowId <= 0) {
log.info("删除部门业务接口:删除部门失败:{}", result.getMsg());
return JsonResult.error(DeleteDeptCodeEnum.DELETE_DEPT_IS_FAILED.getCode(), DeleteDeptCodeEnum.DELETE_DEPT_IS_FAILED.getMsg());
}

// 删除该部门下所有公路与部门数据
this.deleteRoadDept(tenantId, id);

// 删除该部门下所有路段与部门数据
this.deleteSectionDept(tenantId, id);

log.info("删除部门业务接口:删除部门成功:{}");
return JsonResult.success("删除部门成功");
}

/**
* 检查参数
* @param tenantId
* @param id
* @return
*/
private JsonResult check(String tenantId, String id) {
// 判断id是否为空
if (StringUtils.isEmpty(id)) {
return JsonResult.error(DeleteDeptCodeEnum.DEPT_ID_IS_NULL.getCode(),DeleteDeptCodeEnum.DEPT_ID_IS_NULL.getMsg());
}

// 判断部门是否存在
Dept dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getId, id)
.eq(Dept::getMark, MarkEnum.VALID.getCode()));
if (null == dept) {
return JsonResult.error(DeleteDeptCodeEnum.DEPT_IS_NOT_EXIST.getCode(), DeleteDeptCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
}

// 该部门下有用户禁止删除
Integer count = userMapper.selectCount(new LambdaQueryWrapper<User>()
.eq(User::getTenantId, tenantId)
.eq(User::getDeptId, id)
.eq(User::getMark, MarkEnum.VALID.getCode()));
if (count > 0) {
return JsonResult.error(DeleteDeptCodeEnum.DEPT_IEXIST_USER.getCode(), DeleteDeptCodeEnum.DEPT_IEXIST_USER.getMsg());
}

// 该部门下有部门禁止删除
count = deptMapper.selectCount(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getPid, id)
.eq(Dept::getMark, MarkEnum.VALID.getCode()));
if (count > 0) {
return JsonResult.error(DeleteDeptCodeEnum.DEPT_IEXIST_CHILDREN_DEPT.getCode(), DeleteDeptCodeEnum.DEPT_IEXIST_CHILDREN_DEPT.getMsg());
}
return JsonResult.success(dept);
}

/**
* 删除公路部门数据
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @return 结果
*/
private void deleteRoadDept(String tenantId, String deptId) {
List<String> deptIdList = deptMapper.selectAllChildListById(deptId);
if (CollectionUtil.isEmpty(deptIdList)) {
return;
}
roadDeptMapper.delete(new LambdaQueryWrapper<RoadDept>()
.eq(RoadDept::getTenantId, tenantId)
.in(RoadDept::getDeptId, deptIdList));
}

/**
* 删除路段部门数据
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @return 结果
*/
private void deleteSectionDept(String tenantId, String deptId) {
List<String> deptIdList = deptMapper.selectAllChildListById(deptId);
if (CollectionUtil.isEmpty(deptIdList)) {
return;
}
sectionDeptMapper.delete(new LambdaQueryWrapper<SectionDept>()
.eq(SectionDept::getTenantId, tenantId)
.in(SectionDept::getDeptId, deptIdList));
}
}

+ 87
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/query/QueryChildListService.java View File

@@ -0,0 +1,87 @@
package com.tuoheng.admin.service.dept.query;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.DataPermissionEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.dept.QueryDeptChildListCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* 根据id查询子部门列表业务层处理
*
* @author wanjing
* @team tuoheng
* @date 2022-11-22
*/
@Slf4j
@Service
public class QueryChildListService {

@Autowired
private DeptMapper deptMapper;

/**
* 获取部门树形列表
*
* @return
*/
public JsonResult getChildList(String deptId) {
User user = CurrentUserUtil.getUserInfo();
String tenantId = user.getTenantId();
JsonResult result = this.check(tenantId, deptId);
if (0 != result.getCode()) {
log.info("根据id查询子部门列表业务:校验失败:{}", result.getMsg());
return result;
}
List<String> deptIdList;
if (DataPermissionEnum.ALL.getCode() == user.getDataPermission() || DataPermissionEnum.DEPT_AND_SUB_DEPT.getCode() == user.getDataPermission()) {
deptIdList = deptMapper.selectAllChildListById(deptId);
} else {
deptIdList = new ArrayList<>();
deptIdList.add(user.getDeptId());
}
if (CollectionUtil.isEmpty(deptIdList)) {
log.info("获取部门列表为空");
return JsonResult.success();
}
List<Dept> deptList = deptMapper.selectListByIdList(deptIdList);
return JsonResult.success(deptList);
}

/**
* 检查参数
*
* @param tenantId
* @param id
*
* @return
*/
private JsonResult check(String tenantId, String id) {
// 判断id是否为空
if (StringUtils.isEmpty(id)) {
return JsonResult.error(QueryDeptChildListCodeEnum.DEPT_ID_IS_NULL.getCode(), QueryDeptChildListCodeEnum.DEPT_ID_IS_NULL.getMsg());
}

// 判断部门是否存在
Integer count = deptMapper.selectCount(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getId, id)
.eq(Dept::getMark, MarkEnum.VALID.getCode()));
if (count <= 0) {
return JsonResult.error(QueryDeptChildListCodeEnum.DEPT_IS_NOT_EXIST.getCode(), QueryDeptChildListCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
}
return JsonResult.success();
}
}

+ 231
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/query/QueryDeptInfoService.java View File

@@ -0,0 +1,231 @@
package com.tuoheng.admin.service.dept.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.DeptConverMapper;
import com.tuoheng.admin.dto.RoadSectionDto;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.code.dept.QueryDeptInfoCodeEnum;
import com.tuoheng.admin.mapper.*;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.dept.DeptInfoVo;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* 获取部门信息业务层处理
*
* @author wanjing
* @team tuoheng
* @date 2022-11-19
*/
@Slf4j
@Service
public class QueryDeptInfoService {

@Autowired
private DeptMapper deptMapper;

@Autowired
private UserMapper userMapper;

@Autowired
private RoadInformationMapper roadInformationMapper;

@Autowired
private SectionMapper sectionMapper;

@Autowired
private RoadDeptMapper roadDeptMapper;

@Autowired
private SectionDeptMapper sectionDeptMapper;

public JsonResult getDeptInfo(String deptId) {
log.info("进入获取部门信息业务, deptId:{}", deptId);
String tenantId = CurrentUserUtil.getTenantId();

// 校验
JsonResult result = this.check(tenantId, deptId);
if (0 != result.getCode()) {
log.info("获取部门信息业务:校验失败:{}", result.getMsg());
return result;
}

// 在校验中,判断部门是否存在时,若部门存在,直接将部门信息返回,减少数据库查询次数
Dept dept = (Dept) result.getData();

// 处理公路与路段的对应关系
List<RoadSectionDto> roadSectionDtoList = this.getRoadSectionDtoList(tenantId, deptId);

// 拼装返回数据实体
Dept parent = this.getParentDept(tenantId, dept.getPid());
Integer userCount = this.getUserCount(tenantId, deptId);
Integer childDeptCount = this.getChildDeptCount(tenantId, deptId);

DeptInfoVo deptInfoVo = DeptConverMapper.INSTANCE.fromDeptToDeptVo(dept);
deptInfoVo.setRoadSectionDtoList(roadSectionDtoList);
deptInfoVo.setUserCount(userCount);
deptInfoVo.setChildDeptCount(childDeptCount);
if (null != parent) {
deptInfoVo.setPname(parent.getName());
}

return JsonResult.success(deptInfoVo);
}

/**
* 检查参数
* @param tenantId
* @param id
* @return
*/
private JsonResult check(String tenantId, String id) {
if (StringUtils.isEmpty(id)) {
return JsonResult.error(QueryDeptInfoCodeEnum.DEPT_ID_IS_NULL.getCode(), QueryDeptInfoCodeEnum.DEPT_ID_IS_NULL.getMsg());
}

// 判断部门是否存在
Dept dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getId, id)
.eq(Dept::getMark, 1));
if (null == dept) {
return JsonResult.error(QueryDeptInfoCodeEnum.DEPT_IS_NOT_EXIST.getCode(), QueryDeptInfoCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
}
return JsonResult.success(dept);
}

/**
* 获取父部门信息
*/
private Dept getParentDept(String tenantId, String pid) {
Dept dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getId, pid)
.eq(Dept::getMark, 1));
return dept;
}

/**
* 获取部门下用户数
*/
private Integer getUserCount(String tenantId, String deptId) {
Integer count = userMapper.selectCount(new LambdaQueryWrapper<User>()
.eq(User::getTenantId, tenantId)
.eq(User::getDeptId, deptId)
.eq(User::getMark, 1));
return count;
}

/**
* 获取部门下子部门数
*/
private Integer getChildDeptCount(String tenantId, String deptId) {
Integer count = deptMapper.selectCount(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getPid, deptId)
.eq(Dept::getMark, 1));
return count;
}

/**
* 拼装数据
* @param tenantId
* @param id
* @return
*/
private List<RoadSectionDto> getRoadSectionDtoList(String tenantId, String id) {
// 获取该部门下公路列表
List<RoadInformation> roadInformationList = this.getRoadInformationList(tenantId, id);
if (CollectionUtil.isEmpty(roadInformationList)) {
// 不会出现此场景,在代码层面考虑健壮性,加此判断
// 根据业务需求,添加部门时,必须添加公路和路段信息
// 若公路数据为空,说明肯定也没也路段信息,不用继续往下判断,因此直接返回
log.info("获取部门信息业务:公路信息为空");
return null;
}

// 获取该部门下路段列表
List<Section> sectionList = this.getSectionList(tenantId, id);
if (CollectionUtil.isEmpty(sectionList)) {
// 不会出现此场景,在代码层面考虑健壮性,加此判断
// 若路段信息为空,不用继续往下判断,因此直接返回
log.info("获取部门信息业务:路段信息为空");
return null;
}

List<RoadSectionDto> roadSectionDtoList = new ArrayList<>();
RoadSectionDto roadSectionDto;
List<Section> sectionListTmp;

Map<String, RoadInformation> roadInformationMap = roadInformationList.stream().collect(Collectors.toMap(RoadInformation::getId, Function.identity()));
RoadInformation roadInformation;
for (Section section : sectionList) {
roadSectionDto = new RoadSectionDto();
sectionListTmp = new ArrayList<>();
sectionListTmp.add(section);
roadInformation = roadInformationMap.get(section.getRoadId());
if (ObjectUtil.isNull(roadInformation)) {
continue;
}
roadSectionDto.setRoad(roadInformation);
roadSectionDto.setSectionList(sectionListTmp);
roadSectionDtoList.add(roadSectionDto);
}
return roadSectionDtoList;
}

/**
* 查询该部门下公路信息列表
* @param tenantId
* @param id
* @return
*/
private List<RoadInformation> getRoadInformationList(String tenantId, String id) {
List<RoadDept> roadDeptList = roadDeptMapper.selectList(new LambdaQueryWrapper<RoadDept>()
.eq(RoadDept::getTenantId, tenantId)
.eq(RoadDept::getDeptId, id));
if (CollectionUtil.isEmpty(roadDeptList)) {
return null;
}
List<String> roadIdList = roadDeptList.stream().map(o -> o.getRoadId()).collect(Collectors.toList());
Map<String, Object> map = new HashMap<>();
map.put("tenantId", tenantId);
map.put("roadIdList", roadIdList);
List<RoadInformation> roadInformationList = roadInformationMapper.getListByMap(map);
return roadInformationList;
}

/**
* 查询该部门下路段信息列表
* @param tenantId
* @param id
* @return
*/
private List<Section> getSectionList(String tenantId, String id) {
List<SectionDept> sectionDeptList = sectionDeptMapper.selectList(new LambdaQueryWrapper<SectionDept>()
.eq(SectionDept::getTenantId, tenantId)
.eq(SectionDept::getDeptId, id));
if (CollectionUtil.isEmpty(sectionDeptList)) {
return null;
}
List<String> sectionIdList = sectionDeptList.stream().map(o -> o.getSectionId()).collect(Collectors.toList());
Map<String, Object> map = new HashMap<>();
map.put("tenantId", tenantId);
map.put("sectionIdList", sectionIdList);
List<Section> sectionList = sectionMapper.getListByMap(map);
return sectionList;
}
}

+ 47
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/query/QueryListByTenantIdService.java View File

@@ -0,0 +1,47 @@
package com.tuoheng.admin.service.dept.query;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.SecurityUserUtils;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Author ChengWang
* @Date 2023/3/8
*/
@Service
@Slf4j
public class QueryListByTenantIdService {

@Autowired
private DeptMapper deptMapper;


/**
* 根据租户id获取部门列表
* @return
*/
public JsonResult getListByTenantId() {
//获取当前登录用户租户id
String tenantId = CurrentUserUtil.getTenantId();
if(null == tenantId){
return JsonResult.error("当前租户不存在");
}
//根据当前租户查询部门列表
List<Dept> deptList = deptMapper.selectList(Wrappers.<Dept>lambdaQuery()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getMark, MarkEnum.VALID.getCode()));

return JsonResult.success(deptList);

}
}

+ 105
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/query/QueryListTreeByDeptIdService.java View File

@@ -0,0 +1,105 @@
package com.tuoheng.admin.service.dept.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.conver.DeptConverMapper;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.entity.RoadInformation;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.DataPermissionEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.dept.QueryDeptTreeListCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.dept.DeptTreeVo;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 获取当前部门树形列表业务层处理
*
* @author wanjing
* @team tuoheng
* @date 2022-11-19
*/
@Slf4j
@Service
public class QueryListTreeByDeptIdService {

@Autowired
private DeptMapper deptMapper;

/**
* 获取部门树形列表
*
* @return
*/
public JsonResult getListTree(String deptId) {
List<DeptTreeVo> deptTreeVoList = new ArrayList<>();
User user = CurrentUserUtil.getUserInfo();
List<String> deptIdList;
if (DataPermissionEnum.ALL.getCode() == user.getDataPermission()) {
deptIdList = null;
} else if (DataPermissionEnum.DEPT_AND_SUB_DEPT.getCode() == user.getDataPermission()) {
deptIdList = deptMapper.selectAllChildListById(user.getDeptId());
} else {
deptIdList = new ArrayList<>();
deptIdList.add(user.getDeptId());
}

// List<Dept> deptList = deptMapper.selectListByIdList(user.getTenantId(), deptIdList);
// // 找出当前部门
// Dept currentDept = deptList.stream()
// .filter(item -> deptId.equals(item.getId()))
// .findAny()
// .orElse(null);
// deptList.removeIf(item -> (item.getPid().equals(currentDept.getPid()) && !item.getId().equals(deptId)));

List<Dept> deptList = deptMapper.selectList(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, user.getTenantId())
.in(CollectionUtil.isNotEmpty(deptIdList), Dept::getId, deptIdList)
.eq(Dept::getMark, MarkEnum.VALID.getCode())
.orderByAsc(Dept::getCreateTime));
if (CollectionUtil.isEmpty(deptList)) {
log.info("获取部门列表为空");
return JsonResult.error(QueryDeptTreeListCodeEnum.DEPT_LIST_IS_NULL.getCode(), QueryDeptTreeListCodeEnum.DEPT_LIST_IS_NULL.getMsg());
}
Dept currentDept = deptList.get(0);


List<DeptTreeVo> deptTreeVoListTmp = DeptConverMapper.INSTANCE.deptListToDeptVoList(deptList);
Map<String, DeptTreeVo> deptVoMap = new HashMap<>();
for (DeptTreeVo deptTreeVo : deptTreeVoListTmp) {
deptVoMap.put(deptTreeVo.getId(), deptTreeVo);
}
for (DeptTreeVo deptTreeVo : deptTreeVoListTmp) {
DeptTreeVo child = deptTreeVo;
if (currentDept.getId().equals(child.getId())) {
deptTreeVoList.add(deptTreeVo);
} else {
DeptTreeVo parent = deptVoMap.get(child.getPid());
if (ObjectUtil.isNotNull(parent)) {
deptTreeVo.setPname(parent.getName());
parent.getChildren().add(child);
}
}
}
if (ObjectUtil.isNotNull(currentDept)) {
// 从list中移除上级部门
Dept finalCurrentDept = currentDept;
deptTreeVoList.removeIf(item -> item.getId().equals(finalCurrentDept.getPid()));
}
return JsonResult.success(deptTreeVoList);
}

}

+ 438
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/dept/update/UpdateDeptService.java View File

@@ -0,0 +1,438 @@
package com.tuoheng.admin.service.dept.update;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.dto.RoadSectionDto;
import com.tuoheng.admin.entity.*;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.dept.EditDeptCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.RoadDeptMapper;
import com.tuoheng.admin.mapper.SectionDeptMapper;
import com.tuoheng.admin.request.dept.EditDeptRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* 添加部门业务层处理
*
* @author wanjing
* @team tuoheng
* @date 2022-11-19
*/
@Slf4j
@Service
public class UpdateDeptService {

@Autowired
private DeptMapper deptMapper;

@Autowired
private RoadDeptMapper roadDeptMapper;

@Autowired
private SectionDeptMapper sectionDeptMapper;

/**
* 修改部门信息,涉及公路、路段数据
*
* @param oldEditDeptRequest
* @param newEditDeptRequest
* @return
*/
public JsonResult update(EditDeptRequest oldEditDeptRequest, EditDeptRequest newEditDeptRequest) {
log.info("进入修改部门业务接口");
String tenantId = CurrentUserUtil.getTenantId();
JsonResult result = this.check(tenantId, oldEditDeptRequest, newEditDeptRequest);
if (0 != result.getCode()) {
log.info("修改部门业务接口:校验失败:{}", result.getMsg());
return result;
}

// 修改部门数据
result = this.updateDept(tenantId, oldEditDeptRequest, newEditDeptRequest);
if (0 != result.getCode()) {
log.info("修改部门业务接口:修改部门失败:{}", result.getMsg());
return JsonResult.error(EditDeptCodeEnum.EDIT_DEPT_IS_FAILED.getCode(), EditDeptCodeEnum.EDIT_DEPT_IS_FAILED.getMsg());
}

// 处理公路与部门数据
this.roadHandle(tenantId, oldEditDeptRequest, newEditDeptRequest);

// 处理路段与部门数据
this.sectionhandle(tenantId, oldEditDeptRequest, newEditDeptRequest);

log.info("修改部门业务接口:修改部门成功:{}");
return JsonResult.success("修改部门成功");
}

/**
* 检查参数
*
* @param tenantId
* @param newEditDeptRequest
* @return
*/
private JsonResult check(String tenantId, EditDeptRequest oldEditDeptRequest, EditDeptRequest newEditDeptRequest) {
if (StringUtils.isEmpty(newEditDeptRequest.getPid())) {
return JsonResult.error(EditDeptCodeEnum.DEPT_PID_IS_NULL.getCode(), EditDeptCodeEnum.DEPT_PID_IS_NULL.getMsg());
}

if (StringUtils.isEmpty(newEditDeptRequest.getId())) {
return JsonResult.error(EditDeptCodeEnum.DEPT_ID_IS_NOT_NULL.getCode(), EditDeptCodeEnum.DEPT_ID_IS_NOT_NULL.getMsg());
}

if (StringUtils.isEmpty(newEditDeptRequest.getName())) {
return JsonResult.error(EditDeptCodeEnum.DEPT_NAME_IS_NOT_NULL.getCode(), EditDeptCodeEnum.DEPT_NAME_IS_NOT_NULL.getMsg());
}

if (!oldEditDeptRequest.getId().equals(newEditDeptRequest.getId())) {
return JsonResult.error(EditDeptCodeEnum.DEPT_ID_IS_NOT_SAME.getCode(), EditDeptCodeEnum.DEPT_ID_IS_NOT_SAME.getMsg());
}

if (!oldEditDeptRequest.getPid().equals(newEditDeptRequest.getPid())) {
return JsonResult.error(EditDeptCodeEnum.DEPT_PID_IS_NOT_SAME.getCode(), EditDeptCodeEnum.DEPT_PID_IS_NOT_SAME.getMsg());
}

// 判断部门是否存在
Dept dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getId, newEditDeptRequest.getId())
.eq(Dept::getMark, 1));
if (null == dept) {
return JsonResult.error(EditDeptCodeEnum.DEPT_IS_NOT_EXIST.getCode(), EditDeptCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
}

if (SystemConstant.ROOT_DEPT_PID.equals(newEditDeptRequest.getPid())) {
// 判断部门是否存在
Dept parentDept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getId, newEditDeptRequest.getId())
.eq(Dept::getMark, 1));
if (null == parentDept) {
return JsonResult.error(EditDeptCodeEnum.DEPT_IS_NOT_EXIST.getCode(), EditDeptCodeEnum.DEPT_IS_NOT_EXIST.getMsg());
}
}

if (!oldEditDeptRequest.getName().equals(newEditDeptRequest.getName())) {
// 判断是否已存在该部门名称,同部门下,部门名称不能重复,
// 注意要增加id不能与自己的相同的判断
Integer count = deptMapper.selectCount(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getPid, newEditDeptRequest.getPid())
.ne(Dept::getId, newEditDeptRequest.getId())
.eq(Dept::getName, newEditDeptRequest.getName())
.eq(Dept::getMark, 1));

// 系统中已存在
if (count > 0) {
return JsonResult.error(EditDeptCodeEnum.DEPT_NAME_IS_ALREADY_EXIST.getCode(), EditDeptCodeEnum.DEPT_NAME_IS_ALREADY_EXIST.getMsg());
}
}

if (CollectionUtil.isNotEmpty(newEditDeptRequest.getRoadSectionDtoList())) {
List<Section> sectionList;
List<RoadSectionDto> roadSectionDtoList = newEditDeptRequest.getRoadSectionDtoList();
for (RoadSectionDto roadSectionDto : roadSectionDtoList) {
sectionList = roadSectionDto.getSectionList();
if (CollectionUtil.isEmpty(sectionList)) {
return JsonResult.error(EditDeptCodeEnum.SECTION_IS_NULL.getCode(), EditDeptCodeEnum.SECTION_IS_NULL.getMsg());
}
}
}

return JsonResult.success();
}

/**
* 修改部门信息
* 根据业务修改,部门只能修改部门名称,若部门名称和原来一致,可不用修改
*
* @param tenantId
* @param oldEditDeptRequest
* @param newEditDeptRequest
* @return
*/
private JsonResult updateDept(String tenantId, EditDeptRequest oldEditDeptRequest, EditDeptRequest newEditDeptRequest) {
if (!oldEditDeptRequest.getName().equals(newEditDeptRequest.getName())) {
Dept dept = new Dept();
dept.setId(newEditDeptRequest.getId());
dept.setTenantId(tenantId);
dept.setName(newEditDeptRequest.getName());
dept.setFullname(newEditDeptRequest.getName());
dept.setUpdateUser(CurrentUserUtil.getUserId());
dept.setUpdateTime(DateUtils.now());

Integer rowId = deptMapper.update(dept);
log.info("修改部门, 返回结果: deptId={}", dept.getId());
if (rowId <= 0) {
return JsonResult.error(EditDeptCodeEnum.EDIT_DEPT_IS_FAILED.getCode(), EditDeptCodeEnum.EDIT_DEPT_IS_FAILED.getMsg());
}
}
return JsonResult.success();
}

/**
* 处理公路和部门数据
*
* @param tenantId
* @param oldEditDeptRequest
* @param newEditDeptRequest
*/
private void roadHandle(String tenantId, EditDeptRequest oldEditDeptRequest, EditDeptRequest newEditDeptRequest) {
List<RoadSectionDto> oldRoadSectionList = oldEditDeptRequest.getRoadSectionDtoList();
List<String> oldRoadIdList = null;
if (CollectionUtil.isNotEmpty(oldRoadSectionList)) {
oldRoadIdList = oldRoadSectionList.stream().map(o -> o.getRoad().getId()).collect(Collectors.toList());
}
List<RoadSectionDto> newRoadSectionList = newEditDeptRequest.getRoadSectionDtoList();
List<String> newRoadIdList = null;
if (CollectionUtil.isNotEmpty(newRoadSectionList)) {
newRoadIdList = newRoadSectionList.stream().map(o -> o.getRoad().getId()).collect(Collectors.toList());
}

// 交集:存在公路, 不做处理
// Collection<String> union = org.apache.commons.collections4.CollectionUtils.union(oldRoadIdList, newRoadIdList);

// 差集:新增公路
Collection<String> addRoadIdList = null;
if (CollectionUtil.isNotEmpty(oldRoadIdList)) {
if (CollectionUtil.isNotEmpty(newRoadIdList)) {
addRoadIdList = org.apache.commons.collections4.CollectionUtils.subtract(newRoadIdList, oldRoadIdList);
}
} else {
addRoadIdList = newRoadIdList;
}
if (CollectionUtil.isNotEmpty(addRoadIdList)) {
// 去重,防止前端提交了重复的数据
addRoadIdList= addRoadIdList.stream().distinct().collect(Collectors.toList());
this.addRoadToDept(tenantId, newEditDeptRequest.getId(), addRoadIdList);
}

// 差集:删除公路
if (CollectionUtil.isNotEmpty(oldRoadIdList)) {
Collection<String> subRoadIdList;
if (CollectionUtil.isNotEmpty(newRoadIdList)) {
subRoadIdList = org.apache.commons.collections4.CollectionUtils.subtract(oldRoadIdList, newRoadIdList);
} else {
subRoadIdList = oldRoadIdList;
}
Map<String, Long> oldRoadIdMap = oldRoadIdList.stream().collect(Collectors.groupingBy(p -> p, Collectors.counting()));
Map<String, Long> subRoadIdMap = subRoadIdList.stream().collect(Collectors.groupingBy(p -> p, Collectors.counting()));
if (CollectionUtil.isNotEmpty(subRoadIdList)) {
List<String> deleteRoadIdList = new ArrayList<>();
for (String roadId : subRoadIdList) {
Long oldCount = oldRoadIdMap.get(roadId);
Long subCount = subRoadIdMap.get(roadId);
if (oldCount.equals(subCount)) {
deleteRoadIdList.add(roadId);
}
}
if (CollectionUtil.isNotEmpty(deleteRoadIdList)) {
this.removeRoadToDept(tenantId, newEditDeptRequest.getId(), subRoadIdList);
}
}
}
}

/**
* 新增公路部门数据
* 1、先删除原有记录数据
* 2、新增此次提交的数据
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @param roadIdList 公路Id集合
* @return 结果
*/
private void addRoadToDept(String tenantId, String deptId, Collection<String> roadIdList) {
List<RoadDept> list = new ArrayList<>();
RoadDept roadDept;
RoadDept roadDeptTmp;
for (String roadId : roadIdList) {
roadDept = new RoadDept();
roadDept.setTenantId(tenantId);
roadDept.setDeptId(deptId);
roadDept.setRoadId(roadId);

// 判断是否存在, 存在则不用再次添加
roadDeptTmp = roadDeptMapper.selectOne(new LambdaQueryWrapper<RoadDept>()
.eq(RoadDept::getTenantId, tenantId)
.eq(RoadDept::getDeptId, deptId)
.eq(RoadDept::getRoadId, roadId));
if (null != roadDeptTmp) {
continue;
}
list.add(roadDept);
}
if (CollectionUtil.isNotEmpty(list)) {
roadDeptMapper.insertBatch(list);
}
}

/**
* 移除公路部门数据
* 如若是该公路被分配给了子部门或子子部门,同样一并删除
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @param roadIdList 公路Id集合
* @return 结果
*/
private void removeRoadToDept(String tenantId, String deptId, Collection<String> roadIdList) {
// 查找部门id及下面所有的子部门
Dept parentDept = new Dept();
parentDept.setTenantId(tenantId);
parentDept.setId(deptId);

List<Dept> deptList = new ArrayList<>();
deptList.add(parentDept);
this.queryChildDeptList(tenantId, deptList, parentDept);

List<String> deptIdList = deptList.stream().map(p -> p.getId()).collect(Collectors.toList());;
for (String roadId : roadIdList) {
roadDeptMapper.delete(new LambdaQueryWrapper<RoadDept>()
.eq(RoadDept::getTenantId, tenantId)
.eq(RoadDept::getRoadId, roadId)
.in(RoadDept::getDeptId, deptIdList));
}
}

/**
* 处理路段和部门数据
*
* @param tenantId
* @param oldEditDeptRequest
* @param newEditDeptRequest
*/
private void sectionhandle(String tenantId, EditDeptRequest oldEditDeptRequest, EditDeptRequest newEditDeptRequest) {
List<RoadSectionDto> oldRoadSectionList = oldEditDeptRequest.getRoadSectionDtoList();
List<Section> oldSectionList = new ArrayList<>();
if (CollectionUtil.isNotEmpty(oldRoadSectionList)) {
for (RoadSectionDto roadSectionDto : oldRoadSectionList) {
oldSectionList.addAll(roadSectionDto.getSectionList());
}
}

List<RoadSectionDto> newRoadSectionList = newEditDeptRequest.getRoadSectionDtoList();
List<Section> newSectionList = new ArrayList<>();
if (CollectionUtil.isNotEmpty(newRoadSectionList)) {
for (RoadSectionDto roadSectionDto : newRoadSectionList) {
newSectionList.addAll(roadSectionDto.getSectionList());
}
}
List<String> newSectionIdList = newSectionList.stream().map(o -> o.getId()).collect(Collectors.toList());

// 差集:新增路段
Collection<String> addSectionIdList;
if (CollectionUtil.isNotEmpty(oldSectionList)) {
List<String> oldSectionIdList = oldSectionList.stream().map(o -> o.getId()).collect(Collectors.toList());
addSectionIdList = org.apache.commons.collections4.CollectionUtils.subtract(newSectionIdList, oldSectionIdList);
} else {
addSectionIdList = newSectionIdList;
}
if (CollectionUtil.isNotEmpty(addSectionIdList)) {
// 去重,防止前端提交了重复的数据
addSectionIdList= addSectionIdList.stream().distinct().collect(Collectors.toList());
// 添加路段和部门数据
this.addSectionToDept(tenantId, newEditDeptRequest.getId(), addSectionIdList);
}

// 差集:删除路段
if (CollectionUtil.isNotEmpty(oldSectionList)) {
List<String> oldSectionIdList = oldSectionList.stream().map(o -> o.getId()).collect(Collectors.toList());
Collection<String> subSectionIdList = org.apache.commons.collections4.CollectionUtils.subtract(oldSectionIdList, newSectionIdList);
if (CollectionUtil.isNotEmpty(subSectionIdList)) {
// 删除路段和部门数据
this.removeSectionToDept(tenantId, newEditDeptRequest.getId(), subSectionIdList);
}
}
}

/**
* 新增路段部门数据
* 1、先删除原有记录数据
* 2、新增此次提交的数据
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @param sectionIdList 路段id集合
* @return 结果
*/
private void addSectionToDept(String tenantId, String deptId, Collection<String> sectionIdList) {
List<SectionDept> list = new ArrayList<>();
SectionDept sectionDept;
SectionDept sectionDeptTmp;
for (String sectionId : sectionIdList) {
sectionDept = new SectionDept();
sectionDept.setTenantId(tenantId);
sectionDept.setDeptId(deptId);
sectionDept.setSectionId(sectionId);

// 判断是否存在, 存在则不用再次添加
sectionDeptTmp = sectionDeptMapper.selectOne(new LambdaQueryWrapper<SectionDept>()
.eq(SectionDept::getTenantId, tenantId)
.eq(SectionDept::getDeptId, deptId)
.eq(SectionDept::getSectionId, sectionId));
if (null != sectionDeptTmp) {
continue;
}
list.add(sectionDept);
}

if (CollectionUtil.isNotEmpty(list)) {
sectionDeptMapper.insertBatch(list);
}
}

/**
* 移除路段部门数据
*
* @param tenantId 租户Id
* @param deptId 部门Id
* @param sectionIdList 路段Id集合
* @return 结果
*/
private void removeSectionToDept(String tenantId, String deptId, Collection<String> sectionIdList) {
List<String> deptIdList = deptMapper.selectAllChildListById(deptId);
if (CollectionUtil.isEmpty(deptIdList)) {
return;
}
for (String sectionId : sectionIdList) {
sectionDeptMapper.delete(new LambdaQueryWrapper<SectionDept>()
.eq(SectionDept::getTenantId, tenantId)
.eq(SectionDept::getSectionId, sectionId)
.in(SectionDept::getDeptId, deptIdList));
}
}

/**
* 递归查询子部门信息
* 1、递归算法,效率比较低,考虑时间数据量不会太多,后期可优化
* 2、另一方案,通过数据库查询,根据pid,查询该部门下所有子部门及子子部门数据
*
* @param deptList
* @param parentDept
*/
private void queryChildDeptList(String tenantId, List<Dept> deptList, Dept parentDept) {
List<Dept> childDeptlist = deptMapper.selectList(new LambdaQueryWrapper<Dept>()
.eq(Dept::getTenantId, tenantId)
.eq(Dept::getPid, parentDept.getId())
.eq(Dept::getMark, MarkEnum.VALID.getCode()));

for (Dept dept : childDeptlist) {
queryChildDeptList(tenantId, deptList, dept);
}
deptList.addAll(childDeptlist);
}

}

Loading…
Cancel
Save