package com.itheima.file;

import com.alibaba.fastjson.JSONObject;
import com.itheima.file.pojo.FatherSon;
import com.itheima.file.pojo.Org;
import com.itheima.file.pojo.Person;
import com.itheima.file.service.FatherSonService;
import com.itheima.file.service.OrgService;
import com.itheima.file.service.PersonService;
import com.itheima.file.util.HTTPHelper;
import com.itheima.file.util.HttpClientUtils;
import com.itheima.file.util.SslUtils;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URISyntaxException;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApiTestData {

    @Autowired
    private OrgService orgService;
    @Autowired
    private PersonService personService;
    @Autowired
    private FatherSonService fatherSonService;

    @Test
    public void testFSApi() {
        String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/JTOA/getlist";
        String username = "po_soap";
        String password = "z12345678";
        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("app_id", "hcmcloud");
        reqBodyObj.put("table_name", "USER_ORG_RELATION");
        reqBodyObj.put("stringDate", "20210301");
        reqBodyObj.put("condition", "1=1");
        reqBodyObj.put("secretKey", "f3ac852124d2f991a3f63f2e33dae18b");
        JSONObject jsonObject = HttpClientUtils.doPost(postUrl, reqBodyObj, username, password);
        System.out.println(jsonObject);
        List<FatherSon> list = (List<FatherSon>) jsonObject.get("data");
        fatherSonService.saveBatch(list);
    }

    @Test
    public void testPersonApi() throws URISyntaxException {
        // 设置Basic Authentication的凭证（用户名和密码）
        String username = "po_soap";
        String password = "z12345678";
        final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("httpbin.org", 80),
                new UsernamePasswordCredentials(username, password.toCharArray()));

        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("app_id", "hcmcloud");
        reqBodyObj.put("table_name", "");
        reqBodyObj.put("stringDate", "19000101");
        reqBodyObj.put("condition", "1=1");
        reqBodyObj.put("secretKey", "f3ac852124d2f991a3f63f2e33dae18b");

        String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/GetAssHrTreeObj";

        // 设置HTTP头部
        LinkedHashMap<String, String> header = new LinkedHashMap<>();
        // header.put("Authorization", auth);
        header.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));

        // header.put("Authorization", baseAuth);
        header.put("Content-Type", "application/json");
        String string = reqBodyObj.toString();
        // 发送请求（这里假设HTTPHelper.sendPOST方法支持自定义头部）
        try {
            SslUtils.ignoreSsl();
            String result = HTTPHelper.sendPOST(postUrl, string, header, "UTF-8");
            System.out.println(result);
            // String 转 Map
            JSONObject resultObj = JSONObject.parseObject(result);
            List<Person> list = (List<Person>) resultObj.get("data");
            System.out.println(list);
            personService.saveBatch(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Test
    public void testOrgApi() throws URISyntaxException {
        // 设置Basic Authentication的凭证（用户名和密码）
        String username = "po_soap";
        String password = "z12345678";
        final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("httpbin.org", 80),
                new UsernamePasswordCredentials(username, password.toCharArray()));

        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("app_id", "hcmcloud");
        reqBodyObj.put("table_name", "");
        reqBodyObj.put("stringDate", "19000101");
        reqBodyObj.put("condition", "1=1");
        reqBodyObj.put("secretKey", "f3ac852124d2f991a3f63f2e33dae18b");

        String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/GetAssHrTreeGxb";

        // 设置HTTP头部
        LinkedHashMap<String, String> header = new LinkedHashMap<>();
        // header.put("Authorization", auth);
        header.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));

        // header.put("Authorization", baseAuth);
        header.put("Content-Type", "application/json");
        String string = reqBodyObj.toString();
        // 发送请求（这里假设HTTPHelper.sendPOST方法支持自定义头部）
        try {
            SslUtils.ignoreSsl();
            String result = HTTPHelper.sendPOST(postUrl, string, header, "UTF-8");
            System.out.println(result);
            // String 转 Map
            JSONObject resultObj = JSONObject.parseObject(result);
            List<Org> list = (List<Org>) resultObj.get("data");
            System.out.println(list);
            orgService.saveBatch(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
