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

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.training.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.training.TrainingDO;
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.training.TrainingMapper;

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 TrainingServiceImpl implements TrainingService {

    @Resource
    private TrainingMapper trainingMapper;

    @Override
    public Integer createTraining(TrainingSaveReqVO createReqVO) {
        // 插入
        TrainingDO training = BeanUtils.toBean(createReqVO, TrainingDO.class);
        trainingMapper.insert(training);
        // 返回
        return training.getId();
    }

    @Override
    public void updateTraining(TrainingSaveReqVO updateReqVO) {
        // 校验存在
        validateTrainingExists(updateReqVO.getId());
        // 更新
        TrainingDO updateObj = BeanUtils.toBean(updateReqVO, TrainingDO.class);
        trainingMapper.updateById(updateObj);
    }

    @Override
    public void deleteTraining(Integer id) {
        // 校验存在
        validateTrainingExists(id);
        // 删除
        trainingMapper.deleteById(id);
    }

    private void validateTrainingExists(Integer id) {
        if (trainingMapper.selectById(id) == null) {
            throw exception(TRAINING_NOT_EXISTS);
        }
    }

    @Override
    public TrainingDO getTraining(Integer id) {
        return trainingMapper.selectById(id);
    }

    @Override
    public PageResult<TrainingDO> getTrainingPage(TrainingPageReqVO pageReqVO) {
        return trainingMapper.selectPage(pageReqVO);
    }

}