You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
8.1 KiB

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<Record> 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<Record> tmpList = Db.find(sql, type_id);
// 调用shuffle方法进行随机化打乱
Collections.shuffle(tmpList);
//批量存
List<Record> 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<Record> 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<Record> 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 t1.question_id,t1.type_id,t1.content,t1.A,t1.B,t1.C,t1.D,t1.E,t1.F,t1.G,t1.score,t2.type_name from t_exam_question as t1 inner join t_exam_question_type as t2 on t1.type_id=t2.type_id where t1.question_id=?";
Record record = Db.findFirst(sql, question_id);
Kv kv = Kv.create();
kv.set("A", record.getStr("A"));
kv.set("B", record.getStr("B"));
if (!StrKit.isBlank(record.getStr("C"))) kv.set("C", record.getStr("C"));
if (!StrKit.isBlank(record.getStr("D"))) kv.set("D", record.getStr("D"));
if (!StrKit.isBlank(record.getStr("E"))) kv.set("E", record.getStr("E"));
if (!StrKit.isBlank(record.getStr("F"))) kv.set("F", record.getStr("F"));
if (!StrKit.isBlank(record.getStr("G"))) kv.set("G", record.getStr("G"));
record.remove("A");
record.remove("B");
record.remove("C");
record.remove("D");
record.remove("E");
record.remove("F");
record.remove("G");
record.set("xuanxiang",kv);
return record;
}
/**
* 功能:分页获取人员的答题得分情况
*
* @param page
* @param limit
*/
public Page<Record> getPageSummary(int page, int limit) {
SqlPara sqlPara = Db.getSqlPara("Exam.getPageSummary");
Page<Record> pageRecord = Db.paginate(page, limit, sqlPara);
return pageRecord;
}
/**
* 功能:查看自己答卷的整体情况,哪个题目答了,哪个题目没有答
*/
public List<Record> 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<Record> 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);
}
}