liusheng
昨天 d1bb20f04dd7208840b2316d6f5e77503e6619d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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 cn.lihu.jh.module.infra.service.config.ConfigService;
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.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;
import java.util.Map;
 
@Slf4j
@RestController
@RequestMapping("/ecg/template")
@Validated
public class TemplateCreateController {
 
    @Resource
    private AppointmentService appointmentService;
 
    @Resource
    private ConfigService configService;
 
    private static Configuration configuration = null;
 
    public TemplateCreateController() {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
    }
 
 
    @Operation(summary = "知情同意运动试验")
    @GetMapping(value = "/download/{id}")
    public Map<String, Object> downloadInfo(@PathVariable("id") Long id) {
        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));
 
            // 确保模板是Word XML格式
            Template t = configuration.getTemplate("知情同意运动试验.ftl");
 
            String time = String.valueOf(System.currentTimeMillis());
            String fileName = "平板运动心电图知情同意书-" + appointment.getPatName() + "-" + time + ".doc";
 
            // 将模板渲染结果写入StringWriter
            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) {
            log.error("生成平板运动心电图知情同意书失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("data", null);
            result.put("msg", "文档生成失败: " + e.getMessage());
            return result;
        }
    }
 
    @Operation(summary = "食管心脏电生理知情同意书")
    @GetMapping(value = "/downloadesophagus/{id}")
    public Map<String, Object> downloadEsophagusInfo(@PathVariable("id") Long id) {
        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));
 
            // 确保模板是Word XML格式
            Template t = configuration.getTemplate("食管心脏电生理知情同意书.ftl");
 
            String time = String.valueOf(System.currentTimeMillis());
            String fileName = "食管心脏电生理知情同意书-" + appointment.getPatName() + "-" + time + ".doc";
 
            // 将模板渲染结果写入StringWriter
            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) {
            log.error("生成食管心脏电生理知情同意书失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("data", null);
            result.put("msg", "文档生成失败: " + e.getMessage());
            return result;
        }
    }
 
    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();
    }
 
    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);
            }
        }
    }
 
}