package com.moral.api.task; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.moral.api.entity.*; import com.moral.api.service.*; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * @program: screen * @description: 提示信息自动生成接口 * @author: lizijie * @create: 2021-10-12 08:53 **/ @Component public class InformationTask { @Autowired private OrganizationService organizationService; @Autowired private GovMonitorPointService govMonitorPointService; @Autowired private HistoryAqiService historyAqiService; @Autowired private DeviceService deviceService; @Autowired private HistoryHourlyService historyHourlyService; @Autowired private CityWeatherService cityWeatherService; @Autowired private InformationService informationService; @XxlJob("informationInsert") public ReturnT informationInsert(String params){ Map organizationIdMap = JSON.parseObject(params); List orgIdList = (List) organizationIdMap.get("orgId"); Calendar nowCalendar = Calendar.getInstance(); Calendar beforeCalendar = Calendar.getInstance(); /* HOUR_OF_DAY 指示一天中的小时 */ nowCalendar.set(Calendar.HOUR_OF_DAY, nowCalendar.get(Calendar.HOUR_OF_DAY) - 1); beforeCalendar.set(Calendar.HOUR_OF_DAY, beforeCalendar.get(Calendar.HOUR_OF_DAY) - 2); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH"); String nowTime = df.format(nowCalendar.getTime())+":00:00"; String beforeTime = df.format(beforeCalendar.getTime())+":00:00"; String realTime = df.format(new Date())+":00:00"; for (Integer orgId:orgIdList) { if (!ObjectUtils.isEmpty(organizationService.getOrganizationById(orgId))){ Organization organization = organizationService.getOrganizationById(orgId); Integer locationLevelCode = organization.getLocationLevelCode(); List govMonitorPoints = govMonitorPointService.getGovMonitorPointByRegionCode(locationLevelCode); Double PM2_5AvgBefore = null; Double PM10AvgBefore = null; Double O3AvgBefore = null; Double PM2_5AvgNow = null; Double PM10AvgNow = null; Double O3AvgNow = null; Double AQIAvgNow = null; Double TVOCAvgNow = null; Double TVOCAvgBefore = null; if (govMonitorPoints.size()>0){ List PM2_5ListBefore = new ArrayList<>(); List PM10ListBefore = new ArrayList<>(); List O3ListBefore = new ArrayList<>(); List PM2_5ListNow = new ArrayList<>(); List PM10ListNow = new ArrayList<>(); List O3ListNow = new ArrayList<>(); List AQIListNow = new ArrayList<>(); for (GovMonitorPoint govMonitorPoint:govMonitorPoints) { String guid = govMonitorPoint.getGuid(); HistoryAqi beforeHistoryAqi = new HistoryAqi(); beforeHistoryAqi = historyAqiService.getHistoryApiByTimeAndGuid(guid,beforeTime); if (!ObjectUtils.isEmpty(beforeHistoryAqi)){ String value = beforeHistoryAqi.getValue(); if (!ObjectUtils.isEmpty(value)){ JSONObject jsonObject = new JSONObject(); jsonObject = JSONObject.parseObject(value); if (!ObjectUtils.isEmpty(jsonObject.get("pm2_5"))){ PM2_5ListBefore.add(Double.parseDouble(jsonObject.get("pm2_5").toString())); } if (!ObjectUtils.isEmpty(jsonObject.get("pm10"))){ PM10ListBefore.add(Double.parseDouble(jsonObject.get("pm10").toString())); } if (!ObjectUtils.isEmpty(jsonObject.get("o3"))){ O3ListBefore.add(Double.parseDouble(jsonObject.get("o3").toString())); } } } HistoryAqi nowHistoryAqi = new HistoryAqi(); nowHistoryAqi = historyAqiService.getHistoryApiByTimeAndGuid(guid,nowTime); if (!ObjectUtils.isEmpty(nowHistoryAqi)){ String value = nowHistoryAqi.getValue(); if (!ObjectUtils.isEmpty(value)){ JSONObject jsonObject = JSONObject.parseObject(value); if (!ObjectUtils.isEmpty(jsonObject.get("pm2_5"))){ PM2_5ListNow.add(Double.parseDouble(jsonObject.get("pm2_5").toString())); } if (!ObjectUtils.isEmpty(jsonObject.get("pm10"))){ PM10ListNow.add(Double.parseDouble(jsonObject.get("pm10").toString())); } if (!ObjectUtils.isEmpty(jsonObject.get("o3"))){ O3ListNow.add(Double.parseDouble(jsonObject.get("o3").toString())); } if (!ObjectUtils.isEmpty(jsonObject.get("aqi"))){ AQIListNow.add(Double.parseDouble(jsonObject.get("aqi").toString())); } } } } if (PM2_5ListBefore.size()>0){ PM2_5AvgBefore = (double)Math.round(PM2_5ListBefore.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (PM10ListBefore.size()>0){ PM10AvgBefore = (double)Math.round(PM10ListBefore.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (O3ListBefore.size()>0){ O3AvgBefore = (double)Math.round(O3ListBefore.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (PM2_5ListNow.size()>0){ PM2_5AvgNow = (double)Math.round(PM2_5ListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (PM10ListNow.size()>0){ PM10AvgNow = (double)Math.round(PM10ListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (O3ListNow.size()>0){ O3AvgNow = (double)Math.round(O3ListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (AQIListNow.size()>0){ AQIAvgNow = (double)Math.round(AQIListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } List devices = new ArrayList<>(); devices = deviceService.getDateByOrgId(orgId); List TVOCListNow = new ArrayList<>(); List TVOCListBefore = new ArrayList<>(); SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { for (Device device:devices) { String mac = device.getMac(); List historyHourliesNow = new ArrayList<>(); historyHourliesNow = historyHourlyService.getValueByMacAndTime(mac, df1.parse(nowTime), df1.parse(df.format(nowCalendar.getTime()) + ":00:01")); if (!ObjectUtils.isEmpty(historyHourliesNow) && historyHourliesNow.size()>0){ String value = historyHourliesNow.get(0).getValue(); JSONObject jsonObject = JSONObject.parseObject(value); TVOCListNow.add(Double.parseDouble(jsonObject.get("a99054").toString())); } List historyHourliesBefore = new ArrayList<>(); historyHourliesBefore = historyHourlyService.getValueByMacAndTime("p5dnd7a0392252", df1.parse(beforeTime), df1.parse(df.format(beforeCalendar.getTime()) + ":00:01")); if (!ObjectUtils.isEmpty(historyHourliesBefore) && historyHourliesBefore.size()>0){ String value = historyHourliesBefore.get(0).getValue(); JSONObject jsonObject = JSONObject.parseObject(value); TVOCListBefore.add(Double.parseDouble(jsonObject.get("a99054").toString())); } } }catch (ParseException e){ e.printStackTrace(); } if (TVOCListNow.size()>0){ TVOCAvgNow = (double)Math.round(TVOCListNow.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } if (TVOCListBefore.size()>0){ TVOCAvgBefore = (double)Math.round(TVOCListBefore.stream().mapToDouble(aDouble -> aDouble).summaryStatistics().getAverage()); } } String temp = ""; String humidity = ""; String windDir = ""; String windScale = ""; CityWeather cityWeather = new CityWeather(); cityWeather = cityWeatherService.getDataByCityCodeAndTime(locationLevelCode.toString(),nowTime); if (!ObjectUtils.isEmpty(cityWeather)){ String value = cityWeather.getValue(); if (!ObjectUtils.isEmpty(value)){ JSONObject jsonObject = JSONObject.parseObject(value); if (!ObjectUtils.isEmpty(jsonObject.get("temp"))){ temp = jsonObject.getString("temp"); } if (!ObjectUtils.isEmpty(jsonObject.get("humidity"))){ humidity = jsonObject.getString("humidity"); } if (!ObjectUtils.isEmpty(jsonObject.get("windDir"))){ windDir = jsonObject.getString("windDir"); } if (!ObjectUtils.isEmpty(jsonObject.get("windScale "))){ windScale = jsonObject.getString("windScale"); } } } Information information = new Information(); JSONObject jsonObject = new JSONObject(); String realHour = realTime.substring(11,13); String nowHour = nowTime.substring(11,13); String beforeHour = beforeTime.substring(11,13); String title = "【"+realHour+"时数据提醒】"; String airInfo = nowHour+"时,"; String AQILevel = ""; if (!ObjectUtils.isEmpty(AQIAvgNow)){ airInfo = airInfo + "我市AQI:"+AQIAvgNow+",等级:"; if (AQIAvgNow<50 || AQIAvgNow==50){ AQILevel = "优"; }else if (AQIAvgNow<100 || AQIAvgNow==100){ AQILevel = "良好"; }else if (AQIAvgNow<200 || AQIAvgNow==200){ AQILevel = "轻度污染"; }else if (AQIAvgNow<300 || AQIAvgNow==300){ AQILevel = "中度污染"; }else{ AQILevel = "重度污染"; } }else { airInfo = airInfo + "我市AQI:"+",等级:"; } airInfo = airInfo+AQILevel+"。"; String PM10Info = ""; PM10Info = PM10Info+"PM10:"+PM10AvgNow+"微克/立方米,"; if (!ObjectUtils.isEmpty(PM10AvgBefore) && !ObjectUtils.isEmpty(PM10AvgNow)){ String upOrDown = (PM10AvgBefore