|
|
@@ -1,12 +1,20 @@
|
|
|
package org.springblade.business.controller;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
import io.swagger.annotations.*;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
import org.springblade.business.service.impl.WeatherInfoServiceImpl;
|
|
|
+import org.springblade.business.vo.NeiYeLedgerVO1;
|
|
|
import org.springblade.core.mp.support.Query;
|
|
|
import org.springblade.core.secure.BladeUser;
|
|
|
import org.springblade.core.secure.utils.AuthUtil;
|
|
|
@@ -19,9 +27,19 @@ import org.springblade.business.service.WeatherInfoService;
|
|
|
import org.springblade.manager.entity.ProjectContractArea;
|
|
|
import org.springblade.manager.feign.ProjectContractAreaClient;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.core.io.ByteArrayResource;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springblade.core.mp.support.Condition;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
@@ -145,6 +163,68 @@ public class WeatherController {
|
|
|
return R.data(this.weatherInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(weatherMap, WeatherInfo.class).lambda().orderByDesc(WeatherInfo::getRecordTime)));
|
|
|
}
|
|
|
}
|
|
|
+ @SneakyThrows
|
|
|
+ @PostMapping(value = "/downloadWeatherExcel")
|
|
|
+ @ApiOperationSupport(order = 13)
|
|
|
+ @ApiOperation(value = "客户端-下载元素表对应的excel模板")
|
|
|
+ public ResponseEntity<Resource> downloadNeiYe(
|
|
|
+ @RequestParam String contract,
|
|
|
+ @RequestParam String startTime,
|
|
|
+ @RequestParam String endTime,
|
|
|
+ HttpServletResponse response) {
|
|
|
+
|
|
|
+ // 1. 查询区域信息
|
|
|
+ ProjectContractArea area = this.projectContractAreaClient.queryContractAreaByContractId(contract);
|
|
|
+ // 2. 查询天气数据
|
|
|
+ List<WeatherInfo> list = this.weatherInfoService.getWeatherInfoListByRecordTime(area.getId(), startTime, endTime);
|
|
|
+
|
|
|
+ // 3. 模板文件路径
|
|
|
+ String templatePath = "/mnt/sdc/Users/hongchuangyanfa/Desktop/excel/weather.xlsx";
|
|
|
+ // String templatePath = "C:\\upload\\excel\\weather.xlsx"; // 本地测试路径
|
|
|
+
|
|
|
+ // 4. 读取模板文件(使用try-with-resources自动关闭流)
|
|
|
+ try (InputStream templateStream = new FileInputStream(new File(templatePath));
|
|
|
+ Workbook workbook = WorkbookFactory.create(templateStream);
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
|
|
+
|
|
|
+ // 5. 获取第一个工作表
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+ if (sheet == null) {
|
|
|
+ sheet = workbook.createSheet("天气台账"); // 防止模板sheet不存在
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 填充数据(从第2行开始,行索引从0开始)
|
|
|
+ int startRow = 1;
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ Row row = sheet.createRow(startRow + i);
|
|
|
+ WeatherInfo info = list.get(i);
|
|
|
+
|
|
|
+ // 格式化日期
|
|
|
+ String dateStr = DateUtil.format(info.getRecordTime(), "yyyy-MM-dd");
|
|
|
+
|
|
|
+ // 填充各列数据
|
|
|
+ row.createCell(0).setCellValue(dateStr); // 日期
|
|
|
+ row.createCell(1).setCellValue(info.getWeather()); // 天气
|
|
|
+ row.createCell(2).setCellValue(info.getTempLow() + "~" + info.getTempHigh()); // 温度
|
|
|
+ row.createCell(3).setCellValue(info.getAirTemp()); // 平均温度
|
|
|
+ row.createCell(4).setCellValue(info.getWindLevel()); // 风力
|
|
|
+ }
|
|
|
+
|
|
|
+ // 7. 写入字节流
|
|
|
+ workbook.write(outputStream);
|
|
|
+ // 8. 构建字节数组资源
|
|
|
+ ByteArrayResource resource = new ByteArrayResource(outputStream.toByteArray());
|
|
|
+
|
|
|
+ // 9. 构建响应(解决中文文件名乱码问题)
|
|
|
+ String fileName = new String("天气台账.xlsx".getBytes("UTF-8"), "ISO-8859-1");
|
|
|
+
|
|
|
+ return ResponseEntity.ok()
|
|
|
+ .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName)
|
|
|
+ .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
|
|
+ .contentLength(resource.contentLength())
|
|
|
+ .body(resource);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 查询单个数据
|