package com.pavis.admin.aigc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pavis.admin.aigc.mapper.DocChunkMapper;
import com.pavis.admin.aigc.mapper.DocMapper;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import top.continew.starter.extension.crud.service.BaseServiceImpl;
import com.pavis.admin.aigc.mapper.KnowledgeMapper;
import com.pavis.admin.aigc.model.entity.KnowledgeDO;
import com.pavis.admin.aigc.model.query.KnowledgeQuery;
import com.pavis.admin.aigc.model.req.KnowledgeReq;
import com.pavis.admin.aigc.model.resp.KnowledgeDetailResp;
import com.pavis.admin.aigc.model.resp.KnowledgeResp;
import com.pavis.admin.aigc.service.KnowledgeService;

import java.util.Date;
import java.util.List;

/**
 * AIGC知识库业务实现
 *
 * @author semi
 * @since 2025/05/26 17:28
 */
@Service
@RequiredArgsConstructor
public class KnowledgeServiceImpl extends BaseServiceImpl<KnowledgeMapper, KnowledgeDO, KnowledgeResp, KnowledgeDetailResp, KnowledgeQuery, KnowledgeReq> implements KnowledgeService {

    private final DocMapper aigcDocsMapper;
    private final DocChunkMapper aigcDocsSliceMapper;

    @Override
    public List<KnowledgeResp>selectKnowledge(){
        return baseMapper.selectKnowledge();
    }

    // @Override
    // @Transactional
    // public void addDocs(AigcDocs data) {
    //     data.setCreateTime(new Date());
    //     aigcDocsMapper.insert(data);
    // }

    // @Override
    // @Transactional
    // public void updateDocs(AigcDocs data) {
    //     aigcDocsMapper.updateById(data);
    // }

    // @Override
    // @Transactional
    // public void addDocsSlice(AigcDocsSlice data) {
    //     data.setCreateTime(new Date())
    //             .setWordNum(data.getContent().length())
    //             .setStatus(true)
    //     ;
    //     aigcDocsSliceMapper.insert(data);
    // }

    // @Override
    // @Transactional
    // public void updateDocsSlice(AigcDocsSlice data) {
    //     aigcDocsSliceMapper.updateById(data);
    // }

    // @Override
    // public List<String> listSliceVectorIdsOfDoc(String docsId) {
    //     LambdaQueryWrapper<AigcDocsSlice> selectWrapper = Wrappers.<AigcDocsSlice>lambdaQuery()
    //             .select(AigcDocsSlice::getVectorId)
    //             .eq(AigcDocsSlice::getDocsId, docsId);
    //     List<String> vectorIds = aigcDocsSliceMapper.selectList(selectWrapper)
    //             .stream()
    //             .map(AigcDocsSlice::getVectorId)
    //             .toList();
    //     log.debug("slices of doc: [{}], count: [{}]", docsId, vectorIds.size());
    //     return vectorIds;
    // }

    // @Override
    // public List<AigcDocs> getDocsByKb(String knowledgeId) {
    //     return aigcDocsMapper.selectList(Wrappers.<AigcDocs>lambdaQuery()
    //             .eq(AigcDocs::getKnowledgeId, knowledgeId));
    // }

    // @Override
    // @Transactional
    // public void removeKnowledge(String knowledgeId) {
    //     baseMapper.deleteById(knowledgeId);
    //     // del docs & docsSlice
    //     List<String> docsIds = getDocsByKb(knowledgeId).stream().map(AigcDocs::getId).toList();
    //     docsIds.forEach(this::removeSlicesOfDoc);
    // }
    //
    // @Override
    // @Transactional
    // public void removeSlicesOfDoc(String docsId) {
    //     LambdaQueryWrapper<AigcDocsSlice> deleteWrapper = Wrappers.<AigcDocsSlice>lambdaQuery()
    //             .eq(AigcDocsSlice::getDocsId, docsId);
    //     int count = aigcDocsSliceMapper.delete(deleteWrapper);
    //     log.debug("remove all slices of doc: [{}], count: [{}]", docsId, count);
    // }
}