package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.domain.communityNews.CommentContent;
import org.dromara.domain.communityNews.CommentIndex;
import org.dromara.domain.communityNews.CommunityNews;
import org.dromara.domain.communityNews.bo.CommentContentBo;
import org.dromara.domain.communityNews.vo.CommentContentVo;
import org.dromara.domain.communityNews.vo.CommentInteractionVo;
import org.dromara.mapper.CommentContentMapper;
import org.dromara.mapper.CommentIndexMapper;
import org.dromara.mapper.CommunityNewsMapper;
import org.dromara.service.ICommentContentService;
import org.dromara.service.ICommunityNewsService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.dromara.common.core.constant.Constants.*;

/**
 * 社区资讯评论内容Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class CommentContentServiceImpl implements ICommentContentService {

    private final CommentContentMapper baseMapper;
    private final CommentIndexMapper commentIndexMapper;
    private final CommunityNewsMapper communityNewsMapper;
    private final ICommunityNewsService communityNewsService;

    /**
     * 查询社区资讯评论内容
     *
     * @param contentId 主键
     * @return 社区资讯评论内容
     */
    @Override
    public CommentContentVo queryById(Long contentId){
        return baseMapper.selectVoById(contentId);
    }

    /**
     * 分页查询社区资讯评论内容列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 社区资讯评论内容分页列表
     */
    @Override
    public TableDataInfo<CommentContentVo> queryPageList(CommentContentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CommentContent> lqw = buildQueryWrapper(bo);
        Page<CommentContentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的社区资讯评论内容列表
     *
     * @param bo 查询条件
     * @return 社区资讯评论内容列表
     */
    @Override
    public List<CommentContentVo> queryList(CommentContentBo bo) {
        LambdaQueryWrapper<CommentContent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CommentContent> buildQueryWrapper(CommentContentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CommentContent> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CommentContent::getContentId);
        lqw.eq(bo.getCommentId() != null, CommentContent::getCommentId, bo.getCommentId());
        lqw.eq(bo.getUserId() != null, CommentContent::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getCommentContent()), CommentContent::getCommentContent, bo.getCommentContent());
        lqw.eq(bo.getStatus() != null, CommentContent::getStatus, bo.getStatus());
        lqw.eq(bo.getType() != null, CommentContent::getType, bo.getType());
        lqw.eq(bo.getLikeCount() != null, CommentContent::getLikeCount, bo.getLikeCount());
        return lqw;
    }

    /**
     * 新增社区资讯评论内容
     *
     * @param bo 社区资讯评论内容
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CommentContentBo bo) {
        CommentContent add = MapstructUtils.convert(bo, CommentContent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setContentId(add.getContentId());
        }
        return flag;
    }

    /**
     * 修改社区资讯评论内容
     *
     * @param bo 社区资讯评论内容
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CommentContentBo bo) {
        CommentContent commentContent = MapstructUtils.convert(bo, CommentContent.class);
        validEntityBeforeSave(commentContent);
        boolean flag = baseMapper.updateById(commentContent) > 0;
        if (TWO.equals(commentContent.getStatus())) {
            CommentIndex commentIndex = commentIndexMapper.selectById(commentContent.getCommentId());
            Long communityId = commentIndex.getCommunityId();
            Long userId = commentIndex.getUserId();
            //评论审核通过
            //------设置未读互动，评论的谁就给谁在redis中新增一条未读的互动--------
            CommentInteractionVo commentInteractionVo = new CommentInteractionVo();
            Integer isRoot = commentIndex.getIsRoot();
            //默认是子评论
            commentInteractionVo.setTargetType(FOR);
            commentInteractionVo.setTargetUserId(commentIndex.getToUserId());
            //默认标题是子评论的评论
            commentInteractionVo.setTargetTitle(commentIndex.getCommentContent());
            if (1 == isRoot) {
                //说明是根评论
                //查询该资讯是谁发的
                CommunityNews communityNews = communityNewsMapper.selectById(communityId);
                commentInteractionVo.setTargetUserId(communityNews.getUserId());
                commentInteractionVo.setTargetType(ONE);
                //社区资讯标题
                commentInteractionVo.setTargetTitle(commentIndex.getCommunityTitle());
            }
            commentInteractionVo.setUserId(userId);
            commentInteractionVo.setAvatar(commentIndex.getAvatar());
            commentInteractionVo.setNickName(commentIndex.getNickName());
            commentInteractionVo.setCreateTime(DateUtils.getNowDate());
            commentInteractionVo.setTargetId(commentIndex.getCommentId());
            //新增
            commentInteractionVo.setType(ONE);
            communityNewsService.setCommentInteraction(commentInteractionVo);
            //----------------------设置未读互动完成--------
        }

        return flag;
    }

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

    /**
     * 校验并批量删除社区资讯评论内容信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
