diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java index 8d5dff35..34a10f62 100644 --- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java +++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java @@ -729,11 +729,7 @@ public class SocketDevResponseService { successComm.clear(); failComm.clear(); Map> map = new HashMap<>(1); - List preDetections = BeanUtil.copyToList(FormalTestManager.devList, PreDetection.class); - preDetections.forEach(x -> { - x.setDevType(x.getIcdType()); - }); - map.put("deviceList", preDetections); + map.put("deviceList", FormalTestManager.devList); String jsonString = JSON.toJSONString(map); socketMsg.setRequestId(SourceOperateCodeEnum.YJC_XYJY.getValue()); socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_02.getValue()); @@ -817,11 +813,7 @@ public class SocketDevResponseService { successComm.clear(); failComm.clear(); Map> map = new HashMap<>(1); - List preDetections = BeanUtil.copyToList(FormalTestManager.devList, PreDetection.class); - preDetections.forEach(x -> { - x.setDevType(x.getIcdType()); - }); - map.put("deviceList", preDetections); + map.put("deviceList", FormalTestManager.devList); String jsonString = JSON.toJSONString(map); socketMsg.setRequestId(SourceOperateCodeEnum.YJC_XYJY.getValue()); socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_03.getValue()); @@ -1728,6 +1720,9 @@ public class SocketDevResponseService { List pqDevList = iPqDevService.getDevInfo(param.getDevIds()); FormalTestManager.devList = pqDevList; + FormalTestManager.devList.forEach(x -> { + x.setDevType(x.getIcdType()); + }); FormalTestManager.devMapMonitorNum = pqDevList.stream().collect(Collectors.toMap(PreDetection::getDevIP, it -> it.getMonitorList().stream().map(PreDetection.MonitorListDTO::getLineId).collect(Collectors.toList()))); FormalTestManager.monitorIdListComm = pqDevList.stream().flatMap(x -> x.getMonitorList().stream()).map(PreDetection.MonitorListDTO::getLineId).collect(Collectors.toList()); diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java index 45d08bc6..d5e6cce4 100644 --- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java +++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java @@ -228,11 +228,7 @@ public class SocketSourceResponseService { //开始设备通讯检测(发送设备初始化) //List devList = iPqDevService.getDevInfo(param.getDevIds()); Map> map = new HashMap<>(1); - List preDetections = BeanUtil.copyToList(FormalTestManager.devList, PreDetection.class); - preDetections.forEach(x -> { - x.setDevType(x.getIcdType()); - }); - map.put("deviceList", preDetections); + map.put("deviceList", FormalTestManager.devList); String jsonString = JSON.toJSONString(map); socketMsg.setRequestId(SourceOperateCodeEnum.YJC_SBTXJY.getValue()); socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_01.getValue()); diff --git a/detection/src/main/java/com/njcn/gather/device/pojo/enums/DevReportStateEnum.java b/detection/src/main/java/com/njcn/gather/device/pojo/enums/DevReportStateEnum.java index f971be72..2ba64183 100644 --- a/detection/src/main/java/com/njcn/gather/device/pojo/enums/DevReportStateEnum.java +++ b/detection/src/main/java/com/njcn/gather/device/pojo/enums/DevReportStateEnum.java @@ -10,6 +10,7 @@ import lombok.Getter; public enum DevReportStateEnum { NOT_GENERATED("未生成", 0), GENERATED("已生成", 1), + GENERATED_UPLOADED("已生成且已上传", 3), UNCHECKED("未检", 2); private final Integer value; diff --git a/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java b/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java index 5f00a9a8..17d92530 100644 --- a/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java +++ b/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java @@ -156,4 +156,15 @@ public class ReportController extends BaseController { return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe); } } + + @OperateInfo + @PostMapping("/uploadReportToCloud") + @ApiOperation("批量上传检测报告到云端") + @ApiImplicitParam(name = "deviceIds", value = "被检设备ID列表,为空时上传所有已生成报告的设备", required = false) + public HttpResult uploadReportToCloud(@RequestBody(required = false) List deviceIds) { + String methodDescribe = getMethodDescribe("uploadReportToCloud"); + LogUtil.njcnDebug(log, "{},设备ID列表为:{}", methodDescribe, deviceIds); + pqReportService.uploadReportToCloud(deviceIds); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); + } } diff --git a/detection/src/main/java/com/njcn/gather/report/service/IPqReportService.java b/detection/src/main/java/com/njcn/gather/report/service/IPqReportService.java index 1e21eaf8..1045c03d 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/IPqReportService.java +++ b/detection/src/main/java/com/njcn/gather/report/service/IPqReportService.java @@ -76,6 +76,11 @@ public interface IPqReportService extends IService { */ boolean documented(List id); - + /** + * 批量上传检测报告到云端 + * + * @param deviceIds 被检设备ID列表,为空时上传所有已生成报告的设备 + */ + void uploadReportToCloud(List deviceIds); } diff --git a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java index 4f4a0fc2..f50315d8 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java @@ -103,6 +103,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -618,8 +619,30 @@ public class PqReportServiceImpl extends ServiceImpl i } out.close(); this.updateDevAndPlanState(devReportParam.getDevId(), devReportParam.getPlanId()); -// sendReportToCloud(pqDevVO.getIp(), reportFullPath, pqDevVO.getCreateId() + ".docx"); - sendQrToDevice(pqDevVO.getIp(), pqDevVO.getCreateId() + ".docx"); + // 异步将有效的二维码下装到被检设备 + CompletableFuture.runAsync(() -> { + try { + sendQrToDevice(pqDevVO.getIp(), pqDevVO.getCreateId() + ".docx"); + log.info("二维码下装成功,设备IP: {}", pqDevVO.getIp()); + } catch (Exception e) { + log.error("二维码下装失败,设备IP: {}", pqDevVO.getIp(), e); + } + }); + // 异步将检测报告上传到云端,但是不一定成功,需要无线网支撑 + CompletableFuture.runAsync(() -> { + File file = new File(reportFullPath); + if (file.exists()) { + try { + ResponseEntity responseEntity = restTemplateUtil.uploadFile(cloudUrl + "/upload", file); + if (responseEntity.getStatusCode().is2xxSuccessful()) { + // 将被检设备的报告状态改为已生成且已上传 + iPqDevService.updatePqDevReportState(devReportParam.getDevId(), DevReportStateEnum.GENERATED_UPLOADED.getValue()); + } + } catch (Exception e) { + log.error("文件上传到云端失败", e); + } + } + }); } catch (IOException e) { log.error(ReportResponseEnum.GENERATE_REPORT_ERROR.getMessage(), e); throw new BusinessException(ReportResponseEnum.GENERATE_REPORT_ERROR); @@ -655,12 +678,12 @@ public class PqReportServiceImpl extends ServiceImpl i // 组装业务数据 JSONObject data = new JSONObject(); - if (testFTPConnection(cloudUrl, devPort, devName, devPsd)) { + if (testFTPConnection(devIp, devPort, devName, devPsd)) { data.set("name", devName); data.set("password", devPsd); data.set("port", devPort); data.set("path", "ftp://" + devIp + devPath); - } else if (testFTPConnection(cloudUrl, gcDevPort, gcDevName, gcDevPsd)) { + } else if (testFTPConnection(devIp, gcDevPort, gcDevName, gcDevPsd)) { data.set("name", gcDevName); data.set("password", gcDevPsd); data.set("port", gcDevPort); @@ -689,28 +712,6 @@ public class PqReportServiceImpl extends ServiceImpl i } - /** - * 处理检测报告上传云服务器并生成二维码下装到装置 - * 做成异步执行 - * - * @param devIp 设备IP - * @param reportFullPath 检测报告本地全路径 - * @param reportName 检测报告名称 - */ - public void sendReportToCloud(String devIp, String reportFullPath, String reportName) { - // 将文件上传至目标服务器 - File file = new File(reportFullPath); - try { - ResponseEntity responseEntity = restTemplateUtil.uploadFile(cloudUrl + "/upload", file); - } catch (Exception e) { - // 进行日志记录入口 - System.out.println("异常为:" + e.getMessage()); - } - - - } - - /** * 测试FTP连接 * @@ -1748,5 +1749,62 @@ public class PqReportServiceImpl extends ServiceImpl i } } + @Override + public void uploadReportToCloud(List deviceIds) { + log.info("开始批量上传检测报告到云端,设备ID列表:{}", deviceIds); + + // 查询条件:报告状态为已生成(1)的设备 + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(PqDev::getReportState, DevReportStateEnum.GENERATED.getValue()); + + // 如果指定了设备ID列表,则只查询这些设备 + if (CollUtil.isNotEmpty(deviceIds)) { + wrapper.in(PqDev::getId, deviceIds); + } + + List devices = iPqDevService.list(wrapper); + + if (CollUtil.isEmpty(devices)) { + log.warn("未找到符合条件的设备,无需上传"); + return; + } + + log.info("找到{}台设备需要上传报告", devices.size()); + String dirPath = reportPath; + // 确保目录存在 + ensureDirectoryExists(dirPath); + // 异步批量上传每台设备的报告 + CompletableFuture.runAsync(() -> { + for (PqDev device : devices) { + try { + // 构建报告文件路径 + String fileName = device.getCreateId() + ".docx"; + String reportFullPath = dirPath.concat(File.separator).concat(device.getCreateId()).concat(".docx"); + File reportFile = new File(reportFullPath); + + if (!reportFile.exists()) { + log.warn("设备{}的报告文件不存在:{}", device.getId(), fileName); + continue; + } + + // 上传文件到云端 + ResponseEntity responseEntity = restTemplateUtil.uploadFile(cloudUrl + "/upload", reportFile); + + if (responseEntity.getStatusCode().is2xxSuccessful()) { + // 更新设备报告状态为已生成且已上传 + iPqDevService.updatePqDevReportState(device.getId(), DevReportStateEnum.GENERATED_UPLOADED.getValue()); + log.info("设备{}报告上传成功", device.getId()); + } else { + log.error("设备{}报告上传失败,HTTP状态码:{}", device.getId(), responseEntity.getStatusCode()); + } + + } catch (Exception e) { + log.error("设备{}报告上传异常", device.getId(), e); + } + } + log.info("批量上传任务完成"); + }); + } + } diff --git a/detection/src/main/java/com/njcn/gather/script/service/impl/PqScriptDtlsServiceImpl.java b/detection/src/main/java/com/njcn/gather/script/service/impl/PqScriptDtlsServiceImpl.java index 1afd51dd..321f076f 100644 --- a/detection/src/main/java/com/njcn/gather/script/service/impl/PqScriptDtlsServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/script/service/impl/PqScriptDtlsServiceImpl.java @@ -1035,11 +1035,12 @@ public class PqScriptDtlsServiceImpl extends ServiceImpl