|
@@ -0,0 +1,127 @@
|
|
|
+package thyyxxk.webserver.service.dictionary;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import thyyxxk.webserver.config.exception.ExceptionEnum;
|
|
|
+import thyyxxk.webserver.dao.his.dictionary.EmrDataMaintenanceDao;
|
|
|
+import thyyxxk.webserver.entity.ResultVo;
|
|
|
+import thyyxxk.webserver.entity.zhuyuanyisheng.EmrDataMaintenance;
|
|
|
+import thyyxxk.webserver.utils.*;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class EmrDataMaintenanceServer {
|
|
|
+
|
|
|
+ private final EmrDataMaintenanceDao dao;
|
|
|
+
|
|
|
+ public EmrDataMaintenanceServer(EmrDataMaintenanceDao dao) {
|
|
|
+ this.dao = dao;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String keyParameterSubstitution(String sqlSentence, String patNo, Integer times) {
|
|
|
+ sqlSentence = sqlSentence.replaceAll("\\$\\{PAT_NO}", " '" + patNo + "' ");
|
|
|
+ sqlSentence = sqlSentence.replaceAll("\\$\\{TIMES}", " " + times + " ");
|
|
|
+ return sqlSentence;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void createObject(Map<String, Map<String, Object>> groupMap, String key, Object value, Map<String, Object> extraMap) {
|
|
|
+ String[] keys = key.split("_");
|
|
|
+ Map<String, Object> groupChild = new HashMap<>();
|
|
|
+ if (keys.length == 4) {
|
|
|
+ extraMap.put(StringUtil.humpToUnderline(keys[3]), value);
|
|
|
+ }
|
|
|
+ if (groupMap.containsKey(keys[1])) {
|
|
|
+ groupChild.putAll(groupMap.get(keys[1]));
|
|
|
+ groupChild.put(keys[2], value);
|
|
|
+ groupMap.put(keys[1], groupChild);
|
|
|
+ } else {
|
|
|
+ groupChild.put(keys[2], value);
|
|
|
+ groupMap.put(keys[1], groupChild);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public ResultVo<Page<EmrDataMaintenance>> getElectronicMedicalRecordDataElementSql(int currentPage, int pageSize, long total) {
|
|
|
+ Page<EmrDataMaintenance> page = new Page<>(currentPage, pageSize, total == 0);
|
|
|
+ dao.getElectronicMedicalRecordDataElementSql(page);
|
|
|
+ return ResultVoUtil.success(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResultVo<JSONObject> testSql(EmrDataMaintenance emr) {
|
|
|
+ try {
|
|
|
+ String sql = keyParameterSubstitution(emr.getSqlSentence(), emr.getPatNo(), emr.getTimes());
|
|
|
+
|
|
|
+ Map<String, Object> sqlData = dao.testSql(sql);
|
|
|
+ if (null == sqlData || sqlData.isEmpty()) {
|
|
|
+ return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "sql没问题,但是没得数据。");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Map<String, Object>> groupingMap = new HashMap<>();
|
|
|
+ Map<String, Object> extraMap = new HashMap<>();
|
|
|
+ List<String> groupKeys = new ArrayList<>();
|
|
|
+
|
|
|
+ for (Map.Entry<String, Object> dbitem : sqlData.entrySet()) {
|
|
|
+ Object value = dbitem.getValue();
|
|
|
+ String key = dbitem.getKey();
|
|
|
+ if (value != null) {
|
|
|
+ if (value instanceof String) {
|
|
|
+ sqlData.replace(key, ((String) value).trim());
|
|
|
+ if (key.startsWith("gy_")) {
|
|
|
+ groupKeys.add(key);
|
|
|
+ createObject(groupingMap, key, ((String) value).trim(), extraMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlData.putAll(groupingMap);
|
|
|
+ sqlData.putAll(extraMap);
|
|
|
+ for (String item : groupKeys) {
|
|
|
+ sqlData.remove(item);
|
|
|
+ }
|
|
|
+ return ResultVoUtil.success(JSON.parseObject(JSON.toJSONStringWithDateFormat(sqlData, DateUtil.DEFAULT_PATTERN)));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public ResultVo<String> addData(EmrDataMaintenance emr) {
|
|
|
+ String userCode = TokenUtil.getTokenUserId();
|
|
|
+ dao.addData(emr.getName(), emr.getSqlSentence(), userCode);
|
|
|
+ log.info("新增电子病历维护 ==> 操作人:{},数据:{}", userCode, JSON.toJSONString(emr));
|
|
|
+ return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResultVo<String> updateData(EmrDataMaintenance emr) {
|
|
|
+ if (emr == null || emr.getId() == null) {
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到id");
|
|
|
+ }
|
|
|
+ dao.updateData(emr.getName(), emr.getSqlSentence(), TokenUtil.getTokenUserId(), emr.getId());
|
|
|
+ return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public ResultVo<JSONObject> getEmrInpatientData(String patNo, Integer times) throws Exception {
|
|
|
+ List<EmrDataMaintenance> emrData = dao.getEmrInpatientData();
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ for (EmrDataMaintenance item : emrData) {
|
|
|
+ item.setPatNo(patNo);
|
|
|
+ item.setTimes(times);
|
|
|
+ ResultVo<JSONObject> resultVo = testSql(item);
|
|
|
+ if (resultVo.getCode() == ExceptionEnum.SUCCESS.getCode()) {
|
|
|
+ if (null != resultVo.getData()) {
|
|
|
+ obj = EntityCopy.jsonMerge(resultVo.getData(), obj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ResultVoUtil.success(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|