From 8725f79377d84d314f4906c40d081d8997dd9a6e Mon Sep 17 00:00:00 2001
From: lizijie <lzjiiie@163.com>
Date: Tue, 15 Mar 2022 15:37:01 +0800
Subject: [PATCH] 环比数据接口页数功能修改

---
 screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java |  279 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 266 insertions(+), 13 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
index be28219..6f08c33 100644
--- a/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
+++ b/screen-api/src/main/java/com/moral/api/service/impl/CityAqiServiceImpl.java
@@ -3,27 +3,18 @@
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.moral.api.config.properties.SpecialCitiesProperties;
-import com.moral.api.entity.CityAqi;
-import com.moral.api.entity.CityAqiDaily;
-import com.moral.api.entity.CityAqiMonthly;
-import com.moral.api.entity.CityAqiYearly;
-import com.moral.api.entity.Forecast;
-import com.moral.api.entity.Organization;
-import com.moral.api.entity.SysArea;
+import com.moral.api.entity.*;
 import com.moral.api.mapper.CityAqiMapper;
+import com.moral.api.mapper.DeviceMapper;
 import com.moral.api.mapper.ForecastMapper;
 import com.moral.api.pojo.dto.cityAQI.CityPollutionLevel;
 import com.moral.api.pojo.dto.cityAQI.ConcentrationAndPercent;
 import com.moral.api.pojo.form.aqi.AirQualityComparisonForm;
 import com.moral.api.pojo.vo.cityAQI.AirQualityComparisonVO;
-import com.moral.api.service.CityAqiDailyService;
-import com.moral.api.service.CityAqiMonthlyService;
-import com.moral.api.service.CityAqiService;
+import com.moral.api.service.*;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.moral.api.service.CityAqiYearlyService;
-import com.moral.api.service.OrganizationService;
-import com.moral.api.service.SysAreaService;
 import com.moral.constant.Constants;
 import com.moral.constant.RedisConstants;
 import com.moral.pojo.AQI;
@@ -40,6 +31,7 @@
 import org.springframework.util.ObjectUtils;
 
 import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.DoubleStream;
@@ -78,6 +70,18 @@
 
     @Autowired
     private SpecialCitiesProperties specialCitiesProperties;
+
+    @Autowired
+    private CityWeatherService cityWeatherService;
+
+    @Autowired
+    private OrganizationService organizationService;
+
+    @Autowired
+    private DeviceMapper deviceMapper;
+
+    @Autowired
+    private HistoryHourlyService historyHourlyService;
 
     @Override
     public List<Map<String, Object>> measuredCompareForecastOfO3(Map<String, Object> params) {
@@ -1001,6 +1005,242 @@
         return vos;
     }
 
