<?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.screen.mapper.LivestockResourceScreenMapper">

    <sql id="aiSession">
        consult_type = 2
    </sql>

    <sql id="aiMessageJoin">
        inner join biz_consult_session s on s.id = m.session_id and s.consult_type = 2
    </sql>

    <sql id="aiMessageFilter">
        m.sender_role = 3
        and m.sender_user_id = 0
    </sql>

    <select id="selectYearOverviewMetrics" resultType="com.ruoyi.web.modules.screen.domain.dto.LivestockResourceOverviewDto">
        select
            (select count(distinct asker_user_id)
             from biz_consult_session
             where <include refid="aiSession"/>
               and create_time &gt;= #{yearStart}
               and create_time &lt;= #{yearEnd}) as totalUsers,
            (select count(*)
             from biz_consult_session
             where <include refid="aiSession"/>
               and create_time &gt;= #{yearStart}
               and create_time &lt;= #{yearEnd}) as totalSessions,
            (select count(*)
             from biz_consult_message m
             <include refid="aiMessageJoin"/>
             where <include refid="aiMessageFilter"/>
               and m.create_time &gt;= #{yearStart}
               and m.create_time &lt;= #{yearEnd}) as totalQuestionCount,
            (select avg(m.cost_time)
             from biz_consult_message m
             <include refid="aiMessageJoin"/>
             where <include refid="aiMessageFilter"/>
               and m.cost_time is not null
               and m.create_time &gt;= #{yearStart}
               and m.create_time &lt;= #{yearEnd}) as avgCostTimeMs
    </select>

    <select id="countTodayActiveUsers" resultType="long">
        select count(distinct asker_user_id)
        from biz_consult_session
        where <include refid="aiSession"/>
          and update_time &gt;= #{dayStart}
          and update_time &lt;= #{dayEnd}
    </select>

    <select id="countTodayNewUsers" resultType="long">
        select count(distinct asker_user_id)
        from biz_consult_session
        where <include refid="aiSession"/>
          and create_time &gt;= #{dayStart}
          and create_time &lt;= #{dayEnd}
          and update_time &gt;= #{dayStart}
          and update_time &lt;= #{dayEnd}
          and asker_user_id not in (
              select distinct asker_user_id
              from biz_consult_session
              where <include refid="aiSession"/>
                and create_time &lt; #{dayStart}
          )
    </select>

    <select id="countTodaySessions" resultType="long">
        select count(*)
        from biz_consult_session
        where <include refid="aiSession"/>
          and create_time &gt;= #{dayStart}
          and create_time &lt;= #{dayEnd}
    </select>

    <select id="countTodayQuestionCount" resultType="long">
        select count(*)
        from biz_consult_message m
        <include refid="aiMessageJoin"/>
        where <include refid="aiMessageFilter"/>
          and m.create_time &gt;= #{dayStart}
          and m.create_time &lt;= #{dayEnd}
    </select>

    <select id="selectDailySessionCounts" resultType="com.ruoyi.web.modules.screen.domain.dto.LivestockResourceDateCountDto">
        select date_format(create_time, '%Y-%m-%d') as bucketKey,
               count(*) as count
        from biz_consult_session
        where <include refid="aiSession"/>
          and create_time &gt;= #{rangeStart}
          and create_time &lt;= #{rangeEnd}
        group by date_format(create_time, '%Y-%m-%d')
    </select>

    <select id="selectDailyAiReplyCounts" resultType="com.ruoyi.web.modules.screen.domain.dto.LivestockResourceDateCountDto">
        select date_format(m.create_time, '%Y-%m-%d') as bucketKey,
               count(*) as count
        from biz_consult_message m
        <include refid="aiMessageJoin"/>
        where <include refid="aiMessageFilter"/>
          and m.create_time &gt;= #{rangeStart}
          and m.create_time &lt;= #{rangeEnd}
        group by date_format(m.create_time, '%Y-%m-%d')
    </select>

    <select id="selectUserStructure" resultType="com.ruoyi.web.modules.screen.domain.dto.LivestockResourceUserStructureDto">
        select coalesce(sum(case when datediff(last_use_time, first_use_time) &lt;= 180 then 1 else 0 end), 0) as newUserCount,
               coalesce(sum(case when datediff(last_use_time, first_use_time) &gt; 180 then 1 else 0 end), 0) as oldUserCount
        from biz_consult_user
    </select>

    <select id="countAskerMessagesInYear" resultType="long">
        select count(*)
        from biz_consult_message m
        <include refid="aiMessageJoin"/>
        where <include refid="aiMessageFilter"/>
          and m.create_time &gt;= #{yearStart}
          and m.create_time &lt;= #{yearEnd}
    </select>

    <select id="selectAskerCategoryCounts" resultType="com.ruoyi.web.modules.screen.domain.dto.LivestockResourceCategoryCountDto">
        select category_code as categoryCode,
               count(*) as count,
               max(last_time) as lastEventTime
        from (
            select m.id,
                   case
                       when trim(coalesce((
                           select m2.ai_category
                           from biz_consult_message m2
                           where m2.session_id = m.session_id
                             and m2.sender_role = 1
                             and m2.send_time &lt;= m.send_time
                           order by m2.send_time desc
                           limit 1
                       ), '')) is null
                           or trim(coalesce((
                           select m2.ai_category
                           from biz_consult_message m2
                           where m2.session_id = m.session_id
                             and m2.sender_role = 1
                             and m2.send_time &lt;= m.send_time
                           order by m2.send_time desc
                           limit 1
                       ), '')) = '' then 'UNCAT'
                       else trim(coalesce((
                           select m2.ai_category
                           from biz_consult_message m2
                           where m2.session_id = m.session_id
                             and m2.sender_role = 1
                             and m2.send_time &lt;= m.send_time
                           order by m2.send_time desc
                           limit 1
                       ), ''))
                   end as category_code,
                   m.create_time as last_time
            from biz_consult_message m
            <include refid="aiMessageJoin"/>
            where <include refid="aiMessageFilter"/>
              and m.create_time &gt;= #{yearStart}
              and m.create_time &lt;= #{yearEnd}
        ) t
        group by category_code
    </select>

    <select id="selectHourlySessionCounts" resultType="com.ruoyi.web.modules.screen.domain.dto.LivestockResourceDateCountDto">
        select date_format(create_time, '%Y%m%d%H') as bucketKey,
               count(*) as count
        from biz_consult_session
        where <include refid="aiSession"/>
          and create_time &gt;= #{windowStart}
          and create_time &lt;= #{windowEnd}
        group by date_format(create_time, '%Y%m%d%H')
    </select>

    <select id="selectHourlyAiReplyCounts" resultType="com.ruoyi.web.modules.screen.domain.dto.LivestockResourceDateCountDto">
        select date_format(m.create_time, '%Y%m%d%H') as bucketKey,
               count(*) as count
        from biz_consult_message m
        <include refid="aiMessageJoin"/>
        where <include refid="aiMessageFilter"/>
          and m.create_time &gt;= #{windowStart}
          and m.create_time &lt;= #{windowEnd}
        group by date_format(m.create_time, '%Y%m%d%H')
    </select>

    <select id="countYearSessions" resultType="long">
        select count(*)
        from biz_consult_session
        where <include refid="aiSession"/>
          and create_time &gt;= #{yearStart}
          and create_time &lt;= #{yearEnd}
    </select>

    <select id="selectSessionReplyBuckets" resultType="com.ruoyi.web.modules.screen.domain.dto.LivestockResourceBandCountDto">
        select band_code as bandCode,
               count(*) as sessionCount
        from (
            select s.id,
                   case
                       when r.cnt between 1 and 2 then 'LIGHT'
                       when r.cnt between 3 and 4 then 'NORMAL'
                       when r.cnt between 5 and 10 then 'ACTIVE'
                       else 'DEEP'
                   end as band_code
            from biz_consult_session s
            inner join (
                select m.session_id, count(*) as cnt
                from biz_consult_message m
                inner join biz_consult_session s2 on s2.id = m.session_id and s2.consult_type = 2
                where m.sender_role = 3
                  and m.sender_user_id = 0
                  and m.create_time &gt;= #{yearStart}
                  and m.create_time &lt;= #{yearEnd}
                group by m.session_id
            ) r on r.session_id = s.id
            where s.consult_type = 2
              and s.create_time &gt;= #{yearStart}
              and s.create_time &lt;= #{yearEnd}
        ) t
        group by band_code
    </select>

    <select id="countAiRepliesOk" resultType="long">
        select count(*)
        from biz_consult_message m
        <include refid="aiMessageJoin"/>
        where <include refid="aiMessageFilter"/>
          and m.cost_time is not null
          and m.create_time &gt;= #{yearStart}
          and m.create_time &lt;= #{yearEnd}
    </select>

    <select id="countAiRepliesFail" resultType="long">
        select count(*)
        from biz_consult_message m
        <include refid="aiMessageJoin"/>
        where <include refid="aiMessageFilter"/>
          and m.cost_time is null
          and m.create_time &gt;= #{yearStart}
          and m.create_time &lt;= #{yearEnd}
    </select>

    <select id="selectTextAskerContents" resultType="string">
        select m.content
        from biz_consult_message m
        <include refid="aiMessageJoin"/>
        where m.sender_role = 1
          and m.sender_user_id &lt;&gt; 0
          and m.msg_type = 1
          and m.content is not null
          and trim(m.content) != ''
          and m.send_time &gt;= #{yearStart}
          and m.send_time &lt;= #{yearEnd}
    </select>

    <select id="selectDistinctSessionYears" resultType="int">
        select distinct year(create_time)
        from biz_consult_session
        where <include refid="aiSession"/>
          and create_time is not null
        order by year(create_time) desc
    </select>
</mapper>
