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

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.datarefluxconfig.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.datarefluxconfig.DataRefluxConfigDO;
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.datarefluxconfig.DataRefluxConfigMapper;

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 DataRefluxConfigServiceImpl implements DataRefluxConfigService {

    @Resource
    private DataRefluxConfigMapper dataRefluxConfigMapper;

    @Override
    public Long createDataRefluxConfig(DataRefluxConfigSaveReqVO createReqVO) {
        // 插入
        DataRefluxConfigDO dataRefluxConfig = BeanUtils.toBean(createReqVO, DataRefluxConfigDO.class);
        dataRefluxConfigMapper.insert(dataRefluxConfig);
        // 返回
        return dataRefluxConfig.getId();
    }

    @Override
    public void updateDataRefluxConfig(DataRefluxConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateDataRefluxConfigExists(updateReqVO.getId());
        // 更新
        DataRefluxConfigDO updateObj = BeanUtils.toBean(updateReqVO, DataRefluxConfigDO.class);
        dataRefluxConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteDataRefluxConfig(Long id) {
        // 校验存在
        validateDataRefluxConfigExists(id);
        // 删除
        dataRefluxConfigMapper.deleteById(id);
    }

    private void validateDataRefluxConfigExists(Long id) {
        if (dataRefluxConfigMapper.selectById(id) == null) {
            throw exception(DATA_REFLUX_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public DataRefluxConfigDO getDataRefluxConfig(Long id) {
        return dataRefluxConfigMapper.selectById(id);
    }

    @Override
    public PageResult<DataRefluxConfigDO> getDataRefluxConfigPage(DataRefluxConfigPageReqVO pageReqVO) {
        return dataRefluxConfigMapper.selectPage(pageReqVO);
    }

}