package vip.xiaonuo.biz.api.wxapp.service;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.api.wxapp.param.AccessTokenParam;
import vip.xiaonuo.biz.api.wxapp.param.WechatTemplateMsg;
import vip.xiaonuo.biz.modular.sysUserWxOpenSendLog.entity.SysUserWxOpenSendLog;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 流程消息推送
 */
@Service
@Slf4j
public class WxProcessApprovalPushService {

    @Resource
    private AccessTokenService accessTokenService;

    /**
     * @param openId 推送人openId
     * @param thing1 区域
     * @param thing2 问题点
     * @param time3  日期
     */
    public void sendProcessApprovalMsg(SysUserWxOpenSendLog wxSendLog, String openId, String thing1, String thing2, String time3) {
        WechatTemplateMsg wechatTemplateMsg = new WechatTemplateMsg();
        wechatTemplateMsg.setTemplate_id("EXZLLZTSXy5AgPPVqAQ0xTP8QPpHE-Ypx_t54YR0l-8");
        wechatTemplateMsg.setTouser(openId);
        /*HashMap<String, String> miniprogram = new HashMap<>();
        miniprogram.put("appid", BizStr.APPID);
        miniprogram.put("pagepath", "pages/index/index");
        wechatTemplateMsg.setMiniprogram(miniprogram);*/

        Map<String, Object> params = new HashMap<>();
        //根据具体模板参数组装 车牌号 作业类型 报警原因 报警时间
        params.put("thing1", MapUtil.builder("value",thing1).build());
        params.put("thing2", MapUtil.builder("value",thing2).build());
        params.put("time3", MapUtil.builder("value",time3).build());
        wechatTemplateMsg.setData(params);

        log.info(JSONObject.toJSONString(wechatTemplateMsg));

        StringBuffer url = new StringBuffer("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=");
        try {
            url.append(URLEncoder.encode(accessTokenService.getAccessToken(new AccessTokenParam()), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = HttpRequest.post(url.toString())
                .header("Content-Type", "application/json")
                .body(JSONObject.toJSONString(wechatTemplateMsg))
                .timeout(15000).execute().body();
        //String result = WxHttp.post(url.toString(), JSONObject.toJSONString(wechatTemplateMsg));
        log.info("====== send WxMsg :{}", JSONObject.toJSONString(result));
        wxSendLog.setSendMsg(JSONObject.toJSONString(wechatTemplateMsg));
        wxSendLog.setBackMsg(JSONObject.toJSONString(result));
    }

    /**
     * @param openId 推送人openId
     * @param car_number1   车牌号
     * @param phrase4   作业类型
     * @param thing5   报警原因
     * @param time7   提交时间
     * @param thing6   地点
     */
    public void sendProcessApprovalMsg(SysUserWxOpenSendLog wxSendLog, String openId, String car_number1, String phrase4, String thing5, String time7, String thing6) {
        WechatTemplateMsg wechatTemplateMsg = new WechatTemplateMsg();
        wechatTemplateMsg.setTemplate_id("Y5RGUNnWVkXI0T8FLaKbcR0KjoNuY_rZekJaBeI3rmI");
        wechatTemplateMsg.setTemplate_id("Y5RGUNnWVkXI0T8FLaKbcd1MH5rWNf6oVrfDB7WRLm8");
        wechatTemplateMsg.setTouser(openId);

        Map<String, Object> params = new HashMap<>();
        //根据具体模板参数组装 车牌号 作业类型 报警原因 报警时间
        params.put("car_number1", MapUtil.builder("value",car_number1).build());
        params.put("phrase4", MapUtil.builder("value",phrase4).build());
        params.put("thing5", MapUtil.builder("value",thing5).build());
        params.put("time7", MapUtil.builder("value",time7).build());
        params.put("thing6", MapUtil.builder("value",thing6).build());
        wechatTemplateMsg.setData(params);

        log.info(JSONObject.toJSONString(wechatTemplateMsg));

        StringBuffer url = new StringBuffer("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=");
        try {
            url.append(URLEncoder.encode(accessTokenService.getAccessToken(new AccessTokenParam()), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String result = HttpRequest.post(url.toString())
                .header("Content-Type", "application/json")
                .body(JSONObject.toJSONString(wechatTemplateMsg))
                .timeout(15000).execute().body();
        //String result = WxHttp.post(url.toString(), JSONObject.toJSONString(wechatTemplateMsg));
        log.info("====== send WxMsg :{}", JSONObject.toJSONString(result));
        wxSendLog.setSendMsg(JSONObject.toJSONString(wechatTemplateMsg));
        wxSendLog.setBackMsg(JSONObject.toJSONString(result));
    }

}
