<?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="cn.com.tenlion.materialstore.dao.materialvideo.IMaterialVideoDao">

    <resultMap id="materialVideoDTO" type="cn.com.tenlion.materialstore.pojo.dtos.materialvideo.MaterialVideoDTO">
        <result column="material_status" property="materialStatus"/>
        <result column="creator_name" property="creatorName"/>
        <result column="material_video_id" property="materialVideoId"/>
        <result column="material_video_file_id" property="materialVideoFileId"/>
        <result column="material_video_photo_id" property="materialVideoPhotoId"/>
        <result column="material_video_duration" property="materialVideoDuration"/>
        <result column="material_id" property="materialId"/>
        <result column="material_order" property="materialOrder"/>
        <result column="material_remark" property="materialRemark"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="materialVideoBO" type="cn.com.tenlion.materialstore.pojo.bos.materialvideo.MaterialVideoBO">
        <result column="material_video_id" property="materialVideoId"/>
        <result column="material_video_file_id" property="materialVideoFileId"/>
        <result column="material_id" property="materialId"/>
        <result column="material_order" property="materialOrder"/>
        <result column="material_remark" property="materialRemark"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="materialVideoPO" type="cn.com.tenlion.materialstore.pojo.pos.materialvideo.MaterialVideoPO">
        <result column="material_video_id" property="materialVideoId"/>
        <result column="material_video_file_id" property="materialVideoFileId"/>
        <result column="material_id" property="materialId"/>
        <result column="material_order" property="materialOrder"/>
        <result column="material_remark" property="materialRemark"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 新增视频素材 -->
    <insert id="save" parameterType="map">
        INSERT INTO m_material_video(
            material_status,
            material_video_id,
            material_video_file_id,
            material_id,
            material_order,
            material_remark,
            material_video_photo_id,
            material_video_duration,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            1,
            #{materialVideoId},
            #{materialVideoFileId},
            #{materialId},
            #{materialOrder},
            #{materialRemark},
            #{materialVideoPhotoId},
            #{materialVideoDuration},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除视频素材（物理） -->
    <update id="removeByMaterialId" parameterType="map">
        DELETE FROM
            m_material_video
        WHERE
            material_id = #{materialId}
    </update>

    <!-- 删除视频素材 -->
    <update id="remove" parameterType="map">
        UPDATE
            m_material_video
        SET
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            is_delete = 1
        WHERE
            material_video_id IN
        <foreach collection="materialVideoIds" index="index" open="(" separator="," close=")">
            #{materialVideoIds[${index}]}
        </foreach>
    </update>

    <!-- 删除视频素材（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            m_material_video
        WHERE
            material_video_id IN
        <foreach collection="materialVideoIds" index="index" open="(" separator="," close=")">
            #{materialVideoIds[${index}]}
        </foreach>
    </update>

    <!-- 更改状态 -->
    <update id="updateCheck" parameterType="map">
        UPDATE
            m_material_video
        SET
            material_status = #{materialStatus}
        WHERE
            material_video_id = #{materialVideoId}
    </update>

    <!-- 修改视频素材 -->
    <update id="update" parameterType="map">
        UPDATE
            m_material_video
        SET
        <if test="materialVideoFileId != null and materialVideoFileId != ''">
            material_video_file_id = #{materialVideoFileId},
        </if>
        <if test="materialId != null and materialId != ''">
            material_id = #{materialId},
        </if>
        <if test="materialOrder != null">
            material_order = #{materialOrder},
        </if>
        <if test="materialRemark != null and materialRemark != ''">
            material_remark = #{materialRemark},
        </if>
        <if test="materialVideoPhotoId != null and materialVideoPhotoId != ''">
            material_video_photo_id = #{materialVideoPhotoId},
        </if>
        <if test="materialVideoDuration != null">
            material_video_duration = #{materialVideoDuration},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            material_video_id = material_video_id
        WHERE
            material_video_id = #{materialVideoId}
    </update>

    <!-- 视频素材详情 -->
    <select id="get" parameterType="map" resultMap="materialVideoDTO">
        SELECT
            t1.material_video_file_id,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.material_video_id,
            t1.material_status,
            t1.material_video_photo_id,
            t1.material_video_duration
        FROM
            m_material_video t1
        WHERE
            t1.is_delete = 0
            <if test="materialVideoId != null and materialVideoId != ''">
        AND
            t1.material_video_id = #{materialVideoId}
            </if>
    </select>

    <!-- 视频素材详情 -->
    <select id="getBO" parameterType="map" resultMap="materialVideoBO">
        SELECT
            t1.material_video_id,
            t1.material_video_file_id,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_material_video t1
        WHERE
            t1.is_delete = 0
            <if test="materialVideoId != null and materialVideoId != ''">
        AND
            t1.material_video_id = #{materialVideoId}
            </if>
    </select>

    <!-- 视频素材详情 -->
    <select id="getPO" parameterType="map" resultMap="materialVideoPO">
        SELECT
            t1.material_video_id,
            t1.material_video_file_id,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_material_video t1
        WHERE
            t1.is_delete = 0
            <if test="materialVideoId != null and materialVideoId != ''">
        AND
            t1.material_video_id = #{materialVideoId}
            </if>
    </select>

    <!-- 视频素材列表 -->
    <select id="list" parameterType="map" resultMap="materialVideoDTO">
        SELECT
            t1.material_video_id,
            t1.material_video_file_id,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete,
            t1.material_status,
            t2.user_name AS creator_name,
            t1.material_video_photo_id,
            t1.material_video_duration
        FROM
            m_material_video t1
        LEFT JOIN
            sys_user t2
        ON
            t1.creator = t2.user_id
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            AND (
            <!-- 这里添加其他条件 -->
            t1.material_remark LIKE CONCAT('%', #{keywords}, '%')
            OR
            t2.user_name LIKE CONCAT('%', #{keywords}, '%')
            )
        </if>
        <if test="materialStatus != null and materialStatus != ''">
            AND
            t1.material_status = #{materialStatus}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="materialVideoIds != null and materialVideoIds.size > 0">
        AND
            t1.material_video_id IN
            <foreach collection="materialVideoIds" index="index" open="(" separator="," close=")">
                #{materialVideoIds[${index}]}
            </foreach>
        </if>
        <if test="materialId != '' and materialId != null">
            AND t1.material_id = #{materialId}
        </if>
        <if test="creator != '' and creator != null">
            AND t1.creator = #{creator}
        </if>
        ORDER BY t1.gmt_create DESC, t1.material_order
    </select>

    <!-- 视频素材列表 -->
    <select id="listBO" parameterType="map" resultMap="materialVideoBO">
        SELECT
            t1.material_video_id,
            t1.material_video_file_id,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_material_video t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="materialVideoIds != null and materialVideoIds.size > 0">
        AND
            t1.material_video_id IN
            <foreach collection="materialVideoIds" index="index" open="(" separator="," close=")">
                #{materialVideoIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 视频素材列表 -->
    <select id="listPO" parameterType="map" resultMap="materialVideoPO">
        SELECT
            t1.material_video_id,
            t1.material_video_file_id,
            t1.material_id,
            t1.material_order,
            t1.material_remark,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_material_video t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="materialVideoIds != null and materialVideoIds.size > 0">
        AND
            t1.material_video_id IN
            <foreach collection="materialVideoIds" index="index" open="(" separator="," close=")">
                #{materialVideoIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 视频素材统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            m_material_video t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>