feat: 增加icd文件处理

dev
huangfeng 9 months ago
parent 7e52af155b
commit 1b17bb4f2a

@ -27,6 +27,18 @@ public class IcdConfigType {
@Column(name = "id") @Column(name = "id")
private Integer id; private Integer id;
@ApiModelProperty(name = "ICD文件id")
@Column(name = "icd_file_id")
private Integer icdFileId;
@ApiModelProperty(name = "ip")
@Column(name = "ip")
private String ip;
@ApiModelProperty(name = "OSI-AP-Title")
@Column(name = "ap_title")
private String apTitle;
@ApiModelProperty(name = "IED名称") @ApiModelProperty(name = "IED名称")
@Column(name = "ied_name") @Column(name = "ied_name")
private String iedName; private String iedName;

@ -0,0 +1,39 @@
package com.xydl.cac.entity;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "icd_file")
@ApiModel("ICD文件表")
public class IcdFile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@ApiModelProperty(name = "md5")
@Column(name = "md5")
private String md5;
@ApiModelProperty(name = "xml")
@Column(name = "xml")
private String xml;
@Transient
private List<IcdConfigType> configList;
}

@ -0,0 +1,15 @@
package com.xydl.cac.repository;
import com.xydl.cac.entity.IcdFile;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface IcdFileRepository extends JpaRepository<IcdFile, Integer>, JpaSpecificationExecutor<IcdFile> {
List<IcdFile> findByMd5(String md5);
}

