Browse Source

mysql查询指定月份的数据

yq 4 years ago
parent
commit
5bcfb6c9fd

+ 5 - 3
mhfire-controller/src/main/java/com/bizmatics/mhfire/controller/web/FireStatisticsControllerWeb.java

@@ -32,16 +32,18 @@ public class FireStatisticsControllerWeb {
 
     /**
      * 根据年份和地址查看12月份的火灾统计
-     * @param year 年份
+     * @param startTime 开始时间
+     * @param endTime 开始时间
      * @param address 地址
      * @param fireType 火灾类别
      * @return
      */
     @GetMapping("fireCountByData")
-    public ApiResult<List<Integer>> getFireCountByData(@RequestParam Integer year,
+    public ApiResult<List<Object>> getFireCountByData(@RequestParam Date startTime,
+                                                       @RequestParam Date endTime,
                                                        @RequestParam(required = false)String address,
                                                        @RequestParam(required = false)String fireType){
-        return ApiResult.success(fireStatisticsService.getFireCountByData(year, address,fireType));
+        return ApiResult.success(fireStatisticsService.getFireCountByData(startTime,endTime,address,fireType));
     }
 
 

+ 9 - 7
mhfire-controller/src/main/java/com/bizmatics/mhfire/controller/web/WeatherControllerWeb.java

@@ -8,14 +8,17 @@ import com.bizmatics.common.core.util.HttpUtils;
 
 
 import com.bizmatics.common.core.util.StringUtils;
+import com.bizmatics.mhfire.service.vo.AlertStatisticsVO;
 import okhttp3.*;
 
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.RequestBody;
 
 import java.io.IOException;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
 
 /**
  * @author yq
@@ -50,10 +53,9 @@ public class WeatherControllerWeb {
     }
     public String getWeatherApi(){
         try {
-            OkHttpClient defaultClient = HttpUtils.getDefaultClient();
-            Request request1 = new Request.Builder().url(ALI_WEATHER_API_URL).addHeader(ALI_WEATHER_HEADER_KEY,ALI_WEATHER_HEADER_VALUE).build();
-            Response response = defaultClient.newCall(request1).execute();
-            return HttpUtils.responseToString(response);
+            Map<String,String> headerMap = new HashMap<>();
+            headerMap.put(ALI_WEATHER_HEADER_KEY,ALI_WEATHER_HEADER_VALUE);
+            return HttpUtils.get(ALI_WEATHER_API_URL,headerMap);
         } catch (IOException e) {
             throw new BusinessException(e.getMessage());
         }

+ 12 - 0
mhfire-mapping/src/main/java/com/bizmatics/mhfire/persistence/mapper/FireStatisticsMapper.java

@@ -58,4 +58,16 @@ public interface FireStatisticsMapper {
     List<String> getAddress();
 
 
+    Date selectMaxDate();
+
+    Date selectMinDate();
+
+
+    Integer selectCountByRangeDate(@Param("startTime") Date startTime,
+                                   @Param("endTime") Date endTime,
+                                   @Param("address") String address,
+                                   @Param("fireType") String fireType,
+                                   @Param("month") Integer month);
+
+
 }

+ 23 - 0
mhfire-mapping/src/main/resources/mapper/mysql/FireStatisticsMapper.xml

@@ -48,4 +48,27 @@
     <select id="getAddress" resultType="java.lang.String">
         select `行政区域` from `hzdc-hztjb` group by `行政区域`
     </select>
+    <select id="selectMaxDate" resultType="java.util.Date">
+        select max(`起火时间_日期型`) from `hzdc-hztjb`
+    </select>
+    <select id="selectMinDate" resultType="java.util.Date">
+        select min(`起火时间_日期型`) from `hzdc-hztjb`
+    </select>
+    <select id="selectCountByRangeDate" resultType="java.lang.Integer">
+        select count(1)
+        from `hzdc-hztjb`
+        <where>
+            <if test="address != null and address != ''">
+                and `起火地点` LIKE CONCAT(CONCAT('%', #{address}), '%')
+            </if>
+            <if test="fireType != null and fireType != ''">
+                and `火灾原因分类(一级)` = #{fireType}
+            </if>
+            <if test="startTime != null and endTime != null">
+                and  `起火时间_日期型` between STR_TO_DATE(#{startTime},'%Y-%m-%d %H:%i:%s')
+                and STR_TO_DATE(#{endTime},'%Y-%m-%d %H:%i:%s')
+            </if>
+            and Month(`起火时间_日期型`) = #{month}
+        </where>
+    </select>
 </mapper>

+ 3 - 2
mhfire-service/src/main/java/com/bizmatics/mhfire/service/FireStatisticsService.java

@@ -18,11 +18,12 @@ public interface FireStatisticsService {
 
     /**
      * 根据年份和地址查看12月份的火灾统计
-     * @param year
+     * @param startTime
+     * @param endTime
      * @param address
      * @return
      */
-    List<Integer> getFireCountByData(Integer year,String address,String fireType);
+    List<Object> getFireCountByData(Date startTime,Date endTime,String address,String fireType);
 
 
     /**

+ 57 - 11
mhfire-service/src/main/java/com/bizmatics/mhfire/service/impl/FireStatisticsServiceImpl.java

@@ -35,19 +35,65 @@ public class FireStatisticsServiceImpl implements FireStatisticsService {
     @Autowired
     private UnitService unitService;
     @Override
-    public List<Integer> getFireCountByData(Integer year, String address,String fireType) {
-        List<Integer> list = new ArrayList<>();
+    public List<Object> getFireCountByData(Date startTime,Date endTime,String address,String fireType) {
+        List<Object> list = new ArrayList<>();
+        List<Integer> thisYearList = new ArrayList<>();
+        List<Double> monthList = new ArrayList<>();
+        List<Double> yearList = new ArrayList<>();
         Date date = new Date();
+        //获取传入时间的最大时间和最小时间
+        int distanceOfTwoDate =Integer.parseInt(DateUtils.getYear(endTime)) - Integer.parseInt(DateUtils.getYear(startTime)) + 1;
+        Date startMax = DateUtils.getFirstDayOfMonth(DateUtils.setMonths(startTime, 0));
+        Date endMax = DateUtils.getLastDayOfMonth(DateUtils.setMonths(endTime, 11));
+        //获取数据库全部项目的最小时间和最大时间
+        Date maxDate = fireStatisticsMapper.selectMaxDate();
+        Date minDate = fireStatisticsMapper.selectMinDate();
+        int distanceOfTwoDateYear =Integer.parseInt(DateUtils.getYear(maxDate)) - Integer.parseInt(DateUtils.getYear(minDate)) + 1;
         for (int i = 0; i < 12; i++) {
-            date.setDate(1);
+            //获取今年的12个月份数据
+            date = DateUtils.setDays(date,1);
             Date setMonths = DateUtils.setMonths(date, i);
             Date firstDayOfMonth = DateUtils.getFirstDayOfMonth(setMonths);
             Date lastDayOfMonth = DateUtils.getLastDayOfMonth(setMonths);
-            list.add(fireStatisticsMapper.selectCount(firstDayOfMonth, lastDayOfMonth, address,fireType,null));
+            thisYearList.add(fireStatisticsMapper.selectCount(firstDayOfMonth, lastDayOfMonth, address,fireType,null));
+            //月平均值
+            Integer monthCount = fireStatisticsMapper.selectCountByRangeDate(startMax, endMax, address, fireType,i+1);
+            monthList.add(Arith.div(monthCount, distanceOfTwoDate));
+            Integer yearCount = fireStatisticsMapper.selectCountByRangeDate(null, null,address, fireType,i+1);
+            yearList.add(Arith.div(yearCount, distanceOfTwoDate));
         }
+        list.add(thisYearList);
+        list.add(monthList);
+        list.add(yearList);
         return list;
     }
 
+    /**
+     * 月平均值/年平均值
+     * @param endTime
+     * @param distanceOfTwoDate
+     * @param address
+     * @param fireType
+     * @param month
+     * @return
+     */
+    public Double getFireCountByDate(Date endTime,Integer distanceOfTwoDate,String address,String fireType,Integer month){
+        int count = 0;
+        for (int j = 0; j < distanceOfTwoDate; j++) {
+            Date monthDate = endTime;
+            if (0 != j){
+                monthDate = DateUtils.addYears(endTime, -j);
+            }
+            monthDate = DateUtils.setMonths(monthDate, month);
+            Date monthDateStartTime = DateUtils.getFirstDayOfMonth(monthDate);
+            Date monthDateEndTime = DateUtils.getLastDayOfMonth(monthDate);
+            System.out.println(DateUtils.format(monthDateStartTime,"yyyy-MM-dd HH:mm:ss"));
+            System.out.println(DateUtils.format(monthDateEndTime,"yyyy-MM-dd HH:mm:ss"));
+            count+=fireStatisticsMapper.selectCount(monthDateStartTime, monthDateEndTime, address, fireType, null);
+        }
+        return (double) (count/distanceOfTwoDate);
+    }
+
 
     @Override
     public CommonPage<FireStatisticsPO> page(Integer current, Integer size, Date startTime, Date endTime, String address) {
@@ -100,17 +146,17 @@ public class FireStatisticsServiceImpl implements FireStatisticsService {
 
         //差集求和
         double sum = list.stream().filter(item -> !orderByList.contains(item)).mapToDouble(FireLevelRatioVO::getRadio).sum();
-        FireLevelRatioVO fireLevelRatioVO = orderByList.stream().filter(item -> "其他".equals(item.getFireType())).findAny()
+        orderByList.stream().filter(item -> "其他".equals(item.getFireType())).findAny()
                 .map(fireLevelRatio -> {
                             fireLevelRatio.setRadio(fireLevelRatio.getRadio() + sum);
                             return fireLevelRatio;
                         }
-                ).orElse(new FireLevelRatioVO());
-        if (null == fireLevelRatioVO.getFireType()){
-            fireLevelRatioVO.setFireType("其他");
-            fireLevelRatioVO.setRadio(sum);
-            orderByList.add(fireLevelRatioVO);
-        }
+                ).orElseGet(() -> {FireLevelRatioVO fireLevelRatioVo = new FireLevelRatioVO();
+                    fireLevelRatioVo.setFireType("其他");
+                    fireLevelRatioVo.setRadio(sum);
+                    orderByList.add(fireLevelRatioVo);
+                    return fireLevelRatioVo;
+                });
         int finalFireCount = fireCount;
         if (0 != finalFireCount){
             orderByList.forEach(fireLevelRatio -> fireLevelRatio.setRadio(Arith.div(fireLevelRatio.getRadio(),finalFireCount)));