feat(emr): 保存病历时自动触发修订记录和搜索索引

- 保存门诊病历时自动创建修订历史记录
- 保存门诊病历时自动更新搜索索引
- 修订记录包含版本号、操作人、操作类型、内容快照
- 搜索索引包含患者姓名、诊断、医生等信息
This commit is contained in:
2026-06-21 07:21:40 +08:00
parent f4493cf74b
commit f7b99f8d9e

View File

@@ -28,6 +28,10 @@ import com.healthlink.his.document.service.IEmrDetailService;
import com.healthlink.his.document.service.IEmrDictService;
import com.healthlink.his.document.service.IEmrService;
import com.healthlink.his.document.service.IEmrTemplateService;
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 com.healthlink.his.web.doctorstation.appservice.IDoctorStationEmrAppService;
import com.healthlink.his.web.doctorstation.dto.EmrTemplateDto;
import com.healthlink.his.web.doctorstation.dto.PatientEmrDto;
@@ -63,6 +67,18 @@ public class DoctorStationEmrAppServiceImpl implements IDoctorStationEmrAppServi
@Resource
IDocRecordService docRecordService;
@Resource
IEmrRevisionService emrRevisionService;
@Resource
IEmrSearchIndexService emrSearchIndexService;
@Resource
PatientMapper patientMapper;
@Resource
EncounterMapper encounterMapper;
@Resource
private com.healthlink.his.web.doctorstation.mapper.DoctorStationEmrAppMapper doctorStationEmrAppMapper;
@@ -79,10 +95,12 @@ public class DoctorStationEmrAppServiceImpl implements IDoctorStationEmrAppServi
String contextStr = patientEmrDto.getContextJson().toString();
Emr patientEmr = emrService.getOne(new LambdaQueryWrapper<Emr>().eq(Emr::getEncounterId, emr.getEncounterId()).orderByDesc(Emr::getCreateTime).last("LIMIT 1"), false);
boolean saveSuccess;
boolean isUpdate = patientEmr != null;
// 如果已经保存病历,再次保存走更新
if (patientEmr != null) {
if (isUpdate) {
saveSuccess = emrService.update(new LambdaUpdateWrapper<Emr>().eq(Emr::getEncounterId, emr.getEncounterId())
.set(Emr::getContextJson, contextStr));
emr = patientEmr;
} else {
saveSuccess =
emrService.save(emr.setContextJson(contextStr).setRecordId(SecurityUtils.getLoginUser().getUserId()));
@@ -90,6 +108,21 @@ public class DoctorStationEmrAppServiceImpl implements IDoctorStationEmrAppServi
if (!saveSuccess) {
return R.fail();
}
// 自动触发:记录修订历史
try {
recordRevisionAutomatically(emr, contextStr, isUpdate);
} catch (Exception e) {
log.warn("自动记录修订历史失败: {}", e.getMessage());
}
// 自动触发:更新搜索索引
try {
updateSearchIndexAutomatically(emr, contextStr);
} catch (Exception e) {
log.warn("自动更新搜索索引失败: {}", e.getMessage());
}
// 获取电子病历字典表中全部key用来判断病历JSON串中是否有需要加入到病历详情表的字段
List<String> emrDictList = emrDictService.list(new LambdaQueryWrapper<EmrDict>().select(EmrDict::getEmrKey))
.stream().map(EmrDict::getEmrKey).collect(Collectors.toList());
@@ -114,6 +147,73 @@ public class DoctorStationEmrAppServiceImpl implements IDoctorStationEmrAppServi
return save ? R.ok() : R.fail();
}
/**
* 自动记录修订历史
*/
private void recordRevisionAutomatically(Emr emr, String contextStr, boolean isUpdate) {
EmrRevision latest = emrRevisionService.selectLatest(emr.getId());
int nextNumber = (latest != null) ? latest.getRevisionNumber() + 1 : 1;
EmrRevision revision = new EmrRevision();
revision.setEmrId(emr.getId());
revision.setEncounterId(emr.getEncounterId());
revision.setRevisionNumber(nextNumber);
revision.setOperatorId(SecurityUtils.getLoginUser().getUserId());
revision.setOperatorName(SecurityUtils.getUsername());
revision.setOperationType(isUpdate ? "UPDATE" : "CREATE");
revision.setSnapshotContent(contextStr);
if (isUpdate && latest != null) {
revision.setDiffContent("内容已更新");
}
revision.setCreateTime(new Date());
emrRevisionService.save(revision);
log.info("自动记录修订历史: emrId={}, revision={}", emr.getId(), nextNumber);
}
/**
* 自动更新搜索索引
*/
private void updateSearchIndexAutomatically(Emr emr, String contextStr) {
Map<String, String> contentMap = JsonUtils.parseObject(contextStr, new TypeReference<Map<String, String>>() {});
String chiefComplaint = contentMap.getOrDefault("chiefComplaint", "");
String diagnosis = contentMap.getOrDefault("diagnosis", "");
// 获取患者信息
Patient patient = patientMapper.selectById(emr.getPatientId());
String patientName = patient != null ? patient.getName() : "";
Long patientId = patient != null ? patient.getId() : null;
// 获取医生信息
String doctorName = SecurityUtils.getUsername();
LambdaQueryWrapper<EmrSearchIndex> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(EmrSearchIndex::getEmrId, emr.getId());
EmrSearchIndex existing = emrSearchIndexService.getOne(wrapper);
if (existing != null) {
existing.setPatientName(patientName);
existing.setPatientId(patientId);
existing.setEmrTitle(chiefComplaint);
existing.setDiagnosisText(diagnosis);
existing.setDoctorName(doctorName);
existing.setUpdateTime(new Date());
emrSearchIndexService.updateById(existing);
} else {
EmrSearchIndex index = new EmrSearchIndex();
index.setEmrId(emr.getId());
index.setEncounterId(emr.getEncounterId());
index.setPatientId(patientId);
index.setPatientName(patientName);
index.setEmrType("OUTPATIENT");
index.setEmrTitle(chiefComplaint);
index.setDiagnosisText(diagnosis);
index.setDoctorName(doctorName);
index.setCreateTime(new Date());
emrSearchIndexService.save(index);
}
log.info("自动更新搜索索引: emrId={}", emr.getId());
}
/**
* 获取患者历史病历
*