coursewaretesthead.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package restcontroller.webmanage.saletool.courseware;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import common.Controller;
  5. import common.YosException;
  6. import common.annotation.API;
  7. import common.data.*;
  8. import restcontroller.R;
  9. import java.math.BigDecimal;
  10. import java.util.ArrayList;
  11. import java.util.Collections;
  12. /**
  13. * 考试表头
  14. */
  15. public class coursewaretesthead extends Controller {
  16. String sat_courseware_testhead = "sat_courseware_testhead";
  17. public coursewaretesthead(JSONObject content) throws YosException {
  18. super(content);
  19. }
  20. @API(title = "考试表头新增或更新", apiversion = R.ID20240323112202.v1.class)
  21. public String insertOrUpdate() throws YosException {
  22. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  23. int testaddmode = content.getIntValue("testaddmode", 1);
  24. int num = content.getIntValue("num", 0);
  25. JSONArray sat_courseware_classids = new JSONArray();
  26. if (content.containsKey("sat_courseware_classids")) {
  27. sat_courseware_classids = content.getJSONArray("sat_courseware_classids");
  28. }
  29. ArrayList<Long> sat_courseware_testlibraryids = new ArrayList<>();
  30. if (testaddmode == 1) {
  31. if (num <= 0) {
  32. return getErrReturnObject().setErrMsg("出题方式为系统随机时题目设置数量需大于0").toString();
  33. }
  34. if (sat_courseware_classids.size() <= 0) {
  35. return getErrReturnObject().setErrMsg("请选择分类").toString();
  36. }
  37. StringBuffer where = new StringBuffer("(1=2");
  38. for (Object obj : sat_courseware_classids) {
  39. JSONArray array = (JSONArray) obj;
  40. for (Object obj2 : array) {
  41. where.append(" or (");
  42. where.append("JSON_CONTAINS(t1.sat_courseware_classids,'" + obj2 + "')");
  43. where.append(")");
  44. }
  45. }
  46. where.append(")");
  47. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sat_courseware_testlibrary").setTableAlias("t1");
  48. querySQL.setSiteid(siteid);
  49. querySQL.setWhere(where.toString());
  50. Rows rows = querySQL.query();
  51. if (num > rows.size()) {
  52. return getErrReturnObject().setErrMsg("当前分类下题目数量小于系统随机出题数").toString();
  53. }
  54. sat_courseware_testlibraryids = rows.toArrayList("sat_courseware_testlibraryid", new ArrayList<>());
  55. }
  56. if (sat_courseware_testheadid <= 0) {
  57. sat_courseware_testheadid = createTableID(sat_courseware_testhead);
  58. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, sat_courseware_testhead);
  59. insertSQL.setSiteid(siteid);
  60. insertSQL.setUniqueid(sat_courseware_testheadid);
  61. insertSQL.setValue("title", content.getStringValue("title"));
  62. insertSQL.setValue("status", "新建");
  63. insertSQL.setValue("testaddmode", content.getIntValue("testaddmode", 1));
  64. insertSQL.setValue("passingscore", content.getBigDecimalValue("passingscore", 60));
  65. insertSQL.setValue("num", content.getIntValue("num", 0));
  66. insertSQL.setValue("sat_courseware_classids", sat_courseware_classids);
  67. insertSQL.insert();
  68. content.put("sat_courseware_testheadid", sat_courseware_testheadid);
  69. }
  70. if (sat_courseware_testheadid > 0) {
  71. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
  72. updateSQL.setSiteid(siteid);
  73. updateSQL.setUniqueid(sat_courseware_testheadid);
  74. updateSQL.setValue("title", content.getStringValue("title"));
  75. updateSQL.setValue("testaddmode", content.getIntValue("testaddmode", 1));
  76. updateSQL.setValue("passingscore", content.getBigDecimalValue("passingscore", 60));
  77. updateSQL.setValue("num", content.getIntValue("num", 0));
  78. updateSQL.setValue("sat_courseware_classids", sat_courseware_classids);
  79. updateSQL.update();
  80. }
  81. if (sat_courseware_testlibraryids.size() > 0) {
  82. Collections.shuffle(sat_courseware_testlibraryids);
  83. content.put("sat_courseware_testlibraryids",sat_courseware_testlibraryids.subList(0,num));
  84. return addtestquestions();
  85. }
  86. return detail();
  87. }
  88. @API(title = "考试表头详情", apiversion = R.ID20240323112302.v1.class)
  89. public String detail() throws YosException {
  90. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  91. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sat_courseware_testhead).setTableAlias("t1");
  92. querySQL.setSiteid(siteid);
  93. querySQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadid);
  94. Rows rows = querySQL.query();
  95. Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
  96. JSONArray jsonArray = detailRow.getJSONArray("testquestions");
  97. detailRow.put("testquestions", jsonArray);
  98. JSONArray sat_courseware_classids = detailRow.getJSONArray("sat_courseware_classids");
  99. if (sat_courseware_classids.size() == 0) {
  100. detailRow.put("classnames", "");
  101. } else {
  102. detailRow.put("classnames", CoursewareHelper.getClassnames(this, sat_courseware_classids));
  103. }
  104. if (detailRow.getLong("testaddmode") == 1) {
  105. detailRow.put("testaddmodestr", "系统随机");
  106. }
  107. if (detailRow.getLong("testaddmode") == 2) {
  108. detailRow.put("testaddmodestr", "自选题目");
  109. }
  110. return getSucReturnObject().setData(detailRow).toString();
  111. }
  112. @API(title = "考试表头删除", apiversion = R.ID20240323112402.v1.class)
  113. public String delete() throws YosException {
  114. JSONArray sat_courseware_testheadids = content.getJSONArray("sat_courseware_testheadids");
  115. DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(this, sat_courseware_testhead);
  116. deleteSQL.setSiteid(siteid);
  117. deleteSQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadids.toArray());
  118. deleteSQL.delete();
  119. return getSucReturnObject().toString();
  120. }
  121. @API(title = "考试表头发布", apiversion = R.ID20240323141002.v1.class)
  122. public String release() throws YosException {
  123. JSONArray sat_courseware_testheadids = content.getJSONArray("sat_courseware_testheadids");
  124. int status = content.getIntValue("status", 0);
  125. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
  126. updateSQL.setSiteid(siteid);
  127. updateSQL.setValue("status", status == 0 ? "新建" : "发布");
  128. updateSQL.setWhere("sat_courseware_testheadid", sat_courseware_testheadids.toArray());
  129. updateSQL.update();
  130. return getSucReturnObject().toString();
  131. }
  132. @API(title = "添加考试题目", apiversion = R.ID20240323141602.v1.class)
  133. public String addtestquestions() throws YosException {
  134. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  135. JSONArray sat_courseware_testlibraryids = content.getJSONArray("sat_courseware_testlibraryids");
  136. Rows testheadRows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testhead WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and siteid='" + siteid + "'");
  137. if (testheadRows.isEmpty()) {
  138. return getErrReturnObject().setErrMsg("数据不存在").toString();
  139. }
  140. if (sat_courseware_testlibraryids.size() <= 0) {
  141. return getErrReturnObject().setErrMsg("请选择题目").toString();
  142. }
  143. JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions");
  144. //移除考试题目
  145. testquestions.removeAll(CoursewareHelper.getRemoveArray(testquestions, sat_courseware_testlibraryids));
  146. //添加考试题目
  147. testquestions.addAll(CoursewareHelper.getTestlibraryRows(this, sat_courseware_testlibraryids));
  148. //设置考试分数
  149. testquestions = CoursewareHelper.setTestquestionsScore(testquestions);
  150. int testaddmode = testheadRows.get(0).getInteger("testaddmode");
  151. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
  152. updateSQL.setUniqueid(sat_courseware_testheadid);
  153. updateSQL.setSiteid(siteid);
  154. updateSQL.setValue("testquestions", testquestions);
  155. if (testaddmode == 2) {
  156. updateSQL.setValue("num", testquestions.size());
  157. }
  158. updateSQL.update();
  159. return detail();
  160. }
  161. @API(title = "删除考试题目", apiversion = R.ID20240323155602.v1.class)
  162. public String deletetestquestions() throws YosException {
  163. Long sat_courseware_testheadid = content.getLongValue("sat_courseware_testheadid");
  164. JSONArray sat_courseware_testlibraryids = content.getJSONArray("sat_courseware_testlibraryids");
  165. Rows testheadRows = dbConnect.runSqlQuery("SELECT * from sat_courseware_testhead WHERE sat_courseware_testheadid=" + sat_courseware_testheadid + " and siteid='" + siteid + "'");
  166. if (testheadRows.isEmpty()) {
  167. return getErrReturnObject().setErrMsg("数据不存在").toString();
  168. }
  169. if (sat_courseware_testlibraryids.size() <= 0) {
  170. return getErrReturnObject().setErrMsg("请选择题目").toString();
  171. }
  172. JSONArray testquestions = testheadRows.get(0).getJSONArray("testquestions");
  173. //移除考试题目
  174. testquestions.removeAll(CoursewareHelper.getRemoveArray(testquestions, sat_courseware_testlibraryids));
  175. //设置考试分数
  176. testquestions = CoursewareHelper.setTestquestionsScore(testquestions);
  177. int testaddmode = testheadRows.get(0).getInteger("testaddmode");
  178. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, sat_courseware_testhead);
  179. updateSQL.setUniqueid(sat_courseware_testheadid);
  180. updateSQL.setSiteid(siteid);
  181. updateSQL.setValue("testquestions", testquestions);
  182. if (testaddmode == 2) {
  183. updateSQL.setValue("num", testquestions.size());
  184. }
  185. updateSQL.update();
  186. return detail();
  187. }
  188. @API(title = "考试表头列表", apiversion = R.ID20240323112502.v1.class)
  189. public String list() throws YosException {
  190. StringBuffer where = new StringBuffer(" 1=1 ");
  191. if (content.containsKey("where")) {
  192. JSONObject whereObject = content.getJSONObject("where");
  193. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  194. where.append(" and (");
  195. where.append("t1.title like'%").append(whereObject.getString("condition")).append("%' ");
  196. // where.append("or t1.notes like'%").append(whereObject.getString("condition")).append("%' ");
  197. where.append(")");
  198. }
  199. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  200. where.append(" and (");
  201. where.append("t1.status ='").append(whereObject.getString("status")).append("' ");
  202. where.append(")");
  203. }
  204. if (whereObject.containsKey("testaddmode") && !"".equals(whereObject.getString("testaddmode"))) {
  205. where.append(" and (");
  206. where.append("t1.testaddmode ='").append(whereObject.getString("testaddmode")).append("' ");
  207. where.append(")");
  208. }
  209. if (whereObject.containsKey("sat_courseware_classids") && !"".equals(whereObject.getString("sat_courseware_classids"))) {
  210. JSONArray sat_courseware_classids = whereObject.getJSONArray("sat_courseware_classids");
  211. for (Object obj : sat_courseware_classids) {
  212. JSONArray array = (JSONArray) obj;
  213. for (Object obj2 : array) {
  214. where.append(" and (");
  215. where.append("JSON_CONTAINS(t1.sat_courseware_classids,'" + obj2 + "')");
  216. where.append(")");
  217. }
  218. }
  219. }
  220. if (whereObject.containsKey("begindate_create") && !"".equals(whereObject.getString("begindate_create"))) {
  221. where.append(" and (");
  222. where.append("t1.createdate >='").append(whereObject.getString("begindate_create")).append("' ");
  223. where.append(")");
  224. }
  225. if (whereObject.containsKey("enddate_create") && !"".equals(whereObject.getString("enddate_create"))) {
  226. where.append(" and (");
  227. where.append("t1.createdate <='").append(whereObject.getString("enddate_create")).append(" 23:59:59' ");
  228. where.append(")");
  229. }
  230. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  231. where.append(" and (");
  232. where.append("t1.senddate >='").append(whereObject.getString("begindate")).append("' ");
  233. where.append(")");
  234. }
  235. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  236. where.append(" and (");
  237. where.append("t1.senddate <='").append(whereObject.getString("enddate")).append(" 23:59:59' ");
  238. where.append(")");
  239. }
  240. }
  241. QuerySQL querySQL = SQLFactory.createQuerySQL(this, sat_courseware_testhead)
  242. .setTableAlias("t1");
  243. querySQL.setSiteid(siteid);
  244. querySQL.setWhere(where.toString());
  245. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  246. Rows rows = querySQL.query();
  247. for (Row row : rows) {
  248. if (row.getLong("testaddmode") == 1) {
  249. row.put("testaddmodestr", "系统随机");
  250. }
  251. if (row.getLong("testaddmode") == 2) {
  252. row.put("testaddmodestr", "自选题目");
  253. }
  254. JSONArray jsonArray = row.getJSONArray("testquestions");
  255. row.put("testquestions", jsonArray);
  256. }
  257. return getSucReturnObject().setData(rows).toString();
  258. }
  259. }