| | |
| | | |
| | | 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 org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | 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; |
| | | |
| | |
| | | @Resource |
| | | private AppointmentService appointmentService; |
| | | |
| | | @Resource |
| | | private ConfigService configService; |
| | | |
| | | private static Configuration configuration = null; |
| | | |
| | |
| | | configuration.setDefaultEncoding("utf-8"); |
| | | } |
| | | |
| | | |
| | | @Operation(summary = "ç¥æ
åæè¿å¨è¯éª") |
| | | @GetMapping(value = "/download/{id}") |
| | | public void downloadInfo(@PathVariable("id") Long id, HttpServletResponse response) { |
| | | public Map<String, Object> downloadInfo(@PathVariable("id") Long id) { |
| | | log.info("ç¥æ
åæè¿å¨è¯éªï¼{}", id); |
| | | |
| | | try { |
| | |
| | | |
| | | String filePath = getClass().getResource("/template/").getPath(); |
| | | configuration.setDirectoryForTemplateLoading(new File(filePath)); |
| | | |
| | | // ç¡®ä¿æ¨¡æ¿æ¯Word XMLæ ¼å¼ |
| | | Template t = configuration.getTemplate("ç¥æ
åæè¿å¨è¯éª.ftl"); |
| | | |
| | | String timestamp = String.valueOf(System.currentTimeMillis()); |
| | | String fileName = "ç¥æ
åæè¿å¨è¯éª-" + appointment.getPatName() + "-" + timestamp + ".doc"; |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | String fileName = "å¹³æ¿è¿å¨å¿çµå¾ç¥æ
åæä¹¦-" + appointment.getPatName() + "-" + time + ".doc"; |
| | | |
| | | // 设置ååºå¤´ï¼åè¯æµè§å¨è¿è¡ä¸è½½ |
| | | response.setCharacterEncoding("UTF-8"); |
| | | response.setContentType("application/msword"); // Wordææ¡£ç±»å |
| | | response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); |
| | | // å°æ¨¡æ¿æ¸²æç»æåå
¥StringWriter |
| | | StringWriter stringWriter = new StringWriter(); |
| | | try (Writer out = new BufferedWriter(stringWriter)) { |
| | | t.process(dataMap, out); |
| | | } |
| | | |
| | | // ç´æ¥è¾åºå°responseçè¾åºæµ |
| | | Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")); |
| | | t.process(dataMap, out); |
| | | out.flush(); |
| | | out.close(); |
| | | 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); |
| | | try { |
| | | response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "ææ¡£çæå¤±è´¥"); |
| | | } catch (IOException ex) { |
| | | log.error("åéé误ååºå¤±è´¥", ex); |
| | | } |
| | | 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 void downloadEsophagusInfo(@PathVariable("id") Long id, HttpServletResponse response) { |
| | | public Map<String, Object> downloadEsophagusInfo(@PathVariable("id") Long id) { |
| | | log.info("é£ç®¡å¿èçµççç¥æ
åæä¹¦ï¼{}", id); |
| | | |
| | | try { |
| | |
| | | |
| | | String filePath = getClass().getResource("/template/").getPath(); |
| | | configuration.setDirectoryForTemplateLoading(new File(filePath)); |
| | | |
| | | // ç¡®ä¿æ¨¡æ¿æ¯Word XMLæ ¼å¼ |
| | | Template t = configuration.getTemplate("é£ç®¡å¿èçµççç¥æ
åæä¹¦.ftl"); |
| | | |
| | | String timestamp = String.valueOf(System.currentTimeMillis()); |
| | | String fileName = "é£ç®¡å¿èçµççç¥æ
åæä¹¦-" + appointment.getPatName() + "-" + timestamp + ".doc"; |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | String fileName = "é£ç®¡å¿èçµççç¥æ
åæä¹¦-" + appointment.getPatName() + "-" + time + ".doc"; |
| | | |
| | | // 设置ååºå¤´ï¼åè¯æµè§å¨è¿è¡ä¸è½½ |
| | | response.setCharacterEncoding("UTF-8"); |
| | | response.setContentType("application/msword"); // Wordææ¡£ç±»å |
| | | response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); |
| | | // å°æ¨¡æ¿æ¸²æç»æåå
¥StringWriter |
| | | StringWriter stringWriter = new StringWriter(); |
| | | try (Writer out = new BufferedWriter(stringWriter)) { |
| | | t.process(dataMap, out); |
| | | } |
| | | |
| | | // ç´æ¥è¾åºå°responseçè¾åºæµ |
| | | Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")); |
| | | t.process(dataMap, out); |
| | | out.flush(); |
| | | out.close(); |
| | | 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); |
| | | try { |
| | | response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "ææ¡£çæå¤±è´¥"); |
| | | } catch (IOException ex) { |
| | | log.error("åéé误ååºå¤±è´¥", ex); |
| | | } |
| | | 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 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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |