StudentAdminController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.qmth.exam.reserve.controller.admin;
  2. import com.qmth.boot.api.annotation.Aac;
  3. import com.qmth.boot.api.constant.ApiConstant;
  4. import com.qmth.boot.core.collection.PageResult;
  5. import com.qmth.boot.core.exception.StatusException;
  6. import com.qmth.boot.tools.excel.ExcelWriter;
  7. import com.qmth.boot.tools.excel.enums.ExcelType;
  8. import com.qmth.exam.reserve.bean.Constants;
  9. import com.qmth.exam.reserve.bean.login.LoginUser;
  10. import com.qmth.exam.reserve.bean.student.StudentExportVO;
  11. import com.qmth.exam.reserve.bean.student.StudentReq;
  12. import com.qmth.exam.reserve.bean.student.StudentVO;
  13. import com.qmth.exam.reserve.controller.BaseController;
  14. import com.qmth.exam.reserve.enums.Role;
  15. import com.qmth.exam.reserve.service.StudentService;
  16. import io.swagger.annotations.Api;
  17. import io.swagger.annotations.ApiOperation;
  18. import io.swagger.annotations.ApiParam;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.web.bind.annotation.*;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.io.IOException;
  26. import java.net.URLEncoder;
  27. import java.util.List;
  28. import java.util.Objects;
  29. @RestController
  30. @Api(tags = "【管理端】考生相关接口")
  31. @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/admin/student")
  32. @Aac(strict = false, auth = true)
  33. public class StudentAdminController extends BaseController {
  34. private static final Logger log = LoggerFactory.getLogger(StudentAdminController.class);
  35. @Autowired
  36. private StudentService studentService;
  37. @ApiOperation(value = "考生管理分页")
  38. @PostMapping(value = "/page")
  39. public PageResult<StudentVO> page(@RequestBody StudentReq req) {
  40. LoginUser loginUser = this.curLoginUser();
  41. if(loginUser.getRole().equals(Role.TEACHING)) {
  42. req.setTeachingId(loginUser.getCategoryId());
  43. }
  44. return studentService.pageStudent(req);
  45. }
  46. @ApiOperation(value = "删除考生")
  47. @PostMapping(value = "/delete")
  48. public void delete(@ApiParam("考生ID") @RequestParam Long[] ids) {
  49. LoginUser loginUser = this.curLoginUser();
  50. if (loginUser.getRole().equals(Role.TEACHING)) {
  51. throw new StatusException("没有权限");
  52. }
  53. studentService.delete(ids, loginUser);
  54. }
  55. @ApiOperation(value = "修改考生可预约次数")
  56. @PostMapping(value = "/modify/apply/number")
  57. public void modifyApplyNumber(@ApiParam("考生ID") @RequestParam Long id, @ApiParam("可预约次数") @RequestParam Integer applyNumber) {
  58. LoginUser loginUser = this.curLoginUser();
  59. if (!loginUser.getRole().equals(Role.ADMIN)) {
  60. throw new StatusException("没有权限");
  61. }
  62. studentService.modifyApplyNumber(id, loginUser.getId(), applyNumber);
  63. }
  64. @ApiOperation(value = "上传考生的照片")
  65. @PostMapping(value = "upload/photo")
  66. public void uploadStudentPhoto(@RequestParam MultipartFile file) {
  67. LoginUser loginUser = this.curLoginUser();
  68. if (loginUser.getRole().equals(Role.TEACHING)) {
  69. throw new StatusException("没有权限");
  70. }
  71. String filename = file.getOriginalFilename();
  72. if (filename != null && !filename.toLowerCase().endsWith(".jpg") && !filename.toLowerCase().endsWith(".png")
  73. && !filename.toLowerCase().endsWith(".jpeg")) {
  74. throw new StatusException("请上传后缀为jpg、png或者jpeg的考生头像");
  75. }
  76. studentService.uploadStudentPhoto(loginUser.getId(), file);
  77. }
  78. @ApiOperation(value = "上传考生的照片-异步多文件上传")
  79. @PostMapping(value = "upload/student/photo")
  80. public String uploadStudentPhotoAsync(@RequestParam MultipartFile file) throws IOException {
  81. if (!Objects.requireNonNull(file.getOriginalFilename()).endsWith(".zip")) {
  82. throw new StatusException("请上传zip文件");
  83. }
  84. LoginUser loginUser = this.curLoginUser();
  85. studentService.uploadStudentPhotoAsync(loginUser.getId(), file);
  86. return Constants.ASYNC_TIPS;
  87. }
  88. @ApiOperation(value = "导出考生")
  89. @PostMapping(value = "/export")
  90. public void exportExceptionMessage(@RequestBody StudentReq req, HttpServletResponse response) {
  91. LoginUser loginUser = this.curLoginUser();
  92. if (loginUser.getRole().equals(Role.TEACHING)) {
  93. req.setTeachingId(loginUser.getCategoryId());
  94. }
  95. try {
  96. String fileName = URLEncoder.encode(System.currentTimeMillis() + "", "UTF-8");
  97. response.setHeader("Content-Disposition", "inline; filename=" + fileName + ".xlsx");
  98. response.setContentType("application/vnd.ms-excel");
  99. ExcelWriter writer = ExcelWriter.create(ExcelType.XLSX);
  100. List<StudentExportVO> exportList = studentService.exportPage(req);
  101. if (exportList == null || exportList.isEmpty()) {
  102. throw new StatusException("无考生信息");
  103. }
  104. writer.writeObjects("考生信息", null, StudentExportVO.class, exportList.iterator());
  105. writer.output(response.getOutputStream());
  106. } catch (IOException e) {
  107. log.error(e.getMessage());
  108. }
  109. }
  110. }