@ -6,6 +6,7 @@ import com.xydl.cac.model.IcdAttUpdateModel;
import com.xydl.cac.repository.*; import com.xydl.cac.repository.*;
import com.xydl.cac.service.IcdFileConfigService; import com.xydl.cac.service.IcdFileConfigService;
import com.xydl.cac.util.IcdXmlUtil; import com.xydl.cac.util.IcdXmlUtil;
import com.xydl.cac.util.Md5;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
@ -22,7 +23,9 @@ import java.util.*;
public class IcdFileConfigServiceImpl implements IcdFileConfigService { public class IcdFileConfigServiceImpl implements IcdFileConfigService {
@Resource @Resource
IcdConfigTypeRepository repository; IcdFileRepository fileRepository;
@Resource
IcdConfigTypeRepository configRepository;
@Resource @Resource
IcdConfigTypeAttRepository attRepository; IcdConfigTypeAttRepository attRepository;
@Resource @Resource
@ -34,17 +37,19 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
@Override @Override
public void upload(String xml) throws Exception { public void upload(String xml) throws Exception {
LinkedHashMap<String, IcdConfigType> result = IcdXmlUtil.loadIcdType(xml); String md5 = Md5.getMD5Code(xml);
Collection<IcdConfigType> configList = result.values(); List<IcdFile> files = fileRepository.findByMd5(md5);
for (IcdConfigType config : configList) { if (!CollectionUtils.isEmpty(files)) {
List<IcdConfigType> list = repository.findByIedNameAndLdeviceInstAndLnClass( throw new BusinessException("该文件已存在");
config.getIedName(), config.getLdeviceInst(), config.getLnClass());
if (CollectionUtils.isEmpty(list)) {
repository.save(config);
} else {
config.setId(list.get(0).getId());
} }
IcdFile icdFile = IcdXmlUtil.loadIcdType(xml);
icdFile.setMd5(md5);
fileRepository.save(icdFile);
for (IcdConfigType config : icdFile.getConfigList()) {
config.setIcdFileId(icdFile.getId());
configRepository.save(config);
attRepository.deleteByIcdConfigTypeId(config.getId()); attRepository.deleteByIcdConfigTypeId(config.getId());
if (config.getAttMap() != null) { if (config.getAttMap() != null) {
Collection<IcdConfigTypeAtt> attList = config.getAttMap().values(); Collection<IcdConfigTypeAtt> attList = config.getAttMap().values();
@ -77,9 +82,9 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
public List<IcdConfigType> list(String iedName) throws Exception { public List<IcdConfigType> list(String iedName) throws Exception {
List<IcdConfigType> result; List<IcdConfigType> result;
if (StringUtils.isNotBlank(iedName)) { if (StringUtils.isNotBlank(iedName)) {
result = repository.findByIedName(iedName); result = configRepository.findByIedName(iedName);
} else { } else {
result = repository.findAll(); result = configRepository.findAll();
} }
for (IcdConfigType item : result) { for (IcdConfigType item : result) {
List<IcdConfigTypeAtt> atts = attRepository.findByIcdConfigTypeId(item.getId()); List<IcdConfigTypeAtt> atts = attRepository.findByIcdConfigTypeId(item.getId());
@ -97,7 +102,7 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
@Override @Override
public void update(IcdConfigType item) throws Exception { public void update(IcdConfigType item) throws Exception {
Optional<IcdConfigType> optional = repository.findById(item.getId()); Optional<IcdConfigType> optional = configRepository.findById(item.getId());
if (!optional.isPresent()) { if (!optional.isPresent()) {
throw new BusinessException("未找到该项"); throw new BusinessException("未找到该项");
} }
@ -110,7 +115,7 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
attRepository.saveAll(attList); attRepository.saveAll(attList);
} }
r.setTableName(item.getTableName()); r.setTableName(item.getTableName());
repository.save(r); configRepository.save(r);
} }
@Override @Override
@ -133,7 +138,7 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
public void delete(Integer id) { public void delete(Integer id) {
instRepository.deleteByIcdConfigTypeId(id); instRepository.deleteByIcdConfigTypeId(id);
attRepository.deleteByIcdConfigTypeId(id); attRepository.deleteByIcdConfigTypeId(id);
repository.deleteById(id); configRepository.deleteById(id);
} }
@Override @Override
@ -145,7 +150,8 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
public void clearAll() { public void clearAll() {
instRepository.deleteAll(); instRepository.deleteAll();
attRepository.deleteAll(); attRepository.deleteAll();
repository.deleteAll(); configRepository.deleteAll();
fileRepository.deleteAll();
} }
@Override @Override
@ -157,7 +163,7 @@ public class IcdFileConfigServiceImpl implements IcdFileConfigService {
} }
List<String> paramList = new ArrayList<>(); List<String> paramList = new ArrayList<>();
List<IcdConfigType> typeLIst = repository.findAll(); List<IcdConfigType> typeLIst = configRepository.findAll();
for (IcdConfigType type : typeLIst) { for (IcdConfigType type : typeLIst) {
List<IcdConfigTypeInst> instList = instRepository.findByIcdConfigTypeId(type.getId()); List<IcdConfigTypeInst> instList = instRepository.findByIcdConfigTypeId(type.getId());
List<IcdConfigTypeAtt> attList = attRepository.findByIcdConfigTypeId(type.getId()); List<IcdConfigTypeAtt> attList = attRepository.findByIcdConfigTypeId(type.getId());

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.xydl.cac.entity.IcdConfigType; import com.xydl.cac.entity.IcdConfigType;
import com.xydl.cac.entity.IcdFile;
import com.xydl.cac.exception.BusinessException; import com.xydl.cac.exception.BusinessException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -13,7 +14,7 @@ import java.util.*;
public class IcdXmlUtil { public class IcdXmlUtil {
public static LinkedHashMap<String, IcdConfigType> loadIcdType(String xml) throws Exception { public static IcdFile loadIcdType(String xml) throws Exception {
XmlMapper xmlMapper = XmlMapper.builder() XmlMapper xmlMapper = XmlMapper.builder()
.build(); .build();
JsonNode root; JsonNode root;
@ -22,12 +23,16 @@ public class IcdXmlUtil {
} catch (Exception ex) { } catch (Exception ex) {
throw new BusinessException("ICD文件解析失败请上传正确的xml类型文件"); throw new BusinessException("ICD文件解析失败请上传正确的xml类型文件");
} }
LinkedHashMap<String, IcdConfigType> result = processTypeRoot(root); IcdFile icdFile = IcdFile.builder()
return result; .xml(xml)
.build();
LinkedHashMap<String, IcdConfigType> iedMap = processTypeRoot(root);
icdFile.setConfigList(new ArrayList<>(iedMap.values()));
return icdFile;
} }
private static LinkedHashMap<String, IcdConfigType> processTypeRoot(JsonNode root) { private static LinkedHashMap<String, IcdConfigType> processTypeRoot(JsonNode root) {
LinkedHashMap<String, IcdConfigType> result = new LinkedHashMap<>(); LinkedHashMap<String, IcdConfigType> iedMap = new LinkedHashMap<>();
Map<String, JsonNode> mapLNodeType = buildLNodeTypeMap(root); Map<String, JsonNode> mapLNodeType = buildLNodeTypeMap(root);
Map<String, JsonNode> mapDOType = buildDOTypeMap(root); Map<String, JsonNode> mapDOType = buildDOTypeMap(root);
@ -35,24 +40,46 @@ public class IcdXmlUtil {
List<JsonNode> iedList = findNodes(root, "IED"); List<JsonNode> iedList = findNodes(root, "IED");
for (JsonNode iedNode : iedList) { for (JsonNode iedNode : iedList) {
processIEDNode(result, iedNode, processIEDNode(iedMap, iedNode,
mapLNodeType, mapDOType, mapDAType); mapLNodeType, mapDOType, mapDAType);
} }
return result;
List<JsonNode> apList = findNodes(root, "ConnectedAP");
for (JsonNode apNode : apList) {
String iedName = apNode.get("iedName").asText();
IcdConfigType config = iedMap.get(iedName);
if (config != null) {
processAPNode(apNode, config);
}
}
return iedMap;
}
private static void processAPNode(JsonNode apNode, IcdConfigType config) {
List<JsonNode> pList = findNodes(apNode, "P");
for (JsonNode pNode : pList) {
String type = pNode.get("type").asText();
if ("IP".equalsIgnoreCase(type)) {
config.setIp(pNode.asText());
} else if ("OSI-AP-Title".equalsIgnoreCase(type)) {
config.setApTitle(pNode.asText());
}
}
} }
private static void processIEDNode(LinkedHashMap<String, IcdConfigType> result, JsonNode iedNode, private static void processIEDNode(LinkedHashMap<String, IcdConfigType> iedMap, JsonNode iedNode,
Map<String, JsonNode> mapLNodeType, Map<String, JsonNode> mapDOType, Map<String, JsonNode> mapDAType) { Map<String, JsonNode> mapLNodeType, Map<String, JsonNode> mapDOType, Map<String, JsonNode> mapDAType) {
String iedName = iedNode.get("name").asText(); String iedName = iedNode.get("name").asText();
List<JsonNode> devList = findNodes(iedNode, "LDevice"); List<JsonNode> devList = findNodes(iedNode, "LDevice");
for (JsonNode dev : devList) { for (JsonNode dev : devList) {
processTypeDeviceNode(result, iedName, dev, processTypeDeviceNode(iedMap, iedName, dev,
mapLNodeType, mapDOType, mapDAType); mapLNodeType, mapDOType, mapDAType);
} }
} }
private static void processTypeDeviceNode(LinkedHashMap<String, IcdConfigType> result, String iedName, JsonNode deviceNode, private static void processTypeDeviceNode(LinkedHashMap<String, IcdConfigType> iedMap, String iedName, JsonNode deviceNode,
Map<String, JsonNode> mapLNodeType, Map<String, JsonNode> mapDOType, Map<String, JsonNode> mapDAType) { Map<String, JsonNode> mapLNodeType, Map<String, JsonNode> mapDOType, Map<String, JsonNode> mapDAType) {
String ldeviceInst = deviceNode.get("inst").asText(); String ldeviceInst = deviceNode.get("inst").asText();
Map<String, JsonNode> mapLN = buildLNMap(deviceNode); Map<String, JsonNode> mapLN = buildLNMap(deviceNode);
@ -84,7 +111,7 @@ public class IcdXmlUtil {
String key = iedName + ldeviceInst + "/" + lnClass; String key = iedName + ldeviceInst + "/" + lnClass;
if ("MX".equals(fc)) { if ("MX".equals(fc)) {
IcdConfigType config = result.get(key); IcdConfigType config = iedMap.get(key);
if (config == null) { if (config == null) {
config = IcdConfigType.builder() config = IcdConfigType.builder()
.iedName(iedName) .iedName(iedName)
@ -92,20 +119,20 @@ public class IcdXmlUtil {
.lnClass(lnClass) .lnClass(lnClass)
.lnDesc(lnDesc) .lnDesc(lnDesc)
.build(); .build();
result.put(key, config); iedMap.put(key, config);
} }
String param = fc + "$" + doName + "$" + lastname; String param = fc + "$" + doName + "$" + lastname;
config.addInst(lnInst); config.addInst(lnInst);
config.addAtt(doName, doDesc, param, lnInst); config.addAtt(doName, doDesc, param, lnInst);
} else if ("ST".equals(fc)) { } else if ("ST".equals(fc)) {
IcdConfigType config = result.get(key); IcdConfigType config = iedMap.get(key);
if (config == null) { if (config == null) {
config = IcdConfigType.builder() config = IcdConfigType.builder()
.iedName(iedName) .iedName(iedName)
.ldeviceInst(ldeviceInst) .ldeviceInst(ldeviceInst)
.lnClass(lnClass) .lnClass(lnClass)
.build(); .build();
result.put(key, config); iedMap.put(key, config);
} }
String param = fc + "$" + doName + "$" + lastname; String param = fc + "$" + doName + "$" + lastname;
config.addInst(lnInst); config.addInst(lnInst);

Loading…
Cancel
Save