From 76e8dfb14845172b30ddfaa168517134b316f7e2 Mon Sep 17 00:00:00 2001 From: mh Date: Wed, 20 Nov 2024 17:00:43 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BC=98=E5=8C=96=E7=94=A8=E7=94=B5?= =?UTF-8?q?=E3=80=81=E7=94=A8=E6=B0=B4=E3=80=81=E6=B0=B4=E4=BD=8D=E9=A2=84?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mh/user/job/DealDataJob.java | 37 +++++++++++-------- .../mh/user/mapper/HistoryDataPreMapper.java | 16 +++++--- .../impl/HistoryDataPreServiceImpl.java | 10 ++--- .../test/java/com/mh/user/DealDataTest.java | 2 +- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/user-service/src/main/java/com/mh/user/job/DealDataJob.java b/user-service/src/main/java/com/mh/user/job/DealDataJob.java index 92d2fff..b7fdfbb 100644 --- a/user-service/src/main/java/com/mh/user/job/DealDataJob.java +++ b/user-service/src/main/java/com/mh/user/job/DealDataJob.java @@ -109,9 +109,9 @@ public class DealDataJob { if (Constant.WEB_FLAG) { break; } - CountDownLatch countDownLatch = new CountDownLatch(Math.min(batchSize, dataComMap.size() - k)); + CountDownLatch countDownLatch = new CountDownLatch(Math.min(batchSize, dataComMap.size() - k)); index = k; - for (int j = 0; j < Math.min(batchSize, dataComMap.size() - k); j++) { + for (int j = 0; j < Math.min(batchSize, dataComMap.size() - k); j++) { if (Constant.WEB_FLAG) { break; } @@ -173,19 +173,6 @@ public class DealDataJob { dealDataService.proTotalPumpMinutes(curDate); //统计周\月热泵运行时长 log.info("进入定时调试数据库过程汇总数据!yyyy-MM-dd"); -// // 开始预测数据 -// List buildingModels = buildingService.selectBuildingName(); -// for (BuildingModel buildingModel : buildingModels) { -// String buildingId = String.valueOf(buildingModel.getBuildingId()); -// try { -// // 训练数据 -// historyDataPreService.startTrainData(buildingId); -// // 预测数据 -// historyDataPreService.startPredictData(buildingId, curDate); -// } catch (Exception e) { -// log.error("定时处理数据以及预测数据异常==>", e); -// } -// } stopWatch.stop(); log.info("定时处理数据以及预测数据结束!耗时:" + stopWatch.getTotalTimeSeconds() + "秒"); } catch (Exception e) { @@ -193,6 +180,26 @@ public class DealDataJob { } } + @Scheduled(cron = "0 0 0/1 * * ?") + public void preUseData() { + // 每小时预测一次数据 + SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); + Date date = new Date(); + String curDate = sdf1.format(date); + List buildingModels = buildingService.selectBuildingName(); + for (BuildingModel buildingModel : buildingModels) { + String buildingId = String.valueOf(buildingModel.getBuildingId()); + try { + // 训练数据 + historyDataPreService.startTrainData(buildingId); + // 预测数据 + historyDataPreService.startPredictData(buildingId, curDate); + } catch (Exception e) { + log.error("定时处理数据以及预测数据异常==>", e); + } + } + } + /** * 定时删除历史流水记录(删除前三个月的记录) */ diff --git a/user-service/src/main/java/com/mh/user/mapper/HistoryDataPreMapper.java b/user-service/src/main/java/com/mh/user/mapper/HistoryDataPreMapper.java index 330c670..bcb2d68 100644 --- a/user-service/src/main/java/com/mh/user/mapper/HistoryDataPreMapper.java +++ b/user-service/src/main/java/com/mh/user/mapper/HistoryDataPreMapper.java @@ -27,7 +27,9 @@ public interface HistoryDataPreMapper extends BaseMapper { @Result(column = "water_level", property = "waterLevel"), @Result(column = "people_num", property = "peopleNum") }) - @Select("select env_min_temp, env_max_temp, water_value, elect_value, water_level, people_num from history_data_pre where building_id = #{buildingId} order by cur_date ") + @Select("select env_min_temp, env_max_temp, water_value, elect_value, water_level, people_num from history_data_pre where building_id = #{buildingId} " + + " and env_min_temp > 0 and env_max_temp >0 and water_value > 0 and elect_value > 0 and water_level >0 " + + " order by cur_date ") List getTrainData(@Param("buildingId") String buildingId); @Results(id ="rs_recent_data",value ={ @@ -65,7 +67,8 @@ public interface HistoryDataPreMapper extends BaseMapper { @Result(column = "env_max_temp", property = "envMaxTemp"), @Result(column = "water_value", property = "waterValue"), @Result(column = "elect_value", property = "electValue"), - @Result(column = "water_level", property = "waterLevel") + @Result(column = "water_level", property = "waterLevel"), + @Result(column = "check_in_count", property = "peopleNum") }) @Select("select top 1 " + " convert(date,eds.cur_date) as cur_date, " + @@ -77,7 +80,8 @@ public interface HistoryDataPreMapper extends BaseMapper { " isnull(convert(numeric(24, " + " 2), " + " t1.water_level), " + - " 0) as water_level " + + " 0) as water_level," + + " b.check_in_count " + "from " + " energy_day_sum eds " + "left join ( " + @@ -98,6 +102,8 @@ public interface HistoryDataPreMapper extends BaseMapper { " ) t1 on " + " eds.cur_date = t1.cur_date " + " and eds.building_id = t1.building_id " + + " left join building b " + + " on b.id = eds.building_id " + "where " + " eds.building_id != '所有' " + " and eds.building_id = #{buildingId} " + @@ -108,8 +114,8 @@ public interface HistoryDataPreMapper extends BaseMapper { HistoryDataPre selectCurData(@Param("buildingId") String buildingId, @Param("curDate") String curDate); - @Insert("insert into history_data_pre(cur_date, building_id, env_min_temp, env_max_temp, water_value, elect_value, water_level) values(" + - "convert(date,#{curDate}), #{buildingId}, #{envMinTemp}, #{envMaxTemp}, #{waterValue}, #{electValue}, #{waterLevel}" + + @Insert("insert into history_data_pre(cur_date, building_id, env_min_temp, env_max_temp, water_value, elect_value, water_level, people_num) values(" + + "convert(date,#{curDate}), #{buildingId}, #{envMinTemp}, #{envMaxTemp}, #{waterValue}, #{electValue}, #{waterLevel}, #{peopleNum}" + ")") void insertData(HistoryDataPre curHistoryData); diff --git a/user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java b/user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java index ed3adff..e9a80b1 100644 --- a/user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java +++ b/user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java @@ -62,7 +62,7 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { public static String[] convert(HistoryDataPre dataPre) { // 假设HistoryDataPre有字段如field1, field2, field3等,根据需要进行转换 - return new String[] { + return new String[]{ String.valueOf(dataPre.getEnvMinTemp()), String.valueOf(dataPre.getEnvMaxTemp()), String.valueOf(dataPre.getPeopleNum()), @@ -102,7 +102,7 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { BPModel bpModel = factory.trainBP(bpParameter, trainSet); // 将BPModel序列化到本地 - SerializationUtil.serialize(bpModel, buildingId+"_pre_data"); + SerializationUtil.serialize(bpModel, buildingId + "_pre_data"); } @Override @@ -130,7 +130,7 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject1 = jsonArray.getJSONObject(i); if (jsonObject1.getString("date").equals(curDate)) { - envMinTemp =jsonObject1.getString("nighttemp"); + envMinTemp = jsonObject1.getString("nighttemp"); envMaxTemp = jsonObject1.getString("daytemp"); break; } @@ -158,7 +158,7 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { csvInfo.setCsvFileList(list); Matrix data = csvInfo.toMatrix(); // 将BPModel反序列化 - BPModel bpModel1 = (BPModel) SerializationUtil.deSerialization(buildingId+"_pre_data"); + BPModel bpModel1 = (BPModel) SerializationUtil.deSerialization(buildingId + "_pre_data"); // 创建工厂 BPNeuralNetworkFactory factory = new BPNeuralNetworkFactory(); Matrix result = factory.computeBP(bpModel1, data); @@ -174,7 +174,7 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { // 拼接预测值 preHistoryData.setWaterValuePre(evaluateAndReturnBigDecimal(record[0])); preHistoryData.setElectValuePre(evaluateAndReturnBigDecimal(record[1])); - preHistoryData.setWaterLevelPre(evaluateAndReturnBigDecimal(record[2])); + preHistoryData.setWaterLevelPre(evaluateAndReturnBigDecimal(record[2]).compareTo(BigDecimal.valueOf(100)) > 0 ? BigDecimal.valueOf(100) : evaluateAndReturnBigDecimal(record[2])); } preHistoryData.setWaterValue(curHistoryData.getWaterValue()); preHistoryData.setElectValue(curHistoryData.getElectValue()); diff --git a/user-service/src/test/java/com/mh/user/DealDataTest.java b/user-service/src/test/java/com/mh/user/DealDataTest.java index fe07b4a..3e636a9 100644 --- a/user-service/src/test/java/com/mh/user/DealDataTest.java +++ b/user-service/src/test/java/com/mh/user/DealDataTest.java @@ -52,7 +52,7 @@ public class DealDataTest extends UserServiceApplicationTests { // 训练数据 historyDataPreService.startTrainData("21"); // 预测数据 - historyDataPreService.startPredictData("21", "2024-03-21"); + historyDataPreService.startPredictData("21", "2024-11-19"); } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage());