package com.heyu.api.data.utils;


import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.lz.mybatis.plugins.interceptor.entity.DingTalkDto;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/***
 * https://oapi.dingtalk.com/robot/send?access_token=4e9917f5f782cec0bf139ac867b95c64b9c8bcd71266f334905f5196d9828314
 */
@Slf4j
public class DingDingUtils {


    protected static String dingTalkUrlPre = "https://oapi.dingtalk.com/robot/send?access_token=";
    protected static String token = "4e9917f5f782cec0bf139ac867b95c64b9c8bcd71266f334905f5196d9828314";


    // https://oapi.dingtalk.com/robot/send?access_token=590697c16a95c80798c58caa4205b7067258eaceb2ac10610713b2cf5b269bea


    public static void sendText(String text) {
        try {
            String content = text + "\n";
            content = "【告警】："
                    + "\n" + content;
            DingTalkDto dingTalk = new DingTalkDto();
            dingTalk.setToken("");
            dingTalk.setText(content);
            dingTalk.setTitle("赶快充钱");

            dingTalk.setMsgType("text");
            process(dingTalk,token);
        } catch (Exception e) {
            log.error("DingDingUtils",e);
        }
    }





    private static OapiRobotSendResponse process(DingTalkDto dingTalk, String token ) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient(dingTalkUrlPre + token);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");

        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(dingTalk.getText());
        request.setText(text);
        // 被@人的手机号(在text内容里要有@手机号)
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();

        String[] mobiles = "18969093321".split(",");


        if(StringUtils.isNotBlank(dingTalk.getAtMobiles())){
            mobiles = dingTalk.getAtMobiles().split(",");
        }
        at.setAtMobiles(Arrays.asList(mobiles));

        request.setAt(at);
        OapiRobotSendResponse response = client.execute(request);
        return response;
    }


    public static void main(String[] args) {

        sendText("ieoiew");

    }






}
