intervs.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package http
  2. import (
  3. "fmt"
  4. "go-common/app/admin/main/tv/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. const (
  10. _jsonErr = "Incorrect Json Format"
  11. )
  12. func intervsRank(c *bm.Context) {
  13. var (
  14. form = new(model.RankListReq)
  15. request = new(model.IntervListReq)
  16. )
  17. if err := c.Bind(form); err != nil {
  18. return
  19. }
  20. request.FromRank(form)
  21. c.JSON(tvSrv.Intervs(request))
  22. }
  23. func intervsMod(c *bm.Context) {
  24. var (
  25. form = new(model.ModListReq)
  26. request = new(model.IntervListReq)
  27. )
  28. if err := c.Bind(form); err != nil {
  29. return
  30. }
  31. request.FromMod(form)
  32. c.JSON(tvSrv.Intervs(request))
  33. }
  34. func intervsIndex(c *bm.Context) {
  35. var (
  36. form = new(model.IdxListReq)
  37. request = new(model.IntervListReq)
  38. )
  39. if err := c.Bind(form); err != nil {
  40. return
  41. }
  42. request.FromIndex(form)
  43. c.JSON(tvSrv.Intervs(request))
  44. }
  45. func rankPub(c *bm.Context) {
  46. var (
  47. form = new(model.RankPubReq)
  48. req = new(model.IntervPubReq)
  49. )
  50. if err := c.Bind(form); err != nil {
  51. return
  52. }
  53. if err := req.FromRank(form); err != nil {
  54. renderErrMsg(c, ecode.RequestErr.Code(), _jsonErr)
  55. return
  56. }
  57. intervPublish(c, req)
  58. }
  59. func indexPub(c *bm.Context) {
  60. var (
  61. form = new(model.IdxPubReq)
  62. req = new(model.IntervPubReq)
  63. )
  64. if err := c.Bind(form); err != nil {
  65. return
  66. }
  67. if err := req.FromIndex(form); err != nil {
  68. renderErrMsg(c, ecode.RequestErr.Code(), _jsonErr)
  69. return
  70. }
  71. intervPublish(c, req)
  72. }
  73. func modPub(c *bm.Context) {
  74. var (
  75. form = new(model.ModPubReq)
  76. req = new(model.IntervPubReq)
  77. )
  78. if err := c.Bind(form); err != nil {
  79. return
  80. }
  81. if err := req.FromMod(form); err != nil {
  82. renderErrMsg(c, ecode.RequestErr.Code(), _jsonErr)
  83. return
  84. }
  85. intervPublish(c, req)
  86. }
  87. // combine the alert msg for too many interventions and cut the slice
  88. func alertMsg(items []*model.SimpleRank, nbLimit int) (msg string, restItems []*model.SimpleRank) {
  89. var (
  90. length = len(items)
  91. )
  92. if length <= nbLimit {
  93. return "", items
  94. }
  95. msg = "以下内容因超量未发布干预:"
  96. for i := nbLimit; i < length; i++ {
  97. if i+1 == length {
  98. msg = msg + fmt.Sprintf("id%d", items[i].ContID)
  99. continue
  100. }
  101. msg = msg + fmt.Sprintf("id%d,", items[i].ContID)
  102. }
  103. return msg, items[:nbLimit]
  104. }
  105. func intervPublish(c *bm.Context, req *model.IntervPubReq) {
  106. var (
  107. err error
  108. invalid *model.RankError
  109. alertInfo string // used when too many interventions published
  110. )
  111. alertInfo, req.Items = alertMsg(req.Items, tvSrv.IntervLimit) // too many intervention treatment
  112. invalid, err = tvSrv.RefreshIntervs(req)
  113. if err != nil {
  114. log.Error("RefreshIntervs Error %v", err)
  115. c.JSON(nil, err)
  116. return
  117. }
  118. if invalid != nil {
  119. renderErrMsg(c, ecode.RequestErr.Code(), fmt.Sprintf("发布失败,以下内容状态错误:id%d", invalid.SeasonID))
  120. return
  121. }
  122. if alertInfo != "" {
  123. renderErrMsg(c, ecode.OK.Code(), alertInfo)
  124. return
  125. }
  126. renderErrMsg(c, ecode.OK.Code(), "发布成功")
  127. }