tab.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package channel
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/feed/model/common"
  7. "go-common/app/admin/main/feed/model/show"
  8. "go-common/app/admin/main/feed/util"
  9. "go-common/library/log"
  10. )
  11. const (
  12. //TabOnline channel tab online
  13. TabOnline = 1
  14. //TabDownline channel tab down line
  15. TabDownline = 2
  16. //TabWaitOnline channel tab wait to line
  17. TabWaitOnline = 3
  18. //OrderTimeDown channel tab oder by stime desc
  19. OrderTimeDown = 1
  20. //OrderTimeUp channel tab oder by stime asc
  21. OrderTimeUp = 2
  22. //ActionAddCTab log action
  23. ActionAddCTab = "ActAddChannelTab"
  24. //ActionUpCTab log action
  25. ActionUpCTab = "ActUpChannelTab"
  26. //ActionDelCTab log action
  27. ActionDelCTab = "ActDelChannelTab"
  28. //ActionOfflineCTab log action
  29. ActionOfflineCTab = "ActOfflineChannelTab"
  30. )
  31. //TabList channel tab list
  32. func (s *Service) TabList(lp *show.ChannelTabLP) (pager *show.ChannelTabPager, err error) {
  33. var (
  34. eTime int64
  35. sTime int64
  36. )
  37. pager = &show.ChannelTabPager{
  38. Page: common.Page{
  39. Num: lp.Pn,
  40. Size: lp.Ps,
  41. },
  42. }
  43. w := map[string]interface{}{
  44. "is_delete": common.NotDeleted,
  45. }
  46. query := s.showDao.DB.Model(&show.ChannelTab{})
  47. if lp.TagID > 0 {
  48. w["tag_id"] = lp.TagID
  49. }
  50. if lp.TabID > 0 {
  51. w["tab_id"] = lp.TabID
  52. }
  53. if lp.Stime > 0 {
  54. query = query.Where("stime >= ?", lp.Stime)
  55. }
  56. if lp.Etime > 0 {
  57. query = query.Where("etime <= ?", lp.Etime)
  58. }
  59. if lp.Person != "" {
  60. query = query.Where("person like ?", "%"+lp.Person+"%")
  61. }
  62. if lp.Status == TabWaitOnline {
  63. if lp.Stime != 0 {
  64. if lp.Stime < time.Now().Unix() {
  65. sTime = time.Now().Unix()
  66. } else {
  67. sTime = lp.Stime
  68. }
  69. } else {
  70. sTime = time.Now().Unix()
  71. }
  72. query = query.Where("stime >= ?", sTime)
  73. } else if lp.Status == TabOnline {
  74. if lp.Stime != 0 {
  75. if lp.Stime < time.Now().Unix() {
  76. sTime = time.Now().Unix()
  77. } else {
  78. sTime = lp.Stime
  79. }
  80. } else {
  81. sTime = time.Now().Unix()
  82. }
  83. if lp.Etime != 0 {
  84. if lp.Etime > time.Now().Unix() {
  85. eTime = time.Now().Unix()
  86. } else {
  87. eTime = lp.Etime
  88. }
  89. } else {
  90. eTime = time.Now().Unix()
  91. }
  92. query = query.Where("stime < ?", sTime).Where("etime >= ?", eTime)
  93. } else if lp.Status == TabDownline {
  94. if lp.Etime != 0 {
  95. if lp.Etime < time.Now().Unix() {
  96. eTime = lp.Etime
  97. } else {
  98. eTime = time.Now().Unix()
  99. }
  100. } else {
  101. eTime = time.Now().Unix()
  102. }
  103. query = query.Where("etime < ?", eTime)
  104. }
  105. if lp.Order == OrderTimeDown {
  106. query = query.Order("`stime` ASC")
  107. } else if lp.Order == OrderTimeUp {
  108. query = query.Order("`stime` DESC")
  109. }
  110. if err = query.Where(w).Count(&pager.Page.Total).Error; err != nil {
  111. log.Error("chanelSvc.CardSetupList Index count error(%v)", err)
  112. return
  113. }
  114. tabs := []*show.ChannelTab{}
  115. if err = query.Where(w).Order("`id` DESC").Offset((lp.Pn - 1) * lp.Ps).Limit(lp.Ps).Find(&tabs).Error; err != nil {
  116. log.Error("chanelSvc.CardSetupList First error(%v)", err)
  117. return
  118. }
  119. for k, v := range tabs {
  120. //online for fe
  121. if time.Now().Unix() < v.Stime {
  122. tabs[k].Status = TabWaitOnline
  123. } else if time.Now().Unix() >= v.Etime {
  124. tabs[k].Status = TabDownline
  125. } else {
  126. tabs[k].Status = TabOnline
  127. }
  128. }
  129. pager.Item = tabs
  130. return
  131. }
  132. //AddTab add channel tab
  133. func (s *Service) AddTab(c context.Context, param *show.ChannelTabAP, name string, uid int64) (err error) {
  134. if err = s.IsValid(0, param.TagID, param.Stime, param.Etime, param.Priority); err != nil {
  135. return
  136. }
  137. if err = s.showDao.ChannelTabAdd(param); err != nil {
  138. return
  139. }
  140. if err = util.AddLogs(common.LogChannelTab, name, uid, 0, ActionAddCTab, param); err != nil {
  141. log.Error("chanelSvc.AddTab AddLog error(%v)", err)
  142. return
  143. }
  144. return
  145. }
  146. //IsValid validate data
  147. func (s *Service) IsValid(id, tagID, sTime int64, eTime int64, priority int) (err error) {
  148. var (
  149. count int
  150. )
  151. if sTime > eTime {
  152. err = fmt.Errorf("开始时间不能大于结束时间")
  153. return
  154. }
  155. if sTime < time.Now().Unix() {
  156. err = fmt.Errorf("生效时间需要大于当前时间")
  157. return
  158. }
  159. if count, err = s.showDao.ChannelTabValid(id, tagID, sTime, eTime, priority); err != nil {
  160. return
  161. }
  162. if count > 0 {
  163. err = fmt.Errorf("已有该排序无法创建,请重新选择")
  164. return
  165. }
  166. if count, err = s.showDao.ChannelTabValid(id, tagID, sTime, eTime, 0); err != nil {
  167. return
  168. }
  169. if count >= 3 {
  170. stimeStr := time.Unix(sTime, 0).Format("2006-01-02 15:04:05")
  171. etimeStr := time.Unix(eTime, 0).Format("2006-01-02 15:04:05")
  172. str := "频道在" + stimeStr + " 至 " + etimeStr + " 时间段内已有3个运营tab,无法创建"
  173. err = fmt.Errorf(str)
  174. return
  175. }
  176. return
  177. }
  178. //UpdateTab update channel tab
  179. func (s *Service) UpdateTab(c context.Context, param *show.ChannelTabUP, name string, uid int64) (err error) {
  180. if err = s.IsValid(param.ID, param.TagID, param.Stime, param.Etime, param.Priority); err != nil {
  181. return
  182. }
  183. if err = s.showDao.ChannelTabUpdate(param); err != nil {
  184. return
  185. }
  186. if err = util.AddLogs(common.LogChannelTab, name, uid, 0, ActionUpCTab, param); err != nil {
  187. log.Error("chanelSvc.UpdateTab AddLog error(%v)", err)
  188. return
  189. }
  190. return
  191. }
  192. //DeleteTab delete channel tab
  193. func (s *Service) DeleteTab(id int64, name string, uid int64) (err error) {
  194. if err = s.showDao.ChannelTabDelete(id); err != nil {
  195. return
  196. }
  197. if err = util.AddLogs(common.LogChannelTab, name, uid, id, ActionDelCTab, id); err != nil {
  198. log.Error("chanelSvc.DeleteTab AddLog error(%v)", err)
  199. return
  200. }
  201. return
  202. }
  203. //OfflineTab offline channel tab
  204. func (s *Service) OfflineTab(id int64, name string, uid int64) (err error) {
  205. if err = s.showDao.ChannelTabOffline(id); err != nil {
  206. return
  207. }
  208. if err = util.AddLogs(common.LogChannelTab, name, uid, id, ActionOfflineCTab, id); err != nil {
  209. log.Error("chanelSvc.DeleteTab AddLog error(%v)", err)
  210. return
  211. }
  212. return
  213. }