package com.engine.wysecond.service.impl; import com.engine.core.impl.Service; import com.engine.wysecond.entity.*; import com.engine.wysecond.service.PostionMatrixService; import com.weaver.general.Util; import lombok.SneakyThrows; import weaver.conn.RecordSet; import weaver.hrm.company.DepartmentComInfo; import weaver.hrm.resource.ResourceComInfo; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** * @Author liang.cheng * @Date 2025/2/26 10:04 * @Description: * @Version 1.0 */ public class PostionMatrixServiceImpl extends Service implements PostionMatrixService { @SneakyThrows @Override public PostionMatrixVo postionMatrix() { PostionMatrixVo postionMatrixVo = new PostionMatrixVo(); RecordSet rs = new RecordSet(); //1.人员信息获取 List resourcesList = new ArrayList<>(); ResourceComInfo resource = new ResourceComInfo(); rs.executeQuery("select xm,szbm,ywzwxl,ywzwcj,rbywzwcjqssj from uf_ywzwxx where rbywzwcjqssj is not null"); while (rs.next()) { resourcesList.add(ResourcesPo.builder() .resourceId(Util.null2String(rs.getString("xm"))) .resource(resource.getLastname(Util.null2String(rs.getString("xm")))) .department(Util.null2String(rs.getString("szbm"))) .postionType(Util.null2String(rs.getString("ywzwxl"))) .postion(Util.null2String(rs.getString("ywzwcj"))) .workDate(Util.null2String(rs.getString("rbywzwcjqssj"))) .build()); } //过滤后数据 List resourcesPoList = filterClosestWorkDate(resourcesList); //2.报表岗位层级数据 LinkedList tops = new LinkedList<>(); rs.executeQuery("select id,ywzwxl,ywzwcj from uf_ywzwcj"); while (rs.next()) { tops.add(Postion.builder() .id(Util.null2String(rs.getString("id"))) .postionType(Util.getIntValue(rs.getString("ywzwxl"))) .postionName(Util.null2String(rs.getString("ywzwcj"))) .build()); } //3.双层循环构造中间数据 LinkedList centers = new LinkedList<>(); DepartmentComInfo dept = new DepartmentComInfo(); Map> groupedByDepartment = resourcesPoList.stream() .collect(Collectors.groupingBy(ResourcesPo::getDepartment)); for (Map.Entry> entry : groupedByDepartment.entrySet()) { String department = entry.getKey(); List poList = entry.getValue(); //职能管理序列 List fDatasAll = poList.stream() .filter(po -> ("1".equals(po.getPostionType()) && department.equals(po.getDepartment()))) .collect(Collectors.toList()); LinkedList fDatas = new LinkedList<>(); for (int i = 0; i < tops.size(); i++) { Postion postion = tops.get(i); List collect = fDatasAll.stream() .filter(po -> postion.getId().equals(po.getPostionType())) .collect(Collectors.toList()); String names = collect.stream() .map(ResourcesPo::getResource) .collect(Collectors.joining("、")); fDatas.add(PeoplesAndNames.builder() .peoples(collect.size()) .names(names) .build()); } //专业技术序列 List sDatasAll = poList.stream() .filter(po -> ("2".equals(po.getPostionType()) && department.equals(po.getDepartment()))) .collect(Collectors.toList()); LinkedList sDatas = new LinkedList<>(); for (int i = 0; i < tops.size(); i++) { Postion postion = tops.get(i); List collect = sDatasAll.stream() .filter(po -> postion.getId().equals(po.getPostionType())) .collect(Collectors.toList()); String names = collect.stream() .map(ResourcesPo::getResource) .collect(Collectors.joining("、")); sDatas.add(PeoplesAndNames.builder() .peoples(collect.size()) .names(names) .build()); } int row = 1; if (fDatasAll.size() > 0 && sDatasAll.size() > 0) { row = 2; } centers.add(PostionPeople.builder() .name(dept.getDepartmentName(department)) .row(row) .fsum(fDatasAll.size()) .ssum(sDatasAll.size()) .fdatas(fDatas) .sdatas(sDatas) .build()); } //4.底部合计数据 LinkedList footers = new LinkedList<>(); //1.职能管理序列 LinkedList zndatas = new LinkedList<>(); for (int i = 0; i < tops.size(); i++) { Postion postion = tops.get(i); rs.executeQuery("select xm,rbywzwcjqssj from uf_ywzwxx where ywzwxl = 1 " + " and ywzwcj = ? and rbywzwcjqssj is not null",postion.getId()); List resourcesPos = new ArrayList<>(); while (rs.next()) { resourcesPos.add(ResourcesPo.builder() .resourceId(Util.null2String(rs.getString("xm"))) .workDate(Util.null2String(rs.getString("rbywzwcjqssj"))).build()); } List filterAll = filterClosestWorkDate(resourcesPos); zndatas.add(filterAll.size()); //名单列 增加null代替不展示 zndatas.add(null); if (i == tops.size() - 1) { //增加合计值 todo zndatas.add(null); } }; footers.add(PeopleSums.builder().type("职能管理序列").datas(zndatas).build()); //2.专业管理序列 LinkedList zydatas = new LinkedList<>(); for (int i = 0; i < tops.size(); i++) { Postion postion = tops.get(i); rs.executeQuery("select xm,rbywzwcjqssj from uf_ywzwxx where ywzwxl = 2 " + " and ywzwcj = ? and rbywzwcjqssj is not null",postion.getId()); List resourcesPos = new ArrayList<>(); while (rs.next()) { resourcesPos.add(ResourcesPo.builder() .resourceId(Util.null2String(rs.getString("xm"))) .workDate(Util.null2String(rs.getString("rbywzwcjqssj"))).build()); } List filterAll = filterClosestWorkDate(resourcesPos); zydatas.add(filterAll.size()); //名单列 增加null代替不展示 zydatas.add(null); if (i == tops.size() - 1) { //增加合计值 todo zydatas.add(null); } }; footers.add(PeopleSums.builder().type("专业技术序列").datas(zydatas).build()); //3.总计 LinkedList zjdatas = new LinkedList<>(); for (int i = 0; i < tops.size(); i++) { Postion postion = tops.get(i); rs.executeQuery("select xm,rbywzwcjqssj from uf_ywzwxx where ywzwcj = ? and rbywzwcjqssj is not null",postion.getId()); List resourcesPos = new ArrayList<>(); while (rs.next()) { resourcesPos.add(ResourcesPo.builder() .resourceId(Util.null2String(rs.getString("xm"))) .workDate(Util.null2String(rs.getString("rbywzwcjqssj"))).build()); } List filterAll = filterClosestWorkDate(resourcesPos); zjdatas.add(filterAll.size()); //名单列 增加null代替不展示 zjdatas.add(null); if (i == tops.size() - 1) { //增加合计值 todo zjdatas.add(null); } }; footers.add(PeopleSums.builder().type("总计").datas(zjdatas).build()); return PostionMatrixVo.builder().tops(tops).centers(centers).footers(footers).build(); } private static List filterClosestWorkDate(List resourcesPos) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 按 resourceId 分组 Map> groupedByResource = resourcesPos.stream() .collect(Collectors.groupingBy(ResourcesPo::getResourceId)); List result = new ArrayList<>(); // 遍历分组结果 groupedByResource.forEach((resource, group) -> { try { // 找出该分组中 workDate 最大(最近)的元素 Optional maxDatePo = group.stream() .max(Comparator.comparing(po -> { try { return sdf.parse(po.getWorkDate()); } catch (ParseException e) { throw new RuntimeException(e); } })); maxDatePo.ifPresent(result::add); } catch (Exception e) { e.printStackTrace(); } }); return result; } }