出厂检测检测报告改造

This commit is contained in:
2025-07-24 16:29:31 +08:00
parent a6b0509179
commit 56477157aa
4 changed files with 101 additions and 25 deletions

View File

@@ -10,6 +10,7 @@ import lombok.Getter;
public enum DevReportStateEnum { public enum DevReportStateEnum {
NOT_GENERATED("未生成", 0), NOT_GENERATED("未生成", 0),
GENERATED("已生成", 1), GENERATED("已生成", 1),
GENERATED_UPLOADED("已生成且已上传", 3),
UNCHECKED("未检", 2); UNCHECKED("未检", 2);
private final Integer value; private final Integer value;

View File

@@ -156,4 +156,15 @@ public class ReportController extends BaseController {
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
} }
} }
@OperateInfo
@PostMapping("/uploadReportToCloud")
@ApiOperation("批量上传检测报告到云端")
@ApiImplicitParam(name = "deviceIds", value = "被检设备ID列表为空时上传所有已生成报告的设备", required = false)
public HttpResult<Object> uploadReportToCloud(@RequestBody(required = false) List<String> deviceIds) {
String methodDescribe = getMethodDescribe("uploadReportToCloud");
LogUtil.njcnDebug(log, "{}设备ID列表为{}", methodDescribe, deviceIds);
pqReportService.uploadReportToCloud(deviceIds);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
}
} }

View File

@@ -76,6 +76,11 @@ public interface IPqReportService extends IService<PqReport> {
*/ */
boolean documented(List<String> id); boolean documented(List<String> id);
/**
* 批量上传检测报告到云端
*
* @param deviceIds 被检设备ID列表为空时上传所有已生成报告的设备
*/
void uploadReportToCloud(List<String> deviceIds);
} }

View File

@@ -71,6 +71,7 @@ import com.njcn.gather.type.service.IDevTypeService;
import com.njcn.http.util.RestTemplateUtil; import com.njcn.http.util.RestTemplateUtil;
import com.njcn.web.factory.PageFactory; import com.njcn.web.factory.PageFactory;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
@@ -84,6 +85,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -615,8 +617,30 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
} }
out.close(); out.close();
this.updateDevAndPlanState(devReportParam.getDevId(), devReportParam.getPlanId()); 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<String> 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) { } catch (IOException e) {
log.error(ReportResponseEnum.GENERATE_REPORT_ERROR.getMessage(), e); log.error(ReportResponseEnum.GENERATE_REPORT_ERROR.getMessage(), e);
throw new BusinessException(ReportResponseEnum.GENERATE_REPORT_ERROR); throw new BusinessException(ReportResponseEnum.GENERATE_REPORT_ERROR);
@@ -686,28 +710,6 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
} }
/**
* 处理检测报告上传云服务器并生成二维码下装到装置
* 做成异步执行
*
* @param devIp 设备IP
* @param reportFullPath 检测报告本地全路径
* @param reportName 检测报告名称
*/
public void sendReportToCloud(String devIp, String reportFullPath, String reportName) {
// 将文件上传至目标服务器
File file = new File(reportFullPath);
try {
ResponseEntity<String> responseEntity = restTemplateUtil.uploadFile(cloudUrl + "/upload", file);
} catch (Exception e) {
// 进行日志记录入口
System.out.println("异常为:" + e.getMessage());
}
}
/** /**
* 测试FTP连接 * 测试FTP连接
* *
@@ -1746,5 +1748,62 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
} }
} }
@Override
public void uploadReportToCloud(List<String> deviceIds) {
log.info("开始批量上传检测报告到云端设备ID列表{}", deviceIds);
// 查询条件:报告状态为已生成(1)的设备
LambdaQueryWrapper<PqDev> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(PqDev::getReportState, DevReportStateEnum.GENERATED.getValue());
// 如果指定了设备ID列表则只查询这些设备
if (CollUtil.isNotEmpty(deviceIds)) {
wrapper.in(PqDev::getId, deviceIds);
}
List<PqDev> 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<String> 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("批量上传任务完成");
});
}
} }