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.WbCompileTreeDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 编制模式树 Mapper
 *
 * @author yhy
 */
@Mapper
public interface WbCompileTreeMapper extends BaseMapperX<WbCompileTreeDO> {

    /**
     * 根据项目ID查询所有节点
     */
    default List<WbCompileTreeDO> selectListByProjectId(Long projectId) {
        return selectList(new LambdaQueryWrapperX<WbCompileTreeDO>()
                .eq(WbCompileTreeDO::getProjectId, projectId)
                .orderByAsc(WbCompileTreeDO::getSortOrder));
    }

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

    /**
     * 根据项目ID和节点类型查询
     */
    default WbCompileTreeDO selectByProjectIdAndNodeType(Long projectId, String nodeType) {
        return selectOne(new LambdaQueryWrapperX<WbCompileTreeDO>()
                .eq(WbCompileTreeDO::getProjectId, projectId)
                .eq(WbCompileTreeDO::getNodeType, nodeType));
    }

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

    /**
     * 根据项目ID查询根节点
     */
    default WbCompileTreeDO selectRootByProjectId(Long projectId) {
        return selectOne(new LambdaQueryWrapperX<WbCompileTreeDO>()
                .eq(WbCompileTreeDO::getProjectId, projectId)
                .eq(WbCompileTreeDO::getNodeType, WbCompileTreeDO.NODE_TYPE_ROOT));
    }

    /**
     * 根据项目ID查询所有单位工程节点
     */
    default List<WbCompileTreeDO> selectUnitNodesByProjectId(Long projectId) {
        return selectList(new LambdaQueryWrapperX<WbCompileTreeDO>()
                .eq(WbCompileTreeDO::getProjectId, projectId)
                .eq(WbCompileTreeDO::getNodeType, WbCompileTreeDO.NODE_TYPE_UNIT)
                .orderByAsc(WbCompileTreeDO::getSortOrder));
    }
}
