From 2e5ad6b6a97104693564ebc9108f58642386a362 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Wed, 03 Nov 2021 17:03:30 +0800
Subject: [PATCH] city_daily中aqi,首要污染物计算

---
 screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java |   89 ++++++++++++++++++++++++++++++++++----------
 1 files changed, 69 insertions(+), 20 deletions(-)

diff --git a/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
index 797334b..7fe292a 100644
--- a/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
+++ b/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
@@ -2,18 +2,20 @@
 
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.moral.api.config.mybatis.MybatisPlusConfig;
 import com.moral.api.entity.HistoryHourly;
 import com.moral.api.entity.Sensor;
 import com.moral.api.mapper.HistoryHourlyMapper;
 import com.moral.api.mapper.HistoryMinutelyMapper;
 import com.moral.api.service.HistoryHourlyService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.moral.api.service.SensorService;
 import com.moral.constant.Constants;
 import com.moral.constant.RedisConstants;
+import com.moral.constant.SeparateTableType;
 import com.moral.util.AmendUtils;
 import com.moral.util.DateUtils;
 
+import com.moral.util.MybatisPLUSUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
@@ -39,7 +41,7 @@
  * @since 2021-06-28
  */
 @Service
-public class HistoryHourlyServiceImpl extends ServiceImpl<HistoryHourlyMapper, HistoryHourly> implements HistoryHourlyService {
+public class HistoryHourlyServiceImpl implements HistoryHourlyService {
 
     @Autowired
     private HistoryHourlyMapper historyHourlyMapper;
@@ -54,6 +56,11 @@
     private SensorService sensorService;
 
     @Override
+    public void createTable(String timeUnits) {
+        historyHourlyMapper.createTable(timeUnits);
+    }
+
+    @Override
     public void insertHistoryHourly() {
         //������������������yyyy-MM-dd HH:mm
         String format = DateUtils.yyyy_MM_dd_HH_EN;
@@ -64,16 +71,18 @@
         Date now = new Date();
         String time = DateUtils.dateToDateString(now, format) + ":00:00";
 
-        QueryWrapper<HistoryHourly> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("time", time);
         //������������������������������������������������macs������������������������������
-        Integer count = historyHourlyMapper.selectCount(queryWrapper);
-        if (macs.size() > count) {
+        Map<String, Object> prop = new HashMap<>();
+        prop.put("timeUnits", DateUtils.getDateStringOfMon(0, DateUtils.yyyyMM_EN));
+        prop.put("time", time);
+        Integer count = historyHourlyMapper.selectCountByTime(prop);
+
+        if (macs.size() <= count) {
+            return;
+        } else {
             macs.removeIf(mac -> {
-                queryWrapper.clear();
-                queryWrapper.eq("time", time);
-                queryWrapper.eq("mac", mac);
-                Integer num = historyHourlyMapper.selectCount(queryWrapper);
+                prop.put("mac", mac);
+                Integer num = historyHourlyMapper.selectCountByTime(prop);
                 return num != 0;
             });
         }
@@ -110,14 +119,14 @@
                 .collect(Collectors.groupingBy(o -> (String) o.get("mac")));
 
         //���������������������������
-        List<HistoryHourly> insertData = new ArrayList<>();
+        List<Map<String, Object>> insertData = new ArrayList<>();
 
         data.forEach((key, value) -> {
-            HistoryHourly historyHourly = new HistoryHourly();
-            historyHourly.setMac(key);
-            historyHourly.setTime(end);
-            Map<String, Object> jsonMap = new HashMap<>();
+            Map<String, Object> historyHourly = new HashMap<>();
+            historyHourly.put("mac", key);
+            historyHourly.put("time", end);
 
+            Map<String, Object> jsonMap = new HashMap<>();
             Map<String, Object> map = new HashMap<>();
             map.put("data", value);
             map.put("type", "hour");
@@ -163,7 +172,7 @@
                             Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
                             Object sensorValue = dataValue.get(sensorCode);
                             //������������������������
-                            Object flag = dataValue.get(sensorCode + Constants.MARKER_BIT_KEY);
+                            Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY);
                             if (!Constants.MARKER_BIT_TRUE.equals(flag)) {
                                 return null;
                             }
@@ -199,18 +208,58 @@
                     jsonMap.put(sensorCode, sciCal);
                     //���������
                     if (size.get() >= 45) {
-                        jsonMap.put(sensorCode + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_TRUE);
+                        jsonMap.put(sensorCode + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_TRUE);
                     } else {
-                        jsonMap.put(sensorCode + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_FALSE);
+                        jsonMap.put(sensorCode + "-" + Constants.MARKER_BIT_KEY, Constants.MARKER_BIT_FALSE);
                     }
                 }
             });
-            historyHourly.setValue(JSONObject.toJSONString(jsonMap));
-            historyHourly.setVersion((Integer) value.get(0).get("version"));
+            historyHourly.put("version", value.get(0).get("version"));
+            historyHourly.put("value", JSONObject.toJSONString(jsonMap));
             insertData.add(historyHourly);
         });
 
         //���������������
         historyHourlyMapper.insertHistoryHourly(insertData);
     }
+
+    @Override
+    public List<Map<String, Object>> selectDailyData(Map<String, Object> params) {
+        return historyHourlyMapper.selectDailyData(params);
+    }
+
+    /**
+     * @Description: ���������������������������mac���������
+     * @Param: [mac, startDate, endDate]
+     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
+     * @Author: ���������
+     * @Date: 2021/9/23
+     */
+    @Override
+    public List<HistoryHourly> getValueByMacAndTime(String mac, Date startDate, Date endDate){
+        QueryWrapper<HistoryHourly> wrapper = new QueryWrapper<>();
+        wrapper.eq("mac",mac);
+        wrapper.between("time",startDate,endDate);
+        List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
+        List<HistoryHourly> datas = multiTableQuery(wrapper, tableNames);
+        return datas;
+    }
+
+    /**
+     * @Description: ������������������������������������������������wrapper���������������
+     * @Param: [wrapper, tableNames]
+     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
+     * @Author: ���������
+     * @Date: 2021/9/23
+     */
+    private List<HistoryHourly> multiTableQuery(QueryWrapper<HistoryHourly> wrapper,List<String> tableNames){
+        List<HistoryHourly> result = new ArrayList<>();
+        for (String tableName : tableNames) {
+            MybatisPlusConfig.tableName.set(tableName);
+            List<HistoryHourly> datas = historyHourlyMapper.selectList(wrapper);
+            result.addAll(datas);
+        }
+        MybatisPlusConfig.tableName.remove();
+        return result;
+    }
 }

--
Gitblit v1.8.0