package com.cirle.scientific.service.impl;/*
 *24901
 *2022/12/21
 *11:08
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.cirle.scientific.dao.*;
import com.cirle.scientific.dto.course.RightDTO;
import com.cirle.scientific.dto.qkjquestionoption.QkjQuestionOptionAppDTO;
import com.cirle.scientific.dto.questiondetail.QkjQuestionDetailAppDTO;
import com.cirle.scientific.po.QkjPatientApp;
import com.cirle.scientific.po.QkjQuestionApp;
import com.cirle.scientific.po.QkjQuestionDetailApp;
import com.cirle.scientific.po.QkjQuestionOptionApp;
import com.cirle.scientific.service.PatientAppService;
import com.cirle.scientific.service.QuestionAppService;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 24901
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class QuestionAppServiceImpl implements QuestionAppService {

    private final QkjQuestionnaireAppDao qkjQuestionnaireAppDao;

    private final QkjQuestionDetailAppDao qkjQuestionDetailAppDao;

    private final QkjQuestionOptionAppDao qkjQuestionOptionAppDao;

    private final QkjQuestionAppDao qkjQuestionAppDao;


    @Override
    public List<RightDTO> listRightDTO(Integer exId) {
        //查询所有的问题

        List<RightDTO> rightDTOList = new ArrayList<>();
        List<QkjQuestionDetailApp> detailApps = qkjQuestionDetailAppDao.findAllByTypeAndObjId(1, exId);
        if(CollectionUtil.isEmpty(detailApps)){
            return Collections.emptyList();
        }
        List<Integer> quIds = detailApps.stream().map(QkjQuestionDetailApp::getQuId).collect(Collectors.toList());
        List<QkjQuestionApp> questionApps = qkjQuestionAppDao.findAllByIds(quIds);
        Map<Integer, String> quIdToAnalysisMap = Maps.newHashMap();
        if(CollectionUtil.isNotEmpty(questionApps)){
            quIdToAnalysisMap = questionApps.stream().collect(Collectors.toMap(QkjQuestionApp::getId, QkjQuestionApp::getAnalysis));
        }
        for (QkjQuestionDetailApp qkjQuestionDetailApp : detailApps) {
            //获取试题选项
            List<QkjQuestionOptionApp> qkjQuestionOptionApps = qkjQuestionOptionAppDao.selectAllByQuId(qkjQuestionDetailApp.getQuId());
            QkjQuestionOptionApp optionApp = qkjQuestionOptionApps.stream().filter(e -> e.getScore() > 0).findFirst().orElse(null);
            if(Objects.nonNull(optionApp)){
                String analysis = quIdToAnalysisMap.getOrDefault(qkjQuestionDetailApp.getQuId(), null);
                RightDTO build = RightDTO.builder()
                        .quId(qkjQuestionDetailApp.getQuId())
                        .quTitle(qkjQuestionDetailApp.getQuTitle())
                        .optionId(optionApp.getId().intValue())
                        .option(optionApp.getOption())
                        .optionTitle(optionApp.getOptionTitle())
                        .score(optionApp.getScore())
                        .analysis(analysis)
                        .build();
                rightDTOList.add(build);
            }
        }
        return rightDTOList;
    }
}
