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

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetAnswerRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskauto.vo.ModelAssessTaskAutoPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskauto.vo.ModelAssessTaskAutoRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskauto.vo.ModelAssessTaskStoplistRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual.manualAnnoVo.ManualModelAnswerPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual.vo.LabelInformationVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual.vo.ManualAssessmentPageRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual.vo.ManualEvaluationReportRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanual.vo.ModelAssessTaskDimensionRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanualbackup.vo.ManualModelAnswerBackupPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanualbackup.vo.ModelAssessTaskManualBackupPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanualbackup.vo.ModelAssessTaskManualBackupRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.modelassesstaskmanualbackup.vo.ModelAssessTaskManualBackupSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.basemodel.BaseModelDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetAnswerDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetQuestionDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassessstoplist.ModelAssessStoplistDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskauto.ModelAssessTaskAutoBackupDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskauto.ModelAssessTaskStoplistBackupDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskmanual.ModelAssessDimensionDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskmanualbackup.ManualModelAnswerBackupDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskmanualbackup.ModelAssessTaskDimensionBackupDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.modelassesstaskmanualbackup.ModelAssessTaskManualBackupDO;
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.dataset.DatasetAnswerMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.dataset.DatasetMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.dataset.DatasetQuestionMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassessstoplist.ModelAssessStoplistMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassesstaskauto.ModelAssessTaskAutoBackupMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassesstaskauto.ModelAssessTaskStoplistBackupMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassesstaskmanual.ModelAssessDimensionMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassesstaskmanualbackup.ManualModelAnswerBackupMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassesstaskmanualbackup.ModelAssessTaskDimensionBackupMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelassesstaskmanualbackup.ModelAssessTaskManualBackupMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.modelservice.ModelServiceMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.MODEL_ASSESS_TASK_MANUAL_BACKUP_EXISTS;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.MODEL_ASSESS_TASK_MANUAL_BACKUP_NOT_EXISTS;

/**
 * 人工评估备份 Service 实现类
 *
 * @author 华大大模型
 */
@Service
@Validated
public class ModelAssessTaskManualBackupServiceImpl implements ModelAssessTaskManualBackupService {

    @Resource
    private ModelAssessTaskManualBackupMapper modelAssessTaskManualBackupMapper;
    @Resource
    private ModelAssessTaskDimensionBackupMapper modelAssessTaskDimensionBackupMapper;
    @Resource
    private ManualModelAnswerBackupMapper manualModelAnswerBackupMapper;
    @Resource
    private ModelAssessDimensionMapper modelAssessDimensionMapper;
    @Resource
    private ModelServiceMapper modelServiceMapper;
    @Resource
    private DatasetMapper datasetMapper;
    @Resource
    private BaseModelMapper baseModelMapper;
    @Resource
    private ModelAssessTaskAutoBackupMapper modelAssessTaskAutoBaackupMapper;
    @Resource
    private ModelAssessTaskStoplistBackupMapper modelAssessTaskStoplistBackupMapper;
    @Resource
    private ModelAssessStoplistMapper modelAssessStoplistMapper;
    @Resource
    private DatasetQuestionMapper datasetQuestionMapper;
    @Resource
    private DatasetAnswerMapper datasetAnswerMapper;

    @Override
    public Long createModelAssessTaskManualBackup (ModelAssessTaskManualBackupSaveReqVO createReqVO) {
        // 插入
        ModelAssessTaskManualBackupDO modelAssessTaskManualBackup = BeanUtils.toBean(createReqVO, ModelAssessTaskManualBackupDO.class);
        modelAssessTaskManualBackupMapper.insert(modelAssessTaskManualBackup);
        // 返回
        return modelAssessTaskManualBackup.getId();
    }

