forked from gzzfw/backEnd/gz-dyh

wangwh
2024-08-21 f974bf528f0fd1f7316bdb3f06be8004e8db9f15
dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtDeptService.java
@@ -4,9 +4,12 @@
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.ctuser.dao.mapper.CtDeptMapper;
import cn.huge.module.ctuser.domain.po.CtDept;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@@ -17,6 +20,7 @@
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -26,7 +30,7 @@
 * @Description 客户部门表业务逻辑处理
 * @company hugeinfo
 * @author liyj
 * @Time 2024-08-17 15:30:57
 * @Time 2024-08-19 20:04:19
 * @version 1.0.0
 */
@Slf4j
@@ -116,7 +120,7 @@
    */
    public void saveCtDept(CtDept ctDept){
        try{
            Date nowDate = DateUtils.getMowDate();
            Date nowDate = DateUtils.getNowDate();
            // 判断是否新增
            if (IdUtils.checkNewId(ctDept.getId())){
                ctDept.setId(utilsClient.getNewTimeId());
@@ -130,4 +134,51 @@
        }
    }
    /**
     * 创建某一级部门下树形结构
     * @param ctDeptList 所有部门集合
     * @param firstId 某一级组织编号
     * @return List
     */
    public List<CtDept> createTree(List<CtDept> ctDeptList, String firstId) {
        List<CtDept> firstMapList= new ArrayList<>();
        for (CtDept ctDept: ctDeptList ){
            for(CtDept deptParam: ctDeptList ) {
                if (deptParam.getId().equals(ctDept.getParentId())) {
                    addToMBean(deptParam, ctDept);
                    break;
                }
            }
        }
        //取第一级节点
        for (CtDept dept: ctDeptList ){
            if (StringUtils.isNotEmpty(firstId)){
                if (firstId.equals(dept.getId())) {
                    firstMapList.add(dept);
                }
            }else{
                if (BaseConsts.ROOT.equals(dept.getParentId()) || StringUtils.isEmpty(dept.getParentId())) {
                    firstMapList.add(dept);
                }
            }
        }
        return firstMapList;
    }
    /**
     * 获取部门的子级部门
     * @param targetUnit 目标部门
     * @param currentUnit 子级部门
     */
    public void addToMBean(CtDept targetUnit, CtDept currentUnit){
        List<CtDept> childListObj = targetUnit.getChildren();
        List<CtDept> childList = null;
        if(CollectionUtils.isEmpty(childListObj)) {
            childList = new ArrayList();
            targetUnit.setChildren(childList);
        }else{
            childList = childListObj;
        }
        childList.add(currentUnit);
    }
}