package com.yhy.module.core.dal.mysql.workbench;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yhy.module.core.dal.dataobject.workbench.WbProjectTreeDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 工作台项目管理树 Mapper
 *
 * @author yhy
 */
@Mapper
public interface WbProjectTreeMapper extends BaseMapperX<WbProjectTreeDO> {

    /**
     * 根据父节点ID查询子节点列表
     */
    default List<WbProjectTreeDO> selectListByParentId(Long parentId) {
        return selectList(new LambdaQueryWrapperX<WbProjectTreeDO>()
                .eqIfPresent(WbProjectTreeDO::getParentId, parentId)
                .orderByAsc(WbProjectTreeDO::getSortOrder));
    }

    /**
     * 查询所有节点（按排序）
     */
    default List<WbProjectTreeDO> selectAllOrderBySortOrder() {
        return selectList(new LambdaQueryWrapperX<WbProjectTreeDO>()
                .orderByAsc(WbProjectTreeDO::getSortOrder));
    }

    /**
     * 根据节点类型查询
     */
    default List<WbProjectTreeDO> selectListByNodeType(String nodeType) {
        return selectList(new LambdaQueryWrapperX<WbProjectTreeDO>()
                .eq(WbProjectTreeDO::getNodeType, nodeType)
                .orderByAsc(WbProjectTreeDO::getSortOrder));
    }

    /**
     * 根据项目编号查询
     */
    default WbProjectTreeDO selectByProjectCode(String projectCode) {
        return selectOne(new LambdaQueryWrapperX<WbProjectTreeDO>()
                .eq(WbProjectTreeDO::getProjectCode, projectCode)
                .eq(WbProjectTreeDO::getNodeType, WbProjectTreeDO.NODE_TYPE_PROJECT));
    }

    /**
     * 获取同级最大排序号
     */
    default Integer selectMaxSortOrderByParentId(Long parentId) {
        WbProjectTreeDO maxSortNode = selectOne(new LambdaQueryWrapperX<WbProjectTreeDO>()
                .eqIfPresent(WbProjectTreeDO::getParentId, parentId)
                .orderByDesc(WbProjectTreeDO::getSortOrder)
                .last("LIMIT 1"));
        return maxSortNode != null ? maxSortNode.getSortOrder() : 0;
    }

    /**
     * 根据状态查询项目列表
     */
    default List<WbProjectTreeDO> selectProjectListByStatus(String status) {
        return selectList(new LambdaQueryWrapperX<WbProjectTreeDO>()
                .eq(WbProjectTreeDO::getNodeType, WbProjectTreeDO.NODE_TYPE_PROJECT)
                .eqIfPresent(WbProjectTreeDO::getStatus, status)
                .orderByDesc(WbProjectTreeDO::getCreateTime));
    }

    /**
     * 查询同级节点中排序号大于等于指定值的节点
     */
    default List<WbProjectTreeDO> selectListByParentIdAndSortOrderGe(Long parentId, Integer sortOrder) {
        return selectList(new LambdaQueryWrapperX<WbProjectTreeDO>()
                .eqIfPresent(WbProjectTreeDO::getParentId, parentId)
                .ge(WbProjectTreeDO::getSortOrder, sortOrder)
                .orderByAsc(WbProjectTreeDO::getSortOrder));
    }
}
