package com.qs.mp.framework.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qs.mp.common.utils.LogUtil;
import com.qs.mp.common.utils.http.HttpUtils;
import com.qs.mp.framework.domain.WxTemplateMessage;
import com.qs.mp.framework.service.IAppTokenService;
import com.qs.mp.framework.service.IWxTemplateMessage;
import com.qs.mp.system.domain.SysUser;
import com.qs.mp.system.service.ISysUserService;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 公众号模板消息发送
 *
 * @author zhongcp
 * @Date 2021/9/22
 */
@Service
public class WxTemplateMessageImpl implements IWxTemplateMessage {

  protected final Logger logger = LoggerFactory.getLogger(WxTemplateMessageImpl.class);

  @Autowired
  private ISysUserService sysUserService;

  @Autowired
  private IAppTokenService appTokenService;

  @Value("${wxgzh.appId}")
  private String gzhAppId;

  private boolean sendNotify(Long userId, String bizId, WxTemplateMessage message, String msg) {
    // 获取access_token
    String accessToken = appTokenService.getAccessToken(gzhAppId);
    String param = JSON.toJSONString(message);
    LogUtil.info(logger, param);
    String res = HttpUtils.sendPost(
        "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken,
        param);
    LogUtil.info(logger, "发送" + msg + "结果,userId:{0},bizId:{1},res:{2}",
        new Object[]{userId, bizId, res});
    // res:{"errcode":0,"errmsg":"ok","msgid":2062954374267568128}
    JSONObject jsonObject = JSON.parseObject(res);
    if (null != jsonObject && jsonObject.getIntValue("errorCode") == 0) {
      return true;
    }
    return false;
  }
}
