forked from gzzfw/backEnd/gz-dyh

wangwh
2024-09-10 f0ee71145111bc6ef12c1990fd44d02e688ca31f
dyh-service/dyh-cust/src/main/java/cn/huge/module/ctuser/service/CtUnitService.java
@@ -1,25 +1,35 @@
package cn.huge.module.ctuser.service;
import cn.huge.base.common.dto.SelectTermDTO;
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.common.utils.ObjectUtils;
import cn.huge.base.common.utils.SelectTermUtils;
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 cn.huge.module.ctuser.domain.po.CtUserole;
import cn.huge.module.ctuser.dto.CtUnitWeChatCountDTO;
import cn.huge.module.cust.constant.RoleBaseEnum;
import cn.huge.module.cust.dto.CtUserDTO;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @title: 客户组织信息表业务逻辑处理
@@ -39,6 +49,12 @@
    @Autowired
    private UtilsClientImpl utilsClient;
    @Autowired
    private CtDeptService ctDeptService;
    @Autowired
    private CtUserService ctUserService;
    /**
    * 更新对象
@@ -121,6 +137,9 @@
            if (IdUtils.checkNewId(ctUnit.getId())){
                ctUnit.setId(utilsClient.getNewTimeId());
                ctUnit.setCreateTime(nowDate);
            }else {
                // 1、更新单位信息时,更新调度规则指标目标组织名称
                mapper.updateDispNormCauseTargetUnitName(ctUnit.getUnitName(), ctUnit.getId());
            }
            ctUnit.setUpdateTime(nowDate);
            this.saveOrUpdate(ctUnit);
@@ -130,4 +149,201 @@
        }
    }
    /**
     * 获取所有组织(包含所有下级子组织)
     *
     * @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;
    }
    /**
     * 更新对象
     *
     * @param ctUnit 对象
     */
    public void updateTerms(CtUnit ctUnit) {
        mapper.updateTerms(ctUnit);
    }
    /**
     * 小程序统计机构数量
     * */
    public CtUnitWeChatCountDTO wechatCountUnit(){
        CtUnitWeChatCountDTO ctUnitWeChatCountDTO = new CtUnitWeChatCountDTO();
        QueryWrapper<CtUnit> unitQueryWrapper = new QueryWrapper<>();
        unitQueryWrapper.eq("unit_type", 1);
        Integer zzUnitNum = mapper.selectCount(unitQueryWrapper);
        if(ObjectUtils.isEmpty(zzUnitNum)){
            zzUnitNum = 0;
        }
        ctUnitWeChatCountDTO.setZzUnitNum(zzUnitNum);
        List<Integer> unitTypeList = Arrays.asList(2,3,4,5);
        QueryWrapper<CtUnit> unitQueryWrapper1 = new QueryWrapper<>();
        unitQueryWrapper1.in("unit_type", unitTypeList);
        Integer tzUnitNum = mapper.selectCount(unitQueryWrapper1);
        if(ObjectUtils.isEmpty(tzUnitNum)){
            tzUnitNum = 0;
        }
        ctUnitWeChatCountDTO.setTzUnitNum(tzUnitNum);
        return ctUnitWeChatCountDTO;
    }
    /**
     * 按条件分页查询
     *
     * @param userId  分页对象
     * @return Page
     */
    public List<SelectTermDTO> unitChoose(String userId) {
        // 获取当前登录用户
        CtUserDTO loginUser = ctUserService.clientGetUserAll(userId);
        // 组织条件
        List<CtUnit> ctUnitList = mapper.listAllChild(loginUser.getUnitId());
        List<SelectTermDTO> unitSelectTerms = new ArrayList<>();
        // 部门条件
        QueryWrapper<CtDept> ctDeptQueryWrapper = new QueryWrapper<>();
        ctDeptQueryWrapper.eq("unit_id", loginUser.getUnitId()).select("id", "unit_id", "name", "parent_id");
        List<CtDept> ctDeptList = ctDeptService.list(ctDeptQueryWrapper);
        for(CtUnit ctUnit: ctUnitList){
            SelectTermDTO unitSelectTerm = new SelectTermDTO();
            unitSelectTerm.setValue(ctUnit.getId());
            unitSelectTerm.setLabel(ctUnit.getUnitName());
            unitSelectTerm.setParentId(ctUnit.getParentId());
            unitSelectTerm.setCheckable(true);
            List<SelectTermDTO> deptSelectTerms = new ArrayList<>();
            if(loginUser.getUnitId().equals(ctUnit.getId())){
                for(CtDept ctDept : ctDeptList){
                    if(ctUnit.getId().equals(ctDept.getUnitId())){
                        SelectTermDTO deptSelectTerm = new SelectTermDTO();
                        deptSelectTerm.setValue(ctDept.getId());
                        deptSelectTerm.setLabel(ctDept.getName());
                        deptSelectTerm.setParentId(ctDept.getParentId());
                        deptSelectTerm.setCheckable(true);
                        deptSelectTerms.add(deptSelectTerm);
                    }
                    List<SelectTermDTO> depts = SelectTermUtils.createTreeByRoot(deptSelectTerms);
                    unitSelectTerm.setChildren(depts);
                }
            }
            unitSelectTerms.add(unitSelectTerm);
        }
        return SelectTermUtils.createTreeByFirst(unitSelectTerms, loginUser.getUnitId());
    }
}