package com.dji.sample.wayline.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dji.sample.wayline.model.entity.WaylineJobEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author sean
 * @version 1.1
 * @date 2022/6/1
 */
public interface IWaylineJobMapper extends BaseMapper<WaylineJobEntity> {

    @Select("<script>" +
            " select wj.id" +
            "   ,wj.job_id jobId " +
            "   ,wj.name  " +
            "   ,wj.file_id fileId " +
            "   ,wj.dock_sn dockSn " +
            "   ,wj.workspace_id workspaceId " +
            "   ,wj.task_type taskType " +
            "   ,wj.wayline_type waylineType " +
            "   ,wj.username  " +
            "   ,wj.execute_time executeTime " +
            "   ,wj.end_time endTime " +
            "   ,wj.error_code errorCode " +
            "   ,wj.status  " +
            "   ,wj.rth_altitude rthAltitude " +
            "   ,wj.out_of_control outOfControlAction " +
            "   ,wj.media_count mediaCount " +
            "   ,wj.create_time createTime " +
            "   ,wj.update_time updateTime " +
            "   ,wj.begin_time beginTime " +
            "   ,wj.completed_time completedTime " +
            "   ,wj.parent_id parentId " +
            "   ,wj.breakpoint_continuation breakpointContinuation " +
            "   ,wj.wayline_precision_type waylinePrecisionType " +
            "   ,wj.exit_wayline_when_rc_lost exitWaylineWhenRcLost " +
            "   ,wj.wayline_mission_state waylineMissionState " +
            " from wayline_job wj" +
            " left join wayline_file wf on wj.file_id = wf.wayline_id " +
            " where wj.workspace_id = #{workspaceId} " +
            " <if test='null != beginTime '>" +
            " and wj.begin_time &lt; #{beginTime}  " +
            " </if>" +
            " <if test='null != endTime '>" +
            " and wj.end_time &gt; #{endTime}) " +
            " </if>" +
            " <if test=' null != searchInfo '>" +
            " and (wj.name like #{searchInfo} or wf.name like  #{searchInfo} )" +
            " </if>" +
            " <if test='snList != null and !snList.isEmpty()'>" +
            " and wj.dock_sn in" +
            " <foreach item='item' index='index' collection='snList' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </if>" +
            " order by wj.id" +
            " limit #{page}, #{pageSize}" +
            "</script>")
    List<WaylineJobEntity> selectJobList(@Param("page") long page,
                                         @Param("pageSize") long pageSize,
                                         @Param("beginTime") Long beginTime,
                                         @Param("endTime") Long endTime,
                                         @Param("searchInfo") String searchInfo,
                                         @Param("workspaceId") String workspaceId,
                                        @Param("snList") List<String> snList);

    @Select("<script>" +
            " select count(wj.id) total from wayline_job wj" +
            " left join wayline_file wf on wj.file_id = wf.wayline_id " +
            " where wj.workspace_id = #{workspaceId} " +
            " <if test='null != beginTime '>" +
            " and wj.begin_time &lt; #{beginTime}  " +
            " </if>" +
            " <if test=' null != endTime '>" +
            " and wj.end_time &gt; #{endTime}) " +
            " </if>" +
            " <if test=' null != searchInfo '>" +
            " and (wj.name like #{searchInfo} or wf.name like  #{searchInfo} )" +
            " </if>" +
            " <if test='snList != null and !snList.isEmpty()'>" +
            " and wj.dock_sn in" +
            " <foreach item='item' index='index' collection='snList' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </if>" +
            "</script>")
    long selectTotal(@Param("beginTime") Long beginTime,
                    @Param("endTime") Long endTime,
                    @Param("searchInfo") String searchInfo,
                    @Param("workspaceId") String workspaceId,
                     @Param("snList") List<String> snList);
}
