123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package com.jiayue.ipp.idp.controller;
- import cn.hutool.core.io.IoUtil;
- import cn.hutool.poi.excel.ExcelUtil;
- import cn.hutool.poi.excel.ExcelWriter;
- import com.baomidou.mybatisplus.core.metadata.OrderItem;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.jiayue.ipp.common.data.entity.WeatherStationStatusData;
- import com.jiayue.ipp.idp.service.WeatherStationStatusDataService;
- import com.jiayue.ipp.idp.util.R;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.RequiredArgsConstructor;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServletResponse;
- import java.math.BigDecimal;
- import java.net.URLEncoder;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * idp_weather_station_status_data
- *
- * @author whc
- * @date 2022-03-18 15:49:50
- */
- @RestController
- @RequiredArgsConstructor
- @RequestMapping("/weatherstationstatusdata")
- @Api(value = "weatherstationstatusdata", tags = "idp_weather_station_status_data管理")
- public class WeatherStationStatusDataController {
- private final WeatherStationStatusDataService weatherStationStatusDataService;
- /**
- * 分页查询
- *
- * @param page 分页对象
- * @param weatherStationStatusData idp_weather_station_status_data
- * @return
- */
- @ApiOperation(value = "分页查询", notes = "分页查询")
- @GetMapping("/page")
- public R getWeatherStationStatusDataPage(Page page, WeatherStationStatusData weatherStationStatusData) {
- return R.ok(weatherStationStatusDataService.page(page, Wrappers.query(weatherStationStatusData)).addOrder(OrderItem.asc("time")));
- }
- /**
- * 通过id查询idp_weather_station_status_data
- *
- * @param id id
- * @return R
- */
- @ApiOperation(value = "通过id查询", notes = "通过id查询")
- @GetMapping("/{id}")
- public R getById(@PathVariable("id") String id) {
- return R.ok(weatherStationStatusDataService.getById(id));
- }
- /**
- * 新增idp_weather_station_status_data
- *
- * @param weatherStationStatusData idp_weather_station_status_data
- * @return R
- */
- @ApiOperation(value = "新增idp_weather_station_status_data", notes = "新增idp_weather_station_status_data")
- @PostMapping
- public R save(@RequestBody WeatherStationStatusData weatherStationStatusData) {
- return R.ok(weatherStationStatusDataService.save(weatherStationStatusData));
- }
- /**
- * 修改idp_weather_station_status_data
- *
- * @param weatherStationStatusData idp_weather_station_status_data
- * @return R
- */
- @ApiOperation(value = "修改idp_weather_station_status_data", notes = "修改idp_weather_station_status_data")
- @PutMapping
- public R updateById(@RequestBody WeatherStationStatusData weatherStationStatusData) {
- return R.ok(weatherStationStatusDataService.updateById(weatherStationStatusData));
- }
- /**
- * 通过id删除idp_weather_station_status_data
- *
- * @param id id
- * @return R
- */
- @ApiOperation(value = "通过id删除idp_weather_station_status_data", notes = "通过id删除idp_weather_station_status_data")
- @DeleteMapping("/{id}")
- public R removeById(@PathVariable String id) {
- return R.ok(weatherStationStatusDataService.removeById(id));
- }
- @ApiOperation(value = "根据条件查询", notes = "查询")
- @PostMapping("/queryCharts")
- public R queryCharts(String stationCode, Long startTime, Long endTime, String equipmentId) {
- return R.ok(weatherStationStatusDataService.findByStationCodeAndeTimeBetweenAndEquipmentId(stationCode, new Date(startTime), new Date(endTime), equipmentId));
- }
- @ApiOperation(value = "根据条件查询", notes = "分页查询")
- @PostMapping("/queryTableData")
- public R queryTableData(Long currentPage, Long pageSize, String stationCode, Long startTime, Long endTime, String equipmentId) {
- Page page = new Page(currentPage, pageSize);
- page.addOrder(OrderItem.asc("time"));
- page.setMaxLimit((long) -1);
- return R.ok(weatherStationStatusDataService.page(page, weatherStationStatusDataService.getPageByStationCodeAndeTimeBetweenAndEquipmentId(stationCode, new Date(startTime), new Date(endTime), equipmentId)));
- }
- @GetMapping("/exportDataEvent/{stationCode}/{startTime}/{endTime}/{equipmentId}")
- public R exportDataEvent(
- @PathVariable("stationCode") String stationCode,
- @PathVariable("startTime") Long startTime,
- @PathVariable("endTime") Long endTime,
- @PathVariable("equipmentId") String equipmentId,
- HttpServletResponse response
- ){
- try {
- Map<String , List> dataMap = weatherStationStatusDataService.getCompositeData(stationCode,new Date(startTime),new Date(endTime),equipmentId);
- List<String> headers = Arrays.asList(
- "所属场站", "设备名称", "设备ID", "时间",
- "总辐射(W/㎡)", "直接辐射(W/㎡)", "散射辐射(W/㎡)",
- "环境温度(℃)", "气压(KPa)", "湿度(%)",
- "光伏电池板温度(℃)", "风速(m/s)", "风向(°)"
- );
- // 3. 初始化Excel数据容器并添加表头
- List<List<?>> excelData = new ArrayList<>();
- excelData.add(headers);
- // 4. 从数据映射中获取各字段列表
- List<?> stationNameList = dataMap.get("stationName"); // 所属场站名称
- List<?> equipmentNameList = dataMap.get("equipmentName"); // 设备名称
- List<?> equipmentIdList = dataMap.get("equipmentId"); // 设备ID
- List<?> timeList = dataMap.get("time"); // 时间
- List<?> globalRList = dataMap.get("globalR"); // 总辐射
- List<?> directRList = dataMap.get("directR"); // 直接辐射
- List<?> diffuseRList = dataMap.get("diffuseR"); // 散射辐射
- List<?> airTList = dataMap.get("airT"); // 环境温度
- List<?> pList = dataMap.get("p"); // 气压
- List<?> rhList = dataMap.get("rh"); // 湿度
- List<?> cellTList = dataMap.get("cellT"); // 光伏电池板温度
- List<?> wsList = dataMap.get("ws"); // 风速
- List<?> wdList = dataMap.get("wd"); // 风向
- // 5. 确定数据总行数(以设备ID列表为准)
- int rowCount = equipmentIdList != null ? equipmentIdList.size() : 0;
- // 6. 逐行填充数据
- for (int i = 0; i < rowCount; i++) {
- List<Object> row = new ArrayList<>();
- row.add(getSafeValue(stationNameList, i)); // 所属场站
- row.add(getSafeValue(equipmentNameList, i)); // 设备名称
- row.add(getSafeValue(equipmentIdList, i)); // 设备ID
- row.add(getSafeValue(timeList, i)); // 时间
- row.add(getSafeValue(globalRList, i)); // 总辐射(W/㎡)
- row.add(getSafeValue(directRList, i)); // 直接辐射(W/㎡)
- row.add(getSafeValue(diffuseRList, i)); // 散射辐射(W/㎡)
- row.add(getSafeValue(airTList, i)); // 环境温度(℃)
- row.add(getSafeValue(pList, i)); // 气压(KPa)
- row.add(getSafeValue(rhList, i)); // 湿度(%)
- row.add(getSafeValue(cellTList, i)); // 光伏电池板温度(℃)
- row.add(getSafeValue(wsList, i)); // 风速(m/s)
- row.add(getSafeValue(wdList, i)); // 风向(°)
- excelData.add(row);
- }
- ExcelWriter writer = ExcelUtil.getWriter(true);
- writer.renameSheet(0,"气象站数据");
- writer.write(excelData,true);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String fileName = "气象站数据_" + sdf.format(startTime) + "至" + sdf.format(endTime) + ".xlsx";
- response.setContentType("application/x-msdownload;charset=utf-8");
- response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
- ServletOutputStream out = response.getOutputStream();
- writer.flush(out , true);
- writer.close();
- IoUtil.close(out);
- }catch (Exception e){
- e.printStackTrace();
- }
- return R.ok();
- }
- private Object getSafeValue(List<?> list, int index) {
- if (list == null || index < 0 || index >= list.size()) {
- return ""; // 空值仍返回空字符串
- }
- Object value = list.get(index);
- // 对BigDecimal做去尾零处理
- if (value instanceof BigDecimal) {
- return ((BigDecimal) value).stripTrailingZeros();
- }
- return value == null ? "" : value;
- }
- }
|