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

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.modelservice.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelservice.ModelServiceDO;
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.modelservice.ModelServiceMapper;

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 ModelServiceServiceImpl implements ModelServiceService {

    @Resource
    private ModelServiceMapper modelServiceMapper;

    @Override
    public Long createModelService(ModelServiceSaveReqVO createReqVO) {
        // 插入
        ModelServiceDO modelService = BeanUtils.toBean(createReqVO, ModelServiceDO.class);
        modelServiceMapper.insert(modelService);
        // 返回
        return modelService.getId();
    }

    @Override
    public void updateModelService(ModelServiceSaveReqVO updateReqVO) {
        // 校验存在
        validateModelServiceExists(updateReqVO.getId());
        // 更新
        ModelServiceDO updateObj = BeanUtils.toBean(updateReqVO, ModelServiceDO.class);
        modelServiceMapper.updateById(updateObj);
    }

    @Override
    public void deleteModelService(Long id) {
        // 校验存在
        validateModelServiceExists(id);
        // 删除
        modelServiceMapper.deleteById(id);
    }

    private void validateModelServiceExists(Long id) {
        if (modelServiceMapper.selectById(id) == null) {
            throw exception(MODEL_SERVICE_NOT_EXISTS);
        }
    }

    @Override
    public ModelServiceDO getModelService(Long id) {
        return modelServiceMapper.selectById(id);
    }

    @Override
    public PageResult<ModelServiceDO> getModelServicePage(ModelServicePageReqVO pageReqVO) {
        return modelServiceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ModelServiceDO> getModelServiceList() {
        return modelServiceMapper.selectList();
    }

}
