package cn.com.qmth.examcloud.core.questions.service;

import cn.com.qmth.examcloud.core.questions.base.question.enums.QuesStructType;
import cn.com.qmth.examcloud.core.questions.dao.QuesTypeNameRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
import cn.com.qmth.examcloud.core.questions.dao.entity.QuesTypeName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songyue on 17/8/23.
 */
@Service
public class QuesTypeNameService {

    @Autowired
    private QuesTypeNameRepo quesTypeRepo;
    
    public void saveQuesTypeName(Long rootOrgId,String courseCode,QuesStructType questionType,String detailName) {
    	List<QuesTypeName> quesTypeNames = quesTypeRepo.findQuesName(rootOrgId.toString(), courseCode, questionType);
        if (quesTypeNames != null && quesTypeNames.size() > 0) {
            QuesTypeName quesTypeName = quesTypeNames.get(0);
            List<String> quesNames = quesTypeName.getQuesNames();
            if (quesNames != null && quesNames.size() > 0) {
                if (quesNames.contains(detailName)) {
                    return;
                } else {
                    quesNames.add(detailName);
                }
            } else {
                quesNames = new ArrayList<>();
                quesNames.add(detailName);
            }
            quesTypeName.setQuesNames(quesNames);
            quesTypeRepo.save(quesTypeName);
        } else {
            QuesTypeName quesTypeName = new QuesTypeName();
            List<String> quesNames = new ArrayList<>();
            quesNames.add(detailName);
            quesTypeName.setOrgId(rootOrgId.toString());
            quesTypeName.setCourseNo(courseCode);
            quesTypeName.setQuestionType(questionType);
            quesTypeName.setQuesNames(quesNames);
            quesTypeRepo.save(quesTypeName);
        }
    }
    /**
     * 保存来源大题名称
     *
     * @param detailUnitList
     */
    public void saveQuesTypeName(List<PaperDetailUnit> detailUnitList) {

        for (PaperDetailUnit paperDetailUnit : detailUnitList) {
            String orgId = paperDetailUnit.getPaper().getOrgId();
            String courseNo = paperDetailUnit.getPaper().getCourseNo();
            QuesStructType questionType = paperDetailUnit.getQuestionType();
            String detailName = paperDetailUnit.getPaperDetail().getName();
            List<QuesTypeName> quesTypeNames = quesTypeRepo.findQuesName(orgId, courseNo, questionType);
            if (quesTypeNames != null && quesTypeNames.size() > 0) {
                QuesTypeName quesTypeName = quesTypeNames.get(0);
                List<String> quesNames = quesTypeName.getQuesNames();
                if (quesNames != null && quesNames.size() > 0) {
                    if (quesNames.contains(detailName)) {
                        continue;
                    } else {
                        quesNames.add(detailName);
                    }
                } else {
                    quesNames = new ArrayList<>();
                    quesNames.add(detailName);
                }
                quesTypeName.setQuesNames(quesNames);
                quesTypeRepo.save(quesTypeName);
            } else {
                QuesTypeName quesTypeName = new QuesTypeName();
                List<String> quesNames = new ArrayList<>();
                quesNames.add(detailName);
                quesTypeName.setOrgId(orgId);
                quesTypeName.setCourseNo(courseNo);
                quesTypeName.setQuestionType(questionType);
                quesTypeName.setQuesNames(quesNames);
                quesTypeRepo.save(quesTypeName);
            }
        }
    }

    /**
     * 复制试卷时保存来源大题名称
     *
     * @param detailUnitList
     */
    public void saveCloneQuesTypeName(List<PaperDetailUnit> detailUnitList, String courseNo) {
        for (PaperDetailUnit paperDetailUnit : detailUnitList) {
            String orgId = paperDetailUnit.getPaper().getOrgId();
            QuesStructType questionType = paperDetailUnit.getQuestionType();
            String detailName = paperDetailUnit.getPaperDetail().getName();
            List<QuesTypeName> quesTypeNames = quesTypeRepo.findQuesName(orgId, courseNo, questionType);
            if (quesTypeNames != null && quesTypeNames.size() > 0) {
                QuesTypeName quesTypeName = quesTypeNames.get(0);
                List<String> quesNames = quesTypeName.getQuesNames();
                if (quesNames != null && quesNames.size() > 0) {
                    if (quesNames.contains(detailName)) {
                        continue;
                    } else {
                        quesNames.add(detailName);
                    }
                } else {
                    quesNames = new ArrayList<>();
                    quesNames.add(detailName);
                }
                quesTypeName.setQuesNames(quesNames);
                quesTypeRepo.save(quesTypeName);
            } else {
                QuesTypeName quesTypeName = new QuesTypeName();
                List<String> quesNames = new ArrayList<>();
                quesNames.add(detailName);
                quesTypeName.setOrgId(orgId);
                quesTypeName.setCourseNo(courseNo);
                quesTypeName.setQuestionType(questionType);
                quesTypeName.setQuesNames(quesNames);
                quesTypeRepo.save(quesTypeName);
            }
        }
    }
}
