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

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhy.module.core.dal.dataobject.boq.BoqItemTreeDO;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 清单项树 Mapper
 *
 * @author yhy
 */
@Mapper
public interface BoqItemTreeMapper extends BaseMapperX<BoqItemTreeDO> {

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

    /**
     * 根据清单专业ID查询所有节点
     */
    default List<BoqItemTreeDO> selectListByBoqCatalogItemId(Long boqCatalogItemId) {
        return selectList("boq_catalog_item_id", boqCatalogItemId);
    }

    /**
     * 根据父节点ID查询子节点
     */
    default List<BoqItemTreeDO> selectListByParentId(Long parentId) {
        return selectList("parent_id", parentId);
    }

    /**
     * 查询同级节点
     */
    default List<BoqItemTreeDO> selectSiblings(Long parentId, Long boqCatalogItemId) {
        return selectList(new LambdaQueryWrapper<BoqItemTreeDO>()
                .eq(parentId != null, BoqItemTreeDO::getParentId, parentId)
                .isNull(parentId == null, BoqItemTreeDO::getParentId)
                .eq(BoqItemTreeDO::getBoqCatalogItemId, boqCatalogItemId)
                .orderByAsc(BoqItemTreeDO::getSortOrder));
    }
}
