package com.gxwebsoft.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.cms.entity.ArticleComment;
import com.gxwebsoft.cms.entity.ArticleLike;
import com.gxwebsoft.cms.mapper.ArticleCommentMapper;
import com.gxwebsoft.cms.param.ArticleCommentParam;
import com.gxwebsoft.cms.service.ArticleCommentService;
import com.gxwebsoft.cms.service.ArticleLikeService;
import com.gxwebsoft.cms.service.ArticleService;
import com.gxwebsoft.common.core.utils.CommonUtil;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 文章评论表Service实现
 *
 * @author 科技小王子
 * @since 2023-07-07 14:14:35
 */
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleComment> implements ArticleCommentService {

    @Resource
    private ArticleLikeService articleLikeService;

    @Resource
    private ArticleService articleService;



    @Override
    public PageResult<ArticleComment> pageRel(ArticleCommentParam param) {
        PageParam<ArticleComment, ArticleCommentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<ArticleComment> list = baseMapper.selectPageRel(page, param);

        // 获取带点赞状态和评论的文章列表
        if (param.getSceneType() != null && param.getSceneType().equals("UN_READ_COMMENT")) {
            // 更新为已读
            if(!CollectionUtils.isEmpty(list)){
                Set<Integer> commentIds = list.stream().map(ArticleComment::getCommentId).collect(Collectors.toSet());
                LambdaUpdateWrapper<ArticleComment> updateWrapper = new LambdaUpdateWrapper<ArticleComment>()
                        .in(ArticleComment::getCommentId, commentIds)
                        .set(ArticleComment::getStatus, 1);
                baseMapper.update(null, updateWrapper);
            }
            return new PageResult<>(list, page.getTotal());
        }

        // 我点赞的人
        final List<ArticleLike> myLikes = articleLikeService.list(new LambdaQueryWrapper<ArticleLike>().eq(ArticleLike::getUserId, param.getLoginUserId()));
        list.forEach(d -> {
          final boolean isLike = myLikes.stream().anyMatch(f -> f.getUserId().equals(param.getLoginUserId()));
          d.setLikes(isLike);
        });
        // List转为树形结构
        final List<ArticleComment> articleComments = CommonUtil.toTreeData(list, 0, ArticleComment::getReplyCommentId, ArticleComment::getCommentId, ArticleComment::setChildren);
        return new PageResult<>(articleComments, page.getTotal());
    }

    @Override
    public List<ArticleComment> listRel(ArticleCommentParam param) {
        List<ArticleComment> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<ArticleComment, ArticleCommentParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public ArticleComment getByIdRel(Integer commentId) {
        ArticleCommentParam param = new ArticleCommentParam();
        param.setCommentId(commentId);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public int getUserUnReadCount(Integer userId) {
//        List<Article> list = articleService.list(new LambdaQueryWrapper<Article>().eq(Article::getUserId, userId).select(Article::getArticleId));
//        Set<Integer> articleIds = list.stream().map(Article::getArticleId).collect(Collectors.toSet());
        int count = baseMapper.selectCount(
                new LambdaQueryWrapper<>(ArticleComment.class)
//                        .in(ArticleComment::getArticleId, articleIds)
                        .eq(ArticleComment::getStatus, 0)
                        .eq(ArticleComment::getToUserId, userId)
        );
        return count;
    }

}
