diff --git a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/controller/CarController.java b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/controller/CarController.java index 382af2b..658d496 100644 --- a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/controller/CarController.java +++ b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/controller/CarController.java @@ -252,4 +252,11 @@ public class CarController extends BladeController { public void exportCarCheckRecordData(@RequestParam Long id, HttpServletResponse response) { carCheckRecordService.exportCarCheckRecordData(id, response); } + + @GetMapping("/exportCarMilesData") + @ApiOperation(value = "导出年度里程数据") + @ApiOperationSupport(order = 27) + public void exportCarMilesData(@ApiIgnore @RequestParam Map param, Query query, HttpServletResponse response) { + carService.exportCarMilesData(param, query, response); + } } diff --git a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/ICarService.java b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/ICarService.java index ca8f087..21679b1 100644 --- a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/ICarService.java +++ b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/ICarService.java @@ -7,7 +7,9 @@ import com.hnac.hzims.safeproduct.vo.CarMilesYearVO; import org.springblade.core.mp.support.Query; import org.springblade.core.tool.api.R; +import javax.servlet.http.HttpServletResponse; import java.util.List; +import java.util.Map; /** * 车辆管理服务类 @@ -62,4 +64,12 @@ public interface ICarService extends IService { * @return true-成功,false-失败 */ boolean removeCar(Long id); + + /** + * 导出年度里程数据 + * @param param 参数 + * @param query 分页类 + * @param response 响应类 + */ + void exportCarMilesData(Map param, Query query, HttpServletResponse response); } diff --git a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/CarServiceImpl.java b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/CarServiceImpl.java index c26ddcb..65d920f 100644 --- a/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/CarServiceImpl.java +++ b/hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/CarServiceImpl.java @@ -1,17 +1,25 @@ package com.hnac.hzims.safeproduct.service.impl; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.converters.longconverter.LongStringConverter; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.hnac.hzims.safeproduct.dto.CarMilesYearDTO; +import com.hnac.hzims.safeproduct.dto.RehearsalExportDTO; import com.hnac.hzims.safeproduct.entity.CarEntity; +import com.hnac.hzims.safeproduct.enums.RehearsalStatusEnum; import com.hnac.hzims.safeproduct.mapper.CarMapper; import com.hnac.hzims.safeproduct.service.ICarCheckRecordService; import com.hnac.hzims.safeproduct.service.ICarMaintenanceService; import com.hnac.hzims.safeproduct.service.ICarService; import com.hnac.hzims.safeproduct.service.ICarUsedRecordService; import com.hnac.hzims.safeproduct.vo.CarMilesYearVO; +import org.springblade.core.log.exception.ServiceException; import org.springblade.core.mp.support.Query; import org.springblade.core.tool.api.R; import org.springframework.beans.factory.annotation.Autowired; @@ -19,9 +27,14 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; import java.math.BigDecimal; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -194,4 +207,50 @@ public class CarServiceImpl extends ServiceImpl implements } return false; } + + /** + * 导出年度里程数据 + */ + @Override + public void exportCarMilesData(Map param, Query query, HttpServletResponse response) { + ServletOutputStream outputStream = null; + try { + outputStream = response.getOutputStream(); + String unit = String.valueOf(param.get("unit")); + String year = String.valueOf(param.get("year")); + IPage page = milesDataByYear(year, unit, query); + List list = page.getRecords(); + // 设置响应头 + // URLEncoder.encode防止中文乱码 + String fileName = URLEncoder.encode("年度里程统计表", "UTF-8"); + response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); + response.setContentType("application/vnd.ms-excel"); + response.setCharacterEncoding("UTF-8"); + // ExcelWriter初始化 + ExcelWriter excelWriter = EasyExcel + .write(response.getOutputStream()) + .autoCloseStream(Boolean.TRUE) + .registerConverter(new LongStringConverter()) + .registerWriteHandler(new SimpleColumnWidthStyleStrategy(25)) + .build(); + WriteSheet writeSheet = EasyExcel.writerSheet(1, "年度里程统计表").head(CarMilesYearVO.class) + .build(); + excelWriter.write(list, writeSheet); + excelWriter.finish(); + } catch (Exception e) { + // 重置response + response.reset(); + response.setContentType("application/json"); + response.setCharacterEncoding("utf-8"); + throw new ServiceException("年度里程数据导出异常: " + e.getMessage()); + } finally { + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + log.error("年度里程数据导出-输出流关闭异常: " + e.getMessage()); + } + } + } + } }