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

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

/**
 * 帖子 Mapper
 *
 * @author xindazhou
 */
@Mapper
public interface CommunityPostMapper extends BaseMapperX<PostDO> {

    default PageResult<PostDO> selectPage(PostPageReqVO reqVO) {
        LambdaQueryWrapperX<PostDO> wrapper = new LambdaQueryWrapperX<PostDO>()
                .eq(PostDO::getStatus, 1) // 只查询正常状态的帖子
                .eqIfPresent(PostDO::getUserId, reqVO.getUserId())
                .likeIfPresent(PostDO::getTitle, reqVO.getTitle())
                .eqIfPresent(PostDO::getTopicId, reqVO.getTopicId())
                .eqIfPresent(PostDO::getCity, reqVO.getCity());
        
        // 关键词搜索（使用全文索引 MATCH...AGAINST）
        if (reqVO.getKeyword() != null && !reqVO.getKeyword().trim().isEmpty()) {
            String keyword = reqVO.getKeyword().trim();
            // 使用全文索引进行搜索，性能优于 LIKE
            // MATCH(title, content) AGAINST('keyword' IN NATURAL LANGUAGE MODE)
            // 注意：如果全文索引不存在，此查询会失败，需要先执行 SQL 脚本创建索引
            wrapper.apply("MATCH(title, content) AGAINST({0} IN NATURAL LANGUAGE MODE)", keyword);
        }
        
        // 分类筛选
        if ("official".equals(reqVO.getCategory())) {
            // 官方：需要根据用户角色判断，暂时通过 userId 或其他标识
            // TODO: 需要增加官方账号标识字段或通过用户服务判断
        } else if ("hot".equals(reqVO.getCategory())) {
            // 热门：按热度排序（点赞数+评论数+浏览数）
            wrapper.orderByDesc(PostDO::getIsTop)
                    .orderByDesc(PostDO::getLikeCount)
                    .orderByDesc(PostDO::getCommentCount)
                    .orderByDesc(PostDO::getViewCount)
                    .orderByDesc(PostDO::getCreateTime);
        } else if ("latest".equals(reqVO.getCategory())) {
            // 最新：按时间排序
            wrapper.orderByDesc(PostDO::getIsTop)
                    .orderByDesc(PostDO::getCreateTime);
        } else if ("recommended".equals(reqVO.getCategory())) {
            // 推荐：按精华+热度排序
            wrapper.orderByDesc(PostDO::getIsEssence)
                    .orderByDesc(PostDO::getIsTop)
                    .orderByDesc(PostDO::getLikeCount)
                    .orderByDesc(PostDO::getCommentCount)
                    .orderByDesc(PostDO::getViewCount)
                    .orderByDesc(PostDO::getCreateTime);
        } else {
            // 默认：置顶+时间排序
            wrapper.orderByDesc(PostDO::getIsTop)
                    .orderByDesc(PostDO::getCreateTime);
        }
        
        // 自定义排序
        if ("popularity".equals(reqVO.getSort())) {
            wrapper.orderByDesc(PostDO::getLikeCount)
                    .orderByDesc(PostDO::getCommentCount)
                    .orderByDesc(PostDO::getViewCount);
        } else if ("time".equals(reqVO.getSort())) {
            wrapper.orderByDesc(PostDO::getCreateTime);
        }
        
        return selectPage(reqVO, wrapper);
    }

    default PageResult<PostDO> selectPageAdmin(com.xindazhou.business.controller.admin.community.post.vo.PostPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PostDO>()
                .eqIfPresent(PostDO::getStatus, reqVO.getStatus())
                .eqIfPresent(PostDO::getUserId, reqVO.getUserId())
                .likeIfPresent(PostDO::getTitle, reqVO.getTitle())
                .eqIfPresent(PostDO::getIsTop, reqVO.getIsTop())
                .eqIfPresent(PostDO::getIsEssence, reqVO.getIsEssence())
                .betweenIfPresent(PostDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PostDO::getIsTop)
                .orderByDesc(PostDO::getCreateTime));
    }

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

}

