package com.rf.psychological;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.utils.ExcelUtil;
import lombok.SneakyThrows;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import com.alibaba.fastjson.JSONObject;

/**
 * 压力测试
 * @author Administrator
 *
 */
public class TestPush {
	
	
	public static void main(String[] args)throws Exception {
		
		File file = new File("C:/Users/Administrator/Desktop/弹型导入模板.xlsx");
		String fileName = "弹型导入模板.xlsx";
        FileInputStream fileInputStream = new FileInputStream(file);
		List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(fileInputStream, fileName);
        //获取题目信息并入库
        List<List<Object>> sheet0 = datas.get(0);
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (int i = 0;i <sheet0.size();i++){
            List<Object> scales = sheet0.get(i);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("institutionNo",scales.get(0));
            jsonObject.put("phone",scales.get(1));
            jsonObject.put("password", "205db300b8a350ba594747ff58fa94f1");
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("data", AesEncryptUtils.encrypt(jsonObject.toJSONString()));
            jsonObjects.add(jsonObject2);
        }

        /*for (JSONObject obj:jsonObjects) {
        //创建线程登录
            MyThread t = new MyThread(obj);
            t.start();
        }*/
		//登录
    	/*String loginUrl = "https://www.jue-ming.com:8848/user/login";
    	JSONObject jsonObject = new JSONObject();
    	//{"phone":"test","password":"e10adc3949ba59abbe56e057f20f883e","institutionNo":"10000"}
    	jsonObject.put("phone", "test");
    	jsonObject.put("password", "96e79218965eb72c92a549dd5a330112");
    	jsonObject.put("institutionNo", "10000");
    	JSONObject jsonObject2 = new JSONObject();
    	jsonObject2.put("data",AesEncryptUtils.encrypt(jsonObject.toJSONString()));



		String response = sendPost(jsonObject2, loginUrl);
		System.out.println(response);
		String respStringDecrypt = AesEncryptUtils.decrypt(response.substring(1, response.length()-2));
		System.out.println(respStringDecrypt);
		JSONObject jsonObject3 = JSONObject.parseObject(respStringDecrypt);
		String token = jsonObject3.getJSONObject("data").getString("token");
		System.out.println(token);
		System.out.println("-------------------------------------------------");
		String url = "https://www.jue-ming.com:8848/category/getCategoryList?institutionNo=10000&gId=0";
		String getResponse = sendRequest("Bearer "+token,url,"GET");
		System.out.println(getResponse);*/
	}
	
	
	
	/**
	 * 发送POST请求
	 * @param json
	 * @param URL
	 * @return
	 */
    public static String sendPost(JSONObject json,String URL) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(URL);
        post.setHeader("Content-Type", "application/json");
        post.addHeader("Authorization", "Basic YWRtaW46");
        String result = "";
        try {
            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);
            // ��������
            HttpResponse httpResponse = client.execute(post);
            // ��ȡ��Ӧ������
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                strber.append(line + "\n");
            }
            inStream.close();
            result = strber.toString();
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                System.out.println("---------------------");
            } else {
                System.out.println("---------------------------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 发送GET请求
     * @param urlParam
     * @param requestType
     * @return
     */
    public static String sendRequest(String cookie ,String urlParam,String requestType) {

        HttpURLConnection con = null;  

        BufferedReader buffer = null; 
        StringBuffer resultBuffer = null;  
//        String cookie = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwZXROYW1lIjoi5rWL6K-V5Lq65ZGYIiwidXNlclBob25lIjoidGVzdCIsImdyb3VwSWQiOiIwIiwidHlwZSI6InVzZXIiLCJleHAiOjE2NTMyODUyMzMsInVzZXJJZCI6ImZmODA4MTgxN2M2ZDNkODYwMTdjNmQ0NzE2YjMwMDAxIiwiaW5zdGl0dXRpb25ObyI6IjEwMDAwIn0.xyNl0_6VTB6d7edTFPwHucK7VQiHwxx20kpbgG2MWRM";
        try {
            URL url = new URL(urlParam); 
            //�õ����Ӷ���
            con = (HttpURLConnection) url.openConnection();
            con.addRequestProperty("Authorization", cookie);
            //������������
            con.setRequestMethod(requestType);  
            //����������Ҫ���ص��������ͺ��ַ�������
            con.setRequestProperty("Content-Type", "application/json;charset=utf-8");  
            //����д��
            con.setDoOutput(true);
            //�������
            con.setDoInput(true);
            //��ʹ�û���
            con.setUseCaches(false);
            //�õ���Ӧ��
            int responseCode = con.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                //�õ���Ӧ��
                InputStream inputStream = con.getInputStream();
                //����Ӧ��ת�����ַ���
                resultBuffer = new StringBuffer();
                String line;
                buffer = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                while ((line = buffer.readLine()) != null) {
                    resultBuffer.append(line);
                }
                return resultBuffer.toString();
            }

        }catch(Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    
    static class MyThread extends Thread{
        JSONObject jsonObject2;
        public MyThread(JSONObject jsonObject2){
            this.jsonObject2 = jsonObject2;
        }
    	@SneakyThrows
        @Override
    	public void run() {
    		// TODO Auto-generated method stub
    		super.run();
            String loginUrl = "https://www.jue-ming.com:8848/user/login";
            String response = sendPost(jsonObject2, loginUrl);
            String respStringDecrypt = AesEncryptUtils.decrypt(response.substring(1, response.length()-2));
            JSONObject jsonObject3 = JSONObject.parseObject(respStringDecrypt);
            String token = jsonObject3.getJSONObject("data").getString("token");
            System.out.println(token);
            System.out.println("-------------------------------------------------");
            String url = "https://www.jue-ming.com:8848/category/getCategoryList?institutionNo=10000&gId=0";
            String getResponse = sendRequest("Bearer "+token,url,"GET");
            System.out.println(AesEncryptUtils.decrypt(getResponse));
    	}
    }

}
