ADD:导入检测计划检测结果
This commit is contained in:
@@ -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<Boolean> 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -189,10 +189,21 @@ public interface IAdPlanService extends IService<AdPlan> {
|
||||
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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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<AdPlanMapper, AdPlan> 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<AdPlanMapper, AdPlan> 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<PqDev> 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<PqDevSub> devSubList = planCheckDataVO.getDevSubList();
|
||||
for (PqDevSub devSub : devSubList) {
|
||||
pqDevSubService.update(devSub, new LambdaUpdateWrapper<PqDevSub>().eq(PqDevSub::getDevId, devSub.getDevId()));
|
||||
}
|
||||
|
||||
// 同步检测数据
|
||||
List<AdPair> pairList = planCheckDataVO.getPairList();
|
||||
adPairService.updateBatchById(pairList);
|
||||
Map<String, List<Map<String, Object>>> checkData = planCheckDataVO.getCheckData();
|
||||
// 批量插入数据
|
||||
for (Map.Entry<String, List<Map<String, Object>>> entry : checkData.entrySet()) {
|
||||
String tableName = entry.getKey();
|
||||
List<Map<String, Object>> dataList = entry.getValue();
|
||||
|
||||
if (CollUtil.isNotEmpty(dataList)) {
|
||||
for (Map<String, Object> row : dataList) {
|
||||
// 构造批量插入SQL - 收集所有可能的列名
|
||||
List<String> 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<Object> 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, "导入失败");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user