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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.basemodel.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.basemodel.BaseModelDO;
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.basemodel.BaseModelMapper;

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 BaseModelServiceImpl implements BaseModelService {

    @Resource
    private BaseModelMapper baseModelMapper;

    @Override
    public Integer createBaseModel(BaseModelSaveReqVO createReqVO) {
        // 插入
        BaseModelDO baseModel = BeanUtils.toBean(createReqVO, BaseModelDO.class);
        baseModelMapper.insert(baseModel);
        // 返回
        return baseModel.getId();
    }

    @Override
    public void updateBaseModel(BaseModelSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseModelExists(updateReqVO.getId());
        // 更新
        BaseModelDO updateObj = BeanUtils.toBean(updateReqVO, BaseModelDO.class);
        baseModelMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseModel(Integer id) {
        // 校验存在
        validateBaseModelExists(id);
        // 删除
        baseModelMapper.deleteById(id);
    }

    private void validateBaseModelExists(Integer id) {
        if (baseModelMapper.selectById(id) == null) {
            throw exception(BASE_MODEL_NOT_EXISTS);
        }
    }

    @Override
    public BaseModelDO getBaseModel(Long id) {
        return baseModelMapper.selectById(id);
    }

    @Override
    public PageResult<BaseModelDO> getBaseModelPage(BaseModelPageReqVO pageReqVO) {
        return baseModelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseModelDO> getBaseModelList() {
        LambdaQueryWrapper<BaseModelDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseModelDO::getIsActive,1);
        List<BaseModelDO> baseModelDOS = baseModelMapper.selectList(queryWrapper);
        return baseModelDOS;
    }

}
