<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ink.wgink.module.file.media.dao.video.IVideoDao">

    <cache/>

    <resultMap id="videoDTO" type="ink.wgink.module.file.media.pojo.dtos.video.VideoDTO">
        <id column="file_id" property="fileId"/>
        <result column="file_name" property="fileName"/>
        <result column="file_full_path" property="fileFullPath"/>
        <result column="file_path" property="filePath"/>
        <result column="file_size" property="fileSize"/>
        <result column="file_type" property="fileType"/>
        <result column="file_md5" property="fileMd5"/>
        <result column="is_back" property="isBack"/>
        <result column="static_url" property="staticUrl"/>
        <result column="file_summary" property="fileSummary"/>
        <result column="video_duration" property="videoDuration"/>
        <result column="video_width" property="videoWidth"/>
        <result column="video_height" property="videoHeight"/>
        <result column="video_bit_rate" property="videoBitRate"/>
        <result column="video_encoder" property="videoEncoder"/>
        <result column="video_frame_rate" property="videoFrameRate"/>
        <result column="audio_duration" property="audioDuration"/>
        <result column="audio_bit_rate" property="audioBitRate"/>
        <result column="audio_sample_rate" property="audioSampleRate"/>
        <result column="convert_status" property="convertStatus"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `media_video` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `file_id` char(36) NOT NULL COMMENT '主键',
            `file_name` varchar(255) DEFAULT NULL COMMENT '名称',
            `file_full_path` varchar(500) DEFAULT NULL COMMENT '全路径',
            `file_path` varchar(255) DEFAULT NULL COMMENT '路径',
            `file_size` varchar(100) DEFAULT NULL COMMENT '大小',
            `file_type` varchar(255) DEFAULT NULL COMMENT '类型',
            `file_md5` varchar(255) DEFAULT NULL COMMENT 'MD5',
            `file_summary` varchar(255) DEFAULT NULL COMMENT '备注',
            `video_duration` varchar(255) DEFAULT NULL COMMENT '时长',
            `video_width` int(11) DEFAULT NULL COMMENT '宽度',
            `video_height` int(11) DEFAULT NULL COMMENT '高度',
            `video_bit_rate` int(11) DEFAULT NULL COMMENT '比特率',
            `video_encoder` varchar(255) DEFAULT NULL COMMENT '编码',
            `video_frame_rate` int(11) DEFAULT NULL COMMENT '帧率',
            `audio_duration` varchar(255) DEFAULT NULL COMMENT '音频时长',
            `audio_bit_rate` int(11) DEFAULT NULL COMMENT '音频比特率',
            `audio_sample_rate` int(11) DEFAULT NULL COMMENT '音频采样率',
            `convert_status` varchar(255) DEFAULT 'unConverted' COMMENT '转码状态',
            `is_back` int(1) DEFAULT '0' COMMENT '是否备份',
            `static_url` varchar(500) DEFAULT NULL COMMENT '静态url',
            `creator` char(36) DEFAULT NULL COMMENT '创建人',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            `modifier` char(36) DEFAULT NULL COMMENT '修改人',
            `gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
            `is_delete` int(1) DEFAULT '0',
            PRIMARY KEY (`id`,`file_id`) USING BTREE,
            UNIQUE KEY `file_id` (`file_id`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 保存文件 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO media_video(
            file_id,
            file_name,
            file_full_path,
            file_path,
            file_size,
            file_type,
            file_md5,
            file_summary,
            video_duration,
            video_width,
            video_height,
            video_bit_rate,
            video_encoder,
            video_frame_rate,
            audio_duration,
            audio_bit_rate,
            audio_sample_rate,
            convert_status,
            is_back,
            static_url,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{fileId},
            #{fileName},
            #{fileFullPath},
            #{filePath},
            #{fileSize},
            #{fileType},
            #{fileMd5},
            #{fileSummary},
            #{videoDuration},
            #{videoWidth},
            #{videoHeight},
            #{videoBitRate},
            #{videoEncoder},
            #{videoFrameRate},
            #{audioDuration},
            #{audioBitRate},
            #{audioSampleRate},
            #{convertStatus},
            #{isBack},
            #{staticUrl},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除文件 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            media_video
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            file_id IN
        <foreach collection="fileIds" index="index" open="(" separator="," close=")">
            #{fileIds[${index}]}
        </foreach>
    </update>

    <!-- 删除文件(物理删除) -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            media_video
        WHERE
            file_id IN
        <foreach collection="fileIds" index="index" open="(" separator="," close=")">
            #{fileIds[${index}]}
        </foreach>
    </delete>

    <!-- 更新 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            media_video
        SET
            <if test="fileFullPath != null and fileFullPath != ''">
            file_full_path = #{fileFullPath},
            </if>
            <if test="filePath != null and filePath != ''">
            file_path = #{filePath},
            </if>
            <if test="fileSize != null and fileSize != ''">
            file_size = #{fileSize},
            </if>
            <if test="fileType != null and fileType != ''">
            file_type = #{fileType},
            </if>
            <if test="videoDuration != null and videoDuration != ''">
            video_duration = #{videoDuration},
            </if>
            <if test="videoWidth != null">
            video_width = #{videoWidth},
            </if>
            <if test="videoHeight != null">
            video_height = #{videoHeight},
            </if>
            <if test="videoBitRate != null">
            video_bit_rate = #{videoBitRate},
            </if>
            <if test="videoEncoder != null and videoEncoder != ''">
            video_encoder = #{videoEncoder},
            </if>
            <if test="videoFrameRate != null">
            video_frame_rate = #{videoFrameRate},
            </if>
            <if test="audioDuration != null and audioDuration != ''">
            audio_duration = #{audioDuration},
            </if>
            <if test="audioBitRate != null">
            audio_bit_rate = #{audioBitRate},
            </if>
            <if test="audioSampleRate != null">
            audio_sample_rate = #{audioSampleRate},
            </if>
            <if test="convertStatus != null and convertStatus != ''">
            convert_status = #{convertStatus},
            </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            file_id = #{fileId}
    </update>

    <!-- 更新转码状态 -->
    <update id="updateConvert" parameterType="map" flushCache="true">
        UPDATE
            media_video
        SET
            convert_status = #{convertStatus},
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            file_id = #{fileId}
    </update>

    <!-- 详情 -->
    <select id="get" parameterType="map" resultMap="videoDTO" useCache="true">
        SELECT
            file_id,
            file_name,
            file_full_path,
            file_path,
            file_size,
            file_type,
            file_md5,
            file_summary,
            video_duration,
            video_width,
            video_height,
            video_bit_rate,
            video_encoder,
            video_frame_rate,
            audio_duration,
            audio_bit_rate,
            audio_sample_rate,
            convert_status,
            is_back,
            static_url
        FROM
            media_video
        <where>
            is_delete = 0
            <if test="fileId != null and fileId != ''">
        AND
            file_id = #{fileId}
            </if>
            <if test="fileMd5 != null and fileMd5 != ''">
        AND
            file_md5 = #{fileMd5}
            </if>
        </where>
    </select>

    <!-- 列表 -->
    <select id="list" parameterType="map" resultMap="videoDTO" useCache="true">
        SELECT
            file_id,
            file_name,
            file_full_path,
            file_path,
            file_size,
            file_type,
            file_md5,
            file_summary,
            video_duration,
            video_width,
            video_height,
            video_bit_rate,
            video_encoder,
            video_frame_rate,
            audio_duration,
            audio_bit_rate,
            audio_sample_rate,
            convert_status,
            is_back,
            static_url,
            LEFT(gmt_create, 19) gmt_create
        FROM
            media_video
        <where>
            is_delete = 0
            <if test="keywords != null and keywords != ''">
        AND
            file_name LIKE CONCAT('%', #{keywords}, '%')
            </if>
            <if test="startTime != null and startTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
            </if>
            <if test="fileIds != null and fileIds.size > 0">
        AND
            file_id IN
                <foreach collection="fileIds" index="index" open="(" separator="," close=")">
                #{fileIds[${index}]}
                </foreach>
            </if>
        </where>
    </select>

</mapper>