EmrServer.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. package thyyxxk.webserver.service.zhuyuanyisheng.emr;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.alibaba.fastjson.serializer.SerializerFeature;
  6. import com.baomidou.dynamic.datasource.annotation.DS;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import org.springframework.web.client.RestTemplate;
  12. import thyyxxk.webserver.config.exception.ExceptionEnum;
  13. import thyyxxk.webserver.constants.Capacity;
  14. import thyyxxk.webserver.constants.EmrType;
  15. import thyyxxk.webserver.dao.his.LoginDao;
  16. import thyyxxk.webserver.dao.his.zhuyuanyisheng.EmrPatientDao;
  17. import thyyxxk.webserver.entity.ResultVo;
  18. import thyyxxk.webserver.entity.datamodify.YzTemperature;
  19. import thyyxxk.webserver.entity.dictionary.CodeName;
  20. import thyyxxk.webserver.entity.drg.AuxiliaryFillingOfDiagnosis;
  21. import thyyxxk.webserver.entity.login.UserInfo;
  22. import thyyxxk.webserver.entity.medicalinsurance.inpatient.ZyPatientInfo;
  23. import thyyxxk.webserver.entity.zhuyuanyisheng.ZyZkList;
  24. import thyyxxk.webserver.entity.zhuyuanyisheng.emr.*;
  25. import thyyxxk.webserver.entity.zhuyuanyisheng.jianyanjiancha.YshYjReq;
  26. import thyyxxk.webserver.service.PublicServer;
  27. import thyyxxk.webserver.service.externalhttp.emr.EmrEditor;
  28. import thyyxxk.webserver.service.redislike.RedisLikeService;
  29. import thyyxxk.webserver.utils.*;
  30. import thyyxxk.webserver.websocket.WebSocketServer;
  31. import javax.websocket.Session;
  32. import java.util.*;
  33. import java.util.stream.Collectors;
  34. @Service
  35. @Slf4j
  36. @DS("his")
  37. public class EmrServer {
  38. private final EmrPatientDao dao;
  39. private final EmrEditor emr;
  40. private final PublicServer publicServer;
  41. private final LoginDao loginDao;
  42. private final RedisLikeService redisLikeService;
  43. public EmrServer(EmrPatientDao dao, EmrEditor emr, PublicServer publicServer, LoginDao loginDao, RedisLikeService redisLikeService) {
  44. this.dao = dao;
  45. this.emr = emr;
  46. this.publicServer = publicServer;
  47. this.loginDao = loginDao;
  48. this.redisLikeService = redisLikeService;
  49. }
  50. /**
  51. * 获取这个患者这个住院次数的全部电子病历
  52. *
  53. * @param patNo 住院号
  54. * @param times 住院次数
  55. * @return 数据
  56. */
  57. public ResultVo<List<EmrPatientData>> getPatientDataTree(String patNo, Integer times) {
  58. List<EmrPatientData> data = new ArrayList<>();
  59. Map<String, List<EmrPatientData>> map = dao.getPatientData(patNo, times).stream().collect(Collectors.groupingBy(EmrPatientData::getEmrCategoryCode));
  60. for (Map.Entry<String, List<EmrPatientData>> key : map.entrySet()) {
  61. if (key.getValue().size() == 1) {
  62. data.add(key.getValue().get(0));
  63. } else {
  64. EmrPatientData item = new EmrPatientData();
  65. List<EmrPatientData> children = new ArrayList<>();
  66. for (int i = 0, len = key.getValue().size(); i < len; i++) {
  67. EmrPatientData childNode = key.getValue().get(i);
  68. children.add(childNode);
  69. }
  70. item.setName(key.getValue().get(0).getEmrName());
  71. item.setChildren(new ArrayList<>()).getChildren().addAll(children);
  72. data.add(item);
  73. }
  74. }
  75. return ResultVoUtil.success(data);
  76. }
  77. public ResultVo<Map<String, List<JSONObject>>> getEmrTree(String type) {
  78. Map<String, List<JSONObject>> map = new HashMap<>(Capacity.TWO);
  79. if ("all".equals(type)) {
  80. map.put("all", wholeHospitalTemplate());
  81. map.put("dept", getDeptTemplate());
  82. } else if ("dept".equals(type)) {
  83. map.put("dept", getDeptTemplate());
  84. } else if ("hosp".equals(type)) {
  85. map.put("all", wholeHospitalTemplate());
  86. }
  87. return ResultVoUtil.success(map);
  88. }
  89. private List<JSONObject> getDeptTemplate() {
  90. UserInfo user = redisLikeService.getUserInfoByToken();
  91. JSONArray data = new JSONArray();
  92. if (ListUtil.notBlank(user.getPartTimeDept())) {
  93. user.getPartTimeDept().forEach(item -> {
  94. data.addAll(emr.getDeptList(item));
  95. });
  96. }
  97. data.addAll(emr.getDeptList(user.getDeptCode()));
  98. return emrToTree(data);
  99. }
  100. /**
  101. * 获取 整个医院模板
  102. */
  103. private List<JSONObject> wholeHospitalTemplate() {
  104. JSONArray data = emr.getEmrTree();
  105. return emrToTree(data);
  106. }
  107. /**
  108. * 电子病历转成树状图 二次封装
  109. *
  110. * @param data 模板数据
  111. * @return 返回
  112. */
  113. public List<JSONObject> emrToTree(JSONArray data) {
  114. List<JSONObject> tree = new ArrayList<>();
  115. if (data.isEmpty()) {
  116. return tree;
  117. }
  118. emrArrToTree(data, tree);
  119. return tree;
  120. }
  121. /**
  122. * 电子病历转成树状图
  123. *
  124. * @param data 电子病历
  125. * @param tree 树
  126. */
  127. private static void emrArrToTree(JSONArray data, List<JSONObject> tree) {
  128. Map<String, JSONObject> map = new HashMap<>(data.size());
  129. for (int i = 0; i < data.size(); i++) {
  130. JSONObject item = data.getJSONObject(i);
  131. if (item.get("parent") == null) {
  132. tree.add(item);
  133. }
  134. map.put(item.getString("_id"), item);
  135. }
  136. for (int i = 0; i < data.size(); i++) {
  137. JSONObject item = data.getJSONObject(i);
  138. if (map.containsKey(item.getString("parent"))) {
  139. JSONObject parentList = map.get(item.getString("parent"));
  140. if (parentList.get("children") == null) {
  141. parentList.put("children", new JSONArray());
  142. }
  143. parentList.getJSONArray("children").add(item);
  144. }
  145. }
  146. }
  147. /**
  148. * 获取电子病历的片段
  149. *
  150. * @return 树
  151. */
  152. public ResultVo<List<JSONObject>> getSnippetTree() {
  153. JSONArray data = emr.getSnippetTree();
  154. List<JSONObject> tree = new ArrayList<>();
  155. emrArrToTree(data, tree);
  156. return ResultVoUtil.success(tree);
  157. }
  158. /**
  159. * 提取患者数据元
  160. *
  161. * @param param 结构化文档
  162. */
  163. public Map<String, Object> extractDataElement(EmrPatientData param) {
  164. EmrDataExtract emrDataExtract = dao.extractDataSource(param.getEmrCategoryCode());
  165. if (emrDataExtract == null) {
  166. return new HashMap<>();
  167. }
  168. // 获取 需要提取的数据元
  169. List<String> strings = JSON.parseArray(emrDataExtract.getDataExtract(), String.class);
  170. // 获取前端传入的数据元
  171. JSONObject elementData = param.getEmrDataElement();
  172. // 提取到的数据
  173. Map<String, Object> extractedData = new HashMap<>(strings.size());
  174. for (String el : strings) {
  175. if (elementData.containsKey(el)) {
  176. JSONObject item = elementData.getJSONObject(el);
  177. Object value = item.get("value");
  178. if (value != null) {
  179. extractedData.put(el, value);
  180. }
  181. }
  182. }
  183. EmrDataElement emrDataElement = dao.obtainPatientSOriginalData(param.getPatNo(), param.getTimes());
  184. if (emrDataElement == null) {
  185. // 创建的病历可以解锁了
  186. dao.insertDataSource(param.getPatNo(), param.getTimes(), JSON.toJSONString(extractedData));
  187. } else {
  188. JSONObject jsonObject = JSONObject.parseObject(emrDataElement.getDataElement());
  189. log.info("患者原来的数据:{}", jsonObject);
  190. jsonObject.putAll(extractedData);
  191. dao.updatePatientDataSource(param.getPatNo(), param.getTimes(), JSON.toJSONString(jsonObject));
  192. }
  193. dao.unlockMedicalRecords(param);
  194. log.info("提取到的数据:{}", JSON.toJSONString(extractedData));
  195. return extractedData;
  196. }
  197. /**
  198. * 保存患者电子病历信息
  199. *
  200. * @param param 参数
  201. * @return 提示
  202. */
  203. public ResultVo<Map<String, Object>> insertEmrData(EmrPatientData param) {
  204. JSONObject saveJson = new JSONObject();
  205. saveJson.put("document", param.getDocumentData());
  206. try {
  207. emr.saveDocument(saveJson);
  208. } catch (Exception e) {
  209. e.printStackTrace();
  210. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病历保存错误,请重试!" + e.getMessage());
  211. }
  212. param.setCreateId(TokenUtil.getTokenUserId());
  213. boolean isUpdated = dao.whetherThereIsAMedicalRecord(param.getEmrDocumentId()).equals(1);
  214. Map<String, Object> extractedData = extractDataElement(param);
  215. if (isUpdated) {
  216. dao.updateCreatedTemplate(param);
  217. } else {
  218. dao.emrInsertForTheFirstTime(param);
  219. }
  220. return ResultVoUtil.success(extractedData);
  221. }
  222. /**
  223. * 根据患者的电子病历 id 来删除
  224. * 这里用的是逻辑删除
  225. *
  226. * @param documentId 电子病历id
  227. * @return 删除电子病历的同时需要删除, 提取到的数据源.
  228. */
  229. public ResultVo<List<String>> deletePatientEmrByDocumentId(String documentId) {
  230. try {
  231. emr.deleteEmr(documentId);
  232. } catch (Exception e) {
  233. e.printStackTrace();
  234. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存病历失败,请重试!" + e.getMessage());
  235. }
  236. EmrPatientData patientData = dao.getCategoryCodeByDocumentId(documentId);
  237. EmrDataExtract emrDataExtract = dao.extractDataSource(patientData.getEmrCategoryCode());
  238. List<String> strings = new ArrayList<>();
  239. if (emrDataExtract != null) {
  240. // 获取 需要提取的数据元
  241. strings = JSON.parseArray(emrDataExtract.getDataExtract(), String.class);
  242. log.info("需要提取的数据:{}", strings);
  243. // 获取到已经提取的数据
  244. EmrDataElement emrDataElement = dao.obtainPatientSOriginalData(patientData.getPatNo(), patientData.getTimes());
  245. // 转 json
  246. JSONObject jsonObject = JSONObject.parseObject(emrDataElement.getDataElement());
  247. for (String string : strings) {
  248. // 删除提取到的数据
  249. jsonObject.remove(string);
  250. }
  251. // 更新患者数据元
  252. dao.updatePatientDataSource(patientData.getPatNo(), patientData.getTimes(), JSON.toJSONString(jsonObject));
  253. }
  254. // 删除病历
  255. dao.deletePatientEmrByDocumentId(documentId, TokenUtil.getTokenUserId());
  256. return ResultVoUtil.success(strings);
  257. }
  258. /**
  259. * 查询患者是否有指定的病历了
  260. *
  261. * @param param 住院号,住院次数,病历编码
  262. * @return 返回 Boolean
  263. */
  264. public ResultVo<Boolean> queryWhetherThePatientHasASpecifiedMedicalRecord(EmrPatientData param) {
  265. return ResultVoUtil.success(dao.queryWhetherThePatientHasASpecifiedMedicalRecord(param.getPatNo(), param.getTimes(), param.getEmrCategoryCode()) == 1);
  266. }
  267. /**
  268. * 医生诊断热缩
  269. *
  270. * @param userCode 医生编码
  271. * @param code 诊断 code
  272. * @param tableName 表名
  273. */
  274. public void hotSearchSorting(String userCode, String code, String tableName) {
  275. boolean insert = dao.queryWhetherThereIsHotSearch(userCode, code, tableName) == 0;
  276. if (insert) {
  277. dao.hotSearchSorting(userCode, code, tableName);
  278. } else {
  279. dao.updateHotSearch(userCode, code, tableName);
  280. }
  281. log.info("医生编码:{},搜索关键字:{},表名:{}", userCode, code, tableName);
  282. }
  283. /**
  284. * 根据文档 id 提交病历
  285. *
  286. * @param documentId 文档id
  287. * @return 返回数据
  288. */
  289. public ResultVo<String> submitMedicalRecord(String documentId) {
  290. String userCode = TokenUtil.getTokenUserId();
  291. String createdId = dao.getDocumentIdCreatedBy(documentId);
  292. log.info("提交病历==》 操作人:{},病历id:{},创建人:{}", userCode, documentId, createdId);
  293. if (userCode.equals(createdId)) {
  294. dao.updateSubmissionFlag(documentId);
  295. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "提交成功");
  296. }
  297. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "创建人不一致无法修改。");
  298. }
  299. /**
  300. * 获取电子病历数据
  301. *
  302. * @param panNo 住院号
  303. * @param times 住院次数
  304. * @param etType 类型
  305. * @return 返回数据
  306. */
  307. public JSONArray getEmrData(String panNo, Integer times, EmrType etType) {
  308. // 查询这个病历的 唯一 id
  309. List<String> idList = dao.getDocumentIdByPatietn(panNo, times, etType.getName());
  310. JSONArray returnArray = new JSONArray();
  311. if (ListUtil.isBlank(idList)) {
  312. return returnArray;
  313. }
  314. Map<String, JSONObject> surgicalSequencing = new TreeMap<>();
  315. for (String s : idList) {
  316. JSONObject jsonObject = emr.getEditorJsonDataByDocumentId(s);
  317. if (etType.getCode() == EmrType.DIAGNOSIS.getCode()) {
  318. returnArray.addAll(extractDiagnosis(jsonObject));
  319. } else if (etType.getCode() == EmrType.OPERATION.getCode()) {
  320. String key = DateUtil.formatDatetime(jsonObject.getJSONObject("手术日期").getDate("value"));
  321. surgicalSequencing.put(key, jsonObject);
  322. }
  323. }
  324. if (!surgicalSequencing.isEmpty()) {
  325. for (Map.Entry<String, JSONObject> item : surgicalSequencing.entrySet()) {
  326. returnArray.add(item.getValue());
  327. }
  328. }
  329. return returnArray;
  330. }
  331. /**
  332. * 获取患者提取到的数据
  333. *
  334. * @param patNo 住院号
  335. * @param times 次数
  336. * @return 返回 json
  337. */
  338. public JSONObject getEmrPatientData(String patNo, Integer times) {
  339. String js = dao.getEmrPatientData(patNo, times);
  340. if (StringUtil.isBlank(js)) {
  341. return new JSONObject();
  342. }
  343. return JSON.parseObject(js);
  344. }
  345. /**
  346. * 获取患者的手术
  347. *
  348. * @param patNo 获取住院号
  349. * @param times 获取住院次数
  350. * @return 返回数据
  351. */
  352. public JSONArray getPatientSurgery(String patNo, Integer times) {
  353. List<String> idList = dao.getDocumentIdByPatietn(patNo, times, "a2a2866054fe11edb28ac955a5f5cad1");
  354. JSONArray returnArray = new JSONArray();
  355. if (ListUtil.isBlank(idList)) {
  356. return returnArray;
  357. }
  358. Map<String, JSONObject> surgicalSequencing = new TreeMap<>();
  359. for (String s : idList) {
  360. JSONObject jsonObject = emr.getEditorJsonDataByDocumentId(s);
  361. JSONObject surgeryDate = jsonObject.getJSONObject("手术日期");
  362. if (null == surgeryDate) {
  363. continue;
  364. }
  365. String key = DateUtil.formatDatetime(surgeryDate.getDate("value"));
  366. if (StringUtil.isBlank(key)) {
  367. continue;
  368. }
  369. surgicalSequencing.put(key, jsonObject);
  370. }
  371. if (!surgicalSequencing.isEmpty()) {
  372. for (Map.Entry<String, JSONObject> item : surgicalSequencing.entrySet()) {
  373. returnArray.add(item.getValue());
  374. }
  375. }
  376. return returnArray;
  377. }
  378. private JSONArray extractDiagnosis(JSONObject jsonObject) {
  379. JSONObject diagnosis = jsonObject.getJSONObject("入院诊断");
  380. if (diagnosis != null) {
  381. JSONArray diagnosisArr = diagnosis.getJSONArray("value");
  382. if (diagnosisArr != null) {
  383. return diagnosisArr;
  384. }
  385. }
  386. return new JSONArray();
  387. }
  388. public ResultVo<String> getDrgIntelligentGrouping(String patNo, Integer times) {
  389. // 获取诊断
  390. JSONArray diagnosis = getEmrData(patNo, times, EmrType.DIAGNOSIS);
  391. // 获取手术
  392. JSONArray operation = getEmrData(patNo, times, EmrType.OPERATION);
  393. int ledgerSn = publicServer.getLedgerSn(patNo, times);
  394. // 获取患者数据
  395. Map<String, String> mapData = dao.drgPatientInfo(patNo, times, ledgerSn);
  396. if (mapData == null) {
  397. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到患者信息。");
  398. }
  399. mapData.put("visit_id", patNo + "_" + times + "_" + ledgerSn);
  400. mapData.put("bah", patNo + "_" + times);
  401. for (int i = 0; i < diagnosis.size(); i++) {
  402. JSONObject item = diagnosis.getJSONObject(i);
  403. if (i == 0) {
  404. mapData.put("jbdm", item.getString("code"));
  405. mapData.put("zyzd", item.getString("name"));
  406. } else {
  407. mapData.put("jbdm" + i, item.getString("code"));
  408. }
  409. }
  410. int operationIndex = 1;
  411. for (int i = 0; i < operation.size(); i++) {
  412. JSONObject item = operation.getJSONObject(i);
  413. JSONArray list = item.getJSONObject("手术名称").getJSONArray("value");
  414. if (list == null) {
  415. continue;
  416. }
  417. for (int a = 0; a < list.size(); a++) {
  418. JSONObject valueList = list.getJSONObject(a);
  419. mapData.put("ssjczbm" + operationIndex, valueList.getString("code"));
  420. if (operationIndex == 1) {
  421. mapData.put("ssjczmc" + operationIndex, valueList.getString("name"));
  422. }
  423. operationIndex++;
  424. }
  425. }
  426. AuxiliaryFillingOfDiagnosis data = EntityCopy.Copy(mapData, AuxiliaryFillingOfDiagnosis.class);
  427. String url = "http://172.16.32.126:8080/drg_web/localHelp/drg_dagns/list.action";
  428. RestTemplate template = new RestTemplate();
  429. String toJsonStr = JSON.toJSONString(data, SerializerFeature.WriteNullStringAsEmpty);
  430. log.info("DRG数据:{}", toJsonStr);
  431. String res = template.postForObject(url, JSON.parseObject(toJsonStr), String.class);
  432. return ResultVoUtil.success("http://172.16.32.126:8080" + res);
  433. }
  434. /**
  435. * 获取患者最大的住院次数
  436. *
  437. * @param patNo 住院号
  438. * @return 返回最大次数
  439. */
  440. public ResultVo<Integer> getDischargeTimes(String patNo) {
  441. Integer times = dao.getMaxTimes(patNo);
  442. if (times == null) {
  443. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到患者信息。");
  444. }
  445. return ResultVoUtil.success(times);
  446. }
  447. public ResultVo<JSONObject> getExtractDataElement(String patNo, Integer times) {
  448. Map<String, String> map = dao.selectEmrDataElement(patNo, times);
  449. if (map == null) {
  450. return ResultVoUtil.success();
  451. } else {
  452. return ResultVoUtil.success(JSONObject.parseObject(map.get("data_element")));
  453. }
  454. }
  455. public ResultVo<List<CodeName>> getAllWards() {
  456. return ResultVoUtil.success(loginDao.getAllWards());
  457. }
  458. /**
  459. * 获取患者已经被删除的病历
  460. *
  461. * @param patNo 住院号
  462. * @return 返回
  463. */
  464. public ResultVo<List<EmrPatientData>> getDeleteMedicalRecord(String patNo) {
  465. Integer times = dao.getAdmissTimes(patNo);
  466. if (times == null) {
  467. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "出院患者无法恢复病历,如果要恢复请先召回在院。");
  468. }
  469. return ResultVoUtil.success(dao.selectEmrDeteles(patNo, times, TokenUtil.getTokenUserId()));
  470. }
  471. public ResultVo<JSONObject> getInvalidByDocumentId(String id) {
  472. return ResultVoUtil.success(emr.getInvalidByDocumentIdApi(id));
  473. }
  474. /**
  475. * 恢复电子病历
  476. *
  477. * @param documentId 电子病历文档
  478. * @return 返回提示
  479. */
  480. public ResultVo<String> resumeMedicalRecords(String documentId) {
  481. EmrPatientData data = dao.selectemPatientDataOne(documentId);
  482. if (data.getDelFlag() == 0) {
  483. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "病历已经被恢复了请勿重复点击");
  484. }
  485. Integer times = dao.getAdmissTimes(data.getPatNo());
  486. if (times == null) {
  487. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "出院患者无法恢复病历,如果要恢复请先召回在院。");
  488. }
  489. JSONObject param = new JSONObject();
  490. param.put("_id", documentId);
  491. param.put("valid", 1);
  492. JSONObject json = emr.resumeMedicalRecords(documentId, param);
  493. log.info("恢复电子病历:{}", json);
  494. if (json.getInteger("ok") == 1) {
  495. dao.updateDeleteFlag(data.getId());
  496. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "病历恢复成功。");
  497. } else {
  498. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "病历恢复失败,请重试!");
  499. }
  500. }
  501. /**
  502. * 查询出院患者的申请
  503. *
  504. * @param flag 1 只查询自己的 2 查询审核的
  505. * @return 提示
  506. */
  507. public ResultVo<List<DischargedEmrModifyApply>> getDisReqEmr(Integer flag) {
  508. QueryWrapper<?> qw = new QueryWrapper<>();
  509. if (flag == 1) {
  510. qw.eq("req_id", TokenUtil.getTokenUserId());
  511. } else if (flag == 2) {
  512. qw.eq("req_status", 0);
  513. }
  514. qw.ge("edit_date", DateUtil.formatDatetime(new Date(), DateUtil.DATE));
  515. return ResultVoUtil.success(dao.getDisReqEmr(qw));
  516. }
  517. public ResultVo<List<DischargedEmrModifyApply>> downloadTheDischargeEdit(String start, String end) {
  518. log.info("导出出院编辑数据:开始:{},结束:{}", start, end);
  519. return ResultVoUtil.success(dao.selectDisReqEmyDownload(start, end));
  520. }
  521. /**
  522. * 申请出院患者修改
  523. *
  524. * @param param 参数
  525. * @return 提示
  526. */
  527. @Transactional(rollbackFor = Exception.class)
  528. public ResultVo<String> saveDisEmrReq(DischargedEmrModifyApply param) {
  529. param.setReqId(TokenUtil.getTokenUserId());
  530. log.info("申请出院患者修改病历:{}", JSON.toJSONString(param));
  531. int num = dao.deleteRequest(param.getPatNo());
  532. dao.insertApplicationEmrDis(param);
  533. if (num > 0) {
  534. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "修改原申请成功。");
  535. } else {
  536. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "申请成功。");
  537. }
  538. }
  539. /**
  540. * 删除申请
  541. *
  542. * @param patNo 住院号
  543. * @return 提示
  544. */
  545. public ResultVo<String> deleteDisEmrDis(String patNo) {
  546. dao.deleteRequest(patNo);
  547. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "操作成功。");
  548. }
  549. /**
  550. * 通过申请
  551. *
  552. * @param patNo 住院号
  553. * @return 提示
  554. */
  555. public ResultVo<String> adoptEmrDisReq(String patNo) {
  556. int num = dao.adoptEmrDisReq(patNo, null, 1);
  557. if (num > 0) {
  558. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  559. }
  560. return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "操作失败,原申请可能已经被删除了。");
  561. }
  562. /**
  563. * 拒绝出院患者申请
  564. *
  565. * @param patNo 住院号
  566. * @param reviewNotes 审核信息
  567. * @return 提示
  568. */
  569. public ResultVo<String> refuseEmrDisReq(String patNo, String reviewNotes) {
  570. int num = dao.adoptEmrDisReq(patNo, reviewNotes, 2);
  571. if (num > 0) {
  572. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  573. }
  574. return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "操作失败,原申请可能已经被删除了。");
  575. }
  576. /**
  577. * 查询出院患者是否已经通过了编辑
  578. *
  579. * @param patNo 住院号
  580. * @return Boolean
  581. */
  582. public ResultVo<Boolean> isDisReqEdit(String patNo) {
  583. QueryWrapper<?> qw = new QueryWrapper<>();
  584. qw.eq("pat_no", patNo);
  585. qw.eq("req_status", 1);
  586. qw.ge("edit_date", DateUtil.formatDatetime(new Date(), DateUtil.DATE));
  587. List<DischargedEmrModifyApply> list = dao.getDisReqEmr(qw);
  588. if (ListUtil.isBlank(list)) {
  589. return ResultVoUtil.success(false);
  590. } else {
  591. return ResultVoUtil.success(true);
  592. }
  593. }
  594. public ResultVo<Object> getHistory(String documentId) {
  595. return ResultVoUtil.success(emr.getHistory(documentId));
  596. }
  597. public ResultVo<List<MedicalHistoryPrompts>> getEmrTips() {
  598. return ResultVoUtil.success(dao.selectEmrTips(TokenUtil.getTokenUserId()));
  599. }
  600. public ResultVo<String> addEmrTips(MedicalHistoryPrompts param) {
  601. log.info("数据:{}", JSON.toJSONString(param));
  602. UserInfo userInfo = redisLikeService.getUserInfoByToken();
  603. param.setDept(userInfo.getDeptCode());
  604. param.setCreatorId(userInfo.getCode());
  605. dao.insertEmrTips(param);
  606. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  607. }
  608. public ResultVo<String> updateEmrTips(MedicalHistoryPrompts param) {
  609. if (param.getId() == null) {
  610. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "id 为空无法更新。");
  611. }
  612. dao.updateEmrTips(param);
  613. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  614. }
  615. public ResultVo<String> deleteEmrTips(Integer id) {
  616. dao.delEmrTips(id);
  617. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  618. }
  619. /**
  620. * 在病程记录中获取文本数据
  621. *
  622. * @param name 名称
  623. * @return 返回数据
  624. */
  625. public ResultVo<List<MedicalHistoryPrompts>> getEmrTipsData(String name, int typeCode) {
  626. QueryWrapper<?> qw = new QueryWrapper<>();
  627. UserInfo us = redisLikeService.getUserInfoByToken();
  628. if (typeCode == 1) {
  629. qw.eq("creator_id", us.getCode());
  630. } else if (typeCode == 2) {
  631. qw.eq("dept", us.getDeptCode());
  632. }
  633. qw.like("query_key", name)
  634. .eq("type_code", typeCode)
  635. .orderByDesc("count");
  636. return ResultVoUtil.success(dao.getEmrTipsData(qw));
  637. }
  638. public ResultVo<List<ZyPatientInfo>> getListOfDischargedPatients(String patNo) {
  639. List<ZyPatientInfo> list = dao.getListOfDischargedPatients(patNo);
  640. if (ListUtil.isBlank(list)) {
  641. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有" + patNo + "的出院记录。");
  642. }
  643. return ResultVoUtil.success(list);
  644. }
  645. public ResultVo<JSONObject> getYzTemperature(String patNo, Integer times) {
  646. JSONObject js = new JSONObject();
  647. List<YzTemperature> list = dao.getYzTemperature(patNo, times);
  648. if (ListUtil.isBlank(list)) {
  649. return ResultVoUtil.success(js);
  650. }
  651. Map<Date, YzTemperature> map = new LinkedHashMap<>(list.size());
  652. for (YzTemperature item : list) {
  653. Date key = item.getTempDate();
  654. if (map.containsKey(key)) {
  655. YzTemperature yz = map.get(key);
  656. yz.setOtherInfo(nullToEmpty(yz.getOtherInfo()) + nullToEmpty(item.getOtherInfo()));
  657. } else {
  658. map.put(key, item);
  659. }
  660. }
  661. js.put("list", list);
  662. js.put("map", map);
  663. return ResultVoUtil.success(js);
  664. }
  665. public ResultVo<List<YshYjReq>> getExamine(String patNo, Integer times) {
  666. return ResultVoUtil.success(dao.getExamine(patNo, times));
  667. }
  668. private String nullToEmpty(String str) {
  669. if (StringUtil.isBlank(str)) {
  670. return "";
  671. }
  672. return str;
  673. }
  674. public ResultVo<ZyZkList> whetherItExistsInTheDepartment(String patNo, Integer times) {
  675. ZyZkList zk = dao.getMaxTransferDateByPatNo(patNo, times);
  676. if (zk != null) {
  677. zk.setTimeout(DateUtil.moreThanHours(zk.getOpTime(), 48));
  678. }
  679. return ResultVoUtil.success(zk);
  680. }
  681. public ResultVo<String> emrSocketUnlock(String sid) {
  682. List<Session> list = WebSocketServer.getEmrMap(sid);
  683. list.removeIf(item -> {
  684. if (EmrWebSocketServer.sessionParseUserCode(item).equals(TokenUtil.getTokenUserId())) {
  685. WebSocketServer.emrSendMessage(item, "{\"closeSoctek\":\"true\"}");
  686. return true;
  687. }
  688. return false;
  689. });
  690. return ResultVoUtil.success();
  691. }
  692. }