| | |
| | | |
| | | // 4. 批量查询煤种信息并填充到结果中 |
| | | if (!coalIds.isEmpty()) { |
| | | List<CoalInfo> coalInfos = coalInfoMapper.selectBatchIds(coalIds); |
| | | List<CoalInfo> coalInfos = coalInfoMapper.selectByIds(coalIds); |
| | | for (CoalInfo coalInfo : coalInfos) { |
| | | Map<String, Object> record = resultMap.get(coalInfo.getId()); |
| | | if (record != null) { |
| | |
| | | } |
| | | } |
| | | |
| | | // 最终结果是一个List<Map>,每个Map包含合并后的销售数据和对应的煤种信息 |
| | | List<Map<String, Object>> results = new ArrayList<>(resultMap.values()); |
| | | |
| | | //月度数据 |
| | | //查询所有煤种信息 |
| | | List<CoalInfo> allCoalTypes = coalInfoMapper.selectList( |
| | | new QueryWrapper<CoalInfo>().orderByAsc("id") |
| | | ); |
| | | //预计算销量:按coalId分组统计总销量 |
| | | Map<Long, BigDecimal> salesByCoalId = salesRecords.stream() |
| | | .collect(Collectors.groupingBy( |
| | | SalesRecord::getCoalId, |
| | | Collectors.reducing( |
| | | BigDecimal.ZERO, |
| | | SalesRecord::getSaleQuantity, |
| | | BigDecimal::add |
| | | ) |
| | | )); |
| | | |
| | | //构建结果(保持煤种顺序,销量默认为0) |
| | | List<Map<String, Object>> resultMouth = new ArrayList<>(); |
| | | for (CoalInfo coal : allCoalTypes) { |
| | | Map<String, Object> item = new LinkedHashMap<>(); |
| | | item.put("coal", coal.getCoal()); // 煤种名称 |
| | | item.put("saleQuantity", salesByCoalId.getOrDefault(coal.getId(), BigDecimal.valueOf(0))); // 销量 |
| | | resultMouth.add(item); |
| | | } |
| | | |
| | | result.put("revenueAmount", revenueAmount.setScale(2, RoundingMode.HALF_UP)); |
| | | result.put("changeRate", changeRate); |
| | | result.put("trend", trend); |
| | |
| | | result.put("trendQuantity", trendQuantity); |
| | | result.put("revenueDistribution", revenueDistribution); |
| | | result.put("salesResults", results); |
| | | result.put("resultMouth", resultMouth); |
| | | |
| | | return result; |
| | | } |