package com.keyware.shandan.bianmu.mapper;

import com.keyware.shandan.bianmu.entity.DirectoryVo;
import com.keyware.shandan.bianmu.entity.MetadataBasicVo;
import com.keyware.shandan.common.mapper.IBaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.session.ResultHandler;

import java.util.List;

/**
 * <p>
 * 目录表 Mapper 接口
 * </p>
 *
 * @author GuoXin
 * @since 2021-06-01
 */
@Mapper
public interface DirectoryMapper extends IBaseMapper<DirectoryVo> {
    /**
     * 根据父目录ID获取目录信息
     *
     * @param parentId     父目录ID
     * @param reviewStatus 审核状态
     * @return
     */
    List<DirectoryVo> childrenListByParentIdAndReviewStatus(String parentId, String reviewStatus);

    /**
     * 获取目录下的数据资源
     *
     * @param id 目录ID
     * @return
     */
    List<MetadataBasicVo> selectMetadataByDirectory(String id);

    /**
     * 获取目录下所有数据资源，包含子级
     *
     * @param id 目录ID
     * @return -
     */
    List<MetadataBasicVo> selectAllMetadataByDirectory(String id);

    @Update("update B_DIRECTORY set DIRECTORY_PATH = REPLACE(DIRECTORY_PATH, #{oldPath}, #{newPath}) where DIRECTORY_PATH like '${oldPath}%'")
    void updateDirectoryPath(String oldPath, String newPath);

    @Select("SELECT * FROM B_DIRECTORY D1 WHERE EXISTS(SELECT * FROM B_DIRECTORY D2 WHERE D2.ID = #{dirId} AND D1.DIRECTORY_PATH LIKE D2.DIRECTORY_PATH || '/%')")
    @Options(resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = Integer.MIN_VALUE)
    @ResultType(DirectoryVo.class)
    void handlerAllChildrenListById(@Param("dirId") String dirId, ResultHandler<DirectoryVo> handler);
}
