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

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.yhy.module.core.dal.dataobject.boq.BoqCatalogItemDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 清单配置目录树 Mapper
 *
 * @author yhy
 */
@Mapper
public interface BoqCatalogItemMapper extends BaseMapperX<BoqCatalogItemDO> {

    /**
     * 查询所有节点（树形结构）
     */
    default List<BoqCatalogItemDO> selectList() {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<BoqCatalogItemDO>()
                .orderByAsc(BoqCatalogItemDO::getSortOrder));
    }

    /**
     * 根据父节点ID查询子节点
     */
    default List<BoqCatalogItemDO> selectListByParentId(Long parentId) {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<BoqCatalogItemDO>()
                .eq(BoqCatalogItemDO::getParentId, parentId)
                .orderByAsc(BoqCatalogItemDO::getSortOrder));
    }

    /**
     * 根据编码查询
     */
    default BoqCatalogItemDO selectByCode(String code) {
        return selectOne(BoqCatalogItemDO::getCode, code);
    }

    /**
     * 根据定额基价节点ID查询
     */
    default List<BoqCatalogItemDO> selectListByQuotaCatalogItemId(Long quotaCatalogItemId) {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<BoqCatalogItemDO>()
                .eq(BoqCatalogItemDO::getQuotaCatalogItemId, quotaCatalogItemId));
    }

    /**
     * 查询同级节点（用于排序）
     */
    default List<BoqCatalogItemDO> selectSiblings(Long parentId) {
        return selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<BoqCatalogItemDO>()
                .eq(parentId != null, BoqCatalogItemDO::getParentId, parentId)
                .isNull(parentId == null, BoqCatalogItemDO::getParentId)
                .orderByAsc(BoqCatalogItemDO::getSortOrder));
    }
}
