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

    <!-- 根据标签ID查询标签 -->
    <select id="selectByDate" resultType="com.ruoyi.excel.wecom.vo.CustomerStatisticsVO">
        select *
        from customer_statistics_data
        where tag_id = #{tagId} and corp_id = #{corpId}
    </select>

    <!-- 查询客户统计数据列表 -->
    <select id="selectCustomerStatisticsDataList" resultType="com.ruoyi.excel.wecom.domain.CustomerStatisticsData">
        SELECT *
        FROM customer_statistics_data
        <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="indicatorName != null and indicatorName != ''">
                AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%')
            </if>
        </where>
        ORDER BY cur_date DESC,sort_no
    </select>

    <!-- 查询客户统计数据VO列表(用于导出) -->
    <select id="selectCustomerStatisticsDataVOList" resultType="com.ruoyi.excel.wecom.vo.CustomerStatisticsDataVO">
        SELECT
        *
        FROM customer_statistics_data
        <where>
            corp_id = #{corpId} and hidden_flag = false
            <if test="startDate != null">
                AND cur_date &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND cur_date &lt;= #{endDate}
            </if>
            <if test="indicatorName != null and indicatorName != ''">
                AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%')
            </if>
        </where>
        ORDER BY cur_date DESC,sort_no
    </select>

    <select id="selectDailyDataByWeek" resultType="com.ruoyi.excel.wecom.domain.CustomerStatisticsData">
        SELECT * FROM customer_statistics_data
        <where>
            corp_id = #{corpId}
            AND YEAR(cur_date) = #{year}
            AND WEEK(cur_date, 1) = #{week}
            <if test="indicatorName != null and indicatorName != ''">
                AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%')
            </if>
        </where>
        ORDER BY cur_date, sort_no
    </select>

    <!-- 按日期范围查询周数据（修复跨年周问题） -->
    <select id="selectDailyDataByWeekRange" resultType="com.ruoyi.excel.wecom.domain.CustomerStatisticsData">
        SELECT * FROM customer_statistics_data
        <where>
            corp_id = #{corpId}
            AND cur_date &gt;= #{startDate}
            AND cur_date &lt;= #{endDate}
            <if test="indicatorName != null and indicatorName != ''">
                AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%')
            </if>
        </where>
        ORDER BY cur_date, sort_no
    </select>

    <select id="selectDailyDataByMonth" resultType="com.ruoyi.excel.wecom.domain.CustomerStatisticsData">
        SELECT * FROM customer_statistics_data
        <where>
            corp_id = #{corpId}
            AND DATE_FORMAT(cur_date, '%Y-%m') = #{yearMonth}
            <if test="indicatorName != null and indicatorName != ''">
                AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%')
            </if>
        </where>
        ORDER BY cur_date, sort_no
    </select>

    <!-- 调试查询：检查indicator_name是否有隐藏字符 -->
    <select id="selectDebugIndicatorName" resultType="java.util.Map">
        SELECT id, cur_date, 
               CONCAT('[', indicator_name, ']') as name_with_brackets, 
               LENGTH(indicator_name) as name_length
        FROM customer_statistics_data
        WHERE corp_id = #{corpId}
        AND DATE_FORMAT(cur_date, '%Y-%m') = #{yearMonth}
        <if test="indicatorName != null and indicatorName != ''">
            AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%')
        </if>
        ORDER BY cur_date
    </select>

    <select id="selectAllDailyData" resultType="com.ruoyi.excel.wecom.domain.CustomerStatisticsData">
        SELECT * FROM customer_statistics_data
        <where>
            corp_id = #{corpId}
            <if test="indicatorName != null and indicatorName != ''">
                AND indicator_name LIKE CONCAT('%', #{indicatorName}, '%')
            </if>
        </where>
        ORDER BY cur_date, sort_no
    </select>

</mapper>