+    //������������
+    @Override
+    public Map<String, Object> momData(Integer regionCode, Integer size, Integer current, Integer orgId) {
+        Map<String,Object> result = new HashMap<>();
+        Page<CityAqi> page = new Page<>(current, size);
+        Page<CityAqi> pageNext = new Page<>(current+1, size);
+        QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>();
+        cityAqiQueryWrapper.eq("city_code",regionCode);
+        Integer count = cityAqiMapper.selectCount(cityAqiQueryWrapper);
+        if (count==0){
+            return result;
+        }
+        cityAqiQueryWrapper.orderByDesc("time");
+        Page<CityAqi> resultPage = cityAqiMapper.selectPage(page, cityAqiQueryWrapper);
+        List<CityAqi> cityAqiList = resultPage.getRecords();
+        Page<CityAqi> resultPageNext = cityAqiMapper.selectPage(pageNext, cityAqiQueryWrapper);
+        List<CityAqi> cityAqiListNext = resultPageNext.getRecords();
+        List<Map<String,Object>> resultList = new ArrayList<>();
+        if (cityAqiList.size()==0){
+            return result;
+        }
+        for (int i=0; i<cityAqiList.size(); i++) {
+            Map<String,Object> resultMap = new HashMap<>();
+            String hour = cityAqiList.get(i).getTime().toString().substring(11,13);
+            String previousHour = null;
+            Date previousTtime = null;
+            Double PM2_5 = null;
+            Double PM10 = null;
+            Double O3 = null;
+            Integer AQI = null;
+            String AQIIndex = null;
+            Double TVOC = null;
+            Double previousPM2_5 = null;
+            Double previousPM10 = null;
+            Double previousO3 = null;
+            String PM2_5_change = null;
+            String PM10_change = null;
+            String O3_change = null;
+            String value = cityAqiList.get(i).getValue();
+            if (!ObjectUtils.isEmpty(value)){
+                JSONObject jsonObject = JSONObject.parseObject(value);
+                if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))){
+                    PM2_5 = Double.parseDouble(jsonObject.get("PM2_5").toString());
+                }
+                if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))){
+                    PM10 = Double.parseDouble(jsonObject.get("PM10").toString());
+                }
+                if (!ObjectUtils.isEmpty(jsonObject.get("O3"))){
+                    O3 = Double.parseDouble(jsonObject.get("O3").toString());
+                }
+                if (!ObjectUtils.isEmpty(jsonObject.get("AQI"))){
+                    AQI = Integer.parseInt(jsonObject.get("AQI").toString());
+                    AQIIndex = AQIUtils.classCodeOfPollutionByAqi(AQI);
+                }
+            }
+            if (i<cityAqiList.size()-1){
+                previousHour = cityAqiList.get(i+1).getTime().toString().substring(11,13);
+                previousTtime = cityAqiList.get(i+1).getTime();
+                String previousValue = cityAqiList.get(i+1).getValue();
+                if (!ObjectUtils.isEmpty(previousValue)){
+                    JSONObject jsonObject = JSONObject.parseObject(previousValue);
+                    if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))){
+                        previousPM2_5 = Double.parseDouble(jsonObject.get("PM2_5").toString());
+                    }
+                    if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))){
+                        previousPM10 = Double.parseDouble(jsonObject.get("PM10").toString());
+                    }
+                    if (!ObjectUtils.isEmpty(jsonObject.get("O3"))){
+                        previousO3 = Double.parseDouble(jsonObject.get("O3").toString());
+                    }
+                }
+            }else if (cityAqiListNext.size()>0){
+                previousHour = cityAqiListNext.get(0).getTime().toString().substring(11,13);
+                previousTtime = cityAqiListNext.get(0).getTime();
+                String previousValue = cityAqiListNext.get(0).getValue();
+                if (!ObjectUtils.isEmpty(previousValue)){
+                    JSONObject jsonObject = JSONObject.parseObject(previousValue);
+                    if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))){
+                        previousPM2_5 = Double.parseDouble(jsonObject.get("PM2_5").toString());
+                    }
+                    if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))){
+                        previousPM10 = Double.parseDouble(jsonObject.get("PM10").toString());
+                    }
+                    if (!ObjectUtils.isEmpty(jsonObject.get("O3"))){
+                        previousO3 = Double.parseDouble(jsonObject.get("O3").toString());
+                    }
+                }
+            }else {
+                continue;
+            }
+            if (!ObjectUtils.isEmpty(PM2_5) && !ObjectUtils.isEmpty(previousPM2_5)){
+                Double difference_PM2_5 = PM2_5-previousPM2_5;
+                if (difference_PM2_5==0){
+                    PM2_5_change = "������";
+                }
+                if (difference_PM2_5>0){
+                    PM2_5_change = "������"+difference_PM2_5;
+                }
+                if (difference_PM2_5<0){
+                    PM2_5_change = "������"+Math.abs(difference_PM2_5);
+                }
+            }
+            if (!ObjectUtils.isEmpty(PM10) && !ObjectUtils.isEmpty(previousPM10)){
+                Double difference_PM10 = PM10-previousPM10;
+                if (difference_PM10==0){
+                    PM10_change = "������";
+                }
+                if (difference_PM10>0){
+                    PM10_change = "������"+difference_PM10;
+                }
+                if (difference_PM10<0){
+                    PM10_change = "������"+Math.abs(difference_PM10);
+                }
+            }
+            if (!ObjectUtils.isEmpty(O3) && !ObjectUtils.isEmpty(previousO3)){
+                Double difference_O3 = O3-previousO3;
+                if (difference_O3==0){
+                    O3_change = "������";
+                }
+                if (difference_O3>0){
+                    O3_change = "������"+difference_O3;
+                }
+                if (difference_O3<0){
+                    O3_change = "������"+Math.abs(difference_O3);
+                }
+            }
+            //���������������������������������id
+            List<Integer> allOrgId = new ArrayList<>();
+            allOrgId.add(orgId);
+            //������������
+            //���������������
+            List<Organization> allChildrenOrganization = organizationService.getChildrenOrganizationsById(orgId);
+            if (!ObjectUtils.isEmpty(allChildrenOrganization) || allChildrenOrganization.size() < 1){
+                for (Organization organization:allChildrenOrganization) {
+                    allOrgId.add(organization.getId());
+                }
+            }
+            //������������
+            List<Integer> allOrgIdWithoutDuplicates = allOrgId.stream().distinct().collect(Collectors.toList());
+            //������������list���������������Mac
+            List<String> deviceMacList = new ArrayList<>();
+            for (Integer orgIdWithoutDuplicates:allOrgIdWithoutDuplicates) {
+                //������id������������������
+                QueryWrapper<Device> wrapper_device = new QueryWrapper<>();
+                wrapper_device.eq("is_delete",Constants.NOT_DELETE).eq("organization_id",orgIdWithoutDuplicates);
+                List<Device> devices = new ArrayList<>();
+                devices = deviceMapper.selectList(wrapper_device);
+                if (devices.size()>0){
+                    for (Device device:devices) {
+                        String deviceMac = device.getMac();
+                        deviceMacList.add(deviceMac);
+                    }
+                }else {
+                    continue;
+                }
+            }
+            //������������
+            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            String time = df.format(cityAqiList.get(i).getTime());
+            if (deviceMacList.size()>0){
+                List<HistoryHourly> valueByMacs = historyHourlyService.getValueByMacs(deviceMacList, time);
+                if (valueByMacs.size()>0){
+                    for (HistoryHourly historyHourly:valueByMacs) {
+                        String value_historyHourly = historyHourly.getValue();
+                        if (!ObjectUtils.isEmpty(value_historyHourly)){
+                            JSONObject jsonObject = JSONObject.parseObject(value_historyHourly);
+                            if (!ObjectUtils.isEmpty(jsonObject.get("a99054"))){
+                                TVOC = Double.parseDouble(jsonObject.get("a99054").toString());
+                            }
+                        }
+                    }
+                }
+            }
+            resultMap.put("time",time.substring(0,13));
+            resultMap.put("title","���"+hour+"������������������");
+            String PM2_5_string = subZeroAndDot(PM2_5.toString());
+            String PM10_string = subZeroAndDot(PM10.toString());
+            String O3_string = subZeroAndDot(O3.toString());
+            String PM2_5_change_string = subZeroAndDot(PM2_5_change.toString());
+            String PM10_change_string = subZeroAndDot(PM10_change.toString());
+            String O3_change_string = subZeroAndDot(O3_change.toString());
+            resultMap.put("info",hour+"������������AQI���"+AQI+"������������"+AQIIndex+"������"+previousHour+"������������PM10���"+PM10_string+"������/������������"+PM10_change_string+"���PM2.5���"+PM2_5_string+"������/������������"+PM2_5_change_string+"���TVOC���������"+String.format("%.2f", TVOC)+"������/������������O3���"+O3_string+"������/������������"+O3_change_string+"���");
+            QueryWrapper<CityWeather> cityWeatherQueryWrapper = new QueryWrapper<>();
+            cityWeatherQueryWrapper.eq("city_code",regionCode);
+            cityWeatherQueryWrapper.eq("time",cityAqiList.get(i).getTime());
+            CityWeather cityWeather = cityWeatherService.getOne(cityWeatherQueryWrapper);
+            Integer temp = null;
+            Integer humidity = null;
+            String windDir = "";
+            Integer windScale = null;
+            if (!ObjectUtils.isEmpty(cityWeather)){
+                String cityWeatherValue = cityWeather.getValue();
+                if (!ObjectUtils.isEmpty(cityWeatherValue)){
+                    JSONObject jsonObject = JSONObject.parseObject(cityWeatherValue);
+                    if (!ObjectUtils.isEmpty(jsonObject.get("temp"))){
+                        temp = Integer.parseInt(jsonObject.get("temp").toString());
+                    }
+                    if (!ObjectUtils.isEmpty(jsonObject.get("humidity"))){
+                        humidity = Integer.parseInt(jsonObject.get("humidity").toString());
+                    }
+                    if (!ObjectUtils.isEmpty(jsonObject.get("windDir"))){
+                        windDir = jsonObject.get("windDir").toString();
+                    }
+                    if (!ObjectUtils.isEmpty(jsonObject.get("windScale"))){
+                        windScale = Integer.parseInt(jsonObject.get("windScale").toString());
+                    }
+                }
+            }
+            String temp_string = "";
+            if (temp!=null){
+                temp_string = temp.toString();
+            }
+            String humidity_string = "";
+            if (humidity!=null){
+                humidity_string = humidity.toString();
+            }
+            String windScale_string = "";
+            if (windScale!=null){
+                windScale_string = windScale.toString();
+            }
+            resultMap.put("weatherCondition","���������������������������"+temp_string+"������������"+humidity_string+"%,"+windDir+windScale_string+"������");
+            resultMap.put("auther","���������������������������");
+            resultList.add(resultMap);
+        }
+        Integer totel = count-1;
+        result.put("totel",totel);
+        result.put("resultList",resultList);
+        result.put("current",current);
+        int totalPageNumber = totel/size;
+        if(totel%size != 0){
+            totalPageNumber += 1;
+        }
+        result.put("totalPageNumber",totalPageNumber);
+        return result;
+    }
+
     /**
      * @Description: ������6������������������������������������
      * @Param: [data, comparisonData]
@@ -1208,4 +1448,17 @@
         Double avg = MathUtils.division(sum, num, 2);
         return avg;
     }
+
+    /**
+     * ������java������������������������������.���0
+     * @param s
+     * @return
+     */
+    public static String subZeroAndDot(String s){
+        if(s.indexOf(".") > 0){
+            s = s.replaceAll("0+?$", "");//���������������0
+            s = s.replaceAll("[.]$", "");//������������������.���������
+        }
+        return s;
+    }
 }

--
Gitblit v1.8.0