elastic.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package search
  2. import (
  3. "encoding/json"
  4. "go-common/app/interface/main/tv/model"
  5. "go-common/app/interface/main/tv/model/thirdp"
  6. "go-common/library/time"
  7. "github.com/siddontang/go-mysql/mysql"
  8. )
  9. const (
  10. _pgcType = 1
  11. _ugcType = 2
  12. _hotestOrder = 2
  13. // AllLabel is the string that means all data
  14. AllLabel = "-1" // it means all for index
  15. )
  16. // EsPage def.
  17. type EsPage struct {
  18. Num int `json:"num"`
  19. Size int `json:"size"`
  20. Total int `json:"total"`
  21. PageNum int `json:"page_num"`
  22. }
  23. // GetPageNb def.
  24. func (v *EsPage) GetPageNb() {
  25. if v.Total%v.Size == 0 {
  26. v.PageNum = v.Total / v.Size
  27. } else {
  28. v.PageNum = v.Total/v.Size + 1
  29. }
  30. }
  31. // EsPgcResult def.
  32. type EsPgcResult struct {
  33. Page *EsPage `json:"page"`
  34. Result []*PgcEsMdl `json:"result"`
  35. }
  36. // EsUgcResult def.
  37. type EsUgcResult struct {
  38. Page *EsPage `json:"page"`
  39. Result []*UgcEsMdl `json:"result"`
  40. }
  41. // UgcEsMdl def.
  42. type UgcEsMdl struct {
  43. AID int64 `json:"aid"`
  44. TypeID int32 `json:"typeid"`
  45. }
  46. // PgcEsMdl def.
  47. type PgcEsMdl struct {
  48. SeasonID int64 `json:"season_id"`
  49. }
  50. // EsCard def.
  51. type EsCard struct {
  52. model.Card
  53. DataType int `json:"data_type"` // 1=pgc, 2=ugc
  54. }
  55. // EsPager def.
  56. type EsPager struct {
  57. Title string `json:"title"`
  58. Page *EsPage `json:"page"`
  59. Result []*EsCard `json:"result"`
  60. }
  61. // FromPgc def.
  62. func (v *EsCard) FromPgc(card *model.Card) {
  63. v.Card = *card
  64. v.DataType = _pgcType
  65. }
  66. // FromUgc def.
  67. func (v *EsCard) FromUgc(card *model.Card) {
  68. v.Card = *card
  69. v.DataType = _ugcType
  70. }
  71. // ReqEsPn is part of the es request for page related cfg
  72. type ReqEsPn struct {
  73. Ps int `form:"ps" default:"50"`
  74. Pn int `form:"pn" default:"1"`
  75. Order int `form:"order" validate:"required"`
  76. Sort int `form:"sort" default:"0"`
  77. }
  78. // ReqPgcIdx is bm request for pgc index
  79. type ReqPgcIdx struct {
  80. SeasonType int `form:"category" validate:"required,max=5"`
  81. ProducerID int `form:"producer_id"`
  82. Year string `form:"year"`
  83. StyleID int `form:"style_id"`
  84. PubDate string `form:"pub_date"`
  85. SeasonMonth int `form:"season_month" validate:"max=10"`
  86. SeasonStatus []int `form:"season_status,split"`
  87. Copyright []int `form:"copyright,split"`
  88. IsFinish string `form:"is_finish"`
  89. Area []int `form:"area,split"`
  90. SeasonVersion int `form:"season_version"`
  91. ReqEsPn
  92. }
  93. // IsDefault def.
  94. func (v *ReqPgcIdx) IsDefault() bool {
  95. return v.Order == _hotestOrder && v.ProducerID <= 0 &&
  96. v.IsAllStr(v.Year) && v.StyleID <= 0 && v.IsAllStr(v.PubDate) && v.SeasonMonth <= 0 &&
  97. v.IsAll(v.SeasonStatus) && v.IsAll(v.Copyright) && v.IsAllStr(v.IsFinish) && v.IsAll(v.Area) &&
  98. v.SeasonVersion <= 0
  99. }
  100. // IsDefault def.
  101. func (v *ReqUgcIdx) IsDefault() bool {
  102. return v.Order == _hotestOrder && v.SecondTID <= 0 && (v.PubTime == "" || v.PubTime == AllLabel)
  103. }
  104. // Title def.
  105. func (v *ReqPgcIdx) Title() string {
  106. return thirdp.PgcCat(v.SeasonType)
  107. }
  108. // PgcOrder treats the order to get the responding field
  109. func (v *ReqPgcIdx) PgcOrder() (field string) {
  110. switch v.Order {
  111. case 0: // update time
  112. return "latest_time"
  113. case 1:
  114. return "dm_count"
  115. case 2:
  116. return "play_count"
  117. case 3: // follow
  118. return "fav_count"
  119. case 4:
  120. return "score"
  121. case 5: // for others
  122. return "pub_time"
  123. case 6: // for movie
  124. return "release_date"
  125. default:
  126. return "play_count"
  127. }
  128. }
  129. // IdxSort treats the sort
  130. func IdxSort(sortV int) (sort string) {
  131. switch sortV {
  132. case 0:
  133. return "desc"
  134. case 1:
  135. return "asc"
  136. default:
  137. return "desc"
  138. }
  139. }
  140. // ReqUgcIdx is bm request for ugc index
  141. type ReqUgcIdx struct {
  142. ParentTID int32 `form:"category" validate:"required"`
  143. SecondTID int32 `form:"typeid"`
  144. PubTime string `form:"pubtime"`
  145. ReqEsPn
  146. }
  147. // TimeStr picks json str from request and returns the time range struct in String format
  148. func (v *ReqUgcIdx) TimeStr() (res *UgcTime, err error) {
  149. var pubTimeV = &UgcTimeV{}
  150. if err = json.Unmarshal([]byte(v.PubTime), &pubTimeV); err != nil {
  151. return
  152. }
  153. res = &UgcTime{
  154. STime: pubTimeV.STime.Time().Format(mysql.TimeFormat),
  155. ETime: pubTimeV.ETime.Time().Format(mysql.TimeFormat),
  156. }
  157. return
  158. }
  159. // UgcTimeV def. INT64 for http
  160. type UgcTimeV struct {
  161. STime time.Time `json:"stime"`
  162. ETime time.Time `json:"etime"`
  163. }
  164. // UgcTime def. STRING for ES
  165. type UgcTime struct {
  166. STime string `json:"stime"`
  167. ETime string `json:"etime"`
  168. }
  169. // SrvUgcIdx is the request treated by service for DAO, like type_id field and pub_time
  170. type SrvUgcIdx struct {
  171. TIDs []int32
  172. PubTime *UgcTime
  173. ReqEsPn
  174. }
  175. // UgcOrder treats the order to get the responding field
  176. func (v *SrvUgcIdx) UgcOrder() (field string) {
  177. switch v.Order {
  178. case 1: // update time
  179. return "pubtime"
  180. case 2:
  181. return "click"
  182. default:
  183. return "click"
  184. }
  185. }
  186. // ReqIdxInterv is used for index intervention treatment
  187. type ReqIdxInterv struct {
  188. EsIDs []int64
  189. Category int
  190. IsPGC bool
  191. Pn int
  192. }
  193. // FromPGC def.
  194. func (v *ReqIdxInterv) FromPGC(sids []int64, req *ReqPgcIdx) {
  195. v.EsIDs = sids
  196. v.Category = req.SeasonType
  197. v.IsPGC = true
  198. v.Pn = req.Pn
  199. }
  200. // FromUGC def.
  201. func (v *ReqIdxInterv) FromUGC(aids []int64, req *ReqUgcIdx) {
  202. v.EsIDs = aids
  203. v.Category = int(req.ParentTID)
  204. v.IsPGC = false
  205. v.Pn = req.Pn
  206. }
  207. // IdxIntervSave def.
  208. type IdxIntervSave struct {
  209. Pgc map[int][]int64
  210. Ugc map[int][]int64
  211. }
  212. // IsAll checks whether a slice of int means all data
  213. func (v *ReqPgcIdx) IsAll(params []int) (res bool) {
  214. if len(params) == 0 {
  215. return true
  216. }
  217. for _, v := range params {
  218. if v < 0 {
  219. return true
  220. }
  221. }
  222. return false
  223. }
  224. // IsAllStr checks whether a string means all data
  225. func (v *ReqPgcIdx) IsAllStr(duration string) (res bool) {
  226. if duration == "" {
  227. return true
  228. }
  229. if duration == AllLabel {
  230. return true
  231. }
  232. return false
  233. }