package com.dsideal.FengHuang.Exam.Model; import cn.hutool.core.date.DateTime; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ExamModel { /** * 功能:增加考试人员 * * @param person_name * @param ks * @param tel */ public boolean addPerson(String person_name, String ks, String tel) { //1、检查此人员是不是已经存在过 String sql = "select count(1) as count from t_exam_person where person_name=? and tel=? and b_use=1"; int cnt = Db.findFirst(sql, person_name, tel).getInt("count"); if (cnt > 0) { return false; } //2、增加人员信息 Record record = new Record(); record.set("person_name", person_name); record.set("ks", ks); record.set("tel", tel); record.set("start_time", DateTime.now()); Db.save("t_exam_person", "person_id", record); int person_id = record.getInt("person_id"); //3、创建当前人员的试题 // 目前的思路是单选题,判断题,多选题的顺序来进行题目的随机化处理 sql = "select type_id from t_exam_question group by type_id order by type_id"; List listTypeCnt = Db.find(sql); int order_id = 0; for (Record rType : listTypeCnt) { int type_id = rType.getInt("type_id"); sql = "select question_id,type_id,answer,score from t_exam_question where type_id=? order by question_id"; List tmpList = Db.find(sql, type_id); // 调用shuffle方法进行随机化打乱 Collections.shuffle(tmpList); //批量存 List writeList = new ArrayList<>(); for (Record r1 : tmpList) { Record rExam = new Record(); rExam.set("person_id", person_id); rExam.set("order_id", ++order_id); rExam.set("question_id", r1.getInt("question_id")); rExam.set("type_id", r1.getInt("type_id")); rExam.set("answer", r1.getStr("answer")); rExam.set("score", r1.getInt("score")); writeList.add(rExam); } Db.batchSave("t_exam_record", writeList, 100); } return true; } }