小么熊🐻 %!s(int64=2) %!d(string=hai) anos
pai
achega
e4862ffa2a

+ 9 - 2
nbjk-admin/src/test/java/com/miaxis/test/NormalTest3.java

@@ -31,10 +31,17 @@ public class NormalTest3 {
         questionInfoTestDTO.setSubject(4);
         questionInfoTestDTO.setGs("hc");
 
-        List<QuestionInfo> list = questionInfoService.selectTestK14QuestionInfoList2(questionInfoTestDTO);
-        System.out.println(list.size());
+        for (int i = 0; i < 10; i++) {
+            List<QuestionInfo> list = questionInfoService.selectTestK14QuestionInfoList(questionInfoTestDTO);
+            Thread.sleep(2000);
+            System.out.println(i+"------------------------------------------------------------------------------------------------------");
+        }
+
 
     }
 
 
+
+
+
 }

+ 15 - 0
nbjk-admin/src/test/java/com/miaxis/test/Test.java

@@ -0,0 +1,15 @@
+package com.miaxis.test;
+
+public class Test {
+
+    public static void main(String[] args) {
+
+        for (int i = 0; i < 10; i++) {
+            if(i==5) {
+                continue;
+            }
+            System.out.println("i="+i);
+        }
+    }
+
+}

+ 88 - 40
nbjk-service/src/main/java/com/miaxis/question/service/impl/QuestionInfoServiceImpl.java

@@ -422,58 +422,106 @@ public class QuestionInfoServiceImpl extends ServiceImpl<QuestionInfoMapper, Que
         int total = 0;
         StringBuffer sql = new StringBuffer();
         sql.append("SELECT * FROM (");
-        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(); //多选
 
-            total = total + easyJudge + easyChoice + easyMultiple;
+        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.getLiceTruck()+"+"+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++) {
+                    randomNum = random.nextInt(9);
+
+                    if(qt.getSubject()==1 || (qt.getSubject()==4 && diffMultiple==0)) {
+                        if (randomNum <= 2) {   //判断题
+                            diffJudeg++;
+                            easyJudge--;
+                        } else if (randomNum <= 5) {  //单选题
+                            diffChoice++;
+                            easyChoice--;
+                        } else if (randomNum <= 8) {    //多选题
+                            diffMultiple++;
+                            easyMultiple--;
+                        }
+                    } else if(qt.getSubject()==4){
+                        if (randomNum <= 4) {   //判断题
+                            diffJudeg++;
+                            easyJudge--;
+                        } else if (randomNum <= 9) {  //单选题
+                            diffChoice++;
+                            easyChoice--;
+                        }
+                    }
 
-            int difficult = er.getDifficultCount(); //难题数
-            int chapterId = er.getChapterId();
 
-            Random random = new Random();
-            int diffJudeg = 0; //判断题
-            int diffChoice = 0; //单选题
-            int diffMultiple = 0; //多选题
-            for (int j = 0; j < difficult; j++) {
-                if (random.nextInt(9) <= 2) {   //判断题
-                        diffJudeg++;
-                        easyJudge--;
-                } else if (random.nextInt(9) <= 5) {  //单选题
-                        diffChoice++;
-                        easyChoice--;
-                } else if (random.nextInt(9) <= 8) {    //多选题
-                        diffMultiple++;
-                        easyMultiple--;
                 }
-            }
 
-            if(i!=0){
+                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 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 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 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 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 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(" (SELECT * FROM question_info 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 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(" (SELECT * FROM question_info 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 WHERE chapter_id = " + chapterId + " AND subject = "+qt.getSubject()+" AND question_type = '3' AND diff_degree in (4,5) ORDER BY RAND() LIMIT "+ diffMultiple + ") ");
+                sql.append(" (SELECT * FROM question_info 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 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 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 = questionInfoMapper.selectTestK14QuestionInfoList(sql.toString());
-        if(allList.size()==total) {
+        if (allList.size() == total) {
             return allList;
         } else {
             new CustomException("模拟考试取题出错,请重试。");