package com.hzya.frame;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.util.AESUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName dsasas
 * @Description
 * @Author llg
 * Date 2023/7/16 8:18 上午
 */
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = {WebappApplication.class})
public class temButtom {

    @Test
    public void test01() {
        String a = AESUtil.encrypt("hzya@1314");
        System.out.println(a);
        String b = AESUtil.decrypt("62e4295b615a30dbf3b8ee96f41c820b");
        System.out.println(b);
    }


    @Test
    public void test02() {
//	1056162015172640840	-7858803986346327947	3178176833471791293	合同评审-待办测试(bdmanager 2024-10-22 16:45)	7743552636545550897	bdmanager	18058147870	pending	start	success	新增成功!
//	success	更新待办为已办成功！
//	task7803207f54ff047d6008dcce31c2628f	新增成功!
//	2024-10-24	2024-10-24

        String phone ="19357235324";
        String taskid ="task8b0c7ca72439bc9b0c1c89e8866c8275";
        //token
        Map<String, String> headers = new HashMap<>();
        String token ="https://oapi.dingtalk.com/gettoken?appkey=dingxewtjaserj292ggu&appsecret=DuRw6EEEvhGXfr6Q8wN_x4025qKjrffIGCXF9KeCKKIID-LVSsR6_8KWMei6sug1";
        String body = sendGet(token,headers);
        JSONObject tokenobject = JSONObject.parseObject(body);

        //钉钉id
        headers = new HashMap<>();
        //https://oapi.dingtalk.com/user/get_by_mobile?access_token=9abd3996cb103ba48dd8c69fea5473e7&mobile=15700100840
        String ddid ="https://oapi.dingtalk.com/user/get_by_mobile?access_token="+tokenobject.get("access_token")+"&mobile="+phone;
        String ddidbody = sendGet(ddid,headers);
        JSONObject ddidobject = JSONObject.parseObject(ddidbody);

        //人员id
        headers = new HashMap<>();
        //https://oapi.dingtalk.com/user/get?userid=111336474727636213&access_token=3d21a6614fb037a98542a537336e8149
        String userid ="https://oapi.dingtalk.com/user/get?userid="+ddidobject.get("userid")+"&access_token="+tokenobject.get("access_token");
        String useridbody = sendGet(userid,headers);
        JSONObject useridobject = JSONObject.parseObject(useridbody);



        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPut httpPut = new HttpPut("https://api.dingtalk.com/v1.0/todo/users/"+useridobject.get("unionid")+"/tasks/"+taskid);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
        httpPut.setConfig(requestConfig);
        httpPut.setHeader("Content-type", "application/json");
        httpPut.setHeader("x-acs-dingtalk-access-token", tokenobject.getString("access_token"));
        Map<String, Object> dataMap = new HashMap();
        dataMap.put("done", true);
        CloseableHttpResponse httpResponse = null;

        try {
            httpPut.setEntity(new StringEntity("{\"done\": true}"));
            httpResponse = httpClient.execute(httpPut);
            HttpEntity entity = httpResponse.getEntity();
            String results = EntityUtils.toString(entity);
            System.out.println(results);
        } catch (Exception var15) {
        } finally {
            try {
                httpResponse.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private String sendGet(String url, Map<String, String> headers) {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.disableCookieManagement().build();
        HttpGet get = new HttpGet(url.toString());
        CloseableHttpResponse response = null;

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(60000).build();
        get.setConfig(requestConfig);//设置请求参数【超时时间】
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                get.setHeader(key, headers.get(key));
            }
        }
        StringBuilder body = new StringBuilder();

        try {

            response = closeableHttpClient.execute(get);
            HttpEntity entity = response.getEntity();
             body.append(EntityUtils.toString(entity,"UTF-8"));
        } catch (Exception e) {
            body.append(e.getMessage());
        } finally {
            try {
                // 关闭响应对象
                if (response != null) {
                    response.close();
                }
                // 关闭响应对象
                if (closeableHttpClient != null) {
                    closeableHttpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return body.toString();
    }


}
