package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.SysOaAiConfigBo;
import com.ruoyi.oa.domain.vo.SysOaAiConfigVo;
import com.ruoyi.oa.domain.SysOaAiConfig;
import com.ruoyi.oa.mapper.SysOaAiConfigMapper;
import com.ruoyi.oa.service.ISysOaAiConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * AI配置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-19
 */
@RequiredArgsConstructor
@Service
public class SysOaAiConfigServiceImpl implements ISysOaAiConfigService {

    private final SysOaAiConfigMapper baseMapper;

    /**
     * 查询AI配置
     */
    @Override
    public SysOaAiConfigVo queryById(Long configId) {
        return baseMapper.selectVoById(configId);
    }

    /**
     * 查询AI配置列表
     */
    @Override
    public TableDataInfo<SysOaAiConfigVo> queryPageList(SysOaAiConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysOaAiConfig> lqw = buildQueryWrapper(bo);
        Page<SysOaAiConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询AI配置列表
     */
    @Override
    public List<SysOaAiConfigVo> queryList(SysOaAiConfigBo bo) {
        LambdaQueryWrapper<SysOaAiConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOaAiConfig> buildQueryWrapper(SysOaAiConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaAiConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getConfigName()), SysOaAiConfig::getConfigName, bo.getConfigName());
        lqw.eq(StringUtils.isNotBlank(bo.getApiKey()), SysOaAiConfig::getApiKey, bo.getApiKey());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseUrl()), SysOaAiConfig::getBaseUrl, bo.getBaseUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getModelName()), SysOaAiConfig::getModelName, bo.getModelName());
        lqw.eq(bo.getMaxRetries() != null, SysOaAiConfig::getMaxRetries, bo.getMaxRetries());
        lqw.eq(bo.getTemperature() != null, SysOaAiConfig::getTemperature, bo.getTemperature());
        lqw.eq(bo.getMaxTokens() != null, SysOaAiConfig::getMaxTokens, bo.getMaxTokens());
        lqw.eq(bo.getStatus() != null, SysOaAiConfig::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增AI配置
     */
    @Override
    public Boolean insertByBo(SysOaAiConfigBo bo) {
        SysOaAiConfig add = BeanUtil.toBean(bo, SysOaAiConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setConfigId(add.getConfigId());
        }
        return flag;
    }

    /**
     * 修改AI配置
     */
    @Override
    public Boolean updateByBo(SysOaAiConfigBo bo) {
        SysOaAiConfig update = BeanUtil.toBean(bo, SysOaAiConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysOaAiConfig entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除AI配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取启用的AI配置
     */
    @Override
    public SysOaAiConfigVo getActiveConfig() {
        LambdaQueryWrapper<SysOaAiConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysOaAiConfig::getStatus, 1); // 启用状态
        lqw.last("LIMIT 1");
        return baseMapper.selectVoOne(lqw);
    }
} 