123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- package thyyxxk.webserver.service.zhuyuanyisheng;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import com.baomidou.dynamic.datasource.annotation.DS;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.web.client.RestTemplate;
- import thyyxxk.webserver.config.exception.ExceptionEnum;
- import thyyxxk.webserver.constants.EmrType;
- import thyyxxk.webserver.dao.his.LoginDao;
- import thyyxxk.webserver.dao.his.zhuyuanyisheng.EmrPatientDao;
- import thyyxxk.webserver.entity.ResultVo;
- import thyyxxk.webserver.entity.dictionary.CodeName;
- import thyyxxk.webserver.entity.drg.AuxiliaryFillingOfDiagnosis;
- import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrDataElement;
- import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrDataExtract;
- import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrPatientData;
- import thyyxxk.webserver.service.PublicServer;
- import thyyxxk.webserver.service.externalhttp.emr.EmrEditor;
- import thyyxxk.webserver.utils.*;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- @Slf4j
- @DS("his")
- public class EmrServer {
- private final EmrPatientDao dao;
- private final EmrEditor emr;
- private final PublicServer publicServer;
- private final LoginDao loginDao;
- public EmrServer(EmrPatientDao dao, EmrEditor emr, PublicServer publicServer, LoginDao loginDao) {
- this.dao = dao;
- this.emr = emr;
- this.publicServer = publicServer;
- this.loginDao = loginDao;
- }
- /**
- * 获取这个患者这个住院次数的全部电子病历
- *
- * @param patNo 住院号
- * @param times 住院次数
- * @return 数据
- */
- public ResultVo<List<EmrPatientData>> getPatientDataTree(String patNo, Integer times) {
- List<EmrPatientData> data = new ArrayList<>();
- Map<String, List<EmrPatientData>> map = dao.getPatientData(patNo, times).stream().collect(Collectors.groupingBy(EmrPatientData::getEmrCategoryCode));
- for (Map.Entry<String, List<EmrPatientData>> key : map.entrySet()) {
- if (key.getValue().size() == 1) {
- data.add(key.getValue().get(0));
- } else {
- EmrPatientData item = new EmrPatientData();
- List<EmrPatientData> children = new ArrayList<>();
- for (int i = 0, len = key.getValue().size(); i < len; i++) {
- EmrPatientData childNode = key.getValue().get(i);
- children.add(childNode);
- }
- item.setName(key.getValue().get(0).getEmrName());
- item.setChildren(new ArrayList<>()).getChildren().addAll(children);
- data.add(item);
- }
- }
- return ResultVoUtil.success(data);
- }
- public ResultVo<List<JSONObject>> getEmrTree() {
- JSONArray data = emr.getEmrTree();
- List<JSONObject> tree = new ArrayList<>();
- if (data.isEmpty()) {
- return ResultVoUtil.success(tree);
- }
- emrArrToTree(data, tree);
- return ResultVoUtil.success(tree);
- }
- private static void emrArrToTree(JSONArray data, List<JSONObject> tree) {
- Map<String, JSONObject> map = new HashMap<>(data.size());
- for (int i = 0; i < data.size(); i++) {
- JSONObject item = data.getJSONObject(i);
- if (item.get("parent") == null) {
- tree.add(item);
- }
- map.put(item.getString("_id"), item);
- }
- for (int i = 0; i < data.size(); i++) {
- JSONObject item = data.getJSONObject(i);
- if (map.containsKey(item.getString("parent"))) {
- JSONObject parentList = map.get(item.getString("parent"));
- if (parentList.get("children") == null) {
- parentList.put("children", new JSONArray());
- }
- parentList.getJSONArray("children").add(item);
- }
- }
- }
- public ResultVo<List<JSONObject>> getSnippetTree() {
- JSONArray data = emr.getSnippetTree();
- List<JSONObject> tree = new ArrayList<>();
- emrArrToTree(data, tree);
- return ResultVoUtil.success(tree);
- }
- /**
- * 提取患者数据元
- *
- * @param param 结构化文档
- */
- public Map<String, Object> extractDataElement(EmrPatientData param) {
- EmrDataExtract emrDataExtract = dao.extractDataSource(param.getEmrCategoryCode());
- if (emrDataExtract == null) {
- return new HashMap<>();
- }
- // 获取 需要提取的数据元
- List<String> strings = JSON.parseArray(emrDataExtract.getDataExtract(), String.class);
- // 获取前端传入的数据元
- JSONObject elementData = param.getEmrDataElement();
- // 提取到的数据
- Map<String, Object> extractedData = new HashMap<>(strings.size());
- for (String el : strings) {
- if (elementData.containsKey(el)) {
- JSONObject item = elementData.getJSONObject(el);
- Object value = item.get("value");
- if (value != null) {
- extractedData.put(el, value);
- }
- }
- }
- EmrDataElement emrDataElement = dao.obtainPatientSOriginalData(param.getPatNo(), param.getTimes());
- if (emrDataElement == null) {
- dao.insertDataSource(param.getPatNo(), param.getTimes(), JSON.toJSONString(extractedData));
- } else {
- JSONObject jsonObject = JSONObject.parseObject(emrDataElement.getDataElement());
- log.info("患者原来的数据:{}", jsonObject);
- jsonObject.putAll(extractedData);
- dao.updatePatientDataSource(param.getPatNo(), param.getTimes(), JSON.toJSONString(jsonObject));
- }
- log.info("提取到的数据:{}", JSON.toJSONString(extractedData));
- return extractedData;
- }
- /**
- * 保存患者电子病历信息
- *
- * @param param 参数
- * @return 提示
- */
- public ResultVo<Map<String, Object>> insertEmrData(EmrPatientData param) {
- param.setCreateId(TokenUtil.getTokenUserId());
- boolean isUpdated = dao.whetherThereIsAMedicalRecord(param.getPatNo(), param.getTimes(), param.getEmrDocumentId()).equals(1);
- Map<String, Object> extractedData = extractDataElement(param);
- if (isUpdated) {
- dao.updateCreatedTemplate(param);
- } else {
- dao.emrInsertForTheFirstTime(param);
- }
- return ResultVoUtil.success(extractedData);
- }
- /**
- * 根据患者的电子病历 id 来删除
- * 这里用的是逻辑删除
- *
- * @param documentId 电子病历id
- * @return 删除电子病历的同时需要删除, 提取到的数据源.
- */
- public ResultVo<List<String>> deletePatientEmrByDocumentId(String documentId) {
- EmrPatientData patientData = dao.getCategoryCodeByDocumentId(documentId);
- EmrDataExtract emrDataExtract = dao.extractDataSource(patientData.getEmrCategoryCode());
- List<String> strings = new ArrayList<>();
- if (emrDataExtract != null) {
- // 获取 需要提取的数据元
- strings = JSON.parseArray(emrDataExtract.getDataExtract(), String.class);
- log.info("需要提取的数据:{}", strings);
- // 获取到已经提取的数据
- EmrDataElement emrDataElement = dao.obtainPatientSOriginalData(patientData.getPatNo(), patientData.getTimes());
- // 转 json
- JSONObject jsonObject = JSONObject.parseObject(emrDataElement.getDataElement());
- for (String string : strings) {
- // 删除提取到的数据
- jsonObject.remove(string);
- }
- // 更新患者数据元
- dao.updatePatientDataSource(patientData.getPatNo(), patientData.getTimes(), JSON.toJSONString(jsonObject));
- }
- // 删除病历
- dao.deletePatientEmrByDocumentId(documentId, TokenUtil.getTokenUserId());
- return ResultVoUtil.success(strings);
- }
- /**
- * 查询患者是否有指定的病历了
- *
- * @param param 住院号,住院次数,病历编码
- * @return 返回 Boolean
- */
- public ResultVo<Boolean> queryWhetherThePatientHasASpecifiedMedicalRecord(EmrPatientData param) {
- return ResultVoUtil.success(dao.queryWhetherThePatientHasASpecifiedMedicalRecord(param.getPatNo(), param.getTimes(), param.getEmrCategoryCode()) == 1);
- }
- /**
- * 医生诊断热缩
- *
- * @param userCode 医生编码
- * @param code 诊断 code
- * @param tableName 表名
- */
- public void hotSearchSorting(String userCode, String code, String tableName) {
- boolean insert = dao.queryWhetherThereIsHotSearch(userCode, code, tableName) == 0;
- if (insert) {
- dao.hotSearchSorting(userCode, code, tableName);
- } else {
- dao.updateHotSearch(userCode, code, tableName);
- }
- log.info("医生编码:{},搜索关键字:{},表名:{}", userCode, code, tableName);
- }
- /**
- * 根据文档 id 提交病历
- *
- * @param documentId 文档id
- * @return 返回数据
- */
- public ResultVo<String> submitMedicalRecord(String documentId) {
- String userCode = TokenUtil.getTokenUserId();
- String createdId = dao.getDocumentIdCreatedBy(documentId);
- log.info("提交病历==》 操作人:{},病历id:{},创建人:{}", userCode, documentId, createdId);
- if (userCode.equals(createdId)) {
- dao.updateSubmissionFlag(documentId);
- return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "提交成功");
- }
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "创建人不一致无法修改。");
- }
- /**
- * 获取电子病历数据
- *
- * @param panNo 住院号
- * @param times 住院次数
- * @param etType 类型
- * @return 返回数据
- */
- public JSONArray getEmrData(String panNo, Integer times, EmrType etType) {
- // 查询这个病历的 唯一 id
- List<String> idList = dao.getDocumentIdByPatietn(panNo, times, etType.getName());
- JSONArray returnArray = new JSONArray();
- if (ListUtil.isBlank(idList)) {
- return returnArray;
- }
- Map<String, JSONObject> surgicalSequencing = new TreeMap<>();
- for (String s : idList) {
- JSONObject jsonObject = emr.getEditorJsonDataByDocumentId(s);
- if (etType.getCode() == EmrType.DIAGNOSIS.getCode()) {
- returnArray.addAll(extractDiagnosis(jsonObject));
- } else if (etType.getCode() == EmrType.OPERATION.getCode()) {
- String key = DateUtil.formatDatetime(jsonObject.getJSONObject("手术日期").getDate("value"));
- surgicalSequencing.put(key, jsonObject);
- }
- }
- if (!surgicalSequencing.isEmpty()) {
- for (Map.Entry<String, JSONObject> item : surgicalSequencing.entrySet()) {
- returnArray.add(item.getValue());
- }
- }
- return returnArray;
- }
- private JSONArray extractDiagnosis(JSONObject jsonObject) {
- JSONObject diagnosis = jsonObject.getJSONObject("入院诊断");
- if (diagnosis != null) {
- JSONArray diagnosisArr = diagnosis.getJSONArray("value");
- if (diagnosisArr != null) {
- return diagnosisArr;
- }
- }
- return new JSONArray();
- }
- public ResultVo<String> getDrgIntelligentGrouping(String patNo, Integer times) {
- // 获取诊断
- JSONArray diagnosis = getEmrData(patNo, times, EmrType.DIAGNOSIS);
- // 获取手术
- JSONArray operation = getEmrData(patNo, times, EmrType.OPERATION);
- int ledgerSn = publicServer.getLedgerSn(patNo, times);
- // 获取患者数据
- Map<String, String> mapData = dao.drgPatientInfo(patNo, times, ledgerSn);
- if (mapData == null) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到患者信息。");
- }
- mapData.put("visit_id", patNo + "_" + times + "_" + ledgerSn);
- mapData.put("bah", patNo + "_" + times);
- for (int i = 0; i < diagnosis.size(); i++) {
- JSONObject item = diagnosis.getJSONObject(i);
- if (i == 0) {
- mapData.put("jbdm", item.getString("code"));
- mapData.put("zyzd", item.getString("name"));
- } else {
- mapData.put("jbdm" + i, item.getString("code"));
- }
- }
- int operationIndex = 1;
- for (int i = 0; i < operation.size(); i++) {
- JSONObject item = operation.getJSONObject(i);
- JSONArray list = item.getJSONObject("手术名称").getJSONArray("value");
- for (int a = 0; a < list.size(); a++) {
- JSONObject valueList = list.getJSONObject(a);
- mapData.put("ssjczbm" + operationIndex, valueList.getString("code"));
- if (operationIndex == 1) {
- mapData.put("ssjczmc" + operationIndex, valueList.getString("name"));
- }
- operationIndex++;
- }
- }
- AuxiliaryFillingOfDiagnosis data = EntityCopy.Copy(mapData, AuxiliaryFillingOfDiagnosis.class);
- String url = "http://172.16.32.126:8080/drg_web/localHelp/drg_dagns/list.action";
- RestTemplate template = new RestTemplate();
- String toJsonStr = JSON.toJSONString(data, SerializerFeature.WriteNullStringAsEmpty);
- log.info("DRG数据:{}", toJsonStr);
- String res = template.postForObject(url, JSON.parseObject(toJsonStr), String.class);
- return ResultVoUtil.success("http://172.16.32.126:8080" + res);
- }
- /**
- * 获取患者最大的住院次数
- *
- * @param patNo 住院号
- * @return 返回最大次数
- */
- public ResultVo<Integer> getDischargeTimes(String patNo) {
- Integer times = dao.getMaxTimes(patNo);
- if (times == null) {
- return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到患者信息。");
- }
- return ResultVoUtil.success(times);
- }
- public ResultVo<JSONObject> getExtractDataElement(String patNo, Integer times) {
- Map<String, String> map = dao.selectEmrDataElement(patNo, times);
- if (map == null) {
- return ResultVoUtil.success();
- } else {
- return ResultVoUtil.success(JSONObject.parseObject(map.get("data_element")));
- }
- }
- public ResultVo<List<CodeName>> getAllWards() {
- return ResultVoUtil.success(loginDao.getAllWards());
- }
- }
|