EmrDataMaintenanceServer.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package thyyxxk.webserver.service.dictionary;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.stereotype.Service;
  7. import thyyxxk.webserver.config.exception.ExceptionEnum;
  8. import thyyxxk.webserver.dao.his.dictionary.EmrDataMaintenanceDao;
  9. import thyyxxk.webserver.entity.ResultVo;
  10. import thyyxxk.webserver.entity.zhuyuanyisheng.EmrDataMaintenance;
  11. import thyyxxk.webserver.utils.*;
  12. import java.util.*;
  13. /**
  14. * @author 肖蟾
  15. */
  16. @Service
  17. @Slf4j
  18. public class EmrDataMaintenanceServer {
  19. private final EmrDataMaintenanceDao dao;
  20. public EmrDataMaintenanceServer(EmrDataMaintenanceDao dao) {
  21. this.dao = dao;
  22. }
  23. private static String keyParameterSubstitution(String sqlSentence, String patNo, Integer times) {
  24. sqlSentence = sqlSentence.replaceAll("\\$\\{PAT_NO}", " '" + patNo + "' ");
  25. sqlSentence = sqlSentence.replaceAll("\\$\\{TIMES}", " " + times + " ");
  26. return sqlSentence;
  27. }
  28. private static void createObject(Map<String, List<Map<String, Object>>> groupMap,
  29. String key, Object value,
  30. Map<String, Object> extraMap) {
  31. String[] keys = key.split("_");
  32. List<Map<String, Object>> groupChild = new ArrayList<>();
  33. Map<String, Object> childMap = new HashMap<>();
  34. if (keys.length == 4) {
  35. extraMap.put(StringUtil.humpToUnderline(keys[3]), value);
  36. }
  37. if (groupMap.containsKey(keys[1])) {
  38. childMap.putAll(groupMap.get(keys[1]).get(0));
  39. childMap.put(keys[2], value);
  40. groupChild.add(childMap);
  41. groupMap.replace(keys[1], groupChild);
  42. } else {
  43. childMap.put(keys[2], value);
  44. groupChild.add(childMap);
  45. groupMap.put(keys[1], groupChild);
  46. }
  47. }
  48. public ResultVo<Page<EmrDataMaintenance>> getElectronicMedicalRecordDataElementSql(int currentPage, int pageSize, long total) {
  49. Page<EmrDataMaintenance> page = new Page<>(currentPage, pageSize, total == 0);
  50. dao.getElectronicMedicalRecordDataElementSql(page);
  51. return ResultVoUtil.success(page);
  52. }
  53. public ResultVo<JSONObject> testSql(EmrDataMaintenance emr) {
  54. String sql = keyParameterSubstitution(emr.getSqlSentence(), emr.getPatNo(), emr.getTimes());
  55. log.info("sql:{}", sql);
  56. if (emr.getDataType().equals(0)) {
  57. Map<String, Object> sqlData = dao.testSql(sql);
  58. if (null == sqlData || sqlData.isEmpty()) {
  59. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "sql没问题,但是没得数据。");
  60. }
  61. // 永远添加 住院号和住院次数
  62. sqlData.put("inpatient_no", emr.getPatNo());
  63. sqlData.put("admiss_times", emr.getTimes());
  64. Map<String, List<Map<String, Object>>> groupingMap = new HashMap<>();
  65. Map<String, Object> extraMap = new HashMap<>();
  66. List<String> groupKeys = new ArrayList<>();
  67. for (Map.Entry<String, Object> dbitem : sqlData.entrySet()) {
  68. Object value = dbitem.getValue();
  69. String key = dbitem.getKey();
  70. if (value != null) {
  71. if (value instanceof String) {
  72. sqlData.replace(key, ((String) value).trim());
  73. if (key.startsWith("gy_")) {
  74. groupKeys.add(key);
  75. createObject(groupingMap, key, ((String) value).trim(), extraMap);
  76. }
  77. }
  78. }
  79. }
  80. sqlData.putAll(groupingMap);
  81. sqlData.putAll(extraMap);
  82. for (String item : groupKeys) {
  83. sqlData.remove(item);
  84. }
  85. if (StringUtil.notBlank(emr.getGroupName())) {
  86. String map = String.format("{\"%s\" : %s}", emr.getGroupName(), JSON.toJSONStringWithDateFormat(sqlData, DateUtil.DEFAULT_PATTERN));
  87. return ResultVoUtil.success(JSON.parseObject(map));
  88. }
  89. return ResultVoUtil.success(JSON.parseObject(JSON.toJSONStringWithDateFormat(sqlData, DateUtil.DEFAULT_PATTERN)));
  90. } else {
  91. List<Map<String, Object>> listMap = dao.testSqlList(sql);
  92. if (ListUtil.isBlank(listMap)) {
  93. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "sql没问题,但是没得数据。");
  94. }
  95. Map<String, List<Map<String, Object>>> formatData = new HashMap<>(1);
  96. formatData.put(emr.getGroupName(), listMap);
  97. return ResultVoUtil.success(JSON.parseObject(JSON.toJSONStringWithDateFormat(formatData, DateUtil.DEFAULT_PATTERN)));
  98. }
  99. }
  100. public ResultVo<String> addData(EmrDataMaintenance emr) {
  101. String userCode = TokenUtil.getTokenUserId();
  102. dao.addData(emr.getName(), emr.getSqlSentence(), userCode, emr.getDataType());
  103. log.info("新增电子病历维护 ==> 操作人:{},数据:{}", userCode, JSON.toJSONString(emr));
  104. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  105. }
  106. public ResultVo<String> deleteById(Integer id) {
  107. dao.deleteById(id, TokenUtil.getTokenUserId());
  108. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  109. }
  110. public ResultVo<String> updateData(EmrDataMaintenance emr) {
  111. if (emr == null || emr.getId() == null) {
  112. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到id");
  113. }
  114. dao.updateData(emr, TokenUtil.getTokenUserId());
  115. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
  116. }
  117. public ResultVo<JSONObject> getEmrInpatientData(String patNo, Integer times) throws Exception {
  118. List<EmrDataMaintenance> emrData = dao.getEmrInpatientData();
  119. JSONObject obj = new JSONObject();
  120. for (EmrDataMaintenance item : emrData) {
  121. item.setPatNo(patNo);
  122. item.setTimes(times);
  123. ResultVo<JSONObject> resultVo = testSql(item);
  124. if (resultVo.getCode() == ExceptionEnum.SUCCESS.getCode()) {
  125. if (null != resultVo.getData()) {
  126. obj = EntityCopy.jsonMerge(resultVo.getData(), obj);
  127. }
  128. }
  129. }
  130. return ResultVoUtil.success(obj);
  131. }
  132. }