package com.miaxis.question.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.common.exception.CustomException;
import com.miaxis.question.domain.QuestionInfo;
import com.miaxis.question.domain.QuestionInfoKtNew;
import com.miaxis.question.domain.TAppExamRule;
import com.miaxis.question.dto.*;
import com.miaxis.question.mapper.QuestionInfoKtNewMapper;
import com.miaxis.question.service.IQuestionInfoKtNewService;
import com.miaxis.question.service.ITAppExamRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 获取快通题库Service业务层处理
 *
 * @author miaxis
 * @date 2023-11-03
 */
@Service
public class QuestionInfoKtNewServiceImpl extends ServiceImpl<QuestionInfoKtNewMapper, QuestionInfoKtNew> implements IQuestionInfoKtNewService {
    @Autowired
    private QuestionInfoKtNewMapper questionInfoKtNewMapper;


    @Autowired
    private ITAppExamRuleService appExamRuleService;

    /**
     * 查询获取快通题库列表
     *
     * @param questionInfoKtNew 获取快通题库
     * @return 获取快通题库
     */
    @Override
    public List<QuestionInfoKtNew> selectQuestionInfoKtNewList(QuestionInfoKtNew questionInfoKtNew){
        return questionInfoKtNewMapper.selectQuestionInfoKtNewList(questionInfoKtNew);
    }

    @Override
    public int selectQuestionInfoKtNewCount(QuestionInfoKtNew questionInfoKtNew) {
        return questionInfoKtNewMapper.selectQuestionInfoKtNewCount(questionInfoKtNew);
    }

