@ -1,7 +1,26 @@
package com.ruoyi.business.service.impl ;
import java.util.List ;
import java.time.LocalDate ;
import java.time.LocalDateTime ;
import java.time.LocalTime ;
import java.time.ZoneOffset ;
import java.time.format.DateTimeFormatter ;
import java.util.* ;
import java.util.stream.Collectors ;
import com.github.pagehelper.PageHelper ;
import com.ruoyi.business.domain.HwDevice ;
import com.ruoyi.business.domain.HwElectronicFence ;
import com.ruoyi.business.domain.VO.* ;
import com.ruoyi.business.mapper.HwDeviceMapper ;
import com.ruoyi.business.mapper.HwElectronicFenceMapper ;
import com.ruoyi.common.core.constant.TdEngineConstants ;
import com.ruoyi.common.core.utils.DateUtils ;
import com.ruoyi.common.core.utils.StringUtils ;
import com.ruoyi.system.api.domain.SysDept ;
import com.ruoyi.tdengine.api.RemoteTdEngineService ;
import com.ruoyi.tdengine.api.domain.DeviceStatus ;
import com.ruoyi.tdengine.api.domain.TdSelectDto ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import com.ruoyi.business.mapper.HwMonitorUnitMapper ;
@ -19,6 +38,14 @@ public class HwMonitorUnitServiceImpl implements IHwMonitorUnitService
{
@Autowired
private HwMonitorUnitMapper hwMonitorUnitMapper ;
@Autowired
private HwElectronicFenceMapper hwElectronicFenceMapper ;
@Autowired
private RemoteTdEngineService remoteTdEgineService ;
@Autowired
private HwDeviceMapper hwDeviceMapper ;
/ * *
* 查 询 监 控 单 元
@ -31,6 +58,93 @@ public class HwMonitorUnitServiceImpl implements IHwMonitorUnitService
{
return hwMonitorUnitMapper . selectHwMonitorUnitByMonitorUnitId ( monitorUnitId ) ;
}
/ * *
* 查 询 区 域 数 量 站 场 景 区 总 数 百 分 比
* * /
@Override
public List < HwMonitorUnitVo > selectMonitorPercentage ( ) {
List < HwMonitorUnitVo > list = hwMonitorUnitMapper . selectMonitorPercentage ( ) ;
List < HwMonitorUnitVo > hwScenes = hwMonitorUnitMapper . selectSceneNameById ( ) ;
int count = hwScenes . size ( ) ;
if ( list . size ( ) < = hwScenes . size ( ) ) {
count = list . size ( ) ;
}
for ( int i = 0 ; i < count ; i + + ) {
for ( int j = 0 ; j < count ; j + + ) {
if ( list . get ( i ) . getSceneId ( ) . equals ( hwScenes . get ( i ) . getSceneId ( ) ) ) {
list . get ( i ) . setSceneName ( hwScenes . get ( i ) . getSceneName ( ) ) ;
}
}
}
Integer value = 0 ;
//
Integer sums = 0 ;
if ( list . size ( ) > 5 ) {
for ( int i = 4 ; i < list . size ( ) ; i + + ) {
value = value + list . get ( i ) . sum ;
}
}
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
sums = sums + list . get ( i ) . sum ;
}
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
list . get ( i ) . setPercentage ( ( double ) ( list . get ( i ) . getSum ( ) / sums ) ) ;
}
HwMonitorUnitVo hwMonitorUnitVo = new HwMonitorUnitVo ( ) ;
hwMonitorUnitVo . setSceneName ( "其他" ) ;
hwMonitorUnitVo . setSum ( value ) ;
if ( list . size ( ) > 5 ) {
list . add ( 5 , hwMonitorUnitVo ) ;
return list . subList ( 0 , 5 ) ;
}
else {
return list ;
}
}
//获取总的设备数量与监控单元数量
@Override
public AllNumsVo selectAllNums ( ) {
//监控单元
String monitor = hwMonitorUnitMapper . selectSubSetNums ( ) ;
Integer integer = hwMonitorUnitMapper . selectDeviceNums ( ) ;
AllNumsVo vo = new AllNumsVo ( ) ;
vo . setSubSum ( Integer . valueOf ( monitor ) ) ;
vo . setSum ( integer ) ;
return vo ;
}
@Override
public List < AllNumsVo > selectSenceAllNums ( ) {
List < AllNumsVo > allNumsVos = hwMonitorUnitMapper . selectAllNums ( ) ;
List < AllNumsVo > allNums = hwMonitorUnitMapper . selectsum ( ) ;
for ( int i = 0 ; i < allNums . size ( ) ; i + + ) {
for ( int j = 0 ; j < allNums . size ( ) ; j + + ) {
if ( allNums . get ( i ) . getSceneId ( ) . equals ( allNumsVos . get ( i ) . getSceneId ( ) ) ) {
allNums . get ( i ) . setSubSum ( allNumsVos . get ( i ) . getSubSum ( ) ) ;
}
}
}
return allNums ;
}
@Override
public List < HwMonitorUnit > selectLimitSubMonitorUnit ( ) {
List < HwMonitorUnit > hwMonitorUnits = hwMonitorUnitMapper . selectLimitSubMonitorUnit ( ) ;
return hwMonitorUnits ;
}
@Override
public List < AlarmStatsVo > selectAlarmStats ( ) {
return hwMonitorUnitMapper . selectAlarmStats ( ) ;
}
/ * *
* 查 询 监 控 单 元 列 表
@ -44,6 +158,138 @@ public class HwMonitorUnitServiceImpl implements IHwMonitorUnitService
return hwMonitorUnitMapper . selectHwMonitorUnitList ( hwMonitorUnit ) ;
}
@Override
public SubDeviceSumVo selectSubDeviceSum ( Long sceneId ) {
int SubSum = hwMonitorUnitMapper . selectSubDeviceSum ( sceneId ) ;
List < HwDevice > hwDevices = hwDeviceMapper . selectDeviceNum ( sceneId ) ;
int onlineDevice = 0 ;
for ( int i = 0 ; i < hwDevices . size ( ) ; i + + ) {
if ( hwDevices . get ( i ) . getActiveStatus ( ) . equals ( "1" ) & & hwDevices . get ( i ) . getDeviceStatus ( ) . equals ( "1" ) ) {
onlineDevice = onlineDevice + 1 ;
}
}
SubDeviceSumVo subDeviceSumVo = new SubDeviceSumVo ( ) ;
subDeviceSumVo . setSubSum ( String . valueOf ( SubSum ) ) ;
subDeviceSumVo . setDeviceNum ( String . valueOf ( hwDevices . size ( ) ) ) ;
subDeviceSumVo . setOnlineDeviceNum ( String . valueOf ( onlineDevice ) ) ;
return subDeviceSumVo ;
}
//todo 电子围栏
@Override
public Integer selectElectronicNum ( Long sceneId ) {
List < Long > longs = hwMonitorUnitMapper . selectEleInScene ( sceneId ) ;
return null ;
}
@Override
public HwMonitorUnit selectHwmonitorUnitSub ( Long monitorUnitId ) {
return hwMonitorUnitMapper . selectHwMonitorUnitByMonitorUnitId ( monitorUnitId ) ;
}
@Override
// 加一个begintime参数, endtime参数 stream时间戳 从开始时间0点开始, 结束时间23.59.59结束
public int selectTdengine ( String beginTime , String endTime , Long sceneId ) {
DeviceStatus queryDeviceStatus = new DeviceStatus ( ) ;
Map < String , Object > params = queryDeviceStatus . getParams ( ) ;
DateTimeFormatter ofPattern = DateTimeFormatter . ofPattern ( "yyyy-MM-dd" , Locale . CHINA ) ;
LocalDate parse = LocalDate . parse ( beginTime , ofPattern ) ;
long begintime = parse . atStartOfDay ( ) . toInstant ( ZoneOffset . of ( "+8" ) ) . toEpochMilli ( ) ;
DateTimeFormatter ofPattern1 = DateTimeFormatter . ofPattern ( "yyyy-MM-dd" , Locale . CHINA ) ;
LocalDate parse1 = LocalDate . parse ( endTime , ofPattern ) ;
long endtime = LocalDateTime . of ( parse1 , LocalTime . MAX ) . toInstant ( ZoneOffset . of ( "+8" ) ) . toEpochMilli ( ) ;
params . put ( "beginTime" , begintime ) ;
params . put ( "endTime" , endtime ) ;
queryDeviceStatus . setParams ( params ) ;
if ( sceneId ! = null ) {
queryDeviceStatus . setSceneId ( sceneId ) ;
}
int value = 0 ;
int sum = 0 ;
//todo 使用id(以解决)
//map key时间戳, value List<DeviceStatus> 对象
List < DeviceStatus > list = ( List < DeviceStatus > ) remoteTdEgineService . getOnlineDevicesGroupByDay ( queryDeviceStatus ) ;
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
if ( list . get ( i ) . getDeviceType ( ) = = 1 ) {
sum = sum + hwMonitorUnitMapper . selectReleatedDeviceIdNums ( list . get ( i ) . getDeviceId ( ) ) ;
}
else {
value + + ;
}
value = value + sum ;
}
return value ;
//设备类型==网关 根据releated_device_id = 网关设备设备id查询个数
//网关子设备数+非网关设备数( getDeviceType()! =1)
}
// @Override
// public List<Map<String, Object>> selectLatestDataByTags(Long monitorUnitId) {
// List<Map<String, Object>> latestData = new ArrayList<Map<String, Object>>;
// List<HwDevice> hwDevices = hwDeviceMapper.selectDeviceByMonitorId(monitorUnitId);
// TdSelectDto tdSelectDto = new TdSelectDto();
// for (int i = 0; i < hwDevices.size(); i++){
// tdSelectDto.setDatabaseName(TdEngineConstants.PLATFORM_DB_NAME);
// tdSelectDto.settableName(TdEngineConstants.getDeviceDataTableName(hwDevices.get(i).getDeviceId());
// tdSelectDto.settagsName(monitorUnitId);
// tdSelectDto.set
// latestData = (List<Map<String, Object>>) remoteTdEgineService.getLatestData(tdSelectDto);
// }
// return latestData;
// }
//todo SysDeptServiceImpl类中的递归方法recursionFn(以实现)
@Override
public List < HwMonitorUnit > selectTreeList ( List < HwMonitorUnit > hwMonitorUnits ) {
List < HwMonitorUnit > returnList = new ArrayList < > ( ) ;
List < Long > tempList = hwMonitorUnits . stream ( ) . map ( HwMonitorUnit : : getMonitorUnitId ) . collect ( Collectors . toList ( ) ) ;
for ( HwMonitorUnit hwMonitorUnit : hwMonitorUnits ) {
if ( ! tempList . contains ( hwMonitorUnit . getParentId ( ) ) )
recursionFn ( hwMonitorUnits , hwMonitorUnit ) ;
returnList . add ( hwMonitorUnit ) ;
}
return returnList ;
}
private void recursionFn ( List < HwMonitorUnit > list , HwMonitorUnit t )
{
// 得到子节点列表
List < HwMonitorUnit > childList = getChildList ( list , t ) ;
t . setChild ( childList ) ;
for ( HwMonitorUnit tChild : childList )
{
if ( hasChild ( list , tChild ) )
{
recursionFn ( list , tChild ) ;
}
}
}
private List < HwMonitorUnit > getChildList ( List < HwMonitorUnit > list , HwMonitorUnit t )
{
List < HwMonitorUnit > tlist = new ArrayList < HwMonitorUnit > ( ) ;
Iterator < HwMonitorUnit > it = list . iterator ( ) ;
while ( it . hasNext ( ) )
{
HwMonitorUnit n = ( HwMonitorUnit ) it . next ( ) ;
if ( StringUtils . isNotNull ( n . getParentId ( ) ) & & n . getParentId ( ) . longValue ( ) = = t . getMonitorUnitId ( ) . longValue ( ) )
{
tlist . add ( n ) ;
}
}
return tlist ;
}
private boolean hasChild ( List < HwMonitorUnit > list , HwMonitorUnit t )
{
return getChildList ( list , t ) . size ( ) > 0 ? true : false ;
}
/ * *
* 新 增 监 控 单 元
*
@ -94,3 +340,23 @@ public class HwMonitorUnitServiceImpl implements IHwMonitorUnitService
return hwMonitorUnitMapper . deleteHwMonitorUnitByMonitorUnitId ( monitorUnitId ) ;
}
}
//remoteTdEngineSevice.getOnlineDevicesGroupByDay方法获取
// 参数实例:
// // params.put("beginTime", beginTime);
//// params.put("endTime", endTime);
//// queryDeviceStatus.setParams(params);
//// if (sceneId != null) {
//// queryDeviceStatus.setSceneId(sceneId);
//// }
//
// 这是IDEA参数传的参数Content-Type: application/json
// {
// "params": { "beginTime":1666666666666,"endTime":1766666666666 }
//
// }
//
// 如果在IDEA测试可以{
// "params": {"databaseName":"db_scene_1","superTableName":"st_ds_1",
// "beginTime":1666666666666,"endTime":1766666666666 }
// }