<?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.ruoyi.web.modules.diagnosis.mapper.BizConsultSessionMapper">

    <resultMap type="com.ruoyi.web.modules.diagnosis.domain.BizConsultSession" id="BizConsultSessionResult">
        <id     property="id"                  column="id"/>
        <result property="consultType"         column="consult_type"/>
        <result property="askerUserId"         column="asker_user_id"/>
        <result property="askerName"           column="asker_name"/>
        <result property="receiverUserId"      column="receiver_user_id"/>
        <result property="receiverProviderId"  column="receiver_provider_id"/>
        <result property="receiverName"        column="receiver_name"/>
        <result property="sessionTitle"        column="session_title"/>
        <result property="realSessionId"       column="real_session_id"/>
        <result property="lastMessageTime"     column="last_message_time"/>
        <result property="lastMessagePreview"  column="last_message_preview"/>
        <result property="vetVisible"          column="vet_visible"/>
        <result property="createTime"          column="create_time"/>
        <result property="updateTime"          column="update_time"/>
        <result property="askerAvatar"         column="asker_avatar"/>
    </resultMap>

    <sql id="selectVo">
        select s.id, s.consult_type, s.asker_user_id, s.asker_name, s.receiver_user_id, s.receiver_provider_id,
               s.receiver_name, s.session_title, s.real_session_id, s.last_message_time, s.last_message_preview,
               s.vet_visible, s.create_time, s.update_time, u.avatar as asker_avatar
        from biz_consult_session s
        left join sys_user u on u.user_id = s.asker_user_id and u.del_flag = '0'
    </sql>

    <select id="selectVetSessionList" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizConsultSession"
            resultMap="BizConsultSessionResult">
        <include refid="selectVo"/>
        <where>
            s.consult_type = #{consultType}
            and s.receiver_provider_id = #{receiverProviderId}
            and s.vet_visible = #{vetVisible}
            <if test="searchMode == null or searchMode == false">
                and COALESCE(s.last_message_time, s.create_time) &gt;= #{threeMonthsAgo}
            </if>
            <if test="askerNameFilter != null and askerNameFilter != ''">
                and s.asker_name like concat('%', #{askerNameFilter}, '%')
            </if>
            <if test="contentKeyword != null and contentKeyword != ''">
                and (
                    s.last_message_preview like concat('%', #{contentKeyword}, '%')
                    or s.session_title like concat('%', #{contentKeyword}, '%')
                    or exists (
                        select 1 from biz_consult_message m
                        where m.session_id = s.id
                          and m.msg_type = 1
                          and m.content like concat('%', #{contentKeyword}, '%')
                    )
                )
            </if>
        </where>
        order by COALESCE(s.last_message_time, s.create_time) desc, s.id desc
    </select>

    <select id="selectAiSessionList" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizConsultSession"
            resultMap="BizConsultSessionResult">
        <include refid="selectVo"/>
        <where>
            s.consult_type = #{consultType}
            and s.asker_user_id = #{askerUserId}
            and s.vet_visible = #{vetVisible}
            <if test="searchMode == null or searchMode == false">
                and COALESCE(s.last_message_time, s.create_time) &gt;= #{threeMonthsAgo}
            </if>
            <if test="contentKeyword != null and contentKeyword != ''">
                and (
                    s.last_message_preview like concat('%', #{contentKeyword}, '%')
                    or s.session_title like concat('%', #{contentKeyword}, '%')
                    or exists (
                        select 1 from biz_consult_message m
                        where m.session_id = s.id
                          and m.msg_type = 1
                          and m.content like concat('%', #{contentKeyword}, '%')
                    )
                )
            </if>
        </where>
        order by COALESCE(s.last_message_time, s.create_time) desc, s.id desc
    </select>

    <select id="selectAskerSessionList" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizConsultSession"
            resultMap="BizConsultSessionResult">
        <include refid="selectVo"/>
        <where>
            s.consult_type = 1
            and s.asker_user_id = #{askerUserId}
        </where>
        order by s.last_message_time desc, s.id desc
    </select>

    <select id="selectVetSessionByIdAndProvider" resultMap="BizConsultSessionResult">
        <include refid="selectVo"/>
        where s.id = #{id}
          and s.consult_type = 1
          and s.receiver_provider_id = #{receiverProviderId}
          and s.vet_visible = 1
    </select>

    <select id="selectVetSessionByAskerAndProvider" resultMap="BizConsultSessionResult">
        <include refid="selectVo"/>
        where s.consult_type = 1
          and s.asker_user_id = #{askerUserId}
          and s.receiver_provider_id = #{receiverProviderId}
        limit 1
    </select>

    <select id="selectSessionByIdAndAsker" resultMap="BizConsultSessionResult">
        <include refid="selectVo"/>
        where s.id = #{id}
          and s.consult_type = 1
          and s.asker_user_id = #{askerUserId}
    </select>

    <select id="selectAiSessionById" resultMap="BizConsultSessionResult">
        <include refid="selectVo"/>
        where s.id = #{id}
          and s.consult_type = 2
    </select>

    <select id="selectAiSessionByIdAndAsker" resultMap="BizConsultSessionResult">
        <include refid="selectVo"/>
        where s.id = #{id}
          and s.consult_type = 2
          and s.asker_user_id = #{askerUserId}
          and s.vet_visible = 1
    </select>

    <insert id="insertBizConsultSession" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizConsultSession"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_consult_session (
            consult_type, asker_user_id, asker_name, receiver_user_id, receiver_provider_id, receiver_name,
            session_title, last_message_time, last_message_preview, vet_visible, create_time, update_time
        ) values (
            #{consultType}, #{askerUserId}, #{askerName}, #{receiverUserId}, #{receiverProviderId}, #{receiverName},
            #{sessionTitle}, #{lastMessageTime}, #{lastMessagePreview}, #{vetVisible}, #{createTime}, #{updateTime}
        )
    </insert>

    <update id="updateSessionLastMessage" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizConsultSession">
        update biz_consult_session
        set last_message_time = #{lastMessageTime},
            last_message_preview = #{lastMessagePreview},
            update_time = sysdate()
            <if test="sessionTitle != null and sessionTitle != ''">
                , session_title = #{sessionTitle}
            </if>
        where id = #{id}
    </update>

    <update id="updateVetVisible">
        update biz_consult_session
        set vet_visible = #{vetVisible},
            update_time = sysdate()
        where id = #{id}
          and asker_user_id = #{askerUserId}
          and consult_type = 1
    </update>

    <update id="updateSessionVisible">
        update biz_consult_session
        set vet_visible = #{vetVisible},
            update_time = sysdate()
        where id = #{id}
          and asker_user_id = #{askerUserId}
          and consult_type = #{consultType}
    </update>

    <update id="updateRealSessionId">
        update biz_consult_session
        set real_session_id = #{realSessionId},
            update_time = sysdate()
        where id = #{id}
    </update>
</mapper>
