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

import cn.nianxx.thhotel.workflows.reservation.domain.ReservationTaskEntity;
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 ReservationTaskMapper extends BaseMapper<ReservationTaskEntity> {

    /**
     * 分页查询前端任务列表摘要，支持按任务所属订单状态过滤。
     */
    @Select("""
            <script>
            SELECT t.*
            FROM workflow_reservation_task t
            LEFT JOIN workflow_reservation_order o
              ON o.hotel_id = t.hotel_id
             AND o.id = t.order_id
            WHERE t.hotel_id = #{hotelId}
            <if test="orderId != null">
              AND t.order_id = #{orderId}
            </if>
            <if test="orderStatus != null and orderStatus != ''">
              AND o.order_status = #{orderStatus}
            </if>
            <if test="taskType != null and taskType != ''">
              AND t.system_task_type = #{taskType}
            </if>
            <if test="taskStatus != null and taskStatus != ''">
              AND t.task_status = #{taskStatus}
            </if>
            <if test="taskSubtype != null and taskSubtype != ''">
              AND t.task_subtype = #{taskSubtype}
            </if>
            <if test="queueParticipation != null">
              AND t.queue_participation = #{queueParticipation}
            </if>
            <if test="keyword != null and keyword != ''">
              AND (
                t.system_task_type LIKE CONCAT('%', #{keyword}, '%')
                OR t.task_subtype LIKE CONCAT('%', #{keyword}, '%')
                <if test="keywordOrderIds != null and keywordOrderIds.size() > 0">
                  OR t.order_id IN
                  <foreach collection="keywordOrderIds" item="orderIdItem" open="(" separator="," close=")">
                    #{orderIdItem}
                  </foreach>
                </if>
                <if test="sourceMessageIds != null and sourceMessageIds.size() > 0">
                  OR t.source_message_id IN
                  <foreach collection="sourceMessageIds" item="sourceMessageIdItem" open="(" separator="," close=")">
                    #{sourceMessageIdItem}
                  </foreach>
                </if>
              )
            </if>
            ORDER BY t.order_id ASC, t.execution_order ASC
            </script>
            """)
    Page<ReservationTaskEntity> selectFrontendTaskPage(
            Page<ReservationTaskEntity> page,
            @Param("hotelId") String hotelId,
            @Param("orderId") Long orderId,
            @Param("orderStatus") String orderStatus,
            @Param("taskType") String taskType,
            @Param("taskStatus") String taskStatus,
            @Param("taskSubtype") String taskSubtype,
            @Param("queueParticipation") Boolean queueParticipation,
            @Param("keyword") String keyword,
            @Param("keywordOrderIds") List<Long> keywordOrderIds,
            @Param("sourceMessageIds") List<Long> sourceMessageIds);
}
