|
|
|
@ -1,12 +1,17 @@
|
|
|
|
|
package com.hnac.hzims.history.service.impl; |
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import com.google.common.util.concurrent.ThreadFactoryBuilder; |
|
|
|
|
import com.hnac.hzims.common.logs.utils.StringUtils; |
|
|
|
|
import com.hnac.hzims.history.entity.HistoryPointRecordEntity; |
|
|
|
|
import com.hnac.hzims.history.service.IHistoryDeviceService; |
|
|
|
|
import com.hnac.hzims.history.vo.HistoryDataParamVo; |
|
|
|
|
import com.hnac.hzims.history.vo.HistoryDevicePointVo; |
|
|
|
|
import com.hnac.hzims.history.vo.HistoryFactoryPointVo; |
|
|
|
|
import com.hnac.hzims.history.service.IHistoryPointRecordService; |
|
|
|
|
import com.hnac.hzims.history.vo.*; |
|
|
|
|
import com.hnac.hzinfo.sdk.core.response.Result; |
|
|
|
|
import com.hnac.hzinfo.sdk.v5.device.DeviceDataClient; |
|
|
|
|
import com.hnac.hzinfo.sdk.v5.device.client.DeviceClient; |
|
|
|
|
import com.hnac.hzinfo.sdk.v5.device.source.dto.HisFacSourceDataDTO; |
|
|
|
|
import com.hnac.hzinfo.sdk.v5.device.vo.DeviceInstanceAttrVO; |
|
|
|
|
import com.hnac.hzinfo.sdk.v5.device.vo.DeviceInstanceVO; |
|
|
|
|
import com.hnac.hzinfo.sdk.v5.hzfac.HzFacClient; |
|
|
|
@ -14,14 +19,20 @@ import com.hnac.hzinfo.sdk.v5.hzfac.vo.FacDeviceAttrVO;
|
|
|
|
|
import com.hnac.hzinfo.sdk.v5.hzfac.vo.FacDeviceVO; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import org.springblade.core.log.exception.ServiceException; |
|
|
|
|
import org.springblade.core.secure.utils.AuthUtil; |
|
|
|
|
import org.springblade.core.tool.api.R; |
|
|
|
|
import org.springblade.core.tool.utils.CollectionUtil; |
|
|
|
|
import org.springblade.core.tool.utils.DateUtil; |
|
|
|
|
import org.springblade.core.tool.utils.ObjectUtil; |
|
|
|
|
import org.springblade.core.tool.utils.StringUtil; |
|
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.time.ZoneOffset; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.concurrent.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -31,12 +42,17 @@ import java.util.stream.Collectors;
|
|
|
|
|
@AllArgsConstructor |
|
|
|
|
public class HistoryDeviceServiceImpl implements IHistoryDeviceService { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final IHistoryPointRecordService pointRecordService; |
|
|
|
|
|
|
|
|
|
private final HzFacClient hzFacClient; |
|
|
|
|
|
|
|
|
|
private final DeviceClient deviceClient; |
|
|
|
|
|
|
|
|
|
private final DeviceDataClient deviceDataClient; |
|
|
|
|
|
|
|
|
|
private static final ExecutorService pool = new ThreadPoolExecutor(2, 2, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(10), new ThreadFactoryBuilder().setNameFormat("history-point-save-%d").build() , new ThreadPoolExecutor.CallerRunsPolicy()); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 项目设备列表 |
|
|
|
|
* @param projectId |
|
|
|
@ -163,7 +179,7 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public Map<String, Object> historyCurve(HistoryDataParamVo param) { |
|
|
|
|
public List<HistoryDataVo> historyCurve(HistoryDataParamVo param) { |
|
|
|
|
// 计算查询数据维度 : 0-10秒 、 1-1分钟 、2-5分钟 .....
|
|
|
|
|
Integer intervalType = this.computeInterval(param.getStartTime(),param.getEndTime()); |
|
|
|
|
// 查询数据
|
|
|
|
@ -186,10 +202,62 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService {
|
|
|
|
|
throw new ServiceException(result.getMsg()); |
|
|
|
|
} |
|
|
|
|
if(CollectionUtil.isEmpty(result.getData())){ |
|
|
|
|
return new HashMap<>(); |
|
|
|
|
return new ArrayList<>(); |
|
|
|
|
} |
|
|
|
|
// 异步线程保存常用查询
|
|
|
|
|
this.commonUserSave(param); |
|
|
|
|
|
|
|
|
|
// 返回结构
|
|
|
|
|
return result.getData().entrySet().stream().map(entry->{ |
|
|
|
|
HistoryDataVo history = new HistoryDataVo(); |
|
|
|
|
if("firstAttr".equals(entry.getKey())){ |
|
|
|
|
history.setAttrIndex(param.getFirstAttrId()); |
|
|
|
|
history.setAttrIndexName(param.getFirstAttrName()); |
|
|
|
|
}else if("secondAttr".equals(entry.getKey())){ |
|
|
|
|
history.setAttrIndex(param.getSecondAttrId()); |
|
|
|
|
history.setAttrIndexName(param.getSecondAttrName()); |
|
|
|
|
}else if("thirdlyAttr".equals(entry.getKey())){ |
|
|
|
|
history.setAttrIndex(param.getThirdlyAttrId()); |
|
|
|
|
history.setAttrIndexName(param.getThirdlyAttrName()); |
|
|
|
|
}else if("fourthAttr".equals(entry.getKey())){ |
|
|
|
|
history.setAttrIndex(param.getFourthAttrId()); |
|
|
|
|
history.setAttrIndexName(param.getFourthAttrName()); |
|
|
|
|
}else{ |
|
|
|
|
history.setAttrIndex(param.getFifthAttrName()); |
|
|
|
|
history.setAttrIndexName(param.getFifthAttrName()); |
|
|
|
|
} |
|
|
|
|
HistoryDetailVo historyDetail = JSONObject.parseObject(JSONObject.toJSONString(entry.getValue()),HistoryDetailVo.class); |
|
|
|
|
Date start = null; |
|
|
|
|
String val = null; |
|
|
|
|
List<HistoryDetailDataVo> detailRecords = new ArrayList<>(); |
|
|
|
|
for(HistoryDetailDataVo data : historyDetail.getData()) { |
|
|
|
|
HistoryDetailDataVo detail = new HistoryDetailDataVo(); |
|
|
|
|
detail.setTs(data.getTs()); |
|
|
|
|
if(ObjectUtil.isEmpty(val)){ |
|
|
|
|
start = DateUtil.parse(data.getTs(),DateUtil.PATTERN_DATETIME); |
|
|
|
|
val = data.getVal(); |
|
|
|
|
detail.setVal(val); |
|
|
|
|
}else{ |
|
|
|
|
Date end = DateUtil.parse(data.getTs(),DateUtil.PATTERN_DATETIME); |
|
|
|
|
if(StringUtils.isEmpty(data.getVal())){ |
|
|
|
|
if(end.getTime() - start.getTime() <= 5 * 60 * 1000L){ |
|
|
|
|
detail.setVal(val); |
|
|
|
|
}else{ |
|
|
|
|
detail.setVal(data.getVal()); |
|
|
|
|
} |
|
|
|
|
}else{ |
|
|
|
|
detail.setVal(data.getVal()); |
|
|
|
|
start = DateUtil.parse(data.getTs(),DateUtil.PATTERN_DATETIME); |
|
|
|
|
val = data.getVal(); |
|
|
|
|
} |
|
|
|
|
return result.getData(); |
|
|
|
|
} |
|
|
|
|
detailRecords.add(detail); |
|
|
|
|
} |
|
|
|
|
history.setData(detailRecords); |
|
|
|
|
return history; |
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取设备历史数据列表 |
|
|
|
@ -197,8 +265,20 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public Map<String, Object> historyTabulation(HistoryDataParamVo param) { |
|
|
|
|
return null; |
|
|
|
|
public List<Map<String, Object>> historyTabulation(HistoryTabulationParamVo param) { |
|
|
|
|
HisFacSourceDataDTO source = new HisFacSourceDataDTO(); |
|
|
|
|
source.setProjectId(param.getProjectId()); |
|
|
|
|
source.setFacDeivceAttrIds(param.getFacDeivceAttrIds()); |
|
|
|
|
source.setBeginTime(param.getStartTime()); |
|
|
|
|
source.setFinishTime(param.getEndTime()); |
|
|
|
|
source.setIsPage(true); |
|
|
|
|
source.setCurrent(param.getCurrent()); |
|
|
|
|
source.setPageSize(param.getPageSize()); |
|
|
|
|
Result<List<Map<String, Object>>> tabulations = deviceDataClient.hisFacSourceData(source); |
|
|
|
|
if(!tabulations.isSuccess() || CollectionUtil.isEmpty(tabulations.getData())){ |
|
|
|
|
return new ArrayList<>(); |
|
|
|
|
} |
|
|
|
|
return tabulations.getData(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -231,4 +311,74 @@ public class HistoryDeviceServiceImpl implements IHistoryDeviceService {
|
|
|
|
|
} |
|
|
|
|
return 5; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 异步保存常用记录 |
|
|
|
|
* @param param |
|
|
|
|
*/ |
|
|
|
|
@Async |
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
protected void commonUserSave(HistoryDataParamVo param) { |
|
|
|
|
// 当前用户Id
|
|
|
|
|
Long userId = AuthUtil.getUserId(); |
|
|
|
|
|
|
|
|
|
// 点位Id
|
|
|
|
|
List<String> points = new ArrayList<>(); |
|
|
|
|
if(StringUtils.isNotEmpty(param.getFirstAttrId())){ |
|
|
|
|
points.add(param.getFirstAttrId()); |
|
|
|
|
} |
|
|
|
|
if(StringUtils.isNotEmpty(param.getSecondAttrId())){ |
|
|
|
|
points.add(param.getSecondAttrId()); |
|
|
|
|
} |
|
|
|
|
if(StringUtils.isNotEmpty(param.getThirdlyAttrId())){ |
|
|
|
|
points.add(param.getThirdlyAttrId()); |
|
|
|
|
} |
|
|
|
|
if(StringUtils.isNotEmpty(param.getFourthAttrId())){ |
|
|
|
|
points.add(param.getFourthAttrId()); |
|
|
|
|
} |
|
|
|
|
if(StringUtils.isNotEmpty(param.getFifthAttrId())){ |
|
|
|
|
points.add(param.getFifthAttrId()); |
|
|
|
|
} |
|
|
|
|
// 查询历史保存数据
|
|
|
|
|
List<HistoryPointRecordEntity> records = pointRecordService.list(Wrappers.<HistoryPointRecordEntity>lambdaQuery() |
|
|
|
|
.in(HistoryPointRecordEntity::getHistoryPoint,points) |
|
|
|
|
.eq(HistoryPointRecordEntity::getCreateUser,userId) |
|
|
|
|
.eq(HistoryPointRecordEntity::getStationId,param.getStationId()) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
pointRecordService.saveOrUpdateBatch(points.stream().map(point->{ |
|
|
|
|
HistoryPointRecordEntity entity = new HistoryPointRecordEntity(); |
|
|
|
|
entity.setStationId(param.getStationId()); |
|
|
|
|
entity.setStationName(param.getStationName()); |
|
|
|
|
entity.setHistoryPoint(point); |
|
|
|
|
if(param.getFirstAttrId().equals(point)){ |
|
|
|
|
entity.setHistoryPointName(param.getFirstAttrName()); |
|
|
|
|
}else if(param.getSecondAttrId().equals(point)){ |
|
|
|
|
entity.setHistoryPointName(param.getSecondAttrName()); |
|
|
|
|
}else if(param.getThirdlyAttrId().equals(point)){ |
|
|
|
|
entity.setHistoryPointName(param.getThirdlyAttrName()); |
|
|
|
|
}else if(param.getFourthAttrId().equals(point)){ |
|
|
|
|
entity.setHistoryPointName(param.getFourthAttrName()); |
|
|
|
|
}else{ |
|
|
|
|
entity.setHistoryPointName(param.getFifthAttrName()); |
|
|
|
|
} |
|
|
|
|
if(CollectionUtil.isEmpty(records)){ |
|
|
|
|
entity.setNumber(1); |
|
|
|
|
}else{ |
|
|
|
|
// 包含
|
|
|
|
|
if(records.stream().map(HistoryPointRecordEntity::getHistoryPoint).collect(Collectors.toList()).contains(point)){ |
|
|
|
|
Optional<HistoryPointRecordEntity> optional = records.stream().filter(o->o.getHistoryPoint().equals(point)).findFirst(); |
|
|
|
|
if(optional.isPresent()){ |
|
|
|
|
entity.setId(optional.get().getId()); |
|
|
|
|
entity.setNumber(optional.get().getNumber() + 1); |
|
|
|
|
}else{ |
|
|
|
|
entity.setNumber(1); |
|
|
|
|
} |
|
|
|
|
}else{ |
|
|
|
|
entity.setNumber(1); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return entity; |
|
|
|
|
}).collect(Collectors.toList())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|