package cn.huge.module.ctuser.service; import cn.huge.base.common.exception.ServiceException; 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.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; 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 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-19 20:04:19 * @version 1.0.0 */ @Slf4j @Service @Transactional(rollbackFor = Exception.class) public class CtUnitService extends ServiceImpl{ @Autowired private CtUnitMapper mapper; @Autowired private UtilsClientImpl utilsClient; @Autowired private CtDeptService ctDeptService; @Autowired private CtUserService ctUserService; /** * 更新对象 * @param entity 对象 */ public void updateCtUnit(CtUnit entity){ try{ mapper.updateCtUnit(entity); }catch (Exception e){ log.error("[CtUnitService.updateCtUnit]调用失败,异常信息:"+e, e); throw new ServiceException("CtUnitService.updateCtUnit", e); } } /** * 条件更新对象 * @param entity 对象 * @param terms 条件 */ public void updateCtUnitTerms(CtUnit entity, Map terms){ try{ mapper.updateCtUnitTerms(entity, terms); }catch (Exception e){ log.error("[CtUnitService.updateCtUnitTerms]调用失败,异常信息:"+e, e); throw new ServiceException("CtUnitService.updateCtUnitTerms", e); } } /** * 根据编号物理删除 * @param id 查询条件集合 */ public void deleteCtUnit(String id){ try{ mapper.deleteCtUnit(id); }catch (Exception e){ log.error("[CtUnitService.deleteCtUnit]调用失败,异常信息:"+e, e); throw new ServiceException("CtUnitService.deleteCtUnit", e); } } /** * 按条件查询 * @param terms 条件 * @return List */ public List listTerms(Map terms){ return mapper.listTerms(terms); } /** * 按条件统计 * @param terms 条件 * @return long */ public long countTerms(Map terms){ return mapper.countTerms(terms); } /** * 按条件分页查询 * @param page 分页对象 * @param terms 条件 * @return Page */ public Page pageQuery(PageRequest page, Map terms){ long total = mapper.countTerms(terms); List content = mapper.pageTerms(page, terms); return new PageImpl(content, page, total); } /** * 新增或更新对象 * @param ctUnit 实体对象 */ public void saveCtUnit(CtUnit ctUnit){ try{ Date nowDate = DateUtils.getNowDate(); // 判断是否新增 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); }catch (Exception e){ log.error("[CtUnitService.saveCtUnit]调用失败,异常信息:"+e, e); throw new ServiceException("CtUnitService.saveCtUnit", e); } } /** * 获取所有组织(包含所有下级子组织) * * @param custId 客户编号 * @param firstId 某一级组织编号 * @return List */ public List listUnitTree(String custId, String firstId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("cust_id", custId).orderByDesc("create_time"); List ctUnits = mapper.selectList(queryWrapper); // 获取所有的 CtDept 的数量 QueryWrapper deptQueryWrapper = new QueryWrapper<>(); deptQueryWrapper.in("unit_id", ctUnits.stream().map(CtUnit::getId).collect(Collectors.toList())); List ctDepts = ctDeptService.list(deptQueryWrapper); // 将 CtDept 的数量存储在 Map 中 Map 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 createTree(List ctUnitList, String firstId) { List 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 childListObj = targetUnit.getChildren(); List 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 userQueryWrapper = new QueryWrapper<>(); userQueryWrapper.eq("unit_id", unitId); Integer countCtUser = ctUserService.count(userQueryWrapper); ctUnit.setCountCtUser(countCtUser); // 未配岗人员(单位下) userQueryWrapper.isNull("dept_id"); List unMatchUsers = ctUserService.list(userQueryWrapper); ctUnit.setUnMatchUsers(unMatchUsers); // 部门详情 QueryWrapper deptQueryWrapper = new QueryWrapper<>(); deptQueryWrapper.eq("unit_id", unitId); deptQueryWrapper.orderByDesc("create_time"); List ctDepts = ctDeptService.list(deptQueryWrapper); for (CtDept ctDept : ctDepts) { // 部门 userQueryWrapper.clear(); userQueryWrapper.eq("dept_id", ctDept.getId()); // 部门下成员 List matchUsers = ctUserService.list(userQueryWrapper); ctDept.setMatchUsers(matchUsers); // 部门人员数量 int countDeptUser = ctUserService.count(userQueryWrapper); ctDept.setCountCtUser(countDeptUser); } List ctDeptTrees = ctDeptService.createTree(ctDepts, null); ctUnit.setCtDepts(ctDeptTrees); // 部门数量 ctUnit.setCountCtDept(ctDepts.size()); return ctUnit; } /** * 更新对象 * * @param ctUnit 对象 */ public void updateTerms(CtUnit ctUnit) { mapper.updateTerms(ctUnit); } }