From 0fa1f4767affcd1879709ebea630016356836c75 Mon Sep 17 00:00:00 2001
From: yxh <172933527@qq.com>
Date: 星期五, 18 九月 2026 15:07:20 +0800
Subject: [PATCH] Merge branch 'master' of http://116.62.18.175:6699/r/~yxh/ecg

---
 jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java |  159 +++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 111 insertions(+), 48 deletions(-)

diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java
index cad5cc2..69b6d14 100644
--- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java
@@ -2,6 +2,7 @@
 
 import cn.lihu.jh.module.ecg.dal.dataobject.appointment.AppointmentDO;
 import cn.lihu.jh.module.ecg.service.appointment.AppointmentService;
+import cn.lihu.jh.module.infra.service.config.ConfigService;
 import freemarker.template.Configuration;
 import freemarker.template.Template;
 import io.swagger.v3.oas.annotations.Operation;
@@ -14,10 +15,12 @@
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
-import java.io.*;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.StringWriter;
+import java.io.Writer;
 import java.time.LocalDate;
 import java.time.Period;
-import java.util.Calendar;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -30,83 +33,116 @@
     @Resource
     private AppointmentService appointmentService;
 
+    @Resource
+    private ConfigService configService;
 
     private static Configuration configuration = null;
 
     public TemplateCreateController() {
         configuration = new Configuration();
         configuration.setDefaultEncoding("utf-8");
+        // 浣跨敤classpath鍔犺浇妯℃澘锛岃�屼笉鏄枃浠剁郴缁�
+        configuration.setClassForTemplateLoading(this.getClass(), "/template");
     }
+
 
     @Operation(summary = "鐭ユ儏鍚屾剰杩愬姩璇曢獙")
     @GetMapping(value = "/download/{id}")
-    public Map downloadInfo(@PathVariable("id") Long id) {
+    public Map<String, Object> downloadInfo(@PathVariable("id") Long id) {
         log.info("鐭ユ儏鍚屾剰杩愬姩璇曢獙锛歿}", id);
+
         try {
-            Map dataMap = new HashMap();
+            Map<String, Object> 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);
+            // 纭繚妯℃澘鏄疻ord XML鏍煎紡
+            Template t = configuration.getTemplate("鐭ユ儏鍚屾剰杩愬姩璇曢獙.ftl");
 
-            Map<String, Object> map = new HashMap<>();
-            map.put("downloadUrl", "D:/" + name + ".doc");
-            map.put("downloadName", name + ".doc");
-            return map;
+            String time = String.valueOf(System.currentTimeMillis());
+            String fileName = "骞虫澘杩愬姩蹇冪數鍥剧煡鎯呭悓鎰忎功-" + appointment.getPatName() + "-" + time + ".doc";
+
+            // 灏嗘ā鏉挎覆鏌撶粨鏋滃啓鍏tringWriter
+            StringWriter stringWriter = new StringWriter();
+            try (Writer out = new BufferedWriter(stringWriter)) {
+                t.process(dataMap, out);
+            }
+
+            String xmlContent = stringWriter.toString();
+
+            // 杩斿洖缁撴灉
+            Map<String, Object> result = new HashMap<>();
+            Map<String, Object> result1 = new HashMap<>();
+            result.put("success", true);
+            result.put("fileName", fileName);
+            result.put("fileContent", xmlContent);
+            result.put("fileType", "application/msword");
+            result1.put("code", 200);
+            result1.put("data", result);
+            result1.put("msg", "success");
+
+            log.info("Word XML鍐呭鐢熸垚鎴愬姛");
+            return result1;
+
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("鐢熸垚骞虫澘杩愬姩蹇冪數鍥剧煡鎯呭悓鎰忎功澶辫触", e);
+            Map<String, Object> result = new HashMap<>();
+            result.put("code", 500);
+            result.put("data", null);
+            result.put("msg", "鏂囨。鐢熸垚澶辫触: " + e.getMessage());
+            return result;
         }
-        return null;
     }
-
 
     @Operation(summary = "椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功")
     @GetMapping(value = "/downloadesophagus/{id}")
-    public Map downloadEsophagusInfo(@PathVariable("id") Long id) {
+    public Map<String, Object> downloadEsophagusInfo(@PathVariable("id") Long id) {
         log.info("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功锛歿}", id);
+
         try {
-            Map dataMap = new HashMap();
+            Map<String, Object> 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);
+            // 纭繚妯℃澘鏄疻ord XML鏍煎紡
+            Template t = configuration.getTemplate("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功.ftl");
 
-            Map<String, Object> map = new HashMap<>();
-            map.put("downloadUrl", "D:/" + name + ".doc");
-            map.put("downloadName", name + ".doc");
-            return map;
+            String time = String.valueOf(System.currentTimeMillis());
+            String fileName = "椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功-" + appointment.getPatName() + "-" + time + ".doc";
+
+            // 灏嗘ā鏉挎覆鏌撶粨鏋滃啓鍏tringWriter
+            StringWriter stringWriter = new StringWriter();
+            try (Writer out = new BufferedWriter(stringWriter)) {
+                t.process(dataMap, out);
+            }
+
+            String xmlContent = stringWriter.toString();
+
+            // 杩斿洖缁撴灉
+            Map<String, Object> result1 = new HashMap<>();
+            Map<String, Object> result = new HashMap<>();
+            result.put("success", true);
+            result.put("fileName", fileName);
+            result.put("fileContent", xmlContent);
+            result.put("fileType", "application/msword");
+
+            result1.put("code", 200);
+            result1.put("data", result);
+            result1.put("msg", "success");
+
+            log.info("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功Word XML鍐呭鐢熸垚鎴愬姛");
+            return result1;
+
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("鐢熸垚椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功澶辫触", e);
+            Map<String, Object> result = new HashMap<>();
+            result.put("code", 500);
+            result.put("data", null);
+            result.put("msg", "鏂囨。鐢熸垚澶辫触: " + e.getMessage());
+            return result;
         }
-        return null;
     }
 
     private void getData(Map dataMap, AppointmentDO appointment) {
@@ -115,7 +151,18 @@
         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());
+        String ward = StringUtils.isEmpty(appointment.getPatWardDesc()) ? "" : appointment.getPatWardDesc();
+        String bed = StringUtils.isEmpty(appointment.getPatBedNo()) ? "" : appointment.getPatBedNo();
+
+        // 濡傛灉涓や釜閮芥湁鍊硷紝涓棿鍔犵┖鏍硷紱濡傛灉鍙湁涓�涓湁鍊硷紝灏变笉鍔犵┖鏍�
+        String badNo = "";
+        if (!ward.isEmpty() && !bed.isEmpty()) {
+            badNo = ward + " " + bed;
+        } else {
+            badNo = ward + bed;
+        }
+        dataMap.put("badNo", badNo);
+
         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());
@@ -127,4 +174,20 @@
         return Period.between(birthdayStr, LocalDate.now()).getYears();
     }
 
+    public void createPath(String uploadPath) {
+        // 纭繚璺緞浠ユ枩鏉犵粨灏�
+        if (!uploadPath.endsWith("/") && !uploadPath.endsWith("\\")) {
+            uploadPath += "/";
+        }
+
+        // 鍒涘缓鐩綍锛堝鏋滀笉瀛樺湪锛�
+        File uploadDir = new File(uploadPath);
+        if (!uploadDir.exists()) {
+            boolean created = uploadDir.mkdirs();
+            if (created) {
+                log.info("鍒涘缓鐩綍鎴愬姛: {}", uploadPath);
+            }
+        }
+    }
+
 }

--
Gitblit v1.9.3