|
@@ -1,11 +1,20 @@
|
|
|
package org.springblade.business.controller;
|
|
package org.springblade.business.controller;
|
|
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
+import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
import io.swagger.annotations.*;
|
|
import io.swagger.annotations.*;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.codec.Charsets;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
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.service.impl.WeatherInfoServiceImpl;
|
|
|
|
|
+import org.springblade.business.vo.NeiYeLedgerVO1;
|
|
|
import org.springblade.core.mp.support.Query;
|
|
import org.springblade.core.mp.support.Query;
|
|
|
import org.springblade.core.secure.BladeUser;
|
|
import org.springblade.core.secure.BladeUser;
|
|
|
import org.springblade.core.secure.utils.AuthUtil;
|
|
import org.springblade.core.secure.utils.AuthUtil;
|
|
@@ -18,10 +27,19 @@ import org.springblade.business.service.WeatherInfoService;
|
|
|
import org.springblade.manager.entity.ProjectContractArea;
|
|
import org.springblade.manager.entity.ProjectContractArea;
|
|
|
import org.springblade.manager.feign.ProjectContractAreaClient;
|
|
import org.springblade.manager.feign.ProjectContractAreaClient;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
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.springframework.web.bind.annotation.*;
|
|
|
import org.springblade.core.mp.support.Condition;
|
|
import org.springblade.core.mp.support.Condition;
|
|
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -141,7 +159,84 @@ public class WeatherController {
|
|
|
return R.data(this.weatherInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(weatherMap, WeatherInfo.class).lambda().orderByDesc(WeatherInfo::getRecordTime)));
|
|
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 void downloadWeatherExcel(
|
|
|
|
|
+ @RequestParam String contractId,
|
|
|
|
|
+ @RequestParam String startTime,
|
|
|
|
|
+ @RequestParam String endTime,
|
|
|
|
|
+ HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
+ // 1. 参数校验
|
|
|
|
|
+ if (contractId == null || contractId.isEmpty()) {
|
|
|
|
|
+ throw new IllegalArgumentException("合同ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (startTime == null || startTime.isEmpty() || endTime == null || endTime.isEmpty()) {
|
|
|
|
|
+ throw new IllegalArgumentException("开始/结束时间不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 查询业务数据
|
|
|
|
|
+ ProjectContractArea area = this.projectContractAreaClient.queryContractAreaByContractId(contractId);
|
|
|
|
|
+ if (area == null || area.getId() == null) {
|
|
|
|
|
+ throw new RuntimeException("未查询到合同对应的区域信息");
|
|
|
|
|
+ }
|
|
|
|
|
+ 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";
|
|
|
|
|
+ File templateFile = new File(templatePath);
|
|
|
|
|
+ if (!templateFile.exists() || !templateFile.isFile()) {
|
|
|
|
|
+ throw new RuntimeException("Excel模板文件不存在:" + templatePath);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 读取并填充Excel模板
|
|
|
|
|
+ try (InputStream templateStream = new FileInputStream(templateFile);
|
|
|
|
|
+ Workbook workbook = WorkbookFactory.create(templateStream);
|
|
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
|
|
|
|
+
|
|
|
|
|
+ // 获取/创建工作表
|
|
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
+ if (sheet == null) {
|
|
|
|
|
+ sheet = workbook.createSheet("WeatherRecord");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 填充数据(防Null)
|
|
|
|
|
+ int startRow = 1;
|
|
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
+ Row row = sheet.createRow(startRow + i);
|
|
|
|
|
+ WeatherInfo info = list.get(i);
|
|
|
|
|
+ String dateStr = info.getRecordTime() != null ? DateUtil.format(info.getRecordTime(), "yyyy-MM-dd") : "";
|
|
|
|
|
+
|
|
|
|
|
+ row.createCell(0).setCellValue(dateStr);
|
|
|
|
|
+ row.createCell(1).setCellValue(info.getWeather() == null ? "" : info.getWeather());
|
|
|
|
|
+ row.createCell(2).setCellValue((info.getTempLow() == null ? "" : info.getTempLow()) + "~" + (info.getTempHigh() == null ? "" : info.getTempHigh()));
|
|
|
|
|
+ row.createCell(3).setCellValue(info.getAirTemp() == null ? "" : info.getAirTemp());
|
|
|
|
|
+ row.createCell(4).setCellValue(info.getWindLevel() == null ? "" : info.getWindLevel());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 写入字节流
|
|
|
|
|
+ workbook.write(outputStream);
|
|
|
|
|
+ // ========== 核心:完全照搬你成功的代码逻辑 ==========
|
|
|
|
|
+ // 1. 只编码中文核心名称(和你"工程划分导入模版"的编码方式一致)
|
|
|
|
|
+ String time=startTime.replaceAll("-","")+"~"+endTime.replaceAll("-","");
|
|
|
|
|
+ String fileName = URLEncoder.encode(time+"天气台账", StandardCharsets.UTF_8.name());
|
|
|
|
|
+ // 2. 删掉response.reset()(你成功的代码里没有,避免清空关键头)
|
|
|
|
|
+ // 3. Content-Type和你成功的代码完全一致(用vnd.ms-excel,不要用xlsx专属的)
|
|
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
|
|
+ response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
|
|
|
|
+ // 4. 响应头用小写Content-disposition + 只保留filename参数 + 拼接.xlsx后缀(和你完全一致)
|
|
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
|
|
|
|
+
|
|
|
|
|
+ // 5. 用BufferedOutputStream包装(和你成功的代码一致,提升流稳定性)
|
|
|
|
|
+ OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
|
|
|
+ toClient.write(outputStream.toByteArray());
|
|
|
|
|
+ toClient.flush();
|
|
|
|
|
+ toClient.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
/**
|
|
/**
|
|
|
* 查询单个数据
|
|
* 查询单个数据
|
|
|
*
|
|
*
|