puzhibing
2026-09-30 64464568546b827fc5778a20efb347bf56ba34ac
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TVinChargingOrderServiceImpl.java
@@ -901,6 +901,8 @@
                accountUpdate.setAccountMode(chargeContext.getAccountMode());
                accountUpdate.setLockedAmount(chargeContext.getDispatchAmount());
                accountUpdate.setMaxEndTime(chargeContext.getMaxEndTime());
                // 企业优惠快照落库(建单时固化,结算/补偿优先读快照,抵抗公司优惠设置后续变更)
                chargeAccountSupport.fillPreferentialSnapshot(accountUpdate, vinRecord.getCompanyId());
                updateById(accountUpdate);
                // 同步内存对象,保证后续失败回滚能按账户模式/锁定额释放额度
                order.setAccountMode(chargeContext.getAccountMode());
@@ -1150,20 +1152,6 @@
    private PeriodTypeWindow findCurrentPeriodType(Integer strategyId, LocalTime current) {
        ChargeAccountSupport.PeriodTypeWindow period = chargeAccountSupport.findCurrentPeriodType(strategyId, current);
        return period == null ? null : new PeriodTypeWindow(period.getType(), period.getEndTime());
    }
    /**
     * 查询企业优惠设置(失败或未配置返回 null,按不优惠结算;实现见公共支持类)
     */
    private CompanyPreferentialDTO loadCompanyPreferential(Integer companyId) {
        return chargeAccountSupport.loadCompanyPreferential(companyId);
    }
    /**
     * 按优惠方式加载计费模板(仅方式3需要,取不到时该段按原价结算;实现见公共支持类)
     */
    private TBillingTemplateDTO loadBillingTemplate(CompanyPreferentialDTO preferential) {
        return chargeAccountSupport.loadBillingTemplate(preferential);
    }
    /**
@@ -1528,9 +1516,10 @@
        snapshots.sort(Comparator.comparing(TVinAccountingStrategyDetailOrder::getStartTime,
                Comparator.nullsLast(String::compareTo)));
        // 企业优惠设置与计费模板(1=不优惠,2=服务费按比例优惠,3=按计费模板优惠单价收费)
        CompanyPreferentialDTO preferential = loadCompanyPreferential(order.getCompanyId());
        TBillingTemplateDTO billingTemplate = loadBillingTemplate(preferential);
        // 企业优惠设置与计费模板(1=不优惠,2=服务费按比例优惠,3=按计费模板优惠单价收费):
        // 订单快照优先(建单时固化,策略后续变更不影响本单结算口径),快照为空的历史订单实时查询并在本事务回填
        ChargeAccountSupport.PreferentialSnapshot preferential = chargeAccountSupport.resolvePreferential(order);
        TBillingTemplateDTO billingTemplate = preferential == null ? null : preferential.getTemplate();
        BigDecimal electrovalenceTotal = BigDecimal.ZERO;
        BigDecimal serviceChargeTotal = BigDecimal.ZERO;
@@ -1630,6 +1619,8 @@
        final BigDecimal discountSvcTotal = discountServiceTotal;
        final BigDecimal startSoc = getMonitoringSocQuietly(code, "asc");
        final BigDecimal endSoc = getMonitoringSocQuietly(code, "desc");
        final ChargeAccountSupport.PreferentialSnapshot preferentialSnapshot = preferential;
        final boolean preferentialBackfill = preferentialSnapshot != null && preferentialSnapshot.needBackfill();
        // 本地事务:写计费明细 + 更新订单(状态5;账户结算在事务外执行,避免长事务占用链接)
        transactionTemplate.execute(status -> {
@@ -1663,6 +1654,15 @@
            if (order.getEndMode() == null) {
                update.setEndMode(1);
            }
            if (preferentialBackfill) {
                // 历史订单(快照为空)实时查询结果回填订单快照列,保证后续补偿重算口径一致
                update.setPreferentialType(preferentialSnapshot.getPreferentialType());
                update.setServiceDiscountRate(preferentialSnapshot.getServiceDiscountRate());
                update.setBillingTemplateId(preferentialSnapshot.getBillingTemplateId());
                update.setBillingTemplateSnapshot(
                        ChargeAccountSupport.serializeTemplateSnapshot(preferentialSnapshot.getTemplate()));
                update.setPreferentialSnapshotTime(LocalDateTime.now());
            }
            updateById(update);
            return true;
        });