<?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.reghao.tnb.content.app.vod.db.mapper.PlayRecordMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into vod_play_record
        (`id`,`deleted`,`create_time`,`update_time`,`user_id`,`video_id`,`current_time`,`create_at`,`ended`)
        values 
        (#{id},#{deleted},#{createTime},#{updateTime},#{userId},#{videoId},#{currentTime},#{createAt},#{ended})
    </insert>

    <update id="update">
        update vod_play_record
        set update_time=now(),`current_time`=#{currentTime},create_at=#{createAt},ended=#{ended}
        where id=#{id}
    </update>

    <delete id="delete">
        delete from vod_play_record
        where id=#{id}
    </delete>

    <select id="findAll" resultType="cn.reghao.tnb.content.app.vod.model.po.PlayRecord">
        select *
        from vod_play_record
    </select>
    <select id="findCurrentTime" resultType="java.lang.Double">
        select max(`current_time`)
        from vod_play_record
        where `ended`=0 and user_id=#{userId} and video_id=#{videoId}
    </select>
    <select id="findMaxId" resultType="java.lang.Long">
        select max(create_at)
        from vod_play_record
        where user_id=#{userId}
    </select>
    <select id="countUserRecord" resultType="java.lang.Integer">
        select count(*)
        from vod_play_record record
        inner join vod_video_post post
        on record.video_id=post.video_id and record.user_id=#{userId}
        order by record.create_at desc
    </select>
    <select id="findUserRecordByPage1" resultType="cn.reghao.tnb.content.api.dto.VideoRecord">
        select post.video_id,post.title,post.cover_url,post.duration,post.publish_at,post.publish_by,
               record.current_time,record.create_at
        from vod_play_record record
        inner join vod_video_post post
        on record.video_id=post.video_id and record.user_id=#{userId}
        where record.ended is false and record.create_at &lt; #{nextId}
        order by `create_at` desc
        limit #{pageSize}
    </select>
    <select id="findByUserIdAndVideoId" resultType="cn.reghao.tnb.content.app.vod.model.po.PlayRecord">
        select *
        from vod_play_record
        where ended is false and user_id=#{userId} and video_id=#{videoId}
        order by id desc
        limit 1
    </select>
</mapper>
