Browse Source

1、用能预测后端数据处理;

dev
mh 6 months ago
parent
commit
8d77490a9f
  1. 20
      user-service/src/main/java/com/mh/user/controller/EnergyPreController.java
  2. 14
      user-service/src/main/java/com/mh/user/dto/EnergyPreDTO.java
  3. 37
      user-service/src/main/java/com/mh/user/dto/EnergyPreEchartDataDTO.java
  4. 37
      user-service/src/main/java/com/mh/user/dto/EnergyPreTopDataDTO.java
  5. 102
      user-service/src/main/java/com/mh/user/mapper/HistoryDataPreMapper.java
  6. 15
      user-service/src/main/java/com/mh/user/service/HistoryDataPreService.java
  7. 55
      user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java
  8. 11
      user-service/src/test/java/com/mh/user/UserServiceApplicationTests.java

20
user-service/src/main/java/com/mh/user/controller/EnergyPreController.java

@ -1,8 +1,14 @@
package com.mh.user.controller; package com.mh.user.controller;
import com.mh.common.http.HttpResult;
import com.mh.user.dto.EnergyPreDTO;
import com.mh.user.service.HistoryDataPreService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/** /**
* @author LJF * @author LJF
* @version 1.0 * @version 1.0
@ -11,8 +17,20 @@ import org.springframework.web.bind.annotation.RestController;
* @date 2024-05-09 17:24:48 * @date 2024-05-09 17:24:48
*/ */
@RestController @RestController
@RequestMapping("/energy_pre") @RequestMapping("/energyPre")
public class EnergyPreController { public class EnergyPreController {
@Resource
private HistoryDataPreService historyDataPreService;
@PostMapping("/topData")
public HttpResult getTopData(String buildingId, String type) {
return HttpResult.ok(historyDataPreService.getTopData(buildingId, type));
}
@PostMapping("/echartData")
public HttpResult getEnergyPre(String buildingId, String beginDate, String endDate, String type) {
return HttpResult.ok(historyDataPreService.getEnergyPre(buildingId, beginDate, endDate, type));
}
} }

14
user-service/src/main/java/com/mh/user/dto/EnergyPreDTO.java

@ -1,5 +1,8 @@
package com.mh.user.dto; package com.mh.user.dto;
import lombok.Getter;
import lombok.Setter;
/** /**
* @author LJF * @author LJF
* @version 1.0 * @version 1.0
@ -7,9 +10,18 @@ package com.mh.user.dto;
* @description 用能预测前端类 * @description 用能预测前端类
* @date 2024-05-09 17:31:27 * @date 2024-05-09 17:31:27
*/ */
@Setter
@Getter
public class EnergyPreDTO { public class EnergyPreDTO {
private String buildingId; /**
* 顶部数据
*/
private EnergyPreTopDataDTO topData;
/**
* 折线图数据
*/
private EnergyPreEchartDataDTO echartData;
} }

37
user-service/src/main/java/com/mh/user/dto/EnergyPreEchartDataDTO.java

@ -0,0 +1,37 @@
package com.mh.user.dto;
import lombok.Getter;
import lombok.Setter;
/**
* @author LJF
* @version 1.0
* @project CHWS
* @description 用能预测前端类
* @date 2024-05-09 17:31:27
*/
@Setter
@Getter
public class EnergyPreEchartDataDTO {
/**
* 时间
*/
private String curDate;
/**
* 实际值
*/
private String curData;
/**
* 预测值
*/
private String preData;
/**
* 误差值
*/
private String errorData;
}

37
user-service/src/main/java/com/mh/user/dto/EnergyPreTopDataDTO.java

@ -0,0 +1,37 @@
package com.mh.user.dto;
import lombok.Getter;
import lombok.Setter;
/**
* @author LJF
* @version 1.0
* @project CHWS
* @description 用能预测前端类
* @date 2024-05-09 17:31:27
*/
@Setter
@Getter
public class EnergyPreTopDataDTO {
/**
* 昨日实际值
*/
private String yesData;
/**
* 昨日预测值
*/
private String preYesData;
/**
* 今日预测值
*/
private String curYesData;
/**
* 误差值
*/
private String errorData;
}

102
user-service/src/main/java/com/mh/user/mapper/HistoryDataPreMapper.java

