package com.m2pool.lease.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * @Description 支付工具类
 * @Date 2025/8/8 11:05
 * @Author yyb
 */
public class HttpPayUtils {

    private static final String URI = "http://127.0.0.1:12345";

    public static final String PAY_ETH = "/checkPayment/eth";
    /**
     * 发送 GET 请求
     * @param params 请求参数，键值对形式
     * @return 响应结果字符串
     */
    public static String doGet(String method,Map<String, Object> params) {
        String URL = URI + method;
        if (params != null && !params.isEmpty()) {
            return HttpUtil.get(URL, params);
        }
        return HttpUtil.get(URL);
    }
    /**
     * 发送 POST 请求，使用 JSON 格式提交数据
     * @param json 请求的 JSON 数据
     * @return 响应结果字符串
     */
    public static PaymentDto doPost(String method,String json) {
        String URL = URI + method;
        HttpResponse response = HttpRequest.post(URL)
                .header("Content-Type", "application/json")
                .body(json)
                .execute();
        System.out.println("支付回调结果"+response);
        if (response.body() != null && !response.body().isEmpty()) {
          return  JSONUtil.toBean(response.body(), PaymentDto.class);
        }
        return null;
    }


    @Data
    public class PaymentDto {
        private int code;
        private boolean result;
        private List<PaymentData> data;
    }


    @Data
    public class PaymentData {
        private String blockNumber;
        //大于等于支付订单的时间
        private String timeStamp;
        //交易id
        private String hash;
        private String nonce;
        private String blockHash;
        //发送者钱包地址
        private String from;
        private String contractAddress;
        // 接收者钱包地址
        private String to;
        // 转账金额一致
        private String value;
        private String tokenName;
        private String tokenSymbol;
        private String tokenDecimal;
        private String transactionIndex;
        private String gas;
        private String gasPrice;
        private String gasUsed;
        private String cumulativeGasUsed;
        private String input;
        private String methodId;
        private String functionName;
        private String confirmations;
    }
}
