forked from gzzfw/backEnd/gz-dyh

wangwh
2024-08-21 f974bf528f0fd1f7316bdb3f06be8004e8db9f15
dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtUnitService.java
@@ -3,10 +3,17 @@
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.base.config.CurrentUser;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.ctuser.dao.mapper.CtUnitMapper;
import cn.huge.module.ctuser.domain.po.CtDept;
import cn.huge.module.ctuser.domain.po.CtUnit;
import cn.huge.module.ctuser.domain.po.CtUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;
@@ -15,18 +22,21 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @title: 客户组织信息表业务逻辑处理
 * @Description 客户组织信息表业务逻辑处理
 * @company hugeinfo
 * @author liyj
 * @Time 2024-08-17 15:30:57
 * @Time 2024-08-19 20:04:19
 * @version 1.0.0
 */
@Slf4j
@@ -39,6 +49,12 @@
    @Autowired
    private UtilsClientImpl utilsClient;
    @Autowired
    private CtDeptService ctDeptService;
    @Autowired
    private CtUserService ctUserService;
    /**
    * 更新对象
@@ -116,7 +132,7 @@
    */
    public void saveCtUnit(CtUnit ctUnit){
        try{
            Date nowDate = DateUtils.getMowDate();
            Date nowDate = DateUtils.getNowDate();
            // 判断是否新增
            if (IdUtils.checkNewId(ctUnit.getId())){
                ctUnit.setId(utilsClient.getNewTimeId());
@@ -130,4 +146,126 @@
        }
    }
    /**
     * 获取所有组织(包含所有下级子组织)
     *
     * @param custId  客户编号
     * @param firstId 某一级组织编号
     * @return List
     */
    public List<CtUnit> listUnitTree(String custId, String firstId) {
        QueryWrapper<CtUnit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_id", custId).orderByDesc("create_time");
        List<CtUnit> ctUnits = mapper.selectList(queryWrapper);
        // 获取所有的 CtDept 的数量
        QueryWrapper<CtDept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.in("unit_id", ctUnits.stream().map(CtUnit::getId).collect(Collectors.toList()));
        List<CtDept> ctDepts = ctDeptService.list(deptQueryWrapper);
        // 将 CtDept 的数量存储在 Map 中
        Map<String, Long> ctDeptCountMap = ctDepts.stream()
                .collect(Collectors.groupingBy(CtDept::getUnitId, Collectors.counting()));
        // 为每个 CtUnit 设置 CtDept 的数量
        for (CtUnit ctUnit : ctUnits) {
            Long ctDeptsCount = ctDeptCountMap.getOrDefault(ctUnit.getId(), 0L);
            ctUnit.setCountCtDept(ctDeptsCount.intValue());
        }
        return this.createTree(ctUnits, firstId);
    }
    /**
     * 创建某一级组织下树形结构
     *
     * @param ctUnitList 所有组织集合
     * @param firstId    某一级组织编号
     * @return List
     */
    public List<CtUnit> createTree(List<CtUnit> ctUnitList, String firstId) {
        List<CtUnit> firstMapList = new ArrayList<>();
        for (CtUnit ctUnit : ctUnitList) {
            for (CtUnit currentParam : ctUnitList) {
                if (currentParam.getId().equals(ctUnit.getParentId())) {
                    addToMBean(currentParam, ctUnit);
                    break;
                }
            }
        }
        //取第一级节点
        for (CtUnit unit : ctUnitList) {
            if (StringUtils.isNotEmpty(firstId)) {
                if (firstId.equals(unit.getId())) {
                    firstMapList.add(unit);
                }
            } else {
                if (BaseConsts.ROOT.equals(unit.getParentId()) || StringUtils.isEmpty(unit.getParentId())) {
                    firstMapList.add(unit);
                }
            }
        }
        return firstMapList;
    }
    /**
     * 获取组织的子级组织
     *
     * @param targetUnit  目标组织
     * @param currentUnit 子级组织
     */
    public void addToMBean(CtUnit targetUnit, CtUnit currentUnit) {
        List<CtUnit> childListObj = targetUnit.getChildren();
        List<CtUnit> childList = null;
        if (CollectionUtils.isEmpty(childListObj)) {
            childList = new ArrayList();
            targetUnit.setChildren(childList);
        } else {
            childList = childListObj;
        }
        childList.add(currentUnit);
    }
    /**
     * 获取组织相关数据(包含组织下的部门)
     *
     * @param unitId 组织主键编号
     * @return
     */
    public CtUnit getUnitAndDept(String unitId) {
        // 组织详情
        CtUnit ctUnit = mapper.selectById(unitId);
        // 组织人员数量
        QueryWrapper<CtUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("unit_id", unitId);
        Integer countCtUser = ctUserService.count(userQueryWrapper);
        ctUnit.setCountCtUser(countCtUser);
        // 未配岗人员(单位下)
        userQueryWrapper.isNull("dept_id");
        List<CtUser> unMatchUsers = ctUserService.list(userQueryWrapper);
        ctUnit.setUnMatchUsers(unMatchUsers);
        // 部门详情
        QueryWrapper<CtDept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("unit_id", unitId);
        deptQueryWrapper.orderByDesc("create_time");
        List<CtDept> ctDepts = ctDeptService.list(deptQueryWrapper);
        for (CtDept ctDept : ctDepts) {
            // 部门
            userQueryWrapper.clear();
            userQueryWrapper.eq("dept_id", ctDept.getId());
            // 部门下成员
            List<CtUser> matchUsers = ctUserService.list(userQueryWrapper);
            ctDept.setMatchUsers(matchUsers);
            // 部门人员数量
            int countDeptUser = ctUserService.count(userQueryWrapper);
            ctDept.setCountCtUser(countDeptUser);
        }
        List<CtDept> ctDeptTrees = ctDeptService.createTree(ctDepts, null);
        ctUnit.setCtDepts(ctDeptTrees);
        // 部门数量
        ctUnit.setCountCtDept(ctDepts.size());
        return ctUnit;
    }
}