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.
321 lines
12 KiB
321 lines
12 KiB
package com.dsideal.FengHuang.Exam.Model;
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
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.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 and end_time is not null";
|
|
int cnt = Db.findFirst(sql, person_name, tel).getInt("count");
|
|
if (cnt > 0) {
|
|
kv.set("success", false);
|
|
kv.set("message", "当前人员已经进行过测试并且已交卷,不能重复进行!");
|
|
return kv;
|
|
}
|
|
|
|
sql = "select * from t_exam_person where person_name=? and tel=? and b_use=1";
|
|
List<Record> list = Db.find(sql, person_name, tel);
|
|
if (list.size() > 0) {
|
|
kv.set("person_id",list.get(0).getInt("person_id"));
|
|
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 person_id
|
|
* @return
|
|
*/
|
|
public int HaveJiaoJuan(String person_id) {
|
|
String sql = "select end_time from t_exam_person where person_id=?";
|
|
List<Record> list = Db.find(sql, person_id);
|
|
if (list.size() == 0) return -1;
|
|
Record record = list.get(0);
|
|
String end_time = record.getStr("end_time");
|
|
if (StrKit.isBlank(end_time)) return 0;
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* 功能:保存答题结果
|
|
*
|
|
* @param order_id
|
|
* @param reply
|
|
*/
|
|
public int save(String person_id, int order_id, String reply) {
|
|
if (HaveJiaoJuan(person_id) != 0) return -1;
|
|
if (StrKit.isBlank(reply)) reply = null;
|
|
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);
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* 功能:交卷
|
|
*/
|
|
public void jiaoJuan(String person_id) {
|
|
String sql = "update t_exam_person set end_time=? where person_id=?";
|
|
Db.update(sql, DateTime.now(), 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, int finish) {
|
|
String sql = "select question_id,reply from t_exam_record where person_id=? and order_id=?";
|
|
Record r = Db.findFirst(sql, person_id, order_id);
|
|
int question_id = r.getInt("question_id");
|
|
String reply = r.getStr("reply");
|
|
|
|
sql = "select t1.question_id,t1.type_id,t1.content,t1.answer,t1.memo,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);
|
|
|
|
JSONArray array = new JSONArray();
|
|
for (char x = 'A'; x <= 'F'; x++) {
|
|
if (!StrKit.isBlank(record.getStr(String.valueOf(x)))) {
|
|
JSONObject jo = new JSONObject();
|
|
jo.put("key", String.valueOf(x));
|
|
jo.put("value", record.getStr(String.valueOf(x)));
|
|
boolean flag = true;
|
|
if (StrKit.isBlank(reply)) flag = false;
|
|
else if (reply.indexOf(x) < 0) flag = false;
|
|
|
|
jo.put("checked", flag);
|
|
array.add(jo);
|
|
}
|
|
}
|
|
record.remove("A");
|
|
record.remove("B");
|
|
record.remove("C");
|
|
record.remove("D");
|
|
record.remove("E");
|
|
record.remove("F");
|
|
record.remove("G");
|
|
|
|
//是否正确的标识
|
|
String answer = record.getStr("answer");
|
|
if (StrKit.isBlank(reply)) record.set("flag", 0);
|
|
else if (answer.equals(reply)) {
|
|
record.set("flag", 1);
|
|
} else record.set("flag", 2);
|
|
|
|
//是不是交卷前查阅
|
|
if (finish == 0) {
|
|
record.remove("answer");
|
|
record.remove("memo");
|
|
}
|
|
//选项
|
|
record.set("xuanxiang", array);
|
|
//题号
|
|
record.set("order_id", order_id);
|
|
//作答
|
|
record.set("reply", reply);
|
|
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);
|
|
//1、扩展排名序号
|
|
for (int i = 0; i < pageRecord.getList().size(); i++) {
|
|
Record record = pageRecord.getList().get(i);
|
|
record.set("num", (page - 1) * limit + i + 1);
|
|
LocalDateTime st=record.getLocalDateTime("start_time");
|
|
LocalDateTime ed=record.getLocalDateTime("end_time");
|
|
//2、扩展答题时长
|
|
record.set("ys",getYs(st,ed));
|
|
record.set("start_time",st.toLocalDate()+" "+st.toLocalTime());
|
|
record.set("end_time",st.toLocalDate()+" "+st.toLocalTime());
|
|
}
|
|
return pageRecord;
|
|
}
|
|
|
|
/**
|
|
* 功能:查看自己答卷的整体情况,哪个题目答了,哪个题目没有答
|
|
*/
|
|
public List<Record> getPersonAllInfo(String person_id) {
|
|
String sql = "select *,if(reply is null,0,1) as flag from t_exam_record where person_id=?";
|
|
return Db.find(sql, person_id);
|
|
}
|
|
|
|
/**
|
|
* 功能:查看自己答卷的整体情况,哪个题目答了,哪个题目没有答
|
|
*/
|
|
public int getPersonUnFinishCount(String person_id) {
|
|
String sql = "select count(1) as count from t_exam_record where person_id=? and reply is null";
|
|
return Db.findFirst(sql, person_id).getInt("count");
|
|
}
|
|
|
|
|
|
/**
|
|
* 功能:查看自己答卷的整体情况,哪个题目答对,哪个题目没有答,哪些答错(交卷后可见)
|
|
*/
|
|
public Kv getPersonAllInfoAfterJiaoJuan(String person_id) {
|
|
Kv res = Kv.create();
|
|
Kv kv = Kv.by("person_id", person_id);
|
|
SqlPara sqlPara = Db.getSqlPara("Exam.getPersonAllInfoAfterJiaoJuan", kv);
|
|
List<Record> list = Db.find(sqlPara);
|
|
int sum = 0;
|
|
for (Record record : list) {
|
|
String answer = record.getStr("answer");
|
|
String reply = record.getStr("reply");
|
|
if (StrKit.isBlank(reply)) record.set("flag", 0);
|
|
else if (answer.equals(reply)) {
|
|
sum += record.getInt("score");
|
|
record.set("flag", 1);
|
|
} else record.set("flag", 2);
|
|
}
|
|
res.set("list", list);
|
|
res.set("sum", sum);
|
|
//开始结束时间,用时
|
|
String sql = "select * from t_exam_person where person_id=?";
|
|
Record r = Db.findFirst(sql, person_id);
|
|
LocalDateTime start_time = r.getLocalDateTime("start_time");
|
|
LocalDateTime end_time = r.getLocalDateTime("end_time");
|
|
res.set("start_time", start_time);
|
|
res.set("end_time", end_time);
|
|
res.set("ys", getYs(start_time, end_time));
|
|
return res;
|
|
}
|
|
|
|
public String getYs(LocalDateTime start_time, LocalDateTime end_time) {
|
|
// 计算时间差
|
|
Duration duration = Duration.between(start_time, end_time);
|
|
long seconds = duration.getSeconds(); // 获取时间差秒数
|
|
long hour = seconds / 3600;
|
|
long minute = (seconds - hour * 3600) / 60;
|
|
long second = seconds - hour * 3600 - minute * 60;
|
|
|
|
String ys = "";
|
|
if (hour > 0) ys += hour + "小时";
|
|
if (hour == 0 && minute > 0) ys += minute + "分钟";
|
|
if (hour > 0) ys += minute + "分钟";
|
|
if (second > 0) ys += second + "秒";
|
|
return ys;
|
|
}
|
|
|
|
/**
|
|
* 功能:获取开始时间,剩余时长,单位秒
|
|
*/
|
|
public long getRemainSecond(String person_id) {
|
|
String sql = "select end_time from t_exam_person where person_id=?";
|
|
List<Record> list = Db.find(sql, person_id);
|
|
if (list.size() == 0) return -2;//没找到
|
|
String end_time = list.get(0).getStr("end_time");
|
|
if (!StrKit.isBlank(end_time)) return -1;//已交卷
|
|
|
|
//规定时长
|
|
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);
|
|
}
|
|
}
|