<?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.qw.mapper.QwMsgMapper">

    <resultMap type="QwMsg" id="QwMsgResult">
        <result property="msgId"    column="msg_id"    />
        <result property="sessionId"    column="session_id"    />
        <result property="qwExtId"    column="qw_ext_id"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="content"    column="content"    />
        <result property="msgType"    column="msg_type"    />
        <result property="sendType"    column="send_type"    />
        <result property="companyId"    column="company_id"    />
        <result property="roleId"    column="role_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="msgJson"    column="msg_json"    />
        <result property="status"    column="status"    />
        <result property="nickName"    column="nick_name"    />
        <result property="avatar"    column="avatar"    />
    </resultMap>

    <sql id="selectQwMsgVo">
        select msg_id, session_id, qw_ext_id, qw_user_id, content, msg_type, send_type, company_id, role_id, company_user_id, create_time, msg_json, status, nick_name, avatar from qw_msg
    </sql>

    <select id="selectQwMsgList" parameterType="QwMsg" resultMap="QwMsgResult">
        <include refid="selectQwMsgVo"/>
        <where>
            <if test="sessionId != null "> and session_id = #{sessionId}</if>
            <if test="qwExtId != null  and qwExtId != ''"> and qw_ext_id = #{qwExtId}</if>
            <if test="qwUserId != null  and qwUserId != ''"> and qw_user_id = #{qwUserId}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="msgType != null "> and msg_type = #{msgType}</if>
            <if test="sendType != null "> and send_type = #{sendType}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="roleId != null "> and role_id = #{roleId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="msgJson != null  and msgJson != ''"> and msg_json = #{msgJson}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
        </where>
    </select>

    <select id="selectQwMsgByMsgId" parameterType="Long" resultMap="QwMsgResult">
        <include refid="selectQwMsgVo"/>
        where msg_id = #{msgId}
    </select>

    <insert id="insertQwMsg" parameterType="QwMsg" useGeneratedKeys="true" keyProperty="msgId">
        insert into qw_msg
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sessionId != null">session_id,</if>
            <if test="qwExtId != null">qw_ext_id,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="content != null">content,</if>
            <if test="msgType != null">msg_type,</if>
            <if test="sendType != null">send_type,</if>
            <if test="companyId != null">company_id,</if>
            <if test="roleId != null">role_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="msgJson != null">msg_json,</if>
            <if test="status != null">status,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="avatar != null">avatar,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sessionId != null">#{sessionId},</if>
            <if test="qwExtId != null">#{qwExtId},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="content != null">#{content},</if>
            <if test="msgType != null">#{msgType},</if>
            <if test="sendType != null">#{sendType},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="roleId != null">#{roleId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="msgJson != null">#{msgJson},</if>
            <if test="status != null">#{status},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="avatar != null">#{avatar},</if>
         </trim>
    </insert>

    <update id="updateQwMsg" parameterType="QwMsg">
        update qw_msg
        <trim prefix="SET" suffixOverrides=",">
            <if test="sessionId != null">session_id = #{sessionId},</if>
            <if test="qwExtId != null">qw_ext_id = #{qwExtId},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="content != null">content = #{content},</if>
            <if test="msgType != null">msg_type = #{msgType},</if>
            <if test="sendType != null">send_type = #{sendType},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="roleId != null">role_id = #{roleId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="msgJson != null">msg_json = #{msgJson},</if>
            <if test="status != null">status = #{status},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
        </trim>
        where msg_id = #{msgId}
    </update>

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

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