package com.moral.service.impl;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Optional;
|
import java.util.stream.Collectors;
|
|
import javax.annotation.Resource;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.TypeReference;
|
import com.moral.entity.DictionaryData;
|
import com.moral.entity.OrganizationLayout;
|
import com.moral.entity.OrganizationSensorUnit;
|
import com.moral.entity.Sensor;
|
import com.moral.entity.SensorUnit;
|
import com.moral.entity.layout.RealTimeDeviceLayout;
|
import com.moral.entity.layout.RtdLayoutUpload;
|
import com.moral.entity.layout.SensorComb;
|
import com.moral.mapper.DictionaryDataMapper;
|
import com.moral.mapper.OrganizationLayoutMapper;
|
import com.moral.mapper.OrganizationSensorUnitMapper;
|
import com.moral.mapper.SensorMapper;
|
import com.moral.service.OrganizationLayoutService;
|
|
import org.springframework.data.annotation.Transient;
|
import org.springframework.stereotype.Service;
|
import tk.mybatis.mapper.entity.Example;
|
|
@Service
|
public class OrganizationLayoutServiceImpl implements OrganizationLayoutService{
|
@Resource
|
SensorMapper sensorMapper;
|
@Resource
|
OrganizationLayoutMapper orgLayoutMapper;
|
@Resource
|
OrganizationSensorUnitMapper orgUnitMapper;
|
private static String page_layout_fixed_items = "page_layout_fixed_items";
|
private static String page_layout_versions_sort = "page_layout_versions_sort";
|
@Resource
|
DictionaryDataMapper dataMapper;
|
private Optional<String> queryDictionaryDataValue(String dictDataKey) {
|
DictionaryData query = new DictionaryData();
|
query.setDictDataKey(dictDataKey);
|
List<DictionaryData> dataList = dataMapper.select(query);
|
List<String> fixedItemKeys = new ArrayList<>();
|
String json = null;
|
if(dataList.size()>0) {
|
DictionaryData data = dataList.get(0);
|
json = data.getDictDataValue() != null ? data.getDictDataValue().toString() : null;
|
}
|
return Optional.of(json);
|
}
|
@Override
|
public List<String> getFixedItemKeys() {
|
Optional jsonString = queryDictionaryDataValue(page_layout_fixed_items);
|
List<String> fixedItemKeys = new ArrayList<>();
|
if(jsonString.isPresent()) {
|
List<String> keyList = JSON.parseObject(jsonString.get().toString(),
|
new TypeReference<List<String>>(){});
|
if(keyList!=null) {
|
fixedItemKeys.addAll(keyList);
|
}
|
}
|
|
return fixedItemKeys;
|
}
|
public Integer queryPageConfigCountByOrgId(Integer orgId,String pageType) {
|
OrganizationLayout query = new OrganizationLayout();
|
query.setOrganizationId(orgId);
|
query.setPageType(pageType);
|
return orgLayoutMapper.selectCount(query);
|
}
|
@Override
|
public RealTimeDeviceLayout queryRealTimeDeviceLayout(Integer orgId,Integer version){
|
String pageType = getPageType(version);
|
if(queryPageConfigCountByOrgId(orgId,pageType) > 0) {
|
RealTimeDeviceLayout rtdLayout = new RealTimeDeviceLayout();
|
Example example = new Example(OrganizationLayout.class);
|
// 升序排序
|
example.orderBy("pagePositionIndex").asc();
|
// 查询图表key
|
example.or().andEqualTo("pageType",pageType)
|
.andEqualTo("organizationId",orgId)
|
.andEqualTo("pagePosition",RealTimeDeviceLayout.ChartSensorKey);
|
List<OrganizationLayout> ChartSensorKeys = orgLayoutMapper.selectByExample(example);
|
if(ChartSensorKeys.size()>0) {
|
rtdLayout.setChartSensorKey(ChartSensorKeys.get(0).getPagePositionValue());
|
}
|
// 查询默认区
|
example.clear();
|
example.or().andEqualTo("pageType",pageType)
|
.andEqualTo("organizationId",orgId)
|
.andEqualTo("pagePosition",RealTimeDeviceLayout.DefaultMonitorItems);
|
List<OrganizationLayout> defaultMonitorItems = orgLayoutMapper.selectByExample(example);
|
List<SensorComb> defaultSensorCombList = defaultMonitorItems.stream().map(
|
item-> {
|
SensorComb sensorComb = SensorComb.generate(item);
|
return sensorComb;
|
}
|
).collect(Collectors.toList());
|
rtdLayout.setDefaultMonitorItems(defaultSensorCombList);
|
// 查询核心区
|
example.clear();
|
example.or().andEqualTo("pageType",pageType)
|
.andEqualTo("organizationId",orgId)
|
.andEqualTo("pagePosition",RealTimeDeviceLayout.CoreMonitorItems);
|
List<OrganizationLayout> coreMonitorItems = orgLayoutMapper.selectByExample(example);
|
List<SensorComb> coreSensorCombList = coreMonitorItems.stream().map(
|
item-> {
|
SensorComb sensorComb = SensorComb.generate(item);
|
return sensorComb;
|
}
|
).collect(Collectors.toList());
|
rtdLayout.setCoreMonitorItems(coreSensorCombList);
|
// 查询固定区
|
example.clear();
|
example.or().andEqualTo("pageType",pageType)
|
.andEqualTo("organizationId",orgId)
|
.andEqualTo("pagePosition",RealTimeDeviceLayout.FixedMonitorItems);
|
List<OrganizationLayout> fixedMonitorItems = orgLayoutMapper.selectByExample(example);
|
List<SensorComb> fixedSensorCombList = fixedMonitorItems.stream().map(
|
item-> {
|
SensorComb sensorComb = SensorComb.generate(item);
|
return sensorComb;
|
}
|
).collect(Collectors.toList());
|
rtdLayout.setFixedMonitorItems(fixedSensorCombList);
|
this.loadSensorToComb(rtdLayout);
|
return rtdLayout;
|
}
|
return null;
|
}
|
private void loadSensorToComb(RealTimeDeviceLayout rtdLayout) {
|
List<SensorComb> allSensorCombList = new ArrayList<>();
|
allSensorCombList.addAll(rtdLayout.getCoreMonitorItems());
|
allSensorCombList.addAll(rtdLayout.getDefaultMonitorItems());
|
allSensorCombList.addAll(rtdLayout.getFixedMonitorItems());
|
List<String> sensorKeyList = allSensorCombList.stream().map(
|
item -> {
|
return item.getSensorKey();
|
}
|
).collect(Collectors.toList());
|
Example example = new Example(Sensor.class);
|
example.or().andIn("sensorKey",sensorKeyList);
|
List<Sensor> sensorList = sensorMapper.selectByExample(example);
|
for (SensorComb sensorComb: allSensorCombList) {
|
for (Sensor sensor :sensorList) {
|
if(sensor.getSensorKey()!=null
|
&&sensor.getSensorKey().equals(sensorComb.getSensorKey())){
|
sensorComb.setSensor(sensor);
|
// sensorComb.setName(sensor.getDescription());
|
// sensorComb.setSensorId(sensor.getId());
|
// sensorComb.setSourceUnit(sensor.getUnit());
|
// sensorComb.setTargetUnit(sensor.getUnit());
|
}
|
}
|
}
|
}
|
public static String getPageType(Integer version) {
|
return RealTimeDeviceLayout.PageTypePrefix + version.toString();
|
}
|
@Transient
|
@Override
|
public void saveRtdLayout(RtdLayoutUpload rtdUpload) {
|
|
String pageType = getPageType(rtdUpload.getVersion());
|
Integer orgId = rtdUpload.getOrganizationId();
|
RealTimeDeviceLayout rtdLayout = rtdUpload.getRealTimeDeviceLayout();
|
// 清空旧数据
|
this.deleteRtdLayout(orgId,rtdUpload.getVersion());
|
OrganizationLayout orgLayout = new OrganizationLayout();
|
orgLayout.setOrganizationId(orgId);
|
orgLayout.setPageType(pageType);
|
// 储存chartSensorKey
|
orgLayout.setPagePosition(RealTimeDeviceLayout.ChartSensorKey);
|
orgLayout.setPagePositionIndex(0);
|
orgLayout.setPagePositionValue(rtdLayout.getChartSensorKey());
|
orgLayoutMapper.insert(orgLayout);
|
// 储存核心区
|
List<SensorComb> coreSensorList = rtdLayout.getCoreMonitorItems();
|
orgLayout.setPagePosition(RealTimeDeviceLayout.CoreMonitorItems);
|
if( coreSensorList !=null ) {
|
for(int index=0;index< coreSensorList.size();index++) {
|
orgLayout.setPagePositionIndex(index);
|
orgLayout.setPagePositionValue(coreSensorList.get(index).getSensorKey());
|
orgLayoutMapper.insert(orgLayout);
|
}
|
}
|
// 储存核心区
|
List<SensorComb> defaultSensorList = rtdLayout.getDefaultMonitorItems();
|
orgLayout.setPagePosition(RealTimeDeviceLayout.DefaultMonitorItems);
|
if ( defaultSensorList != null ){
|
for(int index=0;index< defaultSensorList.size();index++) {
|
orgLayout.setPagePositionIndex(index);
|
orgLayout.setPagePositionValue(defaultSensorList.get(index).getSensorKey());
|
orgLayoutMapper.insert(orgLayout);
|
}
|
}
|
|
// 储存固定区
|
List<SensorComb> fixedSensorList = rtdLayout.getFixedMonitorItems();
|
orgLayout.setPagePosition(RealTimeDeviceLayout.FixedMonitorItems);
|
if( fixedSensorList != null ) {
|
for(int index=0;index< fixedSensorList.size();index++) {
|
orgLayout.setPagePositionIndex(index);
|
orgLayout.setPagePositionValue(fixedSensorList.get(index).getSensorKey());
|
orgLayoutMapper.insert(orgLayout);
|
}
|
}
|
}
|
@Override
|
public void deleteRtdLayout(Integer orgId, Integer version) {
|
// 清空旧数据
|
OrganizationLayout query = new OrganizationLayout();
|
query.setOrganizationId(orgId);
|
query.setPageType(getPageType(version));
|
orgLayoutMapper.delete(query);
|
}
|
@Override
|
public RealTimeDeviceLayout queryRtdLayoutWithUnit(Integer orgId, Integer versionNo) {
|
String pageType = getPageType(versionNo);
|
if(queryPageConfigCountByOrgId(orgId,pageType) == 0 ){
|
// orgId = dataMapper.selectSupperOrgId();
|
orgId = 0;
|
}
|
RealTimeDeviceLayout rtdLayout = queryRealTimeDeviceLayout(orgId,versionNo);
|
loadUnitToComb(orgId,rtdLayout);
|
return rtdLayout;
|
}
|
private List<Integer> getVersionNosSort() {
|
List<Integer> versionNoList = new ArrayList<>();
|
Optional<String> jsonString = queryDictionaryDataValue(page_layout_versions_sort);
|
if( jsonString.isPresent() ) {
|
versionNoList = JSON.parseArray(jsonString.get(),Integer.class);
|
}
|
return versionNoList;
|
}
|
@Override
|
public RealTimeDeviceLayout queryRtdLayoutWithUnit(Integer orgId, List<Integer> versionNos) {
|
RealTimeDeviceLayout rtdLayout = null;
|
if(versionNos.size() == 1) {
|
Integer versionNo = versionNos.get(0);
|
rtdLayout = queryRtdLayoutWithUnit(orgId,versionNo);
|
} else {
|
List<Integer> versionNosSort = getVersionNosSort();
|
Integer resultVersion = null;
|
for( int num =0 ; num < versionNosSort.size(); num++ ) {
|
Integer version =versionNosSort.get(num);
|
for (Integer ver:versionNos) {
|
if(ver == version) {
|
resultVersion = ver;
|
versionNos.remove(ver);
|
break;
|
}
|
}
|
if(resultVersion!=null) {
|
break;
|
}
|
}
|
if(resultVersion != null ) {
|
rtdLayout = queryRtdLayoutWithUnit(orgId,resultVersion);
|
if (rtdLayout !=null) {
|
List<Sensor> sensorList = sensorMapper.selectByVersionNos(versionNos);
|
List<SensorComb> allMonitorItems = new ArrayList<>();
|
allMonitorItems.addAll(rtdLayout.getDefaultMonitorItems());
|
allMonitorItems.addAll(rtdLayout.getCoreMonitorItems());
|
if(allMonitorItems.size()>0 && sensorList!=null) {
|
List<SensorComb> sensrCombPatchs = new ArrayList<>();
|
List<String> fixedItems = getFixedItemKeys();
|
sensorList
|
// 排除固定项
|
.stream().filter(
|
item -> {
|
return !fixedItems.stream().anyMatch(
|
key-> key.equals(item.getSensorKey())
|
);
|
}
|
).collect(Collectors.toList()).forEach(sensor -> {
|
boolean isPath = !allMonitorItems.stream().anyMatch(
|
sensorComb -> sensorComb.getSensorId() ==sensor.getId()
|
);
|
if(isPath) {
|
SensorComb sensorComb = SensorComb.generate(sensor);
|
sensrCombPatchs.add(sensorComb);
|
}
|
});
|
if( sensrCombPatchs.size() > 0 ) {
|
RealTimeDeviceLayout rtdLayoutTemp = new RealTimeDeviceLayout();
|
rtdLayoutTemp.setDefaultMonitorItems(sensrCombPatchs);
|
loadUnitToComb(orgId,rtdLayoutTemp);
|
rtdLayout.getDefaultMonitorItems()
|
.addAll(rtdLayoutTemp.getDefaultMonitorItems());
|
}
|
}
|
}
|
}
|
}
|
|
return rtdLayout;
|
}
|
|
// 装载单位信息到布局配置
|
private void loadUnitToComb(Integer orgId,RealTimeDeviceLayout rtdLayout) {
|
List<SensorComb> allList = new ArrayList<>();
|
allList.addAll(rtdLayout.getCoreMonitorItems());
|
allList.addAll(rtdLayout.getDefaultMonitorItems());
|
allList.addAll(rtdLayout.getFixedMonitorItems());
|
List<OrganizationSensorUnit> orgUnitList = orgUnitMapper.selectByOrgId(orgId);
|
allList.forEach(item -> {
|
for(OrganizationSensorUnit orgUnit: orgUnitList) {
|
SensorUnit sensorUnit = orgUnit.getSensorUnit();
|
if( sensorUnit!=null && sensorUnit.getSensorId() !=null) {
|
if(sensorUnit.getSensorId() == item.getSensorId()) {
|
item.setUnit(sensorUnit);
|
// item.setTargetUnit(sensorUnit.getName());
|
// item.setEvaluator(sensorUnit.getRules());
|
}
|
}
|
}
|
});
|
}
|
}
|