dao-term.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "time"
  10. "github.com/jmoiron/sqlx"
  11. )
  12. func AddTerm(t *entity.Term) *result.Result {
  13. sqlStr := "insert into term(name,create_at,update_at) values (?,?,?)"
  14. res, err := config.DB.Exec(sqlStr, t.Name, 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 TermDetail(id string) *result.Result {
  22. sqlStr := "select * from term where id=?"
  23. var t entity.Term
  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 TermList(page, size int, sort string, _ map[string]interface{}) *result.Result {
  34. sqlStr := `SELECT
  35. t.*,
  36. count( u.id ) user_num
  37. FROM
  38. term t
  39. LEFT JOIN class c ON t.id = c.term_id
  40. LEFT JOIN user u ON c.id = u.class_id and u.delete_at is null
  41. WHERE
  42. t.delete_at IS NULL
  43. GROUP BY
  44. t.id
  45. ORDER BY
  46. t.create_at `
  47. if sort == "asc" {
  48. sqlStr += "asc "
  49. } else {
  50. sqlStr += "desc "
  51. }
  52. sqlStr += "limit ?,?"
  53. var tv []*vo.TermVo
  54. err := config.DB.Select(&tv, sqlStr, utils.PostionSize(page, size), size)
  55. if err != nil {
  56. return result.UNKNOW_ERROR.SetMsg(err.Error())
  57. }
  58. for _, t := range tv {
  59. var c []*vo.ClassVo
  60. sqlStr = "select c.*,count(u.id) user_num from class c left join user u on c.id=u.class_id and u.delete_at is null where c.delete_at is null and c.term_id=? group by c.id"
  61. err = config.DB.Select(&c, sqlStr, t.Id)
  62. if err != nil {
  63. return result.UNKNOW_ERROR.SetMsg(err.Error())
  64. }
  65. t.Classes = c
  66. }
  67. var count int
  68. sqlStr = "select count(id) from term where delete_at is null"
  69. err = config.DB.Get(&count, sqlStr)
  70. if err != nil {
  71. return result.UNKNOW_ERROR.SetMsg(err.Error())
  72. }
  73. return result.SUCCESS.SetData(result.NewPage(page, len(tv), count, tv))
  74. }
  75. func UpdateTerm(t *entity.Term) *result.Result {
  76. sqlStr := "update term set name=?,update_at=? where id=? and delete_at is null "
  77. res, err := config.DB.Exec(sqlStr, t.Name, time.Now(), t.Id)
  78. if err != nil {
  79. return result.UNKNOW_ERROR.SetData(err)
  80. }
  81. affected, _ := res.RowsAffected()
  82. return result.SuccessResult(result.NewResultChange(affected))
  83. }
  84. func DeleteTerms(ids []string) *result.Result {
  85. sqlStr := "update term t left join class c on t.id = c.term_id left join user u on c.id = u.class_id set t.delete_at=?,c.delete_at=?,u.delete_at=? where t.id in (?)"
  86. query, param, err := sqlx.In(sqlStr, time.Now(), time.Now(), time.Now(), ids)
  87. if err != nil {
  88. return result.UNKNOW_ERROR.SetMsg(err.Error())
  89. }
  90. res, err := config.DB.Exec(query, param...)
  91. if err != nil {
  92. return result.UNKNOW_ERROR.SetMsg(err.Error())
  93. }
  94. affected, _ := res.RowsAffected()
  95. return result.SUCCESS.SetData(result.NewResultChange(affected))
  96. }