package com.huimv.env.input.config;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Component
public class WeChatMessage {
//    wx:
//    appId: "wx707fcfd7d09e02eb"  #小程序appId
//    secret: "w95K45G9-2xGocleqrtGoHwEGF6ocqSu8EbDBxCQSR0"   #小程序密钥
//    templateId: "f932c54f1cbd427b10218a0d1cfb88bf"  #订阅消息模板
//
//    @Value("${wx.appId}")
//    private String appId;
//
//    @Value("${wx.secret}")
//    private String secret;
//
//    @Value("${wx.templateId}")
//    private String templateId;
    private static  final  String  appId= "wx7982c391ad9c00b3";
    private static  final  String  secret= "87f5a2f00478fbe39ec1e6c832f11068";
    private static  final  String  templateId= "E2HW7OE74SRRY1vd7O9ONp93RU8cUtqg3v4xLFQInmE";

    public static  void sendMsg( Date uploadDate,String warningReason,String location,String zhanDian,String openId){
        //1:获取token（接口调用凭证）
        String token = queryToken();
        //2:发送订阅消息
        send(token,uploadDate,warningReason,location,zhanDian,openId);
    }

    // 1: 获取 access_token  (2h过期)
    public static String queryToken(){
        String tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
        tokenUrl = tokenUrl + "&appid=" + appId + "&secret=" + secret;
        String result = HttpUtil.get(tokenUrl);
        System.out.println(result);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        String token = jsonObject.get("access_token").toString();
        return token;
    }

    public static void send(String token,Date uploadDate,String warningReason,String location,String zhanDian ,String openId){
        String msgUrl="https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
        msgUrl = msgUrl + "?access_token=" + token;
        // 设置模板参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("touser", openId);                 // 接收方
        paramMap.put("template_id", templateId);        // 模板id
        paramMap.put("page","pages/warn/warn");         // 消息中要跳转的页面
        // 设置data 模板内容
        HashMap<String, Object> data = new HashMap<>();
        //报警设备
        data.put("thing2", formatParam(warningReason));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期和时间的格式
        //报警类型
        data.put("time3", formatParam(sdf.format(uploadDate)));
//        //报警时间
        data.put("thing5",formatParam(location));
//        //报警原因
        data.put("thing19", formatParam(zhanDian));
//        //报警地点
        paramMap.put("data", data);
        // 转json字符串
        String jsonObject = JSONUtil.toJsonStr(paramMap);
        String result= HttpUtil.post(msgUrl, jsonObject);
        System.out.println(result);
    }

    public static HashMap<String, Object> formatParam(String value){
        HashMap<String, Object> data = new HashMap<>();
        data.put("value", value);
        return data;
    }

}
