From 3faea5bad90c8e55e8bf83f7a9c603f789ed6c84 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 10 四月 2026 11:41:46 +0800
Subject: [PATCH] feat: 原料生产统计单耗表接口
---
src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java | 227 ++++++++++++++++++++++++++++++++++++++++----------------
1 files changed, 160 insertions(+), 67 deletions(-)
diff --git a/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java b/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java
index 996cd1c..acd2b54 100644
--- a/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java
+++ b/src/main/java/com/ruoyi/home/service/impl/HomeServiceImpl.java
@@ -2566,7 +2566,7 @@
lastDay = date.with(TemporalAdjusters.lastDayOfYear());
}
LocalDate finalLastDay = lastDay;
- salesDeliveries = salesDeliveries
+ List<SalesDelivery> salesDeliverie = salesDeliveries
.stream()
.filter(delivery -> {
LocalDate deliveryDate = delivery.getDeliveryDate();
@@ -2578,9 +2578,9 @@
AddressRegionEnum::getRegionName, // 鍖哄煙鍚嶄綔涓簁ey
enumItem -> 0L // 鍒濆鍊煎叏閮ㄤ负0
));
- if (!CollectionUtils.isEmpty(salesDeliveries)) {
+ if (!CollectionUtils.isEmpty(salesDeliverie)) {
// 鎸夊尯鍩熷垎缁勶紝缁熻姣忎釜鍖哄煙鐨勯攢閲忔�诲拰
- regionCountMap = salesDeliveries.stream()
+ regionCountMap = salesDeliverie.stream()
.filter(delivery -> delivery.getDeliveryPlace() != null)
.collect(Collectors.groupingBy(
delivery -> {
@@ -2590,7 +2590,7 @@
Collectors.summingLong(delivery -> delivery.getVolume() != null ? delivery.getVolume().longValue() : 0L)
));
}
- regionCountMap.put("鍏ㄩ儴", salesDeliveries.stream()
+ regionCountMap.put("鍏ㄩ儴", salesDeliverie.stream()
.mapToLong(item -> item.getVolume() != null ? item.getVolume().longValue() : 0L)
.sum());
maps.add(regionCountMap);
@@ -2598,54 +2598,6 @@
salesTotalDto.setDates(dates);
salesTotalDto.setCustomerTrends(maps);
return salesTotalDto;
- }
-
- @Override
- public List<SalesTotalDetailDto> salesRanking(SalesDeliveryDto salesDeliveryDto) {
- List<SalesTotalDetailDto> salesTotalDetailDtos = new ArrayList<>();
- List<LocalDate> dates = convertDateList(salesDeliveryDto.getDays());
- List<SalesDelivery> salesDeliveries = salesDeliveryMapper.selectList(Wrappers.<SalesDelivery>lambdaQuery()
- .eq(SalesDelivery::getProductName, salesDeliveryDto.getType()));
- for (LocalDate date : dates) {
- LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
- LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
- if (salesDeliveryDto.getDays().equals("骞�")) {
- lastDay = date.with(TemporalAdjusters.lastDayOfYear());
- }
- LocalDate finalLastDay = lastDay;
- salesDeliveries = salesDeliveries
- .stream()
- .filter(delivery -> {
- LocalDate deliveryDate = delivery.getDeliveryDate();
- return !deliveryDate.isBefore(firstDay) && !deliveryDate.isAfter(finalLastDay);
- })
- .collect(Collectors.toList());
- Map<String, Long> regionCountMap = Arrays.stream(AddressRegionEnum.values())
- .collect(Collectors.toMap(
- AddressRegionEnum::getRegionName, // 鍖哄煙鍚嶄綔涓簁ey
- enumItem -> 0L // 鍒濆鍊煎叏閮ㄤ负0
- ));
- if (!CollectionUtils.isEmpty(salesDeliveries)) {
- // 鎸夊尯鍩熷垎缁勶紝缁熻姣忎釜鍖哄煙鐨勯攢閲忔�诲拰
- regionCountMap = salesDeliveries.stream()
- .filter(delivery -> delivery.getDeliveryPlace() != null)
- .collect(Collectors.groupingBy(
- delivery -> {
- AddressRegionEnum regionEnum = AddressRegionEnum.matchRegion(delivery.getDeliveryPlace());
- return regionEnum != null ? regionEnum.getRegionName() : null;
- },
- Collectors.summingLong(delivery -> delivery.getVolume() != null ? delivery.getVolume().longValue() : 0L)
- ));
- }
- regionCountMap.put("鍏ㄩ儴", salesDeliveries.stream()
- .mapToLong(item -> item.getVolume() != null ? item.getVolume().longValue() : 0L)
- .sum());
- SalesTotalDetailDto salesTotalDetailDto = new SalesTotalDetailDto();
- salesTotalDetailDto.setDate(date);
- salesTotalDetailDto.setType(salesDeliveryDto.getType());
- salesTotalDetailDtos.add(salesTotalDetailDto);
- }
- return salesTotalDetailDtos;
}
@Override
@@ -2662,7 +2614,7 @@
lastDay = date.with(TemporalAdjusters.lastDayOfYear());
}
LocalDate finalLastDay = lastDay;
- salesDeliveries = salesDeliveries
+ List<SalesDelivery> salesDeliverie = salesDeliveries
.stream()
.filter(delivery -> {
LocalDate deliveryDate = delivery.getDeliveryDate();
@@ -2674,9 +2626,9 @@
AddressRegionEnum::getRegionName, // 鍖哄煙鍚嶄綔涓簁ey
enumItem -> 0L // 鍒濆鍊煎叏閮ㄤ负0
));
- if (!CollectionUtils.isEmpty(salesDeliveries)) {
+ if (!CollectionUtils.isEmpty(salesDeliverie)) {
// 鎸夊尯鍩熷垎缁勶紝缁熻姣忎釜鍖哄煙鐨勯攢閲忔�诲拰
- regionCountMap = salesDeliveries.stream()
+ regionCountMap = salesDeliverie.stream()
.filter(delivery -> delivery.getDeliveryPlace() != null)
.collect(Collectors.groupingBy(
delivery -> {
@@ -2686,7 +2638,7 @@
Collectors.summingLong(delivery -> delivery.getPrice() != null ? delivery.getPrice().longValue() : 0L)
));
}
- regionCountMap.put("鍏ㄩ儴", salesDeliveries.stream()
+ regionCountMap.put("鍏ㄩ儴", salesDeliverie.stream()
.mapToLong(item -> item.getPrice() != null ? item.getPrice().longValue() : 0L)
.sum());
maps.add(regionCountMap);
@@ -2694,14 +2646,6 @@
salesTotalDto.setDates(dates);
salesTotalDto.setCustomerTrends(maps);
return salesTotalDto;
- }
-
- @Override
- public List<SalesTotalDetailDto> salesDataRanking(SalesDeliveryDto salesDeliveryDto) {
- List<SalesTotalDetailDto> salesTotalDetailDtos = new ArrayList<>();
- List<LocalDate> dates = convertDateList(salesDeliveryDto.getDays());
-
- return salesTotalDetailDtos;
}
@Override
@@ -3652,8 +3596,8 @@
Map<String, PlanTrendsDto> resultMap = new LinkedHashMap<>();
for (int i = 0; i < points; i++) {
- String label = (type == 3)
- ? startDate.plusMonths(i).format(formatter)
+ String label = (type == 3)
+ ? startDate.plusMonths(i).format(formatter)
: startDate.plusDays(i).format(formatter);
PlanTrendsDto dto = new PlanTrendsDto();
dto.setDateStr(label);
@@ -3662,7 +3606,7 @@
dto.setCompletionVolume(BigDecimal.ZERO);
resultMap.put(label, dto);
}
- // 缁熻璁″垝閲�
+ // 缁熻璁″垝閲�
List<ProductionPlan> plans = productionPlanService.list(Wrappers.<ProductionPlan>lambdaQuery()
.ge(ProductionPlan::getStartDate, Date.from(startDate.atStartOfDay(ZoneId.systemDefault()).toInstant())));
for (ProductionPlan plan : plans) {
@@ -3701,5 +3645,154 @@
return new ArrayList<>(resultMap.values());
}
+ @Override
+ public List<RawMaterialProductionDto> rawMaterialProductions(String month) {
+ YearMonth yearMonth;
+ try {
+ yearMonth = YearMonth.parse(month);
+ } catch (Exception e) {
+ log.error("瑙f瀽鏈堜唤澶辫触: {}", 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);
+ }
}
--
Gitblit v1.9.3