coursewaretesthead.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. package restcontroller.webmanage.saletool.courseware;
  2. import beans.department.Department;
  3. import beans.salearea.SaleArea;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.google.gson.JsonArray;
  8. import common.Controller;
  9. import common.YosException;
  10. import common.annotation.API;
  11. import common.annotation.CACHEING;
  12. import common.data.*;
  13. import restcontroller.R;
  14. import java.io.IOException;
  15. import java.math.BigDecimal;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.stream.Collectors;
  19. import java.util.stream.IntStream;
  20. /**
  21. * 考试表头
  22. */
  23. public class coursewaretesthead extends Controller {
  24. String sat_courseware_testhead = "sat_courseware_testhead";
  25. public coursewaretesthead(JSONObject content) throws YosException {
  26. super(content);
  27. }
  28. @API(title = "考试表头新增或更新", apiversion = R.ID20240323112202.v1.class)
  29. public String insertOrUpdate() throws YosException {
  30. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  31. int testaddmode = content.getIntValue("testaddmode", 1);
  32. int num = content.getIntValue("num", 0);
  33. JSONArray sat_courseware_classids = new JSONArray();
  34. if (content.containsKey("sat_courseware_classids")) {
  35. sat_courseware_classids = content.getJSONArray("sat_courseware_classids");
  36. }
  37. ArrayList<Long> sat_courseware_testlibraryids = new ArrayList<>();
  38. //前端需要的逻辑处理是否跳过下方的验证
  39. boolean isJump = false;
  40. for (Object object : sat_courseware_classids) {
  41. if (object instanceof JSONArray) {
  42. JSONArray jsonArray = (JSONArray) object;
  43. for (Object o : jsonArray) {
  44. if (o.toString().contains("9999")) {
  45. isJump = true;
  46. }
  47. }
  48. }
  49. }
  50. if (testaddmode == 1 && !isJump) {
  51. if (num <= 0) {
  52. return getErrReturnObject().setErrMsg("出题方式为系统随机时题目设置数量需大于0").toString();
  53. }
  54. if (sat_courseware_classids.size() <= 0) {
  55. return getErrReturnObject().setErrMsg("请选择分类").toString();
  56. }
  57. Rows rows = CoursewareHelper.getTestlibraryWithCoursewareClassRows(this, sat_courseware_classids);
  58. if (num > rows.size()) {
  59. return getErrReturnObject().setErrMsg("当前分类下题目数量小于系统随机出题数").toString();
  60. }
  61. sat_courseware_testlibraryids = rows.toArrayList("sat_courseware_testlibraryid", new ArrayList<>());
  62. }
  63. if (sat_courseware_testheadid <= 0) {
  64. sat_courseware_testheadid = createTableID(sat_courseware_testhead);
  65. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, sat_courseware_testhead);
  66. insertSQL.setSiteid(siteid);
  67. insertSQL.setUniqueid(sat_courseware_testheadid);
  68. insertSQL.setValue("title", content.getStringValue("title"));
  69. insertSQL.setValue("status", "新建");
  70. insertSQL.setValue("testaddmode", content.getIntValue("testaddmode", 1));
  71. insertSQL.setValue("passingscore", content.getBigDecimalValue("passingscore", 60));
  72. insertSQL.setValue("num", content.getIntValue("num", 0));
  73. insertSQL.setValue("sat_courseware_classids", sat_courseware_classids);
  74. insertSQL.insert();
  75. content.put("sat_courseware_testheadid", sat_courseware_testheadid);
  76. }
  77. if (sat_courseware_testheadid > 0) {
  78. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
  79. updateSQL.setSiteid(siteid);
  80. updateSQL.setUniqueid(sat_courseware_testheadid);
  81. updateSQL.setValue("title", content.getStringValue("title"));
  82. updateSQL.setValue("testaddmode", content.getIntValue("testaddmode", 1));
  83. updateSQL.setValue("passingscore", content.getBigDecimalValue("passingscore", 60));
  84. updateSQL.setValue("num", content.getIntValue("num", 0));
  85. updateSQL.setValue("sat_courseware_classids", sat_courseware_classids);
  86. if (testaddmode == 1) {//系统随机
  87. updateSQL.setValue("testquestions", new JSONArray());
  88. }
  89. updateSQL.update();
  90. }
  91. if (testaddmode == 1) {////系统随机
  92. content.put("sat_courseware_testlibraryids", sat_courseware_testlibraryids);
  93. return addtestquestions();
  94. }
  95. return detail();
  96. }
  97. @API(title = "考试表头详情", apiversion = R.ID20240323112302.v1.class)
  98. public String detail() throws YosException {
  99. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  100. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sat_courseware_testhead).setTableAlias("t1");
  101. querySQL.setSiteid(siteid);
  102. querySQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadid);
  103. Rows rows = querySQL.query();
  104. Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
  105. QuerySQL querySQL2 = SQLFactory.createQuerySQL(this, "sys_dataauths",
  106. "roleid");
  107. querySQL2.addJoinTable(JOINTYPE.inner, "sys_role", "t2", "t1.roleid=t2.roleid and t1.siteid=t2.siteid",
  108. "rolename");
  109. querySQL2.setTableAlias("t1");
  110. querySQL2.setWhere("t1.siteid", siteid);
  111. querySQL2.setWhere("t1.ownertable", "sat_courseware_testhead");
  112. querySQL2.setWhere("t1.ownerid", sat_courseware_testheadid);
  113. Rows roleRows = querySQL2.query();
  114. detailRow.put("role", roleRows.toArrayList("roleid", new ArrayList<Long>()));
  115. JSONArray jsonArray = detailRow.getJSONArray("testquestions");
  116. detailRow.put("testquestions", jsonArray);
  117. for (Object object : jsonArray) {
  118. JSONObject jsonObject = (JSONObject) object;
  119. if (jsonObject.getLong("type") == 1) {
  120. jsonObject.put("typestr", "选择题");
  121. }
  122. if (jsonObject.getLong("type") == 2) {
  123. jsonObject.put("typestr", "简答题");
  124. }
  125. if (jsonObject.getLong("typemx") == 0) {
  126. jsonObject.put("typemxstr", "简答");
  127. }
  128. if (jsonObject.getLong("typemx") == 1) {
  129. jsonObject.put("typemxstr", "单选");
  130. }
  131. if (jsonObject.getLong("typemx") == 2) {
  132. jsonObject.put("typemxstr", "多选");
  133. }
  134. }
  135. JSONArray sat_courseware_classids = detailRow.getJSONArray("sat_courseware_classids");
  136. if (sat_courseware_classids.size() == 0) {
  137. detailRow.put("classnames", "");
  138. } else {
  139. detailRow.put("classnames", CoursewareHelper.getClassnames(this, sat_courseware_classids));
  140. }
  141. if (detailRow.getLong("testaddmode") == 1) {
  142. detailRow.put("testaddmodestr", "系统随机");
  143. }
  144. if (detailRow.getLong("testaddmode") == 2) {
  145. detailRow.put("testaddmodestr", "自选题目");
  146. }
  147. return getSucReturnObject().setData(detailRow).toString();
  148. }
  149. @API(title = "考试表头删除", apiversion = R.ID20240323112402.v1.class)
  150. public String delete() throws YosException {
  151. JSONArray sat_courseware_testheadids = content.getJSONArray("sat_courseware_testheadids");
  152. DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, sat_courseware_testhead);
  153. deleteSQL.setSiteid(siteid);
  154. deleteSQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadids.toArray());
  155. deleteSQL.delete();
  156. return getSucReturnObject().toString();
  157. }
  158. @API(title = "考试表头发布", apiversion = R.ID20240323141002.v1.class)
  159. public String release() throws YosException {
  160. JSONArray sat_courseware_testheadids = content.getJSONArray("sat_courseware_testheadids");
  161. int status = content.getIntValue("status", 0);
  162. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
  163. updateSQL.setSiteid(siteid);
  164. updateSQL.setValue("status", status == 0 ? "新建" : "发布");
  165. updateSQL.setValue("sendby", username);
  166. updateSQL.setValue("senddate", getDateTime_Str());
  167. updateSQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadids.toArray());
  168. updateSQL.update();
  169. return getSucReturnObject().toString();
  170. }
  171. @API(title = "添加考试题目", apiversion = R.ID20240323141602.v1.class)
  172. public String addtestquestions() throws YosException {
  173. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  174. JSONArray sat_courseware_testlibraryids = content.getJSONArray("sat_courseware_testlibraryids");
  175. Rows testheadRows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testhead WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and siteid='" + siteid + "'");
  176. if (testheadRows.isEmpty()) {
  177. return getErrReturnObject().setErrMsg("数据不存在").toString();
  178. }
  179. if (sat_courseware_testlibraryids.size() <= 0) {
  180. return detail();
  181. }
  182. JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions");
  183. //移除考试题目
  184. testquestions.removeAll(CoursewareHelper.getRemoveArray(testquestions, sat_courseware_testlibraryids));
  185. //添加考试题目
  186. testquestions.addAll(CoursewareHelper.getTestlibraryRows(this, sat_courseware_testlibraryids));
  187. int testaddmode = testheadRows.get(0).getInteger("testaddmode");
  188. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
  189. updateSQL.setUniqueid(sat_courseware_testheadid);
  190. updateSQL.setSiteid(siteid);
  191. updateSQL.setValue("testquestions", testquestions);
  192. if (testaddmode == 2) {
  193. updateSQL.setValue("num", testquestions.size());
  194. }
  195. updateSQL.update();
  196. return detail();
  197. }
  198. @API(title = "删除考试题目", apiversion = R.ID20240323155602.v1.class)
  199. public String deletetestquestions() throws YosException {
  200. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  201. JSONArray sat_courseware_testlibraryids = content.getJSONArray("sat_courseware_testlibraryids");
  202. Rows testheadRows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testhead WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and siteid='" + siteid + "'");
  203. if (testheadRows.isEmpty()) {
  204. return getErrReturnObject().setErrMsg("数据不存在").toString();
  205. }
  206. if (sat_courseware_testlibraryids.size() <= 0) {
  207. return getErrReturnObject().setErrMsg("请选择题目").toString();
  208. }
  209. JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions");
  210. //移除考试题目
  211. testquestions.removeAll(CoursewareHelper.getRemoveArray(testquestions, sat_courseware_testlibraryids));
  212. int testaddmode = testheadRows.get(0).getInteger("testaddmode");
  213. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
  214. updateSQL.setUniqueid(sat_courseware_testheadid);
  215. updateSQL.setSiteid(siteid);
  216. updateSQL.setValue("testquestions", testquestions);
  217. if (testaddmode == 2) {
  218. updateSQL.setValue("num", testquestions.size());
  219. }
  220. updateSQL.update();
  221. return detail();
  222. }
  223. @API(title = "考试表头列表", apiversion = R.ID20240323112502.v1.class)
  224. public String list() throws YosException {
  225. StringBuffer where = new StringBuffer(" 1=1 ");
  226. if (content.containsKey("where")) {
  227. JSONObject whereObject = content.getJSONObject("where");
  228. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  229. where.append(" and (");
  230. where.append("t1.title like'%").append(whereObject.getString("condition")).append("%' ");
  231. // where.append("or t1.notes like'%").append(whereObject.getString("condition")).append("%' ");
  232. where.append(")");
  233. }
  234. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  235. where.append(" and (");
  236. where.append("t1.status ='").append(whereObject.getString("status")).append("' ");
  237. where.append(")");
  238. }
  239. if (whereObject.containsKey("testaddmode") && !"".equals(whereObject.getString("testaddmode"))) {
  240. where.append(" and (");
  241. where.append("t1.testaddmode ='").append(whereObject.getString("testaddmode")).append("' ");
  242. where.append(")");
  243. }
  244. if (whereObject.containsKey("sat_courseware_classids") && !"".equals(whereObject.getString("sat_courseware_classids"))) {
  245. JSONArray sat_courseware_classids = whereObject.getJSONArray("sat_courseware_classids");
  246. if (sat_courseware_classids.size() > 0) {
  247. where.append(" and (1=2");
  248. for (Object obj : sat_courseware_classids) {
  249. JSONArray array = (JSONArray) obj;
  250. for (Object obj2 : array) {
  251. where.append(" or (");
  252. where.append("JSON_CONTAINS(t1.sat_courseware_classids,'" + obj2 + "')");
  253. where.append(")");
  254. }
  255. }
  256. where.append(")");
  257. }
  258. }
  259. if (whereObject.containsKey("begindate_create") && !"".equals(whereObject.getString("begindate_create"))) {
  260. where.append(" and (");
  261. where.append("t1.createdate >='").append(whereObject.getString("begindate_create")).append("' ");
  262. where.append(")");
  263. }
  264. if (whereObject.containsKey("enddate_create") && !"".equals(whereObject.getString("enddate_create"))) {
  265. where.append(" and (");
  266. where.append("t1.createdate <='").append(whereObject.getString("enddate_create")).append(" 23:59:59' ");
  267. where.append(")");
  268. }
  269. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  270. where.append(" and (");
  271. where.append("t1.senddate >='").append(whereObject.getString("begindate")).append("' ");
  272. where.append(")");
  273. }
  274. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  275. where.append(" and (");
  276. where.append("t1.senddate <='").append(whereObject.getString("enddate")).append(" 23:59:59' ");
  277. where.append(")");
  278. }
  279. }
  280. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sat_courseware_testhead)
  281. .setTableAlias("t1");
  282. querySQL.setSiteid(siteid);
  283. querySQL.setWhere(where.toString());
  284. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  285. Rows rows = querySQL.query();
  286. for (Row row : rows) {
  287. if (row.getLong("testaddmode") == 1) {
  288. row.put("testaddmodestr", "系统随机");
  289. }
  290. if (row.getLong("testaddmode") == 2) {
  291. row.put("testaddmodestr", "自选题目");
  292. }
  293. JSONArray jsonArray = row.getJSONArray("testquestions");
  294. row.put("testquestions", jsonArray);
  295. }
  296. return getSucReturnObject().setData(rows).toString();
  297. }
  298. @API(title = "生成试卷", apiversion = R.ID20240325102202.v1.class)
  299. public String addTest() throws YosException {
  300. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  301. Rows testheadRows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testhead WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and siteid='" + siteid + "'");
  302. if (testheadRows.isEmpty()) {
  303. return getErrReturnObject().setErrMsg("考试不存在").toString();
  304. }
  305. int testaddmode = testheadRows.get(0).getInteger("testaddmode");
  306. int num = testheadRows.get(0).getInteger("num");
  307. JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions");
  308. if (testaddmode == 1) {
  309. if (num > testquestions.size()) {
  310. return getErrReturnObject().setErrMsg("无法添加,题库数量小于考试题目数量").toString();
  311. }
  312. Collections.shuffle(testquestions);
  313. testquestions = new JSONArray(
  314. IntStream.range(0, num)
  315. .mapToObj(testquestions::get)
  316. .collect(Collectors.toList())
  317. );
  318. }
  319. Long sat_courseware_testid = createTableID("sat_courseware_test");
  320. ArrayList<String> sqlList = CoursewareHelper.getCoursewareTestSql(this, sat_courseware_testheadid, sat_courseware_testid, testquestions);
  321. dbConnect.runSqlUpdate(sqlList);
  322. Row row = new Row();
  323. row.put("sat_courseware_testid", sat_courseware_testid);
  324. return getSucReturnObject().setData(row).toString();
  325. }
  326. @API(title = "删除试卷", apiversion = R.ID20240325105502.v1.class)
  327. public String deleteTest() throws YosException {
  328. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  329. JSONArray userids = content.getJSONArray("userids");
  330. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sat_courseware_test", "sat_courseware_testid");
  331. querySQL.setSiteid(siteid);
  332. querySQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadid);
  333. querySQL.setWhere("userid", userids.toArray());
  334. ArrayList ids = querySQL.query().toArrayList("sat_courseware_testid");
  335. DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, "sat_courseware_test");
  336. deleteSQL.setSiteid(siteid);
  337. deleteSQL.setWhere("sat_courseware_testid", ids);
  338. deleteSQL.delete();
  339. DeleteSQL deleteSQL2 = SQLFactory.createDeleteSQL(this, "sat_courseware_testitems");
  340. deleteSQL2.setSiteid(siteid);
  341. deleteSQL2.setWhere("sat_courseware_testid", ids);
  342. deleteSQL2.delete();
  343. return getSucReturnObject().toString();
  344. }
  345. @API(title = "试卷列表", apiversion = R.ID20240325141602.v1.class)
  346. public String testlist() throws YosException {
  347. StringBuffer where = new StringBuffer(" 1=1 ");
  348. if (content.containsKey("where")) {
  349. JSONObject whereObject = content.getJSONObject("where");
  350. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  351. where.append(" and(");
  352. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  353. where.append(")");
  354. }
  355. }
  356. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  357. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sat_courseware_test").setTableAlias("t1");
  358. querySQL.addJoinTable(JOINTYPE.left, "sat_courseware_testhead", "t2", "t2.sat_courseware_testheadid=t1.sat_courseware_testheadid and t2.siteid=t1.siteid"
  359. , "num");
  360. querySQL.setWhere(where.toString());
  361. querySQL.setSiteid(siteid);
  362. querySQL.setWhere("t1.sat_courseware_testheadid", sat_courseware_testheadid);
  363. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  364. Rows rows = querySQL.query();
  365. for (Row row : rows) {
  366. row.put("answercount", row.getString("answercount") + "/" + row.getString("num"));
  367. row.putIfAbsent("score", 0);
  368. row.putIfAbsent("begdate", "");
  369. row.putIfAbsent("submitdate", "");
  370. }
  371. return getSucReturnObject().setData(rows).toString();
  372. }
  373. @API(title = "试卷详情", apiversion = R.ID20240325143202.v1.class)
  374. public String testinfo() throws YosException {
  375. Long sat_courseware_testid = content.getLongValue("sat_courseware_testid");
  376. if (dbConnect.runSqlQuery("SELECT * from sat_courseware_test WHERE sat_courseware_testid=" + sat_courseware_testid + " and siteid='" + siteid + "' and status='未开始' and userid='" + userid + "'").isNotEmpty()) {
  377. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sat_courseware_test");
  378. updateSQL.setUniqueid(sat_courseware_testid);
  379. updateSQL.setSiteid(siteid);
  380. updateSQL.setValue("begdate", getDateTime_Str());
  381. updateSQL.setValue("status", "进行中");
  382. updateSQL.update();
  383. }
  384. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sat_courseware_test").setTableAlias("t1");
  385. querySQL.addJoinTable(JOINTYPE.left, "sat_courseware_testhead", "t2", "t2.sat_courseware_testheadid=t1.sat_courseware_testheadid and t2.siteid=t1.siteid"
  386. , "num", "passingscore", "title");
  387. querySQL.setWhere("sat_courseware_testid", sat_courseware_testid);
  388. querySQL.setSiteid(siteid);
  389. Rows rows = querySQL.query();
  390. Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
  391. detailRow.putIfAbsent("score", 0.0);
  392. detailRow.putIfAbsent("begdate", "");
  393. detailRow.putIfAbsent("submitdate", "");
  394. BigDecimal passingscore = detailRow.getBigDecimal("passingscore");
  395. BigDecimal score = detailRow.getBigDecimal("score");
  396. if (score.compareTo(passingscore) >= 0) {
  397. detailRow.put("ispassingscore", "及格");
  398. } else {
  399. detailRow.put("ispassingscore", "不及格");
  400. }
  401. QuerySQL querySQL2 = SQLFactory.createQuerySQL(this, "sat_courseware_testitems").setTableAlias("t1");
  402. querySQL2.setSiteid(siteid)
  403. .setWhere("sat_courseware_testid", detailRow.getLong("sat_courseware_testid"));
  404. Rows rows2 = querySQL2.query();
  405. for (Row row : rows2) {
  406. JSONArray jsonArray = row.getJSONArray("options");
  407. row.put("options", jsonArray);
  408. ArrayList<String> answer_fact = new ArrayList<>();
  409. for (Object object : jsonArray) {
  410. JSONObject jsonObject = (JSONObject) object;
  411. if (jsonObject.getString("isfact").equals("1")) {
  412. answer_fact.add(jsonObject.getString("option"));
  413. }
  414. }
  415. row.put("answer_fact", answer_fact);
  416. row.put("typemxstr", answer_fact.size() > 1 ? "多选" : "单选");
  417. JSONArray answer = row.getJSONArray("answer");
  418. row.put("answer", answer);
  419. }
  420. detailRow.put("testquestions", rows2);
  421. return getSucReturnObject().setData(detailRow).toString();
  422. }
  423. @API(title = "查询我的考试列表", apiversion = R.ID20240326133302.v1.class)
  424. public String queryUserTestList() throws YosException {
  425. StringBuffer where = new StringBuffer(" 1=1 ");
  426. if (content.containsKey("where")) {
  427. JSONObject whereObject = content.getJSONObject("where");
  428. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  429. where.append(" and(");
  430. where.append("t1.title like'%").append(whereObject.getString("condition")).append("%' ");
  431. where.append(")");
  432. }
  433. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  434. where.append(" and(");
  435. where.append("t2.status='").append(whereObject.getString("status")).append("' ");
  436. where.append(")");
  437. }
  438. if (whereObject.containsKey("sat_courseware_classids") && !"".equals(whereObject.getString("sat_courseware_classids"))) {
  439. JSONArray sat_courseware_classids = whereObject.getJSONArray("sat_courseware_classids");
  440. if (sat_courseware_classids.size() > 0) {
  441. where.append(" and (1=2");
  442. for (Object obj : sat_courseware_classids) {
  443. JSONArray array = (JSONArray) obj;
  444. for (Object obj2 : array) {
  445. where.append(" or (");
  446. where.append("JSON_CONTAINS(t1.sat_courseware_classids,'" + obj2 + "')");
  447. where.append(")");
  448. }
  449. }
  450. where.append(")");
  451. }
  452. }
  453. }
  454. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sat_courseware_testhead"
  455. ,"sat_courseware_testheadid","title","num")
  456. .setTableAlias("t1");
  457. querySQL.addJoinTable(JOINTYPE.left,"sat_courseware_test","t2","t2.sat_courseware_testheadid=t1.sat_courseware_testheadid and t2.siteid=t1.siteid"
  458. ,"answercount");
  459. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  460. querySQL.setSiteid(siteid);
  461. querySQL.setWhere("status", "发布");
  462. querySQL.setWhere(where.toString());
  463. querySQL.setDataAuth(true);
  464. Rows rows = querySQL.query();
  465. for (Row row : rows) {
  466. row.put("answerinfo", row.getString("answercount") + "/" + row.getString("num"));
  467. row.putIfAbsent("score", 0.0);
  468. if (row.getString("status").equals("")) {
  469. row.put("status", "未开始");
  470. }
  471. }
  472. return getSucReturnObject().setData(rows).toString();
  473. }
  474. @API(title = "考试提交", apiversion = R.ID20240326145902.v1.class)
  475. public String submit() throws YosException {
  476. JSONArray answers = content.getJSONArray("answers");
  477. ArrayList<String> sqlList = new ArrayList<>();
  478. for (Object object : answers) {
  479. JSONObject jsonObject = (JSONObject) object;
  480. Long sat_courseware_testitemsid = jsonObject.getLong("sat_courseware_testitemsid");
  481. JSONArray answer = jsonObject.getJSONArray("answer");
  482. boolean isSame = isSame(answer, getAnswer(sat_courseware_testitemsid));
  483. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sat_courseware_testitems");
  484. updateSQL.setUniqueid(sat_courseware_testitemsid);
  485. updateSQL.setSiteid(siteid);
  486. updateSQL.setValue("answer", answer);
  487. updateSQL.setValue("result", isSame);
  488. sqlList.add(updateSQL.getSQL());
  489. }
  490. dbConnect.runSqlUpdate(sqlList);
  491. Long sat_courseware_testid = content.getLongValue("sat_courseware_testid");
  492. 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 + "'");
  493. 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 + "'");
  494. 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 + "'");
  495. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sat_courseware_test");
  496. updateSQL.setUniqueid(sat_courseware_testid);
  497. updateSQL.setSiteid(siteid);
  498. updateSQL.setValue("submitdate", getDateTime_Str());
  499. updateSQL.setValue("status", "已完成");
  500. updateSQL.setValue("score", scoreRows.get(0).getBigDecimal("score"));
  501. updateSQL.setValue("answercount", answerRows.get(0).getLong("count"));
  502. updateSQL.setValue("errcount", answerRows.get(0).getLong("count") - rightRows.get(0).getLong("count"));
  503. updateSQL.setValue("rightcount", rightRows.get(0).getLong("count"));
  504. System.err.println(updateSQL.getSQL());
  505. updateSQL.update();
  506. return getSucReturnObject().toString();
  507. }
  508. public JSONArray getAnswer(Long sat_courseware_testitemsid) throws YosException {
  509. Rows rows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testitems WHERE sat_courseware_testitemsid=" + sat_courseware_testitemsid + " and siteid='" + siteid + "'");
  510. if (rows.isEmpty()) {
  511. return new JSONArray();
  512. }
  513. JSONArray answer = new JSONArray();
  514. JSONArray jsonArray = rows.get(0).getJSONArray("options");
  515. for (Object object : jsonArray) {
  516. JSONObject jsonObject = (JSONObject) object;
  517. if (jsonObject.getString("isfact").equals("1")) {
  518. answer.add(jsonObject.getString("option"));
  519. }
  520. }
  521. return answer;
  522. }
  523. public boolean isSame(JSONArray a, JSONArray b) {
  524. if (a.size() == 0) {
  525. return false;
  526. }
  527. if (a.size() != b.size()) {
  528. return false;
  529. }
  530. for (Object o : a) {
  531. if (!b.contains(o)) {
  532. return false;
  533. }
  534. }
  535. for (Object o : b) {
  536. if (!a.contains(o)) {
  537. return false;
  538. }
  539. }
  540. return true;
  541. }
  542. }