package cn.van.business.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author Leo
 * @version 1.0
 * @create 2025/1/22 10:20
 * @description： 企业微信推送工具类
 */
@Component
public class WxtsUtil {
    private static final Logger logger = LoggerFactory.getLogger(WxtsUtil.class);
    public static final String TOKEN = "super_token_b62190c26";
    private static final String SERVER_URL = "https://wxts.van333.cn";


    public void sendNotify(String content) {
        try {
            String url = SERVER_URL + "/wx/send/jd";
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("title", "JD机器人微信推送");
            content = content.replaceAll("\\n", "<br>");
            String common = "192.168.8.88 (微信机器人), 信息 : ";
            content = common + content + "<br><br>";
            paramMap.put("text", content);
            HttpResponse execute = HttpRequest.post(url).header("vanToken", TOKEN).header("source", "XZJ_UBUNTU").body(JSON.toJSONString(paramMap)).execute();
            //logger.info("企业微信推送结果：{}", execute);
        } catch (Exception e) {
            logger.error("企业微信推送失败：{}", e.getMessage());
        }

    }


    // 添加分级告警方法
    public void sendCriticalAlert(String title, String content) {
        String formattedMsg = String.format("[CRITICAL] %s\n%s", title, content);
        // 这里调用实际的通知渠道，例如：
        // - 发送邮件
        // - 调用企业微信机器人
        // - 触发短信通知
        sendNotify(formattedMsg); // 复用原有通知方法
    }
}
