lizijie
2021-11-24 c8d4813258b1fd1e27b109db5f4c3c89a890400c
screen-api/src/main/java/com/moral/api/service/impl/CityAqiDailyServiceImpl.java
@@ -1,6 +1,7 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityAqiDaily;
import com.moral.api.mapper.CityAqiDailyMapper;
@@ -185,5 +186,90 @@
        return monthlyPollutionLevels;
    }
    @Override
    public Map<String, Map<String,Object>> oneYearsData(Map map) {
        int city_code = Integer.parseInt(map.get("cityCode").toString());
        String year = map.get("year").toString();
        String startTime = year+"-01-01 00:00:00";
        String endTime = year+"-12-31 23:59:59";
        QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>();
        cityAqiDailyQueryWrapper.eq("city_code", city_code);
        cityAqiDailyQueryWrapper.between("time",startTime,endTime);
        List<CityAqiDaily> cityAqiDailies = cityAqiDailyMapper.selectList(cityAqiDailyQueryWrapper);
        Map<String,Map<String,Object>> resultMap = new HashMap<>();
        for (int i=1;i<13;i++){
            Map<String, Object> monthMap = new HashMap<>();
            String month = null;
            if (i<10){
                month = "0"+i;
            }else {
                month = String.valueOf(i);
            }
            String monthDate = year+"-"+month;
            String monthFirstDay = year+"-"+month+"-01";
            int monthDay = DateUtils.getMonthDay(monthFirstDay);
            for (int j=1;j<monthDay+1;j++){
                String day = null;
                if (j<10){
                    day = "0"+j;
                }else {
                    day = String.valueOf(j);
                }
                String dayDate = year+"-"+month+"-"+day;
                monthMap.put(dayDate,null);
            }
            resultMap.put(monthDate,monthMap);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (CityAqiDaily cityAqiDaily:cityAqiDailies) {
            String time = sdf.format(cityAqiDaily.getTime());
            String monthDate = time.substring(0,7);
            String dayDate = time.substring(0,10);
            Double PM2_5 = null;
            Double PM10 = null;
            Double SO2 = null;
            Double NO2 = null;
            Double CO = null;
            Double O3 = null;
            String primaryPollutant = null;
            String value = cityAqiDaily.getValue();
            JSONObject jsonObject = new 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("SO2"))){
                SO2 = (Double.parseDouble(jsonObject.get("SO2").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("NO2"))){
                NO2 = (Double.parseDouble(jsonObject.get("NO2").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("CO"))){
                CO = (Double.parseDouble(jsonObject.get("CO").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("O3"))){
                O3 = (Double.parseDouble(jsonObject.get("O3").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("primaryPollutant"))){
                primaryPollutant = jsonObject.get("primaryPollutant").toString();
            }
            Map<String,Object> valueMap = new HashMap<>();
            valueMap.put("PM2_5",PM2_5);
            valueMap.put("PM10",PM10);
            valueMap.put("SO2",SO2);
            valueMap.put("NO2",NO2);
            valueMap.put("CO",CO);
            valueMap.put("O3",O3);
            valueMap.put("primaryPollutant",primaryPollutant);
            Map<String,Object> monthMap = new HashMap<>();
            monthMap = resultMap.get(monthDate);
            monthMap.put(dayDate,valueMap);
            resultMap.put(monthDate,monthMap);
        }
        return resultMap;
    }
}