diff --git a/healthlink-his-server/healthlink-his-application/src/main/java/com/healthlink/his/web/doctorstation/appservice/impl/DoctorStationEmrAppServiceImpl.java b/healthlink-his-server/healthlink-his-application/src/main/java/com/healthlink/his/web/doctorstation/appservice/impl/DoctorStationEmrAppServiceImpl.java index 070bea482..5ee04896f 100755 --- a/healthlink-his-server/healthlink-his-application/src/main/java/com/healthlink/his/web/doctorstation/appservice/impl/DoctorStationEmrAppServiceImpl.java +++ b/healthlink-his-server/healthlink-his-application/src/main/java/com/healthlink/his/web/doctorstation/appservice/impl/DoctorStationEmrAppServiceImpl.java @@ -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().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().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 emrDictList = emrDictService.list(new LambdaQueryWrapper().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 contentMap = JsonUtils.parseObject(contextStr, new TypeReference>() {}); + 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 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()); + } + /** * 获取患者历史病历 *