<?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="com.ycwl.basic.mapper.VideoMapper">
    <insert id="add">
        insert into video(id, scenic_id, template_id, task_id, worker_id, video_url, height, width, duration)
        values (#{id}, #{scenicId}, #{templateId}, #{taskId}, #{workerId}, #{videoUrl}, #{height}, #{width}, #{duration})
    </insert>
    <insert id="addRelation">
        replace member_video(member_id, scenic_id, face_id, template_id, task_id, video_id, is_buy, order_id)
        values (#{memberId}, #{scenicId}, #{faceId}, #{templateId}, #{taskId}, #{videoId}, #{isBuy}, #{orderId})
    </insert>
    <insert id="addRelations">
        replace member_video(member_id, scenic_id, face_id, template_id, task_id, video_id, is_buy, order_id)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.memberId}, #{item.scenicId}, #{item.faceId}, #{item.templateId}, #{item.taskId}, #{item.videoId}, #{item.isBuy}, #{orderId})
        </foreach>
    </insert>
    <update id="update">
        update video
        <set>
            <if test="scenicId!= null">scenic_id = #{scenicId}, </if>
            <if test="templateId!= null">template_id = #{templateId}, </if>
            <if test="taskId!= null">task_id = #{taskId}, </if>
            <if test="workerId!= null">worker_id = #{workerId}, </if>
            <if test="videoUrl!= null">video_url = #{videoUrl}, </if>
            <if test="height!= null">height = #{height}, </if>
            <if test="width!= null">width = #{width}, </if>
            <if test="duration!= null">duration = #{duration}, </if>
        </set>
            where id = #{id}
    </update>
    <update id="updateMeta">
        update video
        set height = #{height},
            width = #{width},
            duration = #{duration}
        where id = #{id}
    </update>
    <update id="updateRelation">
        update member_video
        <set>
            <if test="isBuy!= null">is_buy = #{isBuy}, </if>
            <if test="orderId!= null">order_id = #{orderId}, </if>
        </set>
        where member_id = #{memberId} and video_id = #{videoId}
    </update>
    <update id="updateRelationWhenTaskSuccess">
        update member_video
        set video_id = #{videoId}, is_buy = #{isBuy}
        where task_id = #{taskId}
    </update>
    <delete id="deleteById">
        delete from video where id = #{id}
    </delete>
    <delete id="deleteNotBuyRelations">
        delete from member_video
        where scenic_id = #{scenicId} and is_buy = 0 and create_time &lt; #{endTime}
    </delete>
    <delete id="deleteNotBuyFaceRelations">
        delete from member_video
        where member_id = #{userId} and face_id = #{faceId} and is_buy = 0
    </delete>
    <select id="list" resultType="com.ycwl.basic.model.pc.video.resp.VideoRespVO">
        select v.id, v.scenic_id, template_id, task_id, worker_id, video_url, v.create_time, v.update_time,
        s.name scenicName, s.latitude, s.longitude, t.name templateName, t.price templatePrice,t.cover_url templateCoverUrl
        from video v
        left join scenic s on s.id = v.scenic_id
        left join template t on v.template_id = t.id
        <where>
            <if test="scenicId!= null">and v.scenic_id = #{scenicId} </if>
            <if test="templateId!= null">and template_id = #{templateId} </if>
            <if test="taskId!=null">and task_id = #{taskId}</if>
            <if test="workerId!= null">and worker_id = #{workerId} </if>
            <if test="startTime!= null">and v.create_time &gt;= #{startTime} </if>
            <if test="endTime!= null">and v.create_time &lt;= #{endTime} </if>
            <if test="faceId!= null">
                and v.task_id in (
                    select id from task where face_id = #{faceId}
                )
            </if>
        </where>
        order by v.create_time desc
    </select>
    <select id="getById" resultType="com.ycwl.basic.model.pc.video.resp.VideoRespVO">
        select v.id, v.scenic_id, template_id, task_id, worker_id, video_url, v.create_time, v.update_time,
        t.name templateName,t.price templatePrice, t.cover_url templateCoverUrl, t.slash_price slashPrice,
        s.name scenicName, v.height, v.width, v.duration
        from video v
        left join scenic s on v.scenic_id = s.id
        left join template t on v.template_id = t.id
        where v.id = #{id}
    </select>
    <select id="findByTaskId" resultType="com.ycwl.basic.model.pc.video.entity.VideoEntity">
        select * from video where task_id = #{taskId} limit 1
    </select>
    <select id="queryByRelation" resultType="com.ycwl.basic.model.pc.video.resp.VideoRespVO">
        select v.id, mv.scenic_id, v.template_id, mv.task_id, mv.face_id, worker_id, video_url, v.create_time, v.update_time,
        s.name scenicName, t.name templateName, t.price templatePrice,t.cover_url templateCoverUrl, mv.is_buy
        from member_video mv
        left join video v on mv.video_id = v.id
        left join scenic s on s.id = v.scenic_id
        left join template t on mv.template_id = t.id
        <where>
            <if test="scenicId!= null">and mv.scenic_id = #{scenicId} </if>
            <if test="memberId!= null">and mv.member_id = #{memberId} </if>
            <if test="templateId!= null">and mv.template_id = #{templateId} </if>
            <if test="taskId!=null">and mv.task_id = #{taskId} </if>
            <if test="isBuy!=null">and mv.is_buy = #{isBuy}</if>
            <if test="startTime!= null">and v.create_time &gt;= #{startTime} </if>
            <if test="endTime!= null">and v.create_time &lt;= #{endTime} </if>
            <if test="faceId!= null">and mv.face_id = #{faceId} </if>
        </where>
        order by v.create_time desc
    </select>
    <select id="userFaceTemplateVideo" resultType="com.ycwl.basic.model.pc.video.entity.MemberVideoEntity">
        select * from member_video where member_id = #{userId} and face_id = #{faceId} and template_id = #{templateId} order by create_time desc
    </select>
    <select id="listRelationByFace" resultType="com.ycwl.basic.model.pc.video.entity.MemberVideoEntity">
        select mv.*
        from member_video mv
        where mv.member_id = #{userId} and mv.face_id = #{faceId}
    </select>
    <select id="listRelationByTask" resultType="com.ycwl.basic.model.pc.video.entity.MemberVideoEntity">
        select mv.*
        from member_video mv
        where mv.task_id = #{taskId}
    </select>
    <select id="listRelationByFaceAndTemplate" resultType="com.ycwl.basic.model.pc.video.entity.MemberVideoEntity">
        select mv.*
        from member_video mv
        where mv.face_id = #{faceId} and mv.template_id = #{templateId}
    </select>
    <select id="listTaskByScenicRelation" resultType="com.ycwl.basic.model.pc.task.entity.TaskEntity">
        select t.*
        from member_video mv
        left join task t on mv.task_id = t.id
        where mv.member_id = #{userId} and t.scenic_id = #{scenicId}
    </select>
    <select id="queryUserVideo" resultType="com.ycwl.basic.model.pc.video.entity.MemberVideoEntity">
        select *
        from member_video
        where member_id = #{userId} and video_id = #{videoId}
        limit 1
    </select>
    <select id="queryRelationByMemberTask" resultType="com.ycwl.basic.model.pc.video.entity.MemberVideoEntity">
        select *
        from member_video
        where member_id = #{userId} and task_id = #{taskId}
        limit 1
    </select>
    <select id="listRelationByCreateTime" resultType="com.ycwl.basic.model.pc.video.entity.MemberVideoEntity">
        select *
        from member_video
        where create_time &gt;= #{startTime} and create_time &lt;= #{endTime}
    </select>
    <select id="getEntity" resultType="com.ycwl.basic.model.pc.video.entity.VideoEntity">
        select *
        from video
        where id = #{id}
    </select>
</mapper>