package com.dji.sample.media.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dji.sample.media.model.MediaFileEntity;
import com.dji.sample.media.model.UpstreamMediaFileDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author sean
 * @version 0.2
 * @date 2021/12/9
 */
public interface IFileMapper extends BaseMapper<MediaFileEntity> {

    @Select("<script> " +
            "SELECT MF.workspace_id workspaceId " +
            ",MW.workspace_name workspaceName " +
            ",MF.drone deviceNo " +
            ",DE.nickname nickName " +
            ",MD.dir_name dirName " +
            ",MF.size fileSize " +
            ",MF.picture_time pictureTime " +
            ",MF.picture_type pictureType " +
            ",MF.object_key fileUrl " +
            ",MF.file_name fileName " +
            ",MF.file_id fileId " +
            "FROM MEDIA_FILE MF " +
            "LEFT JOIN MEDIA_DIR MD ON MF.DIR_ID = MD.ID " +
            "LEFT JOIN MANAGE_DEVICE DE ON MF.DRONE = DE.DEVICE_SN " +
            "LEFT JOIN MANAGE_WORKSPACE MW ON MF.workspace_id = MW.workspace_id " +
            "WHERE 1 = 1 " +
            "<if test= 'null != workspaceName  '>" +
            "    AND MW.workspace_name LIKE CONCAT('%', #{workspaceName},'%') " +
            "</if>" +
            "<if test= 'null != workspaceId  '>" +
            "    AND MW.workspace_id = #{workspaceId}  " +
            "</if>" +
            "<if test= 'null != startTime'  >" +
            "    AND MF.create_time &gt; #{startTime}  " +
            "</if>" +
            "<if test= 'endTime != null' >" +
            "    AND MF.create_time &lt; #{endTime}  " +
            "</if>" +
            "order by pictureTime desc LIMIT #{page},#{pageSize} " +
            "</script>")
    List<UpstreamMediaFileDTO> getMediaFiles(@Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("workspaceName") String workspaceName,@Param("page") long page,@Param("pageSize") long pageSize,@Param("workspaceId") String workspaceId);
    @Select("<script> " +
            "SELECT count(*) total " +
            "FROM MEDIA_FILE MF " +
            "LEFT JOIN MEDIA_DIR MD ON MF.DIR_ID = MD.ID " +
            "LEFT JOIN MANAGE_DEVICE DE ON MF.DRONE = DE.DEVICE_SN " +
            "LEFT JOIN MANAGE_WORKSPACE MW ON MF.workspace_id = MW.workspace_id " +
            "WHERE 1 = 1 " +
            "<if test= 'null != workspaceName  '>" +
            "    AND MW.workspace_name LIKE CONCAT('%', #{workspaceName},'%') " +
            "</if>" +
            "<if test= 'null != workspaceId  '>" +
            "    AND MW.workspace_id = #{workspaceId}  " +
            "</if>" +
            "<if test= 'null != startTime'  >" +
            "    AND MF.create_time &gt; #{startTime}  " +
            "</if>" +
            "<if test= 'endTime != null' >" +
            "    AND MF.create_time &lt; #{endTime}  " +
            "</if>" +
            "</script>")
    long getMediaFilesCount(@Param("startTime") Long startTime, @Param("endTime") Long endTime, @Param("workspaceName") String workspaceName,@Param("workspaceId") String workspaceId);

}
