content_repo.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package http
  2. import (
  3. "strings"
  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. "github.com/jinzhu/gorm"
  9. )
  10. func contList(c *bm.Context) {
  11. var (
  12. req = c.Request.Form
  13. err error
  14. items []*model.ContentRepo
  15. count int64
  16. order = atoi(req.Get("order"))
  17. page = atoi(req.Get("page"))
  18. size = 20
  19. )
  20. if page == 0 {
  21. page = 1
  22. }
  23. db := contWhere(c)
  24. db.Model(&model.ContentRepo{}).Count(&count)
  25. if order == 1 {
  26. db = db.Order("tv_content.mtime ASC")
  27. } else {
  28. db = db.Order("tv_content.mtime DESC")
  29. }
  30. if err = db.Model(&model.ContentRepo{}).Offset((page - 1) * size).Limit(size).Find(&items).Error; err != nil {
  31. log.Error("%v\n", err)
  32. c.JSON(nil, err)
  33. return
  34. }
  35. for _, v := range items {
  36. v.MtimeFormat = tvSrv.TimeFormat(v.Mtime)
  37. v.Mtime = 0
  38. }
  39. pager := &model.ContentRepoPager{
  40. TotalCount: count,
  41. Pn: page,
  42. Ps: size,
  43. Items: items,
  44. }
  45. c.JSON(pager, nil)
  46. }
  47. func contInfo(c *bm.Context) {
  48. var (
  49. req = c.Request.Form
  50. epid = parseInt(req.Get("id"))
  51. err error
  52. )
  53. exist := model.Content{}
  54. if err = tvSrv.DB.Where("epid=?", epid).Where("is_deleted=?", 0).First(&exist).Error; err != nil {
  55. c.JSON(nil, err)
  56. return
  57. }
  58. c.JSON(exist, nil)
  59. }
  60. func saveCont(c *bm.Context) {
  61. var (
  62. req = c.Request.PostForm
  63. epid = atoi(req.Get("id"))
  64. err error
  65. )
  66. exist := model.Content{}
  67. if err = tvSrv.DB.Where("epid=?", epid).Where("is_deleted=?", 0).First(&exist).Error; err != nil {
  68. c.JSON(nil, err)
  69. return
  70. }
  71. title := req.Get("title")
  72. cover := req.Get("cover")
  73. if cover == "" {
  74. renderErrMsg(c, ecode.RequestErr.Code(), "封面不能为空")
  75. return
  76. }
  77. if err := tvSrv.DB.Model(&model.Content{}).Where("epid = ?", epid).Update(map[string]string{"title": title, "cover": cover}).Error; err != nil {
  78. log.Error("tvSrv.saveCont error(%v)", err)
  79. c.JSON(nil, err)
  80. return
  81. }
  82. if err := tvSrv.DB.Model(&model.TVEpContent{}).Where("id = ?", epid).Update(map[string]string{"long_title": title, "cover": cover}).Error; err != nil {
  83. log.Error("tvSrv.saveCont error(%v)", err)
  84. c.JSON(nil, err)
  85. return
  86. }
  87. c.JSON(nil, nil)
  88. }
  89. func preview(c *bm.Context) {
  90. var (
  91. req = c.Request.Form
  92. err error
  93. epid = atoi(req.Get("id"))
  94. )
  95. exist := model.Content{}
  96. if err = tvSrv.DB.Where("epid=?", epid).Where("is_deleted=?", 0).First(&exist).Error; err != nil {
  97. c.JSON(nil, err)
  98. return
  99. }
  100. url, err := tvSrv.Playurl(exist.CID)
  101. if err != nil {
  102. log.Error("tvSrv.Playurl error(%v)", err)
  103. c.JSON(nil, err)
  104. return
  105. }
  106. c.JSON(url, nil)
  107. }
  108. func contOptions(id string, valid int) (ret bool) {
  109. var (
  110. epid = atoi(id)
  111. exist = model.Content{}
  112. )
  113. ret = false
  114. if err := tvSrv.DB.Where("epid=?", epid).Where("is_deleted=?", 0).First(&exist).Error; err != nil {
  115. log.Error("tvSrv.contOptions error(%v)", err)
  116. return
  117. }
  118. if err := tvSrv.DB.Model(&model.Content{}).Where("epid=?", epid).Update(map[string]int{"valid": valid}).Error; err != nil {
  119. log.Error("tvSrv.contOptions error(%v)", err)
  120. return
  121. }
  122. return true
  123. }
  124. func contOnline(c *bm.Context) {
  125. var (
  126. req = c.Request.PostForm
  127. ids = req.Get("ids")
  128. )
  129. idList := strings.Split(ids, ",")
  130. if len(idList) == 0 {
  131. renderErrMsg(c, ecode.RequestErr.Code(), _errIDNotFound)
  132. return
  133. }
  134. for _, val := range idList {
  135. if !contOptions(val, 1) {
  136. renderErrMsg(c, ecode.RequestErr.Code(), "Online("+val+") fail")
  137. return
  138. }
  139. }
  140. c.JSON(nil, nil)
  141. }
  142. func contHidden(c *bm.Context) {
  143. var (
  144. req = c.Request.PostForm
  145. ids = req.Get("ids")
  146. )
  147. idList := strings.Split(ids, ",")
  148. if len(idList) == 0 {
  149. renderErrMsg(c, ecode.RequestErr.Code(), _errIDNotFound)
  150. return
  151. }
  152. for _, val := range idList {
  153. if !contOptions(val, 0) {
  154. renderErrMsg(c, ecode.RequestErr.Code(), "Hide ("+val+") fail")
  155. return
  156. }
  157. }
  158. c.JSON(nil, nil)
  159. }
  160. func contWhere(c *bm.Context) *gorm.DB {
  161. var (
  162. req = c.Request.Form
  163. sid = atoi(req.Get("sid"))
  164. cat = atoi(req.Get("category"))
  165. epid = atoi(req.Get("epid"))
  166. validStr = req.Get("valid")
  167. )
  168. db := tvSrv.DB.
  169. Joins("LEFT OUTER JOIN tv_ep_season ON tv_content.season_id=tv_ep_season.id").
  170. Select("tv_content.*, tv_ep_season.category, tv_ep_season.title AS season_title").
  171. Where("tv_content.state=?", 3).
  172. Where("tv_content.is_deleted=?", 0).
  173. Where("tv_ep_season.check=?", 1).
  174. Where("tv_ep_season.is_deleted=?", 0)
  175. if sid != 0 {
  176. db = db.Where("tv_content.season_id=?", sid)
  177. }
  178. if epid != 0 {
  179. db = db.Where("tv_content.epid=?", epid)
  180. }
  181. if cat != 0 {
  182. db = db.Where("tv_ep_season.category=?", cat)
  183. }
  184. if validStr == "" {
  185. return db
  186. }
  187. if valid := atoi(validStr); valid == 0 {
  188. db = db.Where("tv_content.valid=?", 0)
  189. } else if valid == 1 {
  190. db = db.Where("tv_content.valid=?", 1)
  191. }
  192. return db
  193. }