package restcontroller.webmanage.saletool.courseware; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import common.Controller; import common.YosException; import common.annotation.API; import common.data.*; import restcontroller.R; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; /** * 考试表头 */ 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 sat_courseware_testlibraryids = new ArrayList<>(); if (testaddmode == 1) { if (num <= 0) { return getErrReturnObject().setErrMsg("出题方式为系统随机时题目设置数量需大于0").toString(); } if (sat_courseware_classids.size() <= 0) { return getErrReturnObject().setErrMsg("请选择分类").toString(); } StringBuffer where = new StringBuffer("(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_testlibrary").setTableAlias("t1"); querySQL.setSiteid(siteid); querySQL.setWhere(where.toString()); Rows rows = querySQL.query(); 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); updateSQL.update(); } if (sat_courseware_testlibraryids.size() > 0) { Collections.shuffle(sat_courseware_testlibraryids); content.put("sat_courseware_testlibraryids",sat_courseware_testlibraryids.subList(0,num)); 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(); JSONArray jsonArray = detailRow.getJSONArray("testquestions"); detailRow.put("testquestions", jsonArray); 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.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 getErrReturnObject().setErrMsg("请选择题目").toString(); } JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions"); //移除考试题目 testquestions.removeAll(CoursewareHelper.getRemoveArray(testquestions, sat_courseware_testlibraryids)); //添加考试题目 testquestions.addAll(CoursewareHelper.getTestlibraryRows(this, sat_courseware_testlibraryids)); //设置考试分数 testquestions = CoursewareHelper.setTestquestionsScore(testquestions); 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)); //设置考试分数 testquestions = CoursewareHelper.setTestquestionsScore(testquestions); 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"); for (Object obj : sat_courseware_classids) { JSONArray array = (JSONArray) obj; for (Object obj2 : array) { where.append(" and ("); where.append("JSON_CONTAINS(t1.sat_courseware_classids,'" + obj2 + "')"); 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(); } }