DiagnoseImportFacade.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package com.diagbot.facade;
  2. import java.io.InputStream;
  3. import java.text.DecimalFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.commons.lang.time.DateFormatUtils;
  10. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.ss.usermodel.Cell;
  13. import org.apache.poi.ss.usermodel.Row;
  14. import org.apache.poi.ss.usermodel.Sheet;
  15. import org.apache.poi.ss.usermodel.Workbook;
  16. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Qualifier;
  19. import org.springframework.stereotype.Component;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  22. import com.diagbot.dto.DiagnoseImportDTO;
  23. import com.diagbot.dto.DiagnosticAllExportDTO;
  24. import com.diagbot.dto.DiagnosticBasisExportDTO;
  25. import com.diagbot.dto.EquationDTO;
  26. import com.diagbot.dto.FeatureDTO;
  27. import com.diagbot.dto.RespDTO;
  28. import com.diagbot.entity.DiagnoseDetail;
  29. import com.diagbot.entity.DiagnoseQuestion;
  30. import com.diagbot.enums.DiagnoseFeatureTypeEnum;
  31. import com.diagbot.enums.DiagnoseTypeEnum;
  32. import com.diagbot.enums.IsDeleteEnum;
  33. import com.diagbot.enums.TermEnum;
  34. import com.diagbot.exception.CommonErrorCode;
  35. import com.diagbot.exception.CommonException;
  36. import com.diagbot.service.DiagnoseDetailService;
  37. import com.diagbot.service.DiagnoseQuestionService;
  38. import com.diagbot.util.ExportBeanExcelUtil;
  39. import com.diagbot.util.GsonUtil;
  40. import com.diagbot.util.StringUtil;
  41. import com.diagbot.vo.DiagnosticAllExportVO;
  42. import com.diagbot.vo.DiagnosticBasisExportVO;
  43. /**
  44. * @author wangfeng
  45. * @Description: TODO
  46. * @date 2019年8月8日 下午1:30:53
  47. */
  48. @Component
  49. public class DiagnoseImportFacade {
  50. @Autowired
  51. DiagnoseQuestionService diagnoseQuestionService;
  52. @Autowired
  53. @Qualifier("diagnoseDetailServiceImpl")
  54. DiagnoseDetailService diagnoseDetailService;
  55. @SuppressWarnings("null")
  56. public RespDTO<DiagnoseImportDTO> importDiagnosticBasisAll(MultipartFile file, HttpServletRequest request) {
  57. List<String> messages = new ArrayList<>();
  58. InputStream inputStream = null;
  59. DiagnoseImportDTO diagnoseImportDTO = new DiagnoseImportDTO();
  60. List<FeatureDTO> featureList = new ArrayList<FeatureDTO>();
  61. List<EquationDTO> equationList = new ArrayList<EquationDTO>();
  62. List<String> assemblys = new ArrayList<String>();
  63. Workbook wb = null;
  64. try {
  65. if (!file.isEmpty()) {
  66. inputStream = file.getInputStream();
  67. if (inputStream.available() > 512000) {
  68. messages.add("化验文件最大支持500KB!");
  69. } else {
  70. String fileName = file.getOriginalFilename();
  71. if (fileName.lastIndexOf(".") != -1) {
  72. String type = fileName.substring(fileName.lastIndexOf("."));
  73. if (type.equals(".xls")) {
  74. wb = new HSSFWorkbook(inputStream);
  75. } else if (type.equals(".xlsx")) {
  76. wb = new XSSFWorkbook(inputStream);
  77. }
  78. if (wb != null) {
  79. Sheet sheet = wb.getSheetAt(0);
  80. diagnoseImportDTO.setDisName(sheet.getSheetName());
  81. int count = 0;
  82. String name, code, standardWord, associatedWord, consequence;
  83. for (Row row : sheet) {
  84. count++;
  85. try {
  86. if (row != null) {
  87. name = getValue(row.getCell(0)).trim().replace(" ", "");
  88. code = getValue(row.getCell(1)).trim().replace(" ", "");
  89. standardWord = getValue(row.getCell(2)).trim();
  90. associatedWord = getValue(row.getCell(3)).trim();
  91. consequence = getValue(row.getCell(4)).trim();
  92. } else {
  93. name = null;
  94. code = null;
  95. standardWord = null;
  96. associatedWord = null;
  97. consequence = null;
  98. }
  99. if (count == 1 && name.equals("类型")) {
  100. continue;
  101. }
  102. if (StringUtil.isEmpty(name)) {
  103. messages.add("第" + count + "行数据不规范,类型必填;");
  104. continue;
  105. }
  106. FeatureDTO feature = new FeatureDTO();
  107. EquationDTO equation = new EquationDTO();
  108. if (code.equals("")) {
  109. equation.setType(name);
  110. equation.setAssociated(associatedWord);
  111. equationList.add(equation);
  112. } else {
  113. feature.setType(name);
  114. feature.setCode(code);
  115. feature.setResult(consequence);
  116. feature.setAssociated(associatedWord);
  117. feature.setStandard(standardWord);
  118. featureList.add(feature);
  119. }
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. throw new CommonException(CommonErrorCode.NOT_EXISTS, e.toString());
  123. // logger.error("",e);
  124. }
  125. }
  126. } else {
  127. // throw new
  128. // CommonException(CommonErrorCode.NOT_EXISTS,
  129. // "非excel文件无法解析!");
  130. messages.add("非excel文件无法解析!");
  131. }
  132. } else {
  133. // throw new CommonException(CommonErrorCode.NOT_EXISTS,
  134. // "未知文件无法解析!");
  135. messages.add("未知文件无法解析!");
  136. }
  137. }
  138. } else {
  139. // throw new CommonException(CommonErrorCode.NOT_EXISTS,
  140. // "无文件上传!");
  141. messages.add("无文件上传!");
  142. }
  143. diagnoseImportDTO.setEquation(equationList);
  144. diagnoseImportDTO.setFeature(featureList);
  145. diagnoseImportDTO.setMessages(messages);
  146. return RespDTO.onSuc(diagnoseImportDTO);
  147. } catch (Exception e) {
  148. e.printStackTrace();
  149. throw new CommonException(CommonErrorCode.NOT_EXISTS, "化验excel文件解析出错!");
  150. // return response.failure("化验excel文件解析出错!");
  151. } finally {
  152. try {
  153. if (inputStream != null) {
  154. inputStream.close();
  155. }
  156. if (wb != null) {
  157. wb.close();
  158. }
  159. } catch (Exception e) {
  160. }
  161. }
  162. }
  163. private String getValue(Cell cell) {
  164. try {
  165. Object obj = null;
  166. switch (cell.getCellTypeEnum()) {
  167. case BOOLEAN:
  168. obj = cell.getBooleanCellValue();
  169. break;
  170. case ERROR:
  171. obj = cell.getErrorCellValue();
  172. break;
  173. case NUMERIC:
  174. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  175. Date date = cell.getDateCellValue();
  176. obj = DateFormatUtils.format(date, "yyyy-MM-dd");
  177. } else {
  178. obj = cell.getNumericCellValue();
  179. DecimalFormat df = new DecimalFormat();
  180. obj = df.format(obj);
  181. }
  182. // obj = cell.getNumericCellValue();
  183. break;
  184. case STRING:
  185. obj = cell.getStringCellValue();
  186. break;
  187. default:
  188. break;
  189. }
  190. return obj.toString();
  191. } catch (Exception e) {
  192. return "";
  193. }
  194. }
  195. public void exportDiagnosticBasis(DiagnosticBasisExportVO diaExportVO, HttpServletResponse response) {
  196. QueryWrapper<DiagnoseQuestion> diaQuery = new QueryWrapper<>();
  197. diaQuery.like("dis_name", diaExportVO.getDisName()).like("question_name", diaExportVO.getQuestion())
  198. .like("type", diaExportVO.getType()).eq("is_deleted", IsDeleteEnum.N.getKey()).orderByDesc("gmt_modified");
  199. List<DiagnoseQuestion> datas = diagnoseQuestionService.list(diaQuery);
  200. GsonUtil.toJson(datas);
  201. System.out.println(GsonUtil.toJson(datas));
  202. List<String> listName = new ArrayList<>();
  203. listName.add("归属诊断");
  204. listName.add("标准术语*");
  205. listName.add("类型*");
  206. listName.add("术语同义词*(多个时用\",\"隔开,必须需要包含本体)");
  207. listName.add("标准术语说明");
  208. listName.add("性别");
  209. listName.add("最小年龄");
  210. listName.add("最大年龄");
  211. List<String> listId = new ArrayList<>();
  212. listId.add("disName");
  213. listId.add("term");
  214. listId.add("type");
  215. listId.add("termSynonym");
  216. listId.add("TermNote");
  217. listId.add("sex");
  218. listId.add("minAge");
  219. listId.add("maxAge");
  220. List<DiagnosticBasisExportDTO> list = new ArrayList<>();
  221. for (int i = 0; i < datas.size(); i++) {
  222. DiagnoseQuestion diagnose = datas.get(i);
  223. list.add(new DiagnosticBasisExportDTO(diagnose.getDisName(),
  224. diagnose.getQuestionName(),
  225. getType(diagnose.getType(), diagnose.getQuestionType()),
  226. diagnose.getQuestionName(), "", "通用", "0",
  227. "200"));
  228. }
  229. response.setContentType("text/html;charset=UTF-8");
  230. ExportBeanExcelUtil ex = new ExportBeanExcelUtil();
  231. ex.exportExcelNew("诊断依据问题词", listName, listId, list, response);
  232. }
  233. /**
  234. * @param diaAllExportVO
  235. * @param response
  236. */
  237. public void exportDiagnosticAll(DiagnosticAllExportVO diaAllExportVO, HttpServletResponse response) {
  238. QueryWrapper<DiagnoseDetail> diaQuery = new QueryWrapper<>();
  239. diaQuery.eq("diagnose_id", diaAllExportVO.getDiagnoseId())
  240. .eq("is_deleted", IsDeleteEnum.N.getKey());
  241. List<DiagnoseDetail> datas = diagnoseDetailService.list(diaQuery);
  242. System.out.println(GsonUtil.toJson(datas));
  243. List<String> listName = new ArrayList<>();
  244. listName.add("类型");
  245. listName.add("序号");
  246. listName.add("标准词*");
  247. listName.add("是否需要病史采集");
  248. listName.add("关联词");
  249. listName.add("结果");
  250. List<String> listId = new ArrayList<>();
  251. listId.add("type");
  252. listId.add("code");
  253. listId.add("standard");
  254. listId.add("historyTaking");
  255. listId.add("relation");
  256. listId.add("result");
  257. String disName = (datas.size() > 0) ? datas.get(0).getDisName() : " ";
  258. List<DiagnosticAllExportDTO> list = new ArrayList<>();
  259. for (int i = 0; i < datas.size(); i++) {
  260. DiagnoseDetail diagnose = datas.get(i);
  261. disName = diagnose.getDisName();
  262. list.add(new DiagnosticAllExportDTO(getDisType(diagnose.getType()), diagnose.getCode(),
  263. diagnose.getStandard(), "", getRelation(diagnose.getType(), diagnose),
  264. diagnose.getResult()));
  265. }
  266. response.setContentType("text/html;charset=UTF-8");
  267. ExportBeanExcelUtil ex = new ExportBeanExcelUtil();
  268. ex.exportExcelNew(disName, listName, listId, list, response);
  269. }
  270. /**
  271. * 导出类型转换
  272. *
  273. * @param type
  274. * @param questionType
  275. * @return
  276. */
  277. public String getType(Integer type, Integer questionType) {
  278. String typeName = "";
  279. if (type == DiagnoseFeatureTypeEnum.Dis.getKey()) {
  280. typeName = TermEnum.getName(0);
  281. } else if (type == DiagnoseFeatureTypeEnum.Symptom.getKey()) {
  282. typeName = TermEnum.getName(1);
  283. } else if (type == DiagnoseFeatureTypeEnum.Vital.getKey()) {
  284. typeName = TermEnum.getName(2);
  285. } else if (type == DiagnoseFeatureTypeEnum.Lis.getKey()) {
  286. if (questionType == 2) {
  287. typeName = TermEnum.getName(3);
  288. }
  289. if (questionType == 3) {
  290. typeName = TermEnum.getName(4);
  291. }
  292. if (questionType == 4) {
  293. typeName = TermEnum.getName(5);
  294. }
  295. } else if (type == DiagnoseFeatureTypeEnum.Pacs.getKey()) {
  296. if (questionType == 2) {
  297. typeName = TermEnum.getName(6);
  298. }
  299. if (questionType == 3) {
  300. typeName = TermEnum.getName(7);
  301. }
  302. } else if (type == DiagnoseFeatureTypeEnum.Antidiastole.getKey()) {
  303. typeName = TermEnum.getName(8);
  304. } else if (type == DiagnoseFeatureTypeEnum.History.getKey()) {
  305. typeName = TermEnum.getName(8);
  306. } else if (type == DiagnoseFeatureTypeEnum.Cause.getKey()) {
  307. typeName = TermEnum.getName(9);
  308. } else if (type == DiagnoseFeatureTypeEnum.CourseOfDisease.getKey()) {
  309. typeName = TermEnum.getName(10);
  310. } else if (type == DiagnoseFeatureTypeEnum.Other.getKey()) {
  311. typeName = TermEnum.getName(10);
  312. }
  313. return typeName;
  314. }
  315. /**
  316. * 导出公式类型转换
  317. */
  318. public String getDisType(Integer type) {
  319. String typeName = "";
  320. typeName = DiagnoseFeatureTypeEnum.getName(type);
  321. if (typeName == null) {
  322. typeName = DiagnoseTypeEnum.getName(type);
  323. }
  324. return typeName;
  325. }
  326. /**
  327. * 导出公式和标准词切换
  328. */
  329. public String getRelation(Integer type, DiagnoseDetail diagnose) {
  330. String typeName = "";
  331. String relation = "";
  332. typeName = DiagnoseFeatureTypeEnum.getName(type);
  333. relation = diagnose.getRelation();
  334. if (typeName == null) {
  335. relation = diagnose.getFormula();
  336. }
  337. return relation;
  338. }
  339. }