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 GouDeZhaoDDUtils {


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


    // 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 sendText1(String content) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient(dingTalkUrlPre + token);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent(content);
        request.setText(text);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        // isAtAll类型如果不为Boolean，请升级至最新SDK
        at.setIsAtAll("false");
        at.setAtMobiles(Arrays.asList("136700000","15200000"));
        request.setAt(at);
        OapiRobotSendResponse response = client.execute(request);
        log.info("success:{}, code:{}, errorCode:{}, errorMsg:{}",response.isSuccess(),response.getCode(),response.getErrcode(),response.getErrmsg());
    }




    public static void sendLink() throws Exception {
        DingTalkClient client = new DefaultDingTalkClient(dingTalkUrlPre + token);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("link");
        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setMessageUrl("https://www.dingtalk.com/");
        link.setPicUrl("https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png");
        link.setTitle("滴滴滴滴时代的火车向前开");
        link.setText("【注意】打开不打完卡不打卡不打卡金娃");
        request.setLink(link);
        OapiRobotSendResponse response = client.execute(request);

    }


    public static void sendMarkDown() throws Exception {
        DingTalkClient client = new DefaultDingTalkClient(dingTalkUrlPre + token);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        request.setMsgtype("markdown");
        markdown.setTitle("下单数据");
        markdown.setText("【注意】 杭州天气 \n" +
                "> 9度，西北风1级，空气良89，相对温度73%\n\n" +
                "> ![screenshot](https://heyuimage.ihzhy.com/prd/product1.png)\n"  +
                "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
        request.setMarkdown(markdown);

        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setAtMobiles(Arrays.asList("13600006","155xxxx"));
        request.setAt(at);
        OapiRobotSendResponse response = client.execute(request);
        System.out.printf("success:%s, code:%s, errorCode:%s, errorMsg:%s%n",response.isSuccess(),response.getCode(),response.getErrcode(),response.getErrmsg());

    }


    public static void sendMarkDownOrder(String title,String url) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient(dingTalkUrlPre + token);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        request.setMsgtype("markdown");
        markdown.setTitle(title);
        markdown.setText("【注意】  \n" +
                "> ![screenshot]("+ url +")\n"  +
                "");
        request.setMarkdown(markdown);

        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        at.setAtMobiles(Arrays.asList("13600006","155xxxx"));
        request.setAt(at);
        OapiRobotSendResponse response = client.execute(request);
        System.out.printf("success:%s, code:%s, errorCode:%s, errorMsg:%s%n",response.isSuccess(),response.getCode(),response.getErrcode(),response.getErrmsg());

    }



    public static void main(String[] args) throws Exception {


    }

}
