From 287ca2cddcaf5bad9a9dd1b9765657295e590d12 Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Wed, 17 Jun 2026 19:32:27 +0800 Subject: [PATCH] =?UTF-8?q?icd=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../icd/controller/PqIcdPathController.java | 58 +++++++++++ .../gather/icd/mapper/PqIcdPathMapper.java | 5 +- .../icd/mapper/mapping/PqIcdPathMapper.xml | 20 ++++ .../icd/pojo/enums/IcdResponseEnum.java | 10 +- .../gather/icd/pojo/param/PqIcdPathParam.java | 4 +- .../gather/icd/pojo/vo/PqIcdExportJsonVO.java | 38 ++++++++ .../gather/icd/service/IPqIcdPathService.java | 2 + .../service/impl/PqIcdPathServiceImpl.java | 97 +++++++++++++++++-- .../service/support/IcdPayloadAssembler.java | 2 +- 9 files changed, 225 insertions(+), 11 deletions(-) create mode 100644 detection/src/main/java/com/njcn/gather/icd/pojo/vo/PqIcdExportJsonVO.java 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 d6a087f4..a2209864 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 @@ -1,13 +1,16 @@ package com.njcn.gather.icd.controller; +import com.alibaba.fastjson.JSON; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.njcn.common.pojo.exception.BusinessException; import com.njcn.common.pojo.annotation.OperateInfo; import com.njcn.common.pojo.constant.OperateType; import com.njcn.common.pojo.enums.common.LogEnum; import com.njcn.common.pojo.enums.response.CommonResponseEnum; import com.njcn.common.pojo.response.HttpResult; import com.njcn.common.utils.LogUtil; +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; @@ -24,9 +27,11 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.util.UriUtils; import javax.validation.Valid; +import java.time.LocalDate; import java.nio.charset.StandardCharsets; import java.util.List; @@ -156,4 +161,57 @@ public class PqIcdPathController extends BaseController { .contentLength(body.length) .body(new ByteArrayResource(body)); } + + @OperateInfo(info = LogEnum.BUSINESS_COMMON) + @PostMapping("/export/json") + @ApiOperation("导出icd json") + @ApiImplicitParam(name = "ids", value = "id集合", required = true) + public ResponseEntity exportJson(@RequestBody List ids) { + byte[] body = pqIcdPathService.exportIcdJson(ids); + String fileName = UriUtils.encode(LocalDate.now() + ".json", StandardCharsets.UTF_8); + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + fileName) + .contentType(MediaType.APPLICATION_JSON) + .contentLength(body.length) + .body(new ByteArrayResource(body)); + } + + @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD) + @PostMapping("/import/json") + @ApiOperation("导入icd json") + @ApiImplicitParam(name = "file", value = "json文件", required = true) + public HttpResult importJson(@RequestParam("file") MultipartFile file) { + String methodDescribe = getMethodDescribe("importJson"); + LogUtil.njcnDebug(log, "{},导入文件为:{}", methodDescribe, file == null ? null : file.getOriginalFilename()); + + List param = parseExternalCreateParams(file); + boolean result = pqIcdPathService.addUpstreamIcd(param); + if (result) { + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe); + } else { + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe); + } + } + + private List parseExternalCreateParams(MultipartFile file) { + if (file == null || file.isEmpty()) { + throw new BusinessException(IcdResponseEnum.ICD_FILE_NOT_NULL); + } + String fileName = file.getOriginalFilename(); + if (fileName == null || !fileName.toLowerCase().endsWith(".json")) { + throw new BusinessException(IcdResponseEnum.JSON_FORMAT_ERROR); + } + try { + String content = new String(file.getBytes(), StandardCharsets.UTF_8); + List params = JSON.parseArray(content, PqIcdPathParam.ExternalCreateParam.class); + if (params == null) { + throw new BusinessException(IcdResponseEnum.JSON_FORMAT_ERROR); + } + return params; + } catch (BusinessException ex) { + throw ex; + } catch (Exception ex) { + throw new BusinessException(IcdResponseEnum.JSON_FORMAT_ERROR); + } + } } diff --git a/detection/src/main/java/com/njcn/gather/icd/mapper/PqIcdPathMapper.java b/detection/src/main/java/com/njcn/gather/icd/mapper/PqIcdPathMapper.java index c1616ecd..91373775 100644 --- a/detection/src/main/java/com/njcn/gather/icd/mapper/PqIcdPathMapper.java +++ b/detection/src/main/java/com/njcn/gather/icd/mapper/PqIcdPathMapper.java @@ -1,8 +1,9 @@ package com.njcn.gather.icd.mapper; -import com.github.yulichang.base.MPJBaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.github.yulichang.base.MPJBaseMapper; import com.njcn.gather.icd.pojo.po.PqIcdPath; +import com.njcn.gather.icd.pojo.vo.PqIcdExportJsonVO; import org.apache.ibatis.annotations.Param; import java.util.List; @@ -34,5 +35,7 @@ public interface PqIcdPathMapper extends MPJBaseMapper { PqIcdPath selectDetailById(@Param("id") String id); PqIcdPath selectExportById(@Param("id") String id); + + List selectExportJsonByIds(@Param("ids") List ids); } diff --git a/detection/src/main/java/com/njcn/gather/icd/mapper/mapping/PqIcdPathMapper.xml b/detection/src/main/java/com/njcn/gather/icd/mapper/mapping/PqIcdPathMapper.xml index 759c3a28..476f3c3e 100644 --- a/detection/src/main/java/com/njcn/gather/icd/mapper/mapping/PqIcdPathMapper.xml +++ b/detection/src/main/java/com/njcn/gather/icd/mapper/mapping/PqIcdPathMapper.xml @@ -114,5 +114,25 @@ where id = #{id} and state = 1 + + 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 index ba7ead7a..aa567353 100644 --- 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 @@ -28,7 +28,15 @@ public enum IcdResponseEnum { REFERENCE_ICD_INVALID("A018018", "参照标准ICD无效"), GENERATE_FILE("A018019", "文件生成失败"), ICD_TXT_SYNC_FAILED("A018020", "ICD txt文件同步失败"), - REFERENCE_STANDARD_ICD_NAME_NOT_FOUND("A018021", "参照标准ICD名称不存在"); + REFERENCE_STANDARD_ICD_NAME_NOT_FOUND("A018021", "参照标准ICD名称不存在"), + EXTERNAL_ICD_ITEM_NOT_NULL("A018022", "导入数据项不能为空"), + EXTERNAL_ICD_ID_REPEAT("A018023", "导入数据中存在重复id"), + EXTERNAL_ICD_DELETED_NOT_ALLOW_OVERWRITE("A018024", "该ICD已删除,不允许通过导入覆盖"), + EXTERNAL_ICD_IMPORT_FAILED("A018025", "ICD导入失败"), + ANGLE_NOT_NULL("A018026", "是否支持相角不能为空"), + ANGLE_VALUE_ERROR("A018027", "是否支持相角取值错误"), + USE_PHASE_INDEX_NOT_NULL("A018028", "是否使用相别指标不能为空"), + USE_PHASE_INDEX_VALUE_ERROR("A018029", "是否使用相别指标取值错误"); private final String code; private final String 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 ef926b85..c97d7df3 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 @@ -68,9 +68,9 @@ public class PqIcdPathParam { @Pattern(regexp = PatternRegex.SCRIPT_NAME_REGEX, message = DetectionValidMessage.ICD_NAME_FORMAT_ERROR) private String name; - @ApiModelProperty(value = "icdFile", required = true) + @ApiModelProperty(value = "icd", required = true) @NotBlank(message = IcdExternalValidMessage.ICD_FILE_NOT_BLANK) - private String icdFile; + private String icd; } diff --git a/detection/src/main/java/com/njcn/gather/icd/pojo/vo/PqIcdExportJsonVO.java b/detection/src/main/java/com/njcn/gather/icd/pojo/vo/PqIcdExportJsonVO.java new file mode 100644 index 00000000..39a901bc --- /dev/null +++ b/detection/src/main/java/com/njcn/gather/icd/pojo/vo/PqIcdExportJsonVO.java @@ -0,0 +1,38 @@ +package com.njcn.gather.icd.pojo.vo; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +/** + * ICD JSON export item. + * + * @author caozehui + * @date 2026-06-17 + */ +@Data +public class PqIcdExportJsonVO { + private String id; + + private String name; + + @JSONField(serialize = false) + private byte[] rawIcd; + + private String icd; + + private Integer angle; + + private Integer usePhaseIndex; + + private String jsonStr; + + private String xmlStr; + + private Integer result; + + private String msg; + + private Integer type; + + private String referenceIcdId; +} diff --git a/detection/src/main/java/com/njcn/gather/icd/service/IPqIcdPathService.java b/detection/src/main/java/com/njcn/gather/icd/service/IPqIcdPathService.java index e5f4d5f8..a78eceae 100644 --- a/detection/src/main/java/com/njcn/gather/icd/service/IPqIcdPathService.java +++ b/detection/src/main/java/com/njcn/gather/icd/service/IPqIcdPathService.java @@ -77,6 +77,8 @@ public interface IPqIcdPathService extends IService { */ byte[] exportIcd(String id); + byte[] exportIcdJson(List ids); + /** * 根据设备类型id获取Icd * 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 1b6fc628..fe6bbbf4 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 @@ -2,20 +2,23 @@ package com.njcn.gather.icd.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.common.pojo.constant.PatternRegex; 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.pojo.vo.PqIcdExportJsonVO; +import com.njcn.gather.pojo.constant.DetectionValidMessage; import com.njcn.gather.icd.service.IPqIcdPathService; import com.njcn.gather.icd.service.support.IcdArchiveBuilder; import com.njcn.gather.icd.service.support.IcdPayloadAssembler; -import com.njcn.gather.pojo.enums.DetectionResponseEnum; import com.njcn.web.factory.PageFactory; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -29,6 +32,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.util.Base64; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -100,20 +104,22 @@ public class PqIcdPathServiceImpl extends ServiceImpl requestStandardIds = collectRequestStandardIds(param); - boolean allSaved = true; Path directory = resolveIcdTxtDir(); for (PqIcdPathParam.ExternalCreateParam item : param) { validateBusinessFields(item, null); PqIcdPath current = this.baseMapper.selectById(item.getId()); + if (current != null && !ObjectUtil.equals(current.getState(), DataStateEnum.ENABLE.getCode())) { + throw new BusinessException(IcdResponseEnum.EXTERNAL_ICD_DELETED_NOT_ALLOW_OVERWRITE); + } PqIcdPath pqIcdPath = icdPayloadAssembler.fromExternalCreate(item, item.getType(), item.getAngle(), item.getUsePhaseIndex()); String currentId = current == null ? null : current.getId(); String oldName = current == null ? null : current.getName(); validateExternalReferenceIcd(pqIcdPath.getType(), pqIcdPath.getReferenceIcdId(), currentId, requestStandardIds); boolean saved = current == null ? this.save(pqIcdPath) : this.updateById(pqIcdPath); if (!saved) { - allSaved = false; - break; + throw new BusinessException(IcdResponseEnum.EXTERNAL_ICD_IMPORT_FAILED); } if (current == null) { writeIcdTextFile(directory, pqIcdPath.getName(), pqIcdPath.getJsonStr()); @@ -121,7 +127,7 @@ public class PqIcdPathServiceImpl extends ServiceImpl ids) { + if (CollUtil.isEmpty(ids)) { + throw new BusinessException(IcdResponseEnum.ICD_NOT_FOUND); + } + List details = this.baseMapper.selectExportJsonByIds(ids); + if (CollUtil.isEmpty(details)) { + throw new BusinessException(IcdResponseEnum.ICD_NOT_FOUND); + } + for (PqIcdExportJsonVO detail : details) { + detail.setIcd(encodeIcdFile(detail.getRawIcd())); + normalizeExportDetail(detail); + } + return JSON.toJSONString(details).getBytes(StandardCharsets.UTF_8); + } + @Override public PqIcdPath getIcdByDevType(String devTypeId) { return this.baseMapper.selectIcdByDevType(devTypeId); @@ -255,6 +277,50 @@ public class PqIcdPathServiceImpl extends ServiceImpl params) { + Set ids = new HashSet<>(); + for (PqIcdPathParam.ExternalCreateParam param : params) { + if (param == null) { + throw new BusinessException(IcdResponseEnum.EXTERNAL_ICD_ITEM_NOT_NULL); + } + validateExternalItem(param); + String trimmedId = StrUtil.trim(param.getId()); + if (!ids.add(trimmedId)) { + throw new BusinessException(IcdResponseEnum.EXTERNAL_ICD_ID_REPEAT); + } + } + } + + private void validateExternalItem(PqIcdPathParam.ExternalCreateParam param) { + if (StrUtil.isBlank(param.getId())) { + throw new BusinessException(DetectionValidMessage.ID_NOT_BLANK); + } + if (!ReUtil.isMatch(PatternRegex.SYSTEM_ID, StrUtil.trim(param.getId()))) { + throw new BusinessException(DetectionValidMessage.ID_FORMAT_ERROR); + } + if (StrUtil.isBlank(param.getName())) { + throw new BusinessException(DetectionValidMessage.NAME_NOT_BLANK); + } + if (!ReUtil.isMatch(PatternRegex.SCRIPT_NAME_REGEX, StrUtil.trim(param.getName()))) { + throw new BusinessException(DetectionValidMessage.ICD_NAME_FORMAT_ERROR); + } + if (StrUtil.isBlank(param.getIcd())) { + throw new BusinessException(IcdResponseEnum.ICD_FILE_NOT_NULL); + } + if (ObjectUtil.isNull(param.getAngle())) { + throw new BusinessException(IcdResponseEnum.ANGLE_NOT_NULL); + } + if (!ObjectUtil.equals(param.getAngle(), 0) && !ObjectUtil.equals(param.getAngle(), 1)) { + throw new BusinessException(IcdResponseEnum.ANGLE_VALUE_ERROR); + } + if (ObjectUtil.isNull(param.getUsePhaseIndex())) { + throw new BusinessException(IcdResponseEnum.USE_PHASE_INDEX_NOT_NULL); + } + if (!ObjectUtil.equals(param.getUsePhaseIndex(), 0) && !ObjectUtil.equals(param.getUsePhaseIndex(), 1)) { + throw new BusinessException(IcdResponseEnum.USE_PHASE_INDEX_VALUE_ERROR); + } + } + private boolean requiresReferenceIcd(Integer type) { return ObjectUtil.equals(type, TYPE_NON_STANDARD) || ObjectUtil.equals(type, TYPE_UPSTREAM_NON_STANDARD); } @@ -311,4 +377,23 @@ public class PqIcdPathServiceImpl extends ServiceImpl