package com.platform.yijia.utils.weixinapp;


import com.platform.yijia.utils.redis.RedisCacheUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/***
 * <Title>  WxPushUtil  </Title>
 * <Description> 微信公众号消息推送 </Description>
 * @author JK
 * @date 2021年2月19日
 */
@Component
public class WxPushUtil {
    @Resource
    private RedisCacheUtil redisCacheUtil;
    private static Logger logger =(Logger) LoggerFactory.getLogger(WxPushUtil.class);

    /*
     * 微信公众号推送
     * @param appId
     * @param appSecret
     * @param templateId
     * @param openId
     * @param wxMpTemplateDataList
     */
    public void push(String appId, String appSecret, String templateId, String openId, List<WxMpTemplateData> wxMpTemplateDataList){
        logger.info("公众号消息推送参数："+ "appId："+ appId + "；App秘钥："+appSecret+ "；模板ID："+templateId + "；openId："+openId + "；模板数据" +wxMpTemplateDataList);
        //创建对象
        WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpInMemoryConfigStorage.setAppId(appId);
        wxMpInMemoryConfigStorage.setSecret(appSecret);
        //公众号token缓存处理
        //String tokenCache = weiXinUserUtil.getTokenByRedisCache(appId, appSecret);
        String tokenCache ="";
        if(!redisCacheUtil.hasKey(appId)){
            String token = WeiXinUserUtil.getToken(appId, appSecret).getAccessToken();
            redisCacheUtil.setCacheObject(appId, token);
            redisCacheUtil.expire(appId, 7200, TimeUnit.SECONDS);
            tokenCache = token;
            logger.info("Redis缓存中token信息: " + tokenCache);
        }else {
            tokenCache = redisCacheUtil.getCacheObject(appId);
        }
        logger.info("token信息: " + tokenCache);
        wxMpInMemoryConfigStorage.setAccessToken(tokenCache);

        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage);
        WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                //.url()
                .build();
        //wxMpTemplateMessage.addData(wxMpTemplateData)
        wxMpTemplateMessage.setData(wxMpTemplateDataList);
        try {
//            wxMpTemplateMessage.setToUser(openId);
            wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
            logger.info("推送成功");
        } catch (WxErrorException e) {
            logger.error("推送失败" + e.getMessage());
            e.printStackTrace();
        }


    }

}
