diff --git a/user-service/src/main/java/com/mh/user/controller/EnergyPreController.java b/user-service/src/main/java/com/mh/user/controller/EnergyPreController.java index bf38d45..1527162 100644 --- a/user-service/src/main/java/com/mh/user/controller/EnergyPreController.java +++ b/user-service/src/main/java/com/mh/user/controller/EnergyPreController.java @@ -1,8 +1,14 @@ 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.RestController; +import javax.annotation.Resource; + /** * @author LJF * @version 1.0 @@ -11,8 +17,20 @@ import org.springframework.web.bind.annotation.RestController; * @date 2024-05-09 17:24:48 */ @RestController -@RequestMapping("/energy_pre") +@RequestMapping("/energyPre") 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)); + } } diff --git a/user-service/src/main/java/com/mh/user/dto/EnergyPreDTO.java b/user-service/src/main/java/com/mh/user/dto/EnergyPreDTO.java index 5dc4120..4028287 100644 --- a/user-service/src/main/java/com/mh/user/dto/EnergyPreDTO.java +++ b/user-service/src/main/java/com/mh/user/dto/EnergyPreDTO.java @@ -1,5 +1,8 @@ package com.mh.user.dto; +import lombok.Getter; +import lombok.Setter; + /** * @author LJF * @version 1.0 @@ -7,9 +10,18 @@ package com.mh.user.dto; * @description 用能预测前端类 * @date 2024-05-09 17:31:27 */ +@Setter +@Getter public class EnergyPreDTO { - private String buildingId; + /** + * 顶部数据 + */ + private EnergyPreTopDataDTO topData; + /** + * 折线图数据 + */ + private EnergyPreEchartDataDTO echartData; } diff --git a/user-service/src/main/java/com/mh/user/dto/EnergyPreEchartDataDTO.java b/user-service/src/main/java/com/mh/user/dto/EnergyPreEchartDataDTO.java new file mode 100644 index 0000000..d83fc8d --- /dev/null +++ b/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; + +} diff --git a/user-service/src/main/java/com/mh/user/dto/EnergyPreTopDataDTO.java b/user-service/src/main/java/com/mh/user/dto/EnergyPreTopDataDTO.java new file mode 100644 index 0000000..84680b9 --- /dev/null +++ b/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; + +} 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 0736e1a..875e2de 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 @@ -1,5 +1,7 @@ package com.mh.user.mapper; +import com.mh.user.dto.EnergyPreEchartDataDTO; +import com.mh.user.dto.EnergyPreTopDataDTO; import com.mh.user.entity.HistoryDataPre; import org.apache.ibatis.annotations.*; import org.springframework.security.core.parameters.P; @@ -118,4 +120,104 @@ public interface HistoryDataPreMapper extends BaseMapper { @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, @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("") + List 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("") + List getEnergyPre(@Param("buildingId") String buildingId, + @Param("beginDate") String beginDate, + @Param("endDate") String endDate, + @Param("type") String type); } diff --git a/user-service/src/main/java/com/mh/user/service/HistoryDataPreService.java b/user-service/src/main/java/com/mh/user/service/HistoryDataPreService.java index 8f9263c..62a2ba6 100644 --- a/user-service/src/main/java/com/mh/user/service/HistoryDataPreService.java +++ b/user-service/src/main/java/com/mh/user/service/HistoryDataPreService.java @@ -1,7 +1,11 @@ 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 java.util.HashMap; import java.util.List; /** @@ -36,4 +40,15 @@ public interface HistoryDataPreService { */ List getRecentData(String buildingId, String curDate); + /** + * 获取预测数据 + * @param buildingId + * @param beginDate + * @param endDate + * @param type + * @return + */ + List> getEnergyPre(String buildingId, String beginDate, String endDate, String type); + + List getTopData(String buildingId, String type); } 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 932ffaa..7533eb6 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 @@ -3,6 +3,7 @@ package com.mh.user.service.impl; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.JSONWriter; import com.github.benmanes.caffeine.cache.Cache; import com.mh.algorithm.bpnn.BPModel; 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.utils.CsvInfo; 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.SysParamEntity; import com.mh.user.mapper.HistoryDataPreMapper; @@ -22,7 +27,10 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; /** @@ -160,4 +168,51 @@ public class HistoryDataPreServiceImpl implements HistoryDataPreService { public List getRecentData(String buildingId, String curDate) { return historyDataPreMapper.getRecentData(buildingId, curDate); } + + @Override + public List> 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 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> resultList = new ArrayList<>(); + HashMap 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 getTopData(String buildingId, String type) { + // 获取顶部数据(昨日,昨日预测,今日预测,昨日偏差值) + return historyDataPreMapper.getTopData(buildingId, type); + } } diff --git a/user-service/src/test/java/com/mh/user/UserServiceApplicationTests.java b/user-service/src/test/java/com/mh/user/UserServiceApplicationTests.java index ce7e0bb..2353e64 100644 --- a/user-service/src/test/java/com/mh/user/UserServiceApplicationTests.java +++ b/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.service.DeviceManageService; import com.mh.user.service.GaugeService; +import com.mh.user.service.HistoryDataPreService; import com.mh.user.strategy.PressureTransStrategy; import com.mh.user.utils.*; 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 java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadPoolExecutor; @@ -134,4 +136,13 @@ class UserServiceApplicationTests { dealDataJob.dealData(); } + @Autowired + private HistoryDataPreService historyDataPreService; + + @Test + public void testHistoryDataPre() { + List> energyPre = historyDataPreService.getEnergyPre("21", null, null, "1"); + System.out.println(energyPre); + } + }