package com.qy.worksheetsystem.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

/**
 * description: ZcWeChatExUtil <br>
 * date: 2022/3/31 20:39 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */

public class ZcWeChatExUtil {
    private static ExpiryMap<String,Object> expiryMap = new ExpiryMap<String,Object>(true);




    private static final ZcWeChatExUtil instance = new ZcWeChatExUtil();
    private ZcWeChatExUtil() {};
    public static ZcWeChatExUtil getInstance() {
        return instance;
    }

    public String sendMessage(String accessToken,Map<String,Object> map) throws Exception {
        String requestUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+ accessToken;
        System.out.println("sendMessage");
        System.out.println(requestUrl);
        System.out.println(JSONUtil.toJsonStr(map));
        String result = HttpUtil.post(requestUrl, JSONUtil.toJsonStr(map));
        Map resultMap = (Map) JSONUtil.parse(result);
        String errcode = resultMap.get("errcode").toString();
        if (errcode.equals("0")) {
            return result;
        }else {
            String errmsg = resultMap.get("errmsg").toString();
            throw new Exception("errcode:" + errcode + ";errmsg:" + errmsg);
        }
    }

    public static void main(String[] args) throws Exception {
        String token = "2pP6wGHPr_L7biqbM5AHuQzieYZlBvR0pv18BgzKEwTvt9X4HStlTkGgNKSWtmIv8FHlBgODJyAKoLir__O3Dbz_oKHVPp4BpMEn07jkH_sLQ2cp-1mI0y3eZP9lSCnqrRU7qk5pDycHThjbumbefje8V_HnVWuE7dUTXWJ5MjKiAfPBAdnaa8u0iY6mcuqEI4oFNcqLvSKlU__0dYU1lQ";

        Map<String, Object> wwMap = Maps.newHashMap();
        Map<String, Object> valMap = Maps.newHashMap();
//                    // tplMap
        wwMap.put("agentid","1000002");
        wwMap.put("msgtype","textcard");
        wwMap.put("touser", "zhang");
        wwMap.put("textcard",valMap);
        valMap.put("title", "报工审批提醒");
        StringBuilder content = new StringBuilder();
        content.append("员工"+"张庆阳"+"报工");
        valMap.put("description", content);
        valMap.put("url", "https://www.baidu.com/");
        valMap.put("btntxt", "详情");
        ZcWeChatExUtil.getInstance().sendMessage(token,wwMap);
    }
}
