liusheng
昨天 2e455dc7dc92aef3458023a9b0a8547871614bf8
模板提交
已修改1个文件
123 ■■■■■ 文件已修改
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java 123 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java
@@ -2,7 +2,6 @@
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.dal.dataobject.config.ConfigDO;
import cn.lihu.jh.module.infra.service.config.ConfigService;
import freemarker.template.Configuration;
import freemarker.template.Template;
@@ -16,7 +15,10 @@
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.HashMap;
@@ -41,79 +43,108 @@
        configuration.setDefaultEncoding("utf-8");
    }
    @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,classpath,数据库教程装载,
            configuration.setDirectoryForTemplateLoading(new File(filePath));
            Template t = null;
            //捐献表.ftl为要装载的模板
            t = configuration.getTemplate("知情同意运动试验.ftl");
            // 确保模板是Word XML格式
            Template t = configuration.getTemplate("知情同意运动试验.ftl");
            String time = String.valueOf(System.currentTimeMillis());
            String name = "平板运动心电图知情同意书-" + appointment.getPatName() + "-" + time;
            String fileName = "平板运动心电图知情同意书-" + appointment.getPatName() + "-" + time + ".doc";
            //输出文档路径及名称
            ConfigDO configByKey = configService.getConfigByKey("file.upload.path");
            createPath(configByKey.getValue());
            File outFile = new File(configByKey.getValue() + name + ".doc");
            Writer out = null;
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
            t.process(dataMap, out);
            // 将模板渲染结果写入StringWriter
            StringWriter stringWriter = new StringWriter();
            try (Writer out = new BufferedWriter(stringWriter)) {
                t.process(dataMap, out);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("downloadUrl", "profile/" + name + ".doc");
            map.put("downloadName", name + ".doc");
            return map;
            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) {
    @GetMapping(value = "/downloadEsophagus/{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,classpath,数据库教程装载,
            configuration.setDirectoryForTemplateLoading(new File(filePath));
            Template t = null;
            //捐献表.ftl为要装载的模板
            t = configuration.getTemplate("食管心脏电生理知情同意书.ftl");
            // 确保模板是Word XML格式
            Template t = configuration.getTemplate("食管心脏电生理知情同意书.ftl");
            String time = String.valueOf(System.currentTimeMillis());
            String name = "食管心脏电生理知情同意书-" + appointment.getPatName() + "-" + time;
            String fileName = "食管心脏电生理知情同意书-" + appointment.getPatName() + "-" + time + ".doc";
            //输出文档路径及名称
            ConfigDO configByKey = configService.getConfigByKey("file.upload.path");
            createPath(configByKey.getValue());
            File outFile = new File(configByKey.getValue() + name + ".doc");
            Writer out = null;
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
            t.process(dataMap, out);
            // 将模板渲染结果写入StringWriter
            StringWriter stringWriter = new StringWriter();
            try (Writer out = new BufferedWriter(stringWriter)) {
                t.process(dataMap, out);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("downloadUrl", "profile/" + name + ".doc");
            map.put("downloadName", name + ".doc");
            return map;
            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) {