icd调整

This commit is contained in:
caozehui
2026-06-17 19:32:27 +08:00
parent 82fdd7664b
commit 287ca2cddc
9 changed files with 225 additions and 11 deletions

View File

@@ -1,13 +1,16 @@
package com.njcn.gather.icd.controller; package com.njcn.gather.icd.controller;
import com.alibaba.fastjson.JSON;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType; import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum; import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum; import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult; import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil; 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.param.PqIcdPathParam;
import com.njcn.gather.icd.pojo.po.PqIcdPath; import com.njcn.gather.icd.pojo.po.PqIcdPath;
import com.njcn.gather.icd.service.IPqIcdPathService; import com.njcn.gather.icd.service.IPqIcdPathService;
@@ -24,9 +27,11 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils; import org.springframework.web.util.UriUtils;
import javax.validation.Valid; import javax.validation.Valid;
import java.time.LocalDate;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
@@ -156,4 +161,57 @@ public class PqIcdPathController extends BaseController {
.contentLength(body.length) .contentLength(body.length)
.body(new ByteArrayResource(body)); .body(new ByteArrayResource(body));
} }
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
@PostMapping("/export/json")
@ApiOperation("导出icd json")
@ApiImplicitParam(name = "ids", value = "id集合", required = true)
public ResponseEntity<ByteArrayResource> exportJson(@RequestBody List<String> 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<Boolean> importJson(@RequestParam("file") MultipartFile file) {
String methodDescribe = getMethodDescribe("importJson");
LogUtil.njcnDebug(log, "{},导入文件为:{}", methodDescribe, file == null ? null : file.getOriginalFilename());
List<PqIcdPathParam.ExternalCreateParam> 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<PqIcdPathParam.ExternalCreateParam> 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<PqIcdPathParam.ExternalCreateParam> 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);
}
}
} }

View File

@@ -1,8 +1,9 @@
package com.njcn.gather.icd.mapper; package com.njcn.gather.icd.mapper;
import com.github.yulichang.base.MPJBaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.po.PqIcdPath;
import com.njcn.gather.icd.pojo.vo.PqIcdExportJsonVO;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List; import java.util.List;
@@ -34,5 +35,7 @@ public interface PqIcdPathMapper extends MPJBaseMapper<PqIcdPath> {
PqIcdPath selectDetailById(@Param("id") String id); PqIcdPath selectDetailById(@Param("id") String id);
PqIcdPath selectExportById(@Param("id") String id); PqIcdPath selectExportById(@Param("id") String id);
List<PqIcdExportJsonVO> selectExportJsonByIds(@Param("ids") List<String> ids);
} }

View File

@@ -114,5 +114,25 @@
where id = #{id} where id = #{id}
and state = 1 and state = 1
</select> </select>
<select id="selectExportJsonByIds" resultType="com.njcn.gather.icd.pojo.vo.PqIcdExportJsonVO">
select id,
name,
Icd as rawIcd,
angle,
use_phase_index as usePhaseIndex,
Json_Str as jsonStr,
Xml_Str as xmlStr,
Result as result,
Msg as msg,
Type as type,
Reference_Icd_Id as referenceIcdId
from pq_icd_path
where state = 1
and id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
</mapper> </mapper>

View File

@@ -28,7 +28,15 @@ public enum IcdResponseEnum {
REFERENCE_ICD_INVALID("A018018", "参照标准ICD无效"), REFERENCE_ICD_INVALID("A018018", "参照标准ICD无效"),
GENERATE_FILE("A018019", "文件生成失败"), GENERATE_FILE("A018019", "文件生成失败"),
ICD_TXT_SYNC_FAILED("A018020", "ICD txt文件同步失败"), 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 code;
private final String message; private final String message;

View File

@@ -68,9 +68,9 @@ public class PqIcdPathParam {
@Pattern(regexp = PatternRegex.SCRIPT_NAME_REGEX, message = DetectionValidMessage.ICD_NAME_FORMAT_ERROR) @Pattern(regexp = PatternRegex.SCRIPT_NAME_REGEX, message = DetectionValidMessage.ICD_NAME_FORMAT_ERROR)
private String name; private String name;
@ApiModelProperty(value = "icdFile", required = true) @ApiModelProperty(value = "icd", required = true)
@NotBlank(message = IcdExternalValidMessage.ICD_FILE_NOT_BLANK) @NotBlank(message = IcdExternalValidMessage.ICD_FILE_NOT_BLANK)
private String icdFile; private String icd;
} }

