package cn.lihu.jh.module.ecg.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 java.io.*; 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 Map downloadInfo(@PathVariable("id") Long id) { log.info("知情同意运动试验:{}", id); try { Map dataMap = new HashMap(); getData(dataMap, id); String filePath = getClass().getResource("/template/").getPath(); System.out.println(filePath); //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库教程装载, configuration.setDirectoryForTemplateLoading(new File(filePath)); Template t = null; //捐献表.ftl为要装载的模板 t = configuration.getTemplate("知情同意运动试验.ftl"); String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); String name = "知情同意运动试验" + year; //输出文档路径及名称 File outFile = new File("D:/download/" + name + ".doc"); Writer out = null; out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8")); t.process(dataMap, out); Map map = new HashMap<>(); map.put("downloadUrl", "D:/download/" + name + ".doc"); map.put("downloadName", name + ".doc"); return map; } catch (Exception e) { e.printStackTrace(); } return null; } private void getData(Map dataMap, Long id) { AppointmentDO appointment = appointmentService.getAppointment(id); 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()); } public int calculateAge(LocalDate birthdayStr) { return Period.between(birthdayStr, LocalDate.now()).getYears(); } }