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

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

@Mapper
public interface ResourceCatalogItemMapper extends BaseMapperX<ResourceCatalogItemDO> {
    
    /**
     * 检查是否存在子节点
     * 使用PostgreSQL数组切片语法：path[1:array_length(path,1)-1] 表示去掉最后一个元素
     * 使用 CAST 函数进行类型转换，确保类型匹配
     * 
     * @param categoryTreeId 机类树ID
     * @param path 父节点路径
     * @param tenantId 租户ID
     * @return 子节点数量
     */
    @Select("SELECT COUNT(*) FROM yhy_catalog_item " +
            "WHERE category_tree_id = #{categoryTreeId} " +
            "AND path[1:array_length(path,1)-1]::varchar[] = #{path} " +
            "AND tenant_id = #{tenantId} " +
            "AND deleted = 0")
    Long countChildNodes(@Param("categoryTreeId") Long categoryTreeId, 
                         @Param("path") String[] path, 
                         @Param("tenantId") Long tenantId);
}
