question.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package train
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "zhiyuan/pkg/app"
  7. "zhiyuan/pkg/db"
  8. "zhiyuan/pkg/utils"
  9. "zhiyuan/services/form"
  10. "zhiyuan/services/train/course"
  11. "zhiyuan/services/train/question"
  12. "github.com/gin-gonic/gin"
  13. "github.com/tealeg/xlsx/v3"
  14. )
  15. func QuestionList(c *gin.Context) {
  16. course_id := utils.ToInt(c.Query("course_id"))
  17. if course_id <= 0 {
  18. app.Error(c, "课程id有误")
  19. return
  20. }
  21. courseInfo, err := course.GetOne(map[string]interface{}{"id": course_id}, []string{"`id`", "`name`", "`intro`", "`role_ids`", "`show`"}, nil)
  22. if err != nil {
  23. app.Error(c, err.Error())
  24. return
  25. }
  26. page := app.HandlePageNums(c)
  27. where := map[string]string{
  28. "where": "`zy_question`.`id`>0 AND `zy_question`.`deleted_at`=0",
  29. "_group_by": "`zy_question`.`id`",
  30. "_order_by": "`zy_question`.`id` desc",
  31. }
  32. if page.PageSize != 0 {
  33. where["_page_size"] = utils.ToStr(page.PageSize)
  34. where["_page_num"] = utils.ToStr(page.PageNum)
  35. }
  36. param := make(map[string]interface{})
  37. where["where"] = where["where"] + " AND `zy_question`.`course_id` = {{course_id}}"
  38. param["course_id"] = course_id
  39. content := c.Query("content")
  40. if content != "" {
  41. where["where"] = where["where"] + " AND `zy_question`.`content` LIKE {{content}}"
  42. param["content"] = "%" + content + "%"
  43. }
  44. total, err := question.CountRaw(where["where"], param)
  45. if err != nil {
  46. app.Error(c, err.Error())
  47. return
  48. }
  49. type QuestionList struct {
  50. ID int `json:"id"`
  51. CourseId int `json:"course_id"`
  52. Type int `json:"type"`
  53. Content string `json:"content"`
  54. Options string `json:"options"`
  55. Answer string `json:"answer"`
  56. Show int `json:"show"`
  57. Checks int `json:"checks"`
  58. Rights int `json:"rights"`
  59. CreatedAt string `json:"created_at"`
  60. UpdatedAt int `json:"updated_at"`
  61. }
  62. questionList := make([]QuestionList, 0)
  63. if err = db.GetMultiRaw("SELECT `zy_question`.*, COUNT(`zy_checks`.`id`) as `checks`, COUNT(`zy_checks`.`answer` = `zy_question`.`answer` or NULL) as `rights` FROM `zy_question` left join `zy_checks` on `zy_checks`.`question_id` = `zy_question`.`id`", where, param, &questionList); err != nil {
  64. app.Error(c, err.Error())
  65. return
  66. }
  67. for k, v := range questionList {
  68. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  69. questionList[k] = v
  70. }
  71. data := gin.H{
  72. "courseInfo": courseInfo,
  73. "list": questionList,
  74. "total": total,
  75. "limit": page.PageSize,
  76. }
  77. app.Success(c, data)
  78. }
  79. func QuestionAdd(c *gin.Context) {
  80. var addForm form.QuestionAdd
  81. if app.Bind(c, &addForm) != nil {
  82. return
  83. }
  84. id, err := question.Add(addForm)
  85. if err != nil {
  86. app.Error(c, err.Error())
  87. return
  88. }
  89. app.Success(c, gin.H{"id": id})
  90. }
  91. func QuestionEdit(c *gin.Context) {
  92. id := utils.ToInt(c.Param("id"))
  93. if id <= 0 {
  94. app.ErrorMsg(c, "question id must be a number", nil)
  95. return
  96. }
  97. var addForm form.QuestionAdd
  98. if app.Bind(c, &addForm) != nil {
  99. return
  100. }
  101. err := question.EditByID(addForm, id)
  102. if err != nil {
  103. app.ErrorMsg(c, err.Error(), nil)
  104. return
  105. }
  106. app.Success(c, nil)
  107. }
  108. func QuestionDel(c *gin.Context) {
  109. id := utils.ToInt(c.Param("id"))
  110. if id <= 0 {
  111. app.ErrorMsg(c, "question id must be a number", nil)
  112. return
  113. }
  114. err := question.DeleteByID(id)
  115. if err != nil {
  116. app.Error(c, err.Error())
  117. return
  118. }
  119. app.Success(c, nil)
  120. }
  121. func QuestionImport(c *gin.Context) {
  122. course_id := utils.ToInt(c.Param("id"))
  123. if course_id <= 0 {
  124. app.ErrorMsg(c, "question id must be a number", nil)
  125. return
  126. }
  127. file, err := c.FormFile("file")
  128. if err != nil {
  129. app.Error(c, fmt.Sprintf("get form err: %s", err.Error()))
  130. return
  131. }
  132. fileExt := utils.FileExt(file.Filename)
  133. attachKey := utils.RandomStr() + fileExt
  134. file.Filename = "data/" + attachKey
  135. if err := c.SaveUploadedFile(file, file.Filename); err != nil {
  136. app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error()))
  137. return
  138. }
  139. wb, err := xlsx.OpenFile(file.Filename)
  140. if err != nil {
  141. app.Error(c, err.Error())
  142. return
  143. }
  144. sh, ok := wb.Sheet["Sheet1"]
  145. if !ok {
  146. fmt.Println("Sheet does not exist")
  147. return
  148. }
  149. courseInfo, _ := course.GetInfoByID(course_id, nil, nil)
  150. if courseInfo == nil {
  151. app.ErrorMsg(c, "invalid id", nil)
  152. return
  153. }
  154. sh.ForEachRow(func(r *xlsx.Row) error {
  155. if r.GetCoordinate() <= 0 {
  156. return nil
  157. }
  158. types := r.GetCell(0).String()
  159. content := r.GetCell(1).String()
  160. if content == "" {
  161. return nil
  162. }
  163. if types == "选择" {
  164. answer := 0
  165. answerText := r.GetCell(2).String()
  166. options := make([]string, 0)
  167. for n := 3; r.GetCell(n).String() != ""; n++ {
  168. if answerText == r.GetCell(n).String() {
  169. answer = n - 2
  170. }
  171. options = append(options, r.GetCell(n).String())
  172. }
  173. _, err := question.Add(form.QuestionAdd{
  174. CourseId: course_id,
  175. Type: 0,
  176. Content: content,
  177. Options: options,
  178. Answer: strconv.Itoa(answer),
  179. Show: 1,
  180. })
  181. if err != nil {
  182. app.Error(c, err.Error())
  183. return err
  184. }
  185. } else {
  186. answers := make([]string, 0)
  187. for n := 2; r.GetCell(n).String() != ""; n++ {
  188. answerText := r.GetCell(n).String()
  189. answers = append(answers, answerText)
  190. }
  191. answer, err := json.Marshal(answers)
  192. if err != nil {
  193. app.Error(c, err.Error())
  194. return err
  195. }
  196. _, err = question.Add(form.QuestionAdd{
  197. CourseId: course_id,
  198. Type: 1,
  199. Content: content,
  200. Answer: string(answer),
  201. Show: 1,
  202. })
  203. if err != nil {
  204. app.Error(c, err.Error())
  205. return err
  206. }
  207. }
  208. return nil
  209. })
  210. app.Success(c, nil)
  211. }