You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
backend/src/main/java/com/xydl/cac/service/impl/ParamBindServiceImpl.java

289 lines
12 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.xydl.cac.service.impl;
import com.xydl.cac.entity.*;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.BindDetail;
import com.xydl.cac.model.BindingModel;
import com.xydl.cac.model.ColumnModel;
import com.xydl.cac.model.Response;
import com.xydl.cac.repository.*;
import com.xydl.cac.service.DataService;
import com.xydl.cac.service.ParamBindService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ParamBindServiceImpl implements ParamBindService {
@Resource
BdzRepository bdzRepository;
@Resource
JgRepository jgRepository;
@Resource
ZsbRepository zsbRepository;
@Resource
ModevRepository modevRepository;
@Resource
ModevTypeRepository modevTypeRepository;
@Resource
IcdConfigTypeRepository typeRepository;
@Resource
IcdConfigTypeInstRepository instRepository;
@Resource
IcdConfigTypeAttRepository attRepository;
@Resource
RptparamindexRepository rptparamindexRepository;
@Resource
DataService dataService;
@Override
public List<Bdz> getTree() throws Exception {
List<Bdz> bdzList = bdzRepository.findAll();
for (Bdz bdz : bdzList) {
List<Jg> jgList = jgRepository.findByBdzid(bdz.getId());
bdz.setChildren(jgList);
for (Jg jg : jgList) {
jg.setBdzName(bdz.getMc());
List<Zsb> zsbList = zsbRepository.findByJgid(jg.getId());
jg.setChildren(zsbList);
for (Zsb zsb : zsbList) {
zsb.setBdzName(bdz.getMc());
zsb.setJgName(jg.getMc());
List<Modev> modevList = modevRepository.findByZsbid(zsb.getId());
zsb.setChildren(modevList);
}
}
}
return bdzList;
}
@Override
public List<IcdConfigTypeInst> instList(String iedName) {
List<IcdConfigTypeInst> result = new ArrayList<>();
List<IcdConfigType> typeList = typeRepository.findByIedName(iedName);
for (IcdConfigType type : typeList) {
List<IcdConfigTypeInst> instList = instRepository.findByIcdConfigTypeId(type.getId());
for (IcdConfigTypeInst inst : instList) {
String param = type.getIedName() + type.getLdeviceInst() + "/" + type.getLnClass()
+ inst.getInst();
inst.setParamIndex(param);
}
result.addAll(instList);
}
return result;
}
@Override
public Response preview(BindingModel item) throws Exception {
Optional<Modev> optionalModev = modevRepository.findById(item.getEqmid());
if (!optionalModev.isPresent()) {
throw new BusinessException("未找到该监测装置, eqmid=" + item.getEqmid());
}
Modev modev = optionalModev.get();
Optional<ModevType> optionalModevType = modevTypeRepository.findById(modev.getModevtid());
if (!optionalModevType.isPresent()) {
throw new BusinessException("该监测装置类型不正确, 请先修正");
}
ModevType modevType = optionalModevType.get();
Optional<IcdConfigTypeInst> optionalInst = instRepository.findById(item.getIcdid());
if (!optionalInst.isPresent()) {
throw new BusinessException("未找到该ICD逻辑设备实例, icdid=" + item.getIcdid());
}
IcdConfigTypeInst inst = optionalInst.get();
Optional<IcdConfigType> optionalType = typeRepository.findById(inst.getIcdConfigTypeId());
if (!optionalType.isPresent()) {
throw new BusinessException("未找到该实例对应的ICD配置类型, icdid=" + item.getIcdid());
}
IcdConfigType type = optionalType.get();
String param = type.getIedName() + type.getLdeviceInst() + "/" + type.getLnClass()
+ inst.getInst();
List<IcdConfigTypeAtt> attList = attRepository.findByIcdConfigTypeId(type.getId());
List<String> msgList = new ArrayList<>();
for (IcdConfigTypeAtt att : attList) {
if (StringUtils.isBlank(att.getColName())) {
msgList.add(att.getDoName() + "该属性还未配置绑定字段");
} else {
// String paramindex = param + "$" + att.getParam();
// Optional<Rptparamindex> optionalRpt = rptparamindexRepository.findById(paramindex);
// if (!optionalRpt.isPresent()) {
// throw new BusinessException("未找到该Rptparamindex对象参引=" + paramindex);
// }
}
}
Response resp = Response.success(attList);
if (StringUtils.isBlank(type.getTableName())) {
msgList.add("该ICD配置类型还未设置对应的tableName");
} else if (!type.getTableName().equals(modevType.getTablename())) {
msgList.add("该ICD配置类型的tableName(" + type.getTableName() + ")和该监测装置类型的tableName(" + modevType.getTablename() + ")不一致");
}
if (msgList.size() > 0) {
String message = msgList.stream().collect(Collectors.joining("\n\r"));
resp.setWarnMsg(message);
}
return resp;
}
@Override
public void bind(BindingModel item) throws Exception {
Optional<Modev> optionalModev = modevRepository.findById(item.getEqmid());
if (!optionalModev.isPresent()) {
throw new BusinessException("未找到该监测装置, eqmid=" + item.getEqmid());
}
Modev modev = optionalModev.get();
Optional<IcdConfigTypeInst> optionalInst = instRepository.findById(item.getIcdid());
if (!optionalInst.isPresent()) {
throw new BusinessException("未找到该ICD逻辑设备实例, icdid=" + item.getIcdid());
}
IcdConfigTypeInst inst = optionalInst.get();
Optional<IcdConfigType> optionalType = typeRepository.findById(inst.getIcdConfigTypeId());
if (!optionalType.isPresent()) {
throw new BusinessException("未找到该实例对应的ICD配置类型, icdid=" + item.getIcdid());
}
IcdConfigType type = optionalType.get();
List<Modev> list = modevRepository.findByIcdidAndIdIsNot(item.getIcdid(), item.getEqmid());
if (!CollectionUtils.isEmpty(list)) {
throw new BusinessException("该逻辑设备实例已被" + list.get(0).getName() + "绑定");
}
List<IcdConfigTypeAtt> attList = attRepository.findByIcdConfigTypeId(type.getId());
Optional<ModevType> optionalModevType = modevTypeRepository.findById(modev.getModevtid());
if (optionalModevType.isPresent()) {
ModevType modevType = optionalModevType.get();
if (StringUtils.isNotBlank(modevType.getTablename())) {
List<ColumnModel> columnList = dataService.getDataTableColumns(modevType.getTablename());
for (IcdConfigTypeAtt att : attList) {
if (StringUtils.isNotBlank(att.getColName())) {
boolean found = false;
for (ColumnModel col : columnList) {
if (col.getName().equals(att.getColName())) {
found = true;
break;
}
}
if (!found) {
throw new BusinessException("当前监控设备对应的表" + modevType.getTablename() + "里不存在配置属性"
+ att.getDoName() + "绑定的字段" + att.getColName());
}
}
}
}
}
modev.setIcdid(item.getIcdid());
modevRepository.save(modev);
}
@Override
public void unbind(Integer eqmid) throws Exception {
Optional<Modev> optionalModev = modevRepository.findById(eqmid);
if (!optionalModev.isPresent()) {
throw new BusinessException("未找到该监测装置, eqmid=" + eqmid);
}
Modev modev = optionalModev.get();
modev.setIcdid(null);
modevRepository.save(modev);
}
@Override
public BindDetail getBind(Integer eqmid) throws Exception {
Optional<Modev> optionalModev = modevRepository.findById(eqmid);
if (!optionalModev.isPresent()) {
throw new BusinessException("未找到该监测装置");
}
Modev modev = optionalModev.get();
BindDetail result = new BindDetail();
result.setEqmid(eqmid);
Optional<ModevType> optionalModevType = modevTypeRepository.findById(modev.getModevtid());
if (optionalModevType.isPresent()) {
ModevType modevType = optionalModevType.get();
if (StringUtils.isNotBlank(modevType.getTablename())) {
List<ColumnModel> columnList = dataService.getDataTableColumns(modevType.getTablename());
result.setColumnList(columnList);
}
}
if (modev.getIcdid() != null) {
result.setIcdid(modev.getIcdid());
Optional<IcdConfigTypeInst> optionalInst = instRepository.findById(modev.getIcdid());
if (optionalInst.isPresent()) {
IcdConfigTypeInst inst = optionalInst.get();
Optional<IcdConfigType> optionalType = typeRepository.findById(inst.getIcdConfigTypeId());
if (optionalType.isPresent()) {
IcdConfigType type = optionalType.get();
result.setIedName(type.getIedName());
String param = type.getIedName() + type.getLdeviceInst() + "/" + type.getLnClass()
+ inst.getInst();
List<IcdConfigTypeAtt> attList = attRepository.findByIcdConfigTypeId(type.getId());
result.setAttList(attList);
}
}
}
return result;
}
@Override
public String generateParamindex() throws Exception {
List<Modev> modevList = modevRepository.findByIcdidIsNotNull();
if (!CollectionUtils.isEmpty(modevList)) {
for (Modev item : modevList) {
generateOne(item);
}
return "已对" + modevList.size() + "条绑定数据生成对应的61850参引数据";
} else {
throw new BusinessException("无任何绑定数据");
}
}
private void generateOne(Modev item) throws Exception {
Optional<IcdConfigTypeInst> optionalInst = instRepository.findById(item.getIcdid());
if (!optionalInst.isPresent()) {
throw new BusinessException("未找到该ICD逻辑设备实例, eqmid=" + item.getId() + ", icdid=" + item.getIcdid());
}
IcdConfigTypeInst inst = optionalInst.get();
Optional<IcdConfigType> optionalType = typeRepository.findById(inst.getIcdConfigTypeId());
if (!optionalType.isPresent()) {
throw new BusinessException("未找到该实例对应的ICD配置类型, icdid=" + item.getIcdid());
}
IcdConfigType type = optionalType.get();
String param = type.getIedName() + type.getLdeviceInst() + "/" + type.getLnClass()
+ inst.getInst();
List<IcdConfigTypeAtt> attList = attRepository.findByIcdConfigTypeId(type.getId());
for (IcdConfigTypeAtt att : attList) {
if (StringUtils.isNotBlank(att.getColName())) {
String paramindex = param + "$" + att.getParam();
Optional<Rptparamindex> optionalRpt = rptparamindexRepository.findById(paramindex);
if (!optionalRpt.isPresent()) {
throw new BusinessException(item.getName() + "的字段" + att.getColName() + "对应的属性"
+ att.getDoName() + "未找到该Rptparamindex对象参引=" + paramindex);
}
Rptparamindex rpt = optionalRpt.get();
rpt.setTablename(type.getTableName());
rpt.setColname(att.getColName());
rpt.setEqmid(item.getId());
rptparamindexRepository.save(rpt);
}
}
}
@Override
public void clearAll() {
dataService.clearAllBind();
}
}