router-exam-record.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package router
  2. import (
  3. "exam_system/dao"
  4. "exam_system/entity"
  5. "exam_system/result"
  6. "exam_system/utils"
  7. "exam_system/vo"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "github.com/gin-gonic/gin"
  12. "github.com/xuri/excelize/v2"
  13. )
  14. func ExamRecord(router *RouterPlus) {
  15. // 添加考试记录管理(开始考试)
  16. router.POST("/examRecord", AddExamRecord)
  17. // 提交答案
  18. router.PUT("/examRecord", UpdateExamRecord)
  19. // 获取考试记录列表
  20. router.GET("/examRecord/list", ExamRecordList)
  21. // 获取考试记录
  22. router.GET("/examRecord/:id", GetExamRecord)
  23. // 导出考试记录
  24. //router.router.GET("/examRecord/download",)
  25. r := router.Group("/admin")
  26. {
  27. // 获取考试记录管理
  28. r.GET("/examRecord/:id", AdminGetExamRecord)
  29. // 获取考试记录管理列表
  30. r.GET("/examRecord/list", AdminExamRecordList)
  31. // 删除考试记录管理
  32. // r.DELETE("/examRecord/:ids", AdminDeleteExamRecord)
  33. // 导出考试记录
  34. r.routerGroup.GET("/examRecord/download/:id", AdminDownloadExamRecord)
  35. }
  36. }
  37. func AddExamRecord(c *gin.Context) *result.Result {
  38. var examRecord entity.ExamRecord
  39. err := c.ShouldBindJSON(&examRecord)
  40. id, exists := c.Get("id")
  41. if !exists {
  42. return result.UNAUTHORIZED
  43. }
  44. examRecord.UserId = id.(int)
  45. if err != nil || examRecord.Id == 0 {
  46. return result.PARAM_ERROR
  47. }
  48. examRecord.Ip = strings.Split(c.Request.RemoteAddr, ":")[0]
  49. return dao.AddExamRecord(&examRecord)
  50. }
  51. func ExamRecordList(c *gin.Context) *result.Result {
  52. page, size, _, query, err := utils.Page(c)
  53. if err != nil {
  54. return result.PARAM_ERROR
  55. }
  56. uid, exists := c.Get("id")
  57. if !exists {
  58. return result.UNAUTHORIZED
  59. }
  60. return dao.ExamRecordList(page, size, query, uid.(int))
  61. }
  62. func GetExamRecord(c *gin.Context) *result.Result {
  63. idStr := c.Param("id")
  64. if idStr == "" {
  65. return result.UNKNOW_ERROR
  66. }
  67. id, err := strconv.Atoi(idStr)
  68. if err != nil {
  69. return result.UNKNOW_ERROR
  70. }
  71. uid, exists := c.Get("id")
  72. if !exists {
  73. return result.UNAUTHORIZED
  74. }
  75. return dao.ExamRecordDetail(id, uid.(int))
  76. }
  77. func UpdateExamRecord(c *gin.Context) *result.Result {
  78. var examRecord vo.ExamRecordVo
  79. err := c.ShouldBindJSON(&examRecord)
  80. if err != nil || examRecord.Id == 0 || examRecord.AnswerVo == nil || examRecord.Token == "" {
  81. return result.PARAM_ERROR
  82. }
  83. return dao.UpdateExamRecord(&examRecord)
  84. }
  85. func AdminGetExamRecord(c *gin.Context) *result.Result {
  86. idStr := c.Param("id")
  87. if idStr == "" {
  88. return result.UNKNOW_ERROR
  89. }
  90. id, err := strconv.Atoi(idStr)
  91. if err != nil {
  92. return result.UNKNOW_ERROR
  93. }
  94. return dao.AdminExamRecordDetail(id)
  95. }
  96. func AdminExamRecordList(c *gin.Context) *result.Result {
  97. page, size, _, query, err := utils.Page(c)
  98. examIdStr := c.Query("exam_id")
  99. if err != nil {
  100. return result.PARAM_ERROR
  101. }
  102. examId, _ := strconv.Atoi(examIdStr)
  103. return dao.AdminExamRecordList(page, size, query, examId)
  104. }
  105. func AdminDeleteExamRecord(c *gin.Context) *result.Result {
  106. idStr := c.Param("ids")
  107. if idStr == "" {
  108. return result.PARAM_ERROR
  109. }
  110. ids := strings.Split(idStr, ",")
  111. return dao.DeleteExamRecords(ids)
  112. }
  113. func AdminDownloadExamRecord(c *gin.Context) {
  114. idStr := c.Param("id")
  115. file, err := excelize.OpenFile(utils.ScorePath)
  116. if err != nil {
  117. c.JSON(http.StatusOK, result.UNKNOW_ERROR.SetMsg(err.Error()))
  118. }
  119. res := dao.AdminDownloadExamRecord(file, idStr)
  120. if res.Code != result.SUCCESS.Code {
  121. c.JSON(http.StatusOK, res)
  122. }
  123. c.Header("Content-Type", "application/octet-stream")
  124. c.Header("Content-Disposition", "attachment; filename=考生信息导出.xlsx")
  125. c.Header("Content-Transfer-Encoding", "binary")
  126. _ = file.Write(c.Writer)
  127. }