package com.cyksj.service.mange.article.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.constant.RandomConstant;
import com.cyksj.mapper.mini.ArticleMapper;
import com.cyksj.mapper.mini.ArticleRecommendMapper;
import com.cyksj.model.entity.Article;
import com.cyksj.model.entity.ArticleRecommend;
import com.cyksj.service.mange.article.ArticleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

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

/*
 *项目名: netflix
 *文件名: ArticleServiceImpl
 *创建者: JavaZou
 *创建时间:2023/6/27 15:17
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
	private final ArticleMapper articleMapper;

	private final ArticleRecommendMapper articleRecommendMapper;



	@Override
	public void saveOrUpdateArticle(Article articleDto) {
		Long id = articleDto.getId();
		List<Long> otherArticleIds = articleDto.getOtherArticleIds();

		if (id == null) {
			articleDto.setAuthor(RandomConstant.getName());
			articleDto.setHeadImgUrl(RandomConstant.getHeadUrlImg());
			articleDto.setDzNum(RandomUtil.randomInt(200) + 1);
			articleDto.setColNum(RandomUtil.randomInt(50) + 1);
			articleMapper.insert(articleDto);
			if (CollUtil.isNotEmpty(otherArticleIds)) {
				otherArticleIds.forEach(otherArticleId -> {
					saveArticleRecommendRelation(articleDto.getId(), otherArticleId);
				});
			}
			return;
		}
		articleMapper.updateById(articleDto);

		List<ArticleRecommend> articleRecommends = articleRecommendMapper.selectList(Wrappers.lambdaQuery(ArticleRecommend.class).eq(ArticleRecommend::getRecommendId, articleDto.getId()));
		Map<Long, List<ArticleRecommend>> collect = articleRecommends.stream().collect(Collectors.groupingBy(ArticleRecommend::getArticleId));
		if (CollUtil.isEmpty(otherArticleIds)) {
			articleRecommendMapper.delete(Wrappers.lambdaQuery(ArticleRecommend.class)
					.eq(ArticleRecommend::getRecommendId, articleDto.getId()));
		}
		otherArticleIds.forEach(otherArticleId -> {
			List<ArticleRecommend> recommends = collect.get(otherArticleId);
			if (CollUtil.isEmpty(recommends)) {
				saveArticleRecommendRelation(articleDto.getId(), otherArticleId);
			}
		});
	}

	@Override
	public Article getArticleDetail(Long id) {
		return articleMapper.getArticleDetail(id);
	}

	public void saveArticleRecommendRelation(Long recommendId, Long otherArticleId) {
		if (recommendId == otherArticleId) return;
		Article article = articleMapper.selectById(otherArticleId);
		if (article == null) return;
		ArticleRecommend articleRecommend = new ArticleRecommend();
		articleRecommend.setArticleId(otherArticleId);
		articleRecommend.setRecommendId(recommendId);
		try {
			articleRecommendMapper.insert(articleRecommend);
		} catch (DuplicateKeyException e) {

		}
	}
}
