package cn.lihu.jh.module.ecg.controller.admin.freemark;
|
|
import cn.lihu.jh.module.ecg.dal.dataobject.appointment.AppointmentDO;
|
import cn.lihu.jh.module.ecg.service.appointment.AppointmentService;
|
import freemarker.template.Configuration;
|
import freemarker.template.Template;
|
import io.swagger.v3.oas.annotations.Operation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.util.StringUtils;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.*;
|
import java.net.URLEncoder;
|
import java.time.LocalDate;
|
import java.time.Period;
|
import java.util.Calendar;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@Slf4j
|
@RestController
|
@RequestMapping("/ecg/template")
|
@Validated
|
public class TemplateCreateController {
|
|
@Resource
|
private AppointmentService appointmentService;
|
|
|
private static Configuration configuration = null;
|
|
public TemplateCreateController() {
|
configuration = new Configuration();
|
configuration.setDefaultEncoding("utf-8");
|
}
|
|
@Operation(summary = "知情同意运动试验")
|
@GetMapping(value = "/download/{id}")
|
public void downloadInfo(@PathVariable("id") Long id, HttpServletResponse response) {
|
log.info("知情同意运动试验:{}", id);
|
|
try {
|
Map<String, Object> dataMap = new HashMap<>();
|
AppointmentDO appointment = appointmentService.getAppointment(id);
|
getData(dataMap, appointment);
|
|
String filePath = getClass().getResource("/template/").getPath();
|
configuration.setDirectoryForTemplateLoading(new File(filePath));
|
Template t = configuration.getTemplate("知情同意运动试验.ftl");
|
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
String fileName = "知情同意运动试验-" + appointment.getPatName() + "-" + timestamp + ".doc";
|
|
// 设置响应头,告诉浏览器进行下载
|
response.setCharacterEncoding("UTF-8");
|
response.setContentType("application/msword"); // Word文档类型
|
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
// 直接输出到response的输出流
|
Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
|
t.process(dataMap, out);
|
out.flush();
|
out.close();
|
|
} catch (Exception e) {
|
log.error("下载知情同意运动试验文档失败", e);
|
try {
|
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "文档生成失败");
|
} catch (IOException ex) {
|
log.error("发送错误响应失败", ex);
|
}
|
}
|
}
|
|
|
@Operation(summary = "食管心脏电生理知情同意书")
|
@GetMapping(value = "/downloadesophagus/{id}")
|
public void downloadEsophagusInfo(@PathVariable("id") Long id, HttpServletResponse response) {
|
log.info("食管心脏电生理知情同意书:{}", id);
|
|
try {
|
Map<String, Object> dataMap = new HashMap<>();
|
AppointmentDO appointment = appointmentService.getAppointment(id);
|
getData(dataMap, appointment);
|
|
String filePath = getClass().getResource("/template/").getPath();
|
configuration.setDirectoryForTemplateLoading(new File(filePath));
|
Template t = configuration.getTemplate("食管心脏电生理知情同意书.ftl");
|
|
String timestamp = String.valueOf(System.currentTimeMillis());
|
String fileName = "食管心脏电生理知情同意书-" + appointment.getPatName() + "-" + timestamp + ".doc";
|
|
// 设置响应头,告诉浏览器进行下载
|
response.setCharacterEncoding("UTF-8");
|
response.setContentType("application/msword"); // Word文档类型
|
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
|
// 直接输出到response的输出流
|
Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
|
t.process(dataMap, out);
|
out.flush();
|
out.close();
|
|
} catch (Exception e) {
|
log.error("下载食管心脏电生理知情同意书文档失败", e);
|
try {
|
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "文档生成失败");
|
} catch (IOException ex) {
|
log.error("发送错误响应失败", ex);
|
}
|
}
|
}
|
|
private void getData(Map dataMap, AppointmentDO appointment) {
|
dataMap.put("name", StringUtils.isEmpty(appointment.getPatName()) ? "" : appointment.getPatName());
|
dataMap.put("sex", appointment.getPatGender() == null ? "" : appointment.getPatGender() == 1 ? "男" : "女");
|
dataMap.put("age", appointment.getPatBirthday() == null ? "" : calculateAge(appointment.getPatBirthday()));
|
dataMap.put("inhospno", StringUtils.isEmpty(appointment.getHospitalNo()) ? "" : appointment.getHospitalNo());
|
dataMap.put("idNo", StringUtils.isEmpty(appointment.getPatId()) ? "" : appointment.getPatId());
|
dataMap.put("badNo", StringUtils.isEmpty(appointment.getPatBedNo()) ? "" : appointment.getPatBedNo());
|
dataMap.put("birthday", appointment.getPatBirthday() == null ? "" : appointment.getPatBirthday());
|
dataMap.put("doctor", StringUtils.isEmpty(appointment.getDoctor()) ? "" : appointment.getDoctor());
|
dataMap.put("deptName", StringUtils.isEmpty(appointment.getPatDeptDesc()) ? "" : appointment.getPatDeptDesc());
|
dataMap.put("episodeId", StringUtils.isEmpty(appointment.getEpisodeId()) ? "" : appointment.getEpisodeId());
|
|
}
|
|
public int calculateAge(LocalDate birthdayStr) {
|
return Period.between(birthdayStr, LocalDate.now()).getYears();
|
}
|
|
}
|