package com.kym.service.wechat.impl;

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

import java.util.Map;

/**
 * 退款成功消息模板参数构建器
 */
public class RefundSuccessParamBuilder implements TemplateParamBuilder {
    private static final String REFUND_AMOUNT = "amount1";
    private static final String BALANCE = "amount2";
    private static final String REFUND_TIME = "time6";
    private static final String USER_STATION = "thing7";

    @Override
    public Map<String, String> buildParams(Object... args) {
        if (args.length < 2 || !(args[0] instanceof RefundLog refundLog)) {
            throw new IllegalArgumentException("需要传入有效的参数");
        }

        Object balanceObj = args[1];
        if (!(balanceObj instanceof Integer)) {
            throw new IllegalArgumentException("第二个参数必须为整数类型");
        }

        int balance = ((Number) balanceObj).intValue();

        String refundAmount = formatAmount(refundLog.getRefund());
        String balanceStr = formatAmount(balance);
        String refundTime = refundLog.getSuccessTime() != null ? refundLog.getSuccessTime().format(formatter) : "";
        String userStation = KymCache.INSTANCE.getUserStationName(refundLog.getUserId());

        return Map.of(
                REFUND_AMOUNT, refundAmount,
                BALANCE, balanceStr,
                REFUND_TIME, refundTime,
                USER_STATION, userStation != null ? userStation : "未知站点"
        );
    }
}
