package com.pz.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pz.common.core.domain.entity.SysUser;
import com.pz.common.core.page.TableDataInfo;
import com.pz.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pz.common.helper.LoginHelper;
import com.pz.common.utils.JsonUtils;
import com.pz.system.domain.ArticleComment;
import com.pz.system.domain.UserAppreciate;
import com.pz.system.domain.UserCollect;
import com.pz.system.domain.bo.ArticleCommentBo;
import com.pz.system.domain.bo.UserAppreciateBo;
import com.pz.system.domain.bo.UserCollectBo;
import com.pz.system.domain.vo.ArticleCommentVo;
import com.pz.system.domain.vo.CityVo;
import com.pz.system.mapper.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.pz.system.domain.bo.ArticleBo;
import com.pz.system.domain.vo.ArticleVo;
import com.pz.system.domain.Article;
import com.pz.system.service.IArticleService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 文章Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@RequiredArgsConstructor
@Service
public class ArticleServiceImpl implements IArticleService {

    private final ArticleMapper baseMapper;

    private final CityMapper cityMapper;

    private final UserAppreciateMapper userAppreciateMapper;

    private final UserCollectMapper userCollectMapper;

    private final ArticleCommentMapper articleCommentMapper;

    private final SysUserMapper sysUserMapper;

    /**
     * 查询文章
     */
    @Override
    public ArticleVo queryById(Integer id) {
        ArticleVo articleVo = baseMapper.selectVoById(id);
        Long aLong = userAppreciateMapper.selectCount(Wrappers.<UserAppreciate>lambdaQuery().eq(UserAppreciate::getAid, id));
        Long aLong1 = userCollectMapper.selectCount(Wrappers.<UserCollect>lambdaQuery().eq(UserCollect::getAid, id));
        //是否点赞
        articleVo.setAppreciate(aLong != 0);
        //点赞数量
        articleVo.setAppreciateNum(Math.toIntExact(aLong));
        //是否收藏
        articleVo.setCollect(aLong1 != 0);
        //收藏数量
        articleVo.setCollectNum(Math.toIntExact(aLong1));
        //用户评论
        List<ArticleCommentVo> articleCommentVos = articleCommentMapper.selectVoList(Wrappers.<ArticleComment>lambdaQuery().eq(ArticleComment::getAid, id).eq(ArticleComment::getUid, LoginHelper.getLoginUser().getUserId().intValue()));

        for (ArticleCommentVo articleCommentVo : articleCommentVos) {
            if(null != articleCommentVo.getUid()){
                SysUser sysUser = sysUserMapper.selectById(LoginHelper.getLoginUser().getUserId().intValue());
                articleCommentVo.setAvatar(sysUser.getAvatar());
                articleCommentVo.setUserName(sysUser.getUserName());
            }
        }
        articleVo.setComment(articleCommentVos);
        return articleVo;
    }

