package cn.lihu.jh.module.ecg.service.appointment;
|
|
import cn.lihu.jh.framework.common.pojo.PageResult;
|
import cn.lihu.jh.framework.common.util.date.DateUtils;
|
import cn.lihu.jh.framework.common.util.object.BeanUtils;
|
import cn.lihu.jh.module.ecg.controller.admin.appointment.vo.AppointmentConfirmReqVO;
|
import cn.lihu.jh.module.ecg.controller.admin.appointment.vo.AppointmentPageReqVO;
|
import cn.lihu.jh.module.ecg.controller.admin.appointment.vo.AppointmentSaveReqVO;
|
import cn.lihu.jh.module.ecg.controller.admin.queue.vo.QueueSaveReqVO;
|
import cn.lihu.jh.module.ecg.dal.dataobject.appointment.AppointmentDO;
|
import cn.lihu.jh.module.ecg.dal.dataobject.patient.PatDetails;
|
import cn.lihu.jh.module.ecg.dal.dataobject.queue.QueueDO;
|
import cn.lihu.jh.module.ecg.dal.mysql.appointment.AppointmentMapper;
|
import cn.lihu.jh.module.ecg.enums.HisCheckCodeEnum;
|
import cn.lihu.jh.module.ecg.feign.RemoteDataService;
|
import cn.lihu.jh.module.ecg.service.config.EcgConfigService;
|
import cn.lihu.jh.module.ecg.service.queue.QueueService;
|
import cn.lihu.jh.module.ecg.service.queuesequence.QueueSequenceService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.ibatis.annotations.Param;
|
import org.springframework.dao.DuplicateKeyException;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.validation.annotation.Validated;
|
|
import javax.annotation.Resource;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Optional;
|
import java.util.stream.Collectors;
|
|
import static cn.lihu.jh.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.lihu.jh.module.ecg.enums.ErrorCodeConstants.*;
|
|
/**
|
* 预约 Service 实现类
|
*
|
* @author 马剑波
|
*/
|
@Service
|
@Validated
|
@Slf4j
|
public class AppointmentServiceImpl implements AppointmentService {
|
|
@Resource
|
private EcgConfigService ecgConfigService;
|
|
@Resource
|
private RemoteDataService remoteDataService;
|
|
@Resource
|
private QueueSequenceService queueSequenceService;
|
|
@Resource
|
private QueueService queueService;
|
|
@Resource
|
private AppointmentMapper appointmentMapper;
|
|
@Override
|
public Long createAppointment(AppointmentSaveReqVO createReqVO) {
|
// 插入
|
AppointmentDO appointment = BeanUtils.toBean(createReqVO, AppointmentDO.class);
|
|
try {
|
appointmentMapper.insert(appointment);
|
} catch (DuplicateKeyException e) {
|
throw exception(APPOINTMENT_EXIST_TODAY);
|
}
|
|
// 返回
|
return appointment.getId();
|
}
|
|
@Override
|
public void updateAppointment(AppointmentSaveReqVO updateReqVO) {
|
// 校验存在
|
validateAppointmentExists(updateReqVO.getId());
|
// 更新
|
AppointmentDO updateObj = BeanUtils.toBean(updateReqVO, AppointmentDO.class);
|
appointmentMapper.updateById(updateObj);
|
}
|
|
@Override
|
public void deleteAppointment(Long id) {
|
// 校验存在
|
validateAppointmentExists(id);
|
// 删除
|
appointmentMapper.deleteById(id);
|
}
|
|
private void validateAppointmentExists(Long id) {
|
if (appointmentMapper.selectById(id) == null) {
|
throw exception(APPOINTMENT_NOT_EXISTS);
|
}
|
}
|
|
@Override
|
public AppointmentDO getAppointment(Long id) {
|
return appointmentMapper.selectById(id);
|
}
|
|
@Override
|
public AppointmentDO getCurAppointmentByPatIdAndCheckType(String patId, Integer checkType) {
|
AppointmentDO appointmentDO = appointmentMapper.getByPatAndCheckTypeAndBookDate(patId, checkType);
|
return appointmentDO;
|
}
|
|
/**
|
* @param patCode 编号 : !!! 不一定就是本系统的 患者编号(patID), 根源在于 检查单扫描出来的编号 不一定是 本系统的 患者编号(patID)
|
* @return
|
*/
|
@Override
|
public List<AppointmentDO> queryAndCreateAppointmentByPatId(String patCode) {
|
// 从医院平台查询
|
List<AppointmentDO> appointmentExtermalList = getAppointmentExtermal(patCode);
|
|
for (int appointmentIndex = 0; appointmentIndex < appointmentExtermalList.size(); appointmentIndex++) {
|
AppointmentDO appointmentExtermal = appointmentExtermalList.get(appointmentIndex);
|
if (null == appointmentExtermal) continue;
|
|
// 从DB查询预约,是否已经存在
|
AppointmentDO appointmentDO = appointmentMapper.getByEpisodeIdAndApplyNo(appointmentExtermal.getEpisodeId(), appointmentExtermal.getApplyNo());
|
if (null == appointmentDO) {
|
appointmentMapper.insert(appointmentExtermal);
|
} else {
|
appointmentExtermal.setId(appointmentDO.getId()); // 确保 返回值包含 appointment id
|
|
// 处理第一次确认时还没有预约时间,再来确认时已有预约时间的情况
|
if (appointmentDO.getBookDate() != appointmentExtermal.getBookDate()) {
|
appointmentMapper.updateById(appointmentExtermal);
|
}
|
}
|
}
|
|
// 查询[queue表]补充 book_seq_num 到返回值中
|
if (!appointmentExtermalList.isEmpty()) {
|
List<Long> appointIdlist = appointmentExtermalList.stream().map(appointmentDO -> appointmentDO.getId()).toList();
|
List<QueueDO> simpleQueueDOList = queueService.selectBookSeqNumByAppointIdList(appointIdlist);
|
if (!simpleQueueDOList.isEmpty()) {
|
Map<Long, Integer> mapAppointIdVsBookSeqNo = simpleQueueDOList.stream().collect(Collectors.toMap(QueueDO::getAppointId, QueueDO::getBookSeqNum));
|
appointmentExtermalList.forEach(appointmentExterma -> appointmentExterma.setBookSeqNum(mapAppointIdVsBookSeqNo.get(appointmentExterma.getId())));
|
}
|
}
|
|
// 不从DB查询
|
//List<AppointmentDO> appointmentDOList = appointmentMapper.getCurrentPatId( patCode );
|
//return 0 == appointmentDOList.size() ? null : appointmentDOList.get(0);
|
|
return appointmentExtermalList;
|
}
|
|
@Override
|
public PageResult<AppointmentDO> getAppointmentPage(AppointmentPageReqVO pageReqVO) {
|
return appointmentMapper.selectPage(pageReqVO);
|
}
|
|
@Override
|
public List<AppointmentDO> getAppointmentExtermal(String patId) {
|
return appointmentMapper.getCurrentPatId(patId);
|
}
|
|
// @Override
|
// public List<AppointmentDO> getAppointmentExtermal(String patId) {
|
// List<AppointmentDO> curAppointDOList = new ArrayList<AppointmentDO>();
|
//
|
// RestApiReqBodyVo reqBodyVo = new RestApiReqBodyVo();
|
// if (18 == patId.length()) {
|
// reqBodyVo.setSfzh( patId );
|
// } else {
|
// reqBodyVo.setMzzyh(patId);
|
// }
|
//
|
// // QueryRisReportList queryEcgRequest
|
// RestApiResult<AppointmentExternal> result = remoteDataService.httpApi("queryEcgRequest", "ECG", "ECG", reqBodyVo);
|
// if (0 == result.getRow().size()) {
|
// return curAppointDOList;
|
// }
|
//
|
// for (int appointIndex=0; appointIndex < result.getRow().size(); appointIndex++ ) {
|
// AppointmentExternal appointmentExternal = result.getRow().get( appointIndex );
|
//
|
// AppointmentDO appointmentDO = BeanUtils.toBean(appointmentExternal, AppointmentDO.class);
|
// appointmentDO.setApplyNo( appointmentExternal.getReqIdeApplyno() );
|
// appointmentDO.setEpisodeId( appointmentExternal.getEpisodeID() );
|
// appointmentDO.setPatSrc( getCorrespondingPatientSource(appointmentExternal.getAdmTypeCode()) );
|
// appointmentDO.setPatId(appointmentExternal.getPatientID()); // 内容为 身份证号 或 门诊住院号
|
// appointmentDO.setPatName(appointmentExternal.getPatName());
|
// appointmentDO.setPatGender(Byte.valueOf(appointmentExternal.getPatgender()));
|
// appointmentDO.setPatBirthday(DateUtils.ofUTC(appointmentExternal.getEncPatBirthDate()).toLocalDate());
|
// appointmentDO.setPatIdentityId(appointmentExternal.getIdentityID());
|
// appointmentDO.setPatAddr(appointmentExternal.getAddress());
|
// appointmentDO.setPatDeptCode(appointmentExternal.getPatLocDeptCode());
|
// appointmentDO.setPatDeptDesc(appointmentExternal.getPatLocDeptDesc());
|
// appointmentDO.setPatWardCode(appointmentExternal.getPatLocWardCode());
|
// appointmentDO.setPatWardDesc(appointmentExternal.getPatLocWardDesc());
|
// appointmentDO.setPatMobile(appointmentExternal.getPhone());
|
// appointmentDO.setPatBedNo(appointmentExternal.getEnBedno());
|
// appointmentDO.setBookCheckType(getCorrespondingCheckType(appointmentExternal.getPlanDefItemList().getPlanDefItem().getPlanDefItemcode()));
|
// appointmentDO.setBookTime(DateUtils.ofUTC(appointmentExternal.getReqAuthoredOn())); // 开单时间
|
// appointmentDO.setBookSrc( 0 );
|
//
|
// // 光开单的情况,没有预约时间
|
// if (null != appointmentExternal.getReqExtBooktime() ) {
|
// appointmentDO.setBookDate(DateUtils.ofUTC(appointmentExternal.getReqExtBooktime()).toLocalDate());
|
// LocalDateTime bookStartTime = DateUtils.ofUTC(appointmentExternal.getReqExtBooktime());
|
// LocalDateTime bookEndTime = bookStartTime.plusMinutes( ecgConfigService.getTimeslotLength());
|
// appointmentDO.setBookTimeslot((bookStartTime.getHour() * 100 + bookStartTime.getMinute()) * 10000 + bookEndTime.getHour() * 100 + bookEndTime.getMinute());
|
// }
|
//
|
// appointmentDO.setPaid( 0 );
|
//
|
// curAppointDOList.add( appointmentDO );
|
// }
|
//
|
// return curAppointDOList;
|
// }
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Integer appoitmentConfirm(AppointmentConfirmReqVO confirmReqVO) {
|
AppointmentDO appointment = getAppointment(confirmReqVO.getId());
|
if (null == appointment) throw exception(PATIENT_NOT_EXISTS);
|
|
// 手动预约,直接设置预约日期为当天
|
if (confirmReqVO.getIsVip() == 1) {
|
appointment.setBookDate(LocalDate.now());
|
}
|
|
if (!DateUtils.isToday(appointment.getBookDate())) throw exception(APPOINTMENT_NOT_TODAY);
|
|
|
Integer newSeqNo = null;
|
if (confirmReqVO.getIsVip() == 0) {
|
newSeqNo = queueSequenceService.distributeSeqNo(confirmReqVO.getBookCheckType(), confirmReqVO.getBookTimeslot());
|
} else {
|
newSeqNo = queueSequenceService.distributeVipSeqNo(confirmReqVO.getBookCheckType(), confirmReqVO.getBookTimeslot());
|
}
|
|
try {
|
QueueSaveReqVO queueSaveReqVO = new QueueSaveReqVO();
|
queueSaveReqVO.setApplyNo(appointment.getApplyNo());
|
queueSaveReqVO.setEpisodeId(appointment.getEpisodeId());
|
queueSaveReqVO.setAppointId(appointment.getId());
|
queueSaveReqVO.setPatId(appointment.getPatId());
|
queueSaveReqVO.setPatName(appointment.getPatName());
|
queueSaveReqVO.setPatGender(appointment.getPatGender());
|
queueSaveReqVO.setBookDate(appointment.getBookDate());
|
queueSaveReqVO.setBookTimeslot(confirmReqVO.getBookTimeslot()); /*appointment.getBookTimeslot()*/
|
queueSaveReqVO.setBookCheckType(appointment.getBookCheckType());
|
queueSaveReqVO.setBookSeqNum(newSeqNo);
|
queueSaveReqVO.setIsVip(confirmReqVO.getIsVip());
|
queueSaveReqVO.setPassed((byte) 0);
|
queueSaveReqVO.setExpired((byte) 0);
|
queueSaveReqVO.setPatDetails(getPatDetails(appointment));
|
queueService.queue(queueSaveReqVO);
|
} catch (DuplicateKeyException duplicateKeyException) {
|
throw exception(APPOINTMENT_HAVE_QUEUED);
|
}
|
|
return newSeqNo;
|
}
|
|
private PatDetails getPatDetails(AppointmentDO appointment) {
|
PatDetails patDetails = new PatDetails();
|
patDetails.setAppointmentId(appointment.getId());
|
patDetails.setId(appointment.getPatId());
|
patDetails.setName(appointment.getPatName());
|
patDetails.setMobile(appointment.getPatMobile());
|
patDetails.setDeptCode(appointment.getPatDeptCode());
|
patDetails.setDeptDesc(appointment.getPatDeptDesc());
|
patDetails.setWardCode(appointment.getPatWardCode());
|
patDetails.setWardDesc(appointment.getPatWardDesc());
|
patDetails.setBedNo(appointment.getPatBedNo());
|
patDetails.setSource(appointment.getPatSrc());
|
return patDetails;
|
}
|
|
@Override
|
public Long handleAppointmentCreate(Map<String, Object> dataMap) {
|
if (dataMap == null) {
|
throw exception(APPOINTMENT_CREATE_FAIL);
|
}
|
|
try {
|
// 创建AppointmentDO对象
|
AppointmentDO appointment = new AppointmentDO();
|
|
// 从dataMap中提取数据
|
Map<String, Object> encounter = extractEncounter(dataMap);
|
if (encounter == null) {
|
throw exception(APPOINTMENT_CREATE_FAIL);
|
}
|
|
// 设置就诊流水号
|
Optional.ofNullable(encounter.get("id")).map(id -> (Map<String, Object>) id).map(id -> (List<?>) id.get("item")).filter(items -> items.size() > 1).map(items -> (Map<String, Object>) items.get(1)).map(item -> (String) item.get("extension")).ifPresent(appointment::setEpisodeId);
|
|
Optional.ofNullable(encounter.get("location")).map(location -> (Map<String, Object>) location).map(location -> (Map<String, Object>) location.get("serviceDeliveryLocation")).map(serviceDeliveryLocation -> (Map<String, Object>) serviceDeliveryLocation.get("location")).map(location -> (Map<String, Object>) location.get("id")).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).ifPresent(appointment::setPatBedNo);
|
|
|
// 设置患者来源类型
|
Optional.ofNullable(encounter.get("code")).map(code -> (Map<String, Object>) code).map(code -> (String) code.get("code")).map(Integer::parseInt).ifPresent(appointment::setPatSrc);
|
|
// 设置患者信息
|
Optional.ofNullable(encounter.get("subject")).map(subject -> (Map<String, Object>) subject).map(subject -> (Map<String, Object>) subject.get("patient")).map(patient -> (Map<String, Object>) patient.get("patientPerson")).ifPresent(patientPerson -> {
|
// 设置患者ID
|
Optional.ofNullable(encounter.get("subject")).map(subject -> (Map<String, Object>) subject).map(subject -> (Map<String, Object>) subject.get("patient")).map(patient -> (Map<String, Object>) patient.get("id")).map(id -> (Map<String, Object>) id).map(id -> (List<?>) id.get("item")).filter(items -> items.size() > 1).map(items -> (Map<String, Object>) items.get(1)).map(item -> (String) item.get("extension")).ifPresent(appointment::setPatId);
|
|
// 设置患者姓名
|
Optional.ofNullable(patientPerson.get("name")).map(name -> (Map<String, Object>) name).map(name -> (Map<String, Object>) name.get("item")).map(item -> (Map<String, Object>) item.get("part")).map(part -> (String) part.get("value")).ifPresent(appointment::setPatName);
|
|
// 设置患者手机和电话
|
Optional.ofNullable(patientPerson.get("telecom")).map(telecom -> (Map<String, Object>) telecom).map(telecom -> (Map<String, Object>) telecom.get("item")).map(item -> (String) item.get("value")).ifPresent(phone -> {
|
appointment.setPatMobile(phone);
|
appointment.setPatPhone(phone);
|
});
|
|
// 设置患者性别
|
Optional.ofNullable(patientPerson.get("administrativeGenderCode")).map(genderCode -> (Map<String, Object>) genderCode).map(genderCode -> (String) genderCode.get("code")).map(Byte::parseByte).ifPresent(appointment::setPatGender);
|
|
// 设置患者生日
|
Optional.ofNullable(patientPerson.get("birthTime")).map(birthTime -> (Map<String, Object>) birthTime).map(birthTime -> (String) birthTime.get("value")).map(birthdayStr -> LocalDate.parse(birthdayStr, DateTimeFormatter.ofPattern("yyyyMMdd"))).ifPresent(appointment::setPatBirthday);
|
|
// 设置患者地址
|
Optional.ofNullable(patientPerson.get("addr")).map(addr -> (Map<String, Object>) addr).map(addr -> (Map<String, Object>) addr.get("item")).map(item -> (Map<String, Object>) item.get("part")).map(part -> (String) part.get("value")).ifPresent(appointment::setPatAddr);
|
|
// 设置患者身份证号
|
Optional.ofNullable(patientPerson.get("id")).map(id -> (Map<String, Object>) id).map(id -> (List<?>) id.get("item")).filter(items -> !items.isEmpty()).map(items -> (Map<String, Object>) items.get(0)).map(item -> (String) item.get("extension")).ifPresent(appointment::setPatIdentityId);
|
});
|
|
// 设置科室和病区信息
|
Optional.ofNullable(encounter.get("location")).map(location -> (Map<String, Object>) location).map(location -> (Map<String, Object>) location.get("serviceDeliveryLocation")).map(location -> (Map<String, Object>) location.get("serviceProviderOrganization")).ifPresent(org -> {
|
// 设置科室代码和名称
|
Optional.ofNullable(org.get("id")).map(id -> (Map<String, Object>) id).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).ifPresent(appointment::setPatDeptCode);
|
|
Optional.ofNullable(org.get("name")).map(name -> (Map<String, Object>) name).map(name -> (Map<String, Object>) name.get("item")).map(item -> (Map<String, Object>) item.get("part")).map(part -> (String) part.get("value")).ifPresent(appointment::setPatDeptDesc);
|
|
// 设置病区代码和名称
|
Optional.ofNullable(org.get("asOrganizationPartOf")).map(wardOrg -> (Map<String, Object>) wardOrg).map(wardOrg -> (Map<String, Object>) wardOrg.get("wholeOrganization")).ifPresent(wardOrg -> {
|
Optional.ofNullable(wardOrg.get("id")).map(id -> (Map<String, Object>) id).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).ifPresent(appointment::setPatWardCode);
|
|
Optional.ofNullable(wardOrg.get("name")).map(name -> (Map<String, Object>) name).map(name -> (Map<String, Object>) name.get("item")).map(item -> (Map<String, Object>) item.get("part")).map(part -> (String) part.get("value")).ifPresent(appointment::setPatWardDesc);
|
});
|
});
|
|
// 设置支付状态
|
Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("zdy")).map(zdy -> {
|
Object payStatus = zdy.get("PayStatus");
|
if (payStatus != null) {
|
try {
|
return Integer.parseInt(payStatus.toString());
|
} catch (NumberFormatException e) {
|
log.warn("PayStatus转换失败: {}", payStatus);
|
return null;
|
}
|
}
|
return null;
|
}).ifPresent(appointment::setPaid);
|
// 设置申请单号
|
Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("id")).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).ifPresent(appointment::setApplyNo);
|
|
//设置检查类型
|
// String checkType = Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("component2")).map(component2 -> (Map<String, Object>) component2.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("code")).map(code -> (String) code.get("code")).orElseThrow(() -> exception(APPOINTMENT_CREATE_FAIL));
|
String checkType = Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("component2")).map(component2 -> (Map<String, Object>) component2.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("code")).map(code -> (String) code.get("code")).orElseThrow(() -> exception(APPOINTMENT_CREATE_FAIL));
|
if (checkType != null || checkType != "")
|
appointment.setBookCheckType(Integer.valueOf(HisCheckCodeEnum.getByCode(checkType.substring(1))));
|
|
// 将AppointmentDO转换为AppointmentSaveReqVO并保存
|
AppointmentSaveReqVO saveReqVO = BeanUtils.toBean(appointment, AppointmentSaveReqVO.class);
|
return createAppointment(saveReqVO);
|
} catch (Exception e) {
|
log.error("[handleAppointmentCreate][处理预约创建失败 dataMap({})]", dataMap, e);
|
throw exception(APPOINTMENT_CREATE_FAIL);
|
}
|
}
|
|
@Override
|
public void handleAppointmentUpdate(Map<String, Object> dataMap) {
|
if (dataMap == null) {
|
throw exception(APPOINTMENT_UPDATE_FAIL);
|
}
|
|
try {
|
// 从dataMap中提取申请单号
|
String applyNo = Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("id")).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).orElseThrow(() -> exception(APPOINTMENT_CREATE_FAIL));
|
|
// 查询现有预约记录
|
AppointmentDO existingAppointment = appointmentMapper.getByApplyNo(applyNo);
|
|
// 如果记录不存在,则调用handleAppointmentCreate进行新增
|
if (existingAppointment == null) {
|
handleAppointmentCreate(dataMap);
|
return;
|
}
|
|
// 从dataMap中提取数据
|
Map<String, Object> encounter = extractEncounter(dataMap);
|
if (encounter == null) {
|
throw exception(APPOINTMENT_CREATE_FAIL);
|
}
|
|
// 更新就诊流水号
|
Optional.ofNullable(encounter.get("id")).map(id -> (Map<String, Object>) id).map(id -> (List<?>) id.get("item")).filter(items -> items.size() > 1).map(items -> (Map<String, Object>) items.get(1)).map(item -> (String) item.get("extension")).ifPresent(existingAppointment::setEpisodeId);
|
|
// 设置床位号
|
Optional.ofNullable(encounter.get("location")).map(location -> (Map<String, Object>) location).map(location -> (Map<String, Object>) location.get("serviceDeliveryLocation")).map(serviceDeliveryLocation -> (Map<String, Object>) serviceDeliveryLocation.get("location")).map(location -> (Map<String, Object>) location.get("id")).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).ifPresent(existingAppointment::setPatBedNo);
|
|
|
// 更新患者来源类型
|
Optional.ofNullable(encounter.get("code")).map(code -> (Map<String, Object>) code).map(code -> (String) code.get("code")).map(Integer::parseInt).ifPresent(existingAppointment::setPatSrc);
|
|
// 更新患者信息
|
Optional.ofNullable(encounter.get("subject")).map(subject -> (Map<String, Object>) subject).map(subject -> (Map<String, Object>) subject.get("patient")).map(patient -> (Map<String, Object>) patient.get("patientPerson")).ifPresent(patientPerson -> {
|
// 更新患者ID
|
Optional.ofNullable(encounter.get("subject")).map(subject -> (Map<String, Object>) subject).map(subject -> (Map<String, Object>) subject.get("patient")).map(patient -> (Map<String, Object>) patient.get("id")).map(id -> (Map<String, Object>) id).map(id -> (List<?>) id.get("item")).filter(items -> items.size() > 1).map(items -> (Map<String, Object>) items.get(1)).map(item -> (String) item.get("extension")).ifPresent(existingAppointment::setPatId);
|
|
// 更新患者姓名
|
Optional.ofNullable(patientPerson.get("name")).map(name -> (Map<String, Object>) name).map(name -> (Map<String, Object>) name.get("item")).map(item -> (Map<String, Object>) item.get("part")).map(part -> (String) part.get("value")).ifPresent(existingAppointment::setPatName);
|
|
// 更新患者手机和电话
|
Optional.ofNullable(patientPerson.get("telecom")).map(telecom -> (Map<String, Object>) telecom).map(telecom -> (Map<String, Object>) telecom.get("item")).map(item -> (String) item.get("value")).ifPresent(phone -> {
|
existingAppointment.setPatMobile(phone);
|
existingAppointment.setPatPhone(phone);
|
});
|
|
// 更新患者性别
|
Optional.ofNullable(patientPerson.get("administrativeGenderCode")).map(genderCode -> (Map<String, Object>) genderCode).map(genderCode -> (String) genderCode.get("code")).map(Byte::parseByte).ifPresent(existingAppointment::setPatGender);
|
|
// 更新患者生日
|
Optional.ofNullable(patientPerson.get("birthTime")).map(birthTime -> (Map<String, Object>) birthTime).map(birthTime -> (String) birthTime.get("value")).map(birthdayStr -> LocalDate.parse(birthdayStr, DateTimeFormatter.ofPattern("yyyyMMdd"))).ifPresent(existingAppointment::setPatBirthday);
|
|
// 更新患者地址
|
Optional.ofNullable(patientPerson.get("addr")).map(addr -> (Map<String, Object>) addr).map(addr -> (Map<String, Object>) addr.get("item")).map(item -> (Map<String, Object>) item.get("part")).map(part -> (String) part.get("value")).ifPresent(existingAppointment::setPatAddr);
|
|
// 更新患者身份证号
|
Optional.ofNullable(patientPerson.get("id")).map(id -> (Map<String, Object>) id).map(id -> (List<?>) id.get("item")).filter(items -> !items.isEmpty()).map(items -> (Map<String, Object>) items.get(0)).map(item -> (String) item.get("extension")).ifPresent(existingAppointment::setPatIdentityId);
|
});
|
|
// 更新科室和病区信息
|
Optional.ofNullable(encounter.get("location")).map(location -> (Map<String, Object>) location).map(location -> (Map<String, Object>) location.get("serviceDeliveryLocation")).map(location -> (Map<String, Object>) location.get("serviceProviderOrganization")).ifPresent(org -> {
|
// 更新科室代码和名称
|
Optional.ofNullable(org.get("id")).map(id -> (Map<String, Object>) id).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).ifPresent(existingAppointment::setPatDeptCode);
|
|
Optional.ofNullable(org.get("name")).map(name -> (Map<String, Object>) name).map(name -> (Map<String, Object>) name.get("item")).map(item -> (Map<String, Object>) item.get("part")).map(part -> (String) part.get("value")).ifPresent(existingAppointment::setPatDeptDesc);
|
|
// 更新病区代码和名称
|
Optional.ofNullable(org.get("asOrganizationPartOf")).map(wardOrg -> (Map<String, Object>) wardOrg).map(wardOrg -> (Map<String, Object>) wardOrg.get("wholeOrganization")).ifPresent(wardOrg -> {
|
Optional.ofNullable(wardOrg.get("id")).map(id -> (Map<String, Object>) id).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).ifPresent(existingAppointment::setPatWardCode);
|
|
Optional.ofNullable(wardOrg.get("name")).map(name -> (Map<String, Object>) name).map(name -> (Map<String, Object>) name.get("item")).map(item -> (Map<String, Object>) item.get("part")).map(part -> (String) part.get("value")).ifPresent(existingAppointment::setPatWardDesc);
|
});
|
});
|
|
// 更新支付状态
|
Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("zdy")).map(zdy -> {
|
Object payStatus = zdy.get("PayStatus");
|
if (payStatus != null) {
|
try {
|
return Integer.parseInt(payStatus.toString());
|
} catch (NumberFormatException e) {
|
log.warn("PayStatus转换失败: {}", payStatus);
|
return null;
|
}
|
}
|
return null;
|
}).ifPresent(existingAppointment::setPaid);
|
//设置检查类型
|
String checkType = Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("component2")).map(component2 -> (Map<String, Object>) component2.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("code")).map(code -> (String) code.get("code")).orElseThrow(() -> exception(APPOINTMENT_CREATE_FAIL));
|
if (checkType != null || checkType != "")
|
existingAppointment.setBookCheckType(Integer.valueOf(HisCheckCodeEnum.getByCode(checkType.substring(1))));
|
|
// 将AppointmentDO转换为AppointmentSaveReqVO并更新
|
AppointmentSaveReqVO updateReqVO = BeanUtils.toBean(existingAppointment, AppointmentSaveReqVO.class);
|
updateAppointment(updateReqVO);
|
} catch (Exception e) {
|
log.error("[handleAppointmentUpdate][申请单更新失败 dataMap({})]", dataMap, e);
|
throw exception(APPOINTMENT_UPDATE_FAIL);
|
}
|
}
|
|
@Override
|
public void handleAppointmentStateUpdate(Map<String, Object> dataMap) {
|
if (dataMap == null) {
|
throw exception(APPOINTMENT_STATUS_UPDATE_FAIL);
|
}
|
// 从dataMap中提取申请单号
|
String applyNo = Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("placerGroup")).map(placerGroup -> (Map<String, Object>) placerGroup.get("component2")).map(component2 -> (Map<String, Object>) component2.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("id")).map(id -> (Map<String, Object>) id.get("item")).map(item -> (String) item.get("extension")).orElseThrow(() -> exception(APPOINTMENT_CREATE_FAIL));
|
|
// 查询现有预约记录
|
AppointmentDO existingAppointment = appointmentMapper.getByApplyNo(applyNo);
|
|
// 如果记录不存在,则调用handleAppointmentCreate进行新增
|
if (existingAppointment == null) {
|
handleAppointmentCreate(dataMap);
|
return;
|
}
|
|
|
// 从dataMap中提取申请单号
|
String status = Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("placerGroup")).map(placerGroup -> (Map<String, Object>) placerGroup.get("component2")).map(component2 -> (Map<String, Object>) component2.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("component1")).map(component1 -> (Map<String, Object>) component1.get("processStep")).map(processStep -> (Map<String, Object>) processStep.get("code")).map(code -> (Map<String, Object>) code.get("displayName")).map(displayName -> (String) displayName.get("value")).orElseThrow(() -> exception(APPOINTMENT_CREATE_FAIL));
|
|
existingAppointment.setStatus(status);
|
// 将AppointmentDO转换为AppointmentSaveReqVO并更新
|
AppointmentSaveReqVO updateReqVO = BeanUtils.toBean(existingAppointment, AppointmentSaveReqVO.class);
|
updateAppointment(updateReqVO);
|
}
|
|
|
/**
|
* 从dataMap中提取encounter数据
|
*/
|
private Map<String, Object> extractEncounter(Map<String, Object> dataMap) {
|
return Optional.ofNullable(dataMap.get("controlActProcess")).map(controlActProcess -> (Map<String, Object>) controlActProcess).map(controlActProcess -> (Map<String, Object>) controlActProcess.get("subject")).map(subject -> (Map<String, Object>) subject.get("observationRequest")).map(observationRequest -> (Map<String, Object>) observationRequest.get("componentOf1")).map(componentOf1 -> (Map<String, Object>) componentOf1.get("encounter")).orElse(null);
|
}
|
|
@Override
|
public void handleCheckAppointmentUpdate(Map<String, Object> dataMap) {
|
// 从dataMap中获取controlActProcess
|
Map<String, Object> controlActProcess = (Map<String, Object>) dataMap.get("controlActProcess");
|
Map<String, Object> subject = (Map<String, Object>) controlActProcess.get("subject");
|
Map<String, Object> actAppointment = (Map<String, Object>) subject.get("actAppointment");
|
|
// 获取申请编号
|
List<Map<String, Object>> referencedOrders = (List<Map<String, Object>>) actAppointment.get("referencedOrder");
|
String applyNo = null;
|
if (!CollectionUtils.isEmpty(referencedOrders)) {
|
Map<String, Object> actOrder = (Map<String, Object>) referencedOrders.get(0).get("actOrder");
|
Map<String, Object> id = (Map<String, Object>) actOrder.get("id");
|
applyNo = (String) id.get("extension");
|
}
|
|
if (applyNo == null) {
|
log.warn("[handleCheckAppointmentUpdate][未找到申请编号]");
|
return;
|
}
|
|
// 根据applyNo查询预约记录
|
AppointmentDO appointment = appointmentMapper.getByApplyNo(applyNo);
|
if (appointment == null) {
|
log.warn("[handleCheckAppointmentUpdate][未找到预约记录 applyNo({})]", applyNo);
|
return;
|
}
|
|
// 获取预约时间
|
Map<String, Object> effectiveTime = (Map<String, Object>) actAppointment.get("effectiveTime");
|
Map<String, Object> any = (Map<String, Object>) effectiveTime.get("any");
|
String bookTimeStr = (String) any.get("value");
|
|
// 获取预约来源
|
Map<String, Object> code = (Map<String, Object>) actAppointment.get("code");
|
String bookSrc = (String) code.get("code");
|
|
// 解析预约时间
|
LocalDateTime bookTime = LocalDateTime.parse(bookTimeStr, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
|
LocalDate bookDate = bookTime.toLocalDate();
|
|
// 获取时间段
|
String timeRange = (String) ((Map<String, Object>) actAppointment.get("ZDY")).get("time");
|
String[] time1 = timeRange.split(" ");
|
String[] times = time1[1].split("-");
|
String startTime = times[0].trim();
|
String endTime = times[1].trim();
|
|
// 转换时间段格式为hhmmhhmm
|
int bookTimeslot = Integer.parseInt(startTime.replace(":", "")) * 10000 + Integer.parseInt(endTime.replace(":", ""));
|
|
// 更新预约信息
|
appointment.setBookDate(bookDate);
|
appointment.setBookTimeslot(bookTimeslot);
|
appointment.setBookTime(bookTime);
|
appointment.setBookSrc(Integer.parseInt(bookSrc));
|
|
// 保存更新
|
appointmentMapper.updateById(appointment);
|
}
|
|
public AppointmentDO getByApplyNo(String applyNo) {
|
return appointmentMapper.getByApplyNo(applyNo);
|
}
|
}
|