package com.whyc.service; import com.whyc.constant.BattAlarmIdEnum; import com.whyc.dto.NameValueIntDto; import com.whyc.dto.Response; import com.whyc.factory.ThreadPoolExecutorFactory; import com.whyc.pojo.db_alarm.BattalarmData; import com.whyc.pojo.db_ram_db.DeviceState; import com.whyc.pojo.db_station.BattInf; import com.whyc.pojo.db_station.StationInf; import com.whyc.pojo.db_user.BaojigroupPower; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadPoolExecutor; import java.util.stream.Collectors; @Service public class MonitorService { @Autowired private LeaderHomeService leaderHomeService; @Autowired private StationInfService stationInfService; @Autowired private PowerInfService powerInfService; @Autowired private BattInfService battInfService; @Autowired private PwrdevAlarmService powerAlarmService; @Autowired private DevalarmDataService devAlarmDataService; @Autowired private BattalarmDataService battAlarmDataService; @Autowired private DeviceStateService deviceStateService; @Autowired private BaoJiGroupPowerService baoJiGroupPowerService; public Response getAll(Integer userId) { Response response = new Response(); Map map = new HashMap<>(); CountDownLatch latch = new CountDownLatch(9); ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor(); //TODO 交流告警统计 待明确告警分类 poolExecutor.execute(() -> { //acAlarmStatistics(userId, map); latch.countDown(); }); //资产信息统计 poolExecutor.execute(() -> { assetsInfoStatistics(userId, map); latch.countDown(); }); //蓄电池核容信息统计 poolExecutor.execute(() -> { leaderHomeService.testDataInfoStatistics(userId, map); latch.countDown(); }); //电池品牌信息统计 poolExecutor.execute(() -> { leaderHomeService.batteryInfoStatistics(userId, map); latch.countDown(); }); //电源品牌信息统计 poolExecutor.execute(() -> { leaderHomeService.powerInfoStatistics(userId, map); latch.countDown(); }); //TODO 电源告警信息统计 待明确告警分类 poolExecutor.execute(() -> { //powerAlarmStatistics(userId, map); latch.countDown(); }); //单节蓄电池告警统计 poolExecutor.execute(() -> { battMonAlarmStatistics(userId, map); latch.countDown(); }); //设备实时状态统计 poolExecutor.execute(() -> { deviceStatusStatistics(userId, map); latch.countDown(); }); //班组统计 及 告警统计 poolExecutor.execute(() -> { baoJiAndAlarmStatistics(userId, map); latch.countDown(); }); return response; } private void baoJiAndAlarmStatistics(Integer userId, Map map) { List baoJiGroupStationDtoList = new ArrayList<>(); //获取管理的所有班组和站点 List baoJiGroupStationList =baoJiGroupPowerService.getListByUserId(userId); Map> baoJiGroupStationMap = baoJiGroupStationList.stream().collect(Collectors.groupingBy(BaojigroupPower::getBaoJiGroupName)); baoJiGroupStationMap.forEach((baoJiGroupName, stationList) -> { NameValueIntDto baoJiGroupStationDto = new NameValueIntDto(); baoJiGroupStationDto.setName(baoJiGroupName); baoJiGroupStationDto.setValue(stationList.size()); baoJiGroupStationDtoList.add(baoJiGroupStationDto); }); map.put("baoJiStationStatistics", baoJiGroupStationDtoList); List alarmStatisticsList = new ArrayList<>(); long powerAlarmCount = powerAlarmService.getListByUserId(userId, 1).stream().count(); long devAlarmCount = devAlarmDataService.getListByUserId(userId, 1).stream().count(); long battAlarmCount = battAlarmDataService.getListByUserId(userId, 1).stream().count(); NameValueIntDto powerAlarmDto = new NameValueIntDto("电源监测", (int) powerAlarmCount); alarmStatisticsList.add(powerAlarmDto); NameValueIntDto devAlarmDto = new NameValueIntDto("设备监测", (int) devAlarmCount); alarmStatisticsList.add(devAlarmDto); NameValueIntDto battAlarmDto = new NameValueIntDto("电池监测", (int) battAlarmCount); alarmStatisticsList.add(battAlarmDto); map.put("alarmStatistics", alarmStatisticsList); } private void deviceStatusStatistics(Integer userId, Map map) { List deviceStateList =deviceStateService.getListByUserId(userId); long chargeCount = deviceStateList.stream().filter(deviceState -> deviceState.getDevWorkstate() == 0).count(); long hrChargeCount = deviceStateList.stream().filter(deviceState -> deviceState.getDevWorkstate() == 1).count(); long hrDischargeCount = deviceStateList.stream().filter(deviceState -> deviceState.getDevWorkstate() == 2).count(); long powerOffDischargeCount = deviceStateList.stream().filter(deviceState -> deviceState.getDevWorkstate() == 3).count(); long resTestCount = deviceStateList.stream().filter(deviceState -> deviceState.getDevWorkstate() == 4).count(); List nameValueList = new ArrayList<>(); NameValueIntDto chargeCountDto = new NameValueIntDto("直连充电", (int) chargeCount); nameValueList.add(chargeCountDto); NameValueIntDto hrChargeCountDto = new NameValueIntDto("核容充电", (int) hrChargeCount); nameValueList.add(hrChargeCountDto); NameValueIntDto hrDischargeCountDto = new NameValueIntDto("核容放电", (int) hrDischargeCount); nameValueList.add(hrDischargeCountDto); NameValueIntDto powerOffDischargeCountDto = new NameValueIntDto("停电放电", (int) powerOffDischargeCount); nameValueList.add(powerOffDischargeCountDto); NameValueIntDto resTestCountDto = new NameValueIntDto("内阻测试", (int) resTestCount); nameValueList.add(resTestCountDto); map.put("deviceStatusStatistics", nameValueList); } private void battMonAlarmStatistics(Integer userId, Map map) { List nameValueList = new ArrayList<>(); List battAlarmDataList = battAlarmDataService.getMonAlarm(userId); //获取电压异常告警 List monVolAlarmList = battAlarmDataList.stream() .filter(alarm -> alarm.getAlmId().intValue() == BattAlarmIdEnum.ALARM_119005.getStateId()).collect(Collectors.toList()); Map> monVolAlarmMap = monVolAlarmList.stream().collect( Collectors.groupingBy(alarm -> alarm.getBattgroupId() + "_" + alarm.getMonNum())); nameValueList.add(new NameValueIntDto("电压", monVolAlarmMap.size())); //获取内阻异常告警 List monResAlarmList = battAlarmDataList.stream() .filter(alarm -> alarm.getAlmId().intValue() == BattAlarmIdEnum.ALARM_119007.getStateId()).collect(Collectors.toList()); Map> monResAlarmMap = monResAlarmList.stream().collect( Collectors.groupingBy(alarm -> alarm.getBattgroupId() + "_" + alarm.getMonNum())); nameValueList.add(new NameValueIntDto("内阻", monResAlarmMap.size())); //获取温度异常告警 List monTmpAlarmList = battAlarmDataList.stream() .filter(alarm -> alarm.getAlmId().intValue() == BattAlarmIdEnum.ALARM_119006.getStateId()).collect(Collectors.toList()); Map> monTmpAlarmMap = monTmpAlarmList.stream().collect( Collectors.groupingBy(alarm -> alarm.getBattgroupId() + "_" + alarm.getMonNum())); nameValueList.add(new NameValueIntDto("温度", monTmpAlarmMap.size())); map.put("battMonAlarmStatistics", nameValueList); } private void assetsInfoStatistics(Integer userId, Map map) { List nameValueList = new ArrayList<>(); List stationInfList =stationInfService.getListByUserId(userId); int stationCount = stationInfList.size(); NameValueIntDto stationCountDto = new NameValueIntDto(); stationCountDto.setName("站点数量"); stationCountDto.setValue(stationCount); int powerCount = powerInfService.getListByUserId(userId).size(); NameValueIntDto powerCountDto = new NameValueIntDto(); powerCountDto.setName("电源数量"); powerCountDto.setValue(powerCount); List battInfList = battInfService.getListByUserId(userId); int battCount = battInfList.size(); Map> devIdMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getDevId)); NameValueIntDto devCountDto = new NameValueIntDto(); devCountDto.setName("设备数量"); devCountDto.setValue(devIdMap.size()); NameValueIntDto battCountDto = new NameValueIntDto(); battCountDto.setName("电池数量"); battCountDto.setValue(battCount); nameValueList.add(stationCountDto); nameValueList.add(powerCountDto); nameValueList.add(devCountDto); nameValueList.add(battCountDto); map.put("assetsInfoStatistics", nameValueList); } }