notice.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/reply/model"
  5. "go-common/library/database/sql"
  6. xtime "go-common/library/time"
  7. )
  8. const (
  9. _selOneNotice = "SELECT id,plat,version,condi,build,title,content,link,stime,etime,status,ctime,mtime,client_type FROM notice where id=?"
  10. _selCountNoticeSQL = "SELECT count(*) as count FROM notice"
  11. _selAllNoticeSQL = "SELECT id,plat,version,condi,build,title,content,link,stime,etime,status,ctime,mtime,client_type FROM notice ORDER BY stime DESC limit ?,?"
  12. _addNoticeSQL = "INSERT INTO notice (plat,version,condi,build,title,content,link,status,stime,etime,client_type) VALUES (?,?,?,?,?,?,?,?,?,?,?)"
  13. _updateNotcieSQL = "UPDATE notice set plat=?,version=?,condi=?,build=?,title=?,content=?,link=?,stime=?,etime=?,client_type=? where id=?"
  14. _updateNoticeStatusSQL = "UPDATE notice set status=? where id=?"
  15. _delNoticeSQL = "DELETE FROM notice where id=?"
  16. _selOnlineNoticeSQL = "SELECT id,plat,version,condi,build,title,content,link,stime,etime,status,ctime,mtime,client_type FROM notice WHERE plat=? and (stime<=? and etime>?) and status=1"
  17. )
  18. // RangeNotice 获取发布时间与某一时间范围有交集的公告.
  19. func (dao *Dao) RangeNotice(c context.Context, plat model.NoticePlat, stime xtime.Time, etime xtime.Time) (nts []*model.Notice, err error) {
  20. rows, err := dao.db.Query(c, _selOnlineNoticeSQL, plat, etime.Time(), stime.Time())
  21. if err != nil {
  22. return
  23. }
  24. defer rows.Close()
  25. nts = make([]*model.Notice, 0)
  26. for rows.Next() {
  27. nt := new(model.Notice)
  28. if err = rows.Scan(&nt.ID, &nt.Plat, &nt.Version, &nt.Condition, &nt.Build, &nt.Title, &nt.Content, &nt.Link, &nt.StartTime, &nt.EndTime, &nt.Status, &nt.CreateTime, &nt.ModifyTime, &nt.ClientType); err != nil {
  29. return
  30. }
  31. nts = append(nts, nt)
  32. }
  33. return
  34. }
  35. // Notice get one notice detail.
  36. func (dao *Dao) Notice(c context.Context, id uint32) (nt *model.Notice, err error) {
  37. row := dao.db.QueryRow(c, _selOneNotice, id)
  38. nt = new(model.Notice)
  39. if err = row.Scan(&nt.ID, &nt.Plat, &nt.Version, &nt.Condition, &nt.Build, &nt.Title, &nt.Content, &nt.Link, &nt.StartTime, &nt.EndTime, &nt.Status, &nt.CreateTime, &nt.ModifyTime, &nt.ClientType); err != nil {
  40. if err == sql.ErrNoRows {
  41. nt = nil
  42. err = nil
  43. }
  44. }
  45. return
  46. }
  47. // CountNotice return notice count.
  48. func (dao *Dao) CountNotice(c context.Context) (count int64, err error) {
  49. row := dao.db.QueryRow(c, _selCountNoticeSQL)
  50. err = row.Scan(&count)
  51. return
  52. }
  53. // ListNotice retrive reply's notice list from db by offset and count.
  54. func (dao *Dao) ListNotice(c context.Context, offset int64, count int64) (nts []*model.Notice, err error) {
  55. rows, err := dao.db.Query(c, _selAllNoticeSQL, offset, count)
  56. if err != nil {
  57. return
  58. }
  59. defer rows.Close()
  60. nts = make([]*model.Notice, 0)
  61. for rows.Next() {
  62. nt := new(model.Notice)
  63. if err = rows.Scan(&nt.ID, &nt.Plat, &nt.Version, &nt.Condition, &nt.Build, &nt.Title, &nt.Content, &nt.Link, &nt.StartTime, &nt.EndTime, &nt.Status, &nt.CreateTime, &nt.ModifyTime, &nt.ClientType); err != nil {
  64. return
  65. }
  66. nts = append(nts, nt)
  67. }
  68. err = rows.Err()
  69. return
  70. }
  71. // CreateNotice insert a notice into db.
  72. func (dao *Dao) CreateNotice(c context.Context, nt *model.Notice) (rows int64, err error) {
  73. res, err := dao.db.Exec(c, _addNoticeSQL, nt.Plat, nt.Version, nt.Condition, nt.Build, nt.Title, nt.Content, nt.Link, nt.Status, nt.StartTime, nt.EndTime, nt.ClientType)
  74. if err != nil {
  75. return
  76. }
  77. return res.LastInsertId()
  78. }
  79. // UpdateNotice update main notice's main fileds.
  80. func (dao *Dao) UpdateNotice(c context.Context, nt *model.Notice) (rows int64, err error) {
  81. res, err := dao.db.Exec(c, _updateNotcieSQL, nt.Plat, nt.Version, nt.Condition, nt.Build, nt.Title, nt.Content, nt.Link, nt.StartTime, nt.EndTime, nt.ClientType, nt.ID)
  82. if err != nil {
  83. return
  84. }
  85. return res.RowsAffected()
  86. }
  87. // UpdateNoticeStatus change notice's status to offline\online.
  88. func (dao *Dao) UpdateNoticeStatus(c context.Context, status model.NoticeStatus, id uint32) (rows int64, err error) {
  89. res, err := dao.db.Exec(c, _updateNoticeStatusSQL, status, id)
  90. if err != nil {
  91. return
  92. }
  93. return res.RowsAffected()
  94. }
  95. // DeleteNotice delete a notice entry from db.
  96. func (dao *Dao) DeleteNotice(c context.Context, id uint32) (rows int64, err error) {
  97. res, err := dao.db.Exec(c, _delNoticeSQL, id)
  98. if err != nil {
  99. return
  100. }
  101. return res.RowsAffected()
  102. }