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

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.knowledgedocuments.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.knowledgedocuments.KnowledgeDocumentsDO;
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.knowledgedocuments.KnowledgeDocumentsMapper;

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 KnowledgeDocumentsServiceImpl implements KnowledgeDocumentsService {

    @Resource
    private KnowledgeDocumentsMapper knowledgeDocumentsMapper;

    @Override
    public Long createKnowledgeDocuments(KnowledgeDocumentsSaveReqVO createReqVO) {
        // 插入
        KnowledgeDocumentsDO knowledgeDocuments = BeanUtils.toBean(createReqVO, KnowledgeDocumentsDO.class);
        knowledgeDocumentsMapper.insert(knowledgeDocuments);
        // 返回
        return knowledgeDocuments.getId();
    }

    @Override
    public void updateKnowledgeDocuments(KnowledgeDocumentsSaveReqVO updateReqVO) {
        // 校验存在
        validateKnowledgeDocumentsExists(updateReqVO.getId());
        // 更新
        KnowledgeDocumentsDO updateObj = BeanUtils.toBean(updateReqVO, KnowledgeDocumentsDO.class);
        knowledgeDocumentsMapper.updateById(updateObj);
    }

    @Override
    public void deleteKnowledgeDocuments(Long id) {
        // 校验存在
        validateKnowledgeDocumentsExists(id);
        // 删除
        knowledgeDocumentsMapper.deleteById(id);
    }

    private void validateKnowledgeDocumentsExists(Long id) {
        if (knowledgeDocumentsMapper.selectById(id) == null) {
            throw exception(KNOWLEDGE_DOCUMENTS_NOT_EXISTS);
        }
    }

    @Override
    public KnowledgeDocumentsDO getKnowledgeDocuments(Long id) {
        return knowledgeDocumentsMapper.selectById(id);
    }

    @Override
    public PageResult<KnowledgeDocumentsDO> getKnowledgeDocumentsPage(KnowledgeDocumentsPageReqVO pageReqVO) {
        return knowledgeDocumentsMapper.selectPage(pageReqVO);
    }

}