package com.hotent.openapi.demo;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hotent.base.util.RSAUtil;
import com.hotent.base.util.time.DateFormatUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author 梁宏锦
 * @date 2024/07/08
 **/
public class TodoMsgTest {
    private static final Logger logger= LoggerFactory.getLogger(TodoMsgTest.class);

    //公钥
    public static String publicKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCMnhQ99yP-eEU2jXdQWc6j-wWbqNLqOLinEGBY11WJUCmzHiEycDXPc6-3YMOvrdAiHZcjkMCzU_eRnBLUqkcNw9nhQrCak-sTpEVlAV21LskD6KMf-6PsfttUvpXeCO5g3Hg48F_vbLKxb8s_lcvQgCpKBIpsUdYRcp_PgSg8BQIDAQAB";
    // 私钥
    private static String privateKey ="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIyeFD33I_54RTaNd1BZzqP7BZuo0uo4uKcQYFjXVYlQKbMeITJwNc9zr7dgw6-t0CIdlyOQwLNT95GcEtSqRw3D2eFCsJqT6xOkRWUBXbUuyQPoox_7o-x-21S-ld4I7mDceDjwX-9ssrFvyz-Vy9CAKkoEimxR1hFyn8-BKDwFAgMBAAECgYBt4JAco2Mi0sPyjrQuux43EE0fhtnYX54aHTfL8-dItrT-24pBKWFCyzkA58HBIuZB4jFZfyC0f-Yxh21HdA60T8EAmHcJubIFOREeQdFbvTDBYuwcrQSmTsJ17yw_qB-DZrbSd0YvpAnJnu1rBhzrcrskQPmaxM4Jv-08aKj8gQJBAMnqpcSi3onhUUVDYnQLJWIGqRjkPdWTgd4-k0JhBwxwGrIMgzsOBS21i7BfSSrKyFRhs6tgMYA0DsfIavwcP5ECQQCySCxbpvaxr8hBKXc0Xy50w4_F1IBNWY47v4kZGEf-DA8LmwtuWb-5Jdnoss8ZFx6rGxD2uZKzHWIwWbIOGmM1AkB3fRuyHOvyzlro0TKvF3V86BIjf9Yz0MH2wjMlAgRI0QKLQSpbxTU9UoUiz5Cd-TjJ7rODhD5m7uecRLdDEHhxAkBkpCGGS6aPf-_H-SR1yBVtjMgooWEcSSXlzKYUKITbgOstXgUB8mAsoJV2qxoFy-tgJBNbdD7AJiI7sSa2l2bRAkEAhjzNzzGqhHnBYrExOtuU7UZlEK-d-2DTS_6b6stokpbZncKI7G65-yX6CILZ798wGI6rzhkzYg1_bcj6b5jptQ";

    //public static String hostUrl = "http://localhost:8088";
    //public static String hostUrl = "http://10.7.14.215:8280/stage-api";
    public static String hostUrl = "http://1.71.9.148:8001/stage-api";

    public static String getUc() {
        if(hostUrl.indexOf("stage-api")!=-1) {
            return "/uc";
        }
        return "";
    }

    public static String getBpmRunTime() {
        if(hostUrl.indexOf("stage-api")!=-1) {
            return "/bpmRunTime";
        }
        return "";
    }

    public String getToken() throws Exception {
        //外部接口获取 token
        String sysCode = "zgxt"; //外部系统 编码
        String secretKey = "zgxt123456"; //外部系统 密匙
        String getTokenUrl = TodoMsgTest.hostUrl + getUc() + "/apiManager/getToken";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sysCode", sysCode);
        String secretKeyEncrypt = RSAUtil.publicEncrypt(secretKey, RSAUtil.getPublicKey(publicKey));
        param.put("secretKey", secretKeyEncrypt);
        String result = HttpUtil.get(getTokenUrl, param);
        logger.info("getToken_result:{}",result);

        JSONObject tokenJsonResult = JSONUtil.parseObj(result);
        boolean tokenState = tokenJsonResult.getBool("state");
        String tokenCode = tokenJsonResult.getStr("code");
        String tokenMessage = tokenJsonResult.getStr("message");
        String tokenValue = tokenJsonResult.getStr("value");
        return tokenValue;
    }

    /**
     * 获取字符型的ID
     * @return
     */
    public static String getSuid(){
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }


