<?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.fs.live.mapper.LiveWatchUserMapper">

    <resultMap type="LiveWatchUser" id="LiveWatchUserResult">
        <result property="id"    column="id"    />
        <result property="liveId"    column="live_id"    />
        <result property="userId"    column="user_id"    />
        <result property="msgStatus"    column="msg_status"    />
        <result property="online"    column="online"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectLiveWatchUserVo">
        select id, live_id,user_id, msg_status, online, create_time, create_by, update_by, update_time, remark from live_watch_user
    </sql>

    <select id="selectLiveWatchUserList" parameterType="LiveWatchUser" resultMap="LiveWatchUserResult">
        <include refid="selectLiveWatchUserVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="msgStatus != null "> and msg_status = #{msgStatus}</if>
            <if test="online != null "> and online = #{online}</if>
        </where>
    </select>

    <select id="selectLiveWatchUserById" parameterType="Long" resultMap="LiveWatchUserResult">
        <include refid="selectLiveWatchUserVo"/>
        where id = #{id}
    </select>

    <select id="selectWatchUserListByLiveId" resultType="com.fs.live.vo.LiveWatchUserVO">
        select
            lwu.live_id     liveId,
            lwu.user_id     userId,
            lwu.msg_status  msgStatus,
            lwu.online      online,
            fu.nickname    nickName,
            fu.avatar       avatar
        from live_watch_user lwu
        left join fs_user fu on lwu.user_id = fu.user_id
        where lwu.live_id = #{params.liveId}
        order by lwu.create_time desc
    </select>

    <select id="selectLiveWatchUserByIdAndCompanyIdAndCompanyUserId" resultMap="LiveWatchUserResult">
        <include refid="selectLiveWatchUserVo"/>
        where id = #{id} and company_id = #{companyId} and company_user_id = #{companyUserId}
    </select>

    <select id="selectWatchUserByLiveIdAndUserId" resultType="com.fs.live.vo.LiveWatchUserVO">
        select
            lwu.live_id     liveId,
            lwu.user_id     userId,
            lwu.msg_status  msgStatus,
            lwu.online      online,
            fu.nickname    nickName,
            fu.avatar       avatar
        from live_watch_user lwu
        left join fs_user fu on lwu.user_id = fu.user_id
        where lwu.live_id = #{liveId} and lwu.user_id = #{userId}
    </select>

    <select id="selectOnlineUserList" parameterType="LiveWatchUser" resultType="com.fs.live.vo.LiveWatchUserVO">
        select lwu.*,
        fu.nickname    nickName,
        fu.avatar       avatar

        from live_watch_user lwu
        left join fs_user fu on lwu.user_id = fu.user_id
        <where>
        <if test="liveId != null "> and live_id = #{liveId}</if>
        <if test="online != null "> and online = #{online}</if>
        </where>
    </select>

    <insert id="insertLiveWatchUser" parameterType="LiveWatchUser" useGeneratedKeys="true" keyProperty="id">
        insert into live_watch_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="msgStatus != null">msg_status,</if>
            <if test="online != null">online,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="msgStatus != null">#{msgStatus},</if>
            <if test="online != null">#{online},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateLiveWatchUser" parameterType="LiveWatchUser">
        update live_watch_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="msgStatus != null">msg_status = #{msgStatus},</if>
            <if test="online != null">online = #{online},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLiveWatchUserById" parameterType="Long">
        delete from live_watch_user where id = #{id}
    </delete>

    <delete id="deleteLiveWatchUserByIds" parameterType="String">
        delete from live_watch_user where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="checkOnlineNoRewardUser" resultType="com.fs.live.domain.LiveWatchUser">
        SELECT lwu.*
        FROM live_watch_user lwu
        WHERE lwu.live_id = #{liveId}
          AND lwu.online = 0
          AND NOT EXISTS (
            SELECT 1
            FROM live_reward_record lrr
            WHERE lrr.user_id = lwu.user_id
              AND lrr.live_id = lwu.live_id
              AND DATE(lrr.create_time) = DATE(#{now})
        )
    </select>
</mapper>
