<?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.chat.db.mapper.ChatDialogMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into chat_chat_dialog
        (`chat_id`,`chat_type`,`receiver_id`,`disturb`,`robot`,`top`,`display`,`user_id`,`unread_num`)
        values 
        (#{chatId},#{chatType},#{receiverId},#{disturb},#{robot},#{top},#{display},#{userId},#{unreadNum})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert into chat_chat_dialog
        (`chat_id`,`chat_type`,`receiver_id`,`disturb`,`robot`,`top`,`display`,`user_id`,`unread_num`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.chatId},#{item.chatType},#{item.receiverId},#{item.disturb},#{item.robot},#{item.top},#{item.display},#{item.userId},#{item.unreadNum})
        </foreach>
    </insert>

    <update id="updateSetDisturb">
        update chat_chat_dialog set update_time=now(),disturb=#{disturb}
        where id=#{dialogId} and user_id=#{userId}
    </update>
    <update id="updateSetTop">
        update chat_chat_dialog set update_time=now(),top=#{top}
        where id=#{dialogId} and user_id=#{userId}
    </update>
    <update id="updateSetDisplay">
        update chat_chat_dialog set update_time=now(),display=#{display}
        where id=#{dialogId} and user_id=#{userId}
    </update>
    <update id="updateSetClearUnread">
        update chat_chat_dialog set update_time=now(),unread_num=unread_num+1
        where receiver_id=#{receiverId} and user_id=#{userId}
    </update>
    <update id="updateSetIncreaseUnread">
        update chat_chat_dialog set update_time=now(),unread_num=0
        where receiver_id=#{receiverId} and user_id=#{userId}
    </update>

    <delete id="deleteGroupChatDialog">
        delete from chat_chat_dialog
        where receiver_id=#{groupId} and user_id in
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <select id="findByReceiverAndUserId" resultType="cn.reghao.tnb.content.app.chat.model.po.chat.ChatDialog">
        select * from chat_chat_dialog where receiver_id=#{receiverId} and user_id=#{userId}
    </select>
    <select id="findChatDialog" resultType="cn.reghao.tnb.content.app.chat.model.po.chat.ChatDialog">
        select * from chat_chat_dialog
        where chat_type=#{chatType} and receiver_id=#{receiverId} and user_id=#{userId}
    </select>
    <select id="findChatDialogsByUserId" resultType="cn.reghao.tnb.content.app.chat.model.po.chat.ChatDialog">
        select *
        from chat_chat_dialog
        where display is true and user_id=#{userId}
    </select>
</mapper>
