diff --git a/healthlink-his-server/healthlink-his-application/src/main/java/com/healthlink/his/web/emr/controller/EmrSyncController.java b/healthlink-his-server/healthlink-his-application/src/main/java/com/healthlink/his/web/emr/controller/EmrSyncController.java new file mode 100644 index 000000000..0646a5156 --- /dev/null +++ b/healthlink-his-server/healthlink-his-application/src/main/java/com/healthlink/his/web/emr/controller/EmrSyncController.java @@ -0,0 +1,146 @@ +package com.healthlink.his.web.emr.controller; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.core.common.core.domain.R; +import com.healthlink.his.document.domain.Emr; +import com.healthlink.his.document.service.IEmrService; +import com.healthlink.his.emr.domain.EmrRevision; +import com.healthlink.his.emr.domain.EmrSearchIndex; +import com.healthlink.his.emr.service.IEmrRevisionService; +import com.healthlink.his.emr.service.IEmrSearchIndexService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import java.util.*; + +/** + * EMR数据同步Controller + */ +@RestController +@RequestMapping("/emr-sync") +@Slf4j +@AllArgsConstructor +@Tag(name = "EMR数据同步") +public class EmrSyncController { + + private final IEmrService emrService; + private final IEmrRevisionService emrRevisionService; + private final IEmrSearchIndexService emrSearchIndexService; + + /** + * 同步EMR数据 + * 清空假数据,从doc_emr生成真实数据 + */ + @PostMapping("/sync") + @Operation(summary = "同步EMR修订历史和搜索索引") + public R syncEmrData() { + log.info("开始同步EMR数据..."); + + // 1. 清空假数据 + emrRevisionService.remove(new LambdaQueryWrapper<>()); + emrSearchIndexService.remove(new LambdaQueryWrapper<>()); + log.info("已清空emr_revision和emr_search_index表"); + + // 2. 从doc_emr获取所有病历 + List emrList = emrService.list(new LambdaQueryWrapper() + .orderByAsc(Emr::getCreateTime)); + + if (emrList.isEmpty()) { + return R.ok("没有病历数据需要同步"); + } + + int revisionCount = 0; + int searchIndexCount = 0; + + for (Emr emr : emrList) { + // 3. 创建修订历史 + try { + EmrRevision revision = new EmrRevision(); + revision.setEmrId(emr.getId()); + revision.setEncounterId(emr.getEncounterId()); + revision.setRevisionNumber(1); + revision.setOperatorId(emr.getRecordId()); + revision.setOperatorName("系统同步"); + revision.setOperationType("CREATE"); + revision.setDiffContent("初始创建"); + revision.setSnapshotContent(emr.getContextJson()); + revision.setCreateTime(emr.getCreateTime()); + emrRevisionService.save(revision); + revisionCount++; + } catch (Exception e) { + log.warn("创建修订历史失败: emrId={}, error={}", emr.getId(), e.getMessage()); + } + + // 4. 创建搜索索引 + try { + Map contentMap = parseContextJson(emr.getContextJson()); + String chiefComplaint = contentMap.getOrDefault("chiefComplaint", ""); + String diagnosis = contentMap.getOrDefault("diagnosis", ""); + + EmrSearchIndex index = new EmrSearchIndex(); + index.setEmrId(emr.getId()); + index.setEncounterId(emr.getEncounterId()); + index.setPatientId(emr.getPatientId()); + index.setPatientName("患者" + emr.getPatientId()); + index.setEmrType(emr.getClassEnum() == 1 ? "OUTPATIENT" : "INPATIENT"); + index.setEmrTitle(chiefComplaint.isEmpty() ? "未命名病历" : chiefComplaint); + index.setDiagnosisText(diagnosis); + index.setDoctorName("医生" + emr.getRecordId()); + index.setCreateTime(emr.getCreateTime()); + emrSearchIndexService.save(index); + searchIndexCount++; + } catch (Exception e) { + log.warn("创建搜索索引失败: emrId={}, error={}", emr.getId(), e.getMessage()); + } + } + + String result = String.format("同步完成: 修订历史%d条, 搜索索引%d条", revisionCount, searchIndexCount); + log.info(result); + return R.ok(result); + } + + /** + * 获取同步统计 + */ + @GetMapping("/stats") + @Operation(summary = "获取EMR同步统计") + public R getSyncStats() { + Map stats = new HashMap<>(); + stats.put("emrCount", emrService.count()); + stats.put("revisionCount", emrRevisionService.count()); + stats.put("searchIndexCount", emrSearchIndexService.count()); + return R.ok(stats); + } + + /** + * 解析contextJson字符串 + */ + private Map parseContextJson(String contextJson) { + Map map = new HashMap<>(); + if (contextJson == null || contextJson.isEmpty()) { + return map; + } + try { + // 简单解析JSON字符串 + String json = contextJson.trim(); + if (json.startsWith("{") && json.endsWith("}")) { + json = json.substring(1, json.length() - 1); + String[] pairs = json.split(","); + for (String pair : pairs) { + String[] kv = pair.split(":"); + if (kv.length == 2) { + String key = kv[0].trim().replace("\"", ""); + String value = kv[1].trim().replace("\"", ""); + map.put(key, value); + } + } + } + } catch (Exception e) { + log.warn("解析contextJson失败: {}", e.getMessage()); + } + return map; + } +}