diff --git a/detection/src/main/java/com/njcn/gather/detection/pojo/vo/DetectionData.java b/detection/src/main/java/com/njcn/gather/detection/pojo/vo/DetectionData.java index 392d09e9..7256a1e2 100644 --- a/detection/src/main/java/com/njcn/gather/detection/pojo/vo/DetectionData.java +++ b/detection/src/main/java/com/njcn/gather/detection/pojo/vo/DetectionData.java @@ -53,4 +53,9 @@ public class DetectionData { * 误差体系详情ID(比对式使用) */ private String errorDtlId; + + /** + * 有效组数 + */ + private Integer validGroupNum; } diff --git a/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java b/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java index 4e2a5141..d8f816b4 100644 --- a/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java @@ -1654,7 +1654,7 @@ public class DetectionServiceImpl { resultMap.put(PowerIndexEnum.HSI.getKey(), isHarmQualified(devDataList, standardDevDataList, devIdMapComm, pqErrSysDtls, I, fIn, INHARMONIC_FLAG, isDelta && isExitDelta, dataRule, num, code, oneConfig.getScale(), waveNum, dataSourceEnum)); break; case PST: - resultMap.put(PowerIndexEnum.F.getKey(), isQualified(devDataList, standardDevDataList, devIdMapComm, pqErrSysDtls, true, fUn , DetectionCodeEnum.PST.getCode(), dataRule, num, code, oneConfig.getScale(), waveNum, DataSourceEnum.MINUTE_STATISTICS_AVG)); + resultMap.put(PowerIndexEnum.F.getKey(), isQualified(devDataList, standardDevDataList, devIdMapComm, pqErrSysDtls, true, fUn, DetectionCodeEnum.PST.getCode(), dataRule, num, code, oneConfig.getScale(), waveNum, DataSourceEnum.MINUTE_STATISTICS_AVG)); break; case UNKNOWN_ERROR: break; @@ -1750,8 +1750,16 @@ public class DetectionServiceImpl { result.setDataType(DataSourceEnum.WAVE_DATA.getValue()); } + Map> validData = this.getValidData(standardDevDataList, desc); + if (map.containsKey(TYPE_T)) { + Integer validGroupNum = 0; + if (ObjectUtil.isNotNull(validData)) { + List tValidDataList = validData.get(TYPE_T); + validGroupNum = this.getValidGroupNum(tValidDataList, null, errSysDtls, fData, scale, dataRule); + } List tList = rangeComparisonList(map.get(TYPE_T).get(0), map.get(TYPE_T).get(1), errSysDtls, fData, scale, dataRule); + this.setValidGroupNum(tList, validGroupNum); result.setTValue(JSON.toJSONString(tList)); result.setResultFlag(setResultFlag(tList)); } else { @@ -1783,6 +1791,169 @@ public class DetectionServiceImpl { return StorageUtil.getInteger(resultFlag); } + private Map> getValidData(List dev, String desc) { + List aDev = new ArrayList<>(); + List bDev = new ArrayList<>(); + List cDev = new ArrayList<>(); + List tDev = new ArrayList<>(); + + for (int i = 0; i < dev.size(); i++) { + List devSqlData = dev.get(i).getSqlData(); + + String finalDesc = desc; + List devList = devSqlData.stream().filter(j -> finalDesc.equals(j.getDesc())) + .map(DevData.SqlDataDTO::getList).collect(Collectors.toList()); + + if (CollUtil.isEmpty(devList)) { + return null; + } + aDev.addAll(devList.stream().filter(x -> ObjectUtil.isNotNull(x.getA())) + .map(x -> x.getA().doubleValue()) + .collect(Collectors.toList())); + bDev.addAll(devList.stream().filter(x -> ObjectUtil.isNotNull(x.getB())) + .map(x -> x.getB().doubleValue()) + .collect(Collectors.toList())); + cDev.addAll(devList.stream().filter(x -> ObjectUtil.isNotNull(x.getC())) + .map(x -> x.getC().doubleValue()) + .collect(Collectors.toList())); + tDev.addAll(devList.stream().filter(x -> ObjectUtil.isNotNull(x.getT())) + .map(x -> x.getT().doubleValue()) + .collect(Collectors.toList())); + } + + return new HashMap() { + { + put(TYPE_A, aDev); + put(TYPE_B, bDev); + put(TYPE_C, cDev); + put(TYPE_T, tDev); + } + }; + } + + private Map>> getHarmValidData(List dev, String fundCode, String harmCode, Integer harmonicFlag) { + // key为相别、value{key为谐波次数,value为按数据处理原则处理后的数据} + Map>> devMap = new HashMap<>(3); + + List harmNum = new ArrayList<>(); + if (1 == harmonicFlag) { + harmNum.addAll(Stream.iterate(0.5, x -> x + 1.0).limit(50).collect(Collectors.toList())); + } else { + harmNum.addAll(Stream.iterate(1.0, x -> x + 1.0).limit(50).collect(Collectors.toList())); + } + + for (int i = 0; i < dev.size(); i++) { + List devSqlData = dev.get(i).getSqlData(); + + Optional first1 = Optional.empty(); + + if (CollUtil.isNotEmpty(devSqlData)) { + first1 = devSqlData.stream().filter(j -> fundCode.equals(j.getDesc())).collect(Collectors.toList()).stream().findFirst(); + } + + DevData.SqlDataDTO fund1 = null; + if (first1.isPresent()) { + fund1 = first1.get(); + } + + List devHarmList = dev.get(i).getSqlDataHarm().stream().filter(x -> harmCode.equals(x.getDesc())).collect(Collectors.toList()); + + if (CollUtil.isEmpty(devHarmList)) { + return null; + } + + if (CollUtil.isNotEmpty(devHarmList)) { + DevData.SqlDataHarmDTO devHarm = devHarmList.get(0); + if (ObjectUtil.isNotNull(fund1)) { + harmPut(TYPE_A, devMap, harmNum, devHarm, String.valueOf(fund1.getList().getA()), harmonicFlag); + harmPut(TYPE_B, devMap, harmNum, devHarm, String.valueOf(fund1.getList().getB()), harmonicFlag); + harmPut(TYPE_C, devMap, harmNum, devHarm, String.valueOf(fund1.getList().getC()), harmonicFlag); + } else { + harmPut(TYPE_A, devMap, harmNum, devHarm, "1.0", harmonicFlag); + harmPut(TYPE_B, devMap, harmNum, devHarm, "1.0", harmonicFlag); + harmPut(TYPE_C, devMap, harmNum, devHarm, "1.0", harmonicFlag); + } + } + } + return devMap; + } + + private Integer getValidGroupNum(List devDataList, List U1List, List errSysDtls, Double fData, Integer scale, DictDataEnum dataRule) { + int result = 0; + if (dataRule.getCode().equals(DictDataEnum.Own_value.getCode()) || dataRule.getCode().equals(DictDataEnum.SECTION_VALUE.getCode())) { + return 1; + } else { + for (int i = 0; i < devDataList.size(); i++) { + Double U1 = CollUtil.isNotEmpty(U1List) ? U1List.get(i) : null; + Double stdDevData = BigDecimal.valueOf(devDataList.get(i)).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); + Double devData = BigDecimal.valueOf(devDataList.get(i)).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); + DetectionData detectionData = new DetectionData(); + detectionData.setIsData(ResultEnum.NO_ERROR_SYS.getValue()); + + //获得误差体系 + List errSysDtls1 = errSysDtls.stream().filter(x -> rangeComparison(startRadiusEnd(x.getStartValue(), fData, stdDevData, x.getConditionType(), scale), + x.getStartFlag(), + startRadiusEnd(x.getEndValue(), fData, stdDevData, x.getConditionType(), scale), + x.getEndFlag(), + ObjectUtil.isNotNull(U1) ? stdDevData * U1 * 0.01 : stdDevData)).collect(Collectors.toList()); + detectionData.setData(devData); + detectionData.setResultData(stdDevData); + if (CollUtil.isNotEmpty(errSysDtls1)) { + result++; + + } + } + } + + return result; + } + + private Map getHarmValidGroupNum(Map> devDataMap, List U1List, List errSysDtls, Double fData, Integer scale, DictDataEnum dataRule) { + Map resultMap = new HashMap<>(); + devDataMap.forEach((harmNum, dataList) -> { + if (dataRule.getCode().equals(DictDataEnum.Own_value.getCode()) || dataRule.getCode().equals(DictDataEnum.SECTION_VALUE.getCode())) { + resultMap.put(harmNum, 1); + } else { + Integer validGroupNum = 0; + for (int i = 0; i < dataList.size(); i++) { + Double U1 = CollUtil.isNotEmpty(U1List) ? U1List.get(i) : null; + Double stdDevData = BigDecimal.valueOf(dataList.get(i)).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); + Double devData = BigDecimal.valueOf(dataList.get(i)).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); + DetectionData detectionData = new DetectionData(); + detectionData.setIsData(ResultEnum.NO_ERROR_SYS.getValue()); + + //获得误差体系 + List errSysDtls1 = errSysDtls.stream().filter(x -> rangeComparison(startRadiusEnd(x.getStartValue(), fData, stdDevData, x.getConditionType(), scale), + x.getStartFlag(), + startRadiusEnd(x.getEndValue(), fData, stdDevData, x.getConditionType(), scale), + x.getEndFlag(), + ObjectUtil.isNotNull(U1) ? stdDevData * U1 * 0.01 : stdDevData)).collect(Collectors.toList()); + detectionData.setData(devData); + detectionData.setResultData(stdDevData); + if (CollUtil.isNotEmpty(errSysDtls1)) { + validGroupNum++; + } + } + resultMap.put(harmNum, validGroupNum); + } + }); + return resultMap; + } + + private void setValidGroupNum(List tList, Integer validGroupNum) { + tList.forEach(detectionData -> { + detectionData.setValidGroupNum(validGroupNum); + }); + } + + private void setHarmValidGroupNum(List integerBooleanA, Map harmValidGroupNumA) { + integerBooleanA.forEach(detectionData -> { + Double harmNum = detectionData.getNum(); + Integer validGroupNum = harmValidGroupNumA.get(harmNum); + detectionData.setValidGroupNum(validGroupNum); + }); + } + /** * 处理谐波数据 @@ -1869,9 +2040,22 @@ public class DetectionServiceImpl { result.setDataType(DataSourceEnum.WAVE_DATA.getValue()); } + Map>> harmValidData = this.getHarmValidData(standardDevDataList, fundCode, harmCode, harmonicFlag); + List integerBooleanA = harmRangeComparison(devMap.get(TYPE_A).get(0), devMap.get(TYPE_A).get(1), errSysDtls, type, fData, scale, dataRule); List integerBooleanB = harmRangeComparison(devMap.get(TYPE_B).get(0), devMap.get(TYPE_B).get(1), errSysDtls, type, fData, scale, dataRule); List integerBooleanC = harmRangeComparison(devMap.get(TYPE_C).get(0), devMap.get(TYPE_C).get(1), errSysDtls, type, fData, scale, dataRule); + + Map>> U1ListMap = this.getU1ListMap(standardDevDataList, fundCode, harmCode, harmonicFlag); + Map harmValidGroupNumA = this.getHarmValidGroupNum(harmValidData.get(TYPE_A), U1ListMap.get(TYPE_A).get(0.0), errSysDtls, fData, scale, dataRule); + this.setHarmValidGroupNum(integerBooleanA, harmValidGroupNumA); + + Map harmValidGroupNumB = this.getHarmValidGroupNum(harmValidData.get(TYPE_B), U1ListMap.get(TYPE_B).get(0.0), errSysDtls, fData, scale, dataRule); + this.setHarmValidGroupNum(integerBooleanB, harmValidGroupNumB); + + Map harmValidGroupNumC = this.getHarmValidGroupNum(harmValidData.get(TYPE_C), U1ListMap.get(TYPE_C).get(0.0), errSysDtls, fData, scale, dataRule); + this.setHarmValidGroupNum(integerBooleanC, harmValidGroupNumC); + reflectHarmonic(true, "a", integerBooleanA, result, harmonicFlag); reflectHarmonic(true, "b", integerBooleanB, result, harmonicFlag); reflectHarmonic(true, "c", integerBooleanC, result, harmonicFlag); @@ -1888,6 +2072,59 @@ public class DetectionServiceImpl { return StorageUtil.getInteger(resultFlag); } + private Map>> getU1ListMap(List devDataList, String fundCode, String harmCode, Integer harmonicFlag) { + Map>> resultMap = new HashMap<>(3); + + // key为相别、value{key为谐波次数,value为按数据处理原则处理后的数据} + Map>> devMap = new HashMap<>(3); + + List harmNum = new ArrayList<>(); + if (1 == harmonicFlag) { + harmNum.addAll(Stream.iterate(0.5, x -> x + 1.0).limit(50).collect(Collectors.toList())); + } else { + harmNum.addAll(Stream.iterate(1.0, x -> x + 1.0).limit(50).collect(Collectors.toList())); + } + + for (int i = 0; i < devDataList.size(); i++) { + List standardDevSqlData = devDataList.get(i).getSqlData(); + + Optional first1 = Optional.empty(); + + if (CollUtil.isNotEmpty(standardDevSqlData)) { + first1 = standardDevSqlData.stream().filter(j -> fundCode.equals(j.getDesc())).collect(Collectors.toList()).stream().findFirst(); + } + + DevData.SqlDataDTO fund1 = null; + if (first1.isPresent()) { + fund1 = first1.get(); + } + + List devHarmList = devDataList.get(i).getSqlDataHarm().stream().filter(x -> harmCode.equals(x.getDesc())).collect(Collectors.toList()); + + if (CollUtil.isEmpty(devHarmList)) { + return null; + } + + if (CollUtil.isNotEmpty(devHarmList)) { + DevData.SqlDataHarmDTO devHarm = devHarmList.get(0); + if (ObjectUtil.isNotNull(fund1)) { + harmPut(TYPE_A, devMap, harmNum, devHarm, String.valueOf(fund1.getList().getA()), harmonicFlag); + harmPut(TYPE_B, devMap, harmNum, devHarm, String.valueOf(fund1.getList().getB()), harmonicFlag); + harmPut(TYPE_C, devMap, harmNum, devHarm, String.valueOf(fund1.getList().getC()), harmonicFlag); + } else { + harmPut(TYPE_A, devMap, harmNum, devHarm, "1.0", harmonicFlag); + harmPut(TYPE_B, devMap, harmNum, devHarm, "1.0", harmonicFlag); + harmPut(TYPE_C, devMap, harmNum, devHarm, "1.0", harmonicFlag); + } + } + } + resultMap.put(TYPE_A, devMap.get(TYPE_A)); + resultMap.put(TYPE_B, devMap.get(TYPE_C)); + resultMap.put(TYPE_C, devMap.get(TYPE_C)); + + return resultMap; + } + /** * 处理非谐波类取值 @@ -1908,6 +2145,7 @@ public class DetectionServiceImpl { List cStandardDev = new ArrayList<>(); List tDev = new ArrayList<>(); List tStandardDev = new ArrayList<>(); + for (int i = 0; i < dev.size(); i++) { List devSqlData = dev.get(i).getSqlData(); List standardDevSqlData = standardDev.get(i).getSqlData(); diff --git a/detection/src/main/java/com/njcn/gather/icd/controller/PqIcdPathController.java b/detection/src/main/java/com/njcn/gather/icd/controller/PqIcdPathController.java index e07fd5df..f30566ca 100644 --- a/detection/src/main/java/com/njcn/gather/icd/controller/PqIcdPathController.java +++ b/detection/src/main/java/com/njcn/gather/icd/controller/PqIcdPathController.java @@ -60,7 +60,7 @@ public class PqIcdPathController extends BaseController { @PostMapping("/add") @ApiOperation("新增icd") @ApiImplicitParam(name = "param", value = "icd新增参数", required = true) - public HttpResult add(@RequestBody @Validated PqIcdPathParam param) { + public HttpResult add(PqIcdPathParam param) { String methodDescribe = getMethodDescribe("add"); LogUtil.njcnDebug(log, "{},新增数据为:{}", methodDescribe, param); @@ -76,7 +76,7 @@ public class PqIcdPathController extends BaseController { @PostMapping("/update") @ApiOperation("修改icd") @ApiImplicitParam(name = "param", value = "icd修改参数", required = true) - public HttpResult update(@RequestBody @Validated PqIcdPathParam.UpdateParam param) { + public HttpResult update(PqIcdPathParam.UpdateParam param) { String methodDescribe = getMethodDescribe("update"); LogUtil.njcnDebug(log, "{},修改数据为:{}", methodDescribe, param); diff --git a/detection/src/main/java/com/njcn/gather/icd/pojo/enums/IcdResponseEnum.java b/detection/src/main/java/com/njcn/gather/icd/pojo/enums/IcdResponseEnum.java new file mode 100644 index 00000000..770e853c --- /dev/null +++ b/detection/src/main/java/com/njcn/gather/icd/pojo/enums/IcdResponseEnum.java @@ -0,0 +1,19 @@ +package com.njcn.gather.icd.pojo.enums; + +/** + * @author caozehui + * @data 2025-11-11 + */ +public enum IcdResponseEnum { + FILE_NOT_NULL("A018001", "映射文件不能为空"), + FILE_TYPE_ERROR("A018002", "映射文件类型错误"), + FILE_SIZE_ERROR("A018003", "映射文件大小超出限制"); + + private String code; + private String message; + + IcdResponseEnum(String code, String message) { + this.code = code; + this.message = message; + } +} diff --git a/detection/src/main/java/com/njcn/gather/icd/pojo/param/PqIcdPathParam.java b/detection/src/main/java/com/njcn/gather/icd/pojo/param/PqIcdPathParam.java index e55f0005..ebcc884f 100644 --- a/detection/src/main/java/com/njcn/gather/icd/pojo/param/PqIcdPathParam.java +++ b/detection/src/main/java/com/njcn/gather/icd/pojo/param/PqIcdPathParam.java @@ -6,6 +6,7 @@ import com.njcn.web.pojo.param.BaseParam; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; +import org.springframework.web.multipart.MultipartFile; import javax.validation.constraints.NotBlank; import javax.validation.constraints.Pattern; @@ -32,6 +33,9 @@ public class PqIcdPathParam { @ApiModelProperty(value = "角型接线时是否使用相别的指标来进行检测,0表示否,1表示是", required = true) private Integer usePhaseIndex; + @ApiModelProperty(value = "映射文件", required = true) + private MultipartFile mappingFile; + /** * 分页查询实体 */ diff --git a/detection/src/main/java/com/njcn/gather/icd/pojo/po/PqIcdPath.java b/detection/src/main/java/com/njcn/gather/icd/pojo/po/PqIcdPath.java index 542a8faf..b59c962d 100644 --- a/detection/src/main/java/com/njcn/gather/icd/pojo/po/PqIcdPath.java +++ b/detection/src/main/java/com/njcn/gather/icd/pojo/po/PqIcdPath.java @@ -1,5 +1,6 @@ package com.njcn.gather.icd.pojo.po; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import com.njcn.db.mybatisplus.bo.BaseEntity; import lombok.Data; @@ -46,5 +47,18 @@ public class PqIcdPath extends BaseEntity implements Serializable { * 角型接线时是否使用相别的指标来进行检测,0表示否,1表示是 */ private Integer usePhaseIndex; + + /** + * 映射文件路径 + */ + @TableField(exist = false) + private FileVO mappingFile; + + @Data + public static class FileVO{ + private String name; + + private String url; + } } diff --git a/detection/src/main/java/com/njcn/gather/icd/service/impl/PqIcdPathServiceImpl.java b/detection/src/main/java/com/njcn/gather/icd/service/impl/PqIcdPathServiceImpl.java index 8a7e72a3..e487ed22 100644 --- a/detection/src/main/java/com/njcn/gather/icd/service/impl/PqIcdPathServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/icd/service/impl/PqIcdPathServiceImpl.java @@ -1,5 +1,6 @@ package com.njcn.gather.icd.service.impl; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -7,17 +8,26 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.njcn.common.pojo.enums.common.DataStateEnum; import com.njcn.common.pojo.exception.BusinessException; import com.njcn.gather.icd.mapper.PqIcdPathMapper; +import com.njcn.gather.icd.pojo.enums.IcdResponseEnum; import com.njcn.gather.icd.pojo.param.PqIcdPathParam; import com.njcn.gather.icd.pojo.po.PqIcdPath; import com.njcn.gather.icd.service.IPqIcdPathService; import com.njcn.gather.pojo.enums.DetectionResponseEnum; +import com.njcn.gather.report.pojo.enums.ReportResponseEnum; import com.njcn.web.factory.PageFactory; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; /** @@ -41,6 +51,13 @@ public class PqIcdPathServiceImpl extends ServiceImpl page = this.page(new Page<>(PageFactory.getPageNum(param), PageFactory.getPageSize(param)), wrapper); + String commInstallPath = this.getCommInstallPath(); + page.getRecords().forEach(pqIcdPath -> { + PqIcdPath.FileVO fileVO = new PqIcdPath.FileVO(); + fileVO.setUrl(commInstallPath + "\\DeviceControl\\Config\\" + pqIcdPath.getName() + ".txt"); + fileVO.setName(pqIcdPath.getName()+ ".txt"); + pqIcdPath.setMappingFile(fileVO); + }); return page; } @@ -52,9 +69,63 @@ public class PqIcdPathServiceImpl extends ServiceImpl FILE_SIZE_LIMIT) { +// throw new BusinessException(IcdResponseEnum.FILE_SIZE_ERROR); +// } +// +// try { +// // 如果文件存在,则先删除 +// String mappingFilePath = commInstallPath + "\\DeviceControl\\Config" + mappingFilename; +// Path path = Paths.get(mappingFilePath); +// File file = path.toFile(); +// if (file.exists()) { +// file.delete(); +// } +// +// // 保存文件 +// byte[] bytes = mappingFile.getBytes(); +// BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(mappingFilePath)); +// bufferedOutputStream.write(bytes); +// bufferedOutputStream.flush(); +// +// bufferedOutputStream.close(); +// } catch (IOException e) { +// throw new BusinessException(ReportResponseEnum.FILE_UPLOAD_FAILED); +// } +// } else { +// throw new BusinessException(IcdResponseEnum.FILE_NOT_NULL); +// } + + // 以管理员身份运行bat脚本 + String batFilePath = commInstallPath + "\\重启通讯服务.bat"; + try { + Runtime.getRuntime().exec(batFilePath); + } catch (IOException e) { + log.error("重启通讯服务失败", e); + } + return this.save(pqIcdPath); } + private String getCommInstallPath() { +// String workDir = System.getProperty("user.dir"); + String workDir = "D:\\program\\CN_Gather"; + // 获取映射文件存放文件夹 + String dirPath = workDir.substring(0, workDir.lastIndexOf("\\")) + "\\9100"; + return dirPath; + } + @Override @Transactional public boolean updateIcd(PqIcdPathParam.UpdateParam param) { @@ -62,6 +133,40 @@ public class PqIcdPathServiceImpl extends ServiceImpl FILE_SIZE_LIMIT) { + throw new BusinessException(IcdResponseEnum.FILE_SIZE_ERROR); + } + + try { + // 如果文件存在,则先删除 + String mappingFilePath = commInstallPath + "\\DeviceControl\\Config" + mappingFilename; + Path path = Paths.get(mappingFilePath); + File file = path.toFile(); + if (file.exists()) { + file.delete(); + } + + // 保存文件 + byte[] bytes = mappingFile.getBytes(); + BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(mappingFilePath)); + bufferedOutputStream.write(bytes); + bufferedOutputStream.flush(); + + bufferedOutputStream.close(); + } catch (IOException e) { + throw new BusinessException(ReportResponseEnum.FILE_UPLOAD_FAILED); + } + } return this.updateById(pqIcdPath); } 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 a1594536..40a2f0c7 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 @@ -789,61 +789,63 @@ public class PqReportServiceImpl extends ServiceImpl i if (Objects.isNull(pqDevVO)) { throw new BusinessException(ReportResponseEnum.DEVICE_NOT_EXIST); } - devReportParam.setDevId(devId); - // 获取设备型号 - DevType devType = devTypeService.getById(pqDevVO.getDevType()); - if (Objects.isNull(devType)) { - throw new BusinessException(ReportResponseEnum.DEVICE_TYPE_NOT_EXIST); - } - PqReport report = this.lambdaQuery().eq(PqReport::getId, plan.getReportTemplateId()).eq(PqReport::getState, DataStateEnum.ENABLE.getCode()).one(); - if (Objects.isNull(report)) { - throw new BusinessException(ReportResponseEnum.REPORT_TEMPLATE_NOT_EXIST); - } - Path basePath = Paths.get(report.getBasePath()); - Path detailPath = Paths.get(report.getDetailPath()); - try (InputStream baseInputStream = Files.newInputStream(basePath); - InputStream detailInputStream = Files.newInputStream(detailPath)) { - WordprocessingMLPackage detailModelDocument = WordprocessingMLPackage.load(detailInputStream); - // 获取文档基础部分,并替换占位符 - Map baseModelDataMap = dealBaseModelContrastData(plan, pqDevVO, devType); - InputStream wordFinishInputStream = wordReportService.replacePlaceholders(baseInputStream, baseModelDataMap); - WordprocessingMLPackage baseModelDocument = WordprocessingMLPackage.load(wordFinishInputStream); - MainDocumentPart baseDocumentPart = baseModelDocument.getMainDocumentPart(); - - // 获取数据模版页内容,根据脚本动态组装数据页内容 - MainDocumentPart detailDocumentPart = detailModelDocument.getMainDocumentPart(); - dealDataModelScatteredByBookmarkByPlanContrast(baseDocumentPart, detailDocumentPart, devReportParam, pqDevVO); - // 保存新的文档 - String dirPath = reportPath.concat(File.separator).concat(plan.getName()); - // 确保目录存在 - ensureDirectoryExists(dirPath); - // 构建文件名:cityName_gdName_subName_name.docx - String fileName = String.format("%s_%s_%s_%s.docx", - pqDevVO.getCityName() != null ? pqDevVO.getCityName() : "未知地市", - pqDevVO.getGdName() != null ? pqDevVO.getGdName() : "未知供电公司", - pqDevVO.getSubName() != null ? pqDevVO.getSubName() : "未知电站", - pqDevVO.getName() != null ? pqDevVO.getName() : "未知设备"); - // 判断是否需要在报告上输出水印 - String leader = baseModelDataMap.get(BaseReportKeyEnum.AUDIT_BY.getKey()); - String loginName = RequestUtil.getLoginNameByToken(); - SysUser user = sysUserService.getUserByLoginName(loginName); - if (!leader.equals(user.getName())) { - log.info("当前用户不是审核人,添加非正式水印"); - Docx4jUtil.addWatermarkToDocument(baseModelDocument, "非正式"); + if (CheckStateEnum.CHECKED.getValue().compareTo(pqDevVO.getCheckState()) <= 0) { + devReportParam.setDevId(devId); + // 获取设备型号 + DevType devType = devTypeService.getById(pqDevVO.getDevType()); + if (Objects.isNull(devType)) { + throw new BusinessException(ReportResponseEnum.DEVICE_TYPE_NOT_EXIST); + } + PqReport report = this.lambdaQuery().eq(PqReport::getId, plan.getReportTemplateId()).eq(PqReport::getState, DataStateEnum.ENABLE.getCode()).one(); + if (Objects.isNull(report)) { + throw new BusinessException(ReportResponseEnum.REPORT_TEMPLATE_NOT_EXIST); + } + Path basePath = Paths.get(report.getBasePath()); + Path detailPath = Paths.get(report.getDetailPath()); + try (InputStream baseInputStream = Files.newInputStream(basePath); + InputStream detailInputStream = Files.newInputStream(detailPath)) { + WordprocessingMLPackage detailModelDocument = WordprocessingMLPackage.load(detailInputStream); + // 获取文档基础部分,并替换占位符 + Map baseModelDataMap = dealBaseModelContrastData(plan, pqDevVO, devType); + InputStream wordFinishInputStream = wordReportService.replacePlaceholders(baseInputStream, baseModelDataMap); + WordprocessingMLPackage baseModelDocument = WordprocessingMLPackage.load(wordFinishInputStream); + MainDocumentPart baseDocumentPart = baseModelDocument.getMainDocumentPart(); + + // 获取数据模版页内容,根据脚本动态组装数据页内容 + MainDocumentPart detailDocumentPart = detailModelDocument.getMainDocumentPart(); + dealDataModelScatteredByBookmarkByPlanContrast(baseDocumentPart, detailDocumentPart, devReportParam, pqDevVO); + // 保存新的文档 + String dirPath = reportPath.concat(File.separator).concat(plan.getName()); + // 确保目录存在 + ensureDirectoryExists(dirPath); + // 构建文件名:cityName_gdName_subName_name.docx + String fileName = String.format("%s_%s_%s_%s.docx", + pqDevVO.getCityName() != null ? pqDevVO.getCityName() : "未知地市", + pqDevVO.getGdName() != null ? pqDevVO.getGdName() : "未知供电公司", + pqDevVO.getSubName() != null ? pqDevVO.getSubName() : "未知电站", + pqDevVO.getName() != null ? pqDevVO.getName() : "未知设备"); + // 判断是否需要在报告上输出水印 + String leader = baseModelDataMap.get(BaseReportKeyEnum.AUDIT_BY.getKey()); + String loginName = RequestUtil.getLoginNameByToken(); + SysUser user = sysUserService.getUserByLoginName(loginName); + if (!leader.equals(user.getName())) { + log.info("当前用户不是审核人,添加非正式水印"); + Docx4jUtil.addWatermarkToDocument(baseModelDocument, "非正式"); + } + Docx4jUtil.cleanBlankPagesAndRedundantPageBreaks(baseModelDocument); + baseModelDocument.save(new File(dirPath.concat(File.separator).concat(fileName))); + this.updateDevAndPlanState(devId, devReportParam.getPlanId()); + } catch (NoSuchFileException e) { + String filePath = e.getFile() != null ? e.getFile().replaceAll("\\\\", "/") : "未知文件"; + log.error("报告模板文件不存在 - 文件路径: {}", filePath, e); + throw new BusinessException(SystemResponseEnum.FILE_NOT_FOUND); + } catch (IOException e) { + log.error("报告文件读写异常", e); + throw new BusinessException(SystemResponseEnum.FILE_IO_ERROR); + } catch (Exception e) { + log.error(ReportResponseEnum.GENERATE_REPORT_ERROR.getMessage(), e); + throw new BusinessException(ReportResponseEnum.GENERATE_REPORT_ERROR); } - Docx4jUtil.cleanBlankPagesAndRedundantPageBreaks(baseModelDocument); - baseModelDocument.save(new File(dirPath.concat(File.separator).concat(fileName))); - this.updateDevAndPlanState(devId, devReportParam.getPlanId()); - } catch (NoSuchFileException e) { - String filePath = e.getFile() != null ? e.getFile().replaceAll("\\\\", "/") : "未知文件"; - log.error("报告模板文件不存在 - 文件路径: {}", filePath, e); - throw new BusinessException(SystemResponseEnum.FILE_NOT_FOUND); - } catch (IOException e) { - log.error("报告文件读写异常", e); - throw new BusinessException(SystemResponseEnum.FILE_IO_ERROR); - } catch (Exception e) { - log.error(ReportResponseEnum.GENERATE_REPORT_ERROR.getMessage(), e); - throw new BusinessException(ReportResponseEnum.GENERATE_REPORT_ERROR); } });