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

    <resultMap type="QwConversation" id="QwConversationResult">
        <result property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="conversationId"    column="conversation_id"    />
        <result property="lastMessageId"    column="last_message_id"    />
        <result property="type"    column="type"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="ext"    column="ext"    />
        <result property="unreadNum"    column="unread_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="homeUserId"    column="home_user_id"    />
    </resultMap>

    <sql id="selectQwConversationVo">
        select id, device_id, conversation_id, last_message_id, type, ancestors, ext, unread_num, create_time, update_time,home_user_id  from qw_conversation
    </sql>

    <select id="selectQwConversationList" parameterType="QwConversation" resultMap="QwConversationResult">
        <include refid="selectQwConversationVo"/>
        <where>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="conversationId != null  and conversationId != ''"> and conversation_id = #{conversationId}</if>
            <if test="lastMessageId != null "> and last_message_id = #{lastMessageId}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="ext != null  and ext != ''"> and ext = #{ext}</if>
            <if test="unreadNum != null "> and unread_num = #{unreadNum}</if>
            <if test="homeUserId != null "> and home_user_id = #{homeUserId}</if>
        </where>
    </select>

    <select id="selectQwConversationById" parameterType="Long" resultMap="QwConversationResult">
        <include refid="selectQwConversationVo"/>
        where id = #{id}
    </select>

    <select id="selectByConversationId" parameterType="String" resultMap="QwConversationResult">
        <include refid="selectQwConversationVo"/>
        where conversation_id = #{conversationId}
    </select>

    <insert id="insertQwConversation" parameterType="QwConversation" useGeneratedKeys="true" keyProperty="id">
        insert into qw_conversation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="conversationId != null">conversation_id,</if>
            <if test="lastMessageId != null">last_message_id,</if>
            <if test="type != null">type,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="ext != null">ext,</if>
            <if test="unreadNum != null">unread_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="homeUserId != null">home_user_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">#{deviceId},</if>
            <if test="conversationId != null">#{conversationId},</if>
            <if test="lastMessageId != null">#{lastMessageId},</if>
            <if test="type != null">#{type},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="ext != null">#{ext},</if>
            <if test="unreadNum != null">#{unreadNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="homeUserId != null">#{homeUserId},</if>
        </trim>
    </insert>

    <update id="updateQwConversation" parameterType="QwConversation">
        update qw_conversation
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="conversationId != null">conversation_id = #{conversationId},</if>
            <if test="lastMessageId != null">last_message_id = #{lastMessageId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="ext != null">ext = #{ext},</if>
            <if test="unreadNum != null">unread_num = #{unreadNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="homeUserId != null ">home_user_id = #{homeUserId}</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateLastMessage" parameterType="QwConversation">
        update qw_conversation
        <trim prefix="SET" suffixOverrides=",">
            <if test="lastMessageId != null">last_message_id = #{lastMessageId},</if>
        </trim>
        where conversation_id = #{conversationId}
    </update>

    <delete id="deleteQwConversationById" parameterType="Long">
        delete from qw_conversation where id = #{id}
    </delete>

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