diff --git a/openhis-server-new/openhis-domain/src/main/java/com/openhis/clinical/mapper/OrderMapper.java b/openhis-server-new/openhis-domain/src/main/java/com/openhis/clinical/mapper/OrderMapper.java index 5d106a20..6df707a4 100644 --- a/openhis-server-new/openhis-domain/src/main/java/com/openhis/clinical/mapper/OrderMapper.java +++ b/openhis-server-new/openhis-domain/src/main/java/com/openhis/clinical/mapper/OrderMapper.java @@ -44,10 +44,12 @@ public interface OrderMapper extends BaseMapper { int updatePayStatus(@Param("orderId") Long orderId, @Param("payStatus") Integer payStatus, @Param("payTime") Date payTime); /** - * 统计同一患者在同一科室、同一时段(上午/下午)内的有效预约订单数量 + * 统计同一患者在同一科室、同一自然日(预约日 00:00~次日 00:00)内的有效预约订单数量。 + * 匹配规则:优先 {@code department_id}(对应 adm_organization.id);仅当 ID 为空时用 {@code department_name} 兜底。 * * @param patientId 患者ID - * @param departmentId 科室ID + * @param departmentId 科室 ID(order_main.department_id) + * @param departmentName 科室名称(ID 为空时与 order_main.department_name 比对) * @param startTime 时段起始时间(含) * @param endTime 时段结束时间(不含) * @param statuses 订单状态集合(如 1=已预约,2=已取号) @@ -55,6 +57,7 @@ public interface OrderMapper extends BaseMapper { */ int countPatientDeptOrdersInPeriod(@Param("patientId") Long patientId, @Param("departmentId") Long departmentId, + @Param("departmentName") String departmentName, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("statuses") List statuses); diff --git a/openhis-server-new/openhis-domain/src/main/java/com/openhis/clinical/service/impl/TicketServiceImpl.java b/openhis-server-new/openhis-domain/src/main/java/com/openhis/clinical/service/impl/TicketServiceImpl.java index 95bc8101..f48164a5 100644 --- a/openhis-server-new/openhis-domain/src/main/java/com/openhis/clinical/service/impl/TicketServiceImpl.java +++ b/openhis-server-new/openhis-domain/src/main/java/com/openhis/clinical/service/impl/TicketServiceImpl.java @@ -183,25 +183,23 @@ public class TicketServiceImpl extends ServiceImpl impleme throw new RuntimeException("该排班医生已停诊"); } - // 2.1 同一患者同一天/同一科室/同一时段(上午/下午)不可重复预约 - if (dto.getPatientId() != null && slot.getDepartmentId() != null && slot.getScheduleDate() != null && slot.getExpectTime() != null) { - boolean isMorning = slot.getExpectTime().isBefore(LocalTime.NOON); + // 2.1 同一患者同一天/同一科室不可重复预约(自然日 00:00~次日 00:00,上午+下午共限 1 次;科室以 department_id 为准,无 ID 时用科室名兜底) + if (dto.getPatientId() != null && slot.getScheduleDate() != null + && (slot.getDepartmentId() != null + || (slot.getDepartmentName() != null && !slot.getDepartmentName().isBlank()))) { LocalDate scheduleDateForCheck = slot.getScheduleDate(); - LocalDateTime periodStart = isMorning - ? LocalDateTime.of(scheduleDateForCheck, LocalTime.MIN) - : LocalDateTime.of(scheduleDateForCheck, LocalTime.NOON); - LocalDateTime periodEnd = isMorning - ? LocalDateTime.of(scheduleDateForCheck, LocalTime.NOON) - : LocalDateTime.of(scheduleDateForCheck.plusDays(1), LocalTime.MIN); + LocalDateTime periodStart = LocalDateTime.of(scheduleDateForCheck, LocalTime.MIN); + LocalDateTime periodEnd = LocalDateTime.of(scheduleDateForCheck.plusDays(1), LocalTime.MIN); Date startTime = Date.from(periodStart.atZone(ZoneId.systemDefault()).toInstant()); Date endTime = Date.from(periodEnd.atZone(ZoneId.systemDefault()).toInstant()); // 预约去重以订单为准(order_main),因为预约成功会先落订单;clinical_ticket 不一定在此链路写入 List effectiveOrderStatuses = Arrays.asList(AppointmentOrderStatus.BOOKED, AppointmentOrderStatus.CHECKED_IN); - int exists = orderMapper.countPatientDeptOrdersInPeriod(dto.getPatientId(), slot.getDepartmentId(), startTime, endTime, effectiveOrderStatuses); + int exists = orderMapper.countPatientDeptOrdersInPeriod(dto.getPatientId(), slot.getDepartmentId(), slot.getDepartmentName(), + startTime, endTime, effectiveOrderStatuses); if (exists > 0) { - throw new RuntimeException("该患者已在当前科室该时段存在预约记录,不可重复预约"); + throw new RuntimeException("该患者已在当前科室当日存在预约记录,不可重复预约"); } } diff --git a/openhis-server-new/openhis-domain/src/main/resources/mapper/clinical/OrderMapper.xml b/openhis-server-new/openhis-domain/src/main/resources/mapper/clinical/OrderMapper.xml index c3212215..6bf5c677 100644 --- a/openhis-server-new/openhis-domain/src/main/resources/mapper/clinical/OrderMapper.xml +++ b/openhis-server-new/openhis-domain/src/main/resources/mapper/clinical/OrderMapper.xml @@ -222,7 +222,17 @@ from order_main and patient_id = #{patientId} - and department_id = #{departmentId} + + + and department_id = #{departmentId} + + + and trim(department_name) = trim(#{departmentName}) + + + and 1 = 0 + + and appointment_time >= #{startTime} and appointment_time < #{endTime}