package cn.axzo.workflow.server.common.util;

import cn.hutool.json.JSONUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import lombok.SneakyThrows;
import org.slf4j.MDC;

import java.util.Objects;

import static cn.azxo.framework.common.constatns.Constants.CTX_LOG_ID_MDC;

/**
 * 钉钉告警处理
 *
 * @author wangli
 * @since 2024/1/12 14:34
 */
public class DingTalkUtils {

    private static final String dingtalk_robot_webhook = "https://oapi.dingtalk" +
            ".com/robot/send?access_token=341ee2907f3ebc15dc495fb7771a646230058710999fec7838066c109849878e";

    @SneakyThrows
    public static void sendDingTalk(String profile, String title, Object req, Throwable throwable) {
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("Notice " + title + ", Env: " + profile);
        markdown.setText("#### [" + profile + "]" + title + "\n" +
                "> 入参: " + JSONUtil.toJsonStr(req) + "\n\n" +
                "> ###### 异常信息: " + JSONUtil.toJsonStr(Objects.isNull(throwable.getCause()) ? throwable.getMessage() :
                throwable.getCause().getMessage()) + " \n\n" +
                "> ##### traceId: " + MDC.get(CTX_LOG_ID_MDC) + " \n");
        request.setMarkdown(markdown);
        sendDingTalk(request);
    }

    @SneakyThrows
    public static void sendDingTalkForSlowUrl(String profile, Double time, String apiUrl, Object requestParam, Object responseBody) {
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("Notice 请求二方接口慢 URL, Env: " + profile);
        markdown.setText("#### [" + profile + "]请求二方接口过慢\n" +
                "> 接口地址: " + apiUrl + ",经过了" + time + "秒\n\n" +
                "> 请求参数: " + JSONUtil.toJsonStr(requestParam) + "\n\n" +
                "> ###### 结果: " + JSONUtil.toJsonStr(responseBody) + " \n");
        request.setMarkdown(markdown);
        sendDingTalk(request);
    }

    @SneakyThrows
    public static void sendDingTalkForSlowApi(String profile, Double time, String apiUrl, String uniqueId) {
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("Notice API 处理耗时报告, Env: " + profile);
        markdown.setText("#### [" + profile + "]API 处理耗时报告\n" +
                "> 接口地址: " + apiUrl + ",经过了" + time + "秒\n\n" +
                "> ###### 标识: " + uniqueId + " \n");
        request.setMarkdown(markdown);
        sendDingTalk(request);
    }

    @SneakyThrows
    private static void sendDingTalk(OapiRobotSendRequest request) {
        DingTalkClient client = new DefaultDingTalkClient(dingtalk_robot_webhook);
        OapiRobotSendResponse response = client.execute(request);
    }
}
