package com.jeesharp.business.modules.finance.payorder.support;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.finance.payorder.entity.PayOrder;

/**
 * 财务支付单微信路由快照辅助：判断 PARTNER 模式及是否具备收付通后置结算前提。
 *
 * @author jeesharp
 * @since 2026-06-12
 */
public final class PayOrderWechatRouteSupport {

    private static final String WECHAT_PAY_MODE_PARTNER = "PARTNER";
    private static final String ROUTE_PARTNER = "PARTNER";
    private static final String ROUTE_COMBINE = "COMBINE";

    private PayOrderWechatRouteSupport() {
    }

    /**
     * 是否为收付通合作伙伴模式（L1 PARTNER）。
     */
    public static boolean isPartnerMode(PayOrder payOrder) {
        if (payOrder == null) {
            return false;
        }
        return WECHAT_PAY_MODE_PARTNER.equalsIgnoreCase(StrUtil.blankToDefault(payOrder.getWechatPayMode(), ""));
    }

    /**
     * 解析支付单快照的微信模式；历史单未写入时默认 DIRECT。
     */
    public static String resolveWechatPayMode(PayOrder payOrder) {
        if (payOrder == null || StrUtil.isBlank(payOrder.getWechatPayMode())) {
            return "DIRECT";
        }
        return payOrder.getWechatPayMode().trim().toUpperCase();
    }

    /**
     * 是否具备 partner/合单支付流水（后置应收、分账须满足）。
     */
    public static boolean hasPartnerPayRoute(PayOrder payOrder) {
        if (!isPartnerMode(payOrder)) {
            return false;
        }
        String routeType = StrUtil.blankToDefault(payOrder.getPayRouteType(), "").trim().toUpperCase();
        if (ROUTE_COMBINE.equals(routeType)) {
            return StrUtil.isNotBlank(payOrder.getCombineOutTradeNo())
                    || StrUtil.isNotBlank(payOrder.getOutTradeNo());
        }
        if (ROUTE_PARTNER.equals(routeType)) {
            return StrUtil.isNotBlank(payOrder.getSubMchId());
        }
        return false;
    }
}
