<?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.excel.wecom.mapper.CustomerStatisticsDataV2Mapper">

    <resultMap id="BaseResultMap" type="com.ruoyi.excel.wecom.domain.CustomerStatisticsDataV2">
        <id column="id" property="id"/>
        <result column="corp_id" property="corpId"/>
        <result column="cur_date" property="curDate"/>
        <result column="group_name" property="groupName"/>
        <result column="tag_name" property="tagName"/>
        <result column="tag_group_id" property="tagGroupId"/>
        <result column="tag_id" property="tagId"/>
        <result column="data_level" property="dataLevel"/>
        <result column="parent_id" property="parentId"/>
        <result column="total_cost" property="totalCost"/>
        <result column="single_cost" property="singleCost"/>
        <result column="order_cost" property="orderCost"/>
        <result column="cost_input_type" property="costInputType"/>
        <result column="order_count" property="orderCount"/>
        <result column="customer_count" property="customerCount"/>
        <result column="timely_order_count" property="timelyOrderCount"/>
        <result column="non_timely_order_count" property="nonTimelyOrderCount"/>
        <result column="conversion_rate" property="conversionRate"/>
        <result column="timely_rate" property="timelyRate"/>
        <result column="non_timely_rate" property="nonTimelyRate"/>
        <result column="customer_attr_count" property="customerAttrCount"/>
        <result column="parent_count" property="parentCount"/>
        <result column="student_count" property="studentCount"/>
        <result column="teacher_count" property="teacherCount"/>
        <result column="unknown_attr_count" property="unknownAttrCount"/>
        <result column="parent_rate" property="parentRate"/>
        <result column="student_rate" property="studentRate"/>
        <result column="teacher_rate" property="teacherRate"/>
        <result column="unknown_rate" property="unknownRate"/>
        <result column="parent_order_count" property="parentOrderCount"/>
        <result column="student_order_count" property="studentOrderCount"/>
        <result column="teacher_order_count" property="teacherOrderCount"/>
        <result column="unknown_order_count" property="unknownOrderCount"/>
        <result column="parent_daily_count" property="parentDailyCount"/>
        <result column="student_daily_count" property="studentDailyCount"/>
        <result column="teacher_daily_count" property="teacherDailyCount"/>
        <result column="unknown_daily_count" property="unknownDailyCount"/>
        <result column="parent_daily_order_count" property="parentDailyOrderCount"/>
        <result column="student_daily_order_count" property="studentDailyOrderCount"/>
        <result column="teacher_daily_order_count" property="teacherDailyOrderCount"/>
        <result column="unknown_daily_order_count" property="unknownDailyOrderCount"/>
        <result column="parent_order_rate" property="parentOrderRate"/>
        <result column="student_order_rate" property="studentOrderRate"/>
        <result column="teacher_order_rate" property="teacherOrderRate"/>
        <result column="unknown_order_rate" property="unknownOrderRate"/>
        <result column="intention_count" property="intentionCount"/>
        <result column="active_quote_count" property="activeQuoteCount"/>
        <result column="passive_quote_count" property="passiveQuoteCount"/>
        <result column="no_quote_count" property="noQuoteCount"/>
        <result column="deleted_quote_count" property="deletedQuoteCount"/>
        <result column="active_quote_rate" property="activeQuoteRate"/>
        <result column="passive_quote_rate" property="passiveQuoteRate"/>
        <result column="no_quote_rate" property="noQuoteRate"/>
        <result column="deleted_quote_rate" property="deletedQuoteRate"/>
        <result column="grade_count" property="gradeCount"/>
        <result column="primary_count" property="primaryCount"/>
        <result column="middle_count" property="middleCount"/>
        <result column="high_count" property="highCount"/>
        <result column="primary_rate" property="primaryRate"/>
        <result column="middle_rate" property="middleRate"/>
        <result column="high_rate" property="highRate"/>
        <result column="sort_no" property="sortNo"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, corp_id, cur_date, group_name, tag_name, tag_group_id, tag_id, data_level, parent_id,
        total_cost, single_cost, order_cost, cost_input_type,
        order_count, customer_count, timely_order_count, non_timely_order_count,
        conversion_rate, timely_rate, non_timely_rate,
        customer_attr_count, parent_count, student_count, teacher_count, unknown_attr_count,
        parent_rate, student_rate, teacher_rate, unknown_rate,
        parent_order_count, student_order_count, teacher_order_count, unknown_order_count,
        parent_daily_count, student_daily_count, teacher_daily_count, unknown_daily_count,
        parent_daily_order_count, student_daily_order_count, teacher_daily_order_count, unknown_daily_order_count,
        parent_order_rate, student_order_rate, teacher_order_rate, unknown_order_rate,
        intention_count, active_quote_count, passive_quote_count, no_quote_count, deleted_quote_count,
        active_quote_rate, passive_quote_rate, no_quote_rate, deleted_quote_rate,
        grade_count, primary_count, middle_count, high_count,
        primary_rate, middle_rate, high_rate,
        sort_no, create_time, update_time
    </sql>

    <!-- 根据企业ID、日期、组名、标签名查询数据 -->
    <select id="selectByCorpDateGroupTag" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
          AND cur_date = #{curDate}
          AND group_name = #{groupName}
          AND (
              (tag_name = #{tagName} AND #{tagName} IS NOT NULL AND #{tagName} != '')
              OR (tag_name IS NULL AND (#{tagName} IS NULL OR #{tagName} = ''))
          )
    </select>

    <!-- 查询组级数据列表 -->
    <select id="selectGroupLevelList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
          AND data_level = 1
        <if test="startDate != null">
            AND cur_date &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND cur_date &lt;= #{endDate}
        </if>
        ORDER BY cur_date DESC, sort_no, group_name
    </select>

    <!-- 查询标签级数据列表 -->
    <select id="selectTagLevelList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
          AND data_level = 2
        <if test="startDate != null">
            AND cur_date &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND cur_date &lt;= #{endDate}
        </if>
        <if test="groupName != null and groupName != ''">
            AND group_name = #{groupName}
        </if>
        ORDER BY cur_date DESC, group_name, tag_name
    </select>

    <!-- 根据企业ID、日期、组名查询组级数据 -->
    <select id="selectGroupLevelByCorpDateGroup" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
          AND cur_date = #{curDate}
          AND group_name = #{groupName}
          AND data_level = 1
    </select>

    <!-- 根据企业ID、日期、组名查询标签级数据列表 -->
    <select id="selectTagLevelByCorpDateGroup" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
          AND cur_date = #{curDate}
          AND group_name = #{groupName}
          AND data_level = 2
        ORDER BY tag_name
    </select>

    <!-- 批量插入数据 -->
    <insert id="batchInsert">
        INSERT INTO customer_statistics_data_v2 (
            corp_id, cur_date, group_name, tag_name, tag_group_id, tag_id, data_level, parent_id,
            total_cost, single_cost, order_cost, cost_input_type,
            order_count, customer_count, timely_order_count, non_timely_order_count,
            conversion_rate, timely_rate, non_timely_rate,
            customer_attr_count, parent_count, student_count, teacher_count, unknown_attr_count,
            parent_rate, student_rate, teacher_rate, unknown_rate,
            parent_order_count, student_order_count, teacher_order_count, unknown_order_count,
            parent_daily_count, student_daily_count, teacher_daily_count, unknown_daily_count,
            parent_daily_order_count, student_daily_order_count, teacher_daily_order_count, unknown_daily_order_count,
            parent_order_rate, student_order_rate, teacher_order_rate, unknown_order_rate,
            intention_count, active_quote_count, passive_quote_count, no_quote_count, deleted_quote_count,
            active_quote_rate, passive_quote_rate, no_quote_rate, deleted_quote_rate,
            grade_count, primary_count, middle_count, high_count,
            primary_rate, middle_rate, high_rate,
            sort_no
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.corpId}, #{item.curDate}, #{item.groupName}, #{item.tagName}, #{item.tagGroupId}, #{item.tagId},
                #{item.dataLevel}, #{item.parentId},
                #{item.totalCost}, #{item.singleCost}, #{item.orderCost}, #{item.costInputType},
                #{item.orderCount}, #{item.customerCount}, #{item.timelyOrderCount}, #{item.nonTimelyOrderCount},
                #{item.conversionRate}, #{item.timelyRate}, #{item.nonTimelyRate},
                #{item.customerAttrCount}, #{item.parentCount}, #{item.studentCount}, #{item.teacherCount}, #{item.unknownAttrCount},
                #{item.parentRate}, #{item.studentRate}, #{item.teacherRate}, #{item.unknownRate},
                #{item.parentOrderCount}, #{item.studentOrderCount}, #{item.teacherOrderCount}, #{item.unknownOrderCount},
                #{item.parentDailyCount}, #{item.studentDailyCount}, #{item.teacherDailyCount}, #{item.unknownDailyCount},
                #{item.parentDailyOrderCount}, #{item.studentDailyOrderCount}, #{item.teacherDailyOrderCount}, #{item.unknownDailyOrderCount},
                #{item.parentOrderRate}, #{item.studentOrderRate}, #{item.teacherOrderRate}, #{item.unknownOrderRate},
                #{item.intentionCount}, #{item.activeQuoteCount}, #{item.passiveQuoteCount}, #{item.noQuoteCount}, #{item.deletedQuoteCount},
                #{item.activeQuoteRate}, #{item.passiveQuoteRate}, #{item.noQuoteRate}, #{item.deletedQuoteRate},
                #{item.gradeCount}, #{item.primaryCount}, #{item.middleCount}, #{item.highCount},
                #{item.primaryRate}, #{item.middleRate}, #{item.highRate},
                #{item.sortNo}
            )
        </foreach>
    </insert>

    <!-- 删除指定日期范围的数据 -->
    <delete id="deleteByDateRange">
        DELETE FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
        <if test="startDate != null">
            AND cur_date &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND cur_date &lt;= #{endDate}
        </if>
    </delete>

    <!-- 查询树状结构数据（组+标签） -->
    <select id="selectTreeData" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
        <if test="startDate != null">
            AND cur_date &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND cur_date &lt;= #{endDate}
        </if>
        ORDER BY data_level, group_name, tag_name
    </select>

    <!-- 根据筛选条件查询数据列表（支持按组、标签筛选） -->
    <select id="selectListByFilter" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
        <if test="startDate != null">
            AND cur_date &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND cur_date &lt;= #{endDate}
        </if>
        <if test="groupName != null and groupName != ''">
            AND group_name = #{groupName}
        </if>
        <if test="tagName != null and tagName != ''">
            AND tag_name = #{tagName}
        </if>
        ORDER BY cur_date DESC, data_level ASC, group_name ASC, sort_no ASC
    </select>

    <!-- 按日期范围聚合查询（支持按组、标签筛选） -->
    <!-- 注意：比率指标需要在Java层重新计算，SQL只聚合数量指标 -->
    <select id="selectAggregatedByDateRange" resultMap="BaseResultMap">
        SELECT
            NULL as id,
            #{corpId} as corp_id,
            MAX(cur_date) as cur_date,
            group_name,
            tag_name,
            NULL as parent_id,
            MIN(sort_no) as sort_no,
            data_level,
            SUM(order_count) as order_count,
            SUM(customer_count) as customer_count,
            SUM(customer_attr_count) as customer_attr_count,
            SUM(parent_count) as parent_count,
            SUM(student_count) as student_count,
            SUM(teacher_count) as teacher_count,
            SUM(unknown_attr_count) as unknown_attr_count,
            SUM(timely_order_count) as timely_order_count,
            SUM(non_timely_order_count) as non_timely_order_count,
            NULL as conversion_rate,
            NULL as timely_rate,
            NULL as non_timely_rate,
            NULL as parent_rate,
            NULL as student_rate,
            NULL as teacher_rate,
            NULL as unknown_rate,
            SUM(total_cost) as total_cost,
            NULL as single_cost,
            NULL as order_cost,
            SUM(intention_count) as intention_count,
            SUM(active_quote_count) as active_quote_count,
            SUM(passive_quote_count) as passive_quote_count,
            SUM(no_quote_count) as no_quote_count,
            SUM(deleted_quote_count) as deleted_quote_count,
            NULL as active_quote_rate,
            NULL as passive_quote_rate,
            NULL as no_quote_rate,
            NULL as deleted_quote_rate,
            SUM(grade_count) as grade_count,
            SUM(primary_count) as primary_count,
            SUM(middle_count) as middle_count,
            SUM(high_count) as high_count,
            NULL as primary_rate,
            NULL as middle_rate,
            NULL as high_rate,
            SUM(parent_order_count) as parent_order_count,
            SUM(student_order_count) as student_order_count,
            SUM(teacher_order_count) as teacher_order_count,
            SUM(unknown_order_count) as unknown_order_count,
            SUM(parent_daily_count) as parent_daily_count,
            SUM(student_daily_count) as student_daily_count,
            SUM(teacher_daily_count) as teacher_daily_count,
            SUM(unknown_daily_count) as unknown_daily_count,
            SUM(parent_daily_order_count) as parent_daily_order_count,
            SUM(student_daily_order_count) as student_daily_order_count,
            SUM(teacher_daily_order_count) as teacher_daily_order_count,
            SUM(unknown_daily_order_count) as unknown_daily_order_count,
            NULL as parent_order_rate,
            NULL as student_order_rate,
            NULL as teacher_order_rate,
            NULL as unknown_order_rate
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
        <choose>
            <when test="tagName != null and tagName != ''">
                AND data_level = 2
            </when>
            <otherwise>
                AND data_level = 1
            </otherwise>
        </choose>
        <if test="startDate != null">
            AND cur_date &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND cur_date &lt;= #{endDate}
        </if>
        <if test="groupName != null and groupName != ''">
            AND group_name = #{groupName}
        </if>
        <if test="tagName != null and tagName != ''">
            AND tag_name = #{tagName}
        </if>
        GROUP BY group_name, tag_name, data_level
        ORDER BY group_name ASC, tag_name ASC, data_level ASC, MIN(sort_no) ASC
    </select>

    <!-- 查询所有数据并聚合（支持按组、标签筛选） -->
    <!-- 注意：比率指标需要在Java层重新计算，SQL只聚合数量指标 -->
    <select id="selectAllAggregated" resultMap="BaseResultMap">
        SELECT
            NULL as id,
            #{corpId} as corp_id,
            MAX(cur_date) as cur_date,
            group_name,
            tag_name,
            NULL as parent_id,
            MIN(sort_no) as sort_no,
            data_level,
            SUM(order_count) as order_count,
            SUM(customer_count) as customer_count,
            SUM(customer_attr_count) as customer_attr_count,
            SUM(parent_count) as parent_count,
            SUM(student_count) as student_count,
            SUM(teacher_count) as teacher_count,
            SUM(unknown_attr_count) as unknown_attr_count,
            SUM(timely_order_count) as timely_order_count,
            SUM(non_timely_order_count) as non_timely_order_count,
            NULL as conversion_rate,
            NULL as timely_rate,
            NULL as non_timely_rate,
            NULL as parent_rate,
            NULL as student_rate,
            NULL as teacher_rate,
            NULL as unknown_rate,
            SUM(total_cost) as total_cost,
            NULL as single_cost,
            NULL as order_cost,
            SUM(intention_count) as intention_count,
            SUM(active_quote_count) as active_quote_count,
            SUM(passive_quote_count) as passive_quote_count,
            SUM(no_quote_count) as no_quote_count,
            SUM(deleted_quote_count) as deleted_quote_count,
            NULL as active_quote_rate,
            NULL as passive_quote_rate,
            NULL as no_quote_rate,
            NULL as deleted_quote_rate,
            SUM(grade_count) as grade_count,
            SUM(primary_count) as primary_count,
            SUM(middle_count) as middle_count,
            SUM(high_count) as high_count,
            NULL as primary_rate,
            NULL as middle_rate,
            NULL as high_rate,
            SUM(parent_order_count) as parent_order_count,
            SUM(student_order_count) as student_order_count,
            SUM(teacher_order_count) as teacher_order_count,
            SUM(unknown_order_count) as unknown_order_count,
            SUM(parent_daily_count) as parent_daily_count,
            SUM(student_daily_count) as student_daily_count,
            SUM(teacher_daily_count) as teacher_daily_count,
            SUM(unknown_daily_count) as unknown_daily_count,
            SUM(parent_daily_order_count) as parent_daily_order_count,
            SUM(student_daily_order_count) as student_daily_order_count,
            SUM(teacher_daily_order_count) as teacher_daily_order_count,
            SUM(unknown_daily_order_count) as unknown_daily_order_count,
            NULL as parent_order_rate,
            NULL as student_order_rate,
            NULL as teacher_order_rate,
            NULL as unknown_order_rate
        FROM customer_statistics_data_v2
        WHERE corp_id = #{corpId}
        <choose>
            <when test="tagName != null and tagName != ''">
                AND data_level = 2
            </when>
            <otherwise>
                AND data_level = 1
            </otherwise>
        </choose>
        <if test="groupName != null and groupName != ''">
            AND group_name = #{groupName}
        </if>
        <if test="tagName != null and tagName != ''">
            AND tag_name = #{tagName}
        </if>
        GROUP BY group_name, tag_name, data_level
        ORDER BY group_name ASC, tag_name ASC, data_level ASC, MIN(sort_no) ASC
    </select>

</mapper>
