package com.tievd.jyz.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.tievd.jyz.entity.DepartLabel; import com.tievd.jyz.mapper.DepartLabelMapper; import com.tievd.jyz.service.IDepartLabelService; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class DepartLabelServiceImpl extends ServiceImpl implements IDepartLabelService { @Override public List> queryDepartLabelList(String labelName, String parentCode, String parentId) { return baseMapper.queryDepartLabelList(labelName, parentCode, parentId); } @Override public List queryAllLabelNames(String parentCode, String parentId) { return baseMapper.queryAllLabelNames(parentCode, parentId); } @Override public List> queryOrgOilCount(String orgCode, String startTime, String endTime) { List> oilCountList = baseMapper.queryOrgOilCount(orgCode, startTime, endTime); System.out.println("=== queryOrgOilCount 开始 ==="); System.out.println("orgCode: " + orgCode); System.out.println("startTime: " + startTime); System.out.println("endTime: " + endTime); System.out.println("oilCountList size: " + (oilCountList != null ? oilCountList.size() : 0)); if (oilCountList == null || oilCountList.isEmpty()) { return new ArrayList<>(); } Map orgCodeToIdMap = new HashMap<>(); Map idToParentIdMap = new HashMap<>(); Map idOilCountMap = new HashMap<>(); Map idCarCountMap = new HashMap<>(); Map idStationCountMap = new HashMap<>(); Map idOilVolumeMap = new HashMap<>(); for (Map item : oilCountList) { String orgCodeStr = (String) item.get("org_code"); String departId = (String) item.get("depart_id"); Object oilCountObj = item.get("oilCount"); Integer oilCount = null; if (oilCountObj instanceof Integer) { oilCount = (Integer) oilCountObj; } else if (oilCountObj instanceof Number) { oilCount = ((Number) oilCountObj).intValue(); } else if (oilCountObj instanceof String) { oilCount = Integer.parseInt((String) oilCountObj); } Object carCountObj = item.get("carCount"); Integer carCount = null; if (carCountObj instanceof Integer) { carCount = (Integer) carCountObj; } else if (carCountObj instanceof Number) { carCount = ((Number) carCountObj).intValue(); } else if (carCountObj instanceof String) { carCount = Integer.parseInt((String) carCountObj); } Object stationCountObj = item.get("stationCount"); Integer stationCount = null; if (stationCountObj instanceof Integer) { stationCount = (Integer) stationCountObj; } else if (stationCountObj instanceof Number) { stationCount = ((Number) stationCountObj).intValue(); } else if (stationCountObj instanceof String) { stationCount = Integer.parseInt((String) stationCountObj); } Object oilVolumeObj = item.get("oilVolume"); Integer oilVolume = null; if (oilVolumeObj instanceof Integer) { oilVolume = (Integer) oilVolumeObj; } else if (oilVolumeObj instanceof Number) { oilVolume = ((Number) oilVolumeObj).intValue(); } else if (oilVolumeObj instanceof String) { oilVolume = Integer.parseInt((String) oilVolumeObj); } if (departId != null) { orgCodeToIdMap.put(orgCodeStr, departId); if (oilCount != null) { idOilCountMap.put(departId, oilCount); } if (carCount != null) { idCarCountMap.put(departId, carCount); } if (stationCount != null) { idStationCountMap.put(departId, stationCount); } if (oilVolume != null) { idOilVolumeMap.put(departId, oilVolume); } } } List> departList = baseMapper.queryDepartLabelList(null, orgCode, null); System.out.println("departList size: " + (departList != null ? departList.size() : 0)); for (Map depart : departList) { String departId = (String) depart.get("id"); Object parentIdObj = depart.get("parent_id"); System.out.println("departId: " + departId + ", parentId: " + parentIdObj); if (parentIdObj != null) { String parentIdStr = parentIdObj.toString(); if (parentIdStr != null && !parentIdStr.isEmpty()) { String[] parentIds = parentIdStr.split(","); for (String pid : parentIds) { if (!pid.trim().isEmpty()) { idToParentIdMap.put(departId, pid.trim()); System.out.println(" -> 父子关系: " + departId + " -> " + pid.trim()); } } } } } System.out.println("idToParentIdMap size: " + idToParentIdMap.size()); System.out.println("idOilCountMap: " + idOilCountMap); System.out.println("idCarCountMap: " + idCarCountMap); System.out.println("idStationCountMap: " + idStationCountMap); System.out.println("idOilVolumeMap: " + idOilVolumeMap); Map finalOilCountMap = new HashMap<>(idOilCountMap); Map finalCarCountMap = new HashMap<>(idCarCountMap); Map finalStationCountMap = new HashMap<>(idStationCountMap); Map finalOilVolumeMap = new HashMap<>(idOilVolumeMap); for (Map.Entry entry : idToParentIdMap.entrySet()) { String childId = entry.getKey(); String parentIdStr = entry.getValue(); if (parentIdStr != null && !parentIdStr.isEmpty()) { Integer childOilCount = idOilCountMap.get(childId); if (childOilCount != null) { finalOilCountMap.merge(parentIdStr, childOilCount, Integer::sum); System.out.println("累加加油数: 父节点 " + parentIdStr + " += 子节点 " + childId + " 的 " + childOilCount); } Integer childCarCount = idCarCountMap.get(childId); if (childCarCount != null) { finalCarCountMap.merge(parentIdStr, childCarCount, Integer::sum); System.out.println("累加车流量: 父节点 " + parentIdStr + " += 子节点 " + childId + " 的 " + childCarCount); } Integer childStationCount = idStationCountMap.get(childId); if (childStationCount != null) { finalStationCountMap.merge(parentIdStr, childStationCount, Integer::sum); System.out.println("累加进站数: 父节点 " + parentIdStr + " += 子节点 " + childId + " 的 " + childStationCount); } Integer childOilVolume = idOilVolumeMap.get(childId); if (childOilVolume != null) { finalOilVolumeMap.merge(parentIdStr, childOilVolume, Integer::sum); System.out.println("累加油品销量: 父节点 " + parentIdStr + " += 子节点 " + childId + " 的 " + childOilVolume); } } } System.out.println("finalOilCountMap: " + finalOilCountMap); System.out.println("finalCarCountMap: " + finalCarCountMap); System.out.println("finalStationCountMap: " + finalStationCountMap); System.out.println("finalOilVolumeMap: " + finalOilVolumeMap); for (Map item : oilCountList) { String departId = (String) item.get("depart_id"); Integer finalOilCount = finalOilCountMap.get(departId); if (finalOilCount != null) { item.put("oilCount", finalOilCount); System.out.println("更新 " + item.get("depart_name") + " 的加油数: " + finalOilCount); } Integer finalCarCount = finalCarCountMap.get(departId); if (finalCarCount != null) { item.put("carCount", finalCarCount); System.out.println("更新 " + item.get("depart_name") + " 的车流量: " + finalCarCount); } Integer finalStationCount = finalStationCountMap.get(departId); if (finalStationCount != null) { item.put("stationCount", finalStationCount); System.out.println("更新 " + item.get("depart_name") + " 的进站数: " + finalStationCount); } Integer finalOilVolume = finalOilVolumeMap.get(departId); if (finalOilVolume != null) { item.put("oilVolume", finalOilVolume); System.out.println("更新 " + item.get("depart_name") + " 的油品销量: " + finalOilVolume); } } System.out.println("=== queryOrgOilCount 结束 ==="); return oilCountList; } }