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

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

import java.util.concurrent.ConcurrentHashMap;

/**
 * 提现结果匹配
 *
 * @author HH 2022/4/1
 */
public class DrawCashReplyMapping {

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

    public static void putReply(Event event) {
        String data = event.getData();
        DrawCashReply reply = JSON.parseObject(data, DrawCashReply.class);
        Assert.hasText(reply.getOrderNo(), "orderNo is blank !!!");
        DrawCashCallback callback = PAY_RESULT_CALLBACK_MAP.remove(reply.getOrderNo());
        if (callback != null) {
            callback.onReply(reply);
        }
    }

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