longz
2026-08-19 2a916d05d9f14d583d89f77aed6f63fd49375f60
ruoyi-service/ruoyi-order/src/main/resources/mapper/order/TChargingOrderMapper.xml
@@ -103,15 +103,12 @@
        </if>
    </select>
    <select id="chargingOrder" resultType="com.ruoyi.order.api.vo.ChargingOrderVO">
        select t1.*,
        app_user.phone,
        partner.name as partnerName,
        IFNULL(t1.whitelist_electric_refund_amount, 0) + IFNULL(t1.whitelist_service_refund_amount, 0) as whitelistRefundAmount
        from t_charging_order t1
        left join `charging_pile_account`.`t_app_user` app_user on app_user.id = t1.app_user_id
        left join `charging_pile_service`.`t_site` site on site.id = t1.site_id
        left join `charging_pile_service`.`t_partner` partner on partner.id = site.partner_id
    <!--
        充电桩订单分页列表。手动分页(不传 IPage,避免分页拦截器生成无法下推的全量 COUNT);
        ORDER BY + LIMIT 内联,MyCat 可下推到各分片。不跨库 JOIN app_user/site/partner(无筛选时会导致全量广播拉取),
        手机号在 Service 层预解析为 userIds(app_user_id 即分片键,可直接路由),phone/partnerName 由 Service 层回填。
    -->
    <sql id="ChargingOrderListWhere">
        where t1.del_flag = 0
        <if test="null != req.code and req.code!=''">
            and t1.code LIKE CONCAT('%',#{req.code},'%')
@@ -121,9 +118,6 @@
            <foreach collection="req.userIds" item="item" index="index" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="req.phone != null and req.phone != ''">
            and app_user.phone LIKE CONCAT('%',#{req.phone},'%')
        </if>
        <if test="null != req.siteIds and req.siteIds.size()>0">
            and t1.site_id in
@@ -173,8 +167,20 @@
        <if test="endTime1 != null and endTime1!=''">
            and (t1.end_time between #{endTime1} and #{endTime2})
        </if>
        and t1.del_flag = 0
    </sql>
    <select id="chargingOrder" resultType="com.ruoyi.order.api.vo.ChargingOrderVO">
        select t1.*,
        IFNULL(t1.whitelist_electric_refund_amount, 0) + IFNULL(t1.whitelist_service_refund_amount, 0) as whitelistRefundAmount
        from t_charging_order t1
        <include refid="ChargingOrderListWhere"/>
        order by t1.create_time desc
        limit #{offset}, #{pageSize}
    </select>
    <!-- 充电桩订单总数(列表口径,手动分页用)。单表广播,COUNT 可下推到各分片后由代理层求和。 -->
    <select id="chargingOrderCount" resultType="long">
        select count(*)
        from t_charging_order t1
        <include refid="ChargingOrderListWhere"/>
    </select>
    <select id="payOrderQuery" resultType="com.ruoyi.order.dto.PayOrderDto">
        SELECT o.*,i.phone FROM (
@@ -294,115 +300,15 @@
        ORDER BY COALESCE(o.pay_time, o.create_time, o.end_time) DESC, o.create_time DESC, o.id DESC
    </select>
    <select id="getRefundList" resultType="com.ruoyi.order.api.model.TChargingOrderRefund">
        select * from (
        SELECT
        re.id,
        re.`code`,
        re.refund_code,
        re.refund_title,
        re.refund_reason,
        re.refund_amount,
        re.balance_refund_amount,
        re.third_refund_amount,
        re.refund_total_amount,
        re.refund_time,
        re.pay_type,
        re.pay_amount,
        re.refund_remark,
        co.recharge_serial_number as serail_number,
        co.payment_amount,
        co.create_time as pay_time,
        co.id as order_id,
        co.app_user_id as user_id,
        co.site_id as site_id,
        co.pay_method as pay_method,
        1 as type
        FROM t_charging_order_refund re
        LEFT JOIN t_charging_order co ON re.charging_order_id = co.id
        <if test="data.specialRefund != null and data.specialRefund">
            WHERE re.refund_title = '特殊退款处理'
        </if>
        <if test="data.specialRefund == null or !data.specialRefund">
        UNION ALL
        SELECT
        re.id,
        re.`code`,
        re.refund_code,
        re.refund_title,
        re.refund_reason,
        re.refund_amount,
        0 as balance_refund_amount,
        re.refund_amount as third_refund_amount,
        re.refund_total_amount,
        re.refund_time,
        re.pay_type,
        re.pay_amount,
        re.refund_remark,
        so.serial_number,
        so.payment_amount,
        so.create_time as pay_time,
        so.id as order_id,
        so.app_user_id as user_id,
        null as site_id,
        3 as pay_method,
        2 as type
        FROM t_shopping_order_refund re
        LEFT JOIN t_shopping_order so on re.shopping_order_id = so.id
        UNION ALL
        SELECT
        vr.id,
        vr.`code`,
        vr.refund_code,
        vr.refund_title,
        vr.refund_reason,
        vr.refund_amount,
        0 as balance_refund_amount,
        vr.refund_amount as third_refund_amount,
        vr.refund_total_amount,
        vr.refund_time,
        vr.pay_type,
        vr.pay_amount,
        vr.refund_remark,
        vo.serial_number,
        vo.payment_amount,
        vo.create_time as pay_time,
        vo.id as order_id,
        vo.app_user_id as user_id,
        null as site_id,
        3 as pay_method,
        3 as type
        FROM t_vip_order_refund vr
        LEFT JOIN t_vip_order vo on vr.vip_order_id = vo.id
        </if>
        ) o
        <where>
            <if test="data.code != null and data.code != ''">
                AND o.code LIKE CONCAT('%',#{data.code},'%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND o.refund_code LIKE CONCAT('%',#{data.refundCode},'%')
            </if>
            <if test="data.serialNumber != null and data.serialNumber != ''">
                AND o.serail_number LIKE CONCAT('%',#{data.serialNumber},'%')
            </if>
            <if test="data.specialRefund != null and data.specialRefund">
                AND o.refund_title = '特殊退款处理'
            </if>
            <if test="data.siteIds != null and data.siteIds.size() > 0">
                AND (o.type != 1 OR o.site_id IN
                <foreach collection="data.siteIds" item="siteId" open="(" separator="," close=")">
                    #{siteId}
                </foreach>
                )
            </if>
        </where>
        order by o.refund_time desc
    </select>
    <select id="getRefundListWithoutOrderJoin" resultType="com.ruoyi.order.api.model.TChargingOrderRefund">
    <!--
        退款列表(筛选逆反查版)。serialNumber/siteIds 筛选时,先由 Service 按条件查出订单 id 列表,
        再按各类型订单 id IN 过滤退款表(IN 为退款表分片键,MyCat 可路由),替代跨分片 LEFT JOIN。
        type1/type2/type3 订单 id:null = 不过滤;空列表 = 无匹配(AND 1=0)。
        各分支同样内联 ORDER BY + LIMIT 下推。
    -->
    <select id="getRefundListByOrderIds" resultType="com.ruoyi.order.api.model.TChargingOrderRefund">
        SELECT * FROM (
        SELECT
        (SELECT
        re.id,
        re.`code`,
        re.refund_code,
@@ -429,10 +335,31 @@
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
            <choose>
                <when test="type1OrderIds != null and type1OrderIds.size() > 0">
                    AND re.charging_order_id IN
                    <foreach collection="type1OrderIds" item="id" open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </when>
                <otherwise>
                    <if test="type1OrderIds != null">
                        AND 1 = 0
                    </if>
                </otherwise>
            </choose>
        </where>
        ORDER BY re.refund_time DESC
        LIMIT #{branchLimit})
        <if test="data.specialRefund == null or !data.specialRefund">
        UNION ALL
        SELECT
        (SELECT
        re.id,
        re.`code`,
        re.refund_code,
@@ -456,9 +383,30 @@
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
            <choose>
                <when test="type2OrderIds != null and type2OrderIds.size() > 0">
                    AND re.shopping_order_id IN
                    <foreach collection="type2OrderIds" item="id" open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </when>
                <otherwise>
                    <if test="type2OrderIds != null">
                        AND 1 = 0
                    </if>
                </otherwise>
            </choose>
        </where>
        ORDER BY re.refund_time DESC
        LIMIT #{branchLimit})
        UNION ALL
        SELECT
        (SELECT
        re.id,
        re.`code`,
        re.refund_code,
@@ -482,10 +430,348 @@
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
            <choose>
                <when test="type3OrderIds != null and type3OrderIds.size() > 0">
                    AND re.vip_order_id IN
                    <foreach collection="type3OrderIds" item="id" open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </when>
                <otherwise>
                    <if test="type3OrderIds != null">
                        AND 1 = 0
                    </if>
                </otherwise>
            </choose>
        </where>
        ORDER BY re.refund_time DESC
        LIMIT #{branchLimit})
        </if>
        ) o
        ORDER BY o.refund_time DESC
        LIMIT #{offset}, #{limit}
    </select>
    <!--
        退款总数(筛选逆反查版),与 getRefundListByOrderIds 同构,三分支 COUNT 可下推。
    -->
    <select id="countRefundListByOrderIds" resultType="long">
        SELECT SUM(cnt) FROM (
        (SELECT COUNT(*) AS cnt
        FROM t_charging_order_refund re
        <where>
            <if test="data.specialRefund != null and data.specialRefund">
                AND re.refund_title = '特殊退款处理'
            </if>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
            <choose>
                <when test="type1OrderIds != null and type1OrderIds.size() > 0">
                    AND re.charging_order_id IN
                    <foreach collection="type1OrderIds" item="id" open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </when>
                <otherwise>
                    <if test="type1OrderIds != null">
                        AND 1 = 0
                    </if>
                </otherwise>
            </choose>
        </where>)
        <if test="data.specialRefund == null or !data.specialRefund">
        UNION ALL
        (SELECT COUNT(*) AS cnt
        FROM t_shopping_order_refund re
        <where>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
            <choose>
                <when test="type2OrderIds != null and type2OrderIds.size() > 0">
                    AND re.shopping_order_id IN
                    <foreach collection="type2OrderIds" item="id" open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </when>
                <otherwise>
                    <if test="type2OrderIds != null">
                        AND 1 = 0
                    </if>
                </otherwise>
            </choose>
        </where>)
        UNION ALL
        (SELECT COUNT(*) AS cnt
        FROM t_vip_order_refund re
        <where>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
            <choose>
                <when test="type3OrderIds != null and type3OrderIds.size() > 0">
                    AND re.vip_order_id IN
                    <foreach collection="type3OrderIds" item="id" open="(" separator="," close=")">
                        #{id}
                    </foreach>
                </when>
                <otherwise>
                    <if test="type3OrderIds != null">
                        AND 1 = 0
                    </if>
                </otherwise>
            </choose>
        </where>)
        </if>
        ) t
    </select>
    <!--
        按支付流水号模糊匹配充电订单 id(逆反查第一步)。
        注意:前导通配符 LIKE '%x%' 无法走 recharge_serial_number 索引,MyCat 广播 50 张分片表全表扫描;
        匹配行一般极少(流水号近似唯一),结果集小,可接受。ORDER BY id 保证截断时结果确定性。
    -->
    <select id="listChargingOrderIdsBySerialNumber" resultType="long">
        SELECT id FROM t_charging_order
        WHERE recharge_serial_number LIKE CONCAT('%', #{serialNumber}, '%')
        ORDER BY id DESC
        LIMIT #{limit}
    </select>
    <!-- 按支付流水号模糊匹配商城订单 id(同前导通配符说明,广播 20 张分片表) -->
    <select id="listShoppingOrderIdsBySerialNumber" resultType="long">
        SELECT id FROM t_shopping_order
        WHERE serial_number LIKE CONCAT('%', #{serialNumber}, '%')
        ORDER BY id DESC
        LIMIT #{limit}
    </select>
    <!-- 按支付流水号模糊匹配会员订单 id(同前导通配符说明,广播 20 张分片表) -->
    <select id="listVipOrderIdsBySerialNumber" resultType="long">
        SELECT id FROM t_vip_order
        WHERE serial_number LIKE CONCAT('%', #{serialNumber}, '%')
        ORDER BY id DESC
        LIMIT #{limit}
    </select>
    <!-- 按站点匹配充电订单 id(逆反查第一步,site_id 走新增索引;ORDER BY id 保证截断时结果确定性) -->
    <select id="listChargingOrderIdsBySiteIds" resultType="long">
        SELECT id FROM t_charging_order
        WHERE site_id IN
        <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
            #{siteId}
        </foreach>
        ORDER BY id DESC
        LIMIT #{limit}
    </select>
    <!--
        退款列表(分片下推版)。
        三个 UNION 分支各自单表查询,分支内 ORDER BY + LIMIT 可被 MyCat 下推到每张分片表(每表最多取 offset+limit 行),
        代理层只归并三分支共 3×(offset+limit) 行再取页。相比旧版(90 张分片表全量拉回代理层排序)避免全量传输。
    -->
    <select id="getRefundListPage" resultType="com.ruoyi.order.api.model.TChargingOrderRefund">
        SELECT * FROM (
        (SELECT
        re.id,
        re.`code`,
        re.refund_code,
        re.refund_title,
        re.refund_reason,
        re.refund_amount,
        re.balance_refund_amount,
        re.third_refund_amount,
        re.refund_total_amount,
        re.refund_time,
        re.pay_type,
        re.pay_amount,
        re.refund_remark,
        re.charging_order_id AS order_id,
        1 AS type
        FROM t_charging_order_refund re
        <where>
            <if test="data.specialRefund != null and data.specialRefund">
                AND re.refund_title = '特殊退款处理'
            </if>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
        </where>
        ORDER BY re.refund_time DESC
        LIMIT #{branchLimit})
        <if test="data.specialRefund == null or !data.specialRefund">
        UNION ALL
        (SELECT
        re.id,
        re.`code`,
        re.refund_code,
        re.refund_title,
        re.refund_reason,
        re.refund_amount,
        0 AS balance_refund_amount,
        re.refund_amount AS third_refund_amount,
        re.refund_total_amount,
        re.refund_time,
        re.pay_type,
        re.pay_amount,
        re.refund_remark,
        re.shopping_order_id AS order_id,
        2 AS type
        FROM t_shopping_order_refund re
        <where>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
        </where>
        ORDER BY re.refund_time DESC
        LIMIT #{branchLimit})
        UNION ALL
        (SELECT
        re.id,
        re.`code`,
        re.refund_code,
        re.refund_title,
        re.refund_reason,
        re.refund_amount,
        0 AS balance_refund_amount,
        re.refund_amount AS third_refund_amount,
        re.refund_total_amount,
        re.refund_time,
        re.pay_type,
        re.pay_amount,
        re.refund_remark,
        re.vip_order_id AS order_id,
        3 AS type
        FROM t_vip_order_refund re
        <where>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
        </where>
        ORDER BY re.refund_time DESC
        LIMIT #{branchLimit})
        </if>
        ) o
        ORDER BY o.refund_time DESC
        LIMIT #{offset}, #{limit}
    </select>
    <!--
        退款总数(分片下推版)。三分支各自 COUNT(可下推到每张分片表),代理层只 SUM 三个数。
        旧版由分页拦截器生成 SELECT COUNT(*) FROM (90 表全量 UNION),MyCat 无法下推导致全量扫描。
    -->
    <select id="countRefundList" resultType="long">
        SELECT SUM(cnt) FROM (
        (SELECT COUNT(*) AS cnt
        FROM t_charging_order_refund re
        <where>
            <if test="data.specialRefund != null and data.specialRefund">
                AND re.refund_title = '特殊退款处理'
            </if>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
        </where>)
        <if test="data.specialRefund == null or !data.specialRefund">
        UNION ALL
        (SELECT COUNT(*) AS cnt
        FROM t_shopping_order_refund re
        <where>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
        </where>)
        UNION ALL
        (SELECT COUNT(*) AS cnt
        FROM t_vip_order_refund re
        <where>
            <if test="data.code != null and data.code != ''">
                AND re.code LIKE CONCAT('%', #{data.code}, '%')
            </if>
            <if test="data.refundCode != null and data.refundCode != ''">
                AND re.refund_code LIKE CONCAT('%', #{data.refundCode}, '%')
            </if>
            <if test="data.refundTime1 != null and data.refundTime1 != ''">
                AND re.refund_time &gt;= #{data.refundTime1}
            </if>
            <if test="data.refundTime2 != null and data.refundTime2 != ''">
                AND re.refund_time &lt;= #{data.refundTime2}
            </if>
        </where>)
        </if>
        ) t
    </select>
    <select id="chargingOrderGroup" resultType="com.ruoyi.common.core.dto.ChargingOrderGroup">
        SELECT SUM(tc.charging_capacity) as charging_capacity,tc.site_id
@@ -548,6 +834,31 @@
            </foreach>
        </if>
        GROUP BY EXTRACT(MONTH FROM billing_cycle)
    </select>
    <select id="chargeOrderHistory" resultType="com.ruoyi.order.dto.SixChargingDto">
        SELECT EXTRACT(MONTH FROM create_time) AS MONTH,
               SUM(IFNULL(electrovalence, 0)) AS electrovalence,
               SUM(CASE
                       WHEN order_source = 2
                           THEN IFNULL(service_charge, 0) * 0.8
                       ELSE IFNULL(service_charge, 0)
                   END) AS service_charge
        FROM t_charging_order
        WHERE del_flag = 0
          AND status = 5
          AND recharge_payment_status = 2
          AND create_time &lt; DATE_FORMAT(CURDATE(), '%Y-%m-01')
        <if test="sixBefore != null and sixBefore != ''">
            AND create_time &gt;= #{sixBefore}
        </if>
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
        GROUP BY EXTRACT(MONTH FROM create_time)
    </select>
    <select id="chargeCurrentMonth" resultType="com.ruoyi.order.dto.SixChargingDto">
@@ -616,6 +927,9 @@
                       0 AS sharing_amount
                FROM t_charging_order_share_record
                WHERE del_flag = 0 AND online_share_status IN ('SUCCESS', 'RECORDED')
                <if test="sixBefore != null">
                    AND create_time &gt;= #{sixBefore}
                </if>
                <if test="siteIds != null and siteIds.size() > 0">
                    AND site_id IN
                    <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
@@ -630,6 +944,10 @@
                       COALESCE(ROUND(SUM(IFNULL(tripartite_commission, 0)), 2), 0) AS sharing_amount
                FROM t_charging_settlement_bill
                WHERE statistical_date_type = 1 AND site_id != 0
                <if test="sixBefore != null">
                    AND billing_cycle &gt;= #{sixBefore}
                    AND billing_cycle &lt; DATE_FORMAT(CURDATE(), '%Y-%m-01')
                </if>
                <if test="siteIds != null and siteIds.size() > 0">
                    AND site_id IN
                    <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
@@ -644,6 +962,9 @@
        SELECT COALESCE(ROUND(SUM(IFNULL(share_amount, 0)), 2), 0)
        FROM t_charging_order_share_record
        WHERE del_flag = 0 AND online_share_status IN ('SUCCESS', 'RECORDED')
        <if test="sixBefore != null">
            AND create_time &gt;= #{sixBefore}
        </if>
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
@@ -2330,41 +2651,21 @@
    <select id="chargingOrderBaseSummary" resultType="map">
        select
        sum(aa.time) as `time`,
        sum(aa.income) as income,
        sum(aa.electronicMoney) as electronicMoney,
        sum(aa.serviceMoney) as serviceMoney,
        sum(aa.total) as total,
        sum(CASE WHEN aa.recharge_payment_type = 1 and aa.thirdNetAmount > 0 THEN aa.thirdNetAmount ELSE 0 END) as wechatIncome,
        sum(CASE WHEN aa.recharge_payment_type in (2, 3) and aa.thirdNetAmount > 0 THEN aa.thirdNetAmount ELSE 0 END) as alipayIncome,
        sum(CASE WHEN aa.is_whitelist = 1 THEN IFNULL(aa.electrovalence, 0) + IFNULL(aa.serviceCharge, 0) ELSE 0 END) as whitelistChargingIncome,
        sum(CASE WHEN aa.is_whitelist = 1 THEN IFNULL(aa.whitelist_electric_refund_amount, 0) + IFNULL(aa.whitelist_service_refund_amount, 0) ELSE 0 END) as whitelistRefund
        from (
            select
            b.id,
            CASE WHEN count(a.id) > 0 THEN (UNIX_TIMESTAMP(MAX(b.end_time)) - UNIX_TIMESTAMP(MAX(b.start_time))) ELSE 0 END as time,
            sum(a.charging_capacity) as total,
            sum(a.period_electric_price) as electronicMoney,
            sum(CASE
                    WHEN b.order_source = 1 THEN a.period_service_price
                    ELSE a.period_service_price * 0.8
                END) as serviceMoney,
            sum(a.period_electric_price + CASE WHEN b.order_source = 1 THEN a.period_service_price ELSE a.period_service_price * 0.8 END) as income,
            MAX(b.recharge_payment_type) as recharge_payment_type,
            MAX(b.is_whitelist) as is_whitelist,
            MAX(b.electrovalence) as electrovalence,
            MAX(b.service_charge) as serviceCharge,
            MAX(b.whitelist_electric_refund_amount) as whitelist_electric_refund_amount,
            MAX(b.whitelist_service_refund_amount) as whitelist_service_refund_amount,
            MAX(IFNULL(b.third_pay_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.payment_amount, IFNULL(b.recharge_amount, 0)) ELSE 0 END) - IFNULL(b.third_refund_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.refund_amount, 0) ELSE 0 END)) as thirdNetAmount
            from `charging_pile_order`.t_charging_order b
            left join `charging_pile_order`.t_charging_order_accounting_strategy a on a.charging_order_id = b.id
            <if test="req.phone != null and req.phone != ''">
                inner join `charging_pile_account`.`t_app_user` app_user on app_user.id = b.app_user_id
            </if>
            <include refid="ChargingOrderSummaryWhere"/>
            group by b.id,b.end_time,b.start_time,b.order_source
        ) as aa
        sum(COALESCE(summary.charging_duration, UNIX_TIMESTAMP(b.end_time) - UNIX_TIMESTAMP(b.start_time))) as `time`,
        sum(CASE WHEN b.refund_status = 2 THEN 0 ELSE COALESCE(summary.income, IFNULL(b.electrovalence, 0) + CASE WHEN b.order_source = 1 THEN IFNULL(b.service_charge, 0) ELSE IFNULL(b.service_charge, 0) * 0.8 END) END) as income,
        sum(CASE WHEN b.refund_status = 2 THEN 0 ELSE COALESCE(summary.electric_price, IFNULL(b.electrovalence, 0)) END) as electronicMoney,
        sum(CASE WHEN b.refund_status = 2 THEN 0 ELSE COALESCE(summary.service_price, CASE WHEN b.order_source = 1 THEN IFNULL(b.service_charge, 0) ELSE IFNULL(b.service_charge, 0) * 0.8 END) END) as serviceMoney,
        sum(COALESCE(summary.charging_capacity, IFNULL(b.charging_capacity, 0))) as total,
        sum(CASE WHEN b.recharge_payment_type = 1 AND IFNULL(b.third_pay_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.payment_amount, IFNULL(b.recharge_amount, 0)) ELSE 0 END) - IFNULL(b.third_refund_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.refund_amount, 0) ELSE 0 END) &gt; 0 THEN IFNULL(b.third_pay_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.payment_amount, IFNULL(b.recharge_amount, 0)) ELSE 0 END) - IFNULL(b.third_refund_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.refund_amount, 0) ELSE 0 END) ELSE 0 END) as wechatIncome,
        sum(CASE WHEN b.recharge_payment_type in (2, 3) AND IFNULL(b.third_pay_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.payment_amount, IFNULL(b.recharge_amount, 0)) ELSE 0 END) - IFNULL(b.third_refund_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.refund_amount, 0) ELSE 0 END) &gt; 0 THEN IFNULL(b.third_pay_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.payment_amount, IFNULL(b.recharge_amount, 0)) ELSE 0 END) - IFNULL(b.third_refund_amount, CASE WHEN b.pay_method = 3 THEN IFNULL(b.refund_amount, 0) ELSE 0 END) ELSE 0 END) as alipayIncome,
        sum(CASE WHEN b.is_whitelist = 1 THEN IFNULL(b.electrovalence, 0) + IFNULL(b.service_charge, 0) ELSE 0 END) as whitelistChargingIncome,
        sum(CASE WHEN b.is_whitelist = 1 THEN IFNULL(b.whitelist_electric_refund_amount, 0) + IFNULL(b.whitelist_service_refund_amount, 0) ELSE 0 END) as whitelistRefund
        from `charging_pile_order`.t_charging_order b
        left join `charging_pile_order`.t_charging_order_summary_data summary on summary.charging_order_id = b.id
        <if test="req.phone != null and req.phone != ''">
            inner join `charging_pile_account`.`t_app_user` app_user on app_user.id = b.app_user_id
        </if>
        <include refid="ChargingOrderSummaryWhere"/>
    </select>
    <select id="chargingOrderNolimit" resultType="map">
@@ -2421,19 +2722,23 @@
    <select id="chargingOrderShareIncomeByOrderIds" resultType="map">
        select
        s.charging_order_id as orderId,
        o.id as orderId,
        sum(IFNULL(s.channel_pay_amount, 0) - IFNULL(s.share_amount, 0)) as platformIncome,
        sum(IFNULL(s.share_amount, 0)) as partnerIncome
        from `charging_pile_order`.t_charging_order_share_record s
        inner join `charging_pile_order`.t_charging_order o on o.id = s.charging_order_id
        where s.del_flag = 0 and s.charging_order_id is not null
        sum(IFNULL(s.share_amount, 0)) as partnerIncome,
        CASE WHEN o.is_whitelist = 1 AND o.status = 5 THEN IFNULL(o.whitelist_electric_refund_amount, 0)
            + IFNULL(o.whitelist_service_refund_amount, 0) ELSE 0 END as whitelistRefund
        from `charging_pile_order`.t_charging_order o
        left join `charging_pile_order`.t_charging_order_share_record s
            on s.charging_order_id = o.id and s.del_flag = 0
        where o.del_flag = 0
        <if test="orderIds != null and orderIds.size()>0">
            and s.charging_order_id in
            and o.id in
            <foreach collection="orderIds" item="item" index="index" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by s.charging_order_id
        group by o.id, o.is_whitelist, o.status,
            o.whitelist_electric_refund_amount, o.whitelist_service_refund_amount
    </select>
    <select id="chargingOrderShareIncomeByPayChannel" resultType="map">
@@ -2535,21 +2840,19 @@
    </select>
    <select id="countOrdersByDate" resultType="com.ruoyi.order.vo.OrderCountByDate">
        SELECT DATE_FORMAT(create_time, '%m/%d') AS `date`, COUNT(*) AS `count`
    <!-- 电池安全检测按日计数: 单日有界 COUNT, 无 GROUP BY/ORDER BY, 规避 MyCat 规划器
         在函数列上排序(MycatMemSort)失败的报错; 天级分组由 Java 端循环完成 -->
    <select id="countOrdersInRangeByGunIds" resultType="java.lang.Long">
        SELECT COUNT(1)
        FROM t_charging_order
        WHERE
        del_flag = 0
        and
        status in (3,5)
        and charging_gun_id in
        WHERE del_flag = 0
          AND status IN (3, 5)
          AND charging_gun_id IN
        <foreach item="item" index="index" collection="gunIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        and create_time BETWEEN #{startTime} AND #{endTime}
        GROUP BY DATE(create_time, '%m/%d')
        ORDER BY DATE(create_time, '%m/%d') ASC
          AND create_time &gt;= #{startTime}
          AND create_time &lt; #{endTime}
    </select>
@@ -2720,137 +3023,91 @@
        ORDER BY b.site_id
    </select>
    <sql id="enterpriseChargingBaseWhere">
        toi.invoicing_object_type = 2
        AND toi.status IN (2, 3)
        AND tod.order_type = 1
    <!--
        企业用户充电分析 —— 逆反查聚合(原实现见 git:enterpriseChargingAnalysis 等把
        t_order_invoice(10片/app_user_id) + t_order_invoice_detail(20片/order_invoice_id) +
        t_charging_order(50片/app_user_id) 在代理层跨分片 JOIN,分片键不一致只能全量广播,无筛选必超时)。
        这里只提供单表/按分片键 IN 路由的构建块,按公司聚合在 Service 层完成(与 enterpriseChargingDetail 同模式)。
    -->
    <!-- 企业发票行(t_order_invoice 单表 10 片,抬头级过滤可下推到各分片;id 唯一故无需 GROUP BY) -->
    <select id="enterpriseInvoiceRows" resultType="com.ruoyi.order.dto.EnterpriseChargingAggRow$Invoice">
        SELECT
            id,
            TRIM(name) AS companyName,
            IFNULL(TRIM(tax_identification_number), '') AS socialCreditCode,
            company_address AS companyAddress,
            company_phone AS companyPhone,
            IFNULL(total_amount, 0) AS totalAmount
        FROM t_order_invoice
        WHERE invoicing_object_type = 2
          AND status IN (2, 3)
        <if test="data.exactCompanyName != null and data.exactCompanyName != ''">
            AND TRIM(toi.name) = TRIM(#{data.exactCompanyName})
            AND TRIM(name) = TRIM(#{data.exactCompanyName})
        </if>
        <if test="(data.exactCompanyName == null or data.exactCompanyName == '') and data.companyName != null and data.companyName != ''">
            AND TRIM(toi.name) LIKE CONCAT('%', TRIM(#{data.companyName}), '%')
            AND TRIM(name) LIKE CONCAT('%', TRIM(#{data.companyName}), '%')
        </if>
        <if test="data.socialCreditCode != null and data.socialCreditCode != ''">
            AND IFNULL(TRIM(toi.tax_identification_number), '') = TRIM(#{data.socialCreditCode})
            AND IFNULL(TRIM(tax_identification_number), '') = TRIM(#{data.socialCreditCode})
        </if>
        <if test="data.siteType != null">
            AND s.site_type = #{data.siteType}
        </if>
        <if test="data.partnerId != null">
            AND s.partner_id = #{data.partnerId}
        </if>
        <if test="data.siteId != null">
            AND co.site_id = #{data.siteId}
        </if>
        <if test="data.createTime1 != null">
            AND co.create_time &gt;= #{data.createTime1}
        </if>
        <if test="data.createTime2 != null">
            AND co.create_time &lt;= #{data.createTime2}
        </if>
    </sql>
    </select>
    <select id="enterpriseChargingAnalysis" resultType="com.ruoyi.order.dto.EnterpriseChargingAnalysisVO">
    <!-- 企业发票充电明细(t_order_invoice_detail 按 order_invoice_id 分片,IN 直接路由;order_type=1 只取充电明细) -->
    <select id="enterpriseInvoiceDetailRows" resultType="com.ruoyi.order.dto.EnterpriseChargingAggRow$Detail">
        SELECT
            b.companyName,
            b.socialCreditCode,
            MAX(b.companyAddress) AS companyAddress,
            MAX(b.companyPhone) AS companyPhone,
            COUNT(1) AS invoiceCount,
            SUM(CASE WHEN b.detailInvoiceAmount &gt; 0 THEN b.detailInvoiceAmount ELSE b.totalAmount END) AS invoiceAmount,
            MAX(b.latestChargingTime) AS latestChargingTime
        FROM (
            SELECT
                toi.id AS invoiceId,
                TRIM(toi.name) AS companyName,
                IFNULL(TRIM(toi.tax_identification_number), '') AS socialCreditCode,
                MAX(toi.company_address) AS companyAddress,
                MAX(toi.company_phone) AS companyPhone,
                MAX(IFNULL(toi.total_amount, 0)) AS totalAmount,
                SUM(IFNULL(tod.invoice_amount, 0)) AS detailInvoiceAmount,
                MAX(co.start_time) AS latestChargingTime
            FROM t_order_invoice toi
            INNER JOIN t_order_invoice_detail tod ON tod.order_invoice_id = toi.id
            LEFT JOIN t_charging_order co ON co.id = tod.order_id
            LEFT JOIN charging_pile_service.t_site s ON s.id = co.site_id
            <where>
                <include refid="enterpriseChargingBaseWhere" />
            </where>
            GROUP BY toi.id, TRIM(toi.name), TRIM(toi.tax_identification_number)
        ) b
        GROUP BY b.companyName, b.socialCreditCode
        ORDER BY invoiceCount DESC
            order_invoice_id AS invoiceId,
            order_id AS orderId,
            IFNULL(invoice_amount, 0) AS invoiceAmount
        FROM t_order_invoice_detail
        WHERE order_type = 1
          AND order_invoice_id IN
        <foreach collection="invoiceIds" item="invoiceId" open="(" separator="," close=")">
            #{invoiceId}
        </foreach>
    </select>
    <select id="enterpriseTopSites" resultType="map">
    <!-- 命中订单级条件的订单 id(co.id IN 走主键广播,条件同 enterpriseChargingOrderWhere,与明细口径一致) -->
    <select id="enterpriseAnalysisMatchingOrderIds" resultType="long">
        SELECT DISTINCT co.id
        FROM t_charging_order co
        <if test="data.siteType != null or data.partnerId != null or (data.partnerName != null and data.partnerName != '')">
            INNER JOIN charging_pile_service.t_site s ON s.id = co.site_id
        </if>
        <if test="data.partnerName != null and data.partnerName != ''">
            INNER JOIN charging_pile_service.t_partner p ON p.id = s.partner_id
        </if>
        <if test="data.phone != null and data.phone != ''">
            INNER JOIN charging_pile_account.t_app_user au ON au.id = co.app_user_id
        </if>
        <where>
            <include refid="enterpriseChargingOrderWhere" />
        </where>
    </select>
    <!-- 命中订单数据(co.id IN 走主键广播,仅取 start_time/site_id) -->
    <select id="enterpriseAnalysisChargingOrders" resultType="com.ruoyi.order.dto.EnterpriseChargingAggRow$Order">
        SELECT
            s.name AS siteName,
            COUNT(DISTINCT co.id) AS counts
        FROM t_order_invoice toi
        INNER JOIN t_order_invoice_detail tod ON tod.order_invoice_id = toi.id
        INNER JOIN t_charging_order co ON co.id = tod.order_id
        LEFT JOIN charging_pile_service.t_site s ON s.id = co.site_id
        <where>
            <include refid="enterpriseChargingBaseWhere" />
            AND s.name IS NOT NULL
        </where>
        GROUP BY s.id, s.name
        ORDER BY counts DESC
        LIMIT 3
            id AS orderId,
            start_time AS startTime,
            site_id AS siteId
        FROM t_charging_order
        WHERE id IN
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </select>
    <select id="enterpriseTopSitesByCompanies" resultType="map">
    <!-- 站点名(charging_pile_service.t_site 单表,id IN) -->
    <select id="enterpriseAnalysisSites" resultType="com.ruoyi.order.dto.EnterpriseChargingAggRow$Site">
        SELECT
            TRIM(toi.name) AS companyName,
            IFNULL(TRIM(toi.tax_identification_number), '') AS socialCreditCode,
            s.name AS siteName,
            COUNT(DISTINCT co.id) AS counts
        FROM t_order_invoice toi
        INNER JOIN t_order_invoice_detail tod ON tod.order_invoice_id = toi.id
        INNER JOIN t_charging_order co ON co.id = tod.order_id
        LEFT JOIN charging_pile_service.t_site s ON s.id = co.site_id
        <where>
            <include refid="enterpriseChargingBaseWhere" />
            AND s.name IS NOT NULL
            AND TRIM(toi.name) IN
            <foreach collection="companyNames" item="companyName" open="(" separator="," close=")">
                #{companyName}
            </foreach>
        </where>
        GROUP BY TRIM(toi.name), TRIM(toi.tax_identification_number), s.id, s.name
    </select>
    <select id="enterpriseChargingTimes" resultType="java.time.LocalDateTime">
        SELECT DISTINCT co.start_time
        FROM t_order_invoice toi
        INNER JOIN t_order_invoice_detail tod ON tod.order_invoice_id = toi.id
        INNER JOIN t_charging_order co ON co.id = tod.order_id
        LEFT JOIN charging_pile_service.t_site s ON s.id = co.site_id
        <where>
            <include refid="enterpriseChargingBaseWhere" />
            AND co.start_time IS NOT NULL
        </where>
        ORDER BY co.start_time ASC
    </select>
    <select id="enterpriseChargingTimesByCompanies" resultType="map">
        SELECT DISTINCT
            TRIM(toi.name) AS companyName,
            IFNULL(TRIM(toi.tax_identification_number), '') AS socialCreditCode,
            co.start_time AS startTime
        FROM t_order_invoice toi
        INNER JOIN t_order_invoice_detail tod ON tod.order_invoice_id = toi.id
        INNER JOIN t_charging_order co ON co.id = tod.order_id
        LEFT JOIN charging_pile_service.t_site s ON s.id = co.site_id
        <where>
            <include refid="enterpriseChargingBaseWhere" />
            AND co.start_time IS NOT NULL
            AND TRIM(toi.name) IN
            <foreach collection="companyNames" item="companyName" open="(" separator="," close=")">
                #{companyName}
            </foreach>
        </where>
        ORDER BY TRIM(toi.name), co.start_time
            id AS siteId,
            name AS siteName
        FROM charging_pile_service.t_site
        WHERE id IN
        <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
            #{siteId}
        </foreach>
    </select>
    <select id="enterpriseInvoiceIds" resultType="long">
@@ -3045,7 +3302,14 @@
    <select id="getLargeScreenSummary" resultType="java.util.Map">
        SELECT COUNT(DISTINCT charging_order.id) AS validOrderCount,
               COUNT(DISTINCT charging_order.app_user_id) AS chargingUserCount,
               (SELECT COUNT(DISTINCT user_count_order.app_user_id)
                FROM t_charging_order user_count_order
                <if test="siteIds != null and siteIds.size() > 0">
                    WHERE user_count_order.site_id IN
                    <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                        #{siteId}
                    </foreach>
                </if>) AS chargingUserCount,
               (SELECT COUNT(*)
                FROM charging_pile_account.t_app_user app_user
                WHERE app_user.del_flag = 0) AS registeredUserCount,
@@ -3113,4 +3377,421 @@
            AND site_id IN (${siteIdsCsv})
        </if>
    </select>
    <!-- ==================== 大屏预聚合表 t_large_screen_site_stat ==================== -->
    <!-- 任务用: 按月切片聚合。订单数为未删除、状态1-5;金额和电量保持已结束且已支付口径。 -->
    <select id="monthSliceAgg" resultType="java.util.Map">
        SELECT charging_order.site_id AS site_id,
               COUNT(DISTINCT charging_order.id) AS order_count,
               COUNT(DISTINCT charging_order.app_user_id) AS user_count,
               COALESCE(ROUND(SUM(CASE WHEN charging_order.status = 5
                   AND charging_order.recharge_payment_status = 2 THEN accounting.charging_capacity ELSE 0 END), 2), 0) AS capacity,
               COALESCE(ROUND(SUM(CASE WHEN charging_order.status = 5
                   AND charging_order.recharge_payment_status = 2 THEN accounting.period_electric_price ELSE 0 END), 2), 0) AS electric_fee,
               COALESCE(ROUND(SUM(CASE WHEN charging_order.status = 5
                   AND charging_order.recharge_payment_status = 2 AND charging_order.order_source IN (0, 1) THEN accounting.period_service_price
                   WHEN charging_order.status = 5
                   AND charging_order.recharge_payment_status = 2 THEN accounting.period_service_price * 0.8
                   ELSE 0 END), 2), 0) AS service_fee
        FROM t_charging_order charging_order
        LEFT JOIN t_charging_order_accounting_strategy accounting
               ON accounting.charging_order_id = charging_order.id
        WHERE charging_order.del_flag = 0
          AND charging_order.status IN (1, 2, 3, 4, 5)
          AND charging_order.create_time &gt;= #{startTime}
          AND charging_order.create_time &lt; #{endTime}
        GROUP BY charging_order.site_id
    </select>
    <!-- 任务用: 按月切片统计充电用户数(工作台口径: 所有订单的去重用户, 不区分状态/删除标记,
         与 countAllUserData 同定义; 按站点求和会有跨站重复计数) -->
    <select id="monthSliceUserAgg" resultType="java.util.Map">
        SELECT site_id AS site_id,
               COUNT(DISTINCT app_user_id) AS user_count
        FROM t_charging_order
        WHERE create_time &gt;= #{startTime}
          AND create_time &lt; #{endTime}
        GROUP BY site_id
    </select>
    <!-- 任务用: 按时间切片取 (site_id, app_user_id) 对(所有订单, 与工作台 countAllUserData 同口径) -->
    <select id="userSitePairsAgg" resultType="java.util.Map">
        SELECT DISTINCT site_id AS site_id, app_user_id AS app_user_id
        FROM t_charging_order
        WHERE create_time &gt;= #{startTime}
          AND create_time &lt; #{endTime}
          AND app_user_id IS NOT NULL
    </select>
    <!-- 任务用: 批量写入用户-站点对(幂等) -->
    <insert id="insertUserSitePairs">
        INSERT IGNORE INTO t_large_screen_user_site (site_id, app_user_id)
        VALUES
        <foreach collection="rows" item="row" separator=",">
            (#{row.siteId}, #{row.appUserId})
        </foreach>
    </insert>
    <!-- 接口用: 充电用户数精确去重(对表, 选中站点全局去重) -->
    <select id="userCountFromStat" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT app_user_id)
        FROM t_large_screen_user_site
        WHERE site_id IN
        <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
            #{siteId}
        </foreach>
    </select>
    <!-- 任务用: 对表行数(启动回填判断) -->
    <select id="userSiteTableCount" resultType="java.lang.Long">
        SELECT COUNT(*) FROM t_large_screen_user_site
    </select>
    <!-- 任务用: 按日切片聚合,功率保持原口径;收入与月度切片一致。 -->
    <select id="daySliceAgg" resultType="java.util.Map">
        SELECT charging_order.site_id AS site_id,
               COUNT(1) AS order_count,
               COALESCE(SUM(IFNULL(charging_order.power, 0)), 0) AS total_power,
               COALESCE(ROUND(SUM(CASE WHEN charging_order.status = 5
                   AND charging_order.recharge_payment_status = 2 THEN accounting.period_electric_price ELSE 0 END), 2), 0) AS electric_fee,
               COALESCE(ROUND(SUM(CASE WHEN charging_order.status = 5
                   AND charging_order.recharge_payment_status = 2 AND charging_order.order_source IN (0, 1) THEN accounting.period_service_price
                   WHEN charging_order.status = 5
                   AND charging_order.recharge_payment_status = 2 THEN accounting.period_service_price * 0.8
                   ELSE 0 END), 2), 0) AS service_fee
        FROM t_charging_order charging_order
        LEFT JOIN t_charging_order_accounting_strategy accounting
               ON accounting.charging_order_id = charging_order.id
        WHERE charging_order.del_flag = 0
          AND charging_order.create_time &gt;= #{startTime}
          AND charging_order.create_time &lt; #{endTime}
        GROUP BY charging_order.site_id
    </select>
    <!-- 任务用: 注册用户总数 -->
    <select id="globalRegisteredUsers" resultType="java.lang.Long">
        SELECT COUNT(*)
        FROM charging_pile_account.t_app_user
        WHERE del_flag = 0
    </select>
    <!-- 任务用: 批量写入聚合行 -->
    <insert id="replaceSiteStatBatch">
        REPLACE INTO t_large_screen_site_stat
        (stat_type, period, site_id, order_count, user_count, capacity, electric_fee,
         service_fee, income, total_power, registered_user_count, update_time)
        VALUES
        <foreach collection="rows" item="row" separator=",">
            (#{row.statType}, #{row.period}, #{row.siteId}, #{row.orderCount}, #{row.userCount},
             #{row.capacity}, #{row.electricFee}, #{row.serviceFee}, #{row.income},
             #{row.totalPower}, #{row.registeredUserCount}, NOW())
        </foreach>
    </insert>
    <!-- 任务用: 表行数(启动回填判断) -->
    <select id="statTableCount" resultType="java.lang.Long">
        SELECT COUNT(*) FROM t_large_screen_site_stat
    </select>
    <!-- 接口用: 累计指标(M 行汇总) -->
    <select id="summaryFromStat" resultType="java.util.Map">
        SELECT COALESCE(SUM(order_count), 0) AS validOrderCount,
               COALESCE(SUM(user_count), 0) AS chargingUserCount,
               COALESCE(SUM(capacity), 0) AS totalChargingCapacity,
               COALESCE(SUM(electric_fee), 0) AS electricityFee,
               COALESCE(SUM(service_fee), 0) AS serviceFee,
               COALESCE(SUM(income), 0) AS totalIncome
        FROM t_large_screen_site_stat
        WHERE stat_type = 'M'
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
    </select>
    <!-- 接口用: 近 N 日收入趋势(D 行按 period 分组, period 为 yyyy-MM-dd 字典序可比) -->
    <select id="incomeTrendFromStat" resultType="com.ruoyi.order.dto.SixChargingDto">
        SELECT period AS month,
               COALESCE(SUM(electric_fee), 0) AS electrovalence,
               COALESCE(SUM(service_fee), 0) AS service_charge
        FROM t_large_screen_site_stat
        WHERE stat_type = 'D'
          AND period &gt;= #{sixBefore}
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
        GROUP BY period
    </select>
    <select id="sumWhitelistRefundByOrderIds" resultType="java.math.BigDecimal">
        select coalesce(sum(IFNULL(whitelist_electric_refund_amount, 0)
            + IFNULL(whitelist_service_refund_amount, 0)), 0)
        from `charging_pile_order`.t_charging_order
        where del_flag = 0 and is_whitelist = 1 and status = 5
        <if test="orderIds != null and orderIds.size()>0">
            and id in
            <foreach collection="orderIds" item="item" index="index" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>
    <!-- 接口用: 最近七日收入,直接查询确保历史日切片升级后也能立即显示正确金额。 -->
    <select id="incomeTrendFromOrders" resultType="com.ruoyi.order.dto.SixChargingDto">
        SELECT DATE_FORMAT(charging_order.create_time, '%Y-%m-%d') AS month,
               COALESCE(ROUND(SUM(accounting.period_electric_price), 2), 0) AS electrovalence,
               COALESCE(ROUND(SUM(CASE WHEN charging_order.order_source IN (0, 1) THEN accounting.period_service_price
                   ELSE accounting.period_service_price * 0.8 END), 2), 0) AS service_charge
        FROM t_charging_order charging_order
        LEFT JOIN t_charging_order_accounting_strategy accounting
               ON accounting.charging_order_id = charging_order.id
        WHERE charging_order.del_flag = 0
          AND charging_order.status = 5
          AND charging_order.recharge_payment_status = 2
          AND charging_order.create_time &gt;= #{startTime}
          AND charging_order.create_time &lt; #{endTime}
        <if test="siteIds != null and siteIds.size() > 0">
            AND charging_order.site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
        GROUP BY DATE_FORMAT(charging_order.create_time, '%Y-%m-%d')
        ORDER BY month
    </select>
    <!-- 接口用: 日功率曲线(D 行按 period 分组, period 为 yyyy-MM-dd 字典序可比) -->
    <select id="powerFromStat" resultType="java.util.Map">
        SELECT period AS day,
               COALESCE(SUM(total_power), 0) AS total_power,
               COALESCE(SUM(order_count), 0) AS order_count
        FROM t_large_screen_site_stat
        WHERE stat_type = 'D'
          AND period &gt;= #{dayStart}
          AND period &lt; #{dayEnd}
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
        GROUP BY period
    </select>
    <!-- 接口用: 注册用户数(G 行) -->
    <select id="registeredFromStat" resultType="java.lang.Long">
        SELECT registered_user_count
        FROM t_large_screen_site_stat
        WHERE stat_type = 'G'
          AND period = 'ALL'
          AND site_id = 0
        LIMIT 1
    </select>
    <!-- 接口用: 大屏累计充电量(与 summary 同源: 已结束+已支付订单, M 行汇总) -->
    <select id="capacityFromStat" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(capacity), 0)
        FROM t_large_screen_site_stat
        WHERE stat_type = 'M'
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
    </select>
    <!-- ==================== 加盟商看板聚合查询(SQL 聚合下推,替代全量拉订单到 JVM) ==================== -->
    <!-- 均为单表 GROUP BY,MyCat 下推到各分片表后代理合并少量行;过滤条件内联,不带 IPage 参数。 -->
    <!-- 注意: 本模块 mapUnderscoreToCamelCase=false,列别名显式使用驼峰。 -->
    <!-- MyCat Calcite 规划器限制(2026-08-04 实测,勿改回):
         1) GROUP BY 必须用序号(GROUP BY 1 / 1,2,3),函数表达式如 GROUP BY DATE(end_time)、GROUP BY CASE... 会触发
            ArrayIndexOutOfBoundsException:17 / "is not being grouped" 规划崩溃;
         2) 聚合表达式内禁用 LEAST(),LEAST(ROUND(...)) 会触发 NPE,用嵌套 CASE 等价封顶替代。 -->
    <!-- 按 pay_time 日聚合: 关键数据收入/服务费/总充电电量与管理后台 chargingOrderBaseSummary 保持一致 -->
    <select id="franchiseeDayStats" resultType="com.ruoyi.order.dto.FranchiseeDayStat">
        SELECT DATE(b.pay_time)                       AS statDay,
               COUNT(*)                               AS orderCount,
               SUM(IFNULL(b.order_amount, 0))         AS orderAmount,
               SUM(COALESCE(b.order_amount, b.payment_amount)) AS orderAmountEffective,
               SUM(IFNULL(b.payment_amount, 0))       AS paymentAmount,
               SUM(COALESCE(summary.service_price, CASE
                       WHEN b.order_source = 1 THEN IFNULL(b.service_charge, 0)
                       ELSE IFNULL(b.service_charge, 0) * 0.8
                   END))                               AS serviceFee,
               SUM(COALESCE(summary.electric_price, IFNULL(b.electrovalence, 0))) AS electrovalence,
               SUM(COALESCE(summary.income, IFNULL(b.electrovalence, 0) + CASE
                       WHEN b.order_source = 1 THEN IFNULL(b.service_charge, 0)
                       ELSE IFNULL(b.service_charge, 0) * 0.8
                   END))                               AS chargingIncome,
               SUM(COALESCE(summary.charging_capacity, IFNULL(b.charging_capacity, 0))) AS capacity,
               SUM(IFNULL(b.charging_power, b.power))  AS totalPower,
               COUNT(IFNULL(b.charging_power, b.power)) AS powerCount,
               MAX(IFNULL(IFNULL(b.charging_power, b.power), 0)) AS maxPower,
               SUM(CASE WHEN b.`current` IS NOT NULL AND b.need_elec IS NOT NULL AND b.need_elec &gt; 0
                        THEN CASE WHEN ROUND(b.`current` * 100 / b.need_elec, 4) &gt; 100 THEN 100
                                  ELSE ROUND(b.`current` * 100 / b.need_elec, 4) END ELSE 0 END) AS elecSatSum,
               SUM(CASE WHEN b.`current` IS NOT NULL AND b.need_elec IS NOT NULL AND b.need_elec &gt; 0
                        THEN 1 ELSE 0 END)             AS elecSatCount,
               SUM(CASE WHEN IFNULL(IFNULL(b.charging_power, b.power), 0) &gt; 0
                        THEN IFNULL(IFNULL(b.charging_power, b.power), 0) ELSE 0 END) AS utilPowerSum,
               SUM(CASE WHEN IFNULL(IFNULL(b.charging_power, b.power), 0) &gt; 0
                        THEN 1 ELSE 0 END)             AS utilPowerCount
        FROM `charging_pile_order`.t_charging_order b
        LEFT JOIN `charging_pile_order`.t_charging_order_summary_data summary
          ON summary.charging_order_id = b.id
        WHERE b.del_flag = 0
          AND b.recharge_payment_status = 2
          AND b.start_time IS NOT NULL
          AND b.end_time IS NOT NULL
        <if test="siteIds != null and siteIds.size() > 0">
            AND b.site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
          AND b.pay_time &gt;= #{start}
          AND b.pay_time &lt; #{end}
        GROUP BY 1
    </select>
    <!-- 功率分布直方图(0-30/30-60/60-120/120-300/300kW以上) -->
    <select id="franchiseePowerBuckets" resultType="com.ruoyi.order.dto.FranchiseePowerBucket">
        SELECT CASE WHEN IFNULL(IFNULL(charging_power, power), 0) &lt; 30 THEN 0
                    WHEN IFNULL(IFNULL(charging_power, power), 0) &lt; 60 THEN 1
                    WHEN IFNULL(IFNULL(charging_power, power), 0) &lt; 120 THEN 2
                    WHEN IFNULL(IFNULL(charging_power, power), 0) &lt; 300 THEN 3
                    ELSE 4 END AS bucket,
               COUNT(*)        AS orderCount
        FROM t_charging_order
        WHERE del_flag = 0
          AND recharge_payment_status = 2
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
          AND end_time &gt;= #{start}
          AND end_time &lt; #{end}
        GROUP BY 1
    </select>
    <!-- 尖峰平谷阶段电量: 按 charging_order_id 分片键路由,分块传入避免超大 IN -->
    <select id="franchiseePhaseStatsByOrderIds" resultType="com.ruoyi.order.dto.FranchiseePhaseStat">
        SELECT `type` AS type,
               SUM(IFNULL(charging_capacity, 0)) AS capacity
        FROM t_charging_order_accounting_strategy
        WHERE charging_order_id IN
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        GROUP BY `type`
    </select>
    <!-- 站点+时间范围内订单 id 列表(仅 id 投影) -->
    <select id="franchiseeOrderIds" resultType="java.lang.Long">
        SELECT id
        FROM t_charging_order
        WHERE del_flag = 0
          AND recharge_payment_status = 2
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
          AND end_time &gt;= #{start}
          AND end_time &lt; #{end}
    </select>
    <!-- 用户维度聚合(仅 status=5 已结束订单) -->
    <select id="franchiseeUserAggs" resultType="com.ruoyi.order.dto.FranchiseeUserAgg">
        SELECT app_user_id              AS appUserId,
               app_user_car_id          AS appUserCarId,
               DATE(end_time)           AS statDay,
               COUNT(*)                 AS orderCount,
               SUM(IFNULL(charging_capacity, 0)) AS capacity,
               SUM(IFNULL(payment_amount, 0))     AS paymentAmount
        FROM t_charging_order
        WHERE del_flag = 0
          AND recharge_payment_status = 2
          AND status = 5
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
          AND end_time &gt;= #{start}
          AND end_time &lt; #{end}
        GROUP BY 1, 2, 3
    </select>
    <!-- 加盟商站点"新增用户"识别:统计窗内每用户首次充电成功日(MIN(DATE(end_time)),与 franchiseeUserAggs 同过滤条件) -->
    <select id="franchiseeUserFirstChargedDays" resultType="com.ruoyi.order.dto.FranchiseeUserFirstCharge">
        SELECT app_user_id                AS appUserId,
               MIN(DATE(end_time))        AS firstChargedDay
        FROM t_charging_order
        WHERE del_flag = 0
          AND recharge_payment_status = 2
          AND status = 5
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
          AND end_time &gt;= #{start}
          AND end_time &lt; #{end}
        GROUP BY app_user_id
    </select>
    <!-- 流量来源统计(仅 status=5 已结束订单,空值归"明星充电"由服务端处理) -->
    <select id="franchiseeTrafficStats" resultType="com.ruoyi.order.dto.FranchiseeSourceStat">
        SELECT IFNULL(tripartite_platform_name, '') AS source,
               COUNT(*)                             AS orderCount
        FROM t_charging_order
        WHERE del_flag = 0
          AND recharge_payment_status = 2
          AND status = 5
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
          AND end_time &gt;= #{start}
          AND end_time &lt; #{end}
        GROUP BY tripartite_platform_name
    </select>
    <!-- 按充电枪的需求满足率聚合(与 franchiseeDayStats 相同基础过滤,不含 status 过滤) -->
    <select id="franchiseeGunSatisfaction" resultType="com.ruoyi.order.dto.FranchiseeGunSat">
        SELECT charging_gun_id AS chargingGunId,
               SUM(CASE WHEN `current` IS NOT NULL AND need_elec IS NOT NULL AND need_elec &gt; 0
                        THEN CASE WHEN ROUND(`current` * 100 / need_elec, 4) &gt; 100 THEN 100
                                  ELSE ROUND(`current` * 100 / need_elec, 4) END ELSE 0 END) AS satSum,
               SUM(CASE WHEN `current` IS NOT NULL AND need_elec IS NOT NULL AND need_elec &gt; 0
                        THEN 1 ELSE 0 END)          AS satCount
        FROM t_charging_order
        WHERE del_flag = 0
          AND recharge_payment_status = 2
        <if test="siteIds != null and siteIds.size() > 0">
            AND site_id IN
            <foreach collection="siteIds" item="siteId" open="(" separator="," close=")">
                #{siteId}
            </foreach>
        </if>
          AND end_time &gt;= #{start}
          AND end_time &lt; #{end}
        GROUP BY charging_gun_id
    </select>
</mapper>