123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- package restcontroller.webmanage.saletool.courseware;
- import beans.attachment.Attachment;
- import beans.department.Department;
- import beans.salearea.SaleArea;
- import beans.time.Time;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.google.gson.JsonArray;
- import common.Controller;
- import common.YosException;
- import common.annotation.API;
- import common.annotation.CACHEING;
- import common.data.*;
- import restcontroller.R;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- /**
- * 考试表头
- */
- public class coursewaretesthead extends Controller {
- String sat_courseware_testhead = "sat_courseware_testhead";
- public coursewaretesthead(JSONObject content) throws YosException {
- super(content);
- }
- @API(title = "考试表头新增或更新", apiversion = R.ID20240323112202.v1.class)
- public String insertOrUpdate() throws YosException {
- Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
- int testaddmode = content.getIntValue("testaddmode", 1);
- int num = content.getIntValue("num", 0);
- JSONArray sat_courseware_classids = new JSONArray();
- if (content.containsKey("sat_courseware_classids")) {
- sat_courseware_classids = content.getJSONArray("sat_courseware_classids");
- }
- ArrayList<Long> sat_courseware_testlibraryids = new ArrayList<>();
- //前端需要的逻辑处理是否跳过下方的验证
- boolean isJump = false;
- for (Object object : sat_courseware_classids) {
- if (object instanceof JSONArray) {
- JSONArray jsonArray = (JSONArray) object;
- for (Object o : jsonArray) {
- if (o.toString().contains("9999")) {
- isJump = true;
- }
- }
- }
- }
- if (testaddmode == 1 && !isJump) {
- if (num <= 0) {
- return getErrReturnObject().setErrMsg("出题方式为系统随机时题目设置数量需大于0").toString();
- }
- if (sat_courseware_classids.size() <= 0) {
- return getErrReturnObject().setErrMsg("请选择分类").toString();
- }
- Rows rows = CoursewareHelper.getTestlibraryWithCoursewareClassRows(this, sat_courseware_classids);
- if (num > rows.size()) {
- return getErrReturnObject().setErrMsg("当前分类下题目数量小于系统随机出题数").toString();
- }
- sat_courseware_testlibraryids = rows.toArrayList("sat_courseware_testlibraryid", new ArrayList<>());
- }
- if (sat_courseware_testheadid <= 0) {
- sat_courseware_testheadid = createTableID(sat_courseware_testhead);
- InsertSQL insertSQL = SQLFactory.createInsertSQL(this, sat_courseware_testhead);
- insertSQL.setSiteid(siteid);
- insertSQL.setUniqueid(sat_courseware_testheadid);
- insertSQL.setValue("title", content.getStringValue("title"));
- insertSQL.setValue("status", "新建");
- insertSQL.setValue("testaddmode", content.getIntValue("testaddmode", 1));
- insertSQL.setValue("passingscore", content.getBigDecimalValue("passingscore", 60));
- insertSQL.setValue("num", content.getIntValue("num", 0));
- insertSQL.setValue("sat_courseware_classids", sat_courseware_classids);
- insertSQL.insert();
- content.put("sat_courseware_testheadid", sat_courseware_testheadid);
- }
- if (sat_courseware_testheadid > 0) {
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
- updateSQL.setSiteid(siteid);
- updateSQL.setUniqueid(sat_courseware_testheadid);
- updateSQL.setValue("title", content.getStringValue("title"));
- updateSQL.setValue("testaddmode", content.getIntValue("testaddmode", 1));
- updateSQL.setValue("passingscore", content.getBigDecimalValue("passingscore", 60));
- updateSQL.setValue("num", content.getIntValue("num", 0));
- updateSQL.setValue("sat_courseware_classids", sat_courseware_classids);
- if (testaddmode == 1) {//系统随机
- updateSQL.setValue("testquestions", new JSONArray());
- }
- updateSQL.update();
- }
- if (testaddmode == 1) {////系统随机
- content.put("sat_courseware_testlibraryids", sat_courseware_testlibraryids);
- return addtestquestions();
- }
- return detail();
- }
- @API(title = "考试表头详情", apiversion = R.ID20240323112302.v1.class)
- public String detail() throws YosException {
- Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
- QuerySQL querySQL = SQLFactory.createQuerySQL(this, sat_courseware_testhead).setTableAlias("t1");
- querySQL.setSiteid(siteid);
- querySQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadid);
- Rows rows = querySQL.query();
- Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
- QuerySQL querySQL2 = SQLFactory.createQuerySQL(this, "sys_dataauths",
- "roleid");
- querySQL2.addJoinTable(JOINTYPE.inner, "sys_role", "t2", "t1.roleid=t2.roleid and t1.siteid=t2.siteid",
- "rolename");
- querySQL2.setTableAlias("t1");
- querySQL2.setWhere("t1.siteid", siteid);
- querySQL2.setWhere("t1.ownertable", "sat_courseware_testhead");
- querySQL2.setWhere("t1.ownerid", sat_courseware_testheadid);
- Rows roleRows = querySQL2.query();
- detailRow.put("role", roleRows.toArrayList("roleid", new ArrayList<Long>()));
- JSONArray jsonArray = detailRow.getJSONArray("testquestions");
- detailRow.put("testquestions", jsonArray);
- for (Object object : jsonArray) {
- JSONObject jsonObject = (JSONObject) object;
- if (jsonObject.getLong("type") == 1) {
- jsonObject.put("typestr", "选择题");
- }
- if (jsonObject.getLong("type") == 2) {
- jsonObject.put("typestr", "简答题");
- }
- if (jsonObject.getLong("typemx") == 0) {
- jsonObject.put("typemxstr", "简答");
- }
- if (jsonObject.getLong("typemx") == 1) {
- jsonObject.put("typemxstr", "单选");
- }
- if (jsonObject.getLong("typemx") == 2) {
- jsonObject.put("typemxstr", "多选");
- }
- }
- JSONArray sat_courseware_classids = detailRow.getJSONArray("sat_courseware_classids");
- if (sat_courseware_classids.size() == 0) {
- detailRow.put("classnames", "");
- } else {
- detailRow.put("classnames", CoursewareHelper.getClassnames(this, sat_courseware_classids));
- }
- if (detailRow.getLong("testaddmode") == 1) {
- detailRow.put("testaddmodestr", "系统随机");
- }
- if (detailRow.getLong("testaddmode") == 2) {
- detailRow.put("testaddmodestr", "自选题目");
- }
- return getSucReturnObject().setData(detailRow).toString();
- }
- @API(title = "考试表头删除", apiversion = R.ID20240323112402.v1.class)
- public String delete() throws YosException {
- JSONArray sat_courseware_testheadids = content.getJSONArray("sat_courseware_testheadids");
- DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, sat_courseware_testhead);
- deleteSQL.setSiteid(siteid);
- deleteSQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadids.toArray());
- deleteSQL.delete();
- return getSucReturnObject().toString();
- }
- @API(title = "考试表头发布", apiversion = R.ID20240323141002.v1.class)
- public String release() throws YosException {
- JSONArray sat_courseware_testheadids = content.getJSONArray("sat_courseware_testheadids");
- int status = content.getIntValue("status", 0);
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
- updateSQL.setSiteid(siteid);
- updateSQL.setValue("status", status == 0 ? "新建" : "发布");
- updateSQL.setValue("sendby", status == 0 ? "null" : username);
- updateSQL.setValue("senddate", status == 0 ? "null" : Time.getDateTime_Str());
- updateSQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadids.toArray());
- updateSQL.update();
- return getSucReturnObject().toString();
- }
- @API(title = "添加考试题目", apiversion = R.ID20240323141602.v1.class)
- public String addtestquestions() throws YosException {
- Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
- JSONArray sat_courseware_testlibraryids = content.getJSONArray("sat_courseware_testlibraryids");
- Rows testheadRows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testhead WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and siteid='" + siteid + "'");
- if (testheadRows.isEmpty()) {
- return getErrReturnObject().setErrMsg("数据不存在").toString();
- }
- if (sat_courseware_testlibraryids.size() <= 0) {
- return detail();
- }
- JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions");
- //移除考试题目
- testquestions.removeAll(CoursewareHelper.getRemoveArray(testquestions, sat_courseware_testlibraryids));
- //添加考试题目
- testquestions.addAll(CoursewareHelper.getTestlibraryRows(this, sat_courseware_testlibraryids));
- int testaddmode = testheadRows.get(0).getInteger("testaddmode");
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
- updateSQL.setUniqueid(sat_courseware_testheadid);
- updateSQL.setSiteid(siteid);
- updateSQL.setValue("testquestions", testquestions);
- if (testaddmode == 2) {
- updateSQL.setValue("num", testquestions.size());
- }
- updateSQL.update();
- return detail();
- }
- @API(title = "删除考试题目", apiversion = R.ID20240323155602.v1.class)
- public String deletetestquestions() throws YosException {
- Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
- JSONArray sat_courseware_testlibraryids = content.getJSONArray("sat_courseware_testlibraryids");
- Rows testheadRows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testhead WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and siteid='" + siteid + "'");
- if (testheadRows.isEmpty()) {
- return getErrReturnObject().setErrMsg("数据不存在").toString();
- }
- if (sat_courseware_testlibraryids.size() <= 0) {
- return getErrReturnObject().setErrMsg("请选择题目").toString();
- }
- JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions");
- //移除考试题目
- testquestions.removeAll(CoursewareHelper.getRemoveArray(testquestions, sat_courseware_testlibraryids));
- int testaddmode = testheadRows.get(0).getInteger("testaddmode");
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
- updateSQL.setUniqueid(sat_courseware_testheadid);
- updateSQL.setSiteid(siteid);
- updateSQL.setValue("testquestions", testquestions);
- if (testaddmode == 2) {
- updateSQL.setValue("num", testquestions.size());
- }
- updateSQL.update();
- return detail();
- }
- @API(title = "考试表头列表", apiversion = R.ID20240323112502.v1.class)
- public String list() throws YosException {
- StringBuffer where = new StringBuffer(" 1=1 ");
- if (content.containsKey("where")) {
- JSONObject whereObject = content.getJSONObject("where");
- if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
- where.append(" and (");
- where.append("t1.title like'%").append(whereObject.getString("condition")).append("%' ");
- // where.append("or t1.notes like'%").append(whereObject.getString("condition")).append("%' ");
- where.append(")");
- }
- if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
- where.append(" and (");
- where.append("t1.status ='").append(whereObject.getString("status")).append("' ");
- where.append(")");
- }
- if (whereObject.containsKey("testaddmode") && !"".equals(whereObject.getString("testaddmode"))) {
- where.append(" and (");
- where.append("t1.testaddmode ='").append(whereObject.getString("testaddmode")).append("' ");
- where.append(")");
- }
- if (whereObject.containsKey("sat_courseware_classids") && !"".equals(whereObject.getString("sat_courseware_classids"))) {
- JSONArray sat_courseware_classids = whereObject.getJSONArray("sat_courseware_classids");
- if (sat_courseware_classids.size() > 0) {
- where.append(" and (1=2");
- for (Object obj : sat_courseware_classids) {
- JSONArray array = (JSONArray) obj;
- for (Object obj2 : array) {
- where.append(" or (");
- where.append("JSON_CONTAINS(t1.sat_courseware_classids,'" + obj2 + "')");
- where.append(")");
- }
- }
- where.append(")");
- }
- }
- if (whereObject.containsKey("begindate_create") && !"".equals(whereObject.getString("begindate_create"))) {
- where.append(" and (");
- where.append("t1.createdate >='").append(whereObject.getString("begindate_create")).append("' ");
- where.append(")");
- }
- if (whereObject.containsKey("enddate_create") && !"".equals(whereObject.getString("enddate_create"))) {
- where.append(" and (");
- where.append("t1.createdate <='").append(whereObject.getString("enddate_create")).append(" 23:59:59' ");
- where.append(")");
- }
- if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
- where.append(" and (");
- where.append("t1.senddate >='").append(whereObject.getString("begindate")).append("' ");
- where.append(")");
- }
- if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
- where.append(" and (");
- where.append("t1.senddate <='").append(whereObject.getString("enddate")).append(" 23:59:59' ");
- where.append(")");
- }
- }
- QuerySQL querySQL = SQLFactory.createQuerySQL(this, sat_courseware_testhead)
- .setTableAlias("t1");
- querySQL.setSiteid(siteid);
- querySQL.setWhere(where.toString());
- querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
- Rows rows = querySQL.query();
- for (Row row : rows) {
- if (row.getLong("testaddmode") == 1) {
- row.put("testaddmodestr", "系统随机");
- }
- if (row.getLong("testaddmode") == 2) {
- row.put("testaddmodestr", "自选题目");
- }
- JSONArray jsonArray = row.getJSONArray("testquestions");
- row.put("testquestions", jsonArray);
- }
- return getSucReturnObject().setData(rows).toString();
- }
- @API(title = "生成试卷", apiversion = R.ID20240325102202.v1.class)
- public String addTest() throws YosException {
- Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
- Rows testheadRows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testhead WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and siteid='" + siteid + "'");
- if (testheadRows.isEmpty()) {
- return getErrReturnObject().setErrMsg("考试不存在").toString();
- }
- Rows testRows = dbConnect.runSqlQuery("SELECT sat_courseware_testid from sat_courseware_test WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and userid=" + userid + " and siteid='" + siteid + "'");
- if (testRows.isNotEmpty()) {
- Long sat_courseware_testid = testRows.get(0).getLong("sat_courseware_testid");
- content.put("sat_courseware_testid", sat_courseware_testid);
- return testinfo();
- }
- int testaddmode = testheadRows.get(0).getInteger("testaddmode");
- int num = testheadRows.get(0).getInteger("num");
- JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions");
- if (testaddmode == 1) {
- if (num > testquestions.size()) {
- return getErrReturnObject().setErrMsg("无法添加,题库数量小于考试题目数量").toString();
- }
- Collections.shuffle(testquestions);
- testquestions = new JSONArray(
- IntStream.range(0, num)
- .mapToObj(testquestions::get)
- .collect(Collectors.toList())
- );
- }
- Long sat_courseware_testid = createTableID("sat_courseware_test");
- ArrayList<String> sqlList = CoursewareHelper.getCoursewareTestSql(this, sat_courseware_testheadid, sat_courseware_testid, testquestions);
- dbConnect.runSqlUpdate(sqlList);
- content.put("sat_courseware_testid", sat_courseware_testid);
- return testinfo();
- }
- @API(title = "删除试卷", apiversion = R.ID20240325105502.v1.class)
- public String deleteTest() throws YosException {
- Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
- JSONArray userids = content.getJSONArray("userids");
- QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sat_courseware_test", "sat_courseware_testid");
- querySQL.setSiteid(siteid);
- querySQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadid);
- querySQL.setWhere("userid", userids.toArray());
- ArrayList ids = querySQL.query().toArrayList("sat_courseware_testid");
- DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, "sat_courseware_test");
- deleteSQL.setSiteid(siteid);
- deleteSQL.setWhere("sat_courseware_testid", ids);
- deleteSQL.delete();
- DeleteSQL deleteSQL2 = SQLFactory.createDeleteSQL(this, "sat_courseware_testitems");
- deleteSQL2.setSiteid(siteid);
- deleteSQL2.setWhere("sat_courseware_testid", ids);
- deleteSQL2.delete();
- return getSucReturnObject().toString();
- }
- @API(title = "试卷列表", apiversion = R.ID20240325141602.v1.class)
- public String testlist() throws YosException {
- StringBuffer where = new StringBuffer(" 1=1 ");
- if (content.containsKey("where")) {
- JSONObject whereObject = content.getJSONObject("where");
- if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
- where.append(" and(");
- where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
- where.append(")");
- }
- }
- Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
- QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sys_usersite"
- , "userid")
- .setTableAlias("t1");
- querySQL.addJoinTable(JOINTYPE.inner, "sat_courseware_testhead", "t2", "t2.siteid=t1.siteid"
- , "sat_courseware_testheadid", "num");
- querySQL.addJoinTable(JOINTYPE.left, "sat_courseware_test", "t3", "t3.userid=t1.userid and t3.siteid=t1.siteid and t3.sat_courseware_testheadid=" + sat_courseware_testheadid
- , "sat_courseware_testid");
- querySQL.addJoinTable(JOINTYPE.inner, "sys_dataauth_view", "t4", "t4.userid=t1.userid and t4.siteid=t1.siteid and t4.ownertable='sat_courseware_testhead' and t4.ownerid=" + sat_courseware_testheadid + " and t4.siteid='" + siteid + "'");
- querySQL.addJoinTable(JOINTYPE.inner, "sys_users", "t5", "t5.userid=t1.userid");
- querySQL.addQueryFields("name", "CONCAT(t5.name,'-',t5.accountno)");
- querySQL.addQueryFields("score", "IFNULL(t3.score,0)");
- querySQL.addQueryFields("answercount", "IFNULL(t3.answercount,0)");
- querySQL.addQueryFields("errcount", "IFNULL(t3.errcount,0)");
- querySQL.addQueryFields("begdate", "IFNULL(t3.begdate,'')");
- querySQL.addQueryFields("status", "IFNULL(t3.`status`,'未开始')");
- querySQL.addQueryFields("rightcount", "IFNULL(t3.`rightcount`,0)");
- querySQL.setWhere(where.toString());
- querySQL.setSiteid(siteid);
- querySQL.setWhere("t2.sat_courseware_testheadid", sat_courseware_testheadid);
- if (pageSorting.equals("''")) {
- pageSorting = "t3.`status` desc";
- }
- querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
- Rows rows = querySQL.query();
- for (Row row : rows) {
- row.put("answercount", row.getString("answercount") + "/" + row.getString("num"));
- }
- return getSucReturnObject().setData(rows).toString();
- }
- @API(title = "试卷详情", apiversion = R.ID20240325143202.v1.class)
- public String testinfo() throws YosException {
- Long sat_courseware_testid = content.getLongValue("sat_courseware_testid");
- if (dbConnect.runSqlQuery("SELECT * from sat_courseware_test WHERE sat_courseware_testid=" + sat_courseware_testid + " and siteid='" + siteid + "' and status='未开始' and userid='" + userid + "'").isNotEmpty()) {
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sat_courseware_test");
- updateSQL.setUniqueid(sat_courseware_testid);
- updateSQL.setSiteid(siteid);
- updateSQL.setValue("begdate", getDateTime_Str());
- updateSQL.setValue("status", "进行中");
- updateSQL.update();
- }
- QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sat_courseware_test").setTableAlias("t1");
- querySQL.addJoinTable(JOINTYPE.left, "sat_courseware_testhead", "t2", "t2.sat_courseware_testheadid=t1.sat_courseware_testheadid and t2.siteid=t1.siteid"
- , "num", "passingscore", "title");
- querySQL.setWhere("sat_courseware_testid", sat_courseware_testid);
- querySQL.setSiteid(siteid);
- Rows rows = querySQL.query();
- Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
- detailRow.putIfAbsent("score", 0.0);
- detailRow.putIfAbsent("begdate", "");
- detailRow.putIfAbsent("submitdate", "");
- BigDecimal passingscore = detailRow.getBigDecimal("passingscore");
- BigDecimal score = detailRow.getBigDecimal("score");
- if (score.compareTo(passingscore) >= 0) {
- detailRow.put("ispassingscore", "及格");
- } else {
- detailRow.put("ispassingscore", "不及格");
- }
- QuerySQL querySQL2 = SQLFactory.createQuerySQL(this, "sat_courseware_testitems").setTableAlias("t1");
- querySQL2.setSiteid(siteid)
- .setWhere("sat_courseware_testid", detailRow.getLong("sat_courseware_testid"));
- Rows rows2 = querySQL2.query();
- for (Row row : rows2) {
- JSONArray jsonArray = row.getJSONArray("options");
- row.put("options", jsonArray);
- ArrayList<String> answer_fact = new ArrayList<>();
- for (Object object : jsonArray) {
- JSONObject jsonObject = (JSONObject) object;
- if (jsonObject.getString("isfact").equals("1")) {
- answer_fact.add(jsonObject.getString("option"));
- }
- }
- row.put("answer_fact", answer_fact);
- row.put("typemxstr", answer_fact.size() > 1 ? "多选" : "单选");
- JSONArray answer = row.getJSONArray("answer");
- row.put("answer", answer);
- Rows optionsrows = Attachment.get(this, "sat_courseware_testitems", row.getLong("sat_courseware_testitemsid"));
- row.put("attinfos", optionsrows);
- }
- detailRow.put("testquestions", rows2);
- return getSucReturnObject().setData(detailRow).toString();
- }
- @API(title = "查询我的考试列表", apiversion = R.ID20240326133302.v1.class)
- public String queryUserTestList() throws YosException {
- StringBuffer where = new StringBuffer(" 1=1 ");
- if (content.containsKey("where")) {
- JSONObject whereObject = content.getJSONObject("where");
- if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
- where.append(" and(");
- where.append("t1.title like'%").append(whereObject.getString("condition")).append("%' ");
- where.append(")");
- }
- if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
- if (whereObject.getString("status").equals("未开始")) {
- where.append(" and(");
- where.append("t2.status is null ");
- where.append(")");
- } else {
- where.append(" and(");
- where.append("t2.status='").append(whereObject.getString("status")).append("' ");
- where.append(")");
- }
- }
- if (whereObject.containsKey("sat_courseware_classids") && !"".equals(whereObject.getString("sat_courseware_classids"))) {
- JSONArray sat_courseware_classids = whereObject.getJSONArray("sat_courseware_classids");
- if (sat_courseware_classids.size() > 0) {
- where.append(" and (1=2");
- for (Object obj : sat_courseware_classids) {
- JSONArray array = (JSONArray) obj;
- for (Object obj2 : array) {
- where.append(" or (");
- where.append("JSON_CONTAINS(t1.sat_courseware_classids,'" + obj2 + "')");
- where.append(")");
- }
- }
- where.append(")");
- }
- }
- }
- QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sat_courseware_testhead"
- , "sat_courseware_testheadid", "title", "num")
- .setTableAlias("t1");
- querySQL.addJoinTable(JOINTYPE.left, "sat_courseware_test", "t2", "t2.sat_courseware_testheadid=t1.sat_courseware_testheadid and t2.siteid=t1.siteid and t2.userid = " + userid
- , "answercount", "status", "score");
- querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
- querySQL.setSiteid(siteid);
- querySQL.setWhere("status", "发布");
- querySQL.setWhere(where.toString());
- querySQL.setDataAuth(true);
- Rows rows = querySQL.query();
- for (Row row : rows) {
- row.put("answerinfo", row.getString("answercount") + "/" + row.getString("num"));
- row.putIfAbsent("score", 0.0);
- if (row.getString("status").equals("")) {
- row.put("status", "未开始");
- }
- }
- return getSucReturnObject().setData(rows).toString();
- }
- @API(title = "考试提交", apiversion = R.ID20240326145902.v1.class)
- public String submit() throws YosException {
- JSONArray answers = content.getJSONArray("answers");
- ArrayList<String> sqlList = new ArrayList<>();
- for (Object object : answers) {
- JSONObject jsonObject = (JSONObject) object;
- Long sat_courseware_testitemsid = jsonObject.getLong("sat_courseware_testitemsid");
- JSONArray answer = jsonObject.getJSONArray("answer");
- boolean isSame = isSame(answer, getAnswer(sat_courseware_testitemsid));
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sat_courseware_testitems");
- updateSQL.setUniqueid(sat_courseware_testitemsid);
- updateSQL.setSiteid(siteid);
- updateSQL.setValue("answer", answer);
- updateSQL.setValue("result", isSame);
- sqlList.add(updateSQL.getSQL());
- }
- dbConnect.runSqlUpdate(sqlList);
- Long sat_courseware_testid = content.getLongValue("sat_courseware_testid");
- Rows scoreRows = dbConnect.runSqlQuery("SELECT sum(score) score from sat_courseware_testitems WHERE result=1 and sat_courseware_testid=" + sat_courseware_testid + " and siteid='" + siteid + "'");
- Rows rightRows = dbConnect.runSqlQuery("SELECT count(1) count from sat_courseware_testitems WHERE result=1 and sat_courseware_testid=" + sat_courseware_testid + " and siteid='" + siteid + "'");
- Rows answerRows = dbConnect.runSqlQuery("SELECT count(1) count from sat_courseware_testitems WHERE answer is not null and sat_courseware_testid=" + sat_courseware_testid + " and siteid='" + siteid + "'");
- UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sat_courseware_test");
- updateSQL.setUniqueid(sat_courseware_testid);
- updateSQL.setSiteid(siteid);
- updateSQL.setValue("submitdate", getDateTime_Str());
- updateSQL.setValue("status", "已完成");
- updateSQL.setValue("countdown", content.getStringValue("countdown"));
- updateSQL.setValue("score", scoreRows.get(0).getBigDecimal("score"));
- updateSQL.setValue("answercount", answerRows.get(0).getLong("count"));
- updateSQL.setValue("errcount", answerRows.get(0).getLong("count") - rightRows.get(0).getLong("count"));
- updateSQL.setValue("rightcount", rightRows.get(0).getLong("count"));
- System.err.println(updateSQL.getSQL());
- updateSQL.update();
- return getSucReturnObject().toString();
- }
- public JSONArray getAnswer(Long sat_courseware_testitemsid) throws YosException {
- Rows rows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testitems WHERE sat_courseware_testitemsid=" + sat_courseware_testitemsid + " and siteid='" + siteid + "'");
- if (rows.isEmpty()) {
- return new JSONArray();
- }
- JSONArray answer = new JSONArray();
- JSONArray jsonArray = rows.get(0).getJSONArray("options");
- for (Object object : jsonArray) {
- JSONObject jsonObject = (JSONObject) object;
- if (jsonObject.getString("isfact").equals("1")) {
- answer.add(jsonObject.getString("option"));
- }
- }
- return answer;
- }
- public boolean isSame(JSONArray a, JSONArray b) {
- if (a.size() == 0) {
- return false;
- }
- if (a.size() != b.size()) {
- return false;
- }
- for (Object o : a) {
- if (!b.contains(o)) {
- return false;
- }
- }
- for (Object o : b) {
- if (!a.contains(o)) {
- return false;
- }
- }
- return true;
- }
- }
|