    @Override
    public List<QuestionInfoKtNew> selectTestK14QuestionInfoList(QuestionInfoNewTestDTO qt) {
        String cx = null;

        if ("mtc".equals(qt.getModel())) {
            cx = "mtc";
        } else if ("cart".equals(qt.getModel())) {
            cx = "xc";
        } else if ("truck".equals(qt.getModel())) {
            cx = "hc";
        } else if ("kc".equals(qt.getModel())) {
            cx = "bus";
        }



        //全题库
        List<QuestionInfoKtNew> allList = new ArrayList<QuestionInfoKtNew>();
        TAppExamRule tAppExamRule = new TAppExamRule();
        tAppExamRule.setKemu(qt.getSubject());
        tAppExamRule.setGs(cx);
        tAppExamRule.setAreacode(0);

        List<TAppExamRule> list = appExamRuleService.selectTAppExamRuleList(tAppExamRule);
        if (list.size() == 0) {
            return allList;
        }
        int total = 0;
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM (");

        boolean shouldRetry = false;
        do {
            shouldRetry = false;
            for (int i = 0; i < list.size(); i++) {
                TAppExamRule er = list.get(i);
                int easyJudge = er.getJudgeCount(); //判断
                int easyChoice = er.getChoiceCount(); //选择
                int easyMultiple = er.getMultipleChoiceCount(); //多选

                System.out.println("i="+i);
                System.out.println(qt.getModel()+"+"+qt.getSubject());
                System.out.println("判断题："+easyJudge);
                System.out.println("选择题："+easyChoice);
                System.out.println("多选题："+easyMultiple);

                total = total + easyJudge + easyChoice + easyMultiple;

                int difficult = er.getDifficultCount(); //难题数
                int chapterId = er.getChapterId();

                Random random = new Random();
                int diffJudeg = 0; //判断题
                int diffChoice = 0; //单选题
                int diffMultiple = 0; //多选题

                int randomNum = 0;
                for (int j = 0; j < difficult; j++) {


                    if(qt.getSubject()==1 || (qt.getSubject()==4 && easyMultiple==0)) {
                        randomNum = random.nextInt(2); //随机出现 0和1  0表示判断 1表示单选
                        if (randomNum == 0) {   //判断题
                            diffJudeg++;
                            easyJudge--;
                        } else if (randomNum == 1) {  //单选题
                            diffChoice++;
                            easyChoice--;
                        }
                    } else if(qt.getSubject()==4){
                        randomNum = random.nextInt(3); //随机出现 0、1、2  0表示判断 1表示单选 2多选
                        if (randomNum == 0) {   //判断题
                            diffJudeg++;
                            easyJudge--;
                        } else if (randomNum == 1) {  //单选题
                            diffChoice++;
                            easyChoice--;
                        } else if (randomNum == 2) {    //多选题
                            diffMultiple++;
                            easyMultiple--;
                        }

                    }


                }

                if(qt.getSubject()==1) {
                    if (diffJudeg < 0 || easyJudge < 0 || diffChoice < 0 || easyChoice < 0) {
                        shouldRetry = true;
                        sql.setLength(0);  //清空还原
                        sql.append("SELECT * FROM (");
                        break;
                    }
                }
                // 检查变量值是否小于0
                if(qt.getSubject()==4) {
                    if (diffJudeg < 0 || easyJudge < 0 || diffChoice < 0 || easyChoice < 0 || diffMultiple < 0 || easyMultiple < 0) {
                        shouldRetry = true;
                        sql.setLength(0);  //清空还原
                        sql.append("SELECT * FROM (");
                        break;
                    }
                }


                if (i != 0) {
                    sql.append(" union ");
                }
                //判断题
                sql.append(" (SELECT * FROM question_info_kt_new WHERE chapter_id = " + chapterId + " AND subject = " + qt.getSubject() + " AND question_type = '1' AND diff_degree in (1,2,3) ORDER BY RAND() LIMIT " + easyJudge + ") ");
                sql.append(" union ");
                sql.append(" (SELECT * FROM question_info_kt_new WHERE chapter_id = " + chapterId + " AND subject = " + qt.getSubject() + " AND question_type = '1' AND diff_degree in (4,5) ORDER BY RAND() LIMIT " + diffJudeg + ") ");
                sql.append(" union ");
                //选择题
                sql.append(" (SELECT * FROM question_info_kt_new WHERE chapter_id = " + chapterId + " AND subject = " + qt.getSubject() + " AND question_type = '2' AND diff_degree in (1,2,3) ORDER BY RAND() LIMIT " + easyChoice + ") ");
                sql.append(" union ");
                sql.append(" (SELECT * FROM question_info_kt_new WHERE chapter_id = " + chapterId + " AND subject = " + qt.getSubject() + " AND question_type = '2' AND diff_degree in (4,5) ORDER BY RAND() LIMIT " + diffChoice + ") ");
                //多选题
                if (qt.getSubject() == 4) {
                    sql.append(" union ");
                    sql.append(" (SELECT * FROM question_info_kt_new WHERE chapter_id = " + chapterId + " AND subject = " + qt.getSubject() + " AND question_type = '3' AND diff_degree in (1,2,3) ORDER BY RAND() LIMIT " + easyMultiple + ") ");
                    sql.append(" union ");
                    sql.append(" (SELECT * FROM question_info_kt_new WHERE chapter_id = " + chapterId + " AND subject = " + qt.getSubject() + " AND question_type = '3' AND diff_degree in (4,5) ORDER BY RAND() LIMIT " + diffMultiple + ") ");
                }
            }

        } while (shouldRetry);

        sql.append(" ) as db");
        System.out.println(sql.toString());
        allList = questionInfoKtNewMapper.selectTestK14QuestionInfoList(sql.toString());
        if (allList.size() == total) {
            return allList;
        } else {
            new CustomException("模拟考试取题出错，请重试。");
            return null;
        }
    }

    @Override
    public List<QuestionInfoKtNew> selectFreeQuestionInfo(QuestionInfoFreeDTO freeDTO) {
        List<QuestionInfoKtNew> list = questionInfoKtNewMapper.selectFreeQuestionInfoList(freeDTO);
        return list;
    }

    @Override
    public List<QuestionInfoKtNew> selectQuestionInfoList(QuestionInfoNewDTO questionInfoNewDTO) {
        return questionInfoKtNewMapper.selectQuestionInfoList(questionInfoNewDTO);
    }

    @Override
    public List<QuestionInfoKtNew> selectNormalQuestionInfoList(QuestionInfoNewDTO questionInfoNewDTO) {
        return questionInfoKtNewMapper.selectNormalQuestionInfoList(questionInfoNewDTO);
    }

    @Override
    public List<QuestionInfoKtNew> selectQuestionInfoListByIds(Long[] ids) {
        return questionInfoKtNewMapper.selectQuestionInfoListByIds(ids);
    }


}
