|  | @@ -0,0 +1,220 @@
 | 
	
		
			
				|  |  | +package com.xunmei.iot.service.impl;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import cn.hutool.core.date.DateUtil;
 | 
	
		
			
				|  |  | +import cn.hutool.core.util.ObjectUtil;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.enums.iot.DeviceTypeEnum;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.utils.NumberUtils;
 | 
	
		
			
				|  |  | +import com.xunmei.iot.dto.webStatisticBoard.BoardAlarmTrendDto;
 | 
	
		
			
				|  |  | +import com.xunmei.iot.dto.webStatisticBoard.BoardDeviceCountByTypeDto;
 | 
	
		
			
				|  |  | +import com.xunmei.iot.enums.BoardPeriodEnum;
 | 
	
		
			
				|  |  | +import com.xunmei.iot.mapper.IotWebStatisticBoardMapper;
 | 
	
		
			
				|  |  | +import com.xunmei.iot.service.IIotWebStatisticBoardService;
 | 
	
		
			
				|  |  | +import com.xunmei.iot.vo.webStatisticBoard.*;
 | 
	
		
			
				|  |  | +import com.xunmei.system.api.RemoteOrgService;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.text.ParseException;
 | 
	
		
			
				|  |  | +import java.text.SimpleDateFormat;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  | +import java.util.stream.Collectors;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +@Service
 | 
	
		
			
				|  |  | +public class IotWebStatisticBoardServiceImpl implements IIotWebStatisticBoardService {
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    RemoteOrgService orgService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    IotWebStatisticBoardMapper iotWebStatisticBoardMapper;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    final List<String> monitorDeviceTypes = Arrays.asList(DeviceTypeEnum.DVS.getCode(), DeviceTypeEnum.MONITOR_CAMERA.getCode(), DeviceTypeEnum.NUMBER_CAMERA.getCode());
 | 
	
		
			
				|  |  | +    final List<String> fireDeviceTypes = Arrays.asList(DeviceTypeEnum.FSU_Smoke.getCode(), DeviceTypeEnum.FSU_Gas.getCode(), DeviceTypeEnum.FSU_TemperatureAndHumidity.getCode());
 | 
	
		
			
				|  |  | +    final List<String> environmentTypes = Arrays.asList(DeviceTypeEnum.FSU_Theft.getCode(), DeviceTypeEnum.FSU_Infrared.getCode(), DeviceTypeEnum.FSU_DoorMagnetic.getCode(),
 | 
	
		
			
				|  |  | +            DeviceTypeEnum.FSU_RollingShutterDoor.getCode(), DeviceTypeEnum.FSU_Water.getCode(), DeviceTypeEnum.FSU_Ups.getCode(), DeviceTypeEnum.FSU_AirConditioner.getCode());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    final SimpleDateFormat dayFormat = new SimpleDateFormat("M月d");
 | 
	
		
			
				|  |  | +    final SimpleDateFormat monthFormat = new SimpleDateFormat("yy年M月");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public BoardDeviceCountVo deviceCount(Long orgId) {
 | 
	
		
			
				|  |  | +        List<BoardDeviceCountByTypeDto> list = iotWebStatisticBoardMapper.deviceCount(orgId);
 | 
	
		
			
				|  |  | +        Integer monitorCount = list.stream().filter(i -> monitorDeviceTypes.contains(i.getDeviceType())).mapToInt(i -> i.getCount()).sum();
 | 
	
		
			
				|  |  | +        Integer fireCount = list.stream().filter(i -> fireDeviceTypes.contains(i.getDeviceType())).mapToInt(i -> i.getCount()).sum();
 | 
	
		
			
				|  |  | +        Integer environmentCount = list.stream().filter(i -> environmentTypes.contains(i.getDeviceType())).mapToInt(i -> i.getCount()).sum();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        BoardDeviceCountVo vo = BoardDeviceCountVo.builder()
 | 
	
		
			
				|  |  | +                .monitorCount(monitorCount)
 | 
	
		
			
				|  |  | +                .fireCount(fireCount)
 | 
	
		
			
				|  |  | +                .environmentCount(environmentCount)
 | 
	
		
			
				|  |  | +                .build();
 | 
	
		
			
				|  |  | +        return vo;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public BoardProtectionVo protection(Long orgId) {
 | 
	
		
			
				|  |  | +        return iotWebStatisticBoardMapper.protection(orgId);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public List<BoardOnlineByTypeVo> deviceOnline(Long orgId) {
 | 
	
		
			
				|  |  | +        List<BoardOnlineByTypeVo> list = iotWebStatisticBoardMapper.deviceOnline(orgId, Arrays.asList(DeviceTypeEnum.DVS.getCode(),
 | 
	
		
			
				|  |  | +                DeviceTypeEnum.ALARM_HOST.getCode(), DeviceTypeEnum.FSU_Gateway.getCode()));
 | 
	
		
			
				|  |  | +        Set<String> hasDataType = list.stream().map(BoardOnlineByTypeVo::getDeviceType).collect(Collectors.toSet());
 | 
	
		
			
				|  |  | +        list.addAll(initDeviceOnline().stream().filter(id -> !hasDataType.contains(id.getDeviceType())).collect(Collectors.toList()));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        for (BoardOnlineByTypeVo vo : list) {
 | 
	
		
			
				|  |  | +            vo.setOnLineRate(NumberUtils.computeRate(vo.getDeviceCount(), vo.getOnLineCount()));
 | 
	
		
			
				|  |  | +            if (ObjectUtil.isNotEmpty(DeviceTypeEnum.getDescByCode(vo.getDeviceType()))) {
 | 
	
		
			
				|  |  | +                vo.setDeviceTypeName(DeviceTypeEnum.getDescByCode(vo.getDeviceType()));
 | 
	
		
			
				|  |  | +                if (ObjectUtil.equal(vo.getDeviceType(), DeviceTypeEnum.DVS.getCode())) {
 | 
	
		
			
				|  |  | +                    vo.setDeviceTypeName("监控主机");
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +//        Collections.sort(list, Comparator.comparing(BoardOnlineByTypeVo::getOnLineRate));
 | 
	
		
			
				|  |  | +        list.sort(Comparator.comparing(BoardOnlineByTypeVo::getOnLineRate));
 | 
	
		
			
				|  |  | +        Collections.reverse(list);
 | 
	
		
			
				|  |  | +        list.add(BoardOnlineByTypeVo.builder().deviceTypeName("门禁主机").deviceCount(0).onLineCount(0).onLineRate(0f).build());
 | 
	
		
			
				|  |  | +        list.add(BoardOnlineByTypeVo.builder().deviceTypeName("对讲主机").deviceCount(0).onLineCount(0).onLineRate(0f).build());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return list;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private List<BoardOnlineByTypeVo> initDeviceOnline() {
 | 
	
		
			
				|  |  | +        List<BoardOnlineByTypeVo> list = new ArrayList<>();
 | 
	
		
			
				|  |  | +        list.add(BoardOnlineByTypeVo.builder().deviceType(DeviceTypeEnum.DVS.getCode()).deviceTypeName("监控主机").deviceCount(0).onLineCount(0).onLineRate(0f).build());
 | 
	
		
			
				|  |  | +        list.add(BoardOnlineByTypeVo.builder().deviceType(DeviceTypeEnum.ALARM_HOST.getCode()).deviceTypeName(DeviceTypeEnum.ALARM_HOST.getDesc()).deviceCount(0).onLineCount(0).onLineRate(0f).build());
 | 
	
		
			
				|  |  | +        list.add(BoardOnlineByTypeVo.builder().deviceType(DeviceTypeEnum.FSU_Gateway.getCode()).deviceTypeName(DeviceTypeEnum.FSU_Gateway.getDesc()).deviceCount(0).onLineCount(0).onLineRate(0f).build());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return list;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public BoardDeviceHealthVo deviceHealth(Long orgId) {
 | 
	
		
			
				|  |  | +        Date start = DateUtil.beginOfDay(new Date());
 | 
	
		
			
				|  |  | +        Date end = DateUtil.endOfDay(new Date());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        BoardHealthSummaryVo summaryVo = iotWebStatisticBoardMapper.healthSummary(orgId, start, end);
 | 
	
		
			
				|  |  | +        List<BoardHealthRankingVo> rankingVos = iotWebStatisticBoardMapper.healthRanking(orgId, start, end);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        BoardDeviceHealthVo vo = BoardDeviceHealthVo.builder()
 | 
	
		
			
				|  |  | +                .healthSummary(summaryVo)
 | 
	
		
			
				|  |  | +                .healthRanking(rankingVos)
 | 
	
		
			
				|  |  | +                .build();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return vo;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public Collection<BoardAlarmTrendVo> alarmTrend(Long orgId, BoardPeriodEnum period) throws ParseException {
 | 
	
		
			
				|  |  | +        Date earliestDate = getEarliestDate(period);
 | 
	
		
			
				|  |  | +        List<BoardAlarmTrendDto> list = iotWebStatisticBoardMapper.alarmTrend(orgId, earliestDate);
 | 
	
		
			
				|  |  | +        LinkedHashMap<String, BoardAlarmTrendVo> map = getAlarmTrendMap(period);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        for (BoardAlarmTrendDto item : list) {
 | 
	
		
			
				|  |  | +            String xaxisDate = getOutputDateFormat(period, item.getDate());
 | 
	
		
			
				|  |  | +            BoardAlarmTrendVo vo = map.get(xaxisDate);
 | 
	
		
			
				|  |  | +            if (ObjectUtil.isNull(vo)) {
 | 
	
		
			
				|  |  | +                continue;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (monitorDeviceTypes.contains(item.getDeviceType())) {
 | 
	
		
			
				|  |  | +                vo.setMonitorCount(vo.getMonitorCount() + item.getCount());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (fireDeviceTypes.contains(item.getDeviceType())) {
 | 
	
		
			
				|  |  | +                vo.setFireCount(vo.getFireCount() + item.getCount());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (environmentTypes.contains(item.getDeviceType())) {
 | 
	
		
			
				|  |  | +                vo.setEnvironmentCount(vo.getEnvironmentCount() + item.getCount());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<BoardAlarmTrendVo> r = map.values().stream().collect(Collectors.toList());
 | 
	
		
			
				|  |  | +        for (BoardAlarmTrendVo vo : r) {
 | 
	
		
			
				|  |  | +            vo.setTotal(vo.getMonitorCount() + vo.getFireCount() + vo.getEnvironmentCount());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        Collections.reverse(r);
 | 
	
		
			
				|  |  | +        return r;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Date getEarliestDate(BoardPeriodEnum period) {
 | 
	
		
			
				|  |  | +        switch (period) {
 | 
	
		
			
				|  |  | +            case NEAR_7_DAYS:
 | 
	
		
			
				|  |  | +                return DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -6);
 | 
	
		
			
				|  |  | +            case NEAR_7_WEEKS:
 | 
	
		
			
				|  |  | +                return DateUtil.offsetWeek(DateUtil.beginOfWeek(new Date()), -6);
 | 
	
		
			
				|  |  | +            case NEAR_6_MONTHS:
 | 
	
		
			
				|  |  | +                return DateUtil.offsetMonth(DateUtil.beginOfMonth(new Date()), -5);
 | 
	
		
			
				|  |  | +            default:
 | 
	
		
			
				|  |  | +                throw new RuntimeException("无法识别的周期");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private String getOutputDateFormat(BoardPeriodEnum period, String date) throws ParseException {
 | 
	
		
			
				|  |  | +        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 | 
	
		
			
				|  |  | +        Date d = dateFormat.parse(date);
 | 
	
		
			
				|  |  | +        return getOutputDateFormat(period, d);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private String getOutputDateFormat(BoardPeriodEnum period, Date date) {
 | 
	
		
			
				|  |  | +        switch (period) {
 | 
	
		
			
				|  |  | +            case NEAR_7_DAYS:
 | 
	
		
			
				|  |  | +                return dayFormat.format(date);
 | 
	
		
			
				|  |  | +            case NEAR_7_WEEKS:
 | 
	
		
			
				|  |  | +                return dayFormat.format(DateUtil.beginOfWeek(date));
 | 
	
		
			
				|  |  | +            case NEAR_6_MONTHS:
 | 
	
		
			
				|  |  | +                return monthFormat.format(date);
 | 
	
		
			
				|  |  | +            default:
 | 
	
		
			
				|  |  | +                throw new RuntimeException("无法识别的周期");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private LinkedHashMap<String, BoardAlarmTrendVo> getAlarmTrendMap(BoardPeriodEnum period) {
 | 
	
		
			
				|  |  | +        LinkedHashMap<String, BoardAlarmTrendVo> map = new LinkedHashMap<>();
 | 
	
		
			
				|  |  | +        Date date = new Date();
 | 
	
		
			
				|  |  | +        int num = 7;
 | 
	
		
			
				|  |  | +        if (ObjectUtil.equal(period, BoardPeriodEnum.NEAR_6_MONTHS)) {
 | 
	
		
			
				|  |  | +            num = 6;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        for (int i = 0; i < num; i++) {
 | 
	
		
			
				|  |  | +            String key = getOutputDateFormat(period, date);
 | 
	
		
			
				|  |  | +            map.put(key, BoardAlarmTrendVo.builder()
 | 
	
		
			
				|  |  | +                    .date(key)
 | 
	
		
			
				|  |  | +                    .total(0)
 | 
	
		
			
				|  |  | +                    .fireCount(0)
 | 
	
		
			
				|  |  | +                    .monitorCount(0)
 | 
	
		
			
				|  |  | +                    .environmentCount(0)
 | 
	
		
			
				|  |  | +                    .build());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            switch (period) {
 | 
	
		
			
				|  |  | +                case NEAR_7_DAYS:
 | 
	
		
			
				|  |  | +                    date = DateUtil.offsetDay(date, -1);
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                case NEAR_7_WEEKS:
 | 
	
		
			
				|  |  | +                    date = DateUtil.offsetWeek(date, -1);
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +                case NEAR_6_MONTHS:
 | 
	
		
			
				|  |  | +                    date = DateUtil.offsetMonth(date, -1);
 | 
	
		
			
				|  |  | +                    break;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return map;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public BoardAlarmRateVo alarmRate(Long orgId) {
 | 
	
		
			
				|  |  | +        BoardAlarmRateVo vo=iotWebStatisticBoardMapper.alarmRate(orgId);
 | 
	
		
			
				|  |  | +        if(vo==null){
 | 
	
		
			
				|  |  | +            return new BoardAlarmRateVo();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return vo;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public List<BoardAlarmInfoVo> alarmList(Long orgId) {
 | 
	
		
			
				|  |  | +        return iotWebStatisticBoardMapper.alarmList(orgId);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |