huang
2025-06-12 e206ea8f7dbb655c0d8868996dae8ff1ff5ed11a
JiuMuMesParent/moduleService/DeviceInteractionModule/src/main/java/com/mes/md/service/impl/TaskingLogServiceImpl.java
@@ -75,8 +75,12 @@
                taskingWrapper.apply("operation_record REGEXP '.*[^0-9]" + lineType + "$'");
            }
            // 按时间排序
            taskingWrapper.orderByDesc("operation_record_time");
            // 按时间排序(降序)
            //taskingWrapper.orderByDesc("operation_record_time");
            //(升序)
            taskingWrapper.orderByAsc("operation_record_time");
            // 先按sortOrder排序,再按时间排序
            //taskingWrapper.orderByAsc("sort_order", "operation_record_time");
            // 执行查询
            List<Map<String, Object>> taskingList = baseMapper.selectMaps(taskingWrapper);
@@ -116,7 +120,6 @@
        List<Map<String, Object>> listTasking1 = baseMapper.selectMaps(new QueryWrapper<TaskingLog>()
                .select("task_type, operation_record, operation_mode, DATE_FORMAT(operation_record_time, '%Y-%m-%d') as operation_record_time, count(*) as count")
                .eq("task_type", "定制")
                .eq("operation_record", "旋转1")
                .eq("operation_mode", "结束")
                .gt("operation_record_time", startDate)
@@ -124,7 +127,6 @@
                .orderByAsc("DATE_FORMAT(operation_record_time, '%Y-%m-%d')"));
        List<Map<String, Object>> listTasking2 = baseMapper.selectMaps(new QueryWrapper<TaskingLog>()
                .select("task_type,operation_record,operation_mode,DATE_FORMAT(operation_record_time, '%Y-%m-%d') as operation_record_time,count(1) as count")
                .eq("task_type", "定制")
                .eq("operation_record", "旋转2")
                .eq("operation_mode", "结束")
                .gt("operation_record_time", startDate)
@@ -274,4 +276,150 @@
        }
        return 0;
    }
    /**
     * 查询单小时产量
     * @param dayCount 查询天数
     * @return Map 包含每天两条线的平均小时产量
     */
    @Override
    public Map<String, Object> findHourlyOutput(int dayCount) {
        try {
            // 计算开始时间
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            cal.add(Calendar.DATE, -dayCount + 1);
            Date startDate = cal.getTime();
            // 查询一线数据,按天分组统计总产量和工作小时数
            QueryWrapper<TaskingLog> line1Query = new QueryWrapper<>();
            line1Query.select(
                    "DATE_FORMAT(operation_record_time, '%Y-%m-%d') as date",
                    "COUNT(*) as total_count",
                    "COUNT(DISTINCT DATE_FORMAT(operation_record_time, '%H')) as working_hours"
                )
                .eq("operation_record", "旋转1")
                .eq("operation_mode", "结束")
                .ge("operation_record_time", startDate)
                .groupBy("DATE_FORMAT(operation_record_time, '%Y-%m-%d')")
                .orderByAsc("date");
            List<Map<String, Object>> line1Results = baseMapper.selectMaps(line1Query);
            // 查询二线数据,按天分组统计总产量和工作小时数
            QueryWrapper<TaskingLog> line2Query = new QueryWrapper<>();
            line2Query.select(
                    "DATE_FORMAT(operation_record_time, '%Y-%m-%d') as date",
                    "COUNT(*) as total_count",
                    "COUNT(DISTINCT DATE_FORMAT(operation_record_time, '%H')) as working_hours"
                )
                .eq("operation_record", "旋转2")
                .eq("operation_mode", "结束")
                .ge("operation_record_time", startDate)
                .groupBy("DATE_FORMAT(operation_record_time, '%Y-%m-%d')")
                .orderByAsc("date");
            List<Map<String, Object>> line2Results = baseMapper.selectMaps(line2Query);
            // 合并结果
            Map<String, Object> result = new HashMap<>();
            Map<String, Map<String, Object>> dailyStats = new HashMap<>();
            // 处理一线数据
            for (Map<String, Object> line1Data : line1Results) {
                String date = (String) line1Data.get("date");
                int totalCount = ((Number) line1Data.get("total_count")).intValue();
                int workingHours = ((Number) line1Data.get("working_hours")).intValue();
                Map<String, Object> dailyData = new HashMap<>();
                dailyData.put("date", date);
                dailyData.put("line1Count", totalCount);
                dailyData.put("line1Hours", workingHours);
                dailyData.put("line1HourlyAvg", workingHours > 0 ? totalCount / workingHours : 0);
                dailyStats.put(date, dailyData);
            }
            // 处理二线数据
            for (Map<String, Object> line2Data : line2Results) {
                String date = (String) line2Data.get("date");
                int totalCount = ((Number) line2Data.get("total_count")).intValue();
                int workingHours = ((Number) line2Data.get("working_hours")).intValue();
                Map<String, Object> dailyData = dailyStats.computeIfAbsent(date, k -> {
                    Map<String, Object> newData = new HashMap<>();
                    newData.put("date", date);
                    return newData;
                });
                dailyData.put("line2Count", totalCount);
                dailyData.put("line2Hours", workingHours);
                dailyData.put("line2HourlyAvg", workingHours > 0 ? totalCount / workingHours : 0);
            }
            // 转换为最终的返回格式
            List<Map<String, Object>> finalStats = new ArrayList<>(dailyStats.values());
            // 按日期排序
            finalStats.sort((a, b) -> ((String)a.get("date")).compareTo((String)b.get("date")));
            result.put("dailyStats", finalStats);
            return result;
        } catch (Exception e) {
            log.error("计算单小时产量失败", e);
            throw new RuntimeException("计算单小时产量失败: " + e.getMessage());
        }
    }
    /**
     * 查询库位数据
     * 按库位统计:
     * - 标准工艺:统计上片1和上片2的记录
     * - 定制工艺:统计旋转1和旋转2的记录
     */
    @Override
    public List<Map<String, Object>> selectWareHouse(int dayCount) {
        try {
            // 计算开始时间
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            cal.add(Calendar.DATE, -dayCount + 1);
            Date startDate = cal.getTime();
            // 使用QueryWrapper构建查询
            QueryWrapper<TaskingLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.select(
                    "DATE_FORMAT(operation_record_time, '%Y-%m-%d') as date",
                    "warehouse",
                    "COUNT(*) as count"
                )
                .and(wrapper -> wrapper
                    .and(w -> w
                        .eq("task_type", "标准")
                        .in("operation_record", "上片1", "上片2")
                    )
                    .or(w -> w
                        .eq("task_type", "定制")
                        .in("operation_record", "旋转1", "旋转2")
                    )
                )
                .eq("operation_mode", "结束")
                .ge("operation_record_time", startDate)
                .groupBy("DATE_FORMAT(operation_record_time, '%Y-%m-%d')", "warehouse")
                .orderByAsc("date", "warehouse");
            return baseMapper.selectMaps(queryWrapper);
        } catch (Exception e) {
            log.error("查询库位数据失败", e);
            throw new RuntimeException("查询库位数据失败: " + e.getMessage());
        }
    }
}