<?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.LiveMsgMapper">

    <resultMap type="LiveMsg" id="LiveMsgResult">
        <result property="msgId"    column="msg_id"    />
        <result property="liveId"    column="live_id"    />
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="msg"    column="msg"    />
        <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="selectLiveMsgVo">
        select msg_id, live_id,user_id, nick_name, avatar, msg, create_time, create_by, update_by, update_time, remark from live_msg
    </sql>

    <select id="selectLiveMsgList" parameterType="LiveMsg" resultMap="LiveMsgResult">
        <include refid="selectLiveMsgVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="msg != null  and msg != ''"> and msg = #{msg}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectLiveMsgByMsgId" parameterType="Long" resultMap="LiveMsgResult">
        <include refid="selectLiveMsgVo"/>
        where msg_id = #{msgId}
        order by create_time desc
    </select>

    <select id="selectLiveMsgByMsgIdAndCompanyIdAndCompanyUserId" resultMap="LiveMsgResult">
        <include refid="selectLiveMsgVo"/>
        where msg_id = #{msgId} and company_id = #{companyId} and company_user_id = #{companyUserId}
        order by create_time desc
    </select>

    <insert id="insertLiveMsg" parameterType="LiveMsg" useGeneratedKeys="true" keyProperty="msgId">
        insert into live_msg
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="msg != null">msg,</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>
            <if test="liveFlag != null">live_flag,</if>
            <if test="replayFlag != null">replay_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="msg != null">#{msg},</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>
            <if test="liveFlag != null">#{liveFlag},</if>
            <if test="replayFlag != null">#{replayFlag},</if>
         </trim>
    </insert>

    <update id="updateLiveMsg" parameterType="LiveMsg">
        update live_msg
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="msg != null">msg = #{msg},</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 msg_id = #{msgId}
    </update>

    <delete id="deleteLiveMsgByMsgId" parameterType="Long">
        delete from live_msg where msg_id = #{msgId}
    </delete>

    <delete id="deleteLiveMsgByMsgIds" parameterType="String">
        delete from live_msg where msg_id in
        <foreach item="msgId" collection="array" open="(" separator="," close=")">
            #{msgId}
        </foreach>
    </delete>
</mapper>
