| | |
| | | |
| | | 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); |
| | | } |
| | | |
| | | } |