package com.cyksj.service.corp.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.util.Jsons;
import com.cyksj.mapper.corp.*;
import com.cyksj.model.entity.*;
import com.cyksj.model.request.corp.message.NewsArticle;
import com.cyksj.model.request.corp.message.WxCorpAgentMessage;
import com.cyksj.service.corp.CorpInteractiveReaderFrontService;
import com.cyksj.service.corp.WxCorpOps;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/*
 *项目名: netflix
 *文件名: CorpInteractiveReaderFrontServiceImpl
 *创建者: JavaZou
 *创建时间:2023/8/4 18:22
 */
@Service
@RequiredArgsConstructor
public class CorpInteractiveReaderFrontServiceImpl implements CorpInteractiveReaderFrontService {
	private final CorpInteractiveRadarMapper corpInteractiveRadarMapper;

	private final CorpInteractiveRadarUserRecordMapper corpInteractiveRadarUserRecordMapper;

	private final WxCorpAppMapper wxCorpAppMapper;

	private final CorpUserMapper corpUserMapper;

	private final CorpUserAuthMapper corpUserAuthMapper;

	private final CorpUserFollowRelationMapper corpUserFollowRelationMapper;

	private final WxCorpOps wxCorpOps;

	@Override
	public void clickReader(Long userId, Long radarId) throws Exception {
		CorpInteractiveRadar radar = corpInteractiveRadarMapper.selectById(radarId);
		if (radar == null) return;
		CorpInteractiveRadarUserRecord corpInteractiveRadarUserRecord = new CorpInteractiveRadarUserRecord();
		corpInteractiveRadarUserRecord.setUserId(userId);
		corpInteractiveRadarUserRecord.setRadarId(radarId);
		Integer type = radar.getType();
		if (type == 1 && userId != null) {
			//给企业微信客服发送消息
			WxCorpApp wxCorpApp = wxCorpAppMapper.selectById(radar.getWxCorpId());
			CorpUser corpUser = corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class).eq(CorpUser::getUserId, userId));
			if (corpUser != null) {
				//记录点击
				corpInteractiveRadarUserRecord.setCorpUserId(corpUser.getId());
				CorpUserAuth corpUserAuth = corpUserAuthMapper.selectOne(Wrappers.lambdaQuery(CorpUserAuth.class)
						.eq(CorpUserAuth::getWxCorpId, wxCorpApp.getId())
						.eq(CorpUserAuth::getCorpUserId, corpUser.getId())
						.last(" limit 1"));
				String externalUserid = corpUserAuth.getExternalUserid();
				List<CorpUserFollowRelation> corpUserFollowRelations = corpUserFollowRelationMapper.selectList(Wrappers.lambdaQuery(CorpUserFollowRelation.class).eq(CorpUserFollowRelation::getCorpUserId, externalUserid));

				//发送图文消息
				WxCorpAgentMessage agentMessage = new WxCorpAgentMessage();
				agentMessage.setAgentid(Integer.valueOf(wxCorpApp.getAgentId()));
				agentMessage.setTouser(corpUserFollowRelations.stream().map(CorpUserFollowRelation::getFollowId).collect(Collectors.joining("|")));
				agentMessage.setMsgtype("news");

				//图片信息
				NewsArticle article = new NewsArticle();
				article.setUrl(radar.getUrl());
				String linkInfoStr = radar.getLinkInfoStr();
				CorpInteractiveReaderLink linkInfo = Jsons.parseObject(linkInfoStr, CorpInteractiveReaderLink.class);
				article.setDescription(linkInfo.getDesc());
				article.setTitle(linkInfo.getTitle());
				article.setPicurl(linkInfo.getImg());

				List<NewsArticle> articles = new ArrayList<>();
				articles.add(article);
				WxCorpAgentMessage.News news = new WxCorpAgentMessage.News();
				news.setArticles(articles);
				agentMessage.setNews(news);
				//发送卡片消息
				wxCorpOps.sendAgentMessage(wxCorpApp.getCorpId(), agentMessage);
			}
		}
		corpInteractiveRadarUserRecordMapper.insert(corpInteractiveRadarUserRecord);
	}
}
