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

    <resultMap type="com.ruoyi.web.modules.diagnosis.domain.BizConsultMessage" id="BizConsultMessageResult">
        <id     property="id"            column="id"/>
        <result property="sessionId"     column="session_id"/>
        <result property="senderRole"    column="sender_role"/>
        <result property="senderUserId"  column="sender_user_id"/>
        <result property="senderName"    column="sender_name"/>
        <result property="msgType"       column="msg_type"/>
        <result property="content"       column="content"/>
        <result property="mediaDuration" column="media_duration"/>
        <result property="aiCategory"    column="ai_category"/>
        <result property="costTime"      column="cost_time"/>
        <result property="askerIp"       column="asker_ip"/>
        <result property="sendTime"      column="send_time"/>
        <result property="createTime"    column="create_time"/>
    </resultMap>

    <select id="selectMessagesBeforeId" resultMap="BizConsultMessageResult">
        select id, session_id, sender_role, sender_user_id, sender_name, msg_type, content, media_duration,
               ai_category, cost_time, asker_ip, send_time, create_time
        from biz_consult_message
        where session_id = #{sessionId}
        <if test="beforeId != null">
            and id &lt; #{beforeId}
        </if>
        order by send_time desc, id desc
        limit #{pageSize}
    </select>

    <insert id="insertBizConsultMessage" parameterType="com.ruoyi.web.modules.diagnosis.domain.BizConsultMessage"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_consult_message (
            session_id, sender_role, sender_user_id, sender_name, msg_type, content, media_duration, ai_category,
            cost_time, asker_ip, send_time, create_time
        ) values (
            #{sessionId}, #{senderRole}, #{senderUserId}, #{senderName}, #{msgType}, #{content}, #{mediaDuration},
            #{aiCategory}, #{costTime}, #{askerIp}, #{sendTime}, #{createTime}
        )
    </insert>

    <select id="selectAiYakDiseaseAskerIpCounts"
            resultType="com.ruoyi.web.modules.diagnosis.domain.dto.ConsultAskerIpCountDto">
        select m.asker_ip as askerIp, count(*) as messageCount
        from biz_consult_message m
        inner join biz_consult_session s on m.session_id = s.id
        where s.consult_type = #{consultType}
          and m.sender_role &lt;&gt; 3
          and m.sender_user_id &lt;&gt; 0
          and m.ai_category = #{aiCategory}
          and m.asker_ip is not null
          and m.asker_ip &lt;&gt; ''
          and m.send_time &gt;= #{monthStart}
          and m.send_time &lt;= #{monthEnd}
        group by m.asker_ip
    </select>
</mapper>