@ -1,5 +1,7 @@
package com.mh.user.mapper; package com.mh.user.mapper;
import com.mh.user.dto.EnergyPreEchartDataDTO;
import com.mh.user.dto.EnergyPreTopDataDTO;
import com.mh.user.entity.HistoryDataPre; import com.mh.user.entity.HistoryDataPre;
import org.apache.ibatis.annotations.*; import org.apache.ibatis.annotations.*;
import org.springframework.security.core.parameters.P; import org.springframework.security.core.parameters.P;
@ -118,4 +120,104 @@ public interface HistoryDataPreMapper extends BaseMapper<HistoryDataPre> {
@Select("select top 1 id, building_id, cur_date, env_min_temp, env_max_temp from history_data_pre where building_id = #{buildingId} and cur_date = #{curDate} ") @Select("select top 1 id, building_id, cur_date, env_min_temp, env_max_temp from history_data_pre where building_id = #{buildingId} and cur_date = #{curDate} ")
HistoryDataPre selectOneData(@Param("buildingId") String buildingId, HistoryDataPre selectOneData(@Param("buildingId") String buildingId,
@Param("curDate") String curDate); @Param("curDate") String curDate);
@Results({
@Result(column = "cur_yes_data",property = "curYesData" ),
@Result(column = "yes_data", property = "yesData"),
@Result(column = "pre_yes_data", property = "preYesData"),
@Result(column = "error_data", property = "errorData")
})
@Select("<script>" +
"SELECT " +
" <choose>" +
" <when test='type == \"1\"'>" +
" isnull(t.water_value_pre, 0) as cur_yes_data, " +
" isnull(t1.water_value, 0) as yes_data, " +
" isnull(t1.water_value_pre, 0) as pre_yes_data, " +
" CASE WHEN t1.water_value_pre > 0 THEN CONVERT(decimal(18, 2), ABS(isnull(t1.water_value_pre, 0) - isnull(t1.water_value, 0)) / t1.water_value_pre * 100) ELSE '0' END as error_data" +
" </when>" +
" <when test='type == \"2\"'>" +
" isnull(t.elect_value_pre, 0) as cur_yes_data, " +
" isnull(t1.elect_value, 0) as yes_data, " +
" isnull(t1.elect_value_pre, 0) as pre_yes_data, " +
" CASE WHEN t1.elect_value_pre > 0 THEN CONVERT(decimal(18, 2), ABS(isnull(t1.elect_value_pre, 0) - isnull(t1.elect_value, 0)) / t1.elect_value_pre * 100) ELSE '0' END as error_data" +
" </when>" +
" <when test='type == \"3\"'>" +
" isnull(t.water_level_pre, 0) as cur_yes_data, " +
" isnull(t1.water_level, 0) as yes_data, " +
" isnull(t1.water_level_pre, 0) as pre_yes_data, " +
" CASE WHEN t1.water_level_pre > 0 THEN CONVERT(decimal(18, 2), ABS(isnull(t1.water_level_pre, 0) - isnull(t1.water_level, 0)) / t1.water_level_pre * 100) ELSE '0' END as error_data" +
" </when>" +
" <otherwise>null</otherwise>" +
" </choose>" +
"FROM (" +
" SELECT " +
" building_id, " +
" water_value, " +
" elect_value, " +
" water_level, " +
" water_value_pre, " +
" elect_value_pre, " +
" water_level_pre " +
" FROM history_data_pre " +
" WHERE cur_date = CONVERT(varchar(10), GETDATE(), 120) AND building_id = #{buildingId}" +
") t " +
"JOIN (" +
" SELECT " +
" building_id, " +
" water_value, " +
" elect_value, " +
" water_level, " +
" water_value_pre, " +
" elect_value_pre, " +
" water_level_pre " +
" FROM history_data_pre " +
" WHERE cur_date = CONVERT(varchar(10), DATEADD(day, -1, GETDATE()), 120) AND building_id = #{buildingId}" +
") t1 ON t.building_id = t1.building_id " +
"</script>")
List<EnergyPreTopDataDTO> getTopData(@Param("buildingId") String buildingId, @Param("type") String type);
@Results({
@Result(column = "cur_date",property = "curDate" ),
@Result(column = "cur_data", property = "curData"),
@Result(column = "pre_data", property = "preData"),
@Result(column = "error_data", property = "errorData")
})
@Select("<script>" +
"SELECT " +
" hdp.cur_date, " +
" <choose>" +
" <when test='type == \"1\"'>isnull(hdp.water_value, 0)</when>" +
" <when test='type == \"2\"'>isnull(hdp.elect_value, 0)</when>" +
" <when test='type == \"3\"'>isnull(hdp.water_level, 0)</when>" +
" <otherwise>0</otherwise>" +
" </choose> as cur_data, " +
" <choose>" +
" <when test='type == \"1\"'>isnull(hdp.water_value_pre, 0)</when>" +
" <when test='type == \"2\"'>isnull(hdp.elect_value_pre, 0)</when>" +
" <when test='type == \"3\"'>isnull(hdp.water_level_pre, 0)</when>" +
" <otherwise>0</otherwise>" +
" </choose> as pre_data, " +
" CASE " +
" <when test='type == \"1\"'>" +
" WHEN hdp.water_value_pre > 0 THEN CONVERT(decimal(18, 2), ABS(isnull(hdp.water_value_pre, 0) - isnull(hdp.water_value, 0)) / hdp.water_value_pre * 100) else 0 " +
" </when>" +
" <when test='type == \"2\"'>" +
" WHEN hdp.elect_value_pre > 0 THEN CONVERT(decimal(18, 2), ABS(isnull(hdp.elect_value_pre, 0) - isnull(hdp.elect_value, 0)) / hdp.elect_value_pre * 100) else 0 " +
" </when>" +
" <when test='type == \"3\"'>" +
" WHEN hdp.water_level_pre > 0 THEN CONVERT(decimal(18, 2), ABS(isnull(hdp.water_level_pre, 0) - isnull(hdp.water_level, 0)) / hdp.water_level_pre * 100) else 0 " +
" </when>" +
" END as error_data " +
"FROM history_data_pre hdp " +
"WHERE hdp.building_id = #{buildingId} " +
" AND cur_date BETWEEN #{beginDate} AND #{endDate} " +
"ORDER BY cur_date" +
"</script>")
List<EnergyPreEchartDataDTO> getEnergyPre(@Param("buildingId") String buildingId,
@Param("beginDate") String beginDate,
@Param("endDate") String endDate,
@Param("type") String type);
} }

