package com.ltkj.web.controller.system; import java.io.*; import java.net.URL; import java.util.List; import javax.servlet.http.HttpServletResponse; import cn.hutool.core.date.DateTime; import cn.hutool.core.io.FileUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ltkj.common.core.domain.entity.SysDept; import com.ltkj.common.core.domain.entity.SysUser; import com.ltkj.common.utils.SecurityUtils; import com.ltkj.hosp.domain.TjReport; import com.ltkj.hosp.domain.TjReservation; import com.ltkj.web.config.pdfutils.PDFBinaryUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ltkj.common.annotation.Log; import com.ltkj.common.core.controller.BaseController; import com.ltkj.common.core.domain.AjaxResult; import com.ltkj.common.enums.BusinessType; import com.ltkj.hosp.domain.TjReportTemplate; import com.ltkj.hosp.service.ITjReportTemplateService; import com.ltkj.common.utils.poi.ExcelUtil; import com.ltkj.common.core.page.TableDataInfo; /** * templateController * * @author ltkj_赵佳豪&李格 * @date 2022-12-12 */ @RestController @RequestMapping("/hosp/template") @Api(tags = "体检报告模板") public class TjReportTemplateController extends BaseController { @Autowired private ITjReportTemplateService tjReportTemplateService; @Value("${path.filePath}") private String value; /** * 查询template列表 */ //@PreAuthorize("@ss.hasPermi('hosp:template:list')") @GetMapping("/list") public TableDataInfo list(TjReportTemplate tjReportTemplate) { startPage(); List list = tjReportTemplateService.selectTjReportTemplateList(tjReportTemplate); return getDataTable(list); } /** * 获取template详细信息 */ //@PreAuthorize("@ss.hasPermi('hosp:template:query')") @GetMapping(value = "/{reportTemId}") public AjaxResult getInfo(@PathVariable("reportTemId") Long reportTemId) { return success(tjReportTemplateService.selectTjReportTemplateByReportTemId(reportTemId)); } /** * 删除template */ @Log(title = "template", businessType = BusinessType.DELETE) @DeleteMapping("/{reportTemIds}") public AjaxResult remove(@PathVariable Long[] reportTemIds) { return toAjax(tjReportTemplateService.deleteTjReportTemplateByReportTemIds(reportTemIds)); } /** * 启用状态修改 */ @PutMapping("/changeStatus") @ApiOperation("启用状态修改") public AjaxResult changeStatus(@RequestBody TjReportTemplate tjReportTemplate) { //判断是启用 if ("0".equals(tjReportTemplate.getOpen())){ TjReportTemplate byId = tjReportTemplateService.getById(tjReportTemplate.getReportTemId()); LambdaQueryWrapper we = new LambdaQueryWrapper<>(); we.eq(TjReportTemplate::getFlag, byId.getFlag()); we.eq(TjReportTemplate::getOpen, "0"); List list = tjReportTemplateService.list(we); if (list.size()!=0){ return AjaxResult.error("同类型模板只能启用一个,请查看其他模板启用状态!"); } } tjReportTemplate.setUpdateBy(getUsername()); tjReportTemplate.setUpdateTime(new DateTime()); if (!tjReportTemplateService.updateById(tjReportTemplate)) { return AjaxResult.error("修改失败"); } return AjaxResult.success("修改成功"); } /** * 模板修改接口 */ @PostMapping("/edit") @ApiOperation(value = "模板修改接口") public AjaxResult edit(@RequestBody TjReportTemplate tjReportTemplate) { if (tjReportTemplateService.updateById(tjReportTemplate)) { return AjaxResult.success("操作成功"); } return AjaxResult.error("操作失败"); } @GetMapping("/preShow/{flag}/{reportTemId}") @ApiOperation(value = "模板预览") public void preShow(HttpServletResponse response, @PathVariable("flag") boolean flag, @PathVariable("reportTemId") String reportTemId) { LambdaQueryWrapper we = new LambdaQueryWrapper<>(); we.eq(TjReportTemplate::getReportTemId, reportTemId); TjReportTemplate one = tjReportTemplateService.getOne(we); if (one == null) { return; } String userId = SecurityUtils.getLoginUser().getUsername(); PDFBinaryUtil.base64StringToPDF(one.getTemplate(), FileUtil.mkdir(value).getPath() + File.separator + userId + reportTemId + "_模板预览.pdf"); String filePath = value + File.separator + userId + reportTemId + "_模板预览.pdf"; File f = new File(filePath); if (filePath.isEmpty()) { System.out.println("文件不存在!"); return; } BufferedInputStream br = null; OutputStream out = null; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(f); br = new BufferedInputStream(fileInputStream); byte[] bs = new byte[1024]; int len = 0; response.reset(); // 非常重要 if (flag) { // 在线打开方式 URL u = new URL("file:///" + filePath); //System.out.println(u); String contentType = u.openConnection().getContentType(); response.setContentType(contentType); response.setHeader("Content-Disposition", "inline;filename=" + userId + reportTemId + ".pdf"); } else { // 纯下载方式 response.setContentType("application/x-msdownload"); response.setContentType("application/pdf;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + userId + reportTemId + ".pdf"); } out = response.getOutputStream(); while ((len = br.read(bs)) > 0) { out.write(bs, 0, len); } out.flush(); out.close(); br.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); //System.out.println("pdf处理文件异常" + e); } finally { if (out != null) { try { out.close(); br.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } @GetMapping("/zhiYeTable/{flag}") @ApiOperation(value = "下载职业健康表") public void zhiYeTable(HttpServletResponse response, @PathVariable("flag") boolean flag) { LambdaQueryWrapper we = new LambdaQueryWrapper<>(); we.eq(TjReportTemplate::getName, "职业健康体检个人信息表"); TjReportTemplate one = tjReportTemplateService.getOne(we); if (one == null) { return; } String userId = SecurityUtils.getLoginUser().getUsername(); PDFBinaryUtil.base64StringToPDF(one.getTemplate(), FileUtil.mkdir(value).getPath() + File.separator + userId + "_职业健康体检个人信息表.pdf"); String filePath = value + File.separator + userId + "_职业健康体检个人信息表.pdf"; File f = new File(filePath); if (filePath.isEmpty()) { System.out.println("文件不存在!"); return; } BufferedInputStream br = null; OutputStream out = null; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(f); br = new BufferedInputStream(fileInputStream); byte[] bs = new byte[1024]; int len = 0; response.reset(); // 非常重要 if (flag) { // 在线打开方式 URL u = new URL("file:///" + filePath); //System.out.println(u); String contentType = u.openConnection().getContentType(); response.setContentType(contentType); response.setHeader("Content-Disposition", "inline;filename=" + userId +"_职业健康体检个人信息表.pdf"); } else { // 纯下载方式 response.setContentType("application/x-msdownload"); response.setContentType("application/pdf;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + userId +"_职业健康体检个人信息表.pdf"); } out = response.getOutputStream(); while ((len = br.read(bs)) > 0) { out.write(bs, 0, len); } out.flush(); out.close(); br.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); //System.out.println("pdf处理文件异常" + e); } finally { if (out != null) { try { out.close(); br.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }