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.BoqItemTreeDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 清单项树 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);
    }

    /**
     * 查询同级节点
     */
    @Select("SELECT * FROM yhy_boq_item_tree WHERE " +
            "(parent_id = #{parentId} OR (parent_id IS NULL AND #{parentId} IS NULL)) " +
            "AND boq_catalog_item_id = #{boqCatalogItemId} " +
            "ORDER BY sort_order")
    List<BoqItemTreeDO> selectSiblings(@Param("parentId") Long parentId, 
                                        @Param("boqCatalogItemId") Long boqCatalogItemId);
}
