package org.jk.rag.mapper;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jk.common.mybatis.core.mapper.BaseMapperPlus;
import org.jk.rag.domain.dto.DocumentFileDTO;
import org.jk.rag.domain.entity.*;
import org.jk.rag.domain.vo.KnowledgeSliceVo;
import org.jk.system.domain.SysDept;
import org.jk.system.domain.SysUser;
import org.jk.system.domain.vo.SysDeptVo;
import org.jk.system.domain.vo.SysUserVo;

import java.util.List;

/**
 * 高井 知识列表
 *
 * @author takai
 */
public interface JkDocumentMapper extends BaseMapperPlus<BmDocument, JkDocument>
{
    /**
     * 根据条件分页查询知识信息
     *
     * @param mData 知识信息
     * @return 知识信息
     */
    public List<JkDocument> selectDocumentList(JkDocument mData);

    @Select("<script>" +
        "select document.document_id documentId," +
        " document.knowledge_id knowledgeId," +
        " document.custom_separator customSeparator," +
        " document.sentence_size sentenceSize," +
        " document.length," +
        " document.word_num wordNum," +
        " document.name," +
        " document.url," +
        " document.parse_image parseImage," +
        " document.slice_total sliceTotal," +
        " document.status," +
        " document.remark," +
        " document.parsing_type parsingType," +
        " document.create_by createBy," +
        " document.update_by updateBy," +
        " document.create_time createTime," +
        " document.update_time updateTime," +
        " document.pdf_url pdfUrl," +
        " document.standard_classification standardClassification "+
        " from bm_document document" +
        " left join bm_knowledge knowledge on document.knowledge_id = knowledge.knowledge_id " +
        "  where document.status not in('0','2','6') and (knowledge.visible = 0 or knowledge.create_by = #{ew.createBy} ) " +
        " <if test='ew.name != null '> " +
        " AND document.name like concat('%',#{ew.name},'%')  " +
        " </if>" +
        " <if test='ew.knowledge_id != null '> " +
        " AND document.knowledge_id = #{ew.knowledge_id}  " +
        " </if>" +
        " order by document.create_time asc " +
        "</script>")
    Page<JkSliceDocument> selectSliceDocumentPageList(@Param("page") Page page,@Param("ew") JkDocumentParams mData );

