package com.mindskip.xzs.service.impl; import com.mindskip.xzs.domain.Department; import com.mindskip.xzs.domain.enums.DeptAdminEnum; import com.mindskip.xzs.domain.vo.BaseSelect; import com.mindskip.xzs.domain.vo.CascaderDataVO; import com.mindskip.xzs.domain.vo.DepartmentEditVO; import com.mindskip.xzs.domain.vo.UpdateDeptAdminVO; import com.mindskip.xzs.repository.BaseMapper; import com.mindskip.xzs.repository.DepartmentMapper; import com.mindskip.xzs.repository.UserDepartmentMapper; import com.mindskip.xzs.repository.UserMapper; import com.mindskip.xzs.service.DepartmentService; import com.mindskip.xzs.utility.ModelMapperSingle; import com.mindskip.xzs.utility.TreeUtils; import com.mindskip.xzs.viewmodel.admin.department.DepartmentResponseVM; import org.modelmapper.ModelMapper; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.*; import java.util.stream.Collectors; @Service public class DepartmentServiceImpl extends BaseServiceImpl implements DepartmentService { protected final static ModelMapper modelMapper = ModelMapperSingle.Instance(); private final DepartmentMapper departmentMapper; private final UserDepartmentMapper userDepartmentMapper; private final UserMapper userMapper; public DepartmentServiceImpl(BaseMapper baseMapper, DepartmentMapper departmentMapper, UserDepartmentMapper userDepartmentMapper, UserMapper userMapper) { super(baseMapper); this.departmentMapper = departmentMapper; this.userDepartmentMapper = userDepartmentMapper; this.userMapper = userMapper; } @Override public Integer add(Department model) { model.setDeleted("0"); if (Objects.nonNull(model.getParentId())) { // 查出父级的层级 List fatherList = departmentMapper.getFather(model.getParentId()); Collections.reverse(fatherList); model.setLevel(fatherList.size() + 1); } return departmentMapper.add(model); } @Transactional(rollbackFor = Exception.class) @Override public Integer update(Department department) { Department entity = departmentMapper.getById(department.getId()); if (Objects.isNull(entity)) { throw new RuntimeException("该部门不存在"); } // 先清除之前的管理员标识 List deptUserList = userDepartmentMapper.getDeptUserList(entity.getId()); List userIds = deptUserList.stream().map(BaseSelect::getId).collect(Collectors.toList()); if (! CollectionUtils.isEmpty(userIds)) { userMapper.clearDeptAdmin(userIds, entity.getId()); userMapper.cancelUserDeptAdmin(userIds); } // 设置部门管理员 if (! CollectionUtils.isEmpty(department.getAdminId())) { userMapper.updateDeptAdmin(department.getAdminId(), DeptAdminEnum.YES.getValue(), entity.getId()); UpdateDeptAdminVO updateDeptAdminVO = new UpdateDeptAdminVO(); updateDeptAdminVO.setAdminIds(department.getAdminId()); userMapper.updateUserDeptAdmin(updateDeptAdminVO); } if (Objects.nonNull(department.getParentId())) { // 查出父级的层级 List fatherList = departmentMapper.getFather(department.getParentId()); department.setLevel(fatherList.size() + 1); } else { department.setLevel(1); } Integer update = departmentMapper.update(department); return update; } @Override public Integer remove(Integer id) { Department department = new Department(); department.setId(id); department.setDeleted("1"); return departmentMapper.update(department); } @Override public List gets(DepartmentResponseVM departmentResponseVM) { List deptList = departmentMapper.page(departmentResponseVM); return this.buildPageTree(deptList); } public List buildPageTree(List departments) { // 用于存储已经放入树的节点,以value为键 Map map = new HashMap<>(); List rootNodes = new ArrayList<>(); // 第一步:将所有节点放入map中,并初始化children列表 for (DepartmentResponseVM dept : departments) { map.put(dept.getId(), dept); dept.setChildren(new ArrayList<>()); } // 第二步:构建树形结构 for (DepartmentResponseVM dept : departments) { if (dept.getParentId() == null) { rootNodes.add(dept); } else { DepartmentResponseVM parent = map.get(dept.getParentId()); if (parent != null) { parent.getChildren().add(dept); } } } return rootNodes; } @Override public Department getById(Integer id) { return departmentMapper.getById(id); } @Override public DepartmentEditVO getByIdVO(Integer id) { Department dept = departmentMapper.getById(id); DepartmentEditVO vo = new DepartmentEditVO(); BeanUtils.copyProperties(dept, vo); if (Objects.nonNull(dept.getParentId())) { // 查出完整的父级ID List fatherList = departmentMapper.getFather(dept.getParentId()); Collections.reverse(fatherList); vo.setParentIds(fatherList); } vo.setAdminId(userDepartmentMapper.selectDeptAdmin(id)); return vo; } @Override public List gets(List deptId) { return TreeUtils.build(departmentMapper.gets(deptId)); } @Override public Department getName(String name) { return departmentMapper.getName(name); } /** * 获取部门用户下拉列表 * @param deptId * @return */ @Override public List getDeptUserList(Integer deptId) { return userDepartmentMapper.getDeptUserList(deptId); } @Override @Transactional(rollbackFor = Exception.class) public void updateAdmin(UpdateDeptAdminVO form) { Department entity = departmentMapper.getById(form.getId()); if (Objects.isNull(entity)) { throw new RuntimeException("该部门不存在"); } List deptUserList = userDepartmentMapper.getDeptUserList(entity.getId()); List userIds = deptUserList.stream().map(BaseSelect::getId).collect(Collectors.toList()); if (! CollectionUtils.isEmpty(userIds)) { // 先清除之前的管理员标识 userMapper.clearDeptAdmin(userIds, entity.getId()); // 修改没被选择的用户并且不是别的部门管理员的用户角色为1 userMapper.cancelUserDeptAdmin(userIds); } if (! CollectionUtils.isEmpty(form.getAdminIds())) { // 标识部门管理员 userMapper.updateDeptAdmin(form.getAdminIds(), DeptAdminEnum.YES.getValue(), entity.getId()); // 修改被选择的用户为部门管理员 userMapper.updateUserDeptAdmin(form); } } @Override public List cascader() { List deptList = departmentMapper.list(); List tree = this.buildTree(deptList); return tree; } public List buildTree(List departments) { // 用于存储已经放入树的节点,以value为键 Map map = new HashMap<>(); List rootNodes = new ArrayList<>(); // 第一步:将所有节点放入map中,并初始化children列表 for (CascaderDataVO dept : departments) { map.put(dept.getValue(), dept); dept.setChildren(new ArrayList<>()); } // 第二步:构建树形结构 for (CascaderDataVO dept : departments) { if (dept.getParentId() == null) { // 假设顶级部门的parentId为null或0 rootNodes.add(dept); } else { CascaderDataVO parent = map.get(dept.getParentId()); if (parent != null) { parent.getChildren().add(dept); } } } return rootNodes; } }