feat(emr): 添加EMR数据同步接口
- 新增 /emr-sync/sync 接口:清空假数据并从doc_emr同步真实数据 - 新增 /emr-sync/stats 接口:获取同步统计信息 - 支持门诊和住院病历数据同步到修订历史和搜索索引
This commit is contained in:
@@ -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<Emr> emrList = emrService.list(new LambdaQueryWrapper<Emr>()
|
||||
.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<String, String> 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<String, Object> 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<String, String> parseContextJson(String contextJson) {
|
||||
Map<String, String> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user