notice.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/app/admin/main/growup/model"
  7. )
  8. // InsertNotice insert notice
  9. func (s *Service) InsertNotice(c context.Context, title string, typ int, platform int, link string, status int) (err error) {
  10. notice := &model.Notice{
  11. Title: title,
  12. Type: typ,
  13. Platform: platform,
  14. Link: link,
  15. Status: status,
  16. }
  17. _, err = s.dao.InsertNotice(c, notice)
  18. return
  19. }
  20. // Notices notices
  21. func (s *Service) Notices(c context.Context, typ int, status int, platform int, from int, limit int) (total int, notices []*model.Notice, err error) {
  22. query := queryStr(typ, status, platform)
  23. total, err = s.dao.NoticeCount(c, query)
  24. if err != nil {
  25. return
  26. }
  27. notices, err = s.dao.Notices(c, query, from, limit)
  28. if notices == nil {
  29. notices = make([]*model.Notice, 0)
  30. }
  31. return
  32. }
  33. func queryStr(typ int, status int, platform int) (query string) {
  34. if typ != 0 {
  35. query += " AND "
  36. query += fmt.Sprintf("type=%d", typ)
  37. }
  38. if status != 0 {
  39. query += " AND "
  40. query += fmt.Sprintf("status=%d", status)
  41. }
  42. if platform != 0 {
  43. query += " AND "
  44. query += fmt.Sprintf("platform=%d", platform)
  45. }
  46. query += " AND is_deleted = 0"
  47. return
  48. }
  49. // UpdateNotice update notice
  50. func (s *Service) UpdateNotice(c context.Context, typ int, platform int, title string, link string, id int64, status int) (err error) {
  51. var kv string
  52. if typ != 0 {
  53. kv += fmt.Sprintf("type=%d,", typ)
  54. }
  55. if platform != 0 {
  56. kv += fmt.Sprintf("platform=%d,", platform)
  57. }
  58. if len(title) != 0 {
  59. kv += fmt.Sprintf("title='%s',", title)
  60. }
  61. if len(link) != 0 {
  62. kv += fmt.Sprintf("link='%s',", link)
  63. }
  64. if status != 0 {
  65. kv += fmt.Sprintf("status=%d,", status)
  66. }
  67. if len(kv) == 0 {
  68. return
  69. }
  70. kv = strings.TrimRight(kv, ",")
  71. _, err = s.dao.UpdateNotice(c, kv, id)
  72. return
  73. }