<?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.sqx.modules.gameChat.dao.ChatContentTcwmDao">

    <select id="selectOrdersChatPage" resultType="com.sqx.modules.gameChat.entity.ChatContentTcwm">
        select *, if(chatCreateTime>createTime, chatCreateTime, createTime) as ordertime from (
        select i.indent_id as indentId, i.order_id as ordersId, gs.shop_name as shopName, gs.shop_cover as shopCover,
        u.user_name as userName,u.avatar as userAvatar,riderU.nick_name as riderName,riderU.avatar as riderAvatar,
        u.user_id as userId,i.shop_id as shopId,
        (select count(*) from chat_content_tcwm cct where 1 = 1
        <if test="userId!=null">
            and cct.user_id = #{userId} and cct.user_status = 0
        </if>
        <if test="riderId!=null">
            and cct.rider_id = #{riderId} and cct.rider_status = 0
        </if>
        <if test="shopId!=null">
            and cct.shop_id = #{shopId} and cct.shop_status = 0
        </if>
        and cct.orders_id=i.indent_id
        ) as unreadMessageCount,
        (select t.create_time from chat_content_tcwm t where t.orders_id=i.order_id order by t.create_time desc  limit 1) as chatCreateTime,
        (select t.content from chat_content_tcwm t where t.orders_id=i.order_id order by t.create_time desc  limit 1) as content,
        (select t.message_type from chat_content_tcwm t where t.orders_id=i.order_id order by t.create_time desc  limit 1) as messageType,
        i.create_time as createTime
        from tb_indent i
        left join goods_shop gs on i.shop_id = gs.shop_id
        left join tb_user u on u.user_id=i.user_id
        left join tb_user riderU on riderU.user_id=i.rider_user_id
        where i.indent_type=5
        <if test="userId!=null">
            and i.user_id = #{userId}
        </if>
        <if test="riderId!=null">
            and i.rider_user_id = #{riderId}
        </if>
        <if test="shopId!=null">
            and i.shop_id = #{shopId}
        </if>
        ) a order by ordertime desc
    </select>

    <select id="selectOrdersChatList" resultType="com.sqx.modules.gameChat.entity.ChatContentTcwm">
        select *, if(chatCreateTime>createTime, chatCreateTime, createTime) as ordertime from (
        select i.indent_id as indentId, i.order_id as ordersId, gs.shop_name as shopName, gs.shop_cover as shopCover,
        u.user_name as userName,u.avatar as userAvatar,riderU.nick_name as riderName,riderU.avatar as riderAvatar,
        u.user_id as userId,i.shop_id as shopId,
        (select t.create_time from chat_content_tcwm t where t.orders_id=i.order_id order by t.create_time desc  limit 1) as chatCreateTime,
        (select t.content from chat_content_tcwm t where t.orders_id=i.order_id order by t.create_time desc  limit 1) as content,
        (select t.message_type from chat_content_tcwm t where t.orders_id=i.order_id order by t.create_time desc  limit 1) as messageType,
        i.create_time as createTime
        from tb_indent i
        left join goods_shop gs on i.shop_id = gs.shop_id
        left join tb_user u on u.user_id=i.user_id
        left join tb_user riderU on riderU.user_id=i.rider_user_id
        where i.indent_type=5
        <if test="userName!=null and userName!=''">
            and (gs.shop_name like concat('%',#{userName},'%') or riderU.nick_name like concat('%',#{userName},'%') or u.user_name like concat('%',#{userName},'%'))
        </if>
        ) a order by ordertime desc
    </select>

    <select id="selectGameChatDetails" resultType="com.sqx.modules.gameChat.entity.ChatContentTcwm">
        select cct.*, tu.user_name as userName, tu.avatar as userAvatar, rtu.user_name as riderName,
               rtu.avatar as riderAvatar, gs.shop_name as shopName, gs.shop_cover as shopCover
        from chat_content_tcwm cct
                 left join tb_user tu on cct.user_id = tu.user_id
                 left join tb_user rtu on cct.rider_id = rtu.user_id
                 left join goods_shop gs on cct.shop_id = gs.shop_id
        where orders_id = #{ordersId} order by create_time desc
    </select>

    <select id="selectGameChatList" resultType="com.sqx.modules.gameChat.entity.ChatContentTcwm">
        select cct.*, tu.user_name as userName, tu.avatar as userAvatar, rtu.user_name as riderName,
               rtu.avatar as riderAvatar, gs.shop_name as shopName, gs.shop_cover as shopCover
        from chat_content_tcwm cct
                 left join tb_user tu on cct.user_id = tu.user_id
                 left join tb_user rtu on cct.rider_id = rtu.user_id
                 left join goods_shop gs on cct.shop_id = gs.shop_id
        where 1=1
        <if test="userName!=null and userName!=''">
            and (gs.shop_name like concat('%',#{userName},'%') or rtu.nick_name like concat('%',#{userName},'%') or tu.user_name like concat('%',#{userName},'%'))
        </if>
        <if test="content!=null and content!=''">
            and cct.content like concat('%',#{content},'%')
        </if>
        <if test="ordersId!=null">
            and orders_id = #{ordersId}
        </if>
        order by create_time desc
    </select>

    <select id="selectUnreadCount" resultType="int">
        select count(*) from chat_content_tcwm where orders_id = #{ordersId}
        <if test="i==1">
            and user_status = 0
        </if>
        <if test="i==2">
            and rider_status = 0
        </if>
        <if test="i==3">
            and shop_status = 0
        </if>
    </select>

    <select id="selectUnreadMessage" resultType="int">
        select count(*) from chat_content_tcwm where orders_id = #{ordersId}
        <if test="type==1">
            and user_status = 0
        </if>
        <if test="type==2">
            and rider_status = 0
        </if>
        <if test="type==3">
            and shop_status = 0
        </if>
    </select>

    <update id="updateRead">
        update chat_content_tcwm set
        <if test="type==1">
            user_status = 1
        </if>
        <if test="type==2">
            rider_status = 1
        </if>
        <if test="type==3">
            shop_status = 1
        </if>
        where orders_id = #{ordersId}
    </update>

    <select id="selectAllUnreadCount" resultType="int">
        select count(*) from chat_content_tcwm where 1 = 1
        <if test="userId!=null">
            and orders_id in (select order_id from tb_indent where user_id = #{userId}) and user_status = 0
        </if>
        <if test="riderUserId!=null">
            and orders_id in (select order_id from tb_indent where rider_user_id = #{riderUserId}) and rider_status = 0
        </if>
        <if test="shopId!=null">
            and orders_id in (select order_id from tb_indent where shop_id = #{shopId}) and shop_status = 0
        </if>
    </select>



    <update id="updateShopAllUnreadCount">
        update chat_content_tcwm set user_status=1
        where
        <if test="userId!=null">
            orders_id in (select order_id from tb_indent where user_id = #{userId}) and user_status = 0
        </if>
        <if test="riderUserId!=null">
            orders_id in (select order_id from tb_indent where rider_user_id = #{riderUserId}) and rider_status = 0
        </if>
        <if test="shopId!=null">
            orders_id in (select order_id from tb_indent where shop_id = #{shopId}) and shop_status = 0
        </if>
    </update>



</mapper>