提交 195d053e authored 作者: Matrix's avatar Matrix

[决算模块] 完成决算excel和分页

上级 4c4dd861
...@@ -5,6 +5,7 @@ target/ ...@@ -5,6 +5,7 @@ target/
!**/src/test/** !**/src/test/**
### STS ### ### STS ###
\~*.xlsx
.apt_generated .apt_generated
.classpath .classpath
.factorypath .factorypath
...@@ -29,3 +30,4 @@ build/ ...@@ -29,3 +30,4 @@ build/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
/dev-union/src/main/resources/excelTemplate/~$finalCheck.xlsx
...@@ -2,22 +2,33 @@ package com.tykj.dev.device.finalcheck.controller; ...@@ -2,22 +2,33 @@ package com.tykj.dev.device.finalcheck.controller;
import com.tykj.dev.config.swagger.AutoDocument; import com.tykj.dev.config.swagger.AutoDocument;
import com.tykj.dev.device.finalcheck.entity.other.TimeParam; import com.tykj.dev.device.finalcheck.entity.other.TimeParam;
import com.tykj.dev.device.finalcheck.entity.vo.FinalDetailVo;
import com.tykj.dev.device.finalcheck.entity.vo.FinalReportVo; import com.tykj.dev.device.finalcheck.entity.vo.FinalReportVo;
import com.tykj.dev.device.finalcheck.service.FinalCheckService; import com.tykj.dev.device.finalcheck.service.FinalCheckService;
import com.tykj.dev.misc.base.ResultObj; import com.tykj.dev.misc.base.ResultObj;
import com.tykj.dev.misc.exception.ApiException;
import com.tykj.dev.misc.utils.ResultUtil; import com.tykj.dev.misc.utils.ResultUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource; import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.OutputStream;
import java.util.List; import java.text.SimpleDateFormat;
import java.util.*;
import static com.tykj.dev.misc.utils.TimestampUtil.localDateToDate;
/** /**
* finalCheckController. * finalCheckController.
...@@ -35,10 +46,47 @@ public class FinalCheckController { ...@@ -35,10 +46,47 @@ public class FinalCheckController {
@Autowired @Autowired
private FinalCheckService fcService; private FinalCheckService fcService;
public static void downloadExcel(HttpServletRequest request, HttpServletResponse response, Workbook wb, String excelName) {
// 判断数据
if (wb == null) {
throw new ApiException("workbook can not be null!");
}
// 重置响应对象
response.reset();
// 当前日期,用于导出文件名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String dateStr = excelName + sdf.format(new Date()) + ".xls";
// 指定下载的文件名--设置响应头
response.addHeader("Content-Disposition", "attachment;filename=" + dateStr);
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// 写出数据输出流到页面
try {
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
wb.write(bufferedOutPut);
bufferedOutPut.flush();
bufferedOutPut.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@GetMapping("/reports") @GetMapping("/reports")
@ApiOperation(value = "查询所有决算报告(不附带详情数据)") @ApiOperation(value = "查询所有决算报告(不附带详情数据)")
public ResponseEntity<ResultObj<List<FinalReportVo>>> findAllReports(String keyword) { public ResponseEntity<ResultObj<Page<FinalReportVo>>> findAllReports(
return ResultUtil.success(fcService.findAllWithoutDetail(keyword)); @RequestParam String keyword,
@RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "10") int size) {
return ResultUtil.success(fcService.findAllWithoutDetail(keyword, --page, size));
} }
@GetMapping("/{id}") @GetMapping("/{id}")
...@@ -47,14 +95,6 @@ public class FinalCheckController { ...@@ -47,14 +95,6 @@ public class FinalCheckController {
return ResultUtil.success(fcService.findReportById(id)); return ResultUtil.success(fcService.findReportById(id));
} }
@GetMapping("/test")
public ResponseEntity test() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("excleTemplate/test.xlsx");
InputStream inputStream =classPathResource.getInputStream();
System.out.println(inputStream.getClass());
return ResponseEntity.ok("ok");
}
@PostMapping("/reports") @PostMapping("/reports")
@ApiOperation(value = "生成决算报告") @ApiOperation(value = "生成决算报告")
public ResponseEntity<ResultObj<FinalReportVo>> generateReport(@RequestBody TimeParam time) { public ResponseEntity<ResultObj<FinalReportVo>> generateReport(@RequestBody TimeParam time) {
...@@ -62,11 +102,78 @@ public class FinalCheckController { ...@@ -62,11 +102,78 @@ public class FinalCheckController {
return ResultUtil.success(finalReportVo); return ResultUtil.success(finalReportVo);
} }
@SneakyThrows
@PostMapping("/excel/{id}") @PostMapping("/excel/{id}")
@ApiOperation(value = "根据id导出对应的excel数据") @ApiOperation(value = "根据id导出对应的excel数据")
public ResponseEntity<ResultObj<FinalReportVo>> exportExcel( public void exportExcel(
@PathVariable Integer id, HttpServletResponse response) { @PathVariable Integer id,
return ResultUtil.success(new FinalReportVo()); HttpServletRequest request,
HttpServletResponse response) throws IOException {
FinalReportVo reportVo = fcService.findReportById(id);
TemplateExportParams params = new TemplateExportParams();
Map<String, Object> map = new HashMap<>();
map.put("name", reportVo.getName());
map.put("unitName", reportVo.getUnitName());
map.put("startTime", localDateToDate(reportVo.getStartTime()));
map.put("endTime", localDateToDate(reportVo.getEndTime()));
map.put("reportTime", localDateToDate(reportVo.getReportTime().toLocalDate()));
List<Map<String, String>> listMap = new ArrayList<>();
for (int i = 0; i < reportVo.getDetails().size(); i++) {
Map<String, String> lm = new HashMap<>();
FinalDetailVo detail = reportVo.getDetails().get(i);
lm.put("order", i + 1 + "");
lm.put("model", detail.getModel());
lm.put("type", detail.getType() + "");
lm.put("vertical", detail.getVertical() + "");
lm.put("horizontal", detail.getHorizontal() + "");
lm.put("commission", detail.getCommission() + "");
lm.put("other", detail.getOther() + "");
lm.put("total", detail.getTotal() + "");
lm.put("hosting", detail.getHosting() + "");
lm.put("received", detail.getReceived() + "");
lm.put("sending", detail.getSending() + "");
lm.put("scrapped", detail.getScrapped() + "");
listMap.add(lm);
}
if (listMap.size() <= 15) {
for (int i = 0; i < 15 - listMap.size(); i++) {
Map<String, String> lm = new HashMap<>();
lm.put("order", " ");
lm.put("model", "");
lm.put("type", "");
lm.put("vertical", "");
lm.put("horizontal", "");
lm.put("commission", "");
lm.put("other", "");
lm.put("total", "");
lm.put("hosting", "");
lm.put("received", "");
lm.put("sending", "");
lm.put("scrapped", "");
listMap.add(lm);
}
} }
map.put("list", listMap);
params.setTemplateUrl("excelTemplate/finalCheck.xlsx");
Workbook book = ExcelExportUtil.exportExcel(params, map);
//
// File savefile = new File("/Users/matrix/local-doc/excel");
// if (!savefile.exists()) {
// boolean result = savefile.mkdirs();
// System.out.println("目录不存在,进行创建,创建" + (result ? "成功!" : "失败!"));
// }
// FileOutputStream fos = new FileOutputStream("/Users/matrix/local-doc/excel/templateMapResult.xlsx");
// book.write(fos);
// fos.close();
downloadExcel(request, response, book, "Report");
}
} }
package com.tykj.dev.device.finalcheck.service; package com.tykj.dev.device.finalcheck.service;
import com.tykj.dev.device.finalcheck.entity.vo.FinalReportVo; import com.tykj.dev.device.finalcheck.entity.vo.FinalReportVo;
import org.springframework.data.domain.Page;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List;
/** /**
* FinalCheckService. * FinalCheckService.
...@@ -24,9 +24,11 @@ public interface FinalCheckService { ...@@ -24,9 +24,11 @@ public interface FinalCheckService {
/** /**
* 根据关键字查询报告列表(不附带detail) * 根据关键字查询报告列表(不附带detail)
* @param keyword 要查询的字 * @param keyword 要查询的字
* @param page
* @param size
* @return {@link FinalReportVo} 's List * @return {@link FinalReportVo} 's List
*/ */
List<FinalReportVo> findAllWithoutDetail(String keyword); Page<FinalReportVo> findAllWithoutDetail(String keyword, int page, int size);
/** /**
* 根据主键id查询报告 * 根据主键id查询报告
......
...@@ -15,6 +15,9 @@ import com.tykj.dev.device.usereport.subject.vo.DeviceStatistics; ...@@ -15,6 +15,9 @@ import com.tykj.dev.device.usereport.subject.vo.DeviceStatistics;
import com.tykj.dev.misc.exception.ApiException; import com.tykj.dev.misc.exception.ApiException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -30,6 +33,7 @@ import java.util.stream.Collectors; ...@@ -30,6 +33,7 @@ import java.util.stream.Collectors;
import static com.tykj.dev.misc.utils.TimestampUtil.localDateToDate; import static com.tykj.dev.misc.utils.TimestampUtil.localDateToDate;
import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toMap; import static java.util.stream.Collectors.toMap;
import static org.springframework.data.domain.Sort.Direction.*;
/** /**
* FinalCheckServiceImpl. * FinalCheckServiceImpl.
...@@ -93,11 +97,10 @@ public class FinalCheckServiceImpl implements FinalCheckService { ...@@ -93,11 +97,10 @@ public class FinalCheckServiceImpl implements FinalCheckService {
} }
@Override @Override
public List<FinalReportVo> findAllWithoutDetail(String keyword) { public Page<FinalReportVo> findAllWithoutDetail(String keyword, int page, int size) {
if (StringUtils.isEmpty(keyword)) { if (StringUtils.isEmpty(keyword)) {
return reportDao.findAll().stream() return reportDao.findAll(PageRequest.of(page, size, Sort.by(ASC, "id")))
.map(report -> report.toVo(Collections.emptyList())) .map(report -> report.toVo(Collections.emptyList()));
.collect(Collectors.toList());
} }
//构造查询器 //构造查询器
...@@ -107,9 +110,10 @@ public class FinalCheckServiceImpl implements FinalCheckService { ...@@ -107,9 +110,10 @@ public class FinalCheckServiceImpl implements FinalCheckService {
.build(); .build();
return reportDao.findAll(pred).stream() return reportDao.findAll(pred, PageRequest.of(page, size, Sort.by(ASC, "id")))
.map(report -> report.toVo(Collections.emptyList())) .map(report -> report.toVo(Collections.emptyList()));
.collect(Collectors.toList());
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论