View File

@@ -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;
}

View File

@@ -77,6 +77,8 @@ public interface IPqIcdPathService extends IService<PqIcdPath> {
*/ */
byte[] exportIcd(String id); byte[] exportIcd(String id);
byte[] exportIcdJson(List<String> ids);
/** /**
* 根据设备类型id获取Icd * 根据设备类型id获取Icd
* *

View File

@@ -2,20 +2,23 @@ package com.njcn.gather.icd.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil; 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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum; import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException; 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.mapper.PqIcdPathMapper;
import com.njcn.gather.icd.pojo.enums.IcdResponseEnum; import com.njcn.gather.icd.pojo.enums.IcdResponseEnum;
import com.njcn.gather.icd.pojo.param.PqIcdPathParam; import com.njcn.gather.icd.pojo.param.PqIcdPathParam;
import com.njcn.gather.icd.pojo.po.PqIcdPath; 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.IPqIcdPathService;
import com.njcn.gather.icd.service.support.IcdArchiveBuilder; import com.njcn.gather.icd.service.support.IcdArchiveBuilder;
import com.njcn.gather.icd.service.support.IcdPayloadAssembler; import com.njcn.gather.icd.service.support.IcdPayloadAssembler;
import com.njcn.gather.pojo.enums.DetectionResponseEnum;
import com.njcn.web.factory.PageFactory; import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -29,6 +32,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.Base64;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -100,20 +104,22 @@ public class PqIcdPathServiceImpl extends ServiceImpl<PqIcdPathMapper, PqIcdPath
if (CollUtil.isEmpty(param)) { if (CollUtil.isEmpty(param)) {
return true; return true;
} }
validateExternalItems(param);
Set<String> requestStandardIds = collectRequestStandardIds(param); Set<String> requestStandardIds = collectRequestStandardIds(param);
boolean allSaved = true;
Path directory = resolveIcdTxtDir(); Path directory = resolveIcdTxtDir();
for (PqIcdPathParam.ExternalCreateParam item : param) { for (PqIcdPathParam.ExternalCreateParam item : param) {
validateBusinessFields(item, null); validateBusinessFields(item, null);
PqIcdPath current = this.baseMapper.selectById(item.getId()); 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()); PqIcdPath pqIcdPath = icdPayloadAssembler.fromExternalCreate(item, item.getType(), item.getAngle(), item.getUsePhaseIndex());
String currentId = current == null ? null : current.getId(); String currentId = current == null ? null : current.getId();
String oldName = current == null ? null : current.getName(); String oldName = current == null ? null : current.getName();
validateExternalReferenceIcd(pqIcdPath.getType(), pqIcdPath.getReferenceIcdId(), currentId, requestStandardIds); validateExternalReferenceIcd(pqIcdPath.getType(), pqIcdPath.getReferenceIcdId(), currentId, requestStandardIds);
boolean saved = current == null ? this.save(pqIcdPath) : this.updateById(pqIcdPath); boolean saved = current == null ? this.save(pqIcdPath) : this.updateById(pqIcdPath);
if (!saved) { if (!saved) {
allSaved = false; throw new BusinessException(IcdResponseEnum.EXTERNAL_ICD_IMPORT_FAILED);
break;
} }
if (current == null) { if (current == null) {
writeIcdTextFile(directory, pqIcdPath.getName(), pqIcdPath.getJsonStr()); writeIcdTextFile(directory, pqIcdPath.getName(), pqIcdPath.getJsonStr());
@@ -121,7 +127,7 @@ public class PqIcdPathServiceImpl extends ServiceImpl<PqIcdPathMapper, PqIcdPath
syncUpdatedIcdTextFile(directory, oldName, pqIcdPath.getName(), pqIcdPath.getJsonStr()); syncUpdatedIcdTextFile(directory, oldName, pqIcdPath.getName(), pqIcdPath.getJsonStr());
} }
} }
return allSaved; return true;
} }
@Override @Override
@@ -185,6 +191,22 @@ public class PqIcdPathServiceImpl extends ServiceImpl<PqIcdPathMapper, PqIcdPath
return icdArchiveBuilder.build(detail); return icdArchiveBuilder.build(detail);
} }
@Override
public byte[] exportIcdJson(List<String> ids) {
if (CollUtil.isEmpty(ids)) {
throw new BusinessException(IcdResponseEnum.ICD_NOT_FOUND);
}
List<PqIcdExportJsonVO> 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 @Override
public PqIcdPath getIcdByDevType(String devTypeId) { public PqIcdPath getIcdByDevType(String devTypeId) {
return this.baseMapper.selectIcdByDevType(devTypeId); return this.baseMapper.selectIcdByDevType(devTypeId);
@@ -255,6 +277,50 @@ public class PqIcdPathServiceImpl extends ServiceImpl<PqIcdPathMapper, PqIcdPath
return ids; return ids;
} }
private void validateExternalItems(List<PqIcdPathParam.ExternalCreateParam> params) {
Set<String> 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) { private boolean requiresReferenceIcd(Integer type) {
return ObjectUtil.equals(type, TYPE_NON_STANDARD) || ObjectUtil.equals(type, TYPE_UPSTREAM_NON_STANDARD); return ObjectUtil.equals(type, TYPE_NON_STANDARD) || ObjectUtil.equals(type, TYPE_UPSTREAM_NON_STANDARD);
} }
@@ -311,4 +377,23 @@ public class PqIcdPathServiceImpl extends ServiceImpl<PqIcdPathMapper, PqIcdPath
private Path resolveTxtFile(Path directory, String name) { private Path resolveTxtFile(Path directory, String name) {
return directory.resolve(name + ".txt"); return directory.resolve(name + ".txt");
} }
private String encodeIcdFile(byte[] rawValue) {
if (rawValue == null || rawValue.length == 0) {
return null;
}
return Base64.getEncoder().encodeToString(rawValue);
}
private void normalizeExportDetail(PqIcdExportJsonVO detail) {
detail.setRawIcd(null);
detail.setAngle(detail.getAngle() == null ? 0 : detail.getAngle());
detail.setUsePhaseIndex(detail.getUsePhaseIndex() == null ? 1 : detail.getUsePhaseIndex());
if (isStandardType(detail.getType())) {
detail.setType(TYPE_UPSTREAM_STANDARD);
detail.setReferenceIcdId(null);
return;
}
detail.setType(TYPE_UPSTREAM_NON_STANDARD);
}
} }

View File

@@ -32,7 +32,7 @@ public class IcdPayloadAssembler {
} }
public PqIcdPath fromExternalCreate(PqIcdPathParam.ExternalCreateParam param, int type, int angle, int usePhaseIndex) { public PqIcdPath fromExternalCreate(PqIcdPathParam.ExternalCreateParam param, int type, int angle, int usePhaseIndex) {
byte[] fileBytes = validator.decodeExternalIcdFile(param.getIcdFile()); byte[] fileBytes = validator.decodeExternalIcdFile(param.getIcd());
validator.validateJson(param.getJsonStr()); validator.validateJson(param.getJsonStr());
validator.validateXml(param.getXmlStr()); validator.validateXml(param.getXmlStr());