|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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.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; | 
|---|
|  |  |  | 
|---|
|  |  |  | .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"); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|