package com.mes.md.service.impl; import cn.smallbun.screw.core.util.CollectionUtils; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.github.yulichang.base.MPJBaseServiceImpl; import com.mes.md.entity.KBBTJPDrawingBP; import com.mes.md.entity.KBBTProgramsOperationLogBP; import com.mes.md.entity.TaskingLog; import com.mes.md.mapper.KBBTJPDrawingBPMapper; import com.mes.md.mapper.KBBTProgramsOperationLogBPMapper; import com.mes.md.mapper.TaskingLogMapper; import com.mes.md.service.TaskingLogService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author wu * @since 2024-08-28 */ @Slf4j @Service public class TaskingLogServiceImpl extends MPJBaseServiceImpl implements TaskingLogService { @Autowired KBBTProgramsOperationLogBPMapper kBBTProgramsOperationLogBPMapper; @Autowired KBBTJPDrawingBPMapper kBBTJPDrawingBPMapper; @Override public List selectMechanicalReport(int dayCount, Date startDate, Date endDate, String taskType, String operationRecord, String lineType) { try { // 构建查询条件 QueryWrapper taskingWrapper = new QueryWrapper<>(); // 添加完工状态过滤 taskingWrapper.eq("work_state", "完工"); // 时间范围处理 if (startDate != null && endDate != null) { taskingWrapper.ge("operation_record_time", startDate) .le("operation_record_time", endDate); } else if (dayCount > 0) { // 如果没有时间范围,使用默认的dayCount 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, 1 - dayCount); Date defaultStartDate = cal.getTime(); Date defaultEndDate = new Date(); taskingWrapper.ge("operation_record_time", defaultStartDate) .le("operation_record_time", defaultEndDate); } // 添加可选条件 if (taskType != null && !taskType.isEmpty()) { taskingWrapper.eq("task_type", taskType); } if (operationRecord != null && !operationRecord.isEmpty()) { taskingWrapper.like("operation_record", operationRecord); } if (lineType != null && !lineType.isEmpty()) { taskingWrapper.apply("operation_record REGEXP '.*[^0-9]" + lineType + "$'"); } // 按时间排序(降序) //taskingWrapper.orderByDesc("operation_record_time"); //(升序) taskingWrapper.orderByAsc("operation_record_time"); // 先按sortOrder排序,再按时间排序 //taskingWrapper.orderByAsc("sort_order", "operation_record_time"); // 执行查询 List> taskingList = baseMapper.selectMaps(taskingWrapper); // 直接返回查询结果 return new ArrayList<>(taskingList); } catch (Exception e) { log.error("查询异常", e); throw e; } } public List findTaskingLog(){ return new ArrayList(); } /** * 查询 dayCount 天 完工数量-分线路 */ @Override public List selectTaskingLog(int dayCount) { 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, 1-dayCount); Date startDate = cal.getTime(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("CAST(PlanDate AS DATE) AS CreateDate,isNull(sum(task_quantity),0) as task_quantity_sum") .gt("PlanDate",startDate).groupBy("CAST(PlanDate AS DATE)") .orderByAsc("CAST(PlanDate AS DATE)"); List list=kBBTJPDrawingBPMapper.selectMaps((QueryWrapper)queryWrapper); List> listTasking1 = baseMapper.selectMaps(new QueryWrapper() .select("task_type, operation_record, operation_mode, DATE_FORMAT(operation_record_time, '%Y-%m-%d') as operation_record_time, count(*) as count") .eq("operation_record", "旋转1") .eq("operation_mode", "结束") .gt("operation_record_time", startDate) .groupBy("task_type", "operation_record", "operation_mode", "DATE_FORMAT(operation_record_time, '%Y-%m-%d')") .orderByAsc("DATE_FORMAT(operation_record_time, '%Y-%m-%d')")); List> listTasking2 = baseMapper.selectMaps(new QueryWrapper() .select("task_type,operation_record,operation_mode,DATE_FORMAT(operation_record_time, '%Y-%m-%d') as operation_record_time,count(1) as count") .eq("operation_record", "旋转2") .eq("operation_mode", "结束") .gt("operation_record_time", startDate) .groupBy("task_type", "operation_record", "operation_mode", "DATE_FORMAT(operation_record_time, '%Y-%m-%d')") .orderByAsc("DATE_FORMAT(operation_record_time, '%Y-%m-%d')")); //标准上片记录 List> loadTaskingList1 = baseMapper.selectMaps(new QueryWrapper() .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) .groupBy("task_type", "operation_record", "operation_mode", "DATE_FORMAT(operation_record_time, '%Y-%m-%d')") .orderByAsc("DATE_FORMAT(operation_record_time, '%Y-%m-%d')")); List> loadTaskingList2 = baseMapper.selectMaps(new QueryWrapper() .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", "上片2") .eq("operation_mode", "结束") .gt("operation_record_time", startDate) .groupBy("task_type", "operation_record", "operation_mode", "DATE_FORMAT(operation_record_time, '%Y-%m-%d')") .orderByAsc("DATE_FORMAT(operation_record_time, '%Y-%m-%d')")); // 存储每条线路的数据 Map> lineDataMap = new HashMap<>(); lineDataMap.put("line1", new HashMap<>()); lineDataMap.put("line2", new HashMap<>()); // 处理第一条线路数据 // 处理旋转1结束记录 for (Map map : listTasking1) { String date = map.get("operation_record_time").toString(); int count = Integer.parseInt(map.get("count").toString()); Map line1Map = lineDataMap.get("line1"); line1Map.put(date, line1Map.getOrDefault(date, 0) + count); } // 处理线路1上片记录 for (Map map : loadTaskingList1) { String date = map.get("operation_record_time").toString(); int count = Integer.parseInt(map.get("count").toString()); Map line1Map = lineDataMap.get("line1"); line1Map.put(date, line1Map.getOrDefault(date, 0) + count); } // 处理第二条线路数据 // 处理旋转2结束记录 for (Map map : listTasking2) { String date = map.get("operation_record_time").toString(); int count = Integer.parseInt(map.get("count").toString()); Map line2Map = lineDataMap.get("line2"); line2Map.put(date, line2Map.getOrDefault(date, 0) + count); } // 处理线路2上片记录 for (Map map : loadTaskingList2) { String date = map.get("operation_record_time").toString(); int count = Integer.parseInt(map.get("count").toString()); Map line2Map = lineDataMap.get("line2"); line2Map.put(date, line2Map.getOrDefault(date, 0) + count); } //log.info("客户表计划量:{},{},{}",list,listTasking1,listTasking2); List Result=new ArrayList<>(); for (int i=0;i(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateString = sdf.format(thisdate); thisMap.put("date",dateString); // 获取一线数据 thisMap.put("line1", lineDataMap.get("line1").getOrDefault(dateString, 0)); // 获取二线数据 thisMap.put("line2", lineDataMap.get("line2").getOrDefault(dateString, 0)); if (list.size()>0 && list.get(0).get("CreateDate").toString().equals(dateString)){ thisMap.put("plan",list.get(0).get("task_quantity_sum")); list.remove(0); }else{ thisMap.put("plan",0); } Result.add(thisMap); } return Result; } /** * 回传报工数据+ 设备玻璃过片记录给 九牧 */ @Override public Integer reportTaskingLog() { TaskingLog updateTaskingLog = new TaskingLog(); updateTaskingLog.setIsSend(1); baseMapper.update(updateTaskingLog, new QueryWrapper().lambda(). eq(TaskingLog::getIsSend, 0)); List listTasking = baseMapper.selectList(new QueryWrapper().lambda() .eq(TaskingLog::getIsSend, 1)); // 判端是否为空 if (CollectionUtils.isNotEmpty(listTasking)) { // 进行拆解封装 List report = listTasking.stream().map(item -> { KBBTProgramsOperationLogBP newData = new KBBTProgramsOperationLogBP(); newData.setGlassId(item.getGlassId()); newData.setState(item.getState()); newData.setWorkState(item.getWorkState()); newData.setGlassState(item.getGlassState()); newData.setState(item.getState()); newData.setScanId(item.getScanId()); newData.setProgramId(item.getProgramId()); newData.setBatchNumber(item.getBatchNumber() == null ? "" : item.getBatchNumber()); newData.setLineConfigurationId(item.getLineConfigurationId()); newData.setTaskType(item.getTaskType()); newData.setLength(item.getLength()); newData.setWidth(item.getWidth()); newData.setThickness(item.getThickness()); newData.setDrawingMarking(item.getDrawingMarking()); newData.setIsSilkScreen(item.getIsSilkScreen()); newData.setIsWorking(1); newData.setSilkScreenX(item.getSilkScreenX()); newData.setSilkScreenY(item.getSilkScreenY()); newData.setR_1_1(item.getR_1_1()); newData.setR_1_2(item.getR_1_2()); newData.setR_2_1(item.getR_2_1()); newData.setR_2_2(item.getR_2_2()); newData.setR_3_1(item.getR_3_1()); newData.setR_3_2(item.getR_3_2()); newData.setR_4_1(item.getR_4_1()); newData.setR_4_2(item.getR_4_2()); newData.setSilkScreenY(item.getSilkScreenY()); newData.setOperationRecord(item.getOperationRecord()); newData.setOperationMode(item.getOperationMode()); newData.setOperationRecordTime(item.getOperationRecordTime()); newData.setCreateDate(new Date()); kBBTProgramsOperationLogBPMapper.insert(newData); return newData; }).collect(Collectors.toList()); updateTaskingLog.setIsSend(2); int successfulCount = baseMapper.update(updateTaskingLog, new QueryWrapper().lambda(). eq(TaskingLog::getIsSend, 1)); log.info("未提交的数量:{} 提交数量: {} 成功数量:{}", listTasking.size(), report.size(), successfulCount); return successfulCount; } return 0; } /** * 查询单小时产量 * @param dayCount 查询天数 * @return Map 包含每天两条线的平均小时产量 */ @Override public Map 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 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> line1Results = baseMapper.selectMaps(line1Query); // 查询二线数据,按天分组统计总产量和工作小时数 QueryWrapper 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> line2Results = baseMapper.selectMaps(line2Query); // 合并结果 Map result = new HashMap<>(); Map> dailyStats = new HashMap<>(); // 处理一线数据 for (Map 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 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 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 dailyData = dailyStats.computeIfAbsent(date, k -> { Map 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> 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> 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 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()); } } }