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

import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetAnswerDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetQuestionDO;
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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


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 DatasetQuestionServiceImpl implements DatasetQuestionService {

    @Resource
    private DatasetQuestionMapper datasetQuestionMapper;
    @Resource
    private DatasetAnswerMapper datasetAnswerMapper;
    @Resource
    private DatasetMapper datasetMapper;

    @Override
    public Long createDatasetQuestion(DatasetQuestionSaveReqVO createReqVO) {
        // 插入
        DatasetQuestionDO datasetQuestion = BeanUtils.toBean(createReqVO, DatasetQuestionDO.class);
        datasetQuestionMapper.insert(datasetQuestion);
        // 返回
        return datasetQuestion.getId();
    }

    @Override
    public void updateDatasetQuestion(DatasetQuestionSaveReqVO updateReqVO) {
        // 校验存在
        validateDatasetQuestionExists(updateReqVO.getId());
        // 更新
        DatasetQuestionDO updateObj = BeanUtils.toBean(updateReqVO, DatasetQuestionDO.class);
        datasetQuestionMapper.updateById(updateObj);
    }

    @Override
    public void deleteDatasetQuestion(Long id) {
        // 校验存在
        validateDatasetQuestionExists(id);
        // 删除
        datasetQuestionMapper.deleteById(id);
    }

    private void validateDatasetQuestionExists(Long id) {
        if (datasetQuestionMapper.selectById(id) == null) {
            throw exception(DATASET_QUESTION_NOT_EXISTS);
        }
    }

    @Override
    public DatasetQuestionDO getDatasetQuestion(Long id) {
        return datasetQuestionMapper.selectById(id);
    }

    @Override
    public PageResult<DatasetQuestionRespVO> getDatasetQuestionPage(DatasetQuestionPageReqVO pageReqVO) {
        PageResult<DatasetQuestionDO> datasetQuestionDOPageResult = datasetQuestionMapper.selectPage(pageReqVO);
        PageResult<DatasetQuestionRespVO> result = BeanUtils.toBean(datasetQuestionDOPageResult, DatasetQuestionRespVO.class);
        if (CollectionUtils.isNotEmpty(result.getList())){
            result.getList().forEach(item -> {
                List<DatasetAnswerDO> datasetAnswerDOS = datasetAnswerMapper.selectList(new LambdaQueryWrapper<>(DatasetAnswerDO.class)
                        .eq(DatasetAnswerDO::getQuestionId, item.getId()));
                item.setDatasetAnswerRespVO(BeanUtils.toBean(datasetAnswerDOS, DatasetAnswerRespVO.class));

            });
        }
        return result;
    }

    @Override
    public void updateDatasetQuestionDataAnno(List<DatasetQuestionSaveReqVO> updateReqVOS) {
        updateReqVOS.forEach(updateReqVO -> {
            DatasetQuestionDO datasetQuestionDO =  BeanUtils.toBean(updateReqVO, DatasetQuestionDO.class);
            datasetQuestionMapper.updateById(datasetQuestionDO);
            List<DatasetAnswerSaveReqVO> datasetAnswerSaveReqVO = updateReqVO.getDatasetAnswerRespVO();
            List<DatasetAnswerDO> datasetAnswerDOS = BeanUtils.toBean(datasetAnswerSaveReqVO, DatasetAnswerDO.class);
            if (CollectionUtils.isNotEmpty(datasetAnswerDOS)){
                datasetAnswerMapper.insertOrUpdate(datasetAnswerDOS);
            }
        });
        // 标注进度修改
        LambdaQueryWrapper<DatasetQuestionDO> wrapper =  new LambdaQueryWrapper<DatasetQuestionDO>()
                .eq(DatasetQuestionDO::getDatasetId, updateReqVOS.get(0).getDatasetId());
        Long sumCount = datasetQuestionMapper.selectCount(wrapper);
        wrapper.eq(DatasetQuestionDO::getStatus,2);
        Long annoCount = datasetQuestionMapper.selectCount(wrapper);
        double ratio = sumCount == 0 ? 0 : ((double) annoCount / sumCount) *100;
        Integer formattedRatio = ratio == 0 ? 0 : (int) ratio;
        datasetMapper.updateProcess(formattedRatio, updateReqVOS.get(0).getDatasetId());
    }

}