<?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.chat.dao.ChatConversationDao">


    <select id="selectChatConversationPage" resultType="Map">
        select * from (
        select
        c.chat_conversation_id as chatConversationId,
        c.update_time as updateTime,
        <if test="userType == 1">
            (SELECT company_name FROM  company co LEFT JOIN  company_user cu ON co.company_id=cu.company_id WHERE c.focused_user_id= cu.user_id AND cu.is_use = 1) as companyName,
            (select user_name from tb_user u where u.user_id =  c.focused_user_id) as userName,
            (select hr.hr_img FROM hr WHERE hr.user_id =  c.focused_user_id) as avatar,
        </if>
        <if test="userType == 2">
            (select user_name from tb_user u where c.user_id = u.user_id) as userName,
            (select avatar from tb_user u where c.user_id = u.user_id) as avatar,
        </if>
             (select rule_classify_name from post_push p where p.post_push_id=c.post_push_id) as stationName,
        c.type,
        c.post_push_id as postPushId,
        c.resumes_id as resumesId,
        c.is_send_wx as isSendWx,
        c.is_send_phone as isSendPhone,
        c.user_id as userId,
        c.focused_user_id as focusedUserId,
        (select content from chat_content cc where cc.chat_conversation_id=c.chat_conversation_id order by
        cc.create_time desc limit 1) as content,
        (select create_time from chat_content cc where cc.chat_conversation_id=c.chat_conversation_id order by
        cc.create_time desc limit 1) as messageTime,
        (select message_type from chat_content cc where cc.chat_conversation_id=c.chat_conversation_id order by
        cc.create_time desc limit 1) as messageType,
        (select count(*) from chat_content cc where cc.chat_conversation_id=c.chat_conversation_id and
        user_id!=#{userId} and status=0) as contentCount
        from chat_conversation c
        where 1=1
        <if test="userId!=null">
            <if test="userType == 1">
                and c.user_id=#{userId}
            </if>
            <if test="userType == 2">
                and c.focused_user_id=#{userId}
            </if>
        </if>

        ) a
        order by messageTime desc
    </select>
    <select id="adminSelectChatConversationPage" resultType="java.util.Map">
        select * from (
        select c.chat_conversation_id as chatConversationId,c.update_time as updateTime,
        u1.user_id as userId,u1.user_name as userName,u1.avatar,u2.user_id as byUserId,u2.user_name as byUserName,u2.avatar as byAvatar,
        (select content from chat_content cc where cc.chat_conversation_id=c.chat_conversation_id order by cc.create_time desc limit 1) as content,
        (select create_time from chat_content cc where cc.chat_conversation_id=c.chat_conversation_id order by cc.create_time desc limit 1) as messageTime,
        (select message_type from chat_content cc where cc.chat_conversation_id=c.chat_conversation_id order by cc.create_time desc limit 1) as messageType,
        (select count(*) from chat_content cc where cc.chat_conversation_id=c.chat_conversation_id and user_id!=#{userId} and status=0) as contentCount
        from chat_conversation c
        left join tb_user u1 on u1.user_id=c.user_id
        left join tb_user u2 on u2.user_id=c.focused_user_id
        where 1=1
        <if test="userId!=null">
            and (c.user_id=#{userId} or c.focused_user_id=#{userId})
        </if>
        <if test="nickName!=null and nickName!=''">
            and (u1.user_name like CONCAT('%',#{nickName},'%') or u2.user_name like CONCAT('%',#{nickName},'%') )
        </if>
        ) a
        order by messageTime desc

    </select>

    <insert id="insertChatConversation" useGeneratedKeys="true" keyProperty="chatConversationId"
            parameterType="com.sqx.modules.chat.entity.ChatConversation">
        INSERT INTO chat_conversation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != userId and '' != userId">
                user_id,
            </if>
            <if test="null != focusedUserId and '' != focusedUserId">
                focused_user_id,
            </if>
            <if test="null != status and '' != status">
                status,
            </if>
            <if test="null != createTime and '' != createTime">
                create_time,
            </if>
            <if test="null != updateTime and '' != updateTime">
                update_time,
            </if>
            <if test="null != remark and '' != remark">
                remark
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != userId and '' != userId">
                #{userId},
            </if>
            <if test="null != focusedUserId and '' != focusedUserId">
                #{focusedUserId},
            </if>
            <if test="null != status and '' != status">
                #{status},
            </if>
            <if test="null != createTime and '' != createTime">
                #{createTime},
            </if>
            <if test="null != updateTime and '' != updateTime">
                #{updateTime},
            </if>
            <if test="null != remark and '' != remark">
                #{remark}
            </if>
        </trim>
    </insert>


</mapper>