package com.xunwang.payment.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xunwang.payment.entity.WxTemplateMsg;
import org.springframework.stereotype.Component;

import java.util.TreeMap;

/**
 * @author Guan
 * @date 2023/4/6
 * discription
 */
@Component
public class MyWxUtils {

    public String getAccessToken(){
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + "wx89420f00e823c9ec" +
                "&secret=" + "17eb4a2499cf02d807df515ff0949998";
        // 利用hutool的http工具类请求获取access_token
        String result = HttpUtil.get(url);
        // 将结果解析为json
        JSONObject jsonObject = JSONUtil.parseObj(result);
        // 获取access_token
        return jsonObject.getStr("access_token");
    }

    public String getUnionId(String openid){
        String accessToken = getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openid +"&lang=zh_CN";
        String result = HttpUtil.get(url);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject.getStr("unionid");
    }


    /**
     * 信息费用支付通知
     * @param grade 年级
     * @param subject 科目
     * @param address 地址
     * @param reward 服务费
     * @param phone 联系方式
     * @param remark 备注
     */
    public String payNoticeTemplate(String openIdd , String grade , String subject , String address ,
                                 String reward , String phone , String remark) {
        // 模版ID
        String templateId="2OGz67_V35H41F8nN431r8iIF5C-bFUIU5Q0iTMSHUA";
        TreeMap<String, TreeMap<String, String>> params = new TreeMap<>();
        String hiddenAddress = address.substring(0, address.indexOf("区")) + "区";
        String hiddenPhone= phone.substring(0, 3) + "****" +phone.substring(7, 11);
        //根据具体模板参数组装
        params.put("first", WxTemplateMsg.item("您好！您的家教简历匹配一份新家教订单", "#FF0000"));
        params.put("keyword1", WxTemplateMsg.item(grade, "#FF0000"));
        params.put("keyword2", WxTemplateMsg.item(subject, "#000000"));
        params.put("keyword3", WxTemplateMsg.item(hiddenAddress, "#000000"));
        params.put("keyword4", WxTemplateMsg.item(reward, "#000000"));
        params.put("keyword5", WxTemplateMsg.item(hiddenPhone, "#000000"));
        params.put("remark", WxTemplateMsg.item(remark, "#000000"));
        WxTemplateMsg wxTemplateMsg = new WxTemplateMsg();
        // 模版ID
        wxTemplateMsg.setTemplate_id(templateId);
        // 消息接收者openId
        wxTemplateMsg.setTouser(openIdd);
        // 关键字赋值
        wxTemplateMsg.setData(params);
        // miniprogram
        TreeMap<String,String> miniprogram = new TreeMap<>();
        miniprogram.put("appid","wxf2d10b5ab2b20723");
        miniprogram.put("path","pages/home/home");
        wxTemplateMsg.setMiniprogram(miniprogram);
        String data = JSONUtil.toJsonStr(wxTemplateMsg);
        return data;
    }


    /**
     * 预约通知
     * @param name 预约人姓名
     * @param phone 预约人手机号
     * @param course 预约课程
     * @param remark 备注
     * @param isTeacher 判断发送给老师或是学生
     */
    public String orderNoticeTemplate(String openIdd , String name, String phone ,
                                    String course , String remark , boolean isTeacher) {
        // 模版ID
        String templateId="-dSyZanVZlAEhOAP7XKWHYjpksHWPzOgaomMHlQnLKY";
        TreeMap<String, TreeMap<String, String>> params = new TreeMap<>();
        String hiddenPhone= phone.substring(0, 3) + "****" +phone.substring(7, 11);
        //根据具体模板参数组装
        if (isTeacher == true){
            params.put("first", WxTemplateMsg.item("您好，有学生预约了您的课程！", "#FF0000"));
            params.put("keyword1", WxTemplateMsg.item(name + "(学生)", "#FF0000"));
        }else {
            params.put("first", WxTemplateMsg.item("您好，有老师预约了您的需求！", "#FF0000"));
            params.put("keyword1", WxTemplateMsg.item(name + "(老师)", "#FF0000"));
        }
//        params.put("keyword1", WxTemplateMsg.item(name, "#FF0000"));
        params.put("keyword2", WxTemplateMsg.item(hiddenPhone, "#000000"));
        params.put("keyword3", WxTemplateMsg.item(course, "#000000"));
        params.put("remark", WxTemplateMsg.item(remark, "#000000"));
        WxTemplateMsg wxTemplateMsg = new WxTemplateMsg();
        // 模版ID
        wxTemplateMsg.setTemplate_id(templateId);
        // 消息接收者openId
        wxTemplateMsg.setTouser(openIdd);
        // 关键字赋值
        wxTemplateMsg.setData(params);
        // miniprogram
        TreeMap<String,String> miniprogram = new TreeMap<>();
        miniprogram.put("appid","wxf2d10b5ab2b20723");
        miniprogram.put("path","pages/home/home");
        wxTemplateMsg.setMiniprogram(miniprogram);
        String data = JSONUtil.toJsonStr(wxTemplateMsg);
        return data;
    }

}