package com.miaxis.test;

import org.apache.commons.io.IOUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Test03 {


    public static void main(String[] args) throws IOException {
       // test1();

        String token = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6Ijc1MzRjZGI3LTcyZGMtNGExZi1hYTc1LWFhYjlmYmYzMGI5YyJ9.iKl7-pr-mrkKNf-YRoSC86uRoZHCapTtM94PMvOz7mQAaSV32FsPqBdusA73p0jkn6HZMrZDcXTmKFmY-_9PAg";
        test4();

    }

    /**
     * 登录接口
     * @throws IOException
     */
    public static void test1() throws IOException {
        URL url = new URL("https://jsjp-admin1.zzxcx.net/jsjp-admin/login/noCode");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setReadTimeout(20000);
        conn.setConnectTimeout(20000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("connection", "Keep-Alive");
        //  conn.setRequestProperty("Accept","application/json");// 设置接收数据的格式
        conn.setRequestProperty("Content-Type", "application/json;");
        conn.connect();
        OutputStream out = conn.getOutputStream();
        // 把数据写入请求的Body
        String json = "{\"password\": \"jishi2023\",\"username\": \"jishi\"}";

        out.write(json.getBytes("utf-8"));
        out.flush();
        out.close();
        // 从服务器读取响应
        InputStream inputStream = conn.getInputStream();
        String encoding = "utf-8";
        String body = IOUtils.toString(inputStream, encoding);
        System.out.println(body);
        conn.disconnect();
    }

    public static void test2(String outTradeNo,String token) throws IOException {

        //String token = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImIzZjQ2MjgwLTA0OTAtNGI5Zi1iNGI1LTE3NzU5OTJlYTQ1YiJ9.5-Ho3p7WH2LOVLo5KVquos26obPQMf3xtMGYAm60jSAelg4QibTWbhfReppL-RrG4M3NLunlCy9WXMjZn8ePjQ";
        String urls = "https://jsjp-admin1.zzxcx.net/jsjp-admin/order/info/refund/"+outTradeNo;
        URL url = new URL(urls);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Authorization", token);
        conn.connect();
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        // 把数据写入请求的Body
        String urlParameters  = "reason=" + URLEncoder.encode("退学退款", "UTF-8");
        out.write(urlParameters);
        out.flush();
        out.close();
        // 从服务器读取响应
        InputStream inputStream = conn.getInputStream();
        String encoding = "utf-8";
        String body = IOUtils.toString(inputStream, encoding);
        System.out.println(body);
        conn.disconnect();

    }


    public static void test3(String token) throws IOException {

        Map<String,Object> param = new HashMap<String,Object>();
        param.put("dqbh", URLEncoder.encode(String.valueOf(3501), "UTF-8"));
        param.put("startTime", URLEncoder.encode(String.valueOf("2023-02-01 00:00:00"), "UTF-8"));
        param.put("endTime", URLEncoder.encode(String.valueOf("2023-02-28 23:59:59"), "UTF-8"));
        param.put("isShare", 1);
        param.put("tradeType", 1);


        String urls = "https://jsjp-admin1.zzxcx.net/jsjp-admin/order/info/school/list";
        StringBuffer paramBuffer = new StringBuffer();

        if(param!=null) {
            int i = 0;
            for (String key : param.keySet()) {
                if (i == 0) {
                    paramBuffer.append("?");
                } else {
                    paramBuffer.append("&");
                }
                paramBuffer.append(key).append("=").append(param.get(key));
                i++;
            }
            urls += paramBuffer;
        }


        URL url = new URL(urls);
        System.out.println(url.toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", token);
        conn.connect();
        int responseCode = conn.getResponseCode();
        System.out.println("Response Code: " + responseCode);



        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        System.out.println("Response Body: " + response.toString());


    }


    public static void test4() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date startOfMonth = calendar.getTime();

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date endOfMonth = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Start of Month: " + sdf.format(startOfMonth));
        System.out.println("End of Month: " + sdf.format(endOfMonth));
    }

}
