package com.jeeplus.weChat.service;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jeeplus.weChat.domain.TemplateInfo;
import com.jeeplus.weChat.domain.TranTemplate;
import com.jeeplus.weChat.domain.WXTransportTemplate;
import com.jeeplus.weChat.util.HttpUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 王强
 * @version 1.0
 * @date 2024-07-22 10:31
 */
@Service
@Transactional
@Log4j2
public class WxSendService {

    @Value("${wechat.sendTemplateUrl}")
    private String sendTemplateUrl;

    //发送运输单模板信息给司机
    public String sendTranTemplate(WXTransportTemplate resource){
        //拿到accessToken
        String accessToken = HttpUtils.access_token;
        if (null == accessToken){
            accessToken = HttpUtils.dogetToken();
        }
        System.out.println("现在取出的token是："+accessToken);

        //发送模板消息开始
        TranTemplate tranTemplate = new TranTemplate();
        //设置接收司机微信ID
        tranTemplate.setTouser(resource.getOpenid());
        tranTemplate.setUrl("https://www.baidu.com/");
        //设置模板ID
        tranTemplate.setTemplate_id("3b5rKQ1nTjvML1Xyj0OSMV0CnMNt7WjdzPksAHhvhRI");
        //给模板的内容赋值
        Map<String , TemplateInfo> dataMap = new HashMap<>();
        TemplateInfo first = new TemplateInfo("您好，您有一条运输单需要运输。","#DC143C");
        dataMap.put("first",first);
        TemplateInfo keyword1 = new TemplateInfo(resource.getTransNum(),"#173177");
        dataMap.put("keyword1",keyword1);
        TemplateInfo keyword2 = new TemplateInfo(resource.getEngiName(),"#173177");
        dataMap.put("keyword2",keyword2);
        TemplateInfo remark = new TemplateInfo("\n请及时运输","#DC143C");
        dataMap.put("remark",remark);
        tranTemplate.setData(dataMap);

        JSONObject jsonObject = JSONUtil.parseObj(tranTemplate);
        //将入参转为字符串
        String jsonParam = jsonObject.toString();
        //发起请求发送模板信息
        String result = HttpUtils.requestPost(sendTemplateUrl + accessToken, jsonParam);

        return  result;
    }

    //微信公众号-发送流程通知
    public String sendProcessTemplate(WXTransportTemplate resource){
        //拿到accessToken
        String accessToken = HttpUtils.dogetToken();

        String result = "";

        if ( null != accessToken ) {
            //发送模板消息开始
            TranTemplate tranTemplate = new TranTemplate();
            //设置接收人的openid(当前登录人的openid)

            tranTemplate.setTouser(resource.getOpenid());

            tranTemplate.setUrl(resource.getUrl());

            //设置模板ID
            tranTemplate.setTemplate_id("-e-nHVBDe8SetRjPX8K7cKB4q3KeNV56WHkpQg5rBjU");
            //给模板的内容赋值
            Map<String , TemplateInfo> dataMap = new HashMap<>();

            TemplateInfo title = new TemplateInfo(resource.getTitleName(),"#173177");
            dataMap.put("thing2",title);

            TemplateInfo processName = new TemplateInfo(resource.getProcessName(),"#173177");
            dataMap.put("thing3",processName);

            TemplateInfo currentStage = new TemplateInfo(resource.getCurrentStage(),"#173177");
            dataMap.put("thing11",currentStage);

            TemplateInfo processInitiator = new TemplateInfo(resource.getProcessInitiator(),"#173177");
            dataMap.put("thing4",processInitiator);

            TemplateInfo createTime = new TemplateInfo(resource.getCreateTime(),"#173177");
            dataMap.put("time5",createTime);

            tranTemplate.setData(dataMap);

            JSONObject jsonObject = JSONUtil.parseObj(tranTemplate);
            //将入参转为字符串
            String jsonParam = jsonObject.toString();
            //发起请求发送模板信息
            result = HttpUtils.requestPost(sendTemplateUrl + accessToken, jsonParam);
        }


        return  result;
    }
}
