package com.pz.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.domain.model.LoginUser;
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.*;
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.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) {
        LoginUser loginUser = LoginHelper.getLoginUser();

        ArticleVo articleVo = baseMapper.selectVoById(id);

        // 查询点赞数量和是否点赞
        LambdaQueryWrapper<UserAppreciate> appreciateWrapper = Wrappers.<UserAppreciate>lambdaQuery().eq(UserAppreciate::getAid, id);
        Long appreciateCount = userAppreciateMapper.selectCount(appreciateWrapper);
        appreciateWrapper.eq(UserAppreciate::getUid, loginUser.getUserId());
        boolean appreciated = userAppreciateMapper.selectCount(appreciateWrapper) != 0;
        articleVo.setAppreciate(appreciated);
        articleVo.setAppreciateNum(Math.toIntExact(appreciateCount));

        // 查询收藏数量和是否收藏
        LambdaQueryWrapper<UserCollect> collectWrapper = Wrappers.<UserCollect>lambdaQuery().eq(UserCollect::getAid, id);
        Long collectCount = userCollectMapper.selectCount(collectWrapper);
        collectWrapper.eq(UserCollect::getUid, loginUser.getUserId());
        boolean collected = userCollectMapper.selectCount(collectWrapper) != 0;
        articleVo.setCollect(collected);
        articleVo.setCollectNum(Math.toIntExact(collectCount));

        // 查询用户评论
        List<ArticleCommentVo> articleCommentVos = articleCommentMapper.selectByArticleComment(id);

        // 查询用户信息并设置到评论中
        /*if (articleCommentVos != null && !articleCommentVos.isEmpty()) {
            Set<Long> userIds = new HashSet<>();
            for (ArticleCommentVo articleCommentVo : articleCommentVos) {
                Long uid = articleCommentVo.getUid();
                if (uid != null) {
                    userIds.add(uid);
                }
            }

            Map<Long, SysUser> sysUserMap = new HashMap<>();
            if (!userIds.isEmpty()) {
                List<SysUser> sysUsers = sysUserMapper.selectBatchIds(userIds);
                for (SysUser sysUser : sysUsers) {
                    sysUserMap.put(sysUser.getUserId(), sysUser);
                }
            }

            for (ArticleCommentVo articleCommentVo : articleCommentVos) {
                Long uid = articleCommentVo.getUid();
                if (uid != null) {
                    SysUser sysUser = sysUserMap.get(uid);
                    if (sysUser != null) {
                        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 -> {
            articleVos.forEach(articleVo -> {
                List<ArticleCommentVo> articleCommentVos = articleCommentMapper.selectVoList(Wrappers.<ArticleComment>lambdaQuery().eq(ArticleComment::getAid, articleVo.getId()));
                if (CollectionUtils.isNotEmpty(articleCommentVos)) {
                    articleVo.setPlNum(articleCommentVos.size());
                }
            });
            // 获取所有文章id集合
            List<Integer> articleIds = articleVos.stream()
                .map(ArticleVo::getId)
                .collect(Collectors.toList());

            List<Integer> cityIds = articleVos.stream()
                .map(ArticleVo::getCityId)
                .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(cityIds);
                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);
                bo.setCityId(null);
                lqw = buildQueryWrapper(bo);
                lqw.select(Article.class, info -> !info.getColumn().equals("content"));

                result = baseMapper.selectVoPage(pageQuery.build(), lqw);
            }
        } else if (bo.getIsCollect() == 0) {
            lqw.select(Article.class, info -> !info.getColumn().equals("content"));
            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);
                    List<String> ids = new ArrayList<>();
                    // 使用逗号分割字符串,并逐个转换为 Integer 添加到 List 中
                    if (null != articleVo.getCover()) {
                        Collections.addAll(ids, articleVo.getCover().split(","));
                        articleVo.setCoverList(ids);
                    }
                    // 设置点赞数
                    articleVo.setAppreciateNum(appreciateNumMap.getOrDefault(articleVo.getId(), 0));

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

                    //是否点赞
                    LambdaQueryWrapper<UserAppreciate> appreciateWrapper = Wrappers.<UserAppreciate>lambdaQuery()
                    .eq(UserAppreciate::getAid, articleVo.getId());
                    appreciateWrapper.eq(UserAppreciate::getUid, LoginHelper.getLoginUser().getUserId());
                    boolean appreciated = userAppreciateMapper.selectCount(appreciateWrapper) != 0;
                    articleVo.setAppreciate(appreciated);

                });
            }
        });
        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);
        add.setUid(bo.getUid());
        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;
    }
}