15
user-service/src/main/java/com/mh/user/service/HistoryDataPreService.java

@ -1,7 +1,11 @@
package com.mh.user.service; package com.mh.user.service;
import com.mh.user.dto.EnergyPreDTO;
import com.mh.user.dto.EnergyPreEchartDataDTO;
import com.mh.user.dto.EnergyPreTopDataDTO;
import com.mh.user.entity.HistoryDataPre; import com.mh.user.entity.HistoryDataPre;
import java.util.HashMap;
import java.util.List; import java.util.List;
/** /**
@ -36,4 +40,15 @@ public interface HistoryDataPreService {
*/ */
List<HistoryDataPre> getRecentData(String buildingId, String curDate); List<HistoryDataPre> getRecentData(String buildingId, String curDate);
/**
* 获取预测数据
* @param buildingId
* @param beginDate
* @param endDate
* @param type
* @return
*/
List<HashMap<String, Object>> getEnergyPre(String buildingId, String beginDate, String endDate, String type);
List<EnergyPreTopDataDTO> getTopData(String buildingId, String type);
} }

55
user-service/src/main/java/com/mh/user/service/impl/HistoryDataPreServiceImpl.java

@ -3,6 +3,7 @@ package com.mh.user.service.impl;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Cache;
import com.mh.algorithm.bpnn.BPModel; import com.mh.algorithm.bpnn.BPModel;
import com.mh.algorithm.bpnn.BPNeuralNetworkFactory; import com.mh.algorithm.bpnn.BPNeuralNetworkFactory;
@ -10,6 +11,10 @@ import com.mh.algorithm.bpnn.BPParameter;
import com.mh.algorithm.matrix.Matrix; import com.mh.algorithm.matrix.Matrix;
import com.mh.algorithm.utils.CsvInfo; import com.mh.algorithm.utils.CsvInfo;
import com.mh.algorithm.utils.SerializationUtil; import com.mh.algorithm.utils.SerializationUtil;
import com.mh.common.utils.StringUtils;
import com.mh.user.dto.EnergyPreDTO;
import com.mh.user.dto.EnergyPreEchartDataDTO;
import com.mh.user.dto.EnergyPreTopDataDTO;
import com.mh.user.entity.HistoryDataPre; import com.mh.user.entity.HistoryDataPre;
import com.mh.user.entity.SysParamEntity; import com.mh.user.entity.SysParamEntity;
import com.mh.user.mapper.HistoryDataPreMapper; import com.mh.user.mapper.HistoryDataPreMapper;
@ -22,7 +27,10 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
/** /**
@ -160,4 +168,51 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService {
public List<HistoryDataPre> getRecentData(String buildingId, String curDate) { public List<HistoryDataPre> getRecentData(String buildingId, String curDate) {
return historyDataPreMapper.getRecentData(buildingId, curDate); return historyDataPreMapper.getRecentData(buildingId, curDate);
} }
@Override
public List<HashMap<String, Object>> getEnergyPre(String buildingId, String beginDate, String endDate, String type) {
if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate)) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 获取当前日期
LocalDate now = LocalDate.now();
// 向前推30天
LocalDate startDate = now.minusDays(30);
beginDate = startDate.format(formatter);
// 结束日期是当前日期
endDate = now.format(formatter);
}
if (StringUtils.isBlank(buildingId) || StringUtils.isBlank(type)) {
return null;
}
List<EnergyPreEchartDataDTO> energyPre = historyDataPreMapper.getEnergyPre(buildingId, beginDate, endDate, type);
if (energyPre.size() == 0) {
return null;
}
String[] curDate = energyPre.stream()
.map(EnergyPreEchartDataDTO::getCurDate)
.toArray(String[]::new); // 使用stream和map转换每个对象的特定字段为JSON字符串,然后转换成数组
String[] curData = energyPre.stream()
.map(EnergyPreEchartDataDTO::getCurData)
.toArray(String[]::new); // 使用stream和map转换每个对象的特定字段为JSON字符串,然后转换成数组
String[] preData = energyPre.stream()
.map(EnergyPreEchartDataDTO::getPreData)
.toArray(String[]::new); // 使用stream和map转换每个对象的特定字段为JSON字符串,然后转换成数组
String[] errorData = energyPre.stream()
.map(EnergyPreEchartDataDTO::getErrorData)
.toArray(String[]::new); // 使用stream和map转换每个对象的特定字段为JSON字符串,然后转换成数组
List<HashMap<String, Object>> resultList = new ArrayList<>();
HashMap<String, Object> resultHashMap = new HashMap<>();
resultHashMap.put("curDate", curDate);
resultHashMap.put("curData", curData);
resultHashMap.put("preData", preData);
resultHashMap.put("errorData", errorData);
resultList.add(resultHashMap);
return resultList;
}
@Override
public List<EnergyPreTopDataDTO> getTopData(String buildingId, String type) {
// 获取顶部数据(昨日,昨日预测,今日预测,昨日偏差值)
return historyDataPreMapper.getTopData(buildingId, type);
}
} }

11
user-service/src/test/java/com/mh/user/UserServiceApplicationTests.java

@ -7,6 +7,7 @@ import com.mh.user.job.DealDataJob;
import com.mh.user.serialport.SerialPortThread; import com.mh.user.serialport.SerialPortThread;
import com.mh.user.service.DeviceManageService; import com.mh.user.service.DeviceManageService;
import com.mh.user.service.GaugeService; import com.mh.user.service.GaugeService;
import com.mh.user.service.HistoryDataPreService;
import com.mh.user.strategy.PressureTransStrategy; import com.mh.user.strategy.PressureTransStrategy;
import com.mh.user.utils.*; import com.mh.user.utils.*;
import org.checkerframework.checker.units.qual.A; import org.checkerframework.checker.units.qual.A;
@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
@ -134,4 +136,13 @@ class UserServiceApplicationTests {
dealDataJob.dealData(); dealDataJob.dealData();
} }
@Autowired
private HistoryDataPreService historyDataPreService;
@Test
public void testHistoryDataPre() {
List<HashMap<String, Object>> energyPre = historyDataPreService.getEnergyPre("21", null, null, "1");
System.out.println(energyPre);
}
} }

Loading…
Cancel
Save