package com.ghy.common.adapay.callback.mapping;

import com.alibaba.fastjson.JSON;
import com.ghy.common.adapay.callback.Event;
import com.ghy.common.adapay.callback.RefundCallback;
import com.ghy.common.adapay.callback.model.RefundReply;
import org.springframework.util.Assert;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 退款结果匹配
 *
 * @author HH 2022/3/29
 */
public class RefundReplyMapping {

    /**
     * 临时保存退款结果
     * key: orderNo
     * value: 处理支付结果的回调接口
     */
    private final static ConcurrentHashMap<String, RefundCallback> PAY_RESULT_CALLBACK_MAP = new ConcurrentHashMap<>(1024);

    public static void putReply(Event event) {
        String data = event.getData();
        RefundReply payment = JSON.parseObject(data, RefundReply.class);
        Assert.hasText(payment.getPaymentId(), "paymentId is blank !!!");
        RefundCallback callback = PAY_RESULT_CALLBACK_MAP.remove(payment.getPaymentId());
        if (callback != null) {
            callback.onReply(payment);
        }
    }

    public static void putCallback(String paymentId, RefundCallback callback) {
        Assert.hasText(paymentId, "paymentId is blank !!!");
        Assert.notNull(callback, "RefundCallback is null !!!");
        PAY_RESULT_CALLBACK_MAP.put(paymentId, callback);
    }
}
