package com.chinaztt.mes.report.controller;
|
|
import cn.hutool.core.io.IoUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.chinaztt.mes.report.entity.Template;
|
import com.chinaztt.mes.report.service.JasperReportService;
|
import com.chinaztt.ztt.common.core.util.R;
|
import com.chinaztt.ztt.common.log.annotation.SysLog;
|
import com.chinaztt.ztt.common.oss.service.OssTemplate;
|
import com.chinaztt.ztt.common.security.annotation.Inner;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.AllArgsConstructor;
|
import lombok.SneakyThrows;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.ByteArrayInputStream;
|
import java.io.File;
|
import java.io.InputStream;
|
import java.util.Locale;
|
import java.util.Map;
|
|
import static org.apache.commons.io.IOUtils.copy;
|
|
/**
|
* @Author: zhangxy
|
* @Date: 2021-06-03 10:22
|
*/
|
@RestController
|
@AllArgsConstructor
|
@Slf4j
|
@RequestMapping("/jasper")
|
public class JasperReportController {
|
|
private static final String MINIO_BUCKET = "report";
|
private static final String TEMPLATE_DIR = "template";
|
private static final String FILE_NAME = "fileName";
|
|
private JasperReportService jasperReportService;
|
|
private OssTemplate minioTemplate;
|
|
/**
|
* 打印pdf
|
*
|
* @param id
|
* @param args
|
* @param response
|
*/
|
@Inner(false)
|
@SneakyThrows
|
@RequestMapping(value = "/print/{type}/{id}", method = RequestMethod.GET)
|
public void print(@PathVariable String type, @PathVariable Long id, @RequestParam(value = "args", required = false) String args, HttpServletResponse response) {
|
JasperReportService.ReportType reportType = JasperReportService.ReportType.valueOf(type.toUpperCase(Locale.getDefault()));
|
Template template = jasperReportService.getById(id);
|
if (template == null) {
|
throw new RuntimeException("非法模板id");
|
}
|
|
response.setContentType(reportType.getMimeType());
|
String path = TEMPLATE_DIR + File.separator + template.getModule() + File.separator + template.getTemplatePath();
|
Map<String, Object> params = JSONObject.parseObject(args);
|
|
if (params != null && params.get(FILE_NAME) != null) {
|
response.setHeader("Content-disposition", "attachment; filename=" + params.get(FILE_NAME).toString());
|
}
|
|
int bytes = copy(new ByteArrayInputStream(jasperReportService.gen(path, reportType, params)), response.getOutputStream());
|
|
response.setContentLength(bytes);
|
response.addHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT");
|
response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
|
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
|
response.addHeader("Pragma", "no-cache");
|
}
|
|
|
/**
|
* 显示打印图片
|
*
|
* @param fileName
|
* @param response
|
*/
|
@Inner(false)
|
@RequestMapping(value = "/img/{fileName}", method = RequestMethod.GET)
|
public void getImage(@PathVariable String fileName, HttpServletResponse response) {
|
try (InputStream inputStream = minioTemplate.getObject(MINIO_BUCKET, fileName)) {
|
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
|
IoUtil.copy(inputStream, response.getOutputStream());
|
} catch (Exception e) {
|
log.error("文件读取异常: {}", e.getLocalizedMessage());
|
}
|
}
|
|
|
/**
|
* 分页查询
|
*
|
* @param page 分页对象
|
* @param template jasper报表模板
|
* @return
|
*/
|
@ApiOperation(value = "分页查询", notes = "分页查询")
|
@GetMapping("/page")
|
public R getTemplatePage(Page page, Template template) {
|
return R.ok(jasperReportService.page(page, Wrappers.query(template)));
|
}
|
|
|
/**
|
* 通过id查询jasper报表模板
|
*
|
* @param id id
|
* @return R
|
*/
|
@ApiOperation(value = "通过id查询", notes = "通过id查询")
|
@GetMapping("/{id}")
|
public R getById(@PathVariable("id") Long id) {
|
return R.ok(jasperReportService.getById(id));
|
}
|
|
/**
|
* 新增jasper报表模板
|
*
|
* @param template jasper报表模板
|
* @return R
|
*/
|
@ApiOperation(value = "新增jasper报表模板", notes = "新增jasper报表模板")
|
@SysLog("新增jasper报表模板")
|
@PostMapping
|
public R save(@RequestBody Template template) {
|
return R.ok(jasperReportService.save(template));
|
}
|
|
/**
|
* 修改jasper报表模板
|
*
|
* @param template jasper报表模板
|
* @return R
|
*/
|
@ApiOperation(value = "修改jasper报表模板", notes = "修改jasper报表模板")
|
@SysLog("修改jasper报表模板")
|
@PutMapping
|
public R updateById(@RequestBody Template template) {
|
return R.ok(jasperReportService.updateById(template));
|
}
|
|
/**
|
* 通过id删除jasper报表模板
|
*
|
* @param id id
|
* @return R
|
*/
|
@ApiOperation(value = "通过id删除jasper报表模板", notes = "通过id删除jasper报表模板")
|
@SysLog("通过id删除jasper报表模板")
|
@DeleteMapping("/{id}")
|
public R removeById(@PathVariable Long id) {
|
return R.ok(jasperReportService.removeById(id));
|
}
|
|
}
|