tips.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "go-common/app/admin/main/vip/model"
  7. "go-common/library/database/sql"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _tipsSQL = "SELECT `id`,`platform`,`version`,`tip`,`link`,`start_time`,`end_time`,`level`,`judge_type`,`operator`,`deleted`, `position`,`ctime`,`mtime`FROM `vip_tips` WHERE `deleted` = 0 "
  12. _tipsByIDSQL = "SELECT `id`,`platform`,`version`,`tip`,`link`,`start_time`,`end_time`,`level`,`judge_type`,`operator`,`deleted`, `position`,`ctime`,`mtime`FROM `vip_tips` WHERE `id` = ?;"
  13. _updateTipSQL = "UPDATE `vip_tips` SET `platform` = ?,`version` = ?,`tip` = ?,`link` = ?,`start_time` = ?,`end_time` = ?,`level` = ?, `judge_type` = ?, `operator` = ?, `position` = ? WHERE `id` = ?;"
  14. _addTipSQL = "INSERT INTO `vip_tips`(`platform`,`version`,`tip`,`link`,`start_time`,`end_time`,`level`,`judge_type`,`operator`,`deleted`,`ctime`, `position`)VALUES(?,?,?,?,?,?,?,?,?,?,?,?);"
  15. _deleteTipSQL = "UPDATE `vip_tips` SET `deleted` = ?,`operator` = ? WHERE `id` = ?;"
  16. _expireTipSQL = "UPDATE `vip_tips` SET `end_time` = ?,`operator` = ? WHERE `id` = ?;"
  17. )
  18. // TipList tips list.
  19. func (d *Dao) TipList(c context.Context, platform int8, state int8, now int64, position int8) (rs []*model.Tips, err error) {
  20. var (
  21. rows *sql.Rows
  22. sql = _tipsSQL
  23. )
  24. switch state {
  25. case model.WaitShowTips:
  26. sql += fmt.Sprintf(" AND `start_time` > %d ", now)
  27. case model.EffectiveTips:
  28. sql += fmt.Sprintf(" AND `start_time` < %d AND `end_time`> %d", now, now)
  29. case model.ExpireTips:
  30. sql += fmt.Sprintf(" AND `end_time` < %d ", now)
  31. }
  32. if platform != 0 {
  33. sql += fmt.Sprintf(" AND `platform` = %d ", platform)
  34. }
  35. if position != 0 {
  36. sql += fmt.Sprintf(" AND `position` = %d ", position)
  37. }
  38. if rows, err = d.db.Query(c, sql); err != nil {
  39. err = errors.WithStack(err)
  40. return
  41. }
  42. defer rows.Close()
  43. for rows.Next() {
  44. r := new(model.Tips)
  45. if err = rows.Scan(&r.ID, &r.Platform, &r.Version, &r.Tip, &r.Link, &r.StartTime, &r.EndTime, &r.Level, &r.JudgeType, &r.Operator, &r.Deleted, &r.Position, &r.Ctime, &r.Mtime); err != nil {
  46. rs = nil
  47. err = errors.WithStack(err)
  48. return
  49. }
  50. rs = append(rs, r)
  51. }
  52. if err = rows.Err(); err != nil {
  53. err = errors.WithStack(err)
  54. }
  55. return
  56. }
  57. // TipByID by id .
  58. func (d *Dao) TipByID(c context.Context, id int64) (r *model.Tips, err error) {
  59. res := d.db.QueryRow(c, _tipsByIDSQL, id)
  60. r = new(model.Tips)
  61. if err = res.Scan(&r.ID, &r.Platform, &r.Version, &r.Tip, &r.Link, &r.StartTime, &r.EndTime, &r.Level, &r.JudgeType, &r.Operator, &r.Deleted, &r.Position, &r.Ctime, &r.Mtime); err != nil {
  62. if err == sql.ErrNoRows {
  63. err = nil
  64. r = nil
  65. return
  66. }
  67. err = errors.WithStack(err)
  68. }
  69. return
  70. }
  71. // TipUpdate tip update.
  72. func (d *Dao) TipUpdate(c context.Context, t *model.Tips) (eff int64, err error) {
  73. var res xsql.Result
  74. if res, err = d.db.Exec(c, _updateTipSQL, t.Platform, t.Version, t.Tip, t.Link, t.StartTime, t.EndTime, t.Level, t.JudgeType, t.Operator, t.Position, t.ID); err != nil {
  75. err = errors.WithStack(err)
  76. return
  77. }
  78. if eff, err = res.RowsAffected(); err != nil {
  79. err = errors.WithStack(err)
  80. }
  81. return
  82. }
  83. // AddTip add tip.
  84. func (d *Dao) AddTip(c context.Context, t *model.Tips) (lid int64, err error) {
  85. var res xsql.Result
  86. if res, err = d.db.Exec(c, _addTipSQL, t.Platform, t.Version, t.Tip, t.Link, t.StartTime, t.EndTime, t.Level, t.JudgeType, t.Operator, t.Deleted, t.Ctime, t.Position); err != nil {
  87. err = errors.WithStack(err)
  88. return
  89. }
  90. if lid, err = res.LastInsertId(); err != nil {
  91. err = errors.WithStack(err)
  92. }
  93. return
  94. }
  95. // DeleteTip tip delete.
  96. func (d *Dao) DeleteTip(c context.Context, id int64, deleted int8, operator string) (eff int64, err error) {
  97. var res xsql.Result
  98. if res, err = d.db.Exec(c, _deleteTipSQL, deleted, operator, id); err != nil {
  99. err = errors.WithStack(err)
  100. return
  101. }
  102. if eff, err = res.RowsAffected(); err != nil {
  103. err = errors.WithStack(err)
  104. }
  105. return
  106. }
  107. // ExpireTip tip expire.
  108. func (d *Dao) ExpireTip(c context.Context, id int64, operator string, t int64) (eff int64, err error) {
  109. var res xsql.Result
  110. if res, err = d.db.Exec(c, _expireTipSQL, t, operator, id); err != nil {
  111. err = errors.WithStack(err)
  112. return
  113. }
  114. if eff, err = res.RowsAffected(); err != nil {
  115. err = errors.WithStack(err)
  116. }
  117. return
  118. }