    @Override
    public void updateModelAssessTaskManualBackup (ModelAssessTaskManualBackupSaveReqVO updateReqVO) {
        // 校验存在
        validateModelAssessTaskManualBackupExists(updateReqVO.getId());
        // 更新
        ModelAssessTaskManualBackupDO updateObj = BeanUtils.toBean(updateReqVO, ModelAssessTaskManualBackupDO.class);
        modelAssessTaskManualBackupMapper.updateById(updateObj);
    }

    @Override
    public void deleteModelAssessTaskManualBackup (Long id) {
        // 校验存在
        validateModelAssessTaskManualBackupExists(id);
        // 删除
        modelAssessTaskManualBackupMapper.deleteById(id);
    }

    private void validateModelAssessTaskManualBackupExists (Long id) {
        if (modelAssessTaskManualBackupMapper.selectById(id) == null) {
            throw exception(MODEL_ASSESS_TASK_MANUAL_BACKUP_NOT_EXISTS);
        }
    }

    @Override
    public ModelAssessTaskManualBackupDO getModelAssessTaskManualBackup (Long id) {
        return modelAssessTaskManualBackupMapper.selectById(id);
    }

    @Override
    public PageResult<ModelAssessTaskManualBackupDO> getModelAssessTaskManualBackupPage (ModelAssessTaskManualBackupPageReqVO pageReqVO) {
        return modelAssessTaskManualBackupMapper.selectPage(pageReqVO);
    }

    @Override
    public void isBackup (Long srcId) {
        List<ModelAssessTaskManualBackupDO> backupDOList = this.modelAssessTaskManualBackupMapper.selectList("src_id", srcId);
        if (backupDOList != null && !backupDOList.isEmpty()) {
            throw exception(MODEL_ASSESS_TASK_MANUAL_BACKUP_EXISTS);
        }
    }

    @Override
    public void unBackup (Long id) {
        modelAssessTaskManualBackupMapper.deleteTrue(id);
        modelAssessTaskDimensionBackupMapper.deleteTrue(id);
        manualModelAnswerBackupMapper.deleteTrue(id);
    }

    @Override
    public ModelAssessTaskManualBackupRespVO getModelAssessTaskManualBackup1 (Long id) {
        ModelAssessTaskManualBackupDO modelAssessTaskManual = modelAssessTaskManualBackupMapper.selectById(id);
        ModelAssessTaskManualBackupRespVO result = BeanUtils.toBean(modelAssessTaskManual, ModelAssessTaskManualBackupRespVO.class);
        List<ModelAssessTaskDimensionBackupDO> modelAssessTaskDimensionDOS = modelAssessTaskDimensionBackupMapper.selectList(new LambdaQueryWrapper<ModelAssessTaskDimensionBackupDO>()
                .eq(ModelAssessTaskDimensionBackupDO::getTaskId, id)
                .eq(ModelAssessTaskDimensionBackupDO::getDeleted, false));
        List<ModelAssessTaskDimensionRespVO> modelAssessTaskDimensionRespVOS = BeanUtils.toBean(modelAssessTaskDimensionDOS, ModelAssessTaskDimensionRespVO.class);
        // 提取id
        List<Long> dimensionIds = modelAssessTaskDimensionDOS.stream().map(ModelAssessTaskDimensionBackupDO::getDimensionId).collect(Collectors.toList());
        // 查询模型评估维度 将维度名称返回
        if (!CollectionUtils.isEmpty(dimensionIds)) {
            List<ModelAssessDimensionDO> modelAssessDimensionDOS = modelAssessDimensionMapper.selectList(new LambdaQueryWrapper<ModelAssessDimensionDO>()
                    .in(ModelAssessDimensionDO::getId, dimensionIds));
            Map<Long, ModelAssessDimensionDO> longModelDimesionDOMap = cn.iocoder.yudao.framework.common.util.collection.
                    CollectionUtils.convertMap(modelAssessDimensionDOS, ModelAssessDimensionDO::getId);
            modelAssessTaskDimensionRespVOS.stream().forEach(
                    modelAssessTaskDimensionRespVO -> {
                        ModelAssessDimensionDO modelAssessDimensionDO = longModelDimesionDOMap.get(modelAssessTaskDimensionRespVO.getDimensionId());
                        if (modelAssessDimensionDO != null) {
                            modelAssessTaskDimensionRespVO.setDimensionName(modelAssessDimensionDO.getDimension());
                        }
                    }
            );
        }
        result.setDimensions(modelAssessTaskDimensionRespVOS);
        if (modelAssessTaskManual.getModelType() == 0) {
            ModelServiceDO modelServiceDO = modelServiceMapper.selectById(modelAssessTaskManual.getModelService());
            if (modelServiceDO != null) {
                result.setModelServiceName(modelServiceDO.getServiceName());
            }
        } else {
            BaseModelDO baseModelDO = baseModelMapper.selectById(modelAssessTaskManual.getModelService());
            if (baseModelDO != null) {
                result.setModelServiceName(baseModelDO.getModelName());
            }
        }
        DatasetDO datasetDO = datasetMapper.selectById(modelAssessTaskManual.getDataset());
        if (datasetDO != null) {
            result.setDatasetName(datasetDO.getDatasetName());
        }
        return result;
    }

