notice.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/reply/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. // ListNotice ListNotice
  10. func (s *Service) ListNotice(c context.Context, page, pageSize int64) (nts []*model.Notice, total int64, err error) {
  11. offset := (page - 1) * pageSize
  12. total, err = s.dao.CountNotice(c)
  13. if err != nil {
  14. return
  15. }
  16. nts, err = s.dao.ListNotice(c, offset, pageSize)
  17. if err != nil {
  18. return
  19. }
  20. return nts, total, err
  21. }
  22. // GetNotice GetNotice
  23. func (s *Service) GetNotice(c context.Context, id uint32) (nt *model.Notice, err error) {
  24. return s.dao.Notice(c, id)
  25. }
  26. // UpdateNotice UpdateNotice
  27. func (s *Service) UpdateNotice(c context.Context, nt *model.Notice) (err error) {
  28. var ntGet *model.Notice
  29. ntGet, err = s.dao.Notice(c, nt.ID)
  30. if err != nil || ntGet == nil {
  31. err = ecode.NothingFound
  32. return
  33. }
  34. if ntGet.Status == model.StatusOnline {
  35. err = s.checkConflict(c, nt)
  36. if err != nil {
  37. return
  38. }
  39. }
  40. _, err = s.dao.UpdateNotice(c, nt)
  41. return
  42. }
  43. // CreateNotice CreateNotice
  44. func (s *Service) CreateNotice(c context.Context, nt *model.Notice) (lastID int64, err error) {
  45. lastID, err = s.dao.CreateNotice(c, nt)
  46. if lastID <= 0 {
  47. log.Error("create notice failed!last_id not found")
  48. err = fmt.Errorf("create notice failed!last_id not found")
  49. return
  50. }
  51. return
  52. }
  53. // DeleteNotice DeleteNotice
  54. func (s *Service) DeleteNotice(c context.Context, id uint32) (err error) {
  55. _, err = s.dao.DeleteNotice(c, id)
  56. return err
  57. }
  58. func (s *Service) checkConflict(c context.Context, nt *model.Notice) error {
  59. var nts []*model.Notice
  60. var err error
  61. nts, err = s.dao.RangeNotice(c, nt.Plat, nt.StartTime, nt.EndTime)
  62. if err != nil {
  63. return err
  64. }
  65. for _, data := range nts {
  66. //如果ID相同说明是自己
  67. if data.ID == nt.ID {
  68. continue
  69. }
  70. //如果为web平台则必然冲突
  71. if data.Plat == model.PlatWeb {
  72. return ecode.ReplyNoticeConflict
  73. }
  74. //如果客户端类型不同则跳过检查
  75. if data.ClientType != "" && nt.ClientType != "" && data.ClientType != nt.ClientType {
  76. continue
  77. }
  78. //为每一种版本情况检查
  79. if data.Condition == model.ConditionEQ {
  80. if nt.Condition == model.ConditionEQ && nt.Build == data.Build {
  81. return ecode.ReplyNoticeConflict
  82. }
  83. if nt.Condition == model.ConditionGT && nt.Build <= data.Build {
  84. return ecode.ReplyNoticeConflict
  85. }
  86. if nt.Condition == model.ConditionLT && nt.Build >= data.Build {
  87. return ecode.ReplyNoticeConflict
  88. }
  89. } else if data.Condition == model.ConditionGT {
  90. if nt.Condition == model.ConditionEQ {
  91. if nt.Build >= data.Build {
  92. return ecode.ReplyNoticeConflict
  93. }
  94. } else if nt.Condition == model.ConditionLT {
  95. if nt.Build >= data.Build {
  96. return ecode.ReplyNoticeConflict
  97. }
  98. } else {
  99. return ecode.ReplyNoticeConflict
  100. }
  101. } else if data.Condition == model.ConditionLT {
  102. if nt.Condition == model.ConditionEQ {
  103. if nt.Build <= data.Build {
  104. return ecode.ReplyNoticeConflict
  105. }
  106. } else if nt.Condition == model.ConditionGT {
  107. if nt.Build <= data.Build {
  108. return ecode.ReplyNoticeConflict
  109. }
  110. } else {
  111. return ecode.ReplyNoticeConflict
  112. }
  113. }
  114. }
  115. return nil
  116. }
  117. // UpdateNoticeStatus UpdateNoticeStatus
  118. func (s *Service) UpdateNoticeStatus(c context.Context, status model.NoticeStatus, id uint32) (err error) {
  119. //检测客户端在同一时间内是否存在另外一条已发布的公告,如果存在则不允许发布
  120. if status == model.StatusOnline {
  121. var nt *model.Notice
  122. nt, err = s.dao.Notice(c, id)
  123. if err != nil || nt == nil {
  124. return
  125. }
  126. err = s.checkConflict(c, nt)
  127. if err != nil {
  128. return
  129. }
  130. }
  131. _, err = s.dao.UpdateNoticeStatus(c, status, id)
  132. return err
  133. }