package com.sqx.modules.pay.wuyou;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author GYJ
 */
public class WuyouPay {

    static String NOTIFY_URL = "";
    static String EXTRACT_NOTIFY_URL = "";

    private static final Logger logger = LoggerFactory.getLogger(WuyouPay.class);

    public static void setNotifyUrl(String notifyUrl) {
        NOTIFY_URL = notifyUrl;
    }

    public static void setExtractNotifyUrl(String extractNotifyUrl) {
        EXTRACT_NOTIFY_URL = extractNotifyUrl;
    }

    public static BaseResp payOrder(String orderNo, String amount, String userAgent) {
        Map<String, Object> params = getBaseParams();
        params.put("type", "6001");
        params.put("is_code", "1");
        params.put("out_trade_no", orderNo);
        params.put("total", amount);
        params.put("notify_url", NOTIFY_URL);

        String sign = Encrypt.getParamsSign(params);
        params.put("sign", sign);

        String body = request(Constants.PAY_URL, params, userAgent);

        return JSONObject.parseObject(body, BaseResp.class);
    }

    public static BaseResp queryOrder(String orderNo, String amount, String userAgent) {
        Map<String, Object> params = getBaseParams();
        params.put("out_trade_no", orderNo);
        params.put("total", amount);

        String sign = Encrypt.getParamsSign(params);
        params.put("sign", sign);

        String body = request(Constants.QUERY_URL, params, userAgent);
        return JSONObject.parseObject(body, BaseResp.class);
    }

    /**
     * 提现
     * @param account 支付宝账号
     * @param userName  支付宝名称
     * @return
     */
    public static BaseResp extractOrder(String outOrderNo, String amount, String account, String userName) {
        Map<String, Object> params = getBaseParams();

        params.put("out_trade_no", outOrderNo);
        params.put("total", amount);
        params.put("bank_card", account);
        params.put("bank_account_name", userName);
        params.put("bank_name", "1");
        params.put("bank_branch", "1");
        params.put("province", "1");
        params.put("city", "1");
        params.put("notify_url", EXTRACT_NOTIFY_URL);

        String sign = Encrypt.getParamsSign(params);
        params.put("sign", sign);

        params.put("business_type", 0);
        params.put("business_attr", "alipay");

        String body = request(Constants.EXTRACT_URL, params, "");
        return JSONObject.parseObject(body, BaseResp.class);
    }

    public static BaseResp queryExtractOrder(String outOrderNo, String amount) {
        Map<String, Object> params = getBaseParams();
        params.put("out_trade_no", outOrderNo);
        params.put("total", amount);

        String sign = Encrypt.getParamsSign(params);
        params.put("sign", sign);

        String body = request(Constants.EXTRACT_QUERY_URL, params, "");
        return JSONObject.parseObject(body, BaseResp.class);
    }

    private static Map<String, Object> getBaseParams() {
        Map<String, Object> params = new HashMap<>();
        params.put("mch_id", Constants.MERCHANT_ID);
        params.put("timestamp", System.currentTimeMillis() / 1000);

        return params;
    }

    private static String request(String url, Map<String, Object> params, String userAgent) {
        String body = HttpRequest.post(url)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .header("User-Agent", userAgent)
                .body(JSONObject.toJSONString(params))
                .execute()
                .body();

        logger.info("无忧支付 request url: {}, params: {}, response: {}", url, params, body);

        return body;
    }

    public static void main(String[] args) {
        payOrder("20221118123456794", "0.1", "Mozilla/5.0");
//        queryOrder("20221118123456791", "0.1", "Mozilla/5.0");

        extractOrder("20221118123456793", "1", "1157756119@qq.com", "巩奕杰");
//        queryExtractOrder("20221118123456793", "1");
    }
}
