jointly.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "go-common/app/admin/main/vip/model"
  6. "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _jointlysWillEffectSQL = "SELECT id,title,content,operator,start_time,end_time,link,is_hot,ctime,mtime FROM vip_jointly WHERE start_time > ? AND deleted = 0 ORDER BY mtime DESC;"
  11. _jointlysEffectSQL = "SELECT id,title,content,operator,start_time,end_time,link,is_hot,ctime,mtime FROM vip_jointly WHERE start_time < ? AND end_time > ? AND deleted = 0 ORDER BY mtime DESC;"
  12. _jointlysLoseEffectSQL = "SELECT id,title,content,operator,start_time,end_time,link,is_hot,ctime,mtime FROM vip_jointly WHERE end_time < ? AND deleted = 0 ORDER BY mtime DESC;"
  13. _addjointlySQL = "INSERT INTO vip_jointly(title,content,operator,start_time,end_time,link,is_hot)VALUES(?,?,?,?,?,?,?);"
  14. _updateJointlySQL = "UPDATE vip_jointly SET title = ?,content = ?,operator = ?,link = ?,is_hot = ?,start_time = ?,end_time = ? WHERE id = ?;"
  15. _deleteJointlySQL = "UPDATE vip_jointly SET deleted = 1 WHERE id = ?;"
  16. )
  17. // AddJointly add jointly.
  18. func (d *Dao) AddJointly(c context.Context, j *model.Jointly) (a int64, err error) {
  19. var res xsql.Result
  20. if res, err = d.db.Exec(c, _addjointlySQL, j.Title, j.Content, j.Operator, j.StartTime, j.EndTime, j.Link, j.IsHot); err != nil {
  21. err = errors.WithStack(err)
  22. return
  23. }
  24. if a, err = res.RowsAffected(); err != nil {
  25. err = errors.WithStack(err)
  26. }
  27. return
  28. }
  29. // JointlysByState jointly by state.
  30. func (d *Dao) JointlysByState(c context.Context, state int8, now int64) (res []*model.Jointly, err error) {
  31. var rows *sql.Rows
  32. switch state {
  33. case model.WillEffect:
  34. rows, err = d.db.Query(c, _jointlysWillEffectSQL, now)
  35. case model.Effect:
  36. rows, err = d.db.Query(c, _jointlysEffectSQL, now, now)
  37. case model.LoseEffect:
  38. rows, err = d.db.Query(c, _jointlysLoseEffectSQL, now)
  39. default:
  40. return
  41. }
  42. if err != nil {
  43. err = errors.WithStack(err)
  44. return
  45. }
  46. defer rows.Close()
  47. for rows.Next() {
  48. r := new(model.Jointly)
  49. if err = rows.Scan(&r.ID, &r.Title, &r.Content, &r.Operator, &r.StartTime, &r.EndTime, &r.Link, &r.IsHot, &r.CTime, &r.MTime); err != nil {
  50. err = errors.WithStack(err)
  51. res = nil
  52. return
  53. }
  54. res = append(res, r)
  55. }
  56. err = rows.Err()
  57. return
  58. }
  59. // UpdateJointly update jointly.
  60. func (d *Dao) UpdateJointly(c context.Context, j *model.Jointly) (a int64, err error) {
  61. var res xsql.Result
  62. if res, err = d.db.Exec(c, _updateJointlySQL, j.Title, j.Content, j.Operator, j.Link, j.IsHot, j.StartTime, j.EndTime, j.ID); err != nil {
  63. err = errors.WithStack(err)
  64. return
  65. }
  66. if a, err = res.RowsAffected(); err != nil {
  67. err = errors.WithStack(err)
  68. }
  69. return
  70. }
  71. // DeleteJointly delete jointly.
  72. func (d *Dao) DeleteJointly(c context.Context, id int64) (a int64, err error) {
  73. var res xsql.Result
  74. if res, err = d.db.Exec(c, _deleteJointlySQL, id); err != nil {
  75. err = errors.WithStack(err)
  76. return
  77. }
  78. if a, err = res.RowsAffected(); err != nil {
  79. err = errors.WithStack(err)
  80. }
  81. return
  82. }