瀏覽代碼

天气台账下载

cr 1 天之前
父節點
當前提交
3cf9a99be6

+ 80 - 0
blade-service/blade-business/src/main/java/org/springblade/business/controller/WeatherController.java

@@ -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);
+        }
+    }
 
     /**
      * 查询单个数据

+ 2 - 0
blade-service/blade-business/src/main/java/org/springblade/business/mapper/WeatherInfoMapper.java

@@ -22,4 +22,6 @@ public interface WeatherInfoMapper extends BaseMapper<WeatherInfo> {
     List<LocalDate> getAllWeatherByAreaId(@Param("areaId") Long areaId);
 
     List<ContractInfoVO> getAllContract();
+
+    List<WeatherInfo> getWeatherInfoListByRecordTime(@Param("id") Long id, @Param("startTime") String startTime, @Param("endTime") String endTime);
 }

+ 3 - 0
blade-service/blade-business/src/main/java/org/springblade/business/mapper/WeatherInfoMapper.xml

@@ -47,6 +47,9 @@
         from m_project_info mpi left join m_contract_info mci on mpi.id = mci.p_id
         WHERE mpi.is_deleted = 0 and mci.is_deleted =0
     </select>
+    <select id="getWeatherInfoListByRecordTime" resultType="org.springblade.business.entity.WeatherInfo">
+        select * from u_weather_info where contract_area_id = #{id} and record_time between #{startTime} and #{endTime} order by record_time DESC
+    </select>
 
 
 </mapper>

+ 1 - 0
blade-service/blade-business/src/main/java/org/springblade/business/service/WeatherInfoService.java

@@ -13,4 +13,5 @@ public interface WeatherInfoService extends IService<WeatherInfo> {
      */
     List<Map<String, List<Map<String, Object>>>> queryWeatherAllByYear(String year, String contractId);
 
+    List<WeatherInfo> getWeatherInfoListByRecordTime(Long id, String startTime, String endTime);
 }

+ 5 - 0
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/WeatherInfoServiceImpl.java

@@ -117,6 +117,11 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
         return null;
     }
 
+    @Override
+    public List<WeatherInfo> getWeatherInfoListByRecordTime(Long id, String startTime, String endTime) {
+        return baseMapper.getWeatherInfoListByRecordTime(id, startTime, endTime);
+    }
+
     /**
      * 获取当前系统所有项目下所有合同段的当天天气
      */