package com.happy.common.http;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.Message.Mp_template_msg;
import com.happy.Model.weixin.AccessToken;
import com.happy.Model.weixin.WechatResult;
import com.happy.Model.weixin.WeiXinUtil;
import com.happy.Until.HttpUtils;

import java.util.HashMap;

public class Message3 {

    /** 小程序推送消息到公众号
     * openid:用户小程序openid, order_num订单号,
     *  h_type房型, live_end入住和离店日期：2023-08-02~2023-08-06
     *  user_name:用户名, hotel_name：酒店名
     * **/
    public static String send(String openid,String order_num,
                              String h_type,String live_end,
                              String user_name,String hotel_name,String id)
            throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("grant_type", "client_credential");
        jsonObject.put("appid", WeiXinUtil.appid_c); // 小程序
        jsonObject.put("secret",WeiXinUtil.screct_c);
        String msg3 = HttpsClient.sendJson2("https://api.weixin.qq.com/cgi-bin/stable_token",jsonObject);
        Gson gson = new Gson();
        HashMap<String, String> userMap = gson.fromJson(msg3.toString(), new TypeToken<HashMap<String, String>>() {}.getType());
        JSONObject message = new JSONObject();
        String token = userMap.get("access_token");
        message.put("touser", openid);

        Mp_template_msg mp_template_msg = new Mp_template_msg();
        mp_template_msg.setAppid(WeiXinUtil.appid_g);  // 公众号
        mp_template_msg.setTemplate_id("pQLY-9pKW-lD-6PuOlxieatGmuCHOezubTxI99cs7Zo");
        mp_template_msg.setUrl("index");
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("appid",WeiXinUtil.appid_c);
        jsonObject1.put("pagepath","/pages/push/push?id="+id);
        mp_template_msg.setMiniprogram(jsonObject1);
        // 订单号
        JSONObject character_string1_ = new JSONObject();
        character_string1_.put("value", order_num);
        character_string1_.put("color", "#173177");
        // 房型名称
        JSONObject thing6 = new JSONObject();
        thing6.put("value", h_type);
        thing6.put("color", "#0000FF");
        // 入离时间
        JSONObject time11 = new JSONObject();
        time11.put("value", live_end);
        time11.put("color", "#173177");
        // 客户名称
        JSONObject thing10 = new JSONObject();
        thing10.put("value", user_name);
        thing10.put("color", "#173177");
        // 酒店名称
        JSONObject thing9_ = new JSONObject();
        thing9_.put("value", hotel_name);
        thing9_.put("color", "#173177");
        // 封装data
        JSONObject data = new JSONObject();
        // 订单号
        data.put("character_string1",character_string1_);
        // 房型名称
        data.put("thing6",thing6);
        // 入离时间
        data.put("time11",time11);
        // 客户名称
        data.put("thing10",thing10);
        // 酒店名称
        data.put("thing9",thing9_);
        mp_template_msg.setData(data);
        message.put("mp_template_msg",mp_template_msg);
        return HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token="+token, message);

    }

}
