MedicalFacade.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. package com.diagbot.facade;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.diagbot.client.UserServiceClient;
  5. import com.diagbot.dto.GetMedicalInfoDetailDTO;
  6. import com.diagbot.dto.GetMedicalInfoListDTO;
  7. import com.diagbot.dto.RespDTO;
  8. import com.diagbot.entity.Concept;
  9. import com.diagbot.entity.LibraryInfo;
  10. import com.diagbot.entity.Medical;
  11. import com.diagbot.enums.LexiconTypeEnum;
  12. import com.diagbot.exception.CommonErrorCode;
  13. import com.diagbot.exception.CommonException;
  14. import com.diagbot.service.MedicalService;
  15. import com.diagbot.service.impl.MedicalServiceImpl;
  16. import com.diagbot.util.BeanUtil;
  17. import com.diagbot.util.DateUtil;
  18. import com.diagbot.util.StringUtil;
  19. import com.diagbot.util.UserUtils;
  20. import com.diagbot.vo.AddMedicalInfoVO;
  21. import com.diagbot.vo.GetMedicalInfoDetailVO;
  22. import com.diagbot.vo.GetMedicalInfoListVO;
  23. import com.diagbot.vo.RemoveMedicalInfoVO;
  24. import org.apache.commons.lang.time.DateFormatUtils;
  25. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  26. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.Row;
  29. import org.apache.poi.ss.usermodel.Sheet;
  30. import org.apache.poi.ss.usermodel.Workbook;
  31. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.beans.factory.annotation.Qualifier;
  34. import org.springframework.stereotype.Component;
  35. import org.springframework.web.multipart.MultipartFile;
  36. import java.io.InputStream;
  37. import java.text.DecimalFormat;
  38. import java.util.ArrayList;
  39. import java.util.Date;
  40. import java.util.HashMap;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.stream.Collectors;
  44. /**
  45. * @Description: 术语临床信息查询业务层
  46. * @author: Weixuan Huang
  47. * @time: 2019/1/16 16:07
  48. */
  49. @Component
  50. public class MedicalFacade extends MedicalServiceImpl {
  51. @Autowired
  52. private ConceptFacade conceptFacade;
  53. @Autowired
  54. private LibraryInfoFacade libraryInfoFacade;
  55. @Autowired
  56. @Qualifier("medicalServiceImpl")
  57. private MedicalService medicalService;
  58. @Autowired
  59. private UserServiceClient userServiceClient;
  60. /**
  61. * 获取医学术语命名列表
  62. *
  63. * @param getMedicalInfoListVO
  64. * @return
  65. */
  66. public IPage<GetMedicalInfoListDTO> getMedicalInfoList(GetMedicalInfoListVO getMedicalInfoListVO) {
  67. IPage<GetMedicalInfoListDTO> ipage = this.baseMapper.getMedicalInfoList(getMedicalInfoListVO);
  68. if (ipage.getRecords().size() == 0) {
  69. return ipage;
  70. }
  71. List<String> ids = ipage.getRecords().stream().map(i -> i.getOperName()).distinct().collect(Collectors.toList());
  72. RespDTO<Map<String, String>> respDTO = userServiceClient.getUserInfoByIds(ids);
  73. if (respDTO == null || !CommonErrorCode.OK.getCode().equals(respDTO.code)) {
  74. throw new CommonException(CommonErrorCode.RPC_ERROR,
  75. "获取用户信息失败");
  76. }
  77. ipage.getRecords().forEach(i -> {
  78. i.setOperName(respDTO.data.get(i.getOperName()));
  79. });
  80. return ipage;
  81. }
  82. /**
  83. * 医学术语命名删除
  84. *
  85. * @param removeMedicalInfoVO
  86. * @return
  87. */
  88. public Boolean removeMedicalInfo(RemoveMedicalInfoVO removeMedicalInfoVO) {
  89. Medical medical = this.getById(removeMedicalInfoVO.getMedicalId());
  90. if (medical == null || medical.getIsDeleted().equals("Y")) {
  91. throw new CommonException(CommonErrorCode.NOT_EXISTS);
  92. }
  93. medical.setIsDeleted("Y");
  94. medical.setModifier(UserUtils.getCurrentPrincipleID());
  95. medical.setGmtModified(DateUtil.now());
  96. return updateById(medical);
  97. }
  98. /**
  99. * 获取术语医学属性详情
  100. *
  101. * @param getMedicalInfoDetailVO
  102. * @return
  103. */
  104. public GetMedicalInfoDetailDTO getMedicalInfoDetail(GetMedicalInfoDetailVO getMedicalInfoDetailVO) {
  105. Medical medical = this.getById(getMedicalInfoDetailVO.getMedicalId());
  106. if (medical == null || medical.getIsDeleted().equals("Y")) {
  107. throw new CommonException(CommonErrorCode.NOT_EXISTS);
  108. }
  109. QueryWrapper<LibraryInfo> libraryInfoQe = new QueryWrapper<LibraryInfo>();
  110. libraryInfoQe.eq("is_deleted", "N");
  111. libraryInfoQe.eq("is_concept", "1");
  112. libraryInfoQe.eq("concept_id", medical.getConceptId());
  113. LibraryInfo libraryInfo = libraryInfoFacade.getOne(libraryInfoQe);
  114. if (libraryInfo == null) {
  115. throw new CommonException(CommonErrorCode.RPC_ERROR, "术语医学属性所属概念不存在!");
  116. }
  117. GetMedicalInfoDetailDTO getMedicalInfoDetailDTO = new GetMedicalInfoDetailDTO();
  118. BeanUtil.copyProperties(medical, getMedicalInfoDetailDTO);
  119. getMedicalInfoDetailDTO.setConceptName(libraryInfo.getName());
  120. getMedicalInfoDetailDTO.setConceptType(LexiconTypeEnum.getName(libraryInfo.getTypeId().intValue()));
  121. return getMedicalInfoDetailDTO;
  122. }
  123. /**
  124. * 术语医学属性添加或者编辑
  125. *
  126. * @param addMedicalInfoVO
  127. * @return
  128. */
  129. public Boolean addMedicalInfo(AddMedicalInfoVO addMedicalInfoVO) {
  130. Map<String, Object> ckMap = checkAddMedicalInfoVO(addMedicalInfoVO);
  131. Object message = ckMap.get("message");
  132. if (message != null) {
  133. throw new CommonException(CommonErrorCode.RPC_ERROR, message.toString());
  134. }
  135. return saveOrUpdate((Medical) (ckMap.get("medical")));
  136. }
  137. /**
  138. * 校验即将添加的数据
  139. *
  140. * @param addMedicalInfoVO
  141. * @return 返回map中,包含2个字段:message和medical,message-为空时,表示正常,取medical
  142. */
  143. private Map<String, Object> checkAddMedicalInfoVO(AddMedicalInfoVO addMedicalInfoVO) {
  144. Map<String, Object> retMap = new HashMap<>();
  145. QueryWrapper<LibraryInfo> libraryInfoQe = new QueryWrapper<LibraryInfo>();
  146. libraryInfoQe.eq("is_deleted", "N");
  147. libraryInfoQe.eq("is_concept", "1");
  148. libraryInfoQe.eq("name", addMedicalInfoVO.getConceptName());
  149. libraryInfoQe.eq("type", addMedicalInfoVO.getConceptType());
  150. LibraryInfo libraryInfo = libraryInfoFacade.getOne(libraryInfoQe);
  151. if (libraryInfo == null) {
  152. retMap.put("message", "术语概念名称不存在");
  153. return retMap;
  154. }
  155. Concept concept = conceptFacade.getById(libraryInfo.getConceptId());
  156. if (concept == null || concept.getIsDeleted().equals("Y")) {
  157. retMap.put("message", "术语概念名称不存在");
  158. return retMap;
  159. }
  160. List<String> infoNames = new ArrayList<>();
  161. infoNames.add(addMedicalInfoVO.getName());
  162. infoNames.add(addMedicalInfoVO.getStdName());
  163. infoNames.add(addMedicalInfoVO.getCateName());
  164. infoNames.add(addMedicalInfoVO.getGrp());
  165. QueryWrapper<LibraryInfo> libraryInfoQe1 = new QueryWrapper<LibraryInfo>();
  166. libraryInfoQe1.in("name", infoNames);
  167. libraryInfoQe1.eq("is_concept", "1");
  168. libraryInfoQe1.eq("is_deleted", "N");
  169. Map<String, List<LibraryInfo>> libraryInfoListMap = libraryInfoFacade.list(libraryInfoQe1).stream().collect(Collectors.groupingBy(LibraryInfo::getName));
  170. if (libraryInfoListMap.get(addMedicalInfoVO.getName()).size() == 0) {
  171. retMap.put("message", "术语名称不存在");
  172. return retMap;
  173. }
  174. if (libraryInfoListMap.get(addMedicalInfoVO.getStdName()).size() == 0) {
  175. retMap.put("message", "术语标准名不存在");
  176. return retMap;
  177. }
  178. if (libraryInfoListMap.get(addMedicalInfoVO.getCateName()).size() == 0) {
  179. retMap.put("message", "术语小类名称不存在");
  180. return retMap;
  181. }
  182. if (libraryInfoListMap.get(addMedicalInfoVO.getGrp()).size() == 0) {
  183. retMap.put("message", "术语类组不存在");
  184. return retMap;
  185. }
  186. // QueryWrapper<Bodypart> bodypartQe = new QueryWrapper<>();
  187. // bodypartQe.eq("is_deleted", "N");
  188. // bodypartQe.eq("bodypart", addMedicalInfoVO.getBodypart());
  189. // if(bodypartFacade.list(bodypartQe).size()==0){
  190. // retMap.put("message", "一级部位不存在");
  191. // return retMap;
  192. // }
  193. // bodypartQe.eq("sub_bodypart", addMedicalInfoVO.getSubBodypart());
  194. // if(bodypartFacade.list(bodypartQe).size()==0){
  195. // retMap.put("message", "二级部位不存在");
  196. // return retMap;
  197. // }
  198. String currentUser = UserUtils.getCurrentPrincipleID();
  199. Date now = DateUtil.now();
  200. QueryWrapper<Medical> medicalQe = new QueryWrapper<>();
  201. medicalQe.eq("concept_id", libraryInfo.getConceptId());
  202. Medical medical = this.getOne(medicalQe);
  203. if (medical == null) {
  204. medical = new Medical();
  205. medical.setCreator(currentUser);
  206. medical.setGmtCreated(now);
  207. }
  208. medical.setIsDeleted("N");
  209. medical.setModifier(currentUser);
  210. medical.setGmtModified(now);
  211. medical.setConceptId(libraryInfo.getConceptId());
  212. BeanUtil.copyProperties(addMedicalInfoVO, medical);
  213. retMap.put("medical", medical);
  214. return retMap;
  215. }
  216. /**
  217. * 术语医学属性excel文件导入
  218. *
  219. * @param file
  220. * @return
  221. */
  222. public Boolean medicalInfoExcelIm(MultipartFile file) {
  223. List<AddMedicalInfoVO> addMedicalInfoVOList = new ArrayList<>();
  224. StringBuffer sbf = new StringBuffer();
  225. InputStream inputStream = null;
  226. Workbook wb = null;
  227. try {
  228. if (!file.isEmpty()) {
  229. inputStream = file.getInputStream();
  230. if (inputStream.available() > 512000) {
  231. sbf.append("文件最大支持500KB!").append("<br/>");
  232. } else {
  233. String fileName = file.getOriginalFilename();
  234. if (fileName.lastIndexOf(".") != -1) {
  235. String type = fileName.substring(fileName.lastIndexOf("."));
  236. if (type.equals(".xls")) {
  237. wb = new HSSFWorkbook(inputStream);
  238. } else if (type.equals(".xlsx")) {
  239. wb = new XSSFWorkbook(inputStream);
  240. }
  241. if (wb != null) {
  242. Sheet sheet = wb.getSheetAt(0);
  243. int count = 0;
  244. String conceptName, conceptType, name, stdName, cateName, grp, gender, minAge, maxAge, bodypart, subBodypart, displayOrder, dept, code, note;
  245. Integer minAge_, maxAge_;
  246. for (Row row : sheet) {
  247. count++;
  248. if (count == 1 || row == null) {
  249. continue;
  250. }
  251. conceptName = getValue(row.getCell(0)).trim().replace(" ", "");
  252. conceptType = getValue(row.getCell(1)).trim().replace(" ", "");
  253. name = getValue(row.getCell(2)).trim().replace(" ", "");
  254. stdName = getValue(row.getCell(3)).trim().replace(" ", "");
  255. cateName = getValue(row.getCell(4)).trim().replace(" ", "");
  256. grp = getValue(row.getCell(5)).trim().replace(" ", "");
  257. gender = getValue(row.getCell(6)).trim().replace(" ", "");
  258. minAge = getValue(row.getCell(7)).trim().replace(" ", "");
  259. maxAge = getValue(row.getCell(8)).trim().replace(" ", "");
  260. bodypart = getValue(row.getCell(9)).trim().replace(" ", "");
  261. subBodypart = getValue(row.getCell(10)).trim().replace(" ", "");
  262. displayOrder = getValue(row.getCell(11)).trim().replace(" ", "");
  263. dept = getValue(row.getCell(12)).trim().replace(" ", "");
  264. code = getValue(row.getCell(13)).trim().replace(" ", "");
  265. note = getValue(row.getCell(14)).trim().replace(" ", "");
  266. if (StringUtil.isEmpty(conceptName) && StringUtil.isEmpty(name)
  267. && StringUtil.isEmpty(stdName) && StringUtil.isEmpty(cateName)
  268. && StringUtil.isEmpty(grp) && StringUtil.isEmpty(gender)
  269. && StringUtil.isEmpty(minAge) && StringUtil.isEmpty(maxAge)
  270. && StringUtil.isEmpty(bodypart) && StringUtil.isEmpty(subBodypart)
  271. && StringUtil.isEmpty(displayOrder) && StringUtil.isEmpty(dept)
  272. && StringUtil.isEmpty(code) && StringUtil.isEmpty(note)
  273. && StringUtil.isEmpty(conceptType)) {
  274. continue;
  275. }
  276. if (StringUtil.isEmpty(conceptName) || StringUtil.isEmpty(name)
  277. || StringUtil.isEmpty(stdName) || StringUtil.isEmpty(cateName)
  278. || StringUtil.isEmpty(grp) || StringUtil.isEmpty(gender)
  279. || StringUtil.isEmpty(minAge) || StringUtil.isEmpty(maxAge)
  280. || StringUtil.isEmpty(bodypart) || StringUtil.isEmpty(subBodypart)
  281. || StringUtil.isEmpty(dept) || StringUtil.isEmpty(conceptType)) {
  282. sbf.append("第" + count + "行数据不完整;").append("<br/>");
  283. continue;
  284. }
  285. if (!gender.equals("1") && !gender.equals("2") && !gender.equals("3")) {
  286. sbf.append("第" + count + "行数据中性别非1、2、3;").append("<br/>");
  287. continue;
  288. }
  289. AddMedicalInfoVO addMedicalInfoVO = new AddMedicalInfoVO();
  290. addMedicalInfoVO.setConceptName(conceptName);
  291. addMedicalInfoVO.setConceptType(conceptType);
  292. addMedicalInfoVO.setName(name);
  293. addMedicalInfoVO.setStdName(stdName);
  294. addMedicalInfoVO.setCateName(cateName);
  295. addMedicalInfoVO.setGrp(grp);
  296. addMedicalInfoVO.setGender(gender);
  297. addMedicalInfoVO.setBodypart(bodypart);
  298. addMedicalInfoVO.setSubBodypart(subBodypart);
  299. addMedicalInfoVO.setDisplayOrder(displayOrder);
  300. addMedicalInfoVO.setDept(dept);
  301. addMedicalInfoVO.setCode(code);
  302. addMedicalInfoVO.setNote(note);
  303. try {
  304. minAge_ = Integer.parseInt(minAge);
  305. maxAge_ = Integer.parseInt(maxAge);
  306. } catch (Exception e) {
  307. minAge_ = null;
  308. maxAge_ = null;
  309. }
  310. if (minAge_ == null || maxAge_ == null) {
  311. sbf.append("第" + count + "行数据中年龄非整数;").append("<br/>");
  312. continue;
  313. }
  314. addMedicalInfoVO.setMinAge(minAge_);
  315. addMedicalInfoVO.setMaxAge(maxAge_);
  316. addMedicalInfoVOList.add(addMedicalInfoVO);
  317. }
  318. } else {
  319. sbf.append("非excel文件无法解析!").append("<br/>");
  320. }
  321. } else {
  322. sbf.append("未知文件无法解析!").append("<br/>");
  323. }
  324. }
  325. } else {
  326. sbf.append("无文件上传!").append("<br/>");
  327. }
  328. } catch (Exception e) {
  329. sbf.append("解析失败!").append("<br/>");
  330. } finally {
  331. try {
  332. if (wb != null) {
  333. wb.close();
  334. }
  335. if (inputStream != null) {
  336. inputStream.close();
  337. }
  338. } catch (Exception e) {
  339. }
  340. }
  341. /*****************excel文件本身问题提醒************************/
  342. if (sbf.length() > 0) {
  343. throw new CommonException(CommonErrorCode.RPC_ERROR, sbf.toString());
  344. }
  345. /****************excel文件中所有术语概念存在重复提示---术语概念名称和类型一样即重复***********************/
  346. Map<String, List<AddMedicalInfoVO>> addMedicalInfoVOListMap = addMedicalInfoVOList.stream().collect(Collectors.groupingBy(AddMedicalInfoVO::getConceptName));
  347. for (String key : addMedicalInfoVOListMap.keySet()) {
  348. if (addMedicalInfoVOListMap.get(key).size() > 1 && addMedicalInfoVOListMap.get(key).stream().map(i -> i.getConceptType()).distinct().count() == 1) {
  349. sbf.append(key).append(" ");
  350. }
  351. }
  352. if (sbf.length() > 0) {
  353. sbf.append("(术语概念名称存在重复)");
  354. throw new CommonException(CommonErrorCode.RPC_ERROR, sbf.toString());
  355. }
  356. /**************校验每一组数据,同时组装*************************/
  357. List<Medical> saveOrUpdateMedicalList = new ArrayList<>();
  358. Map<String, Object> ckMap = null;
  359. Object message = null;
  360. for (AddMedicalInfoVO i : addMedicalInfoVOList) {
  361. ckMap = checkAddMedicalInfoVO(i);
  362. message = ckMap.get("message");
  363. if (message != null) {
  364. sbf.append(message.toString()).append("(").append(i.getConceptName()).append(")").append("<br/>");
  365. } else {
  366. saveOrUpdateMedicalList.add((Medical) (ckMap.get("medical")));
  367. }
  368. }
  369. if (sbf.length() > 0) {
  370. throw new CommonException(CommonErrorCode.RPC_ERROR, sbf.toString());
  371. }
  372. return medicalService.saveOrUpdateBatch(saveOrUpdateMedicalList);
  373. }
  374. @SuppressWarnings("deprecation")
  375. private String getValue(Cell cell) {
  376. try {
  377. Object obj = null;
  378. switch (cell.getCellTypeEnum()) {
  379. case BOOLEAN:
  380. obj = cell.getBooleanCellValue();
  381. break;
  382. case ERROR:
  383. obj = cell.getErrorCellValue();
  384. break;
  385. case NUMERIC:
  386. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  387. Date date = cell.getDateCellValue();
  388. obj = DateFormatUtils.format(date, "yyyy-MM-dd");
  389. } else {
  390. obj = cell.getNumericCellValue();
  391. DecimalFormat df = new DecimalFormat("0");
  392. obj = df.format(obj);
  393. }
  394. break;
  395. case STRING:
  396. obj = cell.getStringCellValue();
  397. break;
  398. default:
  399. break;
  400. }
  401. return obj.toString();
  402. } catch (Exception e) {
  403. return "";
  404. }
  405. }
  406. }