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

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.knowledgedocumentschunks.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.knowledgedocumentschunks.KnowledgeDocumentsChunksDO;
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.knowledgedocumentschunks.KnowledgeDocumentsChunksMapper;

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 KnowledgeDocumentsChunksServiceImpl implements KnowledgeDocumentsChunksService {

    @Resource
    private KnowledgeDocumentsChunksMapper knowledgeDocumentsChunksMapper;

    @Override
    public Long createKnowledgeDocumentsChunks(KnowledgeDocumentsChunksSaveReqVO createReqVO) {
        // 插入
        KnowledgeDocumentsChunksDO knowledgeDocumentsChunks = BeanUtils.toBean(createReqVO, KnowledgeDocumentsChunksDO.class);
        knowledgeDocumentsChunksMapper.insert(knowledgeDocumentsChunks);
        // 返回
        return knowledgeDocumentsChunks.getId();
    }

    @Override
    public void updateKnowledgeDocumentsChunks(KnowledgeDocumentsChunksSaveReqVO updateReqVO) {
        // 校验存在
        validateKnowledgeDocumentsChunksExists(updateReqVO.getId());
        // 更新
        KnowledgeDocumentsChunksDO updateObj = BeanUtils.toBean(updateReqVO, KnowledgeDocumentsChunksDO.class);
        knowledgeDocumentsChunksMapper.updateById(updateObj);
    }

    @Override
    public void deleteKnowledgeDocumentsChunks(Long id) {
        // 校验存在
        validateKnowledgeDocumentsChunksExists(id);
        // 删除
        knowledgeDocumentsChunksMapper.deleteById(id);
    }

    private void validateKnowledgeDocumentsChunksExists(Long id) {
        if (knowledgeDocumentsChunksMapper.selectById(id) == null) {
            throw exception(KNOWLEDGE_DOCUMENTS_CHUNKS_NOT_EXISTS);
        }
    }

    @Override
    public KnowledgeDocumentsChunksDO getKnowledgeDocumentsChunks(Long id) {
        return knowledgeDocumentsChunksMapper.selectById(id);
    }

    @Override
    public PageResult<KnowledgeDocumentsChunksDO> getKnowledgeDocumentsChunksPage(KnowledgeDocumentsChunksPageReqVO pageReqVO) {
        return knowledgeDocumentsChunksMapper.selectPage(pageReqVO);
    }

}