package cn.iocoder.yudao.module.llm.service.knowledgedocumentschunksvectorized;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.llm.controller.admin.knowledgedocumentschunksvectorized.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.knowledgedocumentschunksvectorized.KnowledgeDocumentsChunksVectorizedDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.llm.dal.mysql.knowledgedocumentschunksvectorized.KnowledgeDocumentsChunksVectorizedMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.*;

/**
 * 向量化存储 Service 实现类
 *
 * @author 华大大模型
 */
@Service
@Validated
public class KnowledgeDocumentsChunksVectorizedServiceImpl implements KnowledgeDocumentsChunksVectorizedService {

    @Resource
    private KnowledgeDocumentsChunksVectorizedMapper knowledgeDocumentsChunksVectorizedMapper;

    @Override
    public Long createKnowledgeDocumentsChunksVectorized(KnowledgeDocumentsChunksVectorizedSaveReqVO createReqVO) {
        // 插入
        KnowledgeDocumentsChunksVectorizedDO knowledgeDocumentsChunksVectorized = BeanUtils.toBean(createReqVO, KnowledgeDocumentsChunksVectorizedDO.class);
        knowledgeDocumentsChunksVectorizedMapper.insert(knowledgeDocumentsChunksVectorized);
        // 返回
        return knowledgeDocumentsChunksVectorized.getId();
    }

    @Override
    public void updateKnowledgeDocumentsChunksVectorized(KnowledgeDocumentsChunksVectorizedSaveReqVO updateReqVO) {
        // 校验存在
        validateKnowledgeDocumentsChunksVectorizedExists(updateReqVO.getId());
        // 更新
        KnowledgeDocumentsChunksVectorizedDO updateObj = BeanUtils.toBean(updateReqVO, KnowledgeDocumentsChunksVectorizedDO.class);
        knowledgeDocumentsChunksVectorizedMapper.updateById(updateObj);
    }

    @Override
    public void deleteKnowledgeDocumentsChunksVectorized(Long id) {
        // 校验存在
        validateKnowledgeDocumentsChunksVectorizedExists(id);
        // 删除
        knowledgeDocumentsChunksVectorizedMapper.deleteById(id);
    }

    private void validateKnowledgeDocumentsChunksVectorizedExists(Long id) {
        if (knowledgeDocumentsChunksVectorizedMapper.selectById(id) == null) {
            throw exception(KNOWLEDGE_DOCUMENTS_CHUNKS_VECTORIZED_NOT_EXISTS);
        }
    }

    @Override
    public KnowledgeDocumentsChunksVectorizedDO getKnowledgeDocumentsChunksVectorized(Long id) {
        return knowledgeDocumentsChunksVectorizedMapper.selectById(id);
    }

    @Override
    public PageResult<KnowledgeDocumentsChunksVectorizedDO> getKnowledgeDocumentsChunksVectorizedPage(KnowledgeDocumentsChunksVectorizedPageReqVO pageReqVO) {
        return knowledgeDocumentsChunksVectorizedMapper.selectPage(pageReqVO);
    }

}