package com.ruoyi.generator.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.generator.domain.Community.*;
import com.ruoyi.generator.mapper.community.*;
import com.ruoyi.generator.vo.CommunityCollectionArticleVo;
import com.ruoyi.generator.vo.CommunityCollectionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class CommunityCollectionServiceImpl extends ServiceImpl<CommunityCollectionMapper, CommunityCollection> implements ICommunityCollectionService {

    @Autowired
    private CommunityCollectionArticleMapper collectionArticleMapper;

    @Autowired
    private CommunityArticleMapper communityArticleMapper;

    @Autowired
    private CommunityLikeMapper communityLikeMapper;

    @Autowired
    private CommunityArticleCommentMapper communityArticleCommentMapper;

    @Autowired
    private CommunityArticleImagesMapper communityArticleImagesMapper;

    @Autowired
    private CommunityCollectionMapper communityCollectionMapper;

    @Autowired
    private CommunityCollectionArticleMapper communityCollectionArticleMapper;

    /**
     * 添加文章到用户合集中
     *
     * @param collectionArticle 合集列表
     */
    @Override
    public void addArticleToCollection(CommunityCollectionArticle collectionArticle) {
        collectionArticleMapper.insert(collectionArticle);
    }

    /**
     * 删除合集中的某个文章
     *
     * @param collectionId 合集id
     * @param articleId    文章id
     */
    @Override
    public void deleteArticleToCollection(Long collectionId, Long articleId) {
        //删除合集中的文章
        CommunityCollectionArticle collectionArticle = new CommunityCollectionArticle();
        collectionArticle.setDelete(true);
        collectionArticle.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
        collectionArticle.setUpdateBy(SecurityUtils.getUserId());
        communityCollectionArticleMapper.update(collectionArticle, new UpdateWrapper<CommunityCollectionArticle>()
                .eq("collection_id", collectionId)
                .eq("article_id", articleId).and((wrapper) -> {
                    wrapper.ne("is_delete", true)
                            .or()
                            .isNull("is_delete");
                }));
    }

    /**
     * 根据合集id,查询合集下所有文章信息
     *
     * @param collectionId 合集id
     * @return 文章信息
     */
    @Override
    public List<CommunityCollectionArticleVo> selectArticleInfoInCollection(Long collectionId) {
        List<Object> articleIdsObj = collectionArticleMapper
                .selectObjs(new QueryWrapper<CommunityCollectionArticle>()
                        .select("article_id")
                        .eq("collection_id", collectionId)
                        .and((wrapper) -> {
                            wrapper.ne("is_delete", true)
                                    .or()
                                    .isNull("is_delete");
                        }));

        if (articleIdsObj.isEmpty()) {
            return null;
        }

        //转换object为long
        List<Long> articleIds = articleIdsObj.stream()
                .filter(item -> item instanceof Long)
                .map(item -> (Long) item)
                .collect(Collectors.toList());
        List<CommunityArticle> communityArticles = communityArticleMapper.selectBatchIds(articleIds);

        List<CommunityCollectionArticleVo> collectionArticleVos = new ArrayList<>();
        CommunityCollectionArticleVo collectionArticleVo = null;
        for (CommunityArticle communityArticle : communityArticles) {
            collectionArticleVo = new CommunityCollectionArticleVo();
            collectionArticleVo.setArticleId(communityArticle.getId());
            collectionArticleVo.setCollectionId(collectionId);
            collectionArticleVo.setTitle(communityArticle.getTitle());
            collectionArticleVo.setArticleCreateTime(communityArticle.getCreateTime());

            //设置用户点赞数
            int likeCount = communityLikeMapper.selectCount(new QueryWrapper<CommunityLike>().eq("article_id", communityArticle.getId())).intValue();
            collectionArticleVo.setLikeCount(likeCount);
            //设置用户评论数
            int commentCount = communityArticleCommentMapper.selectCount(new QueryWrapper<CommunityArticleComment>().eq("article_id", communityArticle.getId())).intValue();
            collectionArticleVo.setCommentCount(commentCount);
            //设置文章封面
            List<CommunityArticleImages> articleImages = communityArticleImagesMapper.selectList(new QueryWrapper<CommunityArticleImages>()
                    .select("image_url")
                    .eq("article_id", communityArticle.getId()));
            if (!articleImages.isEmpty()) {
                collectionArticleVo.setImageUrl(articleImages.get(0).getImageUrl());
            }

            collectionArticleVos.add(collectionArticleVo);
        }
        return collectionArticleVos;
    }

    /**
     * 删除合集
     *
     * @param collectionId 合集id
     */
    @Override
    public void deleteCollectionById(Long collectionId) {
        //1.更新合集状态
        CommunityCollection collection = communityCollectionMapper.selectOne(new QueryWrapper<CommunityCollection>()
                .eq("id", collectionId)
                .and((wrapper) -> {
                    wrapper.ne("is_delete", true)
                            .or()
                            .isNull("is_delete");
                }));
        collection.setDelete(true);
        collection.setUpdateBy(SecurityUtils.getLoginUser().getUserId());
        collection.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
        communityCollectionMapper.updateById(collection);
        //2.更新合集关联的文章状态
        List<Long> ids = communityCollectionArticleMapper.selectObjs(new QueryWrapper<CommunityCollectionArticle>()
                .eq("collection_id", collectionId)
                .and((wrapper) -> {
                    wrapper .ne("is_delete", true)
                            .or()
                            .isNull("is_delete");
                })).stream().map(item -> (Long) item).collect(Collectors.toList());
        CommunityCollectionArticle collectionArticle = new CommunityCollectionArticle();
        collectionArticle.setDelete(true);
        collectionArticle.setUpdateBy(SecurityUtils.getLoginUser().getUserId());
        collectionArticle.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
        communityCollectionArticleMapper.update(collectionArticle, new UpdateWrapper<CommunityCollectionArticle>().in("id", ids));
    }

    @Override
    public List<CommunityCollectionVo> selectUserCollection(Long userId) {
        return communityCollectionMapper.selectUserCollection(userId);
    }


}
