season_repo.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 seasonList(c *bm.Context) {
  11. var (
  12. req = c.Request.Form
  13. err error
  14. items []*model.SeaRepoDB
  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 := seasonWhere(c)
  24. db.Model(&model.SeaRepoDB{}).Count(&count)
  25. if order == 1 {
  26. db = db.Order("mtime ASC")
  27. } else {
  28. db = db.Order("mtime DESC")
  29. }
  30. if err = db.Model(&model.SeaRepoDB{}).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. pager := &model.SeasonRepoPager{
  36. TotalCount: count,
  37. Pn: page,
  38. Ps: size,
  39. }
  40. for _, v := range items {
  41. pager.Items = append(pager.Items, v.ToList())
  42. }
  43. c.JSON(pager, nil)
  44. }
  45. func seasonInfo(c *bm.Context) {
  46. var (
  47. req = c.Request.Form
  48. sid = parseInt(req.Get("id"))
  49. err error
  50. )
  51. exist := model.TVEpSeason{}
  52. if err = tvSrv.DB.Where("id=?", sid).Where("is_deleted=?", 0).First(&exist).Error; err != nil {
  53. c.JSON(nil, err)
  54. return
  55. }
  56. c.JSON(exist, nil)
  57. }
  58. func saveSeason(c *bm.Context) {
  59. var (
  60. req = c.Request.PostForm
  61. sid = parseInt(req.Get("id"))
  62. err error
  63. )
  64. exist := model.TVEpSeason{}
  65. if err = tvSrv.DB.Where("id=?", sid).Where("is_deleted=?", 0).First(&exist).Error; err != nil {
  66. c.JSON(nil, err)
  67. return
  68. }
  69. title := req.Get("title")
  70. desc := req.Get("desc")
  71. staff := req.Get("staff")
  72. cover := req.Get("cover")
  73. if title == "" {
  74. renderErrMsg(c, ecode.RequestErr.Code(), "标题不能为空")
  75. return
  76. }
  77. if desc == "" {
  78. renderErrMsg(c, ecode.RequestErr.Code(), "简介不能为空")
  79. return
  80. }
  81. if staff == "" {
  82. renderErrMsg(c, ecode.RequestErr.Code(), "staff不能为空")
  83. return
  84. }
  85. if cover == "" {
  86. renderErrMsg(c, ecode.RequestErr.Code(), "封面不能为空")
  87. return
  88. }
  89. if err := tvSrv.DB.Model(&model.TVEpSeason{}).Where("id = ?", sid).Update(map[string]string{"title": title, "desc": desc, "staff": staff, "cover": cover}).Error; err != nil {
  90. log.Error("tvSrv.saveSeason error(%v)", err)
  91. c.JSON(nil, err)
  92. return
  93. }
  94. c.JSON(nil, nil)
  95. }
  96. func seasonOptions(id string, valid int) (ret bool) {
  97. var (
  98. sid = parseInt(id)
  99. exist = model.TVEpSeason{}
  100. )
  101. ret = false
  102. if err := tvSrv.DB.Where("id=?", sid).Where("is_deleted=?", 0).First(&exist).Error; err != nil {
  103. log.Error("tvSrv.seasonOptions error(%v)", err)
  104. return
  105. }
  106. if err := tvSrv.DB.Model(&model.TVEpSeason{}).Where("id=?", sid).Update(map[string]int{"valid": valid}).Error; err != nil {
  107. log.Error("tvSrv.seasonOptions error(%v)", err)
  108. return
  109. }
  110. return true
  111. }
  112. func seasonOnline(c *bm.Context) {
  113. var (
  114. req = c.Request.PostForm
  115. ids = req.Get("ids")
  116. )
  117. idList := strings.Split(ids, ",")
  118. if len(idList) == 0 {
  119. renderErrMsg(c, ecode.RequestErr.Code(), _errIDNotFound)
  120. return
  121. }
  122. for _, val := range idList {
  123. if !seasonOptions(val, 1) {
  124. renderErrMsg(c, ecode.RequestErr.Code(), "Online("+val+") fail")
  125. return
  126. }
  127. }
  128. c.JSON(nil, nil)
  129. }
  130. func seasonHidden(c *bm.Context) {
  131. var (
  132. req = c.Request.PostForm
  133. ids = req.Get("ids")
  134. )
  135. idList := strings.Split(ids, ",")
  136. if len(idList) == 0 {
  137. renderErrMsg(c, ecode.RequestErr.Code(), _errIDNotFound)
  138. return
  139. }
  140. for _, val := range idList {
  141. if !seasonOptions(val, 0) {
  142. renderErrMsg(c, ecode.RequestErr.Code(), "Hidden("+val+") fail")
  143. return
  144. }
  145. }
  146. c.JSON(nil, nil)
  147. }
  148. func seasonWhere(c *bm.Context) *gorm.DB {
  149. var (
  150. req = c.Request.Form
  151. sid = atoi(req.Get("sid"))
  152. cat = atoi(req.Get("category"))
  153. validStr = req.Get("valid")
  154. title = req.Get("title")
  155. )
  156. db := tvSrv.DB.Select("*").
  157. Where("`check`=?", 1).
  158. Where("is_deleted=?", 0)
  159. if title != "" {
  160. db = db.Where("title LIKE ?", "%"+title+"%")
  161. }
  162. if sid != 0 {
  163. db = db.Where("id=?", sid)
  164. }
  165. if cat != 0 {
  166. db = db.Where("category=?", cat)
  167. }
  168. if validStr == "" {
  169. return db
  170. }
  171. if valid := atoi(validStr); valid == 0 {
  172. db = db.Where("valid=?", 0)
  173. } else if valid == 1 {
  174. db = db.Where("valid=?", 1)
  175. }
  176. return db
  177. }