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

    <!-- 根据日期查询统计数据 -->
    <select id="selectByDate" resultType="com.ruoyi.excel.wecom.domain.DepartmentStatisticsData">
        SELECT *
        FROM department_statistics_data
        WHERE stat_date = #{date}
        ORDER BY department_path, sort_no
    </select>

    <!-- 根据部门路径和日期范围查询统计数据 -->
    <select id="selectByDepartmentAndDateRange" resultType="com.ruoyi.excel.wecom.domain.DepartmentStatisticsData">
        SELECT *
        FROM department_statistics_data
        WHERE department_path = #{departmentPath}
        <if test="startDate != null">
            AND stat_date &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND stat_date &lt;= #{endDate}
        </if>
        ORDER BY stat_date, sort_no
    </select>

    <!-- 查询指定部门的历史累计数据（总承接数） -->
    <select id="selectHistoricalAcceptSum" resultType="map">
        SELECT
            SUM(daily_total_accepted) as total_value,
            COUNT(*) as record_count
        FROM department_statistics_data
        WHERE department_path = #{departmentPath}
    </select>

    <!-- 查询指定部门的历史累计数据（总成单数） -->
    <select id="selectHistoricalOrderSum" resultType="map">
        SELECT
            SUM(daily_total_orders) as total_value,
            COUNT(*) as record_count
        FROM department_statistics_data
        WHERE department_path = #{departmentPath}
    </select>

    <!-- 查询部门统计数据VO列表(用于导出) -->
    <select id="selectDepartmentStatisticsDataVOList" resultType="com.ruoyi.excel.wecom.vo.DepartmentStatisticsDataVO">
        SELECT
        stat_date as statDate,
        department_path as departmentPath,
        daily_total_accepted as dailyTotalAccepted,
        daily_total_orders as dailyTotalOrders,
        daily_conversion_rate as dailyConversionRate,
        daily_timely_order_ratio as dailyTimelyOrderRatio,
        daily_non_timely_order_ratio as dailyNonTimelyOrderRatio,
        sort_no as sortNo
        FROM department_statistics_data
        <where>
            <if test="startDate != null">
                AND stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND stat_date &lt;= #{endDate}
            </if>
            <if test="departmentPath != null and departmentPath != ''">
                AND department_path LIKE CONCAT(#{departmentPath}, '%')
            </if>
        </where>
        ORDER BY stat_date DESC, department_path, sort_no
    </select>
    <select id="selectDepartmentStatisticsDataList"
            resultType="com.ruoyi.excel.wecom.domain.DepartmentStatisticsData">
        SELECT
            stat_date as statDate,
            department_path as departmentPath,
            daily_total_accepted as dailyTotalAccepted,
            daily_total_orders as dailyTotalOrders,
            daily_conversion_rate as dailyConversionRate,
        daily_timely_order_ratio as dailyTimelyOrderRatio,
        daily_non_timely_order_ratio as dailyNonTimelyOrderRatio,
            sort_no as sortNo
        FROM department_statistics_data
        <where>
            <if test="startDate != null">
                AND stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND stat_date &lt;= #{endDate}
            </if>
            <if test="departmentPath != null and departmentPath != ''">
                AND department_path LIKE CONCAT(#{departmentPath}, '%')
            </if>
        </where>
        ORDER BY stat_date DESC, department_path, sort_no
    </select>

    <select id="getSummary" resultType="java.util.Map">
        select sum(daily_total_orders) as totalOrders,
               sum(ifnull(daily_total_accepted,0)+ifnull(manager_accepted,0)) as totalIns
        from department_statistics_data
        <where>
            <if test="startDate != null">
                AND stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND stat_date &lt;= #{endDate}
            </if>
            <if test="departmentPath != null and departmentPath != ''">
                AND department_path = #{departmentPath}
            </if>
        </where>
    </select>

</mapper>
