| | |
| | | package com.mindskip.xzs.service.impl; |
| | | |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | 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.UpdateDeptAdminVO; |
| | | import com.mindskip.xzs.repository.BaseMapper; |
| | | import com.mindskip.xzs.repository.DepartmentMapper; |
| | |
| | | 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.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Integer add(String name) { |
| | | Department department = new Department(); |
| | | department.setDeleted("0"); |
| | | department.setName(name); |
| | | return departmentMapper.add(department); |
| | | public Integer add(Department model) { |
| | | model.setDeleted("0"); |
| | | return departmentMapper.add(model); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public PageInfo<DepartmentResponseVM> gets(DepartmentResponseVM departmentResponseVM) { |
| | | return PageHelper.startPage(departmentResponseVM.getPageIndex(), departmentResponseVM.getPageSize(), "id desc").doSelectPageInfo(() -> |
| | | departmentMapper.page(departmentResponseVM) |
| | | ); |
| | | public List<DepartmentResponseVM> gets(DepartmentResponseVM departmentResponseVM) { |
| | | List<DepartmentResponseVM> deptList = departmentMapper.page(departmentResponseVM); |
| | | return this.buildPageTree(deptList); |
| | | } |
| | | |
| | | public List<DepartmentResponseVM> buildPageTree(List<DepartmentResponseVM> departments) { |
| | | // 用于存储已经放入树的节点,以value为键 |
| | | Map<Integer, DepartmentResponseVM> map = new HashMap<>(); |
| | | List<DepartmentResponseVM> 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 |
| | |
| | | |
| | | @Override |
| | | public List<Department> gets(List<Integer> deptId) { |
| | | return departmentMapper.gets(deptId); |
| | | return TreeUtils.build(departmentMapper.gets(deptId)); |
| | | } |
| | | |
| | | @Override |
| | |
| | | // 修改没被选择的用户并且不是别的部门管理员的用户角色为1 |
| | | userMapper.cancelUserDeptAdmin(form); |
| | | } |
| | | |
| | | @Override |
| | | public List<CascaderDataVO> cascader() { |
| | | List<CascaderDataVO> deptList = departmentMapper.list(); |
| | | List<CascaderDataVO> tree = this.buildTree(deptList); |
| | | return tree; |
| | | } |
| | | |
| | | |
| | | public List<CascaderDataVO> buildTree(List<CascaderDataVO> departments) { |
| | | // 用于存储已经放入树的节点,以value为键 |
| | | Map<Integer, CascaderDataVO> map = new HashMap<>(); |
| | | List<CascaderDataVO> 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; |
| | | } |
| | | } |