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;

/**
 * @author 梁宏锦
 * @date 2024/05/29
 **/
public class SysMessageTest {
    private static final Logger logger= LoggerFactory.getLogger(SysMessageTest.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 getPortal() {
        if(hostUrl.indexOf("stage-api")!=-1) {
            return "/portal";
        }
        return "";
    }

    public static void main(String[] args) throws Exception {
        //外部接口获取 token
        String sysCode = "zgxt"; //外部系统 编码
        String secretKey = "zgxt123456"; //外部系统 密匙
        String getTokenUrl = SysMessageTest.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");

//        // 推送系统消息接口
//        JSONObject msg = JSONUtil.createObj();
//        msg.set("ownerSource", "0"); //来源类型 -1:平台 0:PC应用 1:移动应用 2:流程
//        msg.set("ownerSourceId", "1750417096824545280"); //来源应用id
//        //msg.set("ownerSourceId", "1765304648079626240"); //来源移动应用id
//        //msg.set("ownerSourceId", "1798300861001138176"); //来源流程id
//        msg.set("subject", "主题名称_测试1" + DateFormatUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss")); //主题
//        msg.set("description", "主题摘要"); //主题摘要
//        msg.set("messageSourceType", "6"); //消息跳转类型 0:固定内容 1:跳转地址 2:资讯 3:应用 4:流程 5:移动应用 6:模板消息
//        msg.set("templateCode", "2024071500091"); //消息模板Code
//        JSONObject data = JSONUtil.createObj();
//        data.set("address", "清新区车管所");
//        data.set("business", "新车 / 旧车");
//        data.set("yytime", "2019年06月29日 11:30");
//        data.set("alertInfo", "请带齐相关资料提前30分钟到场，过期无效");
//        data.set("code", "12838383222222322");
//        msg.set("templateData", data.toString()); //消息模板数据
//        //msg.set("content", "消息内容"); //消息内容
//        msg.set("supportMobile", "1"); //是否支持手机表单 0：否，1：是(默认)
//        msg.set("messageUrl", "htt://www.baidu.com"); //消息跳转地址
//        //msg.set("resourceId", "1751880629621915648"); //资讯id
//        //msg.set("appId", "1750417096824545280"); //应用id
//        //msg.set("flowId", "1798300861001138176"); //流程id
//        //msg.set("appMobileId", "1765304648079626240"); //移动应用id
//        msg.set("messageParams", "name=csName&code=csCode"); //扩展属性
//        String submitSysMsgInfoUrl = SysMessageTest.hostUrl + getPortal() + "/openApi/server/push/msginfo/submitSysMsgInfo";
//        HttpResponse execute = HttpRequest.post(submitSysMsgInfoUrl)
//                .header("Content-Type", "application/json;charset=UTF-8")
//                .header("Authorization", "Bearer " + tokenValue)
//                .body(msg.toString())
//                .execute();
//        String msgResult = execute.body();
//        System.out.println(msgResult);

        // 推送个人消息接口
        JSONObject msg_1 = JSONUtil.createObj();
        msg_1.set("ownerSource", "0"); //来源类型 -1:平台 0:PC应用 1:移动应用 2:流程
        msg_1.set("ownerSourceId", "1815293798751846400"); //来源应用id 1815293798751846400 1750417096824545280(本地)
        //msg_1.set("ownerSourceId", "1765304648079626240"); //来源移动应用id
        //msg_1.set("ownerSourceId", "1798300861001138176"); //来源流程id
        msg_1.set("subject", "资产管理系统_设备领用申请"+ DateFormatUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm")); //主题
        msg_1.set("description", "资产管理系统_设备领用申请"); //主题摘要
        msg_1.set("messageSourceType", "0"); //消息跳转类型 0:固定内容 1:跳转地址 2:资讯 3:应用 4:流程 5:移动应用 6:模板消息
        msg_1.set("templateCode", "2024071500091"); //消息模板Code
        JSONObject data_1 = JSONUtil.createObj();
        data_1.set("address", "清新区车管所");
        data_1.set("business", "新车 / 旧车");
        data_1.set("yytime", "2019年06月29日 11:30");
        data_1.set("alertInfo", "请带齐相关资料提前30分钟到场，过期无效");
        data_1.set("code", "12838383222222322");
        msg_1.set("templateData", data_1.toString()); //消息模板数据
        msg_1.set("content", "审批提醒：\n" + "您有新的工作流需要审批在"+ DateFormatUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm")); //消息内容
        //msg_1.set("supportMobile", "1"); //是否支持手机表单 0:跳转全兼容(默认)  1:仅支持pc端跳转 2:仅支持移动端跳转 3:不跳转
        msg_1.set("messageUrl", "htt://www.baidu.com"); //消息跳转地址
        //msg_1.set("resourceId", "1751880629621915648"); //资讯id
        msg_1.set("appId", "1815293798751846400"); //应用id 1750417096824545280
        //msg_1.set("flowId", "1798300861001138176"); //流程id
        //msg_1.set("appMobileId", "1765304648079626240"); //移动应用id
        msg_1.set("messageParams", "name=csName&code=csCode"); //扩展属性
        msg_1.set("receiverId", "1"); //1811299145271595008
        msg_1.set("pushRule", "1"); //推送规则设置 0:不推送 1:多通道同时发送 2:任一通道发送
        String submitUserMsgInfo = SysMessageTest.hostUrl + getPortal() + "/openApi/server/push/msginfo/submitUserMsgInfo";
        HttpResponse execute_1 = HttpRequest.post(submitUserMsgInfo)
                .header("Content-Type", "application/json;charset=UTF-8")
                .header("Authorization", "Bearer " + tokenValue)
                .body(msg_1.toString())
                .execute();
        String msgResult_1 = execute_1.body();
        System.out.println(msgResult_1);

//        //删除消息
//        JSONObject msg_2 = JSONUtil.createObj();
//        msg_2.set("msgId", "1813130645430370304"); //消息id
//        String submitUserMsgInfo = SysMessageTest.hostUrl + getPortal() + "/openApi/server/push/msginfo/deleteMsgInfo";
//        HttpResponse execute_2 = HttpRequest.post(submitUserMsgInfo)
//                .header("Content-Type", "application/json;charset=UTF-8")
//                .header("Authorization", "Bearer " + tokenValue)
//                .body(msg_2.toString())
//                .execute();
//        String msgResult_2 = execute_2.body();
//        System.out.println(msgResult_2);
    }
}
