| | |
| | | |
| | | return new ArrayList<>(resultMap.values()); |
| | | } |
| | | @Override |
| | | public List<RawMaterialProductionDto> rawMaterialProductions(String month) { |
| | | YearMonth yearMonth; |
| | | try { |
| | | yearMonth = YearMonth.parse(month); |
| | | } catch (Exception e) { |
| | | log.error("解析月份失败: {}", month); |
| | | return Collections.emptyList(); |
| | | } |
| | | LocalDateTime monthStart = yearMonth.atDay(1).atStartOfDay(); |
| | | LocalDateTime monthEnd = yearMonth.atEndOfMonth().atTime(LocalTime.MAX); |
| | | |
| | | // 获取产品类型字典 |
| | | List<SysDictData> sysDictDataList = sysDictDataMapper.selectDictDataByType("product_type"); |
| | | Map<Long, String> dictCodeToLabelMap = sysDictDataList.stream() |
| | | .filter(d -> d.getDictCode() != null && d.getDictLabel() != null) |
| | | .collect(Collectors.toMap(SysDictData::getDictCode, SysDictData::getDictLabel)); |
| | | |
| | | // 查询当月报工主表 |
| | | List<ProductionProductMain> mainList = productionProductMainService.list(Wrappers.<ProductionProductMain>lambdaQuery() |
| | | .between(ProductionProductMain::getReportingTime, monthStart, monthEnd)); |
| | | |
| | | if (CollectionUtils.isEmpty(mainList)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | List<Long> mainIds = mainList.stream().map(ProductionProductMain::getId).collect(Collectors.toList()); |
| | | List<Long> orderIds = mainList.stream().map(ProductionProductMain::getProductOrderId).distinct().collect(Collectors.toList()); |
| | | Map<Long, ProductionProductMain> mainMap = mainList.stream().collect(Collectors.toMap(ProductionProductMain::getId, m -> m)); |
| | | |
| | | // 获取订单对应的产品类型 |
| | | Map<Long, Long> orderRouteMap = new HashMap<>(); |
| | | if (!CollectionUtils.isEmpty(orderIds)) { |
| | | List<ProductionOrderRoute> routes = productionOrderRouteService.list(Wrappers.<ProductionOrderRoute>lambdaQuery() |
| | | .in(ProductionOrderRoute::getOrderId, orderIds)); |
| | | orderRouteMap = routes.stream() |
| | | .filter(r -> r.getOrderId() != null && r.getDictCode() != null) |
| | | .collect(Collectors.toMap(ProductionOrderRoute::getOrderId, ProductionOrderRoute::getDictCode, (v1, v2) -> v1)); |
| | | } |
| | | |
| | | // 获取投入和产出明细 |
| | | List<ProductionProductInput> allInputs = productionProductInputService.list(Wrappers.<ProductionProductInput>lambdaQuery() |
| | | .in(ProductionProductInput::getProductMainId, mainIds)); |
| | | List<ProductionProductOutput> allOutputs = productionProductOutputService.list(Wrappers.<ProductionProductOutput>lambdaQuery() |
| | | .in(ProductionProductOutput::getProductMainId, mainIds)); |
| | | |
| | | // 获取 SKU 的物料名称 |
| | | Set<Long> inputSkuIds = allInputs.stream().map(ProductionProductInput::getProductId).filter(Objects::nonNull).collect(Collectors.toSet()); |
| | | Map<Long, String> skuToMaterialNameMap = new HashMap<>(); |
| | | if (!inputSkuIds.isEmpty()) { |
| | | List<ProductMaterialSku> skus = productMaterialSkuService.listByIds(inputSkuIds); |
| | | Set<Long> productIds = skus.stream().map(ProductMaterialSku::getProductId).filter(Objects::nonNull).collect(Collectors.toSet()); |
| | | Map<Long, String> materialNameMap = productMaterialService.listByIds(productIds).stream() |
| | | .collect(Collectors.toMap(ProductMaterial::getId, ProductMaterial::getProductName)); |
| | | skuToMaterialNameMap = skus.stream() |
| | | .filter(s -> s.getProductId() != null && materialNameMap.containsKey(s.getProductId())) |
| | | .collect(Collectors.toMap(ProductMaterialSku::getId, s -> materialNameMap.get(s.getProductId()))); |
| | | } |
| | | |
| | | BigDecimal yield35 = BigDecimal.ZERO; |
| | | BigDecimal yield50 = BigDecimal.ZERO; |
| | | BigDecimal yieldPlate = BigDecimal.ZERO; |
| | | |
| | | // 材质名称 -> [3.5用量, 5.0用量, 板材用量] |
| | | Map<String, BigDecimal[]> materialConsumptionMap = new LinkedHashMap<>(); |
| | | |
| | | // 统计产量 |
| | | for (ProductionProductOutput output : allOutputs) { |
| | | ProductionProductMain main = mainMap.get(output.getProductMainId()); |
| | | if (main == null) continue; |
| | | Long dictCode = orderRouteMap.get(main.getProductOrderId()); |
| | | String label = dictCodeToLabelMap.get(dictCode); |
| | | BigDecimal qty = output.getQuantity() != null ? output.getQuantity() : BigDecimal.ZERO; |
| | | |
| | | if (label != null) { |
| | | if (label.contains("3.5")) yield35 = yield35.add(qty); |
| | | else if (label.contains("5.0")) yield50 = yield50.add(qty); |
| | | else if (label.contains("板材")) yieldPlate = yieldPlate.add(qty); |
| | | } |
| | | } |
| | | |
| | | // 统计消耗量 |
| | | for (ProductionProductInput input : allInputs) { |
| | | ProductionProductMain main = mainMap.get(input.getProductMainId()); |
| | | if (main == null) continue; |
| | | Long dictCode = orderRouteMap.get(main.getProductOrderId()); |
| | | String label = dictCodeToLabelMap.get(dictCode); |
| | | String materialName = skuToMaterialNameMap.get(input.getProductId()); |
| | | if (materialName == null) continue; |
| | | |
| | | BigDecimal qty = UnitUtils.convertValueToTon(input.getQuantity(), input.getUnit()); |
| | | |
| | | materialConsumptionMap.putIfAbsent(materialName, new BigDecimal[]{BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO}); |
| | | BigDecimal[] consumptions = materialConsumptionMap.get(materialName); |
| | | |
| | | if (label != null) { |
| | | if (label.contains("3.5")) consumptions[0] = consumptions[0].add(qty); |
| | | else if (label.contains("5.0")) consumptions[1] = consumptions[1].add(qty); |
| | | else if (label.contains("板材")) consumptions[2] = consumptions[2].add(qty); |
| | | } |
| | | } |
| | | |
| | | List<RawMaterialProductionDto> result = new ArrayList<>(); |
| | | |
| | | // 产量行 |
| | | RawMaterialProductionDto yieldRow = new RawMaterialProductionDto(); |
| | | yieldRow.setItemName("产量"); |
| | | yieldRow.setUsage35(yield35); |
| | | yieldRow.setUsage50(yield50); |
| | | yieldRow.setUsagePlate(yieldPlate); |
| | | yieldRow.setTotalUsage(yield35.add(yield50).add(yieldPlate)); |
| | | yieldRow.setBlockTotalUsage(yield35.add(yield50)); |
| | | result.add(yieldRow); |
| | | |
| | | // 物料行 |
| | | List<String> targetMaterials = Arrays.asList( |
| | | "粉煤灰", "水泥", "石灰", "铝粉", "石膏", "脱模剂", |
| | | "打包带", "冷拔丝", "氧化镁", "卡扣", "防腐剂" |
| | | ); |
| | | |
| | | for (String materialName : targetMaterials) { |
| | | BigDecimal[] consumptions = materialConsumptionMap.getOrDefault(materialName, |
| | | new BigDecimal[]{BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO}); |
| | | |
| | | RawMaterialProductionDto row = new RawMaterialProductionDto(); |
| | | row.setItemName(materialName); |
| | | row.setUsage35(consumptions[0]); |
| | | row.setUsage50(consumptions[1]); |
| | | row.setUsagePlate(consumptions[2]); |
| | | |
| | | // 计算单耗 |
| | | row.setUnitConsumption35(calculateUnitConsumption(consumptions[0], yield35)); |
| | | row.setUnitConsumption50(calculateUnitConsumption(consumptions[1], yield50)); |
| | | row.setUnitConsumptionPlate(calculateUnitConsumption(consumptions[2], yieldPlate)); |
| | | |
| | | row.setTotalUsage(consumptions[0].add(consumptions[1]).add(consumptions[2])); |
| | | row.setBlockTotalUsage(consumptions[0].add(consumptions[1])); |
| | | result.add(row); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | private BigDecimal calculateUnitConsumption(BigDecimal usage, BigDecimal yield) { |
| | | if (yield == null || yield.compareTo(BigDecimal.ZERO) == 0) { |
| | | return BigDecimal.ZERO; |
| | | } |
| | | return usage.divide(yield, 4, RoundingMode.HALF_UP); |
| | | } |
| | | |
| | | } |