intervs.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package model
  2. import (
  3. "encoding/json"
  4. "go-common/library/time"
  5. "github.com/jinzhu/gorm"
  6. "github.com/siddontang/go-mysql/mysql"
  7. )
  8. // Tv_rank table related const params
  9. const (
  10. _RankCategory = 5 // _RankCategory 模块干预
  11. RankIdxBase = 5 // index page intervention base, pgc=5+1, ugc=5+2
  12. )
  13. // SimpleRank represents the table TV_RANK, but with only necessary fields for the front-end
  14. type SimpleRank struct {
  15. Title string `json:"title"`
  16. Source int `json:"source"`
  17. SourceName string `json:"source_name"`
  18. Mtime string `json:"mtime"`
  19. Pubdate string `json:"pubdate"`
  20. RankCore
  21. }
  22. // RankCore def
  23. type RankCore struct {
  24. Rank int64 `json:"rank"`
  25. ID int64 `json:"id"`
  26. ContID int64 `json:"cid" gorm:"column:cont_id"`
  27. ContType int `json:"cont_type"`
  28. Position int `json:"position"`
  29. }
  30. // RankError represents the invalid season info
  31. type RankError struct {
  32. ID int `json:"id"`
  33. SeasonID int `json:"season_id"`
  34. }
  35. // RankList is the output format for intervention list
  36. type RankList struct {
  37. List []*SimpleRank `json:"list"`
  38. }
  39. // Rank represents the table TV_RANK
  40. type Rank struct {
  41. Title string
  42. ModuleID int64 `gorm:"column:module_id"`
  43. Category int8
  44. IsDeleted int8
  45. Mtime time.Time
  46. RankCore
  47. }
  48. // TableName tv_rank
  49. func (c SimpleRank) TableName() string {
  50. return "tv_rank"
  51. }
  52. // TableName tv_rank
  53. func (v Rank) TableName() string {
  54. return "tv_rank"
  55. }
  56. // BeComplete transforms a simpleRank to Complete rank in order to create it in DB
  57. func (c SimpleRank) BeComplete(req *IntervPubReq, title string, position int) (res *Rank) {
  58. res = &Rank{
  59. Title: title,
  60. RankCore: c.RankCore,
  61. }
  62. res.Position = position
  63. if req.ModuleID > 0 {
  64. res.Category = _RankCategory
  65. res.ModuleID = req.ModuleID
  66. return
  67. }
  68. if req.Rank > 0 {
  69. res.Rank = req.Rank
  70. }
  71. if req.Category > 0 {
  72. res.Category = int8(req.Category)
  73. }
  74. return
  75. }
  76. type catName func(int) string // translate pgc category to CN name
  77. type tpParName func(int32) (string, int32, error) // translate ugc type to its parent tid and parent's name
  78. // BeSimpleSn def.
  79. func (v *Rank) BeSimpleSn(sn *TVEpSeason, translate catName) *SimpleRank {
  80. return &SimpleRank{
  81. RankCore: v.RankCore,
  82. Title: sn.Title,
  83. Source: sn.Category,
  84. SourceName: translate(sn.Category),
  85. Pubdate: sn.PlayTime.Time().Format(mysql.TimeFormat),
  86. Mtime: v.Mtime.Time().Format(mysql.TimeFormat),
  87. }
  88. }
  89. // BeSimpleArc def.
  90. func (v *Rank) BeSimpleArc(arc *SimpleArc, translate tpParName) (res *SimpleRank) {
  91. res = &SimpleRank{
  92. RankCore: v.RankCore,
  93. Title: arc.Title,
  94. Mtime: v.Mtime.Time().Format(mysql.TimeFormat),
  95. Pubdate: arc.Pubtime.Time().Format(mysql.TimeFormat),
  96. }
  97. if pname, pid, err := translate(arc.TypeID); err == nil {
  98. res.Source = int(pid)
  99. res.SourceName = pname
  100. }
  101. return
  102. }
  103. //BeError transforms a rank to rankError
  104. func (v Rank) BeError() *RankError {
  105. return &RankError{
  106. ID: int(v.ID),
  107. SeasonID: int(v.ContID),
  108. }
  109. }
  110. // RankListReq is rank list request
  111. type RankListReq struct {
  112. Rank int64 `form:"rank" validate:"min=0"`
  113. Category int64 `form:"category" validate:"required,min=1"`
  114. }
  115. // RankPubReq is rank publish request
  116. type RankPubReq struct {
  117. RankListReq
  118. Intervs string `form:"intervs" validate:"required"`
  119. }
  120. // ModListReq is mod list request
  121. type ModListReq struct {
  122. ModuleID int64 `form:"module_id" validate:"required,min=1"`
  123. }
  124. // ModPubReq is mod publish request
  125. type ModPubReq struct {
  126. ModListReq
  127. Intervs string `form:"intervs" validate:"required"`
  128. }
  129. // IdxListReq is index list request
  130. type IdxListReq struct {
  131. TypeID int64 `form:"type_id" validate:"required,min=1"`
  132. RankType int64 `form:"rank_type" validate:"required,min=1,max=2"` // 1=pgc, 2=ugc
  133. }
  134. // IdxPubReq is index publish request.
  135. type IdxPubReq struct {
  136. IdxListReq
  137. Intervs string `form:"intervs" validate:"required"`
  138. }
  139. // IntervListReq is common request for interv list.
  140. type IntervListReq struct {
  141. Rank int64
  142. Category int64
  143. ModuleID int64
  144. }
  145. // IntervPubReq is common request for interv publish.
  146. type IntervPubReq struct {
  147. IntervListReq
  148. Items []*SimpleRank
  149. }
  150. // FromRank builds the request with rank & category params
  151. func (v *IntervListReq) FromRank(rank *RankListReq) {
  152. v.Rank = rank.Rank
  153. v.Category = rank.Category
  154. v.ModuleID = 0
  155. }
  156. // FromRank def.
  157. func (v *IntervPubReq) FromRank(rank *RankPubReq) (err error) {
  158. v.IntervListReq.FromRank(&rank.RankListReq)
  159. return json.Unmarshal([]byte(rank.Intervs), &v.Items)
  160. }
  161. // FromMod builds the request with module params
  162. func (v *IntervListReq) FromMod(mod *ModListReq) {
  163. v.Rank = 0
  164. v.Category = _RankCategory
  165. v.ModuleID = mod.ModuleID
  166. }
  167. // FromMod builds the request with module params
  168. func (v *IntervPubReq) FromMod(mod *ModPubReq) (err error) {
  169. v.IntervListReq.FromMod(&mod.ModListReq)
  170. return json.Unmarshal([]byte(mod.Intervs), &v.Items)
  171. }
  172. // FromIndex builds the request with index params
  173. func (v *IntervListReq) FromIndex(idx *IdxListReq) {
  174. v.Rank = idx.TypeID // category id, pgc or ugc type id
  175. v.Category = idx.RankType + RankIdxBase // 6 or 7
  176. v.ModuleID = 0
  177. }
  178. // IsIdx tells whether this request is from index
  179. func (v *IntervListReq) IsIdx() bool {
  180. return v.Category > RankIdxBase
  181. }
  182. // FromIndex def.
  183. func (v *IntervPubReq) FromIndex(idx *IdxPubReq) (err error) {
  184. v.IntervListReq.FromIndex(&idx.IdxListReq)
  185. return json.Unmarshal([]byte(idx.Intervs), &v.Items)
  186. }
  187. // BuildDB builds the db from the intervention request
  188. func (v *IntervListReq) BuildDB(db *gorm.DB) (newDB *gorm.DB) {
  189. newDB = db.Model(Rank{}).Where("is_deleted = 0")
  190. if v.ModuleID == 0 { // index or rank
  191. newDB = newDB.Where("rank = ?", v.Rank).Where("category = ?", v.Category)
  192. } else {
  193. newDB = newDB.Where("module_id = ?", v.ModuleID)
  194. }
  195. return
  196. }