package org.jk.rag.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jk.common.mybatis.core.mapper.BaseMapperPlus;
import org.jk.common.mybatis.core.page.PageQuery;
import org.jk.rag.domain.SliceInfoHistory;
import org.jk.rag.domain.entity.JkDocument;
import org.jk.rag.domain.entity.JkDocumentParams;
import org.jk.rag.domain.entity.JkSliceDocument;
import org.jk.rag.domain.entity.SliceInfo;
import org.jk.rag.domain.vo.JkReviseDocumentVo;
import org.jk.rag.domain.vo.JkReviseVo;
import org.jk.rag.domain.vo.ReviseHistoryVo;
import org.jk.rag.domain.vo.SliceInfoHistoryVo;

import java.util.List;

/**
 *  数据层
 *
 * @author jk
 */
public interface JkReviseMapper extends BaseMapperPlus<SliceInfoHistory, SliceInfoHistoryVo> {

    @Select("<script>" +
        "select " +
        " knowledge.knowledge_id knowledgeId," +
        " knowledge.document_size documentSize," +
        " knowledge.length," +
        " knowledge.word_num wordNum," +
        " knowledge.name knowledgeName," +
        " knowledge.update_by updateBy," +
        " knowledge.create_time createTime," +
        " knowledge.update_time updateTime" +
        " from bm_knowledge knowledge" +
        "  where ( knowledge.visible = '0' or ( knowledge.create_by = #{createBy} and knowledge.visible = '1') ) " +
        " <if test='knowLedgeName != null and knowLedgeName != \"\"'> " +
        " AND knowledge.name like concat('%',#{knowLedgeName},'%') " +
        " </if>" +
        " order by knowledge.create_time desc " +
        "</script>")
    Page<JkReviseVo> selectRevisePageList(@Param("knowLedgeName") String knowLedgeName,@Param("createBy") Long createBy, @Param("page") Page page);

    @Select("<script>" +
        "select " +
        " name " +
        " from bm_application " +
        "  where  FIND_IN_SET(#{knowLedgeId},knowledge_ids) > 0  " +
        "</script>")
    List<String> selectAppName(@Param("knowLedgeId") String knowLedgeId);

    @Select("<script>" +
        "select " +
        " document.knowledge_id knowledgeId," +
        " document.name documentName," +
        " document.document_id documentId" +
        " from bm_knowledge knowledge" +
        " left join bm_document document on knowledge.knowledge_id = document.knowledge_id " +
        "  where  ( knowledge.visible = '0' or( knowledge.create_by = #{createBy} and knowledge.visible = '1') )  and document.standard_classification = '0' " +
        " <if test='documentName != null and documentName != \"\"'> " +
        " AND document.name like concat('%',#{documentName},'%') " +
        " </if>" +
        " <if test='knowledgeId != null and knowledgeId != \"\"'> " +
        " AND document.knowledge_id = #{knowledgeId} " +
        " </if>" +
        " order by document.create_time desc " +
        "</script>")
    List<JkReviseDocumentVo> selectReviseDocumentList(@Param("documentName") String documentName, @Param("createBy") Long createBy,@Param("knowledgeId") String knowledgeId);


    @Select("<script>" +
        "select " +
        " slice_id sliceId," +
        " knowledge_id knowledgeId," +
        " document_id documentId," +
        " slice_index sliceIndex," +
        " slice_text sliceText," +
        " old_slice_text oldSliceText," +
        " revision_status revisionStatus," +
        " revision_slice_text revisionSliceText "+
        " from slice_info " +
        "  where del_flag = 0 " +
        " <if test='sliceText != null and sliceText != \"\"'> " +
        " AND slice_text like concat('%',#{sliceText},'%') " +
        " </if>" +
        " <if test='documentId != null and documentId != \"\"'> " +
        " AND document_id = #{documentId} " +
        " </if>" +
        " order by create_time,slice_index asc " +
        "</script>")
    List<SliceInfo> selectReviseSliceInfoList(@Param("documentId") String documentId, @Param("sliceText") String sliceText);

    @Update("<script>" +
        "update slice_info set " +
        " update_time = now() " +
        " <if test='reviseStatus != null and reviseStatus != \"\"'> " +
        " ,revision_status = #{reviseStatus} " +
        " </if>" +
        " <if test='refSliceId != null and refSliceId != \"\"'> " +
        " ,ref_slice_id = #{refSliceId} " +
        " </if>" +
        " <if test='reSliceText != null and reSliceText != \"\"'> " +
        " ,revision_slice_text = #{reSliceText} " +
        " </if>" +
        " <if test='revisionBy != null and revisionBy != \"\"'> " +
        " ,revision_by = #{revisionBy} " +
        " ,revision_time = now() " +
        " ,update_by = #{revisionBy} " +
        " </if>" +
        "  where slice_id = #{sliceId} " +
        "</script>")
    void updateSliceRevisionStatus(@Param("reviseStatus")String reviseStatus,@Param("sliceId")String sliceId,@Param("refSliceId")String refSliceId,@Param("reSliceText")String reSliceText,@Param("revisionBy") String revisionBy);

    @Select("<script>" +
        "select " +
        " sih.slice_id sliceId," +
        " sih.knowledge_id knowledgeId," +
        " sih.document_id documentId," +
        " sih.revision_status revisionStatus," +
        " si.slice_text sliceText," +
        " si2.slice_text refSliceText," +
        " bd.name documentName," +
        " bk.name knowledgeName," +
        " bd2.name refDocumentName," +
        " bk2.name refKnowledgeName," +
        " sih.revision_slice_text revisionSliceText, "+
        " sih.create_time createTime, "+
        " bd2.document_id refDocumentId, "+
        " sih.ref_slice_id refSliceId "+
        " from slice_info_history sih " +
        " left join slice_info si on sih.slice_id = si.slice_id"+
        " left join bm_document bd on si.document_id = bd.document_id"+
        " left join bm_knowledge bk on si.knowledge_id = bk.knowledge_id"+
        " left join slice_info si2 on sih.ref_slice_id = si2.slice_id"+
        " left join bm_document bd2 on si2.document_id = bd2.document_id"+
        " left join bm_knowledge bk2 on si2.knowledge_id = bk2.knowledge_id"+
        "  where 1 = 1 " +
        " <if test='knowledgeId != null and knowledgeId != \"\"'> " +
        " AND sih.knowledge_id = #{knowledgeId} " +
        " </if>" +
        " <if test='knowledgeName != null and knowledgeName != \"\"'> " +
        " AND bk.name like concat('%',#{knowledgeName},'%') " +
        " </if>" +
        " <if test='documentName != null and documentName != \"\"'> " +
        " AND bd.name like concat('%',#{documentName},'%') " +
        " </if>" +
        " <if test='refDocumentName != null and refDocumentName != \"\"'> " +
        " AND bd2.name like concat('%',#{refDocumentName},'%') " +
        " </if>" +
        " order by sih.create_time desc " +
        "</script>")
    Page<ReviseHistoryVo> selectReviseHistoryList(@Param("knowledgeId") String knowledgeId, @Param("knowledgeName")String knowledgeName, @Param("documentName")String documentName, @Param("refDocumentName")String refDocumentName,@Param("page") Page pageQuery);

}