    IPage<JkDocument> selectDocumentPageList(Page<BmDocument> page,@Param("ew") JkDocumentParams mData ) ;

//    @Select("<script>" +
//        "SELECT t.* " +
//        "FROM ("+
//        "select document.document_id documentId," +
//        " document.knowledge_id knowledgeId," +
//        " document.custom_separator customSeparator," +
//        " document.sentence_size sentenceSize," +
//        " document.length," +
//        " document.word_num wordNum," +
//        " document.name," +
//        " document.url," +
//        " document.parse_image parseImage," +
//        " document.create_by createBy," +
//        " document.update_by updateBy," +
//        " document.slice_total sliceTotal," +
//        " document.remark," +
//        " document.parsing_type parsingType," +
//        " document.status," +
//        " document.create_time createTime," +
//        " document.update_time updateTime," +
//        " document.pdf_url pdfUrl," +
//        " document.standard_classification standardClassification, "+
////        -- 核心：按oss_id分组，create_time升序（最老在前），生成行号
//        " ROW_NUMBER() OVER (PARTITION BY document.oss_id ORDER BY document.create_time ASC) AS rn "+
//        " from bm_document document" +
//        " left join bm_knowledge knowledge on document.knowledge_id = knowledge.knowledge_id " +
//        "  where (knowledge.visible = 0 or knowledge.create_by = #{ew.createBy} ) " +
//        " <if test='ew.standardClassification != null and ew.standardClassification != \"\" '> " +
//        " AND document.standard_classification =  #{ew.standardClassification} " +
//        " </if>" +
//        " <if test='ew.knowledge_id != null '> " +
//        " AND document.knowledge_id != #{ew.knowledge_id}  " +
//        " </if>" +
//        " <if test='ew.name != null and ew.name != \"\"'> " +
//        " AND (document.name like concat('%',#{ew.name},'%') or knowledge.name like concat('%',#{ew.name},'%')) " +
//        " </if>" +
//        " order by document.create_time asc ) t " +
//        " where t.rn = 1 " +
//        "</script>")
    @Select("<script>" +
        " select document.document_id documentId," +
        " document.knowledge_id knowledgeId," +
        " document.custom_separator customSeparator," +
        " document.sentence_size sentenceSize," +
        " document.word_num wordNum," +
        " document.length," +
        " document.name," +
        " document.url," +
        " document.parse_image parseImage," +
        " document.slice_total sliceTotal," +
        " document.status," +
        " document.remark," +
        " document.parsing_type parsingType," +
        " document.create_by createBy," +
        " document.update_by updateBy," +
        " document.create_time createTime," +
        " document.update_time updateTime," +
        " document.pdf_url pdfUrl," +
        " document.standard_classification standardClassification, "+
        " knowledge.name knowledgeName, "+
        " su.nick_name nickName "+
        " from bm_document document" +
        " left join bm_knowledge knowledge on document.knowledge_id = knowledge.knowledge_id " +
        " left join sys_user su on document.create_by = su.user_id " +
        "  where (knowledge.visible = 0 or knowledge.create_by = #{ew.createBy} ) " +
        " <if test='ew.standardClassification != null and ew.standardClassification != \"\" '> " +
        " AND document.standard_classification =  #{ew.standardClassification} " +
        " </if>" +
        " <if test='ew.knowledge_id != null '> " +
        " AND document.knowledge_id != #{ew.knowledge_id}  " +
        " </if>" +
        " <if test='ew.name != null and ew.name != \"\"'> " +
        " AND (document.name like concat('%',#{ew.name},'%') or knowledge.name like concat('%',#{ew.name},'%')) " +
        " </if>" +
        " and document.status != '2' "+
        " order by document.create_time asc  " +
        "</script>")
    Page<JkQuoteDocument> selectQuoteDocumentPageList(@Param("ew") JkDocumentParams mData, @Param("page") Page<JkDocument> page);

    @Select("<script>" +
        "SELECT t.* " +
        "FROM ("+
        "select document.document_id documentId," +
        " document.knowledge_id knowledgeId," +
        " document.custom_separator customSeparator," +
        " document.sentence_size sentenceSize," +
        " document.length," +
        " document.word_num wordNum," +
        " document.name," +
        " document.url," +
        " document.parse_image parseImage," +
        " document.slice_total sliceTotal," +
        " document.status," +
        " document.remark," +
        " document.parsing_type parsingType," +
        " document.create_by createBy," +
        " document.update_by updateBy," +
        " document.create_time createTime," +
        " document.update_time updateTime," +
        " document.pdf_url pdfUrl," +
        " document.standard_classification standardClassification, "+
//        -- 核心：按oss_id分组，create_time升序（最老在前），生成行号
        " ROW_NUMBER() OVER (PARTITION BY document.oss_id ORDER BY document.create_time ASC) AS rn "+
        " from bm_document document" +
        " left join bm_knowledge knowledge on document.knowledge_id = knowledge.knowledge_id " +
        "  where 1 = 1  and document.status in ('1','5') " +
        " <if test='ossId != null  '> " +
        " AND document.oss_id =  #{ossId} " +
        " </if>" +
        " <if test='customSeparator != null and customSeparator != \"\" '> " +
        " AND document.custom_separator =  #{customSeparator} " +
        " </if>" +
//        " <if test='standardClassification != null and standardClassification != \"\"  '> " +
//        " AND document.standard_classification =  #{standardClassification} " +
//        " </if>" +
        " <if test='sliceValue != null and sliceValue != \"\"  '> " +
        " AND document.slice_value =  #{sliceValue} " +
        " </if>" +
        " <if test='parsingType != null and parsingType != \"\"  '> " +
        " AND document.parsing_type =  #{parsingType} " +
        " </if>" +
        " order by document.create_time asc ) t " +
        " where t.rn = 1 " +
        "</script>")
    JkSliceDocument selectDocumentByOssId(@Param("ossId") String ossId,@Param("customSeparator")String customSeparator,@Param("sliceValue")String sliceValue,@Param("parsingType")String parsingType,@Param("standardClassification")String standardClassification);


