kaiyu
2020-11-16 cd264145f9b4107f1430da46269dd306d26f3116
src/main/java/com/moral/service/impl/HistoryHourlyServiceImpl.java
@@ -1,10 +1,16 @@
package com.moral.service.impl;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import com.moral.entity.MachineActivate;
import com.moral.mapper.HistoryMapper;
import com.moral.util.AQICalculation;
import com.moral.util.AQICalculation2;
import io.swagger.models.auth.In;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;
@@ -23,6 +29,9 @@
public class HistoryHourlyServiceImpl implements HistoryHourlyService {
    @Resource
    private HistoryHourlyMapper historyHourlyMapper;
    @Resource
    private HistoryMapper historyMapper;
    @Resource
    private DeviceService deviceService;
@@ -146,7 +155,6 @@
                String date2 = simpleDateFormat.format(date1);
                List<Map> list = historyHourlyMapper.getDataByMacAndTime(mac, sensor, perTime, date2);
                List<Map> newList = new ArrayList<Map>();
                //System.out.println(list);
                for (int i = 0; i < 24; i++) {
                    Map map=new HashMap();
                    if (i < 10) {
@@ -159,7 +167,6 @@
                        newList.add(map);
                    }
                }
                //System.out.println(newList);
                if (list.size() <= 24 && list.size() >= 0) {
                    for (int i = 0; i <list.size() ; i++) {
                        String timeS = list.get(i).get("time").toString().substring(11, 13);
@@ -216,6 +223,136 @@
    }
    @Override
    public List<Map> getDataAvbByMIdAndTime(String mId, String startTime, String endTime) {
        List<Device> devicesByMonitorPointId = deviceService.getDevicesByMonitorPointId(Integer.valueOf(mId));
        List<List> lists = new ArrayList<>();
        List<List<Map<String,String>>> listArrayList = new ArrayList<>();
        List<Map<String, Object>> dataAvbByMIdAndTime = historyHourlyMapper.getDataAvbByMIdAndTime(mId, startTime, endTime);
        List<Map> mapList = new ArrayList<>();
        for (Map<String, Object> stringStringMap : dataAvbByMIdAndTime) {
            Map<String,String> map = new HashMap();
            String s = stringStringMap.get("O3").toString();
            Object time = stringStringMap.get("time");
            map.put("O3",s);
            map.put("time",time.toString());
            mapList.add(map);
        }
        if (mapList.size()>=8){
            List<Map<String,String>> mapList1 = new ArrayList<>();
        for (int i = 7; i <=mapList.size()-1 ; i++) {
            if (mapList.get(i).get("O3")!=null){
                List<Integer> numList = new ArrayList();
                Map<String,String> O3_8 = new HashMap<>();
                for (int j =i; j<= mapList.size(); j--) {
                    if (j<0){
                        break;
                    }
                    numList.add(j);
                    if (numList.size()==8){
                        List e15 = new ArrayList();
                        for (int k = 0; k <numList.size() ; k++) {
                            String value = mapList.get(Integer.valueOf(numList.get(k))).get("O3").toString();
                            e15.add(value);
                        }
                        double sum =0;
                        for (Object o : e15) {
                            sum=sum+Double.parseDouble(o.toString());
                        }
                        Double avg1 = sum/e15.size();
                        int timeOfHour = numList.get(0);
                        if (timeOfHour==23){
                            timeOfHour=0;
                        }else {
                            timeOfHour=timeOfHour+1;
                        }
                        O3_8.put("time", String.valueOf(timeOfHour));
                        O3_8.put("O3-8h", String.valueOf(avg1));
                        mapList1.add(O3_8);
                    }else {
                        continue;
                    }
            }
            }
            listArrayList.add(mapList1);
        }
        }
        Double maxO3=0.0;
        if(listArrayList.size()>0){
            List<Map<String, String>> mapList1 = listArrayList.get(0);
            List<Double> O3List = new ArrayList();
            for (Map<String, String> stringStringMap : mapList1) {
                O3List.add(Double.parseDouble(stringStringMap.get("O3-8h")));
            }
            maxO3 = Collections.max(O3List);
            for (int i = 0; i <dataAvbByMIdAndTime.size() ; i++) {
                dataAvbByMIdAndTime.get(i).put("O3-8h",null);
                int time = Integer.parseInt(((Object)dataAvbByMIdAndTime.get(i).get("time")).toString().substring(11,13)) ;
                for (int j = 0; j <mapList1.size() ; j++) {
                    int time1 = Integer.parseInt(mapList1.get(j).get("time").toString()) ;
                    if (time==time1){
                        Double O3_8h = Double.parseDouble(mapList1.get(j).get("O3-8h").toString());
                        dataAvbByMIdAndTime.get(i).put("O3-8h",new BigDecimal(O3_8h).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
                    }
                }
            }
        }
        Map<String, Object> dailyAvgData = historyMapper.getDailyAvgData(mId, startTime);
        if (dailyAvgData!=null){
            dailyAvgData.put("maxO3-8h",new BigDecimal(maxO3).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
            dailyAvgData.put("time",startTime);
        }
        if (dataAvbByMIdAndTime.size()>24){
            for (int i = 0; i <dataAvbByMIdAndTime.size()-1 ; i++) {
                Map<String, Object> map =dataAvbByMIdAndTime.get(i);
                Map<String, Object> map1 = AQICalculation2.hourlyAQI(map);
                dataAvbByMIdAndTime.get(i).put("AQI",Double.parseDouble(map1.get("AQI").toString()));
                if (Double.parseDouble(map1.get("AQI").toString())>=50){
                    String name = map1.get("maxSensor").toString();
                    dataAvbByMIdAndTime.get(i).put("首要污染物",name);
                }else {
                    dataAvbByMIdAndTime.get(i).put("首要污染物","-");
                }
            }
        }else {
            for (int i = 0; i <dataAvbByMIdAndTime.size() ; i++) {
                Map<String, Object> map =dataAvbByMIdAndTime.get(i);
                Map<String, Object> map1 = AQICalculation2.hourlyAQI(map);
                dataAvbByMIdAndTime.get(i).put("AQI",Double.parseDouble(map1.get("AQI").toString()));
                if (Double.parseDouble(map1.get("AQI").toString())>=50){
                    String name = map1.get("maxSensor").toString();
                    dataAvbByMIdAndTime.get(i).put("首要污染物",name);
                }else {
                    dataAvbByMIdAndTime.get(i).put("首要污染物","-");
                }
            }
        }
        Map<String, Object> dayAQI = AQICalculation2.dayAQI(dailyAvgData);
        dailyAvgData.put("首要污染物",dayAQI.get("maxSensor"));
        dailyAvgData.put("AQI",dayAQI.get("AQI"));
        dataAvbByMIdAndTime.add(dataAvbByMIdAndTime.size(),dailyAvgData);
        List<Map> finalList = new ArrayList<>();
        for (int i = 0; i <dataAvbByMIdAndTime.size() ; i++) {
            Map timeAndDate = new HashMap();
            if (i!=dataAvbByMIdAndTime.size()-1){
                String time = dataAvbByMIdAndTime.get(i).get("time").toString().substring(11,13);
                dataAvbByMIdAndTime.get(i).remove("time");
                timeAndDate.put("time",time);
                timeAndDate.put("data",dataAvbByMIdAndTime.get(i));
                finalList.add(timeAndDate);
            }else {
                String time = dataAvbByMIdAndTime.get(i).get("time").toString().substring(0,11);
                dataAvbByMIdAndTime.get(i).remove("time");
                timeAndDate.put("time",time);
                timeAndDate.put("data",dataAvbByMIdAndTime.get(i));
                finalList.add(timeAndDate);
            }
        }
        return finalList;
    }
    @Override
    public Point getDirPoint(Map<String, Object> parameters) throws Exception {
        Map<String, Object> pollutionSourceData = getPollutionSourceDataByHour(parameters);
        if (MapUtils.isEmpty(pollutionSourceData)) {