dao-question-bank.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "time"
  11. )
  12. func AddQuestionBank(t *entity.QuestionBank) *result.Result {
  13. sqlStr := "insert into question_bank(name,status,description,create_at,update_at) values (?,?,?,?,?)"
  14. res, err := config.DB.Exec(sqlStr, t.Name, t.Status, t.Description, time.Now(), time.Now())
  15. if err != nil {
  16. return result.UNKNOW_ERROR.SetData(err)
  17. }
  18. id, _ := res.LastInsertId()
  19. return result.SuccessResult(id)
  20. }
  21. func QuestionBankDetail(id string) *result.Result {
  22. sqlStr := "select q.*,count(s.id) sub_num from question_bank q left join subject s on q.id=s.qb_id where q.id=? and q.delete_at is null group by q.id"
  23. var t vo.QuestionBankVo
  24. err := config.DB.Get(&t, sqlStr, id)
  25. if err != nil {
  26. if err == sql.ErrNoRows {
  27. return result.DATA_NOT_FOUND
  28. }
  29. return result.UNKNOW_ERROR.SetData(err.Error())
  30. }
  31. return result.SuccessResult(&t)
  32. }
  33. func QuestionBankList(page, size int, sort string, query map[string]interface{}) *result.Result {
  34. // 1、查询列表
  35. sqlStr := "select q.*,count( s.id ) sub_num from question_bank q left join subject s on q.id=s.qb_id and s.delete_at is null where q.delete_at is null "
  36. params := make([]interface{}, 0)
  37. if query["status"] != nil {
  38. sqlStr += "and q.status = ? "
  39. params = append(params, query["status"])
  40. }
  41. if query["key"] != nil {
  42. sqlStr += "and q.name like ? "
  43. params = append(params, "%"+query["key"].(string)+"%")
  44. }
  45. sqlStr += "group by q.id order by create_at "
  46. if sort == "desc" {
  47. sqlStr += "desc "
  48. }
  49. sqlStr += "limit ?,?"
  50. params = append(params, utils.PostionSize(page,size), size)
  51. var ts []*vo.QuestionBankVo
  52. err := config.DB.Select(&ts, sqlStr, params...)
  53. if err != nil {
  54. return result.UNKNOW_ERROR.SetMsg(err.Error())
  55. }
  56. // 3、查询所有
  57. totalStr := "select count(id) from question_bank where delete_at is null "
  58. params = make([]interface{}, 0)
  59. if query["status"] != nil {
  60. totalStr += "and status = ? "
  61. params = append(params, query["status"])
  62. }
  63. if query["key"] != nil {
  64. totalStr += "and name like ? "
  65. params = append(params, "%"+query["key"].(string))
  66. }
  67. var total int
  68. err = config.DB.Get(&total, totalStr, params...)
  69. if err != nil {
  70. return result.UNKNOW_ERROR.SetMsg(err.Error())
  71. }
  72. return result.SUCCESS.SetData(result.NewPage(page,len(ts),total,ts))
  73. }
  74. func UpdateQuestionBank(q *entity.QuestionBank) *result.Result {
  75. sqlStr := "update question_bank set name=?,description=?,status=?,update_at=? where id=? and delete_at is null "
  76. res, err := config.DB.Exec(sqlStr, q.Name, q.Description, q.Status, time.Now(), q.Id)
  77. if err != nil {
  78. return result.UNKNOW_ERROR.SetData(err)
  79. }
  80. affected, _ := res.RowsAffected()
  81. return result.SuccessResult(result.NewResultChange(affected))
  82. }
  83. func DeleteQuestionBanks(ids []string) *result.Result {
  84. parm := []interface{}{time.Now()}
  85. parmStr := ""
  86. for k, id := range ids {
  87. parm = append(parm, id)
  88. if k == (len(ids) - 1) {
  89. parmStr += "?"
  90. } else {
  91. parmStr += "?,"
  92. }
  93. }
  94. sqlStr := fmt.Sprintf("update question_bank set delete_at=? where id in (%s)", parmStr)
  95. res, err := config.DB.Exec(sqlStr, parm...)
  96. if err != nil {
  97. return result.UNKNOW_ERROR.SetMsg(err.Error())
  98. }
  99. affected, _ := res.RowsAffected()
  100. return result.SUCCESS.SetData(result.NewResultChange(affected))
  101. }