    /**
     * 查询唯一知识信息
     * @param mData
     * @return
     */

    public JkDocument selectTargetDocument(JkDocument mData);


    /**
     * 新增知识信息
     *
     * @param mData 知识信息
     * @return 结果
     */
    public int insertDocument(JkDocument mData);

    /**
     * 更新知识信息
     *
     * @param mData 知识信息
     * @return 结果
     */
    public int updateDocument(JkDocument mData);

    int delDocument(String documentId);

    int delDocumentByKnowledgeId(String knowledgeId);

    List<JkDocument> selectDocumentListByApprover(JkDocument mData);

    @Select("<script>" +
        "select slice.document_id documentId" +
        ",slice.knowledge_id knowledgeId " +
        ",knowledge.name knowledgeName " +
        ",document.name documentName " +
        ",slice.slice_text sliceText " +
        ",slice.bbox  " +
        ",slice.page  " +
        " from bm_document document " +
        " left join bm_knowledge knowledge on document.knowledge_id = knowledge.knowledge_id" +
        " left join slice_info slice on document.document_id = slice.document_id " +
        " where slice.document_id in  " +
        "<foreach item='id' collection='documentIds' open='(' separator=',' close=')'>" +
        "  #{id}  " +
        "</foreach>"+
        " order by slice.document_id,slice.slice_index " +
        "</script>")
    List<KnowledgeSliceVo> selectExportDocumentSlice(@Param("documentIds") List<String> documentIds);

    @Select("<script>" +
        "select document.mark_url " +
        " from bm_document document " +
        " where document.document_id in  " +
        "<foreach item='id' collection= 'documentIds' open='(' separator=',' close=')'>" +
        "  #{id}  " +
        "</foreach>"+
        "</script>")
    List<String> selectDownloadDocumentUrl(@Param("documentIds") List<String> documentIds);

    @Select("<script>" +
        "select document.url " +
        " from bm_document document " +
        " where document.document_id in  " +
        "<foreach item='id' collection= 'documentIds' open='(' separator=',' close=')'>" +
        "  #{id}  " +
        "</foreach>"+
        "</script>")
    List<String> selectDownloadDocumentFileUrl(@Param("documentIds") List<String> documentIds);

    @Select("<script>" +
        "select document.url " +
        " from bm_document document " +
        " where document.knowledge_id = #{knowledgeId}   " +
        "</script>")
    List<String> selectDownloadKnowledgeFileDocument(@Param("knowledgeId") String knowledgeId);

    @Select("<script>" +
        "select document.mark_url " +
        " from bm_document document " +
        " where document.knowledge_id = #{knowledgeId}   " +
        "</script>")
    List<String> selectDownloadKnowledgeMarkDownFileDocument(@Param("knowledgeId") String knowledgeId);

    @Select("<script>" +
        "select document.pdf_url " +
        " from bm_document document " +
        " where document.document_id in  " +
        "<foreach item='id' collection= 'documentIds' open='(' separator=',' close=')'>" +
        "  #{id}  " +
        "</foreach>"+
        "</script>")
    List<String> selectPdfUrl(@Param("documentIds") List<String> documentIds);

    @Select("<script>" +
        "select * " +
        " from bm_document document " +
        " where document_id in  " +
        "<foreach item='id' collection='documentIds' open='(' separator=',' close=')'>" +
        "  #{id}  " +
        "</foreach>"+
        "</script>")
    List<BmDocument> selectDocumentByDocumentIds(@Param("documentIds") List<String> documentIds);

