package com.cyl.tencent;

import com.cyl.manager.oms.domain.Order;
import com.ruoyi.common.utils.http.HttpUtils;
import com.wechat.pay.java.core.util.GsonUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;

public class TencentIpaasUtil {
    public static String url = "https://3124764-1300957566.ipaas.sandbox.myqcloud.com/orderPay";
    public static String order = "{\n" +
            "    \"phone\":  \"18637610959\",\n" +
            "    \"wechatId\": \"Wechatid123\",\n" +
            "    \"openId\": \"o44bE68_2A0BSmbM8mIcOXu8ROOc\",\n" +
            "    \"note\": \"this is a test\",\n" +
            "    \"totalPrice\": \"120\",\n" +
            "    \"address\": {\n" +
            "        \"province\": \"上海市\",\n" +
            "        \"city\": \"上海市\",\n" +
            "        \"district\": \"黄浦区\",\n" +
            "        \"detailAddress\": \"123\"\n" +
            "    },\n" +
            "    \"skuList\":[\n" +
            "        {\n" +
            "            \"skuId\": \"403\",\n" +
            "            \"quantity\": 1\n" +
            "        },{\n" +
            "            \"skuId\": \"404\",\n" +
            "            \"quantity\": 3\n" +
            "        }\n" +
            "    ],\n" +
            "    \"service\":{\n" +
            "        \"serviceFrequency\": \"once_a_day\",\n" +
            "        \"serviceDate\": [\"2024-01-01\",\"2024-01-01\"],\n" +
            "        \"serviceTimeFirst\":\"10:00\",\n" +
            "        \"serviceTimeSecond\":\"16:00\",\n" +
            "        \"pet\": [\n" +
            "            {\n" +
            "                \"type\": \"large_dog\",\n" +
            "                \"quantity\": 2\n" +
            "            }\n" +
            "        ]\n" +
            "    }\n" +
            "}";

    public static String notify(Order orderVO) {
        String result = HttpUtils.sendPost(url, GsonUtil.toJson(orderVO));
        return result;
    }


    public static void main(String[] args) {
        String result = sendPost(url, order);
        System.out.println(result);
    }

    public static String sendPost(String url, String param)
    {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try
        {
            String urlNameString = url;
            System.out.println("sendPost - {}" + urlNameString);
            URL realUrl = new URL(urlNameString);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null)
            {
                result.append(line);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (out != null)
                {
                    out.close();
                }
                if (in != null)
                {
                    in.close();
                }
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        return result.toString();
    }
}
