package cn.nianxx.thhotel.workflows.reservation.mapper;

import cn.nianxx.thhotel.workflows.reservation.domain.ReservationOrderEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * Reservation 订单 Mapper，只负责本表持久化访问。
 */
@Mapper
public interface ReservationOrderMapper extends BaseMapper<ReservationOrderEntity> {

    /**
     * 分页查询前端订单列表。排序按订单维护的最新业务活动时间倒序，避免每次列表查询聚合全量任务。
     */
    @Select("""
            <script>
            SELECT o.*
            FROM workflow_reservation_order o
            WHERE o.hotel_id = #{hotelId}
              AND o.order_visibility = #{visibleVisibility}
            <if test="orderStatus != null and orderStatus != ''">
              AND o.order_status = #{orderStatus}
            </if>
            <if test="groupCode != null and groupCode != ''">
              AND (
                o.order_business_key LIKE CONCAT('%', #{groupCode}, '%')
                OR o.active_business_key LIKE CONCAT('%', #{groupCode}, '%')
                OR o.display_name LIKE CONCAT('%', #{groupCode}, '%')
              )
            </if>
            <if test="confirmationNumber != null and confirmationNumber != ''">
              AND (
                o.order_business_key LIKE CONCAT('%', #{confirmationNumber}, '%')
                OR o.active_business_key LIKE CONCAT('%', #{confirmationNumber}, '%')
                OR o.display_name LIKE CONCAT('%', #{confirmationNumber}, '%')
              )
            </if>
            <if test="keyword != null and keyword != ''">
              AND (
                o.order_business_key LIKE CONCAT('%', #{keyword}, '%')
                OR o.active_business_key LIKE CONCAT('%', #{keyword}, '%')
                OR o.temporary_order_code LIKE CONCAT('%', #{keyword}, '%')
                OR o.order_status LIKE CONCAT('%', #{keyword}, '%')
                OR o.display_name LIKE CONCAT('%', #{keyword}, '%')
                <if test="sourceMessageIds != null and sourceMessageIds.size() > 0">
                  OR o.source_message_id IN
                  <foreach collection="sourceMessageIds" item="sourceMessageIdItem" open="(" separator="," close=")">
                    #{sourceMessageIdItem}
                  </foreach>
                </if>
              )
            </if>
            ORDER BY
              o.latest_activity_at DESC,
              o.updated_at DESC,
              o.id DESC
            </script>
            """)
    Page<ReservationOrderEntity> selectFrontendOrderPage(
            Page<ReservationOrderEntity> page,
            @Param("hotelId") String hotelId,
            @Param("orderStatus") String orderStatus,
            @Param("groupCode") String groupCode,
            @Param("confirmationNumber") String confirmationNumber,
            @Param("keyword") String keyword,
            @Param("visibleVisibility") String visibleVisibility,
            @Param("sourceMessageIds") List<Long> sourceMessageIds);
}