    @Select("<script>" +
        "SELECT t.name " +
        "FROM ("+
        "select document.document_id documentId," +
        " document.knowledge_id knowledgeId," +
        " document.custom_separator customSeparator," +
        " document.sentence_size sentenceSize," +
        " document.length," +
        " document.word_num wordNum," +
        " document.name," +
        " document.url," +
        " document.parse_image parseImage," +
        " document.create_by createBy," +
        " document.update_by updateBy," +
        " document.slice_total sliceTotal," +
        " document.remark," +
        " document.parsing_type parsingType," +
        " document.status," +
        " document.create_time createTime," +
        " document.update_time updateTime," +
        " document.pdf_url pdfUrl," +
        " document.standard_classification standardClassification, "+
//        -- 核心：按oss_id分组，create_time升序（最老在前），生成行号
        " ROW_NUMBER() OVER (PARTITION BY document.oss_id ORDER BY document.create_time ASC) AS rn " +
        " from bm_document document"+
        " where document.knowledge_id = #{ew.knowledgeId} and document.status in ('1','0','5')" +
        " <if test='ew.customSeparator != null and ew.customSeparator != \"\"  '> " +
        " AND custom_separator =  #{ew.customSeparator} " +
        " </if>" +
//        " <if test='ew.standardClassification != null and ew.standardClassification != \"\"  '> " +
//        " AND standard_classification =  #{ew.standardClassification} " +
//        " </if>" +
        " <if test='ew.sliceValue != null and ew.sliceValue != \"\"  '> " +
        " AND slice_value =  #{ew.sliceValue} " +
        " </if>" +
        " <if test='ew.parsingType != null and ew.parsingType != \"\"  '> " +
        " AND parsing_type =  #{ew.parsingType} " +
        " </if>" +
        " and oss_id = #{ossId}   " +
//        "<foreach item='id' collection='ew.ossIds' open='(' separator=',' close=')'>" +
//        "  #{id}  " +
//        "</foreach>"+
        " order by document.create_time asc ) t " +
        " where t.rn = 1 " +
        "</script>")
    String selectDocumentByOssIds(@Param("ew") DocumentFileDTO dto,@Param("ossId")String ossId);

    @Select("<script>" +
        "select count(*) " +
        " from bm_document document " +
        " where document_id != #{documentId} and  mark_oss_id = #{markOssId} " +
        "</script>")
    int selectMarkDownFileExit(@Param("documentId") String documentId,@Param("markOssId") String markOssId);

    @Select("<script>" +
        "SELECT t.name " +
        "FROM ("+
        "select document.document_id documentId," +
        " document.knowledge_id knowledgeId," +
        " document.custom_separator customSeparator," +
        " document.sentence_size sentenceSize," +
        " document.length," +
        " document.word_num wordNum," +
        " document.name," +
        " document.url," +
        " document.parse_image parseImage," +
        " document.create_by createBy," +
        " document.update_by updateBy," +
        " document.slice_total sliceTotal," +
        " document.remark," +
        " document.parsing_type parsingType," +
        " document.status," +
        " document.create_time createTime," +
        " document.update_time updateTime," +
        " document.pdf_url pdfUrl," +
        " document.standard_classification standardClassification, "+
//        -- 核心：按oss_id分组，create_time升序（最老在前），生成行号
        " ROW_NUMBER() OVER (PARTITION BY document.oss_id ORDER BY document.create_time ASC) AS rn " +
        " from bm_document document"+
        " where document.knowledge_id = #{knowledgeId} and document.status in ('1','0','5')" +
        " <if test='ew.customSeparator != null and ew.customSeparator != \"\"  '> " +
        " AND custom_separator =  #{ew.customSeparator} " +
        " </if>" +
        " <if test='ew.sliceValue != null and ew.sliceValue != \"\"  '> " +
        " AND slice_value =  #{ew.sliceValue} " +
        " </if>" +
        " <if test='ew.parsingType != null and ew.parsingType != \"\"  '> " +
        " AND parsing_type =  #{ew.parsingType} " +
        " </if>" +
        " and oss_id = #{ew.ossId}   " +
        " order by document.create_time asc ) t " +
        " where t.rn = 1 " +
        "</script>")
    String selectQuoteDocument(@Param("ew") BmDocument bmDocument,@Param("knowledgeId") String knowledgeId);

}
