package com.gxwebsoft.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.cms.entity.Article;
import com.gxwebsoft.cms.entity.ArticleLike;
import com.gxwebsoft.cms.mapper.ArticleMapper;
import com.gxwebsoft.cms.param.ArticleParam;
import com.gxwebsoft.cms.service.ArticleLikeService;
import com.gxwebsoft.cms.service.ArticleService;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.UserFollow;
import com.gxwebsoft.shop.service.UserFollowService;
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 WebSoft
 * @since 2022-11-16 11:40:27
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Resource
    private UserFollowService userFollowService;
    @Resource
    private ArticleLikeService articleLikeService;

    @Override
    public PageResult<Article> pageRel(ArticleParam param) {
        PageParam<Article, ArticleParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        // 搜素条件
        if (param.getScene() != null) {
          // 最新
          if (param.getScene().equals("new")) {
            param.setCity(null);
            param.setUserIds(null);
          }
          // 同城intraCity
          if (param.getScene().equals("intraCity")) {
            param.setCity(param.getCity());
          }
          // 关注focus
          if (param.getScene().equals("focus")) {
            final List<UserFollow> myFocus = userFollowService.list(new LambdaQueryWrapper<UserFollow>().eq(UserFollow::getUserId, param.getLoginUserId()));
            if (!CollectionUtils.isEmpty(myFocus)) {
              final Set<Integer> collect = myFocus.stream().map(UserFollow::getShopId).collect(Collectors.toSet());
              param.setUserIds(collect);
            }
          }
        }
        // 数据列表
        List<Article> list = baseMapper.selectPageRel(page, param);
        // 我关注的人
        final List<UserFollow> myFollows = userFollowService.list(new LambdaQueryWrapper<UserFollow>().eq(UserFollow::getUserId, param.getLoginUserId()));
        // 我点赞的文章
        final List<ArticleLike> myLikes = articleLikeService.list(new LambdaQueryWrapper<ArticleLike>().eq(ArticleLike::getUserId, param.getLoginUserId()));
        // 是否显示关注状态
        if(param.getShowFollow() != null){
          list.forEach(d -> {
            // 是否关注
            final boolean isFollows = myFollows.stream().filter(f -> f.getShopId().equals(d.getUserId())).findFirst().isPresent();
            final boolean isLike = myLikes.stream().filter(f -> f.getArticleId().equals(d.getArticleId())).findFirst().isPresent();
            d.setFollow(isFollows);
            d.setLiked(isLike);
          });
        }
        return new PageResult<>(list, page.getTotal());
    }

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

    @Override
    public Article getByIdRel(Integer articleId) {
        ArticleParam param = new ArticleParam();
        param.setArticleId(articleId);
        return param.getOne(baseMapper.selectListRel(param));
    }

}
