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

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.llm.dal.dataobject.basemodel.BaseModelDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelservice.ModelServiceDO;
import cn.iocoder.yudao.module.llm.dal.mysql.basemodel.BaseModelMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelservice.ModelServiceMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
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 java.util.stream.Collectors;

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;
    @Resource
    private ModelServiceMapper modelServiceMapper;
    @Resource
    private BaseModelMapper baseModelMapper;

    @Override
    public Long createDataRefluxConfig(DataRefluxConfigSaveReqVO createReqVO) {
        // 插入
        DataRefluxConfigDO dataRefluxConfig = BeanUtils.toBean(createReqVO, DataRefluxConfigDO.class);
        LambdaQueryWrapper<DataRefluxConfigDO> eq = new LambdaQueryWrapper<DataRefluxConfigDO>()
                .eq(DataRefluxConfigDO::getModelServiceId, dataRefluxConfig.getModelServiceId())
                .eq(DataRefluxConfigDO::getModelType, dataRefluxConfig.getModelType())
                .eq(DataRefluxConfigDO::getCreator, SecurityFrameworkUtils.getLoginUser().getId());
//        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if (dataRefluxConfigMapper.selectCount(eq) > 0) {
            throw exception(new ErrorCode(11000,"该模型服务已存在"));
        }
        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) {
        LambdaQueryWrapper<ModelServiceDO> like = new LambdaQueryWrapper<ModelServiceDO>().like(ModelServiceDO::getServiceName, pageReqVO.getModelServiceName());
        List<ModelServiceDO> modelServiceDOS = modelServiceMapper.selectList(like);
        List<Long> collect = modelServiceDOS.stream().map(ModelServiceDO::getId).collect(Collectors.toList());
        LambdaQueryWrapper<BaseModelDO> wrapper = new LambdaQueryWrapper<BaseModelDO>().like(BaseModelDO::getModelName, pageReqVO.getModelServiceName());
        List<BaseModelDO> baseModelDOS = baseModelMapper.selectList(wrapper);
        List<Long> collect1 = baseModelDOS.stream().map(BaseModelDO::getId).collect(Collectors.toList());
        LambdaQueryWrapperX<DataRefluxConfigDO> wrapperX = new LambdaQueryWrapperX<DataRefluxConfigDO>()
                .eqIfPresent(DataRefluxConfigDO::getModelServiceId, pageReqVO.getModelServiceId())
                .betweenIfPresent(DataRefluxConfigDO::getCreateTime, pageReqVO.getCreateTime())
                .orderByDesc(DataRefluxConfigDO::getId);
        if (collect.size()==0&&collect1.size()==0&&pageReqVO.getModelServiceName()!=null){
            return null;
        }
        if (ObjectUtil.isNotEmpty(collect) || ObjectUtil.isNotEmpty(collect1)) {
            wrapperX.and(w -> {
                if (ObjectUtil.isNotEmpty(collect)) {
                    w.or().in(DataRefluxConfigDO::getModelServiceId, collect).eq(DataRefluxConfigDO::getModelType, 0);
                }
                if (ObjectUtil.isNotEmpty(collect1)) {
                    w.or().in(DataRefluxConfigDO::getModelServiceId, collect1).eq(DataRefluxConfigDO::getModelType, 1);
                }
            });
        }
        return dataRefluxConfigMapper.selectPage(pageReqVO,wrapperX);
    }

}
