package com.jay.monitor.data.server.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jay.monitor.data.server.models.request.dingding.ActionCardModel;
import com.jay.monitor.data.server.models.request.dingding.BaseDDModel;
import com.jay.monitor.data.server.models.request.dingding.MdModel;
import com.jay.monitor.data.server.models.request.dingding.TextModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 钉钉消息发送工具类
 * 参考 : https://www.cnblogs.com/bbling/p/9060308.html
 *
 * @author : liukx
 * @create : 2018/11/16 16:03
 * @email : liukx@elab-plus.com
 */
public class DingDingUtils {

    private static Logger logger = LoggerFactory.getLogger(DingDingUtils.class);
    private final static String dd = "https://oapi.dingtalk.com/robot/send?access_token=0009e049c04411ff18487db2bb7e5907f719e8c0d9aa35fa722b93bae2d8a931";

    public static MdModel builderMdModel(String title, String mdText, boolean atAll, List<String> atMobiles) {
        MdModel model = new MdModel();
        MdModel.MarkdownDTO markdownDto = new MdModel.MarkdownDTO();
        markdownDto.setTitle(title);
        markdownDto.setText(mdText);


        MdModel.AtDTO atDTO = new MdModel.AtDTO();
        atDTO.setAtAll(atAll);
        atDTO.setAtMobiles(atMobiles);
        model.setMarkdown(markdownDto);
        model.setAt(atDTO);
        return model;
    }

    /**
     * 构建独立跳转消息
     *
     * @param title   标题名称
     * @param text    消息内容 -> markdown格式
     * @param arrange 按钮排列顺序: 0竖直排列 1横向排列
     * @param btnList 按钮事件 -> 包含按钮名称和跳转链接
     * @return
     */
    public static ActionCardModel buildActionCardModel(String title, String text, Integer arrange, List<ActionCardModel.ActionCardDTO.BtnsDTO> btnList) {
        ActionCardModel actionCardModel = new ActionCardModel();

        ActionCardModel.ActionCardDTO actionCardDTO = new ActionCardModel.ActionCardDTO();
        actionCardDTO.setTitle(title);
        actionCardDTO.setText(text);
        actionCardDTO.setBtnOrientation(arrange + "");
        actionCardDTO.setBtns(btnList);

        actionCardModel.setActionCard(actionCardDTO);
        return actionCardModel;
    }

    /**
     * 发送消息到钉钉上
     *
     * @param url   钉钉机器人的URL
     * @param model 消息模型
     * @return
     */
    public static boolean send(String url, BaseDDModel model) {
        return sendDingDing(url, JSON.toJSONString(model));
    }

    private static boolean sendDingDing(String url, String data) {
        String post = HttpUtil.post(url, data, 5000);
        JSONObject jsonObject = JSON.parseObject(post);
        String errmsg = jsonObject.getString("errmsg");
        if ("ok".equals(errmsg)) {
            return true;
        } else {
            System.out.println(" 钉钉调用返回结果 : " + jsonObject.toJSONString());
            logger.info(" 钉钉调用返回结果 : " + jsonObject.toJSONString());
        }
        return false;
    }

    public static void main(String[] args) {
        String json = "{\n" +
                "     \"msgtype\": \"text\",\n" +
                "     \"text\": {\n" +
                "         \"content\": \"我就是我,  @1825718XXXX 是不一样的烟火\"\n" +
                "     },\n" +
                "     \"at\": {\n" +
                "         \"atMobiles\": [\n" +
                "             \"1825718XXXX\"\n" +
                "         ], \n" +
                "         \"isAtAll\": false\n" +
                "     }\n" +
                " }";

//        String json = "{\"link\":{\"messageUrl\":\"http://106.14.4.198:2281/cat/r/p?domain=elab-marketing-sms\",\"title\":\"[CAT异常告警] [项目: elab-marketing-sms]\",\"text\":\"aaaa\"},\"msgtype\":\"link\"}";

        TextModel textModel = JSON.parseObject(json, TextModel.class);
//        TextModel textModel = JSON.parseObject(json, TextModel.class);
//        textModel.getAt().setIsAtAll(true);
        boolean send = send(dd, textModel);
        System.out.println(send);
        if (send) {
            System.out.println("发送成功 ...");
        }
    }

}
