coursewaretesthead.java 31 KB

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