Merge remote-tracking branch '惠信/develop' into develop

This commit is contained in:
曹鹏飞 2025-02-06 09:24:38 +08:00
commit efb81f7cc6
4 changed files with 151 additions and 7 deletions

View File

@ -2,11 +2,15 @@ package com.nflg.mobilebroken.admin.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.extra.tokenizer.TokenizerUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nflg.mobilebroken.admin.pojo.dto.DepartmentDTO;
import com.nflg.mobilebroken.admin.pojo.query.DepartmentQuery;
import com.nflg.mobilebroken.admin.pojo.vo.BaseDepartmentVO;
import com.nflg.mobilebroken.admin.service.AdminDepartmentService;
import com.nflg.mobilebroken.common.pojo.ApiResult;
import com.nflg.mobilebroken.common.pojo.PageData;
import com.nflg.mobilebroken.common.util.AdminUserUtil;
import com.nflg.mobilebroken.common.util.VUtils;
import com.nflg.mobilebroken.repository.entity.TBaseDepartment;
import com.nflg.mobilebroken.repository.service.ITBaseDepartmentService;
@ -15,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
/**
@ -27,16 +32,20 @@ public class DepartmentController extends ControllerBase {
@Resource
ITBaseDepartmentService departmentService;
@Resource
AdminDepartmentService adminDepartmentService;
/**
* 获取部门列表
* @param query
* @return
*/
@PostMapping("getList")
@MethodInfoMark(value = "获取部件列表",menuName = "部门管理")
public ApiResult<PageData<TBaseDepartment>> getList(@RequestBody DepartmentQuery query){
Page<TBaseDepartment> result = departmentService.selectListByPage(query);
return ApiResult.success(result.getRecords(),query,result.getTotal());
public ApiResult<PageData<BaseDepartmentVO>> getList(@RequestBody DepartmentQuery query){
return adminDepartmentService.getList(query);
}
/**
@ -62,8 +71,14 @@ public class DepartmentController extends ControllerBase {
List<TBaseDepartment> checkCode = departmentService.lambdaQuery().eq(TBaseDepartment::getDeptCode, departmentDTO.getDeptCode()).list();
VUtils.trueThrowBusinessError(CollUtil.isNotEmpty(checkCode)).throwMessage("编码已存在");
departmentService.saveDepartment(Convert.convert(TBaseDepartment.class,departmentDTO));
TBaseDepartment dept = Convert.convert(TBaseDepartment.class, departmentDTO);
dept.setDataCreateUserNo(AdminUserUtil.getUserNo());
dept.setDataCreateUserName(AdminUserUtil.getUserName());
dept.setDataCreateTime(LocalDateTime.now());
dept.setDataModifyUserNo(AdminUserUtil.getUserNo());
dept.setDataModifyUserName(AdminUserUtil.getUserName());
dept.setDataModifyTime(LocalDateTime.now());
departmentService.saveDepartment(dept);
return ApiResult.success(true);
}

View File

@ -0,0 +1,15 @@
package com.nflg.mobilebroken.admin.pojo.vo;
import com.nflg.mobilebroken.repository.entity.TBaseDepartment;
import lombok.Data;
import java.util.List;
@Data
public class BaseDepartmentVO extends TBaseDepartment {
/**
* 子级
*/
private List<BaseDepartmentVO> children;
}

View File

@ -0,0 +1,110 @@
package com.nflg.mobilebroken.admin.service;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nflg.mobilebroken.admin.pojo.query.DepartmentQuery;
import com.nflg.mobilebroken.admin.pojo.vo.BaseDepartmentVO;
import com.nflg.mobilebroken.common.pojo.ApiResult;
import com.nflg.mobilebroken.common.pojo.PageData;
import com.nflg.mobilebroken.repository.entity.TBaseDepartment;
import com.nflg.mobilebroken.repository.service.ITBaseDepartmentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class AdminDepartmentService {
@Resource
ITBaseDepartmentService departmentService;
public ApiResult<PageData<BaseDepartmentVO>> getList(DepartmentQuery query){
Page<TBaseDepartment> result = departmentService.selectListByPage(query);
List<BaseDepartmentVO> dataResult = Convert.toList(BaseDepartmentVO.class, result.getRecords());
if(StrUtil.isNotBlank(query.getDeptCodeOrName())){
List<BaseDepartmentVO> alldept = Convert.toList(BaseDepartmentVO.class,departmentService.list()) ;
Map<Integer, BaseDepartmentVO> collect = alldept.stream().collect(Collectors.toMap(BaseDepartmentVO::getId, Function.identity()));
List<BaseDepartmentVO> allParents=new ArrayList<>();
for (BaseDepartmentVO data:dataResult){
allParents.addAll(getAllParents(data, collect)) ;
}
allParents.addAll(dataResult);
return ApiResult.success(buildTree(allParents),query,result.getTotal());
}
else {
for (BaseDepartmentVO data:dataResult){
initNodeChildren(data);
}
}
return ApiResult.success(dataResult,query,result.getTotal());
}
private void initNodeChildren(BaseDepartmentVO node) {
List<BaseDepartmentVO> immediateChildren = findChildDepartmentsByParentId(node.getId());
node.setChildren(immediateChildren);
for (BaseDepartmentVO child : immediateChildren) {
initNodeChildren(child);
}
}
private List<BaseDepartmentVO> findChildDepartmentsByParentId(int parentId) {
List<TBaseDepartment> depts = departmentService.lambdaQuery().eq(TBaseDepartment::getDeptParentId, parentId).list();
return Convert.toList(BaseDepartmentVO.class,depts);
}
private List<BaseDepartmentVO> getAllParents(BaseDepartmentVO node, Map<Integer, BaseDepartmentVO> idToNodeMap) {
List<BaseDepartmentVO> parents = new ArrayList<>();
Integer parentId = node.getDeptParentId();
while (parentId != null && parentId>0) {
BaseDepartmentVO parentNode = idToNodeMap.get(parentId);
if (parentNode == null) break; // 防止数据不一致导致无限循环
parents.add(parentNode);
parentId = parentNode.getDeptParentId(); // 移动到上一级
}
Collections.reverse(parents); // 如果需要从根节点开始排序则反转列表
return parents;
}
/**
* list 构建树
* @param nodes
* @return
*/
private List<BaseDepartmentVO> buildTree(List<BaseDepartmentVO> nodes) {
// 使用Map存储id到Node的映射便于快速查找父节点
Map<Integer, BaseDepartmentVO> idToNodeMap = nodes.stream()
.collect(Collectors.toMap(BaseDepartmentVO::getId, Function.identity()));
List<BaseDepartmentVO> roots = new ArrayList<>();
for (BaseDepartmentVO node : nodes) {
BaseDepartmentVO parent = idToNodeMap.get(node.getDeptParentId());
if (parent != null) {
if(null==parent.getChildren()) {
parent.setChildren(new ArrayList<>());
}
// 如果有父节点则添加到父节点的孩子列表中
parent.getChildren().add(node);
} else {
// 没有父节点则为根节点
roots.add(node);
}
}
return roots;
}
}

View File

@ -12,7 +12,11 @@
<select id="selectListByPage" resultType="com.nflg.mobilebroken.repository.entity.TBaseDepartment">
select *
from t_base_department
where 1 = 1 and dept_parent_id=0
where 1 = 1
<if test="query.deptCodeOrName==null || query.deptCodeOrName==''">
and dept_parent_id=0
</if>
<include refid="whr"/>
</select>