    public static void main(String[] args) throws Exception {
        TodoMsgTest todo = new TodoMsgTest();
        String token = todo.getToken();

//         // 待办消息发布
//        JSONObject msg = JSONUtil.createObj();
//        msg.set("taskId", TodoMsgTest.getSuid()); //三方业务唯一ID，应用账号内唯一
//        msg.set("subject", "资产管理系统_设备采购申请"+DateFormatUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm")); //主题名称
//        msg.set("ownerId", "1"); //任务所属人ID
//        msg.set("assigneeId", "1"); //任务执行人ID 1811299145271595008
//        msg.set("todoState", TodoMsgConstant.TODO_STATE_WAIT); //待办状态 0:待办 1:已办
//        msg.set("todoDueTime", "2024-07-30"); //待办超期时间(不包含当日)yyyy-MM-dd
//        msg.set("openStyle", TodoMsgConstant.OPEN_STYLE_PAGE); //页面打开方式 0:新页面(默认) 1:页面对话框
//        msg.set("ownerSource", TodoMsgConstant.OWNER_SOURCE_APP_PC); //来源类型 0:PC应用  1:移动应用
//        msg.set("ownerSourceAppId", "1815293798751846400"); //来源_关联应用/移动应用ID
//        msg.set("supportMobile", TodoMsgConstant.SUPPORT_MOBILE_PC); //是否支持手机表单 0:跳转全兼容(默认)  1:仅支持pc端跳转 2:仅支持移动端跳转 3:不跳转
//        msg.set("urlParam", "http://www.baidu.com"); //消息链接地址
//        String submitSysTodoUrl = TodoMsgTest.hostUrl + getBpmRunTime() + "/openApi/server/todo/msginfo/submitSysTodo";
//        HttpResponse execute = HttpRequest.post(submitSysTodoUrl)
//                .header("Content-Type", "application/json;charset=UTF-8")
//                .header("Authorization", "Bearer " + token)
//                .body(msg.toString())
//                .execute();
//        String msgResult = execute.body();
//        System.out.println(msgResult);

        // 待办消息更新
        JSONObject msg2 = JSONUtil.createObj();
        msg2.set("todoId", "1816392261837168640"); //待办消息id
        //msg2.set("subject", "资产管理系统_设备领用申请（更新）"+DateFormatUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm")); //主题名称
        msg2.set("todoState", TodoMsgConstant.TODO_STATE_DONE); //待办状态 0:待办 1:已办
        msg2.set("opinionState", TodoMsgConstant.OPINION_STATE_AGREE); //审批状态
        msg2.set("todoDueTime", "2024-07-31"); //待办超期时间(不包含当日)yyyy-MM-dd
        msg2.set("supportMobile", TodoMsgConstant.SUPPORT_MOBILE_PC); //是否支持手机表单 0:跳转全兼容(默认)  1:仅支持pc端跳转 2:仅支持移动端跳转 3:不跳转
        msg2.set("urlParam", "http://www.baidu.com?page=1"); //消息链接地址
        String updateSysTodoUrl = TodoMsgTest.hostUrl + getBpmRunTime() + "/openApi/server/todo/msginfo/updateSysTodo";
        HttpResponse execute2 = HttpRequest.post(updateSysTodoUrl)
                .header("Content-Type", "application/json;charset=UTF-8")
                .header("Authorization", "Bearer " + token)
                .body(msg2.toString())
                .execute();
        String msgResult2 = execute2.body();
        System.out.println(msgResult2);

//        // 待办消息查询
//        JSONObject msg4 = JSONUtil.createObj();
//        msg4.set("todoId", "1815280041383190528"); //待办消息id
//        msg4.set("taskId", "28cfc5bf-3d7e-4cfe-a3d2-51346166af19"); //三方业务唯一ID，应用账号内唯一
//        msg4.set("ownerSource", TodoMsgConstant.OWNER_SOURCE_APP_PC); //来源类型 0:PC应用 1:移动应用
//        msg4.set("ownerSourceAppId", "1806614226597347328"); //来源_关联应用/移动应用ID
//        String querySysTodoUrl = TodoMsgTest.hostUrl + getBpmRunTime() + "/openApi/server/todo/msginfo/querySysTodo";
//        HttpResponse execute4 = HttpRequest.post(querySysTodoUrl)
//                .header("Content-Type", "application/json;charset=UTF-8")
//                .header("Authorization", "Bearer " + token)
//                .body(msg4.toString())
//                .execute();
//        String msgResult4 = execute4.body();
//        System.out.println(msgResult4);

//        // 待办消息更新
//        JSONObject msg3 = JSONUtil.createObj();
//        msg3.set("todoId", "1811674279780057088"); //待办消息id
//        String deleteSysTodoUrl = TodoMsgTest.hostUrl + getBpmRunTime() + "/openApi/server/todo/msginfo/deleteSysTodo";
//        HttpResponse execute3 = HttpRequest.post(deleteSysTodoUrl)
//                .header("Content-Type", "application/json;charset=UTF-8")
//                .header("Authorization", "Bearer " + token)
//                .body(msg3.toString())
//                .execute();
//        String msgResult3 = execute3.body();
//        System.out.println(msgResult3);
    }
}