    @Override
    public List<ManualEvaluationReportRespVO> gitViewTheReportDetails (Long id) {
        List<ManualEvaluationReportRespVO> manualEvaluationReportRespVOS = new ArrayList<>();
        LambdaQueryWrapper<ManualModelAnswerBackupDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManualModelAnswerBackupDO::getManalTaskId, id)
                .eq(ManualModelAnswerBackupDO::getStatus, 2)
                .isNotNull(ManualModelAnswerBackupDO::getReqRespVos);
        List<ManualModelAnswerBackupDO> manualModelAnswerDOS = manualModelAnswerBackupMapper.selectList(wrapper);
        // 获取维度
        List<String> collect = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(manualModelAnswerDOS)) {
            List<LabelInformationVO> bean = BeanUtils.toBean(manualModelAnswerDOS.get(0).getReqRespVos(), LabelInformationVO.class);
            collect = bean.stream().map(LabelInformationVO::getLabel).collect(Collectors.toList());
        }
        List<LabelInformationVO> labelInformationVOS = new ArrayList<>();
        for (ManualModelAnswerBackupDO manualModelAnswerDO : manualModelAnswerDOS) {
            List<HashMap<String, Object>> reqRespVos = manualModelAnswerDO.getReqRespVos();
            if (CollectionUtils.isNotEmpty(reqRespVos)) {
                List<LabelInformationVO> bean = BeanUtils.toBean(reqRespVos, LabelInformationVO.class);
                labelInformationVOS.addAll(bean);
            }
        }

        if (CollectionUtils.isNotEmpty(labelInformationVOS)) {
            // 计算平均分
            double average = labelInformationVOS.stream().mapToDouble(LabelInformationVO::getScore).average().getAsDouble();
            double score = average * 50.0;
            ManualEvaluationReportRespVO reportRespVO = new ManualEvaluationReportRespVO();
            double bd = new BigDecimal(average).setScale(2, RoundingMode.HALF_UP).doubleValue();
            double v = new BigDecimal(score).setScale(2, RoundingMode.HALF_UP).doubleValue();
            reportRespVO.setDimension("平均分");
            reportRespVO.setScore(bd);
            reportRespVO.setPercent(v);
            manualEvaluationReportRespVOS.add(reportRespVO);

            if (CollectionUtils.isNotEmpty(collect)) {
                boolean allBlank = true;

                for (String collect1 : collect) {
                    if (StringUtils.isNotBlank(collect1)) {
                        allBlank = false;
                        break;
                    }
                }

                if (allBlank) {
                    return manualEvaluationReportRespVOS;
                }

                for (String collect1 : collect) {

                    if (StringUtils.isBlank(collect1)){
                        continue;
                    }

                    List<LabelInformationVO> voList = labelInformationVOS.stream().filter(manualModelAnswerDO -> manualModelAnswerDO.getLabel().equals(collect1)).collect(Collectors.toList());
                    ManualEvaluationReportRespVO reportRespVO1 = new ManualEvaluationReportRespVO();
                    reportRespVO1.setDimension(collect1);
                    double average1 = voList.stream().mapToDouble(LabelInformationVO::getScore).average().getAsDouble();
                    reportRespVO1.setScore(new BigDecimal(average1).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    reportRespVO1.setPercent(new BigDecimal(average1 * 50.0).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    long count = voList.stream().filter(manualModelAnswerDO -> manualModelAnswerDO.getScore() == 0).count();
                    double zeroPercent = (double) count / voList.size() * 100.0;
                    reportRespVO1.setZeroPercent(new BigDecimal(zeroPercent).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    long count1 = voList.stream().filter(manualModelAnswerDO -> manualModelAnswerDO.getScore() == 1).count();
                    long count2 = voList.stream().filter(manualModelAnswerDO -> manualModelAnswerDO.getScore() == 2).count();
                    double onePercent = (double) count1 / voList.size() * 100.0;
                    reportRespVO1.setOnePercent(new BigDecimal(onePercent).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    double twoPercent = (double) count2 / voList.size() * 100.0;
                    reportRespVO1.setTwoPercent(new BigDecimal(twoPercent).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    manualEvaluationReportRespVOS.add(reportRespVO1);
                }
            }
        }
        return manualEvaluationReportRespVOS;
    }

    @Override
    public PageResult<ModelAssessTaskAutoRespVO> getModelAssessTaskAutoPage1 (ModelAssessTaskAutoPageReqVO pageReqVO) {
        PageResult<ModelAssessTaskAutoBackupDO> modelAssessTaskAutoDOPageResult = modelAssessTaskAutoBaackupMapper.selectPage(pageReqVO);
        PageResult<ModelAssessTaskAutoRespVO> result = BeanUtils.toBean(modelAssessTaskAutoDOPageResult, ModelAssessTaskAutoRespVO.class);
        List<ModelAssessTaskAutoRespVO> resultList = result.getList();
        if (CollectionUtils.isNotEmpty(resultList)) {
            // 我的模型获取校验
            List<Long> serviceIds = modelAssessTaskAutoDOPageResult.getList().stream().
                    filter(modelAssessTaskManualDO -> modelAssessTaskManualDO.getModelType() == 0)
                    .map(ModelAssessTaskAutoBackupDO::getModelService).collect(Collectors.toList());
            Map<Long, ModelServiceDO> longModelServiceDOMap = null;
            if (!CollectionUtils.isEmpty(serviceIds)) {
                LambdaQueryWrapper<ModelServiceDO> wrapper = new LambdaQueryWrapper<>();
                wrapper.in(ModelServiceDO::getId, serviceIds);
                List<ModelServiceDO> modelServiceDOS = modelServiceMapper.selectList(wrapper);
                longModelServiceDOMap = cn.iocoder.yudao.framework.common.util.collection.
                        CollectionUtils.convertMap(modelServiceDOS, ModelServiceDO::getId);
            }
            // 人工模型获取校验
            List<Long> serviceForeseeIds = modelAssessTaskAutoDOPageResult.getList().stream().
                    filter(modelAssessTaskManualDO -> modelAssessTaskManualDO.getModelType() == 1)
                    .map(ModelAssessTaskAutoBackupDO::getModelService).collect(Collectors.toList());
            Map<Long, BaseModelDO> serviceForeseeMaps = null;
            if (!CollectionUtils.isEmpty(serviceForeseeIds)) {
                List<BaseModelDO> baseModelDOS = baseModelMapper.selectList(new LambdaQueryWrapper<BaseModelDO>()
                        .in(BaseModelDO::getId, serviceForeseeIds));
                serviceForeseeMaps = cn.iocoder.yudao.framework.common.util.collection.
                        CollectionUtils.convertMap(baseModelDOS, BaseModelDO::getId);
            }
            for (ModelAssessTaskAutoRespVO modelAssessTaskManualRespVO : resultList) {
                if (modelAssessTaskManualRespVO.getModelType() == 0 && longModelServiceDOMap != null) {
                    ModelServiceDO modelServiceDO = longModelServiceDOMap.get(modelAssessTaskManualRespVO.getModelService());
                    if (modelServiceDO != null) {
                        modelAssessTaskManualRespVO.setModelServiceName(modelServiceDO.getServiceName());
                    }
                } else if (modelAssessTaskManualRespVO.getModelType() == 1 && serviceForeseeMaps != null) {
                    BaseModelDO baseModelDO = serviceForeseeMaps.get(modelAssessTaskManualRespVO.getModelService());
                    if (baseModelDO != null) {
                        modelAssessTaskManualRespVO.setModelServiceName(baseModelDO.getModelName());
                    }
                }

            }
        }
        return result;
    }

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

        // 标注查询
        List<ModelAssessTaskStoplistBackupDO> modelAssessTaskStoplistDOS = modelAssessTaskStoplistBackupMapper.selectList(new LambdaQueryWrapper<ModelAssessTaskStoplistBackupDO>()
                .eq(ModelAssessTaskStoplistBackupDO::getTaskId, modelAssessTaskAutoDO.getId())
                .eq(ModelAssessTaskStoplistBackupDO::getDeleted, false));
        List<ModelAssessTaskStoplistRespVO> stoplistRespVOS = BeanUtils.toBean(modelAssessTaskStoplistDOS, ModelAssessTaskStoplistRespVO.class);
        // 提取id
        List<Long> stoplistIds = modelAssessTaskStoplistDOS.stream().map(ModelAssessTaskStoplistBackupDO::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<ModelAssessTaskManualBackupRespVO> getModelAssessTaskManualPage1 (ModelAssessTaskManualBackupPageReqVO pageReqVO) {
        PageResult<ModelAssessTaskManualBackupDO> modelAssessTaskManualDOPageResult = modelAssessTaskManualBackupMapper.selectPage(pageReqVO);
        PageResult<ModelAssessTaskManualBackupRespVO> result = BeanUtils.toBean(modelAssessTaskManualDOPageResult, ModelAssessTaskManualBackupRespVO.class);
        if (CollectionUtils.isNotEmpty(result.getList())) {
            // 我的模型获取校验
            List<Long> serviceMyIds = modelAssessTaskManualDOPageResult.getList().stream().
                    filter(modelAssessTaskManualDO -> modelAssessTaskManualDO.getModelType() == 0)
                    .map(ModelAssessTaskManualBackupDO::getModelService).collect(Collectors.toList());
            List<ModelServiceDO> modelServiceDOS = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(serviceMyIds)) {
                modelServiceDOS = modelServiceMapper.selectList(new LambdaQueryWrapper<ModelServiceDO>()
                        .in(ModelServiceDO::getId, serviceMyIds));
            }
            Map<Long, ModelServiceDO> serviceMyMaps = cn.iocoder.yudao.framework.common.util.collection.
                    CollectionUtils.convertMap(modelServiceDOS, ModelServiceDO::getId);
            // 人工模型获取校验
            List<Long> serviceForeseeIds = modelAssessTaskManualDOPageResult.getList().stream().
                    filter(modelAssessTaskManualDO -> modelAssessTaskManualDO.getModelType() == 1)
                    .map(ModelAssessTaskManualBackupDO::getModelService).collect(Collectors.toList());
            List<BaseModelDO> baseModelDOS = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(serviceForeseeIds)) {
                baseModelDOS = baseModelMapper.selectList(new LambdaQueryWrapper<BaseModelDO>()
                        .in(BaseModelDO::getId, serviceForeseeIds));
            }
            Map<Long, BaseModelDO> serviceForeseeMaps = cn.iocoder.yudao.framework.common.util.collection.
                    CollectionUtils.convertMap(baseModelDOS, BaseModelDO::getId);
            result.getList().stream().forEach(modelAssessTaskManualRespVO -> {
                if (modelAssessTaskManualRespVO.getModelType() == 0) {
                    ModelServiceDO modelServiceDO = serviceMyMaps.get(modelAssessTaskManualRespVO.getModelService());
                    if (modelServiceDO != null) {
                        modelAssessTaskManualRespVO.setModelServiceName(modelServiceDO.getServiceName());
                    }
                } else {
                    BaseModelDO baseModelDO = serviceForeseeMaps.get(modelAssessTaskManualRespVO.getModelService());
                    if (baseModelDO != null) {
                        modelAssessTaskManualRespVO.setModelServiceName(baseModelDO.getModelName());
                    }
                }

            });
        }
        return result;
    }

    @Override
    public PageResult<ManualAssessmentPageRespVO> getManualAssessment (ManualModelAnswerBackupPageReqVO pageReqVO) {
        ModelAssessTaskManualBackupRespVO modelAssessTaskManual = getModelAssessTaskManualBackup1(pageReqVO.getManalTaskId());
        PageResult<ManualAssessmentPageRespVO> result = new PageResult<>();
        PageResult<ManualModelAnswerBackupDO> modelAnswerDOPageResult = manualModelAnswerBackupMapper.selectPage(pageReqVO);
        List<ManualAssessmentPageRespVO> res = new ArrayList<>();
        if (!cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.isAnyEmpty(modelAnswerDOPageResult.getList())) {
            modelAnswerDOPageResult.getList().forEach(modelAnswerDO -> {
                ManualAssessmentPageRespVO resp = BeanUtils.toBean(modelAssessTaskManual, ManualAssessmentPageRespVO.class);
                DatasetQuestionDO datasetQuestionDO = datasetQuestionMapper.selectById(modelAnswerDO.getQuestionId());
                List<DatasetAnswerDO> datasetAnswerDOS = datasetAnswerMapper.selectList(new LambdaQueryWrapper<>(DatasetAnswerDO.class).eq(DatasetAnswerDO::getQuestionId, modelAnswerDO.getQuestionId()));
                resp.setQuestion(datasetQuestionDO.getQuestion());
                resp.setSystem(datasetQuestionDO.getSystem());
                resp.setAnswers(BeanUtils.toBean(datasetAnswerDOS, DatasetAnswerRespVO.class));
                resp.setModelAnswer(modelAnswerDO.getModelAnswer());
                resp.setId(modelAnswerDO.getId());
                resp.setStatus(modelAnswerDO.getStatus());
                resp.setManalTaskId(modelAnswerDO.getManalTaskId());
                if (!cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.isAnyEmpty(modelAnswerDO.getReqRespVos())) {
                    resp.setReqRespVos(modelAnswerDO.getReqRespVos());
                } else {
                    if (!cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.isAnyEmpty(modelAssessTaskManual.getDimensions())) {
                        List<HashMap<String, Object>> map = new ArrayList<>();
                        modelAssessTaskManual.getDimensions().forEach(dimension -> {
                            HashMap<String, Object> map1 = new HashMap<>();
                            map1.put("label", dimension.getDimensionName());
                            map1.put("score", 0);
                            map.add(map1);
                        });
                        resp.setReqRespVos(map);
                    }
                }
                res.add(resp);
            });
        }
        result.setList(res);
        result.setTotal(modelAnswerDOPageResult.getTotal());
        return result;
    }

    @Override
    public List<String> queryTheEvaluationType (ManualModelAnswerPageReqVO pageReqVO) {
        LambdaQueryWrapper<ManualModelAnswerBackupDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManualModelAnswerBackupDO::getManalTaskId, pageReqVO.getManalTaskId())
                .eq(ManualModelAnswerBackupDO::getStatus, 2)
                .isNotNull(ManualModelAnswerBackupDO::getReqRespVos);
        List<ManualModelAnswerBackupDO> manualModelAnswerDOS = manualModelAnswerBackupMapper.selectList(wrapper);
        if (ObjectUtil.isAllEmpty(manualModelAnswerDOS)) {
            return null;
        }
        List<LabelInformationVO> bean = BeanUtils.toBean(manualModelAnswerDOS.get(0).getReqRespVos(), LabelInformationVO.class);
        List<String> collect = bean.stream().map(LabelInformationVO::getLabel).collect(Collectors.toList());
        return collect;
    }

    /**
     * 根据 基础模型id 获取 人工评估备份
     *
     * @param baseModelId 基础模型id
     * @return 人工评估备份
     */
    @Override
    public Map<Long, String> getModelAssessmentTaskByModelId (Long baseModelId) {
        if (baseModelId == null) {
            return Collections.emptyMap();
        }

        List<ModelAssessTaskManualBackupDO> manualBackupDOS = getTaskManualBackup(baseModelId,1);

        if (CollectionUtils.isNotEmpty(manualBackupDOS)) {
            return manualBackupDOS.stream().collect(Collectors.toMap(ModelAssessTaskManualBackupDO::getId, ModelAssessTaskManualBackupDO::getTaskName));
        }

        return Collections.emptyMap();
    }

    /**
     * 根据 模型服务 id 获取 人工评估备份
     *
     * @param modelServiceId 模型服务 id
     * @return 人工评估备份
     */
    @Override
    public Map<Long, String> getModelAssessmentTaskByModelServiceId (Long modelServiceId) {
        if (modelServiceId == null) {
            return Collections.emptyMap();
        }

        List<ModelAssessTaskManualBackupDO> manualBackupDOS = getTaskManualBackup(modelServiceId,0);

        if (CollectionUtils.isNotEmpty(manualBackupDOS)) {
            return manualBackupDOS.stream().collect(Collectors.toMap(ModelAssessTaskManualBackupDO::getId, ModelAssessTaskManualBackupDO::getTaskName));
        }

        return Collections.emptyMap();
    }

    /**
     * 根据 数据集 id 获取 人工评估备份
     *
     * @param datasetId 数据集 id
     * @return 人工评估备份
     */
    @Override
    public Map<Long, String> getModelAssessmentTaskByDatasetId (Long datasetId) {
        if (datasetId == null) {
            return Collections.emptyMap();
        }

        List<ModelAssessTaskManualBackupDO> manualBackupDOS =  modelAssessTaskManualBackupMapper.selectList(new LambdaQueryWrapper<ModelAssessTaskManualBackupDO>()
                .eq(ModelAssessTaskManualBackupDO::getDataset, datasetId));

        if (CollectionUtils.isNotEmpty(manualBackupDOS)) {
            return manualBackupDOS.stream().collect(Collectors.toMap(ModelAssessTaskManualBackupDO::getId, ModelAssessTaskManualBackupDO::getTaskName));
        }

        return Collections.emptyMap();
    }

    /**
     * 根据模型ID和模型类型获取人工评估备份
     *
     * @param baseModelId 模型ID
     * @param modeType    模型类型 1:预制模型 0:我的模型
     * @return 人工评估备份
     */
    private List<ModelAssessTaskManualBackupDO> getTaskManualBackup (Long baseModelId, Integer modeType) {
        List<ModelAssessTaskManualBackupDO> manualBackupDOS = modelAssessTaskManualBackupMapper.selectList(new LambdaQueryWrapper<ModelAssessTaskManualBackupDO>()
                .eq(ModelAssessTaskManualBackupDO::getModelService, baseModelId)
                .eq(ModelAssessTaskManualBackupDO::getModelType, modeType));
        return manualBackupDOS;
    }
}
