package com.cirle.scientific.service.impl;/*
 *24901
 *2022/12/21
 *17:40
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.cirle.scientific.dao.*;
import com.cirle.scientific.dto.qkjquestionoption.QkjQuestionOptionAppDTO;
import com.cirle.scientific.dto.questiondetail.QkjQuestionDetailAppDTO;
import com.cirle.scientific.dto.questionnaireapp.QkjQuestionnaireAppDTO;
import com.cirle.scientific.po.*;
import com.cirle.scientific.service.PatientAppService;
import com.cirle.scientific.service.QuestionnaireService;
import com.cirle.scientific.utils.DateFormater;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
@Slf4j
public class QuestionnaireServiceImpl implements QuestionnaireService {

    private QkjQuestionnaireAppDao qkjQuestionnaireAppDao;

    private QkjQuestionDetailAppDao qkjQuestionDetailAppDao;

    private QkjQuestionOptionAppDao qkjQuestionOptionAppDao;

    private PatientAppService patientAppService;

    private QkjExaminationAppDao qkjExaminationAppDao;

    @Autowired
    public void setQkjExaminationAppDao(QkjExaminationAppDao qkjExaminationAppDao) {
        this.qkjExaminationAppDao = qkjExaminationAppDao;
    }

    @Autowired
    public void setPatientAppService(PatientAppService patientAppService) {
        this.patientAppService = patientAppService;
    }

    @Autowired
    public void setQkjQuestionOptionAppDao(QkjQuestionOptionAppDao qkjQuestionOptionAppDao) {
        this.qkjQuestionOptionAppDao = qkjQuestionOptionAppDao;
    }

    @Autowired
    public void setQkjQuestionDetailAppDao(QkjQuestionDetailAppDao qkjQuestionDetailAppDao) {
        this.qkjQuestionDetailAppDao = qkjQuestionDetailAppDao;
    }


    @Autowired
    public void setQkjQuestionnaireAppDao(QkjQuestionnaireAppDao qkjQuestionnaireAppDao) {
        this.qkjQuestionnaireAppDao = qkjQuestionnaireAppDao;
    }

    @Override
    public List<QkjQuestionnaireAppDTO> findQuestionnaire() {
        List<QkjQuestionnaireAppDTO> qkjQuestionnaireAppDTO = new ArrayList<>();
        try {
            List<QkjQuestionnaireApp> qkjQuestionnaireApps = qkjQuestionnaireAppDao.selectAll();
            for (QkjQuestionnaireApp qkjQuestionnaireApp : qkjQuestionnaireApps) {
                QkjQuestionnaireAppDTO questionnaireAppDTO = BeanUtil.copyProperties(qkjQuestionnaireApp, QkjQuestionnaireAppDTO.class);
                qkjQuestionnaireAppDTO.add(questionnaireAppDTO);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qkjQuestionnaireAppDTO;
    }

    @Override
    public Map<Integer ,List<QkjQuestionDetailAppDTO>> findQuestions(Integer type, Integer id) {
        Map<Integer ,List<QkjQuestionDetailAppDTO>> map = new HashMap<>();
        List<QkjExaminationApp> qkjExaminationApps = qkjExaminationAppDao.selectExIdByTypeAndObjId(type, id);
        if (CollectionUtil.isNotEmpty(qkjExaminationApps)){
            for (QkjExaminationApp eId : qkjExaminationApps) {
                List<QkjQuestionDetailAppDTO> qkjQuestionDetailAppDTO = new ArrayList<>();
                List<QkjQuestionDetailApp> allByTypeAndObjId = qkjQuestionDetailAppDao.findAllByTypeAndObjId(type, eId.getId());
                for (QkjQuestionDetailApp qkjQuestionDetailApp : allByTypeAndObjId) {
                    List<QkjQuestionOptionAppDTO> optionDto = new ArrayList<>();
                    //获取试题选项
                    List<QkjQuestionOptionApp> qkjQuestionOptionApps = qkjQuestionOptionAppDao.selectAllByQuId(qkjQuestionDetailApp.getQuId());
                    for (QkjQuestionOptionApp qkjQuestionOptionApp : qkjQuestionOptionApps) {
                        QkjQuestionOptionAppDTO qkjQuestionOptionAppDTO = BeanUtil.copyProperties(qkjQuestionOptionApp, QkjQuestionOptionAppDTO.class);
                        qkjQuestionOptionAppDTO.setOptionId(qkjQuestionOptionApp.getId());
                        optionDto.add(qkjQuestionOptionAppDTO);
                    }
                    QkjQuestionDetailAppDTO questionDetailAppDTO = BeanUtil.copyProperties(qkjQuestionDetailApp, QkjQuestionDetailAppDTO.class);
                    questionDetailAppDTO.setOption(optionDto);
                    qkjQuestionDetailAppDTO.add(questionDetailAppDTO);
                }
                map.put(eId.getType(),qkjQuestionDetailAppDTO);
            }
        }
        return map;
    }

    @Override
    public Map<Integer, List<QkjQuestionDetailAppDTO>> findQuestionszByExId(Integer exId) {
        Map<Integer, List<QkjQuestionDetailAppDTO>> map = Maps.newHashMap();
        List<QkjQuestionDetailAppDTO> qkjQuestionDetailAppDTOList = new ArrayList<>();
        List<QkjQuestionDetailApp> allByTypeAndObjId = qkjQuestionDetailAppDao.findAllByTypeAndObjId(1, exId);
        for (QkjQuestionDetailApp qkjQuestionDetailApp : allByTypeAndObjId) {
            //获取试题选项
            List<QkjQuestionOptionApp> qkjQuestionOptionApps = qkjQuestionOptionAppDao.selectAllByQuId(qkjQuestionDetailApp.getQuId());
            List<QkjQuestionOptionAppDTO> optionDto = Lists.newArrayList();
            for (QkjQuestionOptionApp qkjQuestionOptionApp : qkjQuestionOptionApps) {
                QkjQuestionOptionAppDTO dto =  BeanUtil.copyProperties(qkjQuestionOptionApp, QkjQuestionOptionAppDTO.class);
                dto.setOptionId(qkjQuestionOptionApp.getId());
                optionDto.add(dto);
            }

            QkjQuestionDetailAppDTO questionDetailAppDTO = BeanUtil.copyProperties(qkjQuestionDetailApp, QkjQuestionDetailAppDTO.class);
            questionDetailAppDTO.setOption(optionDto);
            qkjQuestionDetailAppDTOList.add(questionDetailAppDTO);
        }
        map.put(exId, qkjQuestionDetailAppDTOList);
        return map;
    }

    @Override
    public List<QkjPatientApp> findDoctorQuestions(String doctorCodeMemCard) {
        try {
            String firstDay = DateFormater.thisMonthFirstDay();
            String lastDay = DateFormater.thisMonthLastDay();
            return patientAppService.findIsAccomplishQuestion(doctorCodeMemCard, firstDay, lastDay);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("接口: /app/QuestionnaireApp/findDoctorQuestions报错 e->{e}", e);
        }
        return null;
    }

}
