瀏覽代碼

前端接口优化

xushili 2 年之前
父節點
當前提交
ab1353b87d

+ 15 - 29
gyee-sample-impala/src/main/java/com/gyee/impala/common/util/DateUtil.java

@@ -344,38 +344,12 @@ public class DateUtil extends DateUtils {
     }
 
     /**
-     * 去年同期
-     * @return
-     */
-    public static Date str2QntqDateTime(String time,String pattern) {
-
-        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
-        Date dateTime = null;
-        try {
-            dateTime = sdf.parse(time);
-        } catch (ParseException e) {
-            e.printStackTrace();
-
-        }
-        Calendar calendar = Calendar.getInstance();
-        calendar.setTime(dateTime);
-        calendar.add(Calendar.YEAR,-1);
-        return calendar.getTime();
-    }
-
-    /**
-     * 去年同期
+     * 加减年份
      */
-    public static Date str2QntqDateTime(String time) {
-        return str2QntqDateTime(time,DATE_TIME_PATTERN);
-    }
-    /**
-     * 加一年
-     */
-    public static Date dateTimeAddYear(Date date) {
+    public static Date dateTimeAddYear(Date date,int k) {
         Calendar instance = Calendar.getInstance();
         instance.setTime(date);
-        instance.add(Calendar.YEAR,1);
+        instance.add(Calendar.YEAR,k);
         return instance.getTime();
     }
 
@@ -520,4 +494,16 @@ public class DateUtil extends DateUtils {
         System.out.println(getPreviousDate(3 * 360 * 24));
         System.out.println(covertDateTimestamp("2021-12-30 00:00:00"));
     }
+
+    /**
+     * @return 当天零点
+     */
+    public static Date today0am() {
+        Calendar day = Calendar.getInstance();
+        day.set(Calendar.HOUR_OF_DAY, 0);
+        day.set(Calendar.MINUTE, 0);
+        day.set(Calendar.SECOND, 0);
+        day.set(Calendar.MILLISECOND, 0);
+        return day.getTime();
+    }
 }

+ 11 - 1
gyee-sample-impala/src/main/java/com/gyee/impala/controller/diagnose/WindspeedforecastshorttermController.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.gyee.impala.common.result.JsonResult;
 import com.gyee.impala.common.result.ResultCode;
+import com.gyee.impala.common.util.DateUtil;
 import com.gyee.impala.model.master.Windspeedforecastshortterm;
 import com.gyee.impala.service.master.IWindspeedforecastshorttermService;
 import com.gyee.impala.service.master.IWindspeedforecastspshorttermService;
@@ -11,6 +12,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -57,7 +59,15 @@ public class WindspeedforecastshorttermController {
                              @RequestParam("endtime") String endtime,
                              @RequestParam(value = "timescale", required = false) Integer timescale
                              ){
+        if(stationid.isEmpty()||starttime.isEmpty()||endtime.isEmpty()){
+            return JsonResult.error(ResultCode.PARAM_IS_BLANK);
+        }
+        Date dateStart = DateUtil.str2DateTime(starttime);
+        Date dateEnd = DateUtil.str2DateTime(endtime);
+        if(dateEnd.before(dateStart) || dateEnd.after(DateUtil.today0am())){
+            return JsonResult.error(1007, "时间超出范围");
+        }
         if(timescale==null) timescale = 0;
-        return windspeedforecastshorttermService.getContrast(stationid,starttime,endtime,timescale);
+        return windspeedforecastshorttermService.getContrast(stationid,dateStart,dateEnd,timescale);
     }
 }

+ 8 - 16
gyee-sample-impala/src/main/java/com/gyee/impala/service/impl/master/WindspeedforecastshorttermServiceImpl.java

@@ -99,18 +99,13 @@ public class WindspeedforecastshorttermServiceImpl extends ServiceImpl<Windspeed
 
     @Override
     @Cacheable("windspeedforecastcontrast")
