StudentApplyController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package com.qmth.exam.reserve.controller.admin;
  2. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  3. import com.qmth.boot.api.annotation.Aac;
  4. import com.qmth.boot.api.constant.ApiConstant;
  5. import com.qmth.boot.core.collection.PageResult;
  6. import com.qmth.boot.core.exception.StatusException;
  7. import com.qmth.boot.tools.excel.ExcelWriter;
  8. import com.qmth.boot.tools.excel.enums.ExcelType;
  9. import com.qmth.exam.reserve.bean.Constants;
  10. import com.qmth.exam.reserve.bean.login.LoginUser;
  11. import com.qmth.exam.reserve.bean.stdapply.*;
  12. import com.qmth.exam.reserve.controller.BaseController;
  13. import com.qmth.exam.reserve.enums.Role;
  14. import com.qmth.exam.reserve.service.ApplyTaskService;
  15. import com.qmth.exam.reserve.service.CategoryService;
  16. import com.qmth.exam.reserve.service.ExamSiteService;
  17. import com.qmth.exam.reserve.service.StudentApplyService;
  18. import com.qmth.exam.reserve.util.DateUtil;
  19. import com.qmth.exam.reserve.util.ResourceUtil;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. import io.swagger.annotations.ApiParam;
  23. import org.apache.commons.codec.digest.DigestUtils;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.web.bind.annotation.*;
  26. import org.springframework.web.multipart.MultipartFile;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.net.URLEncoder;
  31. import java.util.ArrayList;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. @RestController
  36. @Api(tags = "【管理端】考生预约明细相关接口")
  37. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/admin/apply")
  38. @Aac(strict = false, auth = true)
  39. public class StudentApplyController extends BaseController {
  40. @Autowired
  41. private StudentApplyService studentApplyService;
  42. @Autowired
  43. private ApplyTaskService applyTaskService;
  44. @Autowired
  45. private CategoryService categoryService;
  46. @Autowired
  47. private ExamSiteService examSiteService;
  48. @ApiOperation(value = "预约任务列表")
  49. @PostMapping(value = "/task/list")
  50. public List<CategoryVO> listTask() {
  51. return applyTaskService.listTask();
  52. }
  53. @ApiOperation(value = "教学点列表")
  54. @PostMapping(value = "/teaching/list")
  55. public List<CategoryVO> listTeaching(@ApiParam("查询启用的教学点或查询所有的教学点") @RequestParam(required = false) Boolean flag) {
  56. LoginUser user = this.curLoginUser();
  57. return categoryService.listTeaching(user, flag);
  58. }
  59. @ApiOperation(value = "考点列表")
  60. @PostMapping(value = "/agent/list")
  61. public List<CategoryVO> listAgent(@ApiParam("教学点ID") @RequestParam Long id,
  62. @ApiParam("查询启用的考点或查询所有的考点") @RequestParam(required = false) Boolean flag) {
  63. return examSiteService.listExamSite(id, flag);
  64. }
  65. @ApiOperation(value = "考生预约名单详情分页")
  66. @PostMapping(value = "/std/page")
  67. public PageResult<StudentApplyVO> page(@RequestBody StudentApplyReq req) {
  68. LoginUser user = this.curLoginUser();
  69. if (user.getRole().equals(Role.TEACHING)) {
  70. req.setTeachingId(user.getCategoryId());
  71. }
  72. return studentApplyService.page(req);
  73. }
  74. @ApiOperation(value = "取消预约")
  75. @PostMapping(value = "/std/cancel")
  76. public void cancel(@ApiParam("预约结果ID") @RequestParam Long id) {
  77. LoginUser user = this.curLoginUser();
  78. studentApplyService.cancel(user, id);
  79. }
  80. @ApiOperation(value = "导入预考模版下载")
  81. @PostMapping(value = "/imp/template")
  82. public void download(HttpServletResponse response) {
  83. exportFile("导入预考模板.xlsx", ResourceUtil.getStream("templates/preExamImport.xlsx"));
  84. }
  85. @ApiOperation(value = "导入预考")
  86. @PostMapping(value = "/import")
  87. public Map<String, Object> importPreExamStd(@ApiParam("教学点ID") @RequestParam(required = false) Long teachingId,
  88. @ApiParam("教学点所在的层级") @RequestParam(required = false) Integer level, @RequestParam MultipartFile file) {
  89. LoginUser user = this.curLoginUser();
  90. if (Role.TEACHING.equals(user.getRole())) {
  91. teachingId = user.getCategoryId();
  92. }
  93. List<Map<String, Object>> failRecords;
  94. try {
  95. failRecords = studentApplyService.importPreExam(user, teachingId, level, file.getInputStream());
  96. } catch (IOException e) {
  97. throw new StatusException("文件读取出错", e);
  98. }
  99. Map<String, Object> map = new HashMap<>();
  100. map.put("hasError", CollectionUtils.isNotEmpty(failRecords));
  101. map.put("failRecords", failRecords);
  102. return map;
  103. }
  104. @ApiOperation(value = "一键自动分配")
  105. @PostMapping(value = "/std/auto/assign")
  106. public String autoAssign(@ApiParam("任务ID") @RequestParam Long taskId) {
  107. if (taskId == null) {
  108. throw new StatusException("请选择预约任务");
  109. }
  110. LoginUser user = this.curLoginUser();
  111. if (!Role.ADMIN.equals(user.getRole())) {
  112. throw new StatusException("没有权限");
  113. }
  114. studentApplyService.autoAssign(taskId, user.getId());
  115. return Constants.ASYNC_TIPS;
  116. }
  117. @ApiOperation(value = "打印签到表")
  118. @PostMapping(value = "/std/auto/sign/in/print")
  119. public void printSignIn(@ApiParam("教学点ID") @RequestParam(required = false) Long teachingId,
  120. @ApiParam("考点ID") @RequestParam(required = false) Long agentId,
  121. @ApiParam("考试日期") @RequestParam Long examDate) {
  122. LoginUser user = this.curLoginUser();
  123. if (Role.ADMIN.equals(user.getRole()) && teachingId == null) {
  124. throw new StatusException("请选择教学点");
  125. }
  126. if (Role.TEACHING.equals(user.getRole())) {
  127. teachingId = user.getCategoryId();
  128. }
  129. File signInZip = studentApplyService.downloadSignIn(teachingId, agentId, examDate);
  130. exportFile(signInZip.getName(), signInZip);
  131. signInZip.delete();
  132. }
  133. @Aac(strict = false, auth = false)
  134. @ApiOperation(value = "自动排考")
  135. @PostMapping(value = "/std/auto/layout")
  136. public void autoLayout(@ApiParam("教学点ID") @RequestParam(required = false) Long teachingId,
  137. @ApiParam("验证密码") @RequestParam String password) {
  138. String now = DateUtil.getShortDateWithoutSplitByLongTime(System.currentTimeMillis());
  139. String verifyPassword = DigestUtils.md5Hex(now);
  140. if (!password.equals(verifyPassword)) {
  141. throw new StatusException("验证失败");
  142. }
  143. studentApplyService.autoLayout(teachingId);
  144. }
  145. @ApiOperation(value = "可打印签到表的日期")
  146. @PostMapping(value = "/sign/in/date")
  147. public List<SignInVO> listSignInDate(@ApiParam("预约任务ID") @RequestParam(required = false) Long taskId) {
  148. return studentApplyService.listSignInDate(taskId);
  149. }
  150. @ApiOperation(value = "导出考场预约情况表")
  151. @PostMapping(value = "/export/teaching/available")
  152. public void exportApplyAvailable(@ApiParam("教学点ID") @RequestParam Long teachingId, HttpServletResponse response) {
  153. try {
  154. String fileName = URLEncoder.encode("考点预约情况表", "UTF-8");
  155. response.setHeader("Content-Disposition", "inline; filename=" + fileName + ".xlsx");
  156. response.setContentType("application/vnd.ms-excel");
  157. ExcelWriter writer = ExcelWriter.create(ExcelType.XLSX);
  158. List<CategoryVO> examSiteList = examSiteService.listExamSite(teachingId, Boolean.TRUE);
  159. if(examSiteList == null || examSiteList.isEmpty()) {
  160. throw new StatusException("当前教学点下没有考点");
  161. }
  162. String[] columnNames = getColumnNames(examSiteList);
  163. String[] firstLineContents = getFirstLineContents(examSiteList);
  164. List<String[]> exportList = new ArrayList<>();
  165. exportList.add(firstLineContents);
  166. List<String[]> avaiableList = studentApplyService.listExamSiteAvailable(examSiteList);
  167. exportList.addAll(avaiableList);
  168. writer.writeDataArrays("考点预约情况表", null, columnNames, exportList.iterator());
  169. writer.output(response.getOutputStream());
  170. } catch (IOException e) {
  171. throw new StatusException(e.getMessage());
  172. }
  173. }
  174. private String[] getFirstLineContents(List<CategoryVO> examSiteList) {
  175. String[] result = new String[examSiteList.size()+1];
  176. result[0] = "考点容量";
  177. for (int i = 0; i < examSiteList.size(); i++) {
  178. CategoryVO category = examSiteList.get(i);
  179. result[i+1] = String.valueOf(category.getCapacity());
  180. }
  181. return result;
  182. }
  183. private String[] getColumnNames(List<CategoryVO> examSiteList) {
  184. String[] columnNames = new String[examSiteList.size()+1];
  185. columnNames[0] = "时段";
  186. for (int i = 0; i < examSiteList.size(); i++) {
  187. CategoryVO category = examSiteList.get(i);
  188. columnNames[i+1] = category.getName();
  189. }
  190. return columnNames;
  191. }
  192. @ApiOperation(value = "导出考生预约的详情信息")
  193. @PostMapping(value = "/detail/export")
  194. public String exportStudentApplyDetail(@RequestBody StudentApplyReq req) {
  195. LoginUser user = curLoginUser();
  196. // 教学点管理员
  197. if (Role.TEACHING.equals(user.getRole())) {
  198. req.setTeachingId(user.getCategoryId());
  199. }
  200. studentApplyService.exportStudentApplyDetail(req, user.getId());
  201. return Constants.ASYNC_TIPS;
  202. }
  203. @ApiOperation(value = "导出未预约的考生-异步执行")
  204. @PostMapping(value = "/export/std/noApply")
  205. public String exportNoApplyStudent(@ApiParam("教学点ID") @RequestParam Long teachingId) {
  206. /* 同步执行
  207. try {
  208. String fileName = URLEncoder.encode("未完成预约考生信息表", "UTF-8");
  209. response.setHeader("Content-Disposition", "inline; filename=" + fileName + ".xlsx");
  210. response.setContentType("application/vnd.ms-excel");
  211. ExcelWriter writer = ExcelWriter.create(ExcelType.XLSX);
  212. List<StudentExport> studentList = studentApplyService.listStudentNoApply(teachingId);
  213. if(CollectionUtils.isEmpty(studentList)) {
  214. throw new StatusException("该教学点下的考生已全部完成预约");
  215. }
  216. writer.writeObjects("未完成预约考生", null, StudentExport.class, studentList.iterator());
  217. writer.output(response.getOutputStream());
  218. } catch (IOException e) {
  219. throw new StatusException(e.getMessage());
  220. }*/
  221. LoginUser user = curLoginUser();
  222. studentApplyService.exportNoApplyStudent(teachingId, user.getId());
  223. return Constants.ASYNC_TIPS;
  224. }
  225. }