|
|
@@ -7,19 +7,12 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.time.format.TextStyle;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Locale;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 大屏统计服务实现类
|
|
|
@@ -35,7 +28,7 @@ public class BigViewMesServiceImpl implements IBigViewMesService {
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> getProductTotal() {
|
|
|
-
|
|
|
+
|
|
|
// 直接从mapper查询产品总量
|
|
|
Map<String, Object> result = bigViewMesMapper.getProductTotal();
|
|
|
|
|
|
@@ -61,6 +54,137 @@ public class BigViewMesServiceImpl implements IBigViewMesService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getTotalProductionAchievementRate() {
|
|
|
+ // 获取生产达成率统计数据
|
|
|
+ Map<String, Object> result = bigViewMesMapper.getTotalProductionAchievementRate();
|
|
|
+
|
|
|
+ // 添加统计时间
|
|
|
+ result.put("statisticsTime", DateUtil.now());
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> getProductionWorkorderMonitoring() {
|
|
|
+ // 获取生产实时监控数据
|
|
|
+ List<Map<String, Object>> result = bigViewMesMapper.getProductionWorkorderMonitoring();
|
|
|
+
|
|
|
+ // 为每条记录添加统计时间
|
|
|
+ for (Map<String, Object> record : result) {
|
|
|
+ record.put("statisticsTime", DateUtil.now());
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getSafeProductionDays(String endDate) {
|
|
|
+ try {
|
|
|
+ // 解析传入的日期
|
|
|
+ LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
|
+ // 获取当前日期
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
+
|
|
|
+ // 计算两个日期之间的天数差(绝对值)
|
|
|
+ long days = Math.abs(ChronoUnit.DAYS.between(now, end));
|
|
|
+
|
|
|
+ return (int) days;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("日期格式错误,请使用yyyy-MM-dd格式", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> getProductReceiptStatisticsByItemCodes(String itemCodes) {
|
|
|
+ // 获取原始数据
|
|
|
+ List<Map<String, Object>> rawData = bigViewMesMapper.selectProductReceiptStatisticsByItemCodes(itemCodes);
|
|
|
+
|
|
|
+ // 生成过去7天的日期列表
|
|
|
+ Map<String, Map<String, Object>> groupedByDate = new TreeMap<>(Collections.reverseOrder());
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ for (int i = 0; i < 7; i++) {
|
|
|
+ LocalDate date = today.minusDays(i);
|
|
|
+ String dateStr = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
|
+ String dayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE);
|
|
|
+
|
|
|
+ Map<String, Object> dayData = new HashMap<>();
|
|
|
+ dayData.put("date", dateStr);
|
|
|
+ dayData.put("dayOfWeek", dayOfWeek);
|
|
|
+ dayData.put("products", new HashMap<String, Object>());
|
|
|
+
|
|
|
+ groupedByDate.put(dateStr, dayData);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按日期和产品编码分组统计数据
|
|
|
+ Map<String, Map<String, List<Map<String, Object>>>> groupedData = new HashMap<>();
|
|
|
+ Set<String> allItemCodes = new HashSet<>();
|
|
|
+ Map<String, String> itemNames = new HashMap<>();
|
|
|
+ Map<String, String> unitOfMeasures = new HashMap<>();
|
|
|
+
|
|
|
+ // 第一次遍历:按日期和产品分组原始数据
|
|
|
+ for (Map<String, Object> record : rawData) {
|
|
|
+ Date receiptDate = (Date) record.get("date");
|
|
|
+ String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(receiptDate);
|
|
|
+ String itemCode = (String) record.get("item_code");
|
|
|
+ String itemName = (String) record.get("item_name");
|
|
|
+ String unitOfMeasure = (String) record.get("unit_of_measure");
|
|
|
+
|
|
|
+ if (itemCode != null && itemName != null) {
|
|
|
+ allItemCodes.add(itemCode);
|
|
|
+ itemNames.put(itemCode, itemName);
|
|
|
+ unitOfMeasures.put(itemCode, unitOfMeasure);
|
|
|
+
|
|
|
+ // 只处理7天内的数据
|
|
|
+ if (groupedByDate.containsKey(dateStr)) {
|
|
|
+ groupedData.computeIfAbsent(dateStr, k -> new HashMap<>())
|
|
|
+ .computeIfAbsent(itemCode, k -> new ArrayList<>())
|
|
|
+ .add(record);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理分组后的数据
|
|
|
+ for (Map.Entry<String, Map<String, Object>> dateEntry : groupedByDate.entrySet()) {
|
|
|
+ String dateStr = dateEntry.getKey();
|
|
|
+ Map<String, Object> dayData = dateEntry.getValue();
|
|
|
+ Map<String, Object> products = (Map<String, Object>) dayData.get("products");
|
|
|
+
|
|
|
+ // 处理每个产品的数据
|
|
|
+ for (String itemCode : allItemCodes) {
|
|
|
+ // 创建产品统计数据
|
|
|
+ Map<String, Object> productData = new HashMap<>();
|
|
|
+ productData.put("itemCode", itemCode);
|
|
|
+ productData.put("itemName", itemNames.get(itemCode));
|
|
|
+ productData.put("unitOfMeasure", unitOfMeasures.get(itemCode));
|
|
|
+
|
|
|
+ // 获取当天该产品的记录
|
|
|
+ List<Map<String, Object>> productRecords = groupedData
|
|
|
+ .getOrDefault(dateStr, new HashMap<>())
|
|
|
+ .getOrDefault(itemCode, new ArrayList<>());
|
|
|
+
|
|
|
+ if (!productRecords.isEmpty()) {
|
|
|
+ // 统计该产品当天的数据
|
|
|
+ int receiptCount = productRecords.size();
|
|
|
+ double totalQuantity = productRecords.stream()
|
|
|
+ .mapToDouble(r -> ((Number) r.get("quantity_recived")).doubleValue())
|
|
|
+ .sum();
|
|
|
+
|
|
|
+ productData.put("receiptCount", receiptCount);
|
|
|
+ productData.put("totalQuantity", totalQuantity);
|
|
|
+ } else {
|
|
|
+ // 当天无数据时填充为0
|
|
|
+ productData.put("receiptCount", 0);
|
|
|
+ productData.put("totalQuantity", 0.0);
|
|
|
+ }
|
|
|
+
|
|
|
+ products.put(itemCode, productData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return new ArrayList<>(groupedByDate.values());
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Map<String, Object> getMonthlyWorkorderCompletion() {
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
@@ -262,59 +386,97 @@ public class BigViewMesServiceImpl implements IBigViewMesService {
|
|
|
// 获取原始数据
|
|
|
List<Map<String, Object>> rawData = bigViewMesMapper.selectProductionWorkorderStatisticsByProductIds(productIds);
|
|
|
|
|
|
- // 按日期分组
|
|
|
- Map<String, Map<String, Object>> groupedByDate = new HashMap<>();
|
|
|
- Set<String> productNames = new HashSet<>();
|
|
|
-
|
|
|
- // 第一次遍历:收集所有产品名称并按日期分组
|
|
|
- for (Map<String, Object> record : rawData) {
|
|
|
- String date = (String) record.get("statisticsDate");
|
|
|
- String productName = (String) record.get("productName");
|
|
|
- productNames.add(productName);
|
|
|
+ // 生成过去7天的日期列表
|
|
|
+ Map<String, Map<String, Object>> groupedByDate = new TreeMap<>(Collections.reverseOrder());
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ for (int i = 0; i < 7; i++) {
|
|
|
+ LocalDate date = today.minusDays(i);
|
|
|
+ String dateStr = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
|
+ String dayOfWeek = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE);
|
|
|
|
|
|
- // 初始化或获取该日期的数据
|
|
|
- Map<String, Object> dayData = groupedByDate.computeIfAbsent(date, k -> {
|
|
|
- Map<String, Object> newDay = new HashMap<>();
|
|
|
- newDay.put("date", date);
|
|
|
- // 获取星期几
|
|
|
- try {
|
|
|
- LocalDate localDate = LocalDate.parse(date);
|
|
|
- String dayOfWeek = localDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE);
|
|
|
- newDay.put("dayOfWeek", dayOfWeek);
|
|
|
- } catch (Exception e) {
|
|
|
- newDay.put("dayOfWeek", "未知");
|
|
|
- }
|
|
|
- return newDay;
|
|
|
- });
|
|
|
+ Map<String, Object> dayData = new HashMap<>();
|
|
|
+ dayData.put("date", dateStr);
|
|
|
+ dayData.put("dayOfWeek", dayOfWeek);
|
|
|
+ dayData.put("products", new HashMap<String, Object>());
|
|
|
|
|
|
- // 将产品数据添加到日期数据中
|
|
|
- dayData.put(productName, new HashMap<String, Object>() {{
|
|
|
- put("workorderCount", record.get("workorderCount"));
|
|
|
- put("totalQuantity", record.get("totalQuantity"));
|
|
|
- put("totalProducedQuantity", record.get("totalProducedQuantity"));
|
|
|
- put("totalIncompleteQuantity", record.get("totalIncompleteQuantity"));
|
|
|
- }});
|
|
|
+ groupedByDate.put(dateStr, dayData);
|
|
|
}
|
|
|
|
|
|
- // 确保每天都有所有产品的数据,没有的补零
|
|
|
- for (Map<String, Object> dayData : groupedByDate.values()) {
|
|
|
- for (String productName : productNames) {
|
|
|
- if (!dayData.containsKey(productName)) {
|
|
|
- dayData.put(productName, new HashMap<String, Object>() {{
|
|
|
- put("workorderCount", 0);
|
|
|
- put("totalQuantity", 0);
|
|
|
- put("totalProducedQuantity", 0);
|
|
|
- put("totalIncompleteQuantity", 0);
|
|
|
- }});
|
|
|
+ // 按日期和产品ID分组统计数据
|
|
|
+ Map<String, Map<String, List<Map<String, Object>>>> groupedData = new HashMap<>();
|
|
|
+ Set<String> allProductIds = new HashSet<>();
|
|
|
+ Map<String, String> productNames = new HashMap<>();
|
|
|
+
|
|
|
+ // 第一次遍历:按日期和产品分组原始数据
|
|
|
+ for (Map<String, Object> record : rawData) {
|
|
|
+ Date createTime = (Date) record.get("date");
|
|
|
+ String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(createTime);
|
|
|
+ String productId = String.valueOf(record.get("product_id"));
|
|
|
+ String productName = (String) record.get("product_name");
|
|
|
+
|
|
|
+ if (productId != null && productName != null) {
|
|
|
+ allProductIds.add(productId);
|
|
|
+ productNames.put(productId, productName);
|
|
|
+
|
|
|
+ // 只处理7天内的数据
|
|
|
+ if (groupedByDate.containsKey(dateStr)) {
|
|
|
+ groupedData.computeIfAbsent(dateStr, k -> new HashMap<>())
|
|
|
+ .computeIfAbsent(productId, k -> new ArrayList<>())
|
|
|
+ .add(record);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 转换为列表并按日期排序
|
|
|
- List<Map<String, Object>> result = new ArrayList<>(groupedByDate.values());
|
|
|
- result.sort((a, b) -> ((String) a.get("date")).compareTo((String) b.get("date")));
|
|
|
+ // 处理分组后的数据
|
|
|
+ for (Map.Entry<String, Map<String, Object>> dateEntry : groupedByDate.entrySet()) {
|
|
|
+ String dateStr = dateEntry.getKey();
|
|
|
+ Map<String, Object> dayData = dateEntry.getValue();
|
|
|
+ Map<String, Object> products = (Map<String, Object>) dayData.get("products");
|
|
|
+
|
|
|
+ // 处理每个产品的数据
|
|
|
+ for (String productId : allProductIds) {
|
|
|
+ List<Map<String, Object>> productRecords = groupedData
|
|
|
+ .getOrDefault(dateStr, new HashMap<>())
|
|
|
+ .getOrDefault(productId, new ArrayList<>());
|
|
|
+
|
|
|
+ // 统计该产品当天的数据
|
|
|
+ int workorderCount = productRecords.size();
|
|
|
+ int totalQuantity = productRecords.stream()
|
|
|
+ .mapToInt(r -> ((Number) r.get("quantity")).intValue())
|
|
|
+ .sum();
|
|
|
+ int producedQuantity = productRecords.stream()
|
|
|
+ .mapToInt(r -> r.get("quantity_produced") != null ?
|
|
|
+ ((Number) r.get("quantity_produced")).intValue() : 0)
|
|
|
+ .sum();
|
|
|
+ int scheduledQuantity = productRecords.stream()
|
|
|
+ .mapToInt(r -> r.get("quantity_scheduled") != null ?
|
|
|
+ ((Number) r.get("quantity_scheduled")).intValue() : 0)
|
|
|
+ .sum();
|
|
|
+ int changedQuantity = productRecords.stream()
|
|
|
+ .mapToInt(r -> r.get("quantity_changed") != null ?
|
|
|
+ ((Number) r.get("quantity_changed")).intValue() : 0)
|
|
|
+ .sum();
|
|
|
+
|
|
|
+ // 创建产品统计数据
|
|
|
+ Map<String, Object> productData = new HashMap<>();
|
|
|
+ productData.put("productId", productId);
|
|
|
+ productData.put("productName", productNames.get(productId));
|
|
|
+ productData.put("workorderCount", workorderCount);
|
|
|
+ productData.put("totalQuantity", totalQuantity);
|
|
|
+ productData.put("producedQuantity", producedQuantity);
|
|
|
+ productData.put("scheduledQuantity", scheduledQuantity);
|
|
|
+ productData.put("changedQuantity", changedQuantity);
|
|
|
+ productData.put("incompleteQuantity", totalQuantity - producedQuantity);
|
|
|
+ productData.put("completionRate", totalQuantity > 0 ?
|
|
|
+ Math.round(producedQuantity * 100.0 / totalQuantity * 100) / 100.0 : 0.00);
|
|
|
+ productData.put("schedulingRate", totalQuantity > 0 ?
|
|
|
+ Math.round(scheduledQuantity * 100.0 / totalQuantity * 100) / 100.0 : 0.00);
|
|
|
+
|
|
|
+ products.put(productId, productData);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return result;
|
|
|
+ return new ArrayList<>(groupedByDate.values());
|
|
|
}
|
|
|
|
|
|
@Override
|