    /**
     * 查询文章列表
     */
    @Override
    public TableDataInfo<ArticleVo> queryPageList(ArticleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Article> lqw = buildQueryWrapper(bo);
        Page<ArticleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Optional.ofNullable(result.getRecords()).ifPresent(articleVos -> {
            // 获取所有文章id集合
            List<Integer> articleIds = articleVos.stream()
                .map(ArticleVo::getId)
                .collect(Collectors.toList());

            // 查询城市名称
            Map<Integer, String> cityMap = new HashMap<>();
            // 查询点赞和收藏数
            Map<Integer, Integer> appreciateNumMap = new HashMap<>();
            Map<Integer, Integer> collectNumMap = new HashMap<>();

            if(CollectionUtils.isNotEmpty(articleIds)){
                List<CityVo> cityVos = cityMapper.selectVoBatchIds(articleIds);
                for (CityVo cityVo : cityVos) {
                    cityMap.put(cityVo.getId(), cityVo.getName());
                }
                List<UserAppreciate> userAppreciates = userAppreciateMapper.selectList(Wrappers.<UserAppreciate>lambdaQuery().in(UserAppreciate::getAid, articleIds));
                List<UserCollect> userCollects = userCollectMapper.selectList(Wrappers.<UserCollect>lambdaQuery().in(UserCollect::getAid, articleIds));

                for (UserAppreciate userAppreciate : userAppreciates) {
                    Integer articleId = userAppreciate.getAid();
                    appreciateNumMap.put(articleId, appreciateNumMap.getOrDefault(articleId, 0) + 1);
                }

                for (UserCollect userCollect : userCollects) {
                    Integer articleId = userCollect.getAid();
                    collectNumMap.put(articleId, collectNumMap.getOrDefault(articleId, 0) + 1);
                }
            }

            // 设置城市名称、点赞数和收藏数
            articleVos.forEach(articleVo -> {
                // 设置城市名称
                articleVo.setCityName(cityMap.get(articleVo.getCityId()));

                // 设置点赞数
                articleVo.setAppreciateNum(appreciateNumMap.getOrDefault(articleVo.getId(), 0));

                // 设置收藏数
                articleVo.setCollectNum(collectNumMap.getOrDefault(articleVo.getId(), 0));
            });
        });
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<ArticleVo> queryPageListByApp(ArticleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        //Page<ArticleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // TODO 待登录写完修改
        //int userId = LoginHelper.getLoginUser().getUserId().intValue();
        int userId = 1;
        Page<ArticleVo> result = new Page<>();
        //判断是否展示收藏文章
        if(bo.getIsCollect() == 1){
            List<UserCollect> Collect = userCollectMapper.selectList(Wrappers.<UserCollect>lambdaQuery().eq(UserCollect::getUid, userId).eq(UserCollect::getDelFlag,0));

            // 获取所有文章id集合
            List<Integer> articleIds = Collect.stream()
                .map(UserCollect::getAid)
                .collect(Collectors.toList());

            if(articleIds.size() != 0) {

                //查询用户收藏的所有文章
                bo.setArticleId(articleIds);
                lqw = buildQueryWrapper(bo);

                result = baseMapper.selectVoPage(pageQuery.build(), lqw);
            }
        }else if(bo.getIsCollect() == 0){
            result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        }
        Optional.ofNullable(result.getRecords()).ifPresent(articleVos -> {
            // 获取所有文章id集合
            List<Integer> articleIds = articleVos.stream()
                .map(ArticleVo::getId)
                .collect(Collectors.toList());
            // 查询点赞和收藏数
            Map<Integer, Integer> appreciateNumMap = new HashMap<>();
            Map<Integer, Integer> collectNumMap = new HashMap<>();

            if (CollectionUtils.isNotEmpty(articleIds)) {

                List<UserAppreciate> userAppreciates = userAppreciateMapper.selectList(Wrappers.<UserAppreciate>lambdaQuery().in(UserAppreciate::getAid, articleIds).eq(UserAppreciate::getDelFlag,0));
                List<UserCollect> userCollects = userCollectMapper.selectList(Wrappers.<UserCollect>lambdaQuery().in(UserCollect::getAid, articleIds).eq(UserCollect::getDelFlag,0));

                for (UserAppreciate userAppreciate : userAppreciates) {
                    Integer articleId = userAppreciate.getAid();
                    appreciateNumMap.put(articleId, appreciateNumMap.getOrDefault(articleId, 0) + 1);
                }

                for (UserCollect userCollect : userCollects) {
                    Integer articleId = userCollect.getAid();
                    collectNumMap.put(articleId, collectNumMap.getOrDefault(articleId, 0) + 1);
                }

                // 设置城市名称、点赞数和收藏数
                articleVos.forEach(articleVo -> {
                   /* // 去除方括号
                    String numbers = articleVo.getCover().substring(1, articleVo.getCover().length() - 1);

                    // 使用逗号分割字符串
                    String[] numberArray = numbers.split(", ");

                    // 创建 List<Integer> 并将转换后的整数加入列表中
                    List<String> integerList = new ArrayList<>();
                    for (String num : numberArray) {
                        integerList.add(num.trim());
                    }*/
                    List<String> ids = JsonUtils.parseArray(articleVo.getCover(), String.class);
                    articleVo.setCoverList(ids);
                    // 设置点赞数
                    articleVo.setAppreciateNum(appreciateNumMap.getOrDefault(articleVo.getId(), 0));

                    // 设置收藏数
                    articleVo.setCollectNum(collectNumMap.getOrDefault(articleVo.getId(), 0));

                });
            }
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询文章列表
     */
    @Override
    public List<ArticleVo> queryList(ArticleBo bo) {
        LambdaQueryWrapper<Article> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Article> buildQueryWrapper(ArticleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Article> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), Article::getTitle, bo.getTitle());
        lqw.eq(!Objects.isNull(bo.getCityId()), Article::getCityId, bo.getCityId());
        if(null != bo.getArticleId() && bo.getArticleId().size() != 0){
            lqw.in(Article::getId, bo.getArticleId());
        }
        return lqw;
    }

    /**
     * 新增点赞
     */
    @Override
    public Boolean insertByAppreciate(UserAppreciateBo bo) {
        UserAppreciate add = BeanUtil.toBean(bo, UserAppreciate.class);
        UserAppreciate userAppreciate = userAppreciateMapper.selectOne(new LambdaQueryWrapper<UserAppreciate>().eq(UserAppreciate::getUid, bo.getUid()).eq(UserAppreciate::getAid,bo.getAid()).eq(UserAppreciate::getDelFlag,0));
        if(null != userAppreciate){
            userAppreciateMapper.deleteById(userAppreciate.getId());
            return true;
        }
        boolean flag = userAppreciateMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 新增收藏
     */
    @Override
    public Boolean insertByCollect(UserCollectBo bo) {
        UserCollect add = BeanUtil.toBean(bo, UserCollect.class);
        UserCollect userCollect = userCollectMapper.selectOne(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getUid, bo.getUid()).eq(UserCollect::getAid,bo.getAid()).eq(UserCollect::getDelFlag,0));
        if(null != userCollect){
            userCollectMapper.deleteById(userCollect.getId());
            return true;
        }
        boolean flag = userCollectMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 新增评论
     */
    @Override
    public Boolean insertByComment(ArticleCommentBo bo) {
        ArticleComment add = BeanUtil.toBean(bo, ArticleComment.class);

        boolean flag = articleCommentMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 新增文章
     */
    @Override
    public Boolean insertByBo(ArticleBo bo) {
        Article add = BeanUtil.toBean(bo, Article.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文章
     */
    @Override
    public Boolean updateByBo(ArticleBo bo) {
        Article update = BeanUtil.toBean(bo, Article.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Article entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文章
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}