xufenglei
2018-07-31 6b9938b5faf4a1b66f1c8886adc402d42bd447c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.moral.controller;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.moral.common.util.WebUtils;
import com.moral.mapper.AlarmDailyMapper;
import com.moral.mapper.DemoMapper;
 
 
@RestController
@RequestMapping("demo")
public class DemoConreoller {
 
    @Resource
    private DemoMapper demoMapper;
 
    @Resource
    private AlarmDailyMapper alarmDailyMapper;
 
    @GetMapping("list")
    public List<Map<String, Object>> getDatas(HttpServletRequest request){
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        parameters.put("size", Integer.valueOf(parameters.get("size").toString()));
        List<Map<String, Object>> list = demoMapper.getDatas(parameters);
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return ((Date)o1.get("time")).compareTo((Date)o2.get("time"));
            }
        });
        return list;
    }
 
    @GetMapping("pollution")
    public List<Map<String, Object>> getPollutionDatas(HttpServletRequest request){
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        Object sensorKey = parameters.get("sensorKey");
        parameters.put("sensorKeys", Arrays.asList(sensorKey));
        parameters.put("start", LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()));
        parameters.put("end", LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth()));
        List<Map<String, Object>> alarmData = alarmDailyMapper.getAlarmData(parameters);
        Collections.sort(alarmData, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return ((BigDecimal) o2.get(sensorKey)).compareTo((BigDecimal) o1.get(sensorKey));
            }
        });
        return alarmData;
    }
    
    @GetMapping("compare")
    public List<Map<String, Object>> getCompareDatas(HttpServletRequest request){
        List<Map<String, Object>> thisMonth = getDatas(request);
        List<Date> timeList = new ArrayList<Date>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd日HH时");
        for (Map<String, Object> map : thisMonth) {
            timeList.add(DateUtils.addMonths((Date)map.get("time"), -1));
            JSONObject json = JSON.parseObject(map.remove("aqi_json").toString());
            map.put("this_month_PM2_5",json.get("PM2_5"));
            map.put("this_month_PM10",json.get("PM10"));
            map.put("format_time", simpleDateFormat.format((Date)map.get("time")));
        }
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("timeList", timeList);
        List<Map<String, Object>> lastMonth = demoMapper.getDatas(parameters);
        for (Map<String, Object> map : thisMonth) {
            String formatTime = map.get("format_time").toString();
            for (Map<String, Object> last : lastMonth) {
                if (formatTime.equals(simpleDateFormat.format((Date)last.get("time")))) {
                    JSONObject json = JSON.parseObject(last.remove("aqi_json").toString());
                    map.put("last_month_PM2_5",json.get("PM2_5"));
                    map.put("last_month_PM10",json.get("PM10"));
                    lastMonth.remove(last);
                    break;
                }
            }
        }
        return thisMonth;
    }
 
    /**
     *
     * 功能描述: 
     *
     * @param: startTime 开始时间
     * @param: endTime 结束时间
     * @return:
     * @author: fengxiang
     * @date: 2018/7/30 11:27
     */
    @GetMapping("get-dayaqis")
    public List<Map<String, Object>> getDayAqis(Date startTime,Date endTime) {
        return  demoMapper.selectByTimeZones(startTime,endTime);
    }
}