router-exam-record.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package router
  2. import (
  3. "encoding/json"
  4. "exam_system/config"
  5. "exam_system/dao"
  6. "exam_system/entity"
  7. "exam_system/result"
  8. "exam_system/utils"
  9. "exam_system/vo"
  10. "fmt"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/gin-gonic/gin"
  16. "github.com/nats-io/nats.go"
  17. "github.com/xuri/excelize/v2"
  18. )
  19. func ExamRecord(router *RouterPlus) {
  20. // 添加考试记录管理(开始考试)
  21. router.POST("/examRecord", AddExamRecord)
  22. // 提交答案
  23. router.PUT("/examRecord", UpdateExamRecord)
  24. // 获取考试记录列表
  25. router.GET("/examRecord/list", ExamRecordList)
  26. // 获取考试记录
  27. router.GET("/examRecord/:id", GetExamRecord)
  28. // 导出考试记录
  29. //router.router.GET("/examRecord/download",)
  30. r := router.Group("/admin")
  31. {
  32. // 获取考试记录管理
  33. r.GET("/examRecord/:id", AdminGetExamRecord)
  34. // 获取考试记录管理列表
  35. r.GET("/examRecord/list", AdminExamRecordList)
  36. // 删除考试记录管理
  37. // r.DELETE("/examRecord/:ids", AdminDeleteExamRecord)
  38. // 导出考试记录
  39. r.routerGroup.GET("/examRecord/download/:id", AdminDownloadExamRecord)
  40. }
  41. // go UpdateExamRecordTask()
  42. }
  43. func UpdateExamRecordTask() {
  44. config.JS.AddConsumer("EXAMS", &nats.ConsumerConfig{
  45. Durable: "MONITOR",
  46. })
  47. sub, err := config.JS.PullSubscribe("EXAMS.info", "MONITOR")
  48. if err != nil {
  49. fmt.Println(err)
  50. }
  51. for {
  52. time.Sleep(time.Second)
  53. // 分批从stream拉取消息一次10个
  54. msgs, err := sub.Fetch(100)
  55. if err != nil && err != nats.ErrTimeout {
  56. fmt.Println(err)
  57. }
  58. for _, msg := range msgs {
  59. var e entity.Exam
  60. err = json.Unmarshal(msg.Data, &e)
  61. if err != nil && err != nats.ErrTimeout {
  62. fmt.Println(err)
  63. }
  64. if e.EndAt.Before(time.Now()) {
  65. // 更新为
  66. res := dao.UpdateExamRecordTask(e.Id)
  67. if res.Code == result.SUCCESS.Code {
  68. err = msg.Ack()
  69. if err != nil {
  70. fmt.Println(err)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. func AddExamRecord(c *gin.Context) *result.Result {
  78. var examRecord entity.ExamRecord
  79. err := c.ShouldBindJSON(&examRecord)
  80. id, exists := c.Get("id")
  81. if !exists {
  82. return result.UNAUTHORIZED
  83. }
  84. examRecord.UserId = id.(int)
  85. if err != nil || examRecord.Id == 0 {
  86. return result.PARAM_ERROR
  87. }
  88. examRecord.Ip = strings.Split(c.Request.RemoteAddr, ":")[0]
  89. return dao.AddExamRecord(&examRecord)
  90. }
  91. func ExamRecordList(c *gin.Context) *result.Result {
  92. page, size, _, query, err := utils.Page(c)
  93. if err != nil {
  94. return result.PARAM_ERROR
  95. }
  96. uid, exists := c.Get("id")
  97. if !exists {
  98. return result.UNAUTHORIZED
  99. }
  100. return dao.ExamRecordList(page, size, query, uid.(int))
  101. }
  102. func GetExamRecord(c *gin.Context) *result.Result {
  103. idStr := c.Param("id")
  104. if idStr == "" {
  105. return result.UNKNOW_ERROR
  106. }
  107. id, err := strconv.Atoi(idStr)
  108. if err != nil {
  109. return result.UNKNOW_ERROR
  110. }
  111. uid, exists := c.Get("id")
  112. if !exists {
  113. return result.UNAUTHORIZED
  114. }
  115. return dao.ExamRecordDetail(id, uid.(int))
  116. }
  117. func UpdateExamRecord(c *gin.Context) *result.Result {
  118. var examRecord vo.ExamRecordVo
  119. err := c.ShouldBindJSON(&examRecord)
  120. if err != nil || examRecord.Id == 0 || examRecord.AnswerVo == nil || examRecord.Token == "" {
  121. return result.PARAM_ERROR
  122. }
  123. return dao.UpdateExamRecord(&examRecord)
  124. }
  125. func AdminGetExamRecord(c *gin.Context) *result.Result {
  126. idStr := c.Param("id")
  127. if idStr == "" {
  128. return result.UNKNOW_ERROR
  129. }
  130. id, err := strconv.Atoi(idStr)
  131. if err != nil {
  132. return result.UNKNOW_ERROR
  133. }
  134. return dao.AdminExamRecordDetail(id)
  135. }
  136. func AdminExamRecordList(c *gin.Context) *result.Result {
  137. page, size, _, query, err := utils.Page(c)
  138. examIdStr := c.Query("exam_id")
  139. if err != nil {
  140. return result.PARAM_ERROR
  141. }
  142. examId, _ := strconv.Atoi(examIdStr)
  143. return dao.AdminExamRecordList(page, size, query, examId)
  144. }
  145. func AdminDeleteExamRecord(c *gin.Context) *result.Result {
  146. idStr := c.Param("ids")
  147. if idStr == "" {
  148. return result.PARAM_ERROR
  149. }
  150. ids := strings.Split(idStr, ",")
  151. return dao.DeleteExamRecords(ids)
  152. }
  153. func AdminDownloadExamRecord(c *gin.Context) {
  154. idStr := c.Param("id")
  155. file, err := excelize.OpenFile(utils.ScorePath)
  156. if err != nil {
  157. c.JSON(http.StatusOK, result.UNKNOW_ERROR.SetMsg(err.Error()))
  158. }
  159. res := dao.AdminDownloadExamRecord(file, idStr)
  160. if res.Code != result.SUCCESS.Code {
  161. c.JSON(http.StatusOK, res)
  162. }
  163. c.Header("Content-Type", "application/octet-stream")
  164. c.Header("Content-Disposition", "attachment; filename=考生信息导出.xlsx")
  165. c.Header("Content-Transfer-Encoding", "binary")
  166. _ = file.Write(c.Writer)
  167. }