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

import com.xindazhou.business.dal.community.dataobject.topic.TopicDO;
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.admin.community.topic.vo.TopicPageReqVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 话题 Mapper
 *
 * @author xindazhou
 */
@Mapper
public interface CommunityTopicMapper extends BaseMapperX<TopicDO> {

    default PageResult<TopicDO> selectPage(TopicPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<TopicDO>()
                .likeIfPresent(TopicDO::getName, reqVO.getName())
                .eqIfPresent(TopicDO::getStatus, reqVO.getStatus())
                .orderByAsc(TopicDO::getSort)
                .orderByDesc(TopicDO::getCreateTime));
    }

    /**
     * 查询启用的话题列表
     */
    default List<TopicDO> selectEnabledList() {
        return selectList(new LambdaQueryWrapperX<TopicDO>()
                .eq(TopicDO::getStatus, 1)
                .orderByAsc(TopicDO::getSort));
    }

    /**
     * 根据名称查询话题
     */
    default TopicDO selectByName(String name) {
        return selectOne(new LambdaQueryWrapperX<TopicDO>()
                .eq(TopicDO::getName, name));
    }

    /**
     * 增加帖子数
     * 注意：需要在Service层实现，先查询再更新
     */
    default void incrementPostCount(Long id) {
        // 需要在Service层实现，先查询再更新
        // TopicDO topic = selectById(id);
        // if (topic != null) {
        //     topic.setPostCount(topic.getPostCount() + 1);
        //     updateById(topic);
        // }
    }

    /**
     * 减少帖子数
     */
    default void decrementPostCount(Long id) {
        // 需要在Service层实现
    }

}

