From 415fc0c1294bca5213a1bf090884d62ed4c086b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BE=E5=90=8C=E5=AD=A6?= Date: Mon, 25 Aug 2025 09:06:04 +0800 Subject: [PATCH] =?UTF-8?q?ADD:=E5=AF=BC=E5=85=A5=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E8=AE=A1=E5=88=92=E6=A3=80=E6=B5=8B=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plan/controller/AdPlanController.java | 23 ++++ .../gather/plan/service/IAdPlanService.java | 13 +- .../plan/service/impl/AdPlanServiceImpl.java | 116 +++++++++++++++++- 3 files changed, 150 insertions(+), 2 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java b/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java index 0bc0c866..e20df2b9 100644 --- a/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java +++ b/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java @@ -390,5 +390,28 @@ public class AdPlanController extends BaseController { LogUtil.njcnDebug(log, "{},导出ID数据为:{}", methodDescribe, planId); adPlanService.exportPlanCheckDataZip(planId, response); } + + + @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD) + @PostMapping(value = "/importSubPlanCheckData") + @ApiOperation("导入子计划检测结果") + @ApiImplicitParams({ + @ApiImplicitParam(name = "file", value = "检测计划检测结果数据文件", required = true), + @ApiImplicitParam(name = "patternId", value = "模式Id", required = true) + }) + public HttpResult importSubPlanCheckData(@RequestParam("file") MultipartFile file, @RequestParam("patternId") String patternId, HttpServletResponse response) { + String methodDescribe = getMethodDescribe("importSubPlanCheckData"); + LogUtil.njcnDebug(log, "{},上传文件为:{}", methodDescribe, file.getOriginalFilename()); + boolean fileType = FileUtil.judgeFileIsZip(file.getOriginalFilename()); + if (!fileType) { + CommonResponseEnum fileTypeError = CommonResponseEnum.FILE_XLSX_ERROR; + fileTypeError.setMessage("请上传zip文件"); + throw new BusinessException(fileTypeError); + } + adPlanService.importSubPlanCheckDataZip(file, patternId, response); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe); + + } + } diff --git a/detection/src/main/java/com/njcn/gather/plan/service/IAdPlanService.java b/detection/src/main/java/com/njcn/gather/plan/service/IAdPlanService.java index 186edd73..465c451c 100644 --- a/detection/src/main/java/com/njcn/gather/plan/service/IAdPlanService.java +++ b/detection/src/main/java/com/njcn/gather/plan/service/IAdPlanService.java @@ -189,10 +189,21 @@ public interface IAdPlanService extends IService { boolean importSubPlanDataZip(MultipartFile file, String patternId, HttpServletResponse response); /** - * 导出计划检测数据 + * 导出计划检测结果数据 * * @param planId * @param response */ void exportPlanCheckDataZip(String planId, HttpServletResponse response); + + /** + * 导入计划检测结果数据 + * + * @param file + * @param patternId + * @param response + */ + boolean importSubPlanCheckDataZip(MultipartFile file, String patternId, HttpServletResponse response); + + } diff --git a/detection/src/main/java/com/njcn/gather/plan/service/impl/AdPlanServiceImpl.java b/detection/src/main/java/com/njcn/gather/plan/service/impl/AdPlanServiceImpl.java index 96e8148b..6e800a00 100644 --- a/detection/src/main/java/com/njcn/gather/plan/service/impl/AdPlanServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/plan/service/impl/AdPlanServiceImpl.java @@ -6,12 +6,14 @@ import cn.afterturn.easypoi.excel.entity.ImportParams; import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.ZipUtil; import cn.hutool.extra.spring.SpringUtil; +import cn.hutool.json.JSONConfig; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; @@ -1826,7 +1828,7 @@ public class AdPlanServiceImpl extends ServiceImpl impleme planCheckDataVO.setCheckData(checkData); // 导出数据.zip文件 - String jsonStr = JSONUtil.toJsonStr(planCheckDataVO); + String jsonStr = JSONUtil.toJsonStr(planCheckDataVO, new JSONConfig().setIgnoreNullValue(false)); try { // 创建临时目录 File tempDir = FileUtil.mkdir(FileUtil.getTmpDirPath() + "export_" + System.currentTimeMillis() + "/"); @@ -1862,4 +1864,116 @@ public class AdPlanServiceImpl extends ServiceImpl impleme } } + @Transactional + @Override + public boolean importSubPlanCheckDataZip(MultipartFile file, String patternId, HttpServletResponse response) { + try { + // 创建临时目录用于解压文件 + File tempDir = FileUtil.mkdir(FileUtil.getTmpDirPath() + "import_plan_check_data_" + System.currentTimeMillis() + "/"); + + // 将上传的zip文件保存到临时目录 + File zipFile = FileUtil.file(tempDir, file.getOriginalFilename()); + file.transferTo(zipFile); + + // 解压zip文件 + File unzipDir = FileUtil.mkdir(FileUtil.file(tempDir, "unzip")); + ZipUtil.unzip(zipFile.getAbsolutePath(), unzipDir.getAbsolutePath()); + + // 查找解压目录中的json文件 + File[] files = unzipDir.listFiles(); + AdPlanCheckDataVO planCheckDataVO = null; + if (files != null) { + for (File f : files) { + if (f.isFile() && f.getName().endsWith(".json")) { + // 读取json文件内容 + String jsonStr = FileUtil.readUtf8String(f); + planCheckDataVO = JSONUtil.toBean(jsonStr, AdPlanCheckDataVO.class); + break; + } + } + } + + if (planCheckDataVO == null) { + FileUtil.del(tempDir); + throw new BusinessException(CommonResponseEnum.FAIL, "ZIP文件中未找到JSON文件"); + } + AdPlan checkPlan = planCheckDataVO.getPlan(); + if (checkPlan == null) { + FileUtil.del(tempDir); + throw new BusinessException(CommonResponseEnum.FAIL, "ZIP文件中未找到检测计划信息"); + } + // 检查导入的计划是否属于当前模式 + if (!StrUtil.equals(checkPlan.getPattern(), patternId)) { + FileUtil.del(tempDir); + throw new BusinessException(CommonResponseEnum.FAIL, "该检修计划当前模式不支持导入"); + } + + List devList = planCheckDataVO.getDevList(); + if (CollUtil.isEmpty(devList)) { + FileUtil.del(tempDir); + throw new BusinessException(CommonResponseEnum.FAIL, "该检修计划未找到被检设备信息"); + } + + AdPlan subPlan = this.getById(checkPlan.getId()); + if (subPlan == null) { + FileUtil.del(tempDir); + throw new BusinessException(CommonResponseEnum.FAIL, "该检修计划不存在"); + } + // 更新检测计划信息 + checkPlan.setFatherPlanId(subPlan.getFatherPlanId()); + this.updateById(checkPlan); + + + // 批量更新被检设备信息 + pqDevService.updateBatchById(devList); + + List devSubList = planCheckDataVO.getDevSubList(); + for (PqDevSub devSub : devSubList) { + pqDevSubService.update(devSub, new LambdaUpdateWrapper().eq(PqDevSub::getDevId, devSub.getDevId())); + } + + // 同步检测数据 + List pairList = planCheckDataVO.getPairList(); + adPairService.updateBatchById(pairList); + Map>> checkData = planCheckDataVO.getCheckData(); + // 批量插入数据 + for (Map.Entry>> entry : checkData.entrySet()) { + String tableName = entry.getKey(); + List> dataList = entry.getValue(); + + if (CollUtil.isNotEmpty(dataList)) { + for (Map row : dataList) { + // 构造批量插入SQL - 收集所有可能的列名 + List columnNames = new ArrayList<>(row.keySet()); + StringBuilder sqlBuilder = new StringBuilder(); + sqlBuilder.append("INSERT INTO ").append(tableName).append(" ("); + sqlBuilder.append(columnNames.stream().map(column -> "`" + column + "`").collect(Collectors.joining(","))); + sqlBuilder.append(") VALUES "); + + sqlBuilder.append("(" + columnNames.stream().map(column -> "?").collect(Collectors.joining(",")) + ")"); + String sql = sqlBuilder.toString(); + List params = new ArrayList<>(); + for (String column : columnNames) { + Object value = row.getOrDefault(column, null); + if (value != null && column.equals("Time_Id")) { + params.add(DateUtil.toLocalDateTime(new Date(Long.parseLong(value.toString())))); + } else { + params.add(value); + } + + } + jdbcTemplate.update(sql, params.toArray()); + } + } + } + + // 删除临时目录 + FileUtil.del(tempDir); + return true; + } catch (Exception e) { + log.error("导入子计划检测结果信息zip失败: ", e); + throw new BusinessException(CommonResponseEnum.FAIL, "导入失败"); + } + } + }