WeatherStationStatusDataController.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package com.jiayue.ipp.idp.controller;
  2. import cn.hutool.core.io.IoUtil;
  3. import cn.hutool.poi.excel.ExcelUtil;
  4. import cn.hutool.poi.excel.ExcelWriter;
  5. import com.baomidou.mybatisplus.core.metadata.OrderItem;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.jiayue.ipp.common.data.entity.WeatherStationStatusData;
  9. import com.jiayue.ipp.idp.service.WeatherStationStatusDataService;
  10. import com.jiayue.ipp.idp.util.R;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import lombok.RequiredArgsConstructor;
  14. import org.springframework.security.access.prepost.PreAuthorize;
  15. import org.springframework.web.bind.annotation.*;
  16. import javax.servlet.ServletOutputStream;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.math.BigDecimal;
  19. import java.net.URLEncoder;
  20. import java.text.SimpleDateFormat;
  21. import java.util.*;
  22. /**
  23. * idp_weather_station_status_data
  24. *
  25. * @author whc
  26. * @date 2022-03-18 15:49:50
  27. */
  28. @RestController
  29. @RequiredArgsConstructor
  30. @RequestMapping("/weatherstationstatusdata")
  31. @Api(value = "weatherstationstatusdata", tags = "idp_weather_station_status_data管理")
  32. public class WeatherStationStatusDataController {
  33. private final WeatherStationStatusDataService weatherStationStatusDataService;
  34. /**
  35. * 分页查询
  36. *
  37. * @param page 分页对象
  38. * @param weatherStationStatusData idp_weather_station_status_data
  39. * @return
  40. */
  41. @ApiOperation(value = "分页查询", notes = "分页查询")
  42. @GetMapping("/page")
  43. public R getWeatherStationStatusDataPage(Page page, WeatherStationStatusData weatherStationStatusData) {
  44. return R.ok(weatherStationStatusDataService.page(page, Wrappers.query(weatherStationStatusData)).addOrder(OrderItem.asc("time")));
  45. }
  46. /**
  47. * 通过id查询idp_weather_station_status_data
  48. *
  49. * @param id id
  50. * @return R
  51. */
  52. @ApiOperation(value = "通过id查询", notes = "通过id查询")
  53. @GetMapping("/{id}")
  54. public R getById(@PathVariable("id") String id) {
  55. return R.ok(weatherStationStatusDataService.getById(id));
  56. }
  57. /**
  58. * 新增idp_weather_station_status_data
  59. *
  60. * @param weatherStationStatusData idp_weather_station_status_data
  61. * @return R
  62. */
  63. @ApiOperation(value = "新增idp_weather_station_status_data", notes = "新增idp_weather_station_status_data")
  64. @PostMapping
  65. public R save(@RequestBody WeatherStationStatusData weatherStationStatusData) {
  66. return R.ok(weatherStationStatusDataService.save(weatherStationStatusData));
  67. }
  68. /**
  69. * 修改idp_weather_station_status_data
  70. *
  71. * @param weatherStationStatusData idp_weather_station_status_data
  72. * @return R
  73. */
  74. @ApiOperation(value = "修改idp_weather_station_status_data", notes = "修改idp_weather_station_status_data")
  75. @PutMapping
  76. public R updateById(@RequestBody WeatherStationStatusData weatherStationStatusData) {
  77. return R.ok(weatherStationStatusDataService.updateById(weatherStationStatusData));
  78. }
  79. /**
  80. * 通过id删除idp_weather_station_status_data
  81. *
  82. * @param id id
  83. * @return R
  84. */
  85. @ApiOperation(value = "通过id删除idp_weather_station_status_data", notes = "通过id删除idp_weather_station_status_data")
  86. @DeleteMapping("/{id}")
  87. public R removeById(@PathVariable String id) {
  88. return R.ok(weatherStationStatusDataService.removeById(id));
  89. }
  90. @ApiOperation(value = "根据条件查询", notes = "查询")
  91. @PostMapping("/queryCharts")
  92. public R queryCharts(String stationCode, Long startTime, Long endTime, String equipmentId) {
  93. return R.ok(weatherStationStatusDataService.findByStationCodeAndeTimeBetweenAndEquipmentId(stationCode, new Date(startTime), new Date(endTime), equipmentId));
  94. }
  95. @ApiOperation(value = "根据条件查询", notes = "分页查询")
  96. @PostMapping("/queryTableData")
  97. public R queryTableData(Long currentPage, Long pageSize, String stationCode, Long startTime, Long endTime, String equipmentId) {
  98. Page page = new Page(currentPage, pageSize);
  99. page.addOrder(OrderItem.asc("time"));
  100. page.setMaxLimit((long) -1);
  101. return R.ok(weatherStationStatusDataService.page(page, weatherStationStatusDataService.getPageByStationCodeAndeTimeBetweenAndEquipmentId(stationCode, new Date(startTime), new Date(endTime), equipmentId)));
  102. }
  103. @GetMapping("/exportDataEvent/{stationCode}/{startTime}/{endTime}/{equipmentId}")
  104. public R exportDataEvent(
  105. @PathVariable("stationCode") String stationCode,
  106. @PathVariable("startTime") Long startTime,
  107. @PathVariable("endTime") Long endTime,
  108. @PathVariable("equipmentId") String equipmentId,
  109. HttpServletResponse response
  110. ){
  111. try {
  112. Map<String , List> dataMap = weatherStationStatusDataService.getCompositeData(stationCode,new Date(startTime),new Date(endTime),equipmentId);
  113. List<String> headers = Arrays.asList(
  114. "所属场站", "设备名称", "设备ID", "时间",
  115. "总辐射(W/㎡)", "直接辐射(W/㎡)", "散射辐射(W/㎡)",
  116. "环境温度(℃)", "气压(KPa)", "湿度(%)",
  117. "光伏电池板温度(℃)", "风速(m/s)", "风向(°)"
  118. );
  119. // 3. 初始化Excel数据容器并添加表头
  120. List<List<?>> excelData = new ArrayList<>();
  121. excelData.add(headers);
  122. // 4. 从数据映射中获取各字段列表
  123. List<?> stationNameList = dataMap.get("stationName"); // 所属场站名称
  124. List<?> equipmentNameList = dataMap.get("equipmentName"); // 设备名称
  125. List<?> equipmentIdList = dataMap.get("equipmentId"); // 设备ID
  126. List<?> timeList = dataMap.get("time"); // 时间
  127. List<?> globalRList = dataMap.get("globalR"); // 总辐射
  128. List<?> directRList = dataMap.get("directR"); // 直接辐射
  129. List<?> diffuseRList = dataMap.get("diffuseR"); // 散射辐射
  130. List<?> airTList = dataMap.get("airT"); // 环境温度
  131. List<?> pList = dataMap.get("p"); // 气压
  132. List<?> rhList = dataMap.get("rh"); // 湿度
  133. List<?> cellTList = dataMap.get("cellT"); // 光伏电池板温度
  134. List<?> wsList = dataMap.get("ws"); // 风速
  135. List<?> wdList = dataMap.get("wd"); // 风向
  136. // 5. 确定数据总行数(以设备ID列表为准)
  137. int rowCount = equipmentIdList != null ? equipmentIdList.size() : 0;
  138. // 6. 逐行填充数据
  139. for (int i = 0; i < rowCount; i++) {
  140. List<Object> row = new ArrayList<>();
  141. row.add(getSafeValue(stationNameList, i)); // 所属场站
  142. row.add(getSafeValue(equipmentNameList, i)); // 设备名称
  143. row.add(getSafeValue(equipmentIdList, i)); // 设备ID
  144. row.add(getSafeValue(timeList, i)); // 时间
  145. row.add(getSafeValue(globalRList, i)); // 总辐射(W/㎡)
  146. row.add(getSafeValue(directRList, i)); // 直接辐射(W/㎡)
  147. row.add(getSafeValue(diffuseRList, i)); // 散射辐射(W/㎡)
  148. row.add(getSafeValue(airTList, i)); // 环境温度(℃)
  149. row.add(getSafeValue(pList, i)); // 气压(KPa)
  150. row.add(getSafeValue(rhList, i)); // 湿度(%)
  151. row.add(getSafeValue(cellTList, i)); // 光伏电池板温度(℃)
  152. row.add(getSafeValue(wsList, i)); // 风速(m/s)
  153. row.add(getSafeValue(wdList, i)); // 风向(°)
  154. excelData.add(row);
  155. }
  156. ExcelWriter writer = ExcelUtil.getWriter(true);
  157. writer.renameSheet(0,"气象站数据");
  158. writer.write(excelData,true);
  159. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  160. String fileName = "气象站数据_" + sdf.format(startTime) + "至" + sdf.format(endTime) + ".xlsx";
  161. response.setContentType("application/x-msdownload;charset=utf-8");
  162. response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
  163. ServletOutputStream out = response.getOutputStream();
  164. writer.flush(out , true);
  165. writer.close();
  166. IoUtil.close(out);
  167. }catch (Exception e){
  168. e.printStackTrace();
  169. }
  170. return R.ok();
  171. }
  172. private Object getSafeValue(List<?> list, int index) {
  173. if (list == null || index < 0 || index >= list.size()) {
  174. return ""; // 空值仍返回空字符串
  175. }
  176. Object value = list.get(index);
  177. // 对BigDecimal做去尾零处理
  178. if (value instanceof BigDecimal) {
  179. return ((BigDecimal) value).stripTrailingZeros();
  180. }
  181. return value == null ? "" : value;
  182. }
  183. }