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

import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual.vo.ModelAssessTaskManualRespVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassessstoplist.ModelAssessStoplistDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskauto.ModelAssessTaskStoplistDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskmanual.ModelAssessTaskManualDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelservice.ModelServiceDO;
import cn.iocoder.yudao.module.llm.dal.mysql.dataset.DatasetMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassessstoplist.ModelAssessStoplistMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassesstaskauto.ModelAssessTaskStoplistMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelservice.ModelServiceMapper;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.modelassesstaskauto.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskauto.ModelAssessTaskAutoDO;
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.modelassesstaskauto.ModelAssessTaskAutoMapper;

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 ModelAssessTaskAutoServiceImpl implements ModelAssessTaskAutoService {

    @Resource
    private ModelAssessTaskAutoMapper modelAssessTaskAutoMapper;
    @Resource
    private ModelAssessTaskStoplistMapper modelingTaskStoplistMapper;
    @Resource
    private ModelServiceMapper modelServiceMapper;
    @Resource
    private DatasetMapper datasetMapper;
    @Resource
    private ModelAssessStoplistMapper modelAssessStoplistMapper;


    @Override
    public Long createModelAssessTaskAuto(ModelAssessTaskAutoSaveReqVO createReqVO) {
        // 插入
        ModelAssessTaskAutoDO modelAssessTaskAuto = BeanUtils.toBean(createReqVO, ModelAssessTaskAutoDO.class);
        modelAssessTaskAutoMapper.insert(modelAssessTaskAuto);
        List<ModelAssessTaskStoplistSaveReqVO> stoplists = createReqVO.getStoplists();
        if (!CollectionUtils.isEmpty(stoplists)){
            stoplists.stream().forEach(stoplist -> {
                stoplist.setTaskId(modelAssessTaskAuto.getId());
                ModelAssessTaskStoplistDO modelAssessTaskStoplistDO = BeanUtils.toBean(stoplist,ModelAssessTaskStoplistDO.class);
                modelingTaskStoplistMapper.insert(modelAssessTaskStoplistDO);
            });
        }
        // 返回
        return modelAssessTaskAuto.getId();
    }

    @Override
    public void updateModelAssessTaskAuto(ModelAssessTaskAutoSaveReqVO updateReqVO) {
        // 校验存在
        validateModelAssessTaskAutoExists(updateReqVO.getId());
        // 更新
        ModelAssessTaskAutoDO updateObj = BeanUtils.toBean(updateReqVO, ModelAssessTaskAutoDO.class);
        List<ModelAssessTaskStoplistSaveReqVO> stoplists = updateReqVO.getStoplists();
       if (!CollectionUtils.isEmpty(stoplists)){
           List<Long> stopIds = stoplists.stream().filter(stoplist -> stoplist.getId() != null)
                   .map(stoplist -> stoplist.getId()).collect(Collectors.toList());
           LambdaQueryWrapper<ModelAssessTaskStoplistDO> wrapper = new LambdaQueryWrapper<ModelAssessTaskStoplistDO>();
           wrapper.eq(ModelAssessTaskStoplistDO::getTaskId,updateReqVO.getId());
           if (!CollectionUtils.isEmpty(stopIds)){
               wrapper.notIn(ModelAssessTaskStoplistDO::getId,stopIds);
           }
           modelingTaskStoplistMapper.delete(wrapper);
           stoplists.stream().forEach(stoplist -> {
//            stoplist.setTaskId(updateObj.getId());
               ModelAssessTaskStoplistDO modelAssessTaskStoplistDO = BeanUtils.toBean(stoplist,ModelAssessTaskStoplistDO.class);
               modelingTaskStoplistMapper.insertOrUpdate(modelAssessTaskStoplistDO);
           });
       }
        modelAssessTaskAutoMapper.updateById(updateObj);
    }

    @Override
    public void deleteModelAssessTaskAuto(Long id) {
        // 校验存在
        validateModelAssessTaskAutoExists(id);
        LambdaUpdateWrapper<ModelAssessTaskAutoDO> wrapper = new LambdaUpdateWrapper<>();
        // 删除
        wrapper.eq(ModelAssessTaskAutoDO::getId, id)
                .set(ModelAssessTaskAutoDO::getDeleted, true);
        modelAssessTaskAutoMapper.update(null, wrapper);
    }

    private void validateModelAssessTaskAutoExists(Long id) {
        if (modelAssessTaskAutoMapper.selectById(id) == null) {
            throw exception(MODEL_ASSESS_TASK_AUTO_NOT_EXISTS);
        }
    }

    @Override
    public ModelAssessTaskAutoRespVO getModelAssessTaskAuto(Long id) {
        ModelAssessTaskAutoDO modelAssessTaskAutoDO = modelAssessTaskAutoMapper.selectById(id);
        ModelAssessTaskAutoRespVO result =  BeanUtils.toBean(modelAssessTaskAutoDO, ModelAssessTaskAutoRespVO.class);

        // 标注查询
        List<ModelAssessTaskStoplistDO> modelAssessTaskStoplistDOS = modelingTaskStoplistMapper.selectList(new LambdaQueryWrapper<ModelAssessTaskStoplistDO>()
                .eq(ModelAssessTaskStoplistDO::getTaskId, id)
                .eq(ModelAssessTaskStoplistDO::getDeleted, false));
        List<ModelAssessTaskStoplistRespVO> stoplistRespVOS = BeanUtils.toBean(modelAssessTaskStoplistDOS, ModelAssessTaskStoplistRespVO.class);
        // 提取id
        List<Long> stoplistIds = modelAssessTaskStoplistDOS.stream().map(ModelAssessTaskStoplistDO::getStoplistId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(stoplistIds)){
            // 查询停用词表 将词表word返回
            LambdaQueryWrapper<ModelAssessStoplistDO> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(ModelAssessStoplistDO::getId,stoplistIds);
            List<ModelAssessStoplistDO> modelAssessStoplistDOs = modelAssessStoplistMapper.selectList(wrapper);
            Map<Long, ModelAssessStoplistDO> longModelServiceDOMap = cn.iocoder.yudao.framework.common.util.collection.
                    CollectionUtils.convertMap(modelAssessStoplistDOs, ModelAssessStoplistDO::getId);
            stoplistRespVOS.stream().forEach(stoplistRespVO -> {
                ModelAssessStoplistDO modelAssessStoplistDO = longModelServiceDOMap.get(stoplistRespVO.getStoplistId());
                if(modelAssessStoplistDO != null){
                    stoplistRespVO.setStoplistName(modelAssessStoplistDO.getWord());
                }
            });
        }
        result.setStoplists(stoplistRespVOS);

        ModelServiceDO modelServiceDO = modelServiceMapper.selectById(modelAssessTaskAutoDO.getModelService());
        if (modelServiceDO != null){
            result.setModelServiceName(modelServiceDO.getServiceName());
        }
        DatasetDO datasetDO = datasetMapper.selectById(modelAssessTaskAutoDO.getDataset());
        if (datasetDO != null){
            result.setDatasetName(datasetDO.getDatasetName());
        }
        return result;
    }

    @Override
    public PageResult<ModelAssessTaskAutoDO> getModelAssessTaskAutoPage(ModelAssessTaskAutoPageReqVO pageReqVO) {
        return modelAssessTaskAutoMapper.selectPage(pageReqVO);
    }
    @Override
    public PageResult<ModelAssessTaskAutoRespVO> getModelAssessTaskAutoPage1(ModelAssessTaskAutoPageReqVO pageReqVO) {

        PageResult<ModelAssessTaskAutoDO> modelAssessTaskAutoDOPageResult = modelAssessTaskAutoMapper.selectPage(pageReqVO);
        PageResult<ModelAssessTaskAutoRespVO> result = BeanUtils.toBean(modelAssessTaskAutoDOPageResult, ModelAssessTaskAutoRespVO.class);
        if (CollectionUtils.isNotEmpty(result.getList())){
            List<Long> serviceIds =  modelAssessTaskAutoDOPageResult.getList().stream().map(ModelAssessTaskAutoDO::getModelService).collect(Collectors.toList());
            LambdaQueryWrapper<ModelServiceDO> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(ModelServiceDO::getId,serviceIds);
            List<ModelServiceDO> modelServiceDOS = modelServiceMapper.selectList(wrapper);
            Map<Long, ModelServiceDO> longModelServiceDOMap = cn.iocoder.yudao.framework.common.util.collection.
                    CollectionUtils.convertMap(modelServiceDOS, ModelServiceDO::getId);
            result.getList().stream().forEach(modelAssessTaskAutoRespVO -> {
                ModelServiceDO modelServiceDO = longModelServiceDOMap.get(modelAssessTaskAutoRespVO.getModelService());
                if(modelServiceDO != null){
                    modelAssessTaskAutoRespVO.setModelServiceName(modelServiceDO.getServiceName());
                }
            });
        }
        return result;
    }

}