package com.cirle.scientific.service.impl;/*
 *24901
 *2022/12/22
 *17:08
 */

import cn.hutool.core.collection.CollectionUtil;
import com.cirle.scientific.dao.QkjExaminationAppDao;
import com.cirle.scientific.dao.QkjQuestionAnswerAppDao;
import com.cirle.scientific.dao.QkjQuestionDetailAppDao;
import com.cirle.scientific.dto.questionanswer.QuestionAnswerAppDTO;
import com.cirle.scientific.po.QkjExaminationApp;
import com.cirle.scientific.po.QkjQuestionAnswerApp;
import com.cirle.scientific.service.PatientAppService;
import com.cirle.scientific.service.QuestionAnswerAppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @author 24901
 */
@Service
@Slf4j
public class QuestionAnswerAppServiceImpl implements QuestionAnswerAppService {


    /**
     * 问题关联表
     */
    private QkjQuestionDetailAppDao qkjQuestionDetailAppDao;

    /**
     * 用户回答保存
     */
    private QkjQuestionAnswerAppDao qkjQuestionAnswerAppDao;

    /**
     * 试题表
     */
    private QkjExaminationAppDao qkjExaminationAppDao;

    /**
     * 患者表接口
     */
    private PatientAppService patientAppService;

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

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

    @Autowired
    public void setQkjQuestionAnswerAppDao(QkjQuestionAnswerAppDao qkjQuestionAnswerAppDao) {
        this.qkjQuestionAnswerAppDao = qkjQuestionAnswerAppDao;
    }

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


    @Override
    public int addQuestionAnswer(String memCard, List<QkjQuestionAnswerApp> qkjQuestionAnswerApp) {
        try {
            QkjQuestionAnswerApp qkjQuestionAnswerApp1 = qkjQuestionAnswerApp.get(1);
            List<QkjQuestionAnswerApp> qkjQuestionAnswerApps = qkjQuestionAnswerAppDao.selectByMemCardAndTypeAndObjIdAndFrequency(memCard, qkjQuestionAnswerApp1.getType(), qkjQuestionAnswerApp1.getObjId(), qkjQuestionAnswerApp1.getFrequency(), qkjQuestionAnswerApp1.getPatientId());
            if(CollectionUtil.isNotEmpty(qkjQuestionAnswerApps)){
                for (QkjQuestionAnswerApp questionAnswerApp : qkjQuestionAnswerApps) {
                    questionAnswerApp.setStatus(1);
                    qkjQuestionAnswerAppDao.save(questionAnswerApp);
                }
            }
            if(CollectionUtil.isNotEmpty(qkjQuestionAnswerApp)){
                for (QkjQuestionAnswerApp questionAnswerApp : qkjQuestionAnswerApp) {
                    questionAnswerApp.setMemCard(memCard);
                    questionAnswerApp.setQuType(0);
                    questionAnswerApp.setCreateTime(new Date());
                    questionAnswerApp.setStatus(0);
                    qkjQuestionAnswerAppDao.save(questionAnswerApp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("批量新增用户问题回答表出错 e->{e}" , e);
            return 500;
        }
        return 200;
    }

    @Override
    public Object findQuestionScore(Integer type, Integer id, Integer patientId, String memCard) {
        List<QuestionAnswerAppDTO> questionAnswerApp = new ArrayList<>();
        List<QkjExaminationApp> qkjExaminationApps = qkjExaminationAppDao.selectExIdByTypeAndObjId(type, id);
        if(CollectionUtil.isNotEmpty(qkjExaminationApps)){
            for (QkjExaminationApp exId : qkjExaminationApps) {
                QuestionAnswerAppDTO questionAnswerAppDTO = new QuestionAnswerAppDTO();
                //获取当下问题id
                List<Integer> quIds = qkjQuestionDetailAppDao.findQuIdByTypeAndObjId(type, exId.getId());
                Integer score = qkjQuestionAnswerAppDao.selectScoreSumByTypeAndObjIdAndQuIdaAndMemCard(type, id, memCard, patientId, quIds);
                questionAnswerAppDTO.setExId(exId.getId());
                if(exId.getType()==2){
                    questionAnswerAppDTO.setExName("第一问卷");
                    patientAppService.updateIsAccomplishQuestion(memCard, patientId, type,id,null,score,null);
                }
                if(exId.getType()==3){
                    questionAnswerAppDTO.setExName("第二问卷");
                    patientAppService.updateIsAccomplishQuestion(memCard, patientId, type,id,null,null,score);
                }
                questionAnswerAppDTO.setScore(score);
                questionAnswerApp.add(questionAnswerAppDTO);
            }
        }
        patientAppService.updateIsAccomplishQuestion(memCard, patientId, type,id,1,null,null);
        return questionAnswerApp;
    }

    @Override
    public int addQuestionAnswerForYs(List<QkjQuestionAnswerApp> qkjQuestionAnswerApp) {
        try {
            if(CollectionUtil.isNotEmpty(qkjQuestionAnswerApp)){
                QkjQuestionAnswerApp answerApp = qkjQuestionAnswerApp.get(0);
                List<QkjQuestionAnswerApp> qkjQuestionAnswerApps = qkjQuestionAnswerAppDao.listByTypeAndMemCard(2, answerApp.getMemCard());
                qkjQuestionAnswerAppDao.deleteAll(qkjQuestionAnswerApps);
                for (QkjQuestionAnswerApp questionAnswerApp : qkjQuestionAnswerApp) {
                    questionAnswerApp.setStatus(0);
                }
                qkjQuestionAnswerAppDao.saveAll(qkjQuestionAnswerApp);
            }
        } catch (Exception e) {
            log.error("addQuestionAnswerForYs e->{e}" , e);
            return 500;
        }
        return 200;
    }

    @Override
    public List<QkjQuestionAnswerApp> findQuestionAnswer(String memCard, Integer type) {

        return qkjQuestionAnswerAppDao.listByTypeAndMemCard(type, memCard);
    }
}
