package com.erp.print.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.print.entity.PrintTask;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;

@Mapper
public interface PrintTaskRepository extends BaseMapper<PrintTask> {

    @Select("SELECT * FROM print_task WHERE deleted = 0 ORDER BY priority DESC, created_at ASC")
    IPage<PrintTask> selectPageList(Page<PrintTask> page);

    @Select("SELECT * FROM print_task WHERE deleted = 0 AND task_no = #{taskNo}")
    PrintTask selectByTaskNo(@Param("taskNo") String taskNo);

    @Select("SELECT * FROM print_task WHERE deleted = 0 AND task_status = #{taskStatus} ORDER BY priority DESC, created_at ASC")
    IPage<PrintTask> selectByStatus(Page<PrintTask> page, @Param("taskStatus") String taskStatus);

    @Select("SELECT * FROM print_task WHERE deleted = 0 AND document_type = #{documentType} AND document_id = #{documentId}")
    PrintTask selectByDocument(@Param("documentType") String documentType, @Param("documentId") Long documentId);

    @Select("<script>" +
            "SELECT * FROM print_task WHERE deleted = 0 " +
            "<if test='taskStatus != null'> AND task_status = #{taskStatus} </if>" +
            "<if test='startDate != null'> AND created_at &gt;= #{startDate} </if>" +
            "<if test='endDate != null'> AND created_at &lt;= #{endDate} </if>" +
            "ORDER BY priority DESC, created_at DESC" +
            "</script>")
    IPage<PrintTask> selectTaskPage(Page<PrintTask> page,
                                     @Param("taskStatus") String taskStatus,
                                     @Param("startDate") LocalDateTime startDate,
                                     @Param("endDate") LocalDateTime endDate);
}
