dao-subject.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package dao
  2. import (
  3. "database/sql"
  4. "exam_system/config"
  5. "exam_system/entity"
  6. "exam_system/result"
  7. "exam_system/utils"
  8. "exam_system/vo"
  9. "fmt"
  10. "mime/multipart"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/jmoiron/sqlx"
  15. "github.com/xuri/excelize/v2"
  16. )
  17. func AddSubject(s *entity.Subject) *result.Result {
  18. sqlStr := "insert into subject(question,type,opt_a,opt_b,opt_c,opt_d,answer,analysis,status,qb_id,create_at,update_at) values (?,?,?,?,?,?,?,?,?,?,?,?)"
  19. res, err := config.DB.Exec(sqlStr, s.Question, s.Type, s.OptA, s.OptB, s.OptC, s.OptD, utils.SubjectSort(s.Answer), s.Analysis, s.Status, s.QbId, time.Now(), time.Now())
  20. if err != nil {
  21. return result.UNKNOW_ERROR.SetData(err)
  22. }
  23. id, _ := res.LastInsertId()
  24. return result.SuccessResult(id)
  25. }
  26. func SubjectDetail(id string) *result.Result {
  27. sqlStr := "select * from subject where id=? and delete_at is null"
  28. var t entity.Subject
  29. err := config.DB.Get(&t, sqlStr, id)
  30. if err != nil {
  31. if err == sql.ErrNoRows {
  32. return result.DATA_NOT_FOUND
  33. }
  34. return result.UNKNOW_ERROR.SetData(err.Error())
  35. }
  36. return result.SuccessResult(&t)
  37. }
  38. func SubjectList(page, size int, sort string, query map[string]interface{}) *result.Result {
  39. // 1、获取列表
  40. parms := make([]interface{}, 0)
  41. sqlStr := "select * from subject where delete_at is null "
  42. if query["key"] != nil {
  43. sqlStr += "and question like ? "
  44. parms = append(parms, "%"+query["key"].(string))
  45. }
  46. if query["qb_id"] != nil {
  47. sqlStr += "and qb_id = ? "
  48. parms = append(parms, query["qb_id"])
  49. }
  50. if query["status"] != nil {
  51. sqlStr += "and status=? "
  52. parms = append(parms, query["status"])
  53. }
  54. if query["type"] != nil {
  55. sqlStr += "and type=? "
  56. parms = append(parms, query["type"])
  57. }
  58. sqlStr += "order by create_at "
  59. if sort == "desc" {
  60. sqlStr += "desc "
  61. } else {
  62. sqlStr += "asc "
  63. }
  64. sqlStr += "limit ?,?"
  65. parms = append(parms, utils.PostionSize(page, size), size)
  66. var ts []*entity.Subject
  67. err := config.DB.Select(&ts, sqlStr, parms...)
  68. if err != nil {
  69. return result.UNKNOW_ERROR.SetMsg(err.Error())
  70. }
  71. // 2、获取元素个数
  72. sqlStr = "select count(id) from subject where delete_at is null "
  73. parms = make([]interface{}, 0)
  74. if query["key"] != nil {
  75. sqlStr += "and question like ? "
  76. parms = append(parms, "%"+query["key"].(string))
  77. }
  78. if query["qb_id"] != nil {
  79. sqlStr += "and qb_id = ? "
  80. parms = append(parms, query["qb_id"])
  81. }
  82. if query["status"] != nil {
  83. sqlStr += "and status=? "
  84. parms = append(parms, query["status"])
  85. }
  86. if query["type"] != nil {
  87. sqlStr += "and type=? "
  88. parms = append(parms, query["type"])
  89. }
  90. var total int
  91. err = config.DB.Get(&total, sqlStr, parms...)
  92. if err != nil {
  93. return result.UNKNOW_ERROR.SetMsg(err.Error())
  94. }
  95. return result.SUCCESS.SetData(result.NewPage(page, len(ts), total, ts))
  96. }
  97. func UpdateSubject(s *entity.Subject) *result.Result {
  98. sqlStr := "update subject set question=?,type=?,opt_a=?,opt_b=?,opt_c=?,opt_d=?,answer=?,analysis=?,status=?,qb_id=?,update_at=? where id=? and delete_at is null "
  99. res, err := config.DB.Exec(sqlStr, s.Question, s.Type, s.OptA, s.OptB, s.OptC, s.OptD, utils.SubjectSort(s.Answer), s.Analysis, s.Status, s.QbId, time.Now(), s.Id)
  100. if err != nil {
  101. return result.UNKNOW_ERROR.SetData(err)
  102. }
  103. affected, _ := res.RowsAffected()
  104. return result.SuccessResult(result.NewResultChange(affected))
  105. }
  106. func DeleteSubjects(ids []string) *result.Result {
  107. parm := []interface{}{time.Now()}
  108. parmStr := ""
  109. for k, id := range ids {
  110. parm = append(parm, id)
  111. if k == (len(ids) - 1) {
  112. parmStr += "?"
  113. } else {
  114. parmStr += "?,"
  115. }
  116. }
  117. sqlStr := fmt.Sprintf("update subject set delete_at=? where id in (%s)", parmStr)
  118. res, err := config.DB.Exec(sqlStr, parm...)
  119. if err != nil {
  120. return result.UNKNOW_ERROR.SetMsg(err.Error())
  121. }
  122. affected, _ := res.RowsAffected()
  123. return result.SUCCESS.SetData(result.NewResultChange(affected))
  124. }
  125. func AddSubjectBatch(file multipart.File, id string) *result.Result {
  126. xlsFile, err := excelize.OpenReader(file)
  127. if err != nil {
  128. return result.UNKNOW_ERROR.SetMsg(err.Error())
  129. }
  130. typeName, err := xlsFile.GetCellValue("Sheet", "A1")
  131. typeName = strings.TrimSpace(typeName)
  132. if err != nil {
  133. return result.UNKNOW_ERROR.SetMsg(err.Error())
  134. }
  135. // 1.设置插入类型
  136. tp := 0
  137. switch typeName {
  138. case "单选题":
  139. tp = entity.SINGEL_CHOICE
  140. case "填空题":
  141. tp = entity.Completion
  142. case "判断题":
  143. tp = entity.Judgement
  144. case "多选题":
  145. tp = entity.MULTIPLE_CHOICE
  146. default:
  147. return result.UNKNOW_ERROR.SetMsg("excel格式错误")
  148. }
  149. tx, err := config.DB.Beginx()
  150. if err != nil {
  151. return result.UNKNOW_ERROR.SetMsg(err.Error())
  152. }
  153. return utils.Transation(tx, err, func(_ *sqlx.Tx) *result.Result {
  154. // 1.sql准备
  155. sqlStr := "insert into subject(question,type,opt_a,opt_b,opt_c,opt_d,answer,analysis,status,qb_id,create_at,update_at) values (?,?,?,?,?,?,?,?,?,?,?,?)"
  156. var stmt *sql.Stmt
  157. stmt, err = config.DB.Prepare(sqlStr)
  158. if err != nil {
  159. return result.UNKNOW_ERROR.SetMsg(err.Error())
  160. }
  161. var count int64
  162. i := 3
  163. for {
  164. question, _ := xlsFile.GetCellValue("Sheet", "A"+strconv.Itoa(i))
  165. var optA, optB, optC, optD, answer string
  166. if typeName == "判断题" {
  167. answer, _ = xlsFile.GetCellValue("Sheet", "B"+strconv.Itoa(i))
  168. } else {
  169. optA, _ = xlsFile.GetCellValue("Sheet", "B"+strconv.Itoa(i))
  170. optB, _ = xlsFile.GetCellValue("Sheet", "C"+strconv.Itoa(i))
  171. optC, _ = xlsFile.GetCellValue("Sheet", "D"+strconv.Itoa(i))
  172. optD, _ = xlsFile.GetCellValue("Sheet", "E"+strconv.Itoa(i))
  173. answer, _ = xlsFile.GetCellValue("Sheet", "F"+strconv.Itoa(i))
  174. answer = utils.SubjectFormat(answer)
  175. }
  176. if question == "" {
  177. break
  178. }
  179. var res sql.Result
  180. res, err = stmt.Exec(question, tp, optA, optB, optC, optD, answer, "", entity.USE, id, time.Now(), time.Now())
  181. if err != nil {
  182. continue
  183. }
  184. affected, _ := res.RowsAffected()
  185. count += affected
  186. i++
  187. }
  188. return result.SuccessResult(result.NewResultChange(count))
  189. })
  190. }
  191. func DownloadSubject(file *excelize.File, id, tp int) *result.Result {
  192. sqlStr := "select s.*,q.name qb_name from subject s left join question_bank q on s.qb_id = q.id where q.id = ? and s.type =? and s.delete_at is null"
  193. var subs []*vo.SubjectVo
  194. err := config.DB.Select(&subs, sqlStr, id, tp)
  195. if err != nil {
  196. return result.UNKNOW_ERROR.SetMsg(err.Error())
  197. }
  198. // 题型
  199. name, err := entity.GetSubjectTypeName(tp)
  200. if err != nil {
  201. return result.UNKNOW_ERROR.SetMsg(err.Error())
  202. }
  203. if len(subs) == 0 {
  204. return result.SUCCESS.SetData("没有" + name)
  205. }
  206. i := 3
  207. for _, sub := range subs {
  208. file.SetCellValue("Sheet", "A"+strconv.Itoa(i), sub.Id)
  209. file.SetCellValue("Sheet", "B"+strconv.Itoa(i), sub.Question)
  210. if tp == entity.Judgement {
  211. file.SetCellValue("Sheet", "C"+strconv.Itoa(i), utils.SubjectParse(sub.Answer))
  212. } else {
  213. file.SetCellValue("Sheet", "C"+strconv.Itoa(i), sub.OptA)
  214. file.SetCellValue("Sheet", "D"+strconv.Itoa(i), sub.OptB)
  215. file.SetCellValue("Sheet", "E"+strconv.Itoa(i), sub.OptC)
  216. file.SetCellValue("Sheet", "F"+strconv.Itoa(i), sub.OptD)
  217. sub.Answer = utils.SubjectParse(sub.Answer)
  218. file.SetCellValue("Sheet", "G"+strconv.Itoa(i), utils.SubjectParse(sub.Answer))
  219. }
  220. i++
  221. }
  222. return result.SUCCESS.SetData(subs[0].QbName + "_" + name)
  223. }