package com.xindazhou.business.dal.community.mysql.comment;

import com.xindazhou.business.dal.community.dataobject.comment.CommentDO;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.framework.mybatis.core.mapper.BaseMapperX;
import com.xindazhou.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xindazhou.business.controller.app.community.comment.vo.CommentPageReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 评论 Mapper
 *
 * @author xindazhou
 */
@Mapper
public interface CommunityCommentMapper extends BaseMapperX<CommentDO> {

    default PageResult<CommentDO> selectPage(CommentPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CommentDO>()
                .eqIfPresent(CommentDO::getPostId, reqVO.getPostId())
                .eqIfPresent(CommentDO::getUserId, reqVO.getUserId())
                .eqIfPresent(CommentDO::getParentId, reqVO.getParentId())
                .eqIfPresent(CommentDO::getStatus, reqVO.getStatus())
                .orderByDesc(CommentDO::getCreateTime));
    }

    default PageResult<CommentDO> selectPageAdmin(com.xindazhou.business.controller.admin.community.comment.vo.CommentPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CommentDO>()
                .eqIfPresent(CommentDO::getPostId, reqVO.getPostId())
                .eqIfPresent(CommentDO::getUserId, reqVO.getUserId())
                .eqIfPresent(CommentDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(CommentDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CommentDO::getCreateTime));
    }

    /**
     * 查询帖子的顶级评论列表
     */
    default List<CommentDO> selectTopLevelByPostId(Long postId) {
        return selectList(new LambdaQueryWrapperX<CommentDO>()
                .eq(CommentDO::getPostId, postId)
                .eq(CommentDO::getParentId, 0)
                .eq(CommentDO::getStatus, 1)
                .orderByDesc(CommentDO::getCreateTime));
    }

    /**
     * 查询评论的回复列表
     */
    default List<CommentDO> selectRepliesByParentId(Long parentId) {
        return selectList(new LambdaQueryWrapperX<CommentDO>()
                .eq(CommentDO::getParentId, parentId)
                .eq(CommentDO::getStatus, 1)
                .orderByAsc(CommentDO::getCreateTime));
    }

    // 注意：增量更新需要在Service层实现，先查询再更新

}