-    public JSONObject getContrast(String stationid, String starttime, String endtime, int timescale) {
-        if(stationid.isEmpty()||starttime.isEmpty()||endtime.isEmpty()){
-            return JsonResult.error(ResultCode.PARAM_IS_BLANK);
-        }
-        Date dateStart = DateUtil.str2DateTime(starttime);
-        Date dateEnd = DateUtil.str2DateTime(endtime);
-        Date dateStartQntq = DateUtil.str2QntqDateTime(starttime);
-        Date dateEndQntq = DateUtil.str2QntqDateTime(endtime);
+    public JSONObject getContrast(String stationid, Date starttime, Date endtime, int timescale) {
+        Date dateStartQntq = DateUtil.dateTimeAddYear(starttime,-1);
+        Date dateEndQntq = DateUtil.dateTimeAddYear(endtime,-1);
 
         //list1短期 list2超短期 list3去年同期短期 list4去年同期超短期
-        List<Windspeedforecastshortterm> list1 = getShortterm(stationid,dateStart,dateEnd);
-        List<Windspeedforecastspshortterm> list2 = getSpShortterm(stationid,dateStart,dateEnd,timescale);
+        List<Windspeedforecastshortterm> list1 = getShortterm(stationid,starttime,endtime);
+        List<Windspeedforecastspshortterm> list2 = getSpShortterm(stationid,starttime,endtime,timescale);
         List<Windspeedforecastshortterm> list3 = getShortterm(stationid,dateStartQntq,dateEndQntq);
         List<Windspeedforecastspshortterm> list4 = getSpShortterm(stationid,dateStartQntq,dateEndQntq,timescale);
         List<WindspeedforecastContrast> windspeedforecastContrasts = new ArrayList<>();
@@ -124,10 +119,7 @@ public class WindspeedforecastshorttermServiceImpl extends ServiceImpl<Windspeed
             windspeedforecastContrasts.add(wsfcc);
         });
         Map<Date, WindspeedforecastContrast> contrastMap = windspeedforecastContrasts.stream().collect(Collectors.toMap(WindspeedforecastContrast::getCalctime, Function.identity()));
-        /*Map<Date, WindspeedforecastContrast> contrastMap = new HashMap<>();
-        for (WindspeedforecastContrast wsfcc : windspeedforecastContrasts) {
-            contrastMap.put(wsfcc.getCalctime(), wsfcc);
-        }*/
+
         list2.forEach(l-> {
             WindspeedforecastContrast wsfcc = contrastMap.get(l.getCalctime());
             wsfcc.setForecastpowersp(l.getForecastpower());
@@ -135,14 +127,14 @@ public class WindspeedforecastshorttermServiceImpl extends ServiceImpl<Windspeed
         });
         //给年份加一年,然后匹配
         list3.forEach(l-> {
-            WindspeedforecastContrast wsfcc = contrastMap.get(DateUtil.dateTimeAddYear(l.getCalctime()));
+            WindspeedforecastContrast wsfcc = contrastMap.get(DateUtil.dateTimeAddYear(l.getCalctime(),1));
             wsfcc.setActualpowertq(l.getActualpower());
             wsfcc.setForecastpowertq(l.getForecastpower());
             wsfcc.setActualwindspeedtq(l.getActualwindspeed());
             wsfcc.setForecastwindspeedtq(l.getForecastwindspeed());
         });
         list4.forEach(l-> {
-            WindspeedforecastContrast wsfcc = contrastMap.get(DateUtil.dateTimeAddYear(l.getCalctime()));
+            WindspeedforecastContrast wsfcc = contrastMap.get(DateUtil.dateTimeAddYear(l.getCalctime(),1));
             wsfcc.setForecastpowersptq(l.getForecastpower());
             wsfcc.setForecastwindspeedsptq(l.getForecastwindspeed());
         });

+ 2 - 1
gyee-sample-impala/src/main/java/com/gyee/impala/service/master/IWindspeedforecastshorttermService.java

@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.gyee.impala.model.master.Windspeedforecastshortterm;
 import org.apache.kudu.client.KuduException;
 
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -23,5 +24,5 @@ public interface IWindspeedforecastshorttermService extends IService<Windspeedfo
 
     IPage<Windspeedforecastshortterm> list(Page<Windspeedforecastshortterm> page, QueryWrapper<Windspeedforecastshortterm> wrapper);
 
-    JSONObject getContrast(String stationid, String starttime, String endtime, int timescale);
+    JSONObject getContrast(String stationid, Date starttime, Date endtime, int timescale);
 }