fengxiang
2018-07-24 7a52cd14e12e5b1053a26ed5a4799da7a09d4b16
src/main/java/com/moral/controller/DemoConreoller.java
@@ -1,22 +1,28 @@
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;
@@ -32,8 +38,10 @@
   private AlarmDailyMapper alarmDailyMapper;
   @GetMapping("list")
   public List<Map<String, Object>> getDatas(){
      List<Map<String, Object>> list = demoMapper.getDatas();
   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) {
@@ -59,4 +67,35 @@
      });
      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;
   }
}