package com.kym.service.wechat.impl;

import com.kym.entity.PayLog;
import com.kym.service.cache.KymCache;
import com.kym.service.wechat.TemplateParamBuilder;

import java.util.Map;

/**
 * 充值支付成功消息模板参数构建器
 */
public class PaymentSuccessParamBuilder implements TemplateParamBuilder {

    // 模板键常量定义
    private static final String KEY_OUT_TRADE_NO = "character_string4";
    private static final String KEY_STATION_NAME = "thing9";
    private static final String KEY_PAY_AMOUNT = "amount1";
    private static final String KEY_RECHARGE_AMOUNT = "amount2";
    private static final String KEY_SUCCESS_TIME = "time5";
    @Override
    public Map<String, String> buildParams(Object... args) {
        if (args.length < 2 || !(args[0] instanceof PayLog)) {
            throw new IllegalArgumentException("需要传入 PayLog 和 Integer 类型的参数");
        }

        PayLog payLog = (PayLog) args[0];
        Object rechargeAmountObj = args[1];

        if (!(rechargeAmountObj instanceof Number)) {
            throw new IllegalArgumentException("第二个参数必须是数字类型");
        }

        int rechargeAmount = ((Number) rechargeAmountObj).intValue();

        String outTradeNo = payLog.getOutTradeNo();
        String stationName = KymCache.INSTANCE.getUserStationName(payLog.getUserId());
        String payAmount = formatAmount(payLog.getTotal());
        String rechargeAmountStr = formatAmount(rechargeAmount);
        String successTime = payLog.getSuccessTime() != null ? payLog.getSuccessTime().format(formatter) : "";

        return Map.of(
                KEY_OUT_TRADE_NO, outTradeNo,
                KEY_STATION_NAME, stationName != null ? stationName : "",
                KEY_PAY_AMOUNT, payAmount,
                KEY_RECHARGE_AMOUNT, rechargeAmountStr,
                KEY_SUCCESS_TIME, successTime
        );
    }
}
