package com.dsideal.FengHuang.Exam.Controller; import com.alibaba.fastjson.JSONObject; import com.dsideal.FengHuang.Exam.Model.ExamModel; import com.dsideal.FengHuang.Interceptor.EmptyInterface; import com.dsideal.FengHuang.Interceptor.IsNumericInterface; import com.dsideal.FengHuang.Util.CommonUtil; import com.dsideal.FengHuang.Util.CookieUtil; import com.dsideal.FengHuang.Util.ExcelCommonUtil; import com.dsideal.FengHuang.Util.FileUtil; import com.jfinal.aop.Before; import com.jfinal.core.Controller; import com.jfinal.ext.interceptor.GET; import com.jfinal.ext.interceptor.POST; import com.jfinal.kit.Kv; import com.jfinal.kit.PathKit; import com.jfinal.kit.PropKit; import com.jfinal.plugin.activerecord.Page; import com.jfinal.plugin.activerecord.Record; import java.io.File; import java.util.List; import java.util.UUID; public class ExamController extends Controller { //实例化model ExamModel em = new ExamModel(); /** * 功能:增加考试人员 * * @param person_name * @param ks * @param tel */ @Before({POST.class}) @EmptyInterface({"person_name","ks","tel"}) public void addPerson(String person_name, String ks, String tel) { Kv kv = em.addPerson(person_name, ks, tel); if (kv.getBoolean("success")) { CookieUtil.set(getResponse(), "person_id", kv.get("person_id").toString(), false, true); } renderJson(kv); } /** * 功能:获取整体信息,比如有几种题型,都是啥,每种题型有几道题 */ @Before({GET.class}) public void getExamInfo() { List list = em.getExamInfo(); Kv kv = Kv.create(); kv.set("list", list); int allCount = 0; for (Record record : list) { allCount += record.getInt("count"); } kv.set("allCount", allCount); renderJson(kv); } /** * 功能:获取剩余时长,单位秒 */ @Before({GET.class}) public void getRemainSecond() { String person_id = getCookie("person_id"); long remainSecond = em.getRemainSecond(person_id); Kv kv = Kv.by("success", true); kv.set("remainSecond", remainSecond); renderJson(kv); } /** * 功能:获取指定题目的信息 * * @param order_id */ @Before({GET.class}) @IsNumericInterface({"order_id"}) public void getQuestion(int order_id, int finish) { String person_id = getCookie("person_id"); if (finish == 1 && em.HaveJiaoJuan(person_id) <= 0) { Kv kv = Kv.by("success", false); kv.set("message", "你还没有交卷,不能使用此接口!"); renderJson(kv); return; } Record record = em.getQuestion(person_id, order_id, finish); renderJson(record); } /** * 功能:保存答题结果 * * @param order_id * @param reply */ @Before({POST.class}) @EmptyInterface({"reply"}) @IsNumericInterface({"order_id"}) public void save(int order_id, String reply) { String person_id = getCookie("person_id"); int result = em.save(person_id, order_id, reply); if (result == -1) { Kv kv = Kv.by("success", false); kv.put("message", "交卷后不能再保存数据!"); renderJson(kv); } else { Kv kv = Kv.by("success", true); kv.put("message", "保存成功!"); renderJson(kv); } } /** * 功能:交卷 */ @Before({POST.class}) public void jiaoJuan() { String person_id = getCookie("person_id"); em.jiaoJuan(person_id); Kv kv = Kv.by("success", true); kv.put("message", "保存成功!"); renderJson(kv); } /** * 功能:查看自己答卷的整体情况,哪个题目答了,哪个题目没有答 * reply为 null或空串表示未答题 */ @Before({GET.class}) public void getPersonAllInfo() { String person_id = getCookie("person_id"); List list = em.getPersonAllInfo(person_id); renderJson(list); } /** * 功能:查看自己答卷的整体情况,哪个题目答了,哪个题目没有答 */ @Before({GET.class}) public void getPersonUnFinishCount() { String person_id = getCookie("person_id"); int count = em.getPersonUnFinishCount(person_id); Kv kv = Kv.create(); kv.set("success", true); kv.set("count", count); renderJson(kv); } /** * 功能:查看自己答卷的整体情况,哪个题目答了,哪个题目没有答(交卷后可见) */ @Before({GET.class}) public void getPersonAllInfoAfterJiaoJuan() { String person_id = getCookie("person_id"); if (em.HaveJiaoJuan(person_id) <= 0) { Kv kv = Kv.by("success", false); kv.set("message", "你还没有交卷,不能使用此接口!"); renderJson(kv); return; } Kv kv = em.getPersonAllInfoAfterJiaoJuan(person_id); renderJson(kv); } /********************************上面为答题人员的接口************************************/ /********************************下面为管理人员的接口************************************/ /** * 功能:删除考试人员 * * @param person_id */ @Before({POST.class}) @EmptyInterface({"person_id"}) public void delPerson(String person_id) { em.delPerson(person_id); Kv kv = Kv.by("success", true); kv.put("message", "删除成功!"); renderJson(kv); } /** * 功能:分页获取人员的答题得分情况 * * @param page * @param limit */ @Before({GET.class}) @IsNumericInterface({"page", "limit"}) public void getPageSummary(int page, int limit) { Page list = em.getPageSummary(page, limit); renderJson(CommonUtil.renderJsonForLayUI(list)); } /** * 功能:导出EXCEL */ @Before({GET.class}) public void exportExcel() { Page list = em.getPageSummary(1, 10000); //模板文件 String excelPath = PathKit.getRootClassPath() + PropKit.get("excelExportTemplatePathSuffix").replace("\\", "/"); String filePath = excelPath + "exam.json"; //转成 json对象 JSONObject jo = FileUtil.readJsonFile(filePath); //导出 String excelFile = excelPath + "excelTemp/" + UUID.randomUUID().toString().toUpperCase() + ".xls"; ExcelCommonUtil.export(list, jo, excelFile,null,"名次"); //提供下载 String filename = "排名结果.xls"; renderFile(new File(excelFile), filename); } }