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

import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetAnswerPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetAnswerSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetAnswerDO;
import cn.iocoder.yudao.module.llm.dal.mysql.dataset.DatasetAnswerMapper;
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.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


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 DatasetAnswerServiceImpl implements DatasetAnswerService {

    @Resource
    private DatasetAnswerMapper datasetAnswerMapper;

    @Override
    public Long createDatasetAnswer(DatasetAnswerSaveReqVO createReqVO) {
        // 插入
        DatasetAnswerDO datasetAnswer = BeanUtils.toBean(createReqVO, DatasetAnswerDO.class);
        datasetAnswerMapper.insert(datasetAnswer);
        // 返回
        return datasetAnswer.getId();
    }

    @Override
    public void updateDatasetAnswer(DatasetAnswerSaveReqVO updateReqVO) {
        // 校验存在
        validateDatasetAnswerExists(updateReqVO.getId());
        // 更新
        DatasetAnswerDO updateObj = BeanUtils.toBean(updateReqVO, DatasetAnswerDO.class);
        datasetAnswerMapper.updateById(updateObj);
    }

    @Override
    public void deleteDatasetAnswer(Long id) {
        // 校验存在
        validateDatasetAnswerExists(id);
        // 删除
        datasetAnswerMapper.deleteById(id);
    }

    private void validateDatasetAnswerExists(Long id) {
        if (datasetAnswerMapper.selectById(id) == null) {
            throw exception(DATASET_ANSWER_NOT_EXISTS);
        }
    }

    @Override
    public DatasetAnswerDO getDatasetAnswer(Long id) {
        return datasetAnswerMapper.selectById(id);
    }

    @Override
    public PageResult<DatasetAnswerDO> getDatasetAnswerPage(DatasetAnswerPageReqVO pageReqVO) {
        return datasetAnswerMapper.selectPage(pageReqVO);
    }

}