package com.keyware.shandan.bianmu.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keyware.shandan.bianmu.entity.DataLabelsVo;
import com.keyware.shandan.bianmu.entity.DirectoryResource;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DirectoryResourceMapper extends BaseMapper<DirectoryResource> {

    @Select("<script>" +
            "   SELECT DISTINCT TT.* FROM (" +
            "      SELECT * FROM V_DIRECTORY_RESOURCE VDR WHERE DIRECTORY_PATH LIKE (#{dirPath} || '%') AND EXISTS(SELECT 1 FROM B_DIRECTORY D1 WHERE D1.ID = VDR.PARENT_ID AND D1.REVIEW_STATUS IN ('PASS','SUBMITTED'))" +
            "   ) TT " +
            "   LEFT JOIN (SELECT B2.*, B1.ENTITY_ID FROM B_DATA_LABEL_ENTITY B1 INNER JOIN B_DATA_LABELS B2 ON B1.LABEL_ID = B2.ID) DLN ON DLN.ENTITY_ID = TT.ID " +
            "   WHERE 1=1 " +
            "   <when test='labels != null and labels.size > 0'>" +
            "       <foreach item=\"label\" collection=\"labels\">" +
            "           AND (DLN.ID_PATH LIKE (#{label.idPath} || '%') OR DLN.ID = #{label.id})" +
            "       </foreach>" +
            "   </when>" +
            "   <when test=\"ew.sqlSegment != null and ew.sqlSegment != '' and ew.sqlSegment != '()'\">" +
            "       AND ${ew.sqlSegment}" +
            "   </when>" +
            "</script>")
    @ResultType(DirectoryResource.class)
    List<DirectoryResource> customSelectList(@Param("dirPath") String dirPath, @Param("labels") List<DataLabelsVo> labels, @Param(Constants.WRAPPER) QueryWrapper<DirectoryResource> queryWrapper);

    @Select("<script>" +
            "   SELECT DISTINCT TT.* FROM (" +
            "      SELECT * FROM V_DIRECTORY_RESOURCE VDR WHERE DIRECTORY_PATH LIKE (#{dirPath} || '%') AND EXISTS(SELECT 1 FROM B_DIRECTORY D1 WHERE D1.ID = VDR.PARENT_ID AND D1.REVIEW_STATUS IN ('PASS','SUBMITTED'))" +
            "   ) TT " +
            "   LEFT JOIN (SELECT B2.*, B1.ENTITY_ID FROM B_DATA_LABEL_ENTITY B1 INNER JOIN B_DATA_LABELS B2 ON B1.LABEL_ID = B2.ID) DLN ON DLN.ENTITY_ID = TT.ID " +
            "   WHERE 1=1 " +
            "   <when test='labels != null and labels.size > 0'>" +
            "       <foreach item=\"label\" collection=\"labels\">" +
            "           AND (DLN.ID_PATH LIKE (#{label.idPath} || '%') OR DLN.ID = #{label.id})" +
            "       </foreach>" +
            "   </when>" +
            "   <when test=\"ew.sqlSegment != null and ew.sqlSegment != '' and ew.sqlSegment != '()'\">" +
            "       AND ${ew.sqlSegment}" +
            "   </when>" +
            "</script>")
    @ResultType(DirectoryResource.class)
    Page<DirectoryResource> customSelectPage(Page<DirectoryResource> page, @Param("dirPath") String dirPath, @Param("labels") List<DataLabelsVo> labels, @Param(Constants.WRAPPER) QueryWrapper<DirectoryResource> queryWrapper);
}
