package com.dsideal.FengHuang.Exam.Model; import cn.hutool.core.date.DateTime; import com.jfinal.aop.Before; import com.jfinal.ext.interceptor.GET; import com.jfinal.ext.interceptor.POST; import com.jfinal.kit.Kv; import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Page; import com.jfinal.plugin.activerecord.Record; import com.jfinal.plugin.activerecord.SqlPara; import java.sql.Time; import java.time.Duration; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ExamModel { /** * 功能:增加考试人员 * * @param person_name * @param ks * @param tel */ public Kv addPerson(String person_name, String ks, String tel) { Kv kv = Kv.create(); //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) { kv.set("success", false); kv.set("message", "当前人员已经进行过测试,不能重复进行!"); return kv; } //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); } kv.set("success", true); kv.set("message", "创建成功!"); kv.set("person_id", person_id); return kv; } /** * 功能:删除人员 * * @param person_id */ public void delPerson(String person_id) { String sql = "update t_exam_person set b_use=0 where person_id=?"; Db.update(sql, person_id); sql = "update t_exam_record set b_use=0 where person_id=?"; Db.update(sql, person_id); } /** * 功能:保存答题结果 * * @param order_id * @param reply */ public void save(String person_id, int order_id, String reply) { String sql = "update t_exam_record set reply=?,update_time=now() where person_id=? and order_id=?"; Db.update(sql, reply, person_id, order_id); } /** * 功能:交卷 */ public void jiaoJuan(String person_id) { String sql = "update t_exam_person set end_time=now() where person_id=?"; Db.update(sql, person_id); } /** * 功能:获取整体信息,比如有几种题型,都是啥,每种题型有几道题 */ public List getExamInfo() { String sql = "select t1.type_id,t2.type_name,count(1) as count from t_exam_question as t1 inner join t_exam_question_type as t2 on t1.type_id=t2.type_id group by t1.type_id,t2.type_name"; List list = Db.find(sql); return list; } /** * 功能:获取指定题目的信息 * * @param order_id */ public Record getQuestion(String person_id, int order_id) { String sql = "select question_id from t_exam_record where person_id=? and order_id=?"; int question_id = Db.findFirst(sql, person_id, order_id).getInt("question_id"); sql = "select question_id,type_id,content,A,B,C,D,E,F,G,score from t_exam_question where question_id=?"; Record record = Db.findFirst(sql, question_id); if (StrKit.isBlank(record.getStr("C"))) record.remove("C"); if (StrKit.isBlank(record.getStr("D"))) record.remove("D"); if (StrKit.isBlank(record.getStr("E"))) record.remove("E"); if (StrKit.isBlank(record.getStr("F"))) record.remove("F"); if (StrKit.isBlank(record.getStr("G"))) record.remove("G"); return record; } /** * 功能:分页获取人员的答题得分情况 * * @param page * @param limit */ public Page getPageSummary(int page, int limit) { SqlPara sqlPara = Db.getSqlPara("Exam.getPageSummary"); Page pageRecord = Db.paginate(page, limit, sqlPara); return pageRecord; } /** * 功能:查看自己答卷的整体情况,哪个题目答了,哪个题目没有答 */ public List getPersonAllInfo(String person_id) { String sql = "select * from t_exam_record where person_id=?"; return Db.find(sql, person_id); } /** * 功能:获取指定题目的信息(交卷后可见)+答案 * * @param order_id */ public Record getQuestionAfterJiaoJuan(String person_id, int order_id) { String sql = "select question_id from t_exam_record where person_id=? and order_id=?"; int question_id = Db.findFirst(sql, person_id, order_id).getInt("question_id"); return Db.findById("t_exam_question", "question_id", question_id); } /** * 功能:查看自己答卷的整体情况,哪个题目答对,哪个题目没有答,哪些答错(交卷后可见) */ public List getPersonAllInfoAfterJiaoJuan(String person_id) { Kv kv = Kv.by("person_id", person_id); SqlPara sqlPara = Db.getSqlPara("Exam.getPersonAllInfoAfterJiaoJuan", kv); return Db.find(sqlPara); } /** * 功能:获取开始时间,剩余时长,单位秒 */ public long getRemainSecond(String person_id) { //规定时长 int minutes = Integer.parseInt(getConfig(1).getStr("config_value")); //当前人员的开始时间 LocalDateTime start_time = Db.findById("t_exam_person", "person_id", person_id).getLocalDateTime("start_time"); //当前时间 LocalDateTime currentDateTime = LocalDateTime.now(); // 计算时间差 Duration duration = Duration.between(start_time, currentDateTime); long seconds = duration.getSeconds(); // 获取时间差秒数 if (seconds > minutes * 60) return 0; return minutes * 60 - seconds; } public Record getConfig(int config_id) { return Db.findById("t_exam_config", "config_id", config_id); } }