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

import com.alibaba.fastjson.JSON;
import com.qs.mp.admin.domain.Marketing;
import com.qs.mp.common.utils.LogUtil;
import com.qs.mp.common.utils.http.HttpUtils;
import com.qs.mp.framework.domain.WxSubscribeMessage;
import com.qs.mp.framework.service.IAppTokenService;
import com.qs.mp.framework.service.IWxSubscribeMessage;
import com.qs.mp.system.domain.SysUser;
import com.qs.mp.system.service.ISysUserService;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 发送小程序订阅消息
 *
 * @author zhongcp
 * @Date 2021/9/22
 */
@Service
public class WxSubscribeMessageImpl implements IWxSubscribeMessage {

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

  @Autowired
  private ISysUserService sysUserService;

  @Autowired
  private IAppTokenService appTokenService;

  @Value("${wx-channel.appId}")
  private String channelAppId;

  @Value("${wx-user.appId}")
  private String userAppId;

  @Value("${miniprogram.state}")
  private String state;

  @Override
  public boolean sendMarketingStart(Long userId, Marketing marketing) {
    String openId = getOpenIdByUserId(userId);
    WxSubscribeMessage wxSubscribeMessage = new WxSubscribeMessage(userAppId, openId, state);
    wxSubscribeMessage.buildMarketingStartSuccessMessage(marketing);
    return this.sendNotify(userId, marketing.getId().toString(), wxSubscribeMessage);
  }

  @Override
  public void sendMarketingHelp(Long userId, Marketing marketing) {
    String openId = getOpenIdByUserId(userId);
    WxSubscribeMessage wxSubscribeMessage = new WxSubscribeMessage(userAppId, openId, state);
    wxSubscribeMessage.buildMarketingHelpSuccessMessage(marketing);
    this.sendNotify(userId, marketing.getId().toString(), wxSubscribeMessage);
  }

  @Override
  public boolean sendMarketingLottery(Long userId, Marketing marketing) {
    String openId = getOpenIdByUserId(userId);
    WxSubscribeMessage wxSubscribeMessage = new WxSubscribeMessage(userAppId, openId, state);
    wxSubscribeMessage.buildMarketingLotterySuccessMessage(marketing);
    return this.sendNotify(userId, marketing.getId().toString(), wxSubscribeMessage);
  }

  private String getOpenIdByUserId(Long userId) {
    return sysUserService.selectUserById(userId).getOpenId();
  }


  private boolean sendNotify(Long userId, String bizId, WxSubscribeMessage message) {
    // 获取access_token
    String accessToken = appTokenService.getAccessToken(userAppId);
    String res = HttpUtils.sendPost(
        "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken,
        JSON.toJSONString(message));
    LogUtil.info(logger, "小程序订阅消息发送结果,userId:{0},bizId:{1},res:{2}",
        new Object[]{userId, bizId, res});
    return true;
  }
}
