package com.cyksj.service.mange.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cyksj.common.EnvCommonService;
import com.cyksj.common.constant.TemplateEnum;
import com.cyksj.common.util.Jsons;
import com.cyksj.mapper.SpotifyUnLimitRecordMapper;
import com.cyksj.mapper.UserMapper;
import com.cyksj.model.dto.WxMpTemplateData;
import com.cyksj.model.dto.WxMpTemplateMessage;
import com.cyksj.model.entity.SpotifyUnLimitRecord;
import com.cyksj.model.entity.User;
import com.cyksj.model.views.WxAppTemplateView;
import com.cyksj.service.mange.SpotifySendMsgService;
import com.cyksj.service.template.TemplateCommonService;
import com.cyksj.service.wechat.WeChatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/*
 *项目名: netflix
 *文件名: SpotifySendMsgServiceImpl
 *创建者: JavaZou
 *创建时间:2022/12/21 16:01
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SpotifySendMsgServiceImpl implements SpotifySendMsgService {
	private final EnvCommonService envCommonService;

	private final WeChatService weChatService;

	private final UserMapper userMapper;

	private final SpotifyUnLimitRecordMapper spotifyUnLimitRecordMapper;

	private static String spotifyUnLimitSuccessUrl;

	private final TemplateCommonService templateCommonService;

	@PostConstruct
	public void init() {
		String host = envCommonService.getNotifyHost();
		String domain = envCommonService.getDomain();
		String redirectUrl = String.format("%s%s", domain, "yinhe/web/?temp=couponId?1?goodsId?4?skuId?14");
		spotifyUnLimitSuccessUrl = String.format("%sauth/weChat?url=%s", host, redirectUrl);
	}

	@Override
	public void sendSpotifyUnLimitSuccessMsg(SpotifyUnLimitRecord record) throws Exception {
		if (!EnvCommonService.active.equals(envCommonService.getEnv())) {
			SpotifyUnLimitRecord unLimitRecord = spotifyUnLimitRecordMapper.selectById(record.getId());
			if (unLimitRecord != null && !unLimitRecord.getIsSend()) {
				User user = userMapper.selectById(unLimitRecord.getUserId());
				if (user != null && StrUtil.isNotBlank(user.getOpenId())) {
					String openId = user.getOpenId();
					WxAppTemplateView wxAppTemplateView = templateCommonService.getWxAppByUserId(openId, TemplateEnum.SPOTIFY_ACCOUNT_UN_LIMIT_SUCCESS_TEMPLATE.getDesc());
					if (wxAppTemplateView != null) {
						WxMpTemplateMessage templateMessage = new WxMpTemplateMessage()
								.setToUser(openId)
								.setTemplateId(wxAppTemplateView.getTemplateId())
								.setUrl(spotifyUnLimitSuccessUrl);
						WxMpTemplateMessage.TemplateData data = templateMessage.getData();
						data.setFirst(new WxMpTemplateData("您的Spotify账号登录限制解除成功"));
						data.setKeyword1(new WxMpTemplateData(user.getNickname()));
						data.setKeyword2(new WxMpTemplateData(DateUtil.format(unLimitRecord.getUpdateTime(), "yyyy-MM-dd HH:mm:ss")));
						data.setRemark(new WxMpTemplateData("建议您购买Spotify年付会员，无需每过14天解除限制，点击了解"));
						weChatService.sendTemplateMessage(weChatService.getAccessToken(wxAppTemplateView.getAppId()), Jsons.toJson(templateMessage));
						unLimitRecord.setIsSend(true);
						spotifyUnLimitRecordMapper.updateById(unLimitRecord);
					}
				}
			}
		}
	}
}
