From 97834ce60827831a864ebe37e585f8a921352669 Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期四, 13 十一月 2025 19:11:06 +0800
Subject: [PATCH] 文件名修改

---
 jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/freemark/TemplateCreateController.java |   93 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 93 insertions(+), 0 deletions(-)

diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/freemark/TemplateCreateController.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/freemark/TemplateCreateController.java
new file mode 100644
index 0000000..dcda1c7
--- /dev/null
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/freemark/TemplateCreateController.java
@@ -0,0 +1,93 @@
+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();
+            AppointmentDO appointment = appointmentService.getAppointment(id);
+            getData(dataMap, appointment);
+            String filePath = getClass().getResource("/template/").getPath();
+            System.out.println(filePath);
+            //璁剧疆妯℃湰瑁呯疆鏂规硶鍜岃矾寰�,FreeMarker鏀寔澶氱妯℃澘瑁呰浇鏂规硶銆傚彲浠ラ噸servlet锛宑lasspath锛屾暟鎹簱鏁欑▼瑁呰浇锛�
+            configuration.setDirectoryForTemplateLoading(new File(filePath));
+            Template t = null;
+            //鎹愮尞琛�.ftl涓鸿瑁呰浇鐨勬ā鏉�
+            t = configuration.getTemplate("鐭ユ儏鍚屾剰杩愬姩璇曢獙.ftl");
+
+            String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
+            String name = "鐭ユ儏鍚屾剰杩愬姩璇曢獙-" + appointment.getPatName() + "-" + year;
+
+            //杈撳嚭鏂囨。璺緞鍙婂悕绉�
+            File outFile = new File("D:/" + name + ".doc");
+            Writer out = null;
+            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
+            t.process(dataMap, out);
+
+            Map<String, Object> map = new HashMap<>();
+            map.put("downloadUrl", "D:/" + name + ".doc");
+            map.put("downloadName", name + ".doc");
+            return map;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    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());
+
+    }
+
+    public int calculateAge(LocalDate birthdayStr) {
+        return Period.between(birthdayStr, LocalDate.now()).getYears();
+    }
+
+}

--
Gitblit v1.9.3