package com.android.trisolarisserver.repo import com.android.trisolarisserver.models.room.RoomStay import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query import org.springframework.data.repository.query.Param import java.util.UUID interface RoomStayRepo : JpaRepository { @Query(""" select rs.room.id from RoomStay rs where rs.property.id = :propertyId and rs.toAt is null """) fun findOccupiedRoomIds(@Param("propertyId") propertyId: UUID): List @Query(""" select distinct rs.room.id from RoomStay rs where rs.property.id = :propertyId and rs.fromAt < :toAt and (rs.toAt is null or rs.toAt > :fromAt) """) fun findOccupiedRoomIdsBetween( @Param("propertyId") propertyId: UUID, @Param("fromAt") fromAt: java.time.OffsetDateTime, @Param("toAt") toAt: java.time.OffsetDateTime ): List @Query(""" select rs from RoomStay rs where rs.booking.id = :bookingId and rs.toAt is null """) fun findActiveByBookingId(@Param("bookingId") bookingId: UUID): List @Query(""" select rs.room.id from RoomStay rs where rs.property.id = :propertyId and rs.room.id in :roomIds and rs.toAt is null """) fun findActiveRoomIds( @Param("propertyId") propertyId: UUID, @Param("roomIds") roomIds: List ): List @Query(""" select case when count(rs) > 0 then true else false end from RoomStay rs where rs.property.id = :propertyId and rs.room.id = :roomId and rs.fromAt < :toAt and (rs.toAt is null or rs.toAt > :fromAt) """) fun existsOverlap( @Param("propertyId") propertyId: UUID, @Param("roomId") roomId: UUID, @Param("fromAt") fromAt: java.time.OffsetDateTime, @Param("toAt") toAt: java.time.OffsetDateTime ): Boolean @Query(""" select rs from RoomStay rs join fetch rs.room r join fetch r.roomType rt join fetch rs.booking b left join fetch b.primaryGuest g where rs.property.id = :propertyId and rs.toAt is null order by r.roomNumber """) fun findActiveByPropertyIdWithDetails(@Param("propertyId") propertyId: UUID): List }