label.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. xtime "time"
  6. arccli "go-common/app/service/main/archive/api"
  7. "go-common/library/time"
  8. "github.com/siddontang/go-mysql/mysql"
  9. )
  10. // label related params
  11. const (
  12. ParamTypeid = "typeid"
  13. ParamUgctime = "pubtime"
  14. UgcLabel = 2
  15. PgcLabel = 1
  16. )
  17. // TpLabel def.
  18. type TpLabel struct {
  19. Category int `json:"-"`
  20. Param string `json:"param"`
  21. ParamName string `json:"param_name"`
  22. }
  23. // ReqLabel def.
  24. type ReqLabel struct {
  25. Category int `form:"category" validate:"required"`
  26. Param string `form:"param" validate:"required"` // pubtime for time labels, typeid for type labels
  27. Title string `form:"title"`
  28. ID int `form:"id"`
  29. }
  30. // LabelDB is the index label in DB
  31. type LabelDB struct {
  32. LabelCore
  33. Mtime time.Time `json:"Mtime"`
  34. }
  35. // SameType tells whether the given label has the exact same type with the V
  36. func (v *LabelDB) SameType(given *LabelDB) bool {
  37. return v.Category == given.Category && v.Param == given.Param && v.CatType == given.CatType
  38. }
  39. // LabelCore is core of Label
  40. type LabelCore struct {
  41. ID int64 `json:"id"`
  42. Name string `json:"name"`
  43. Param string `json:"param"`
  44. ParamName string `json:"param_name"`
  45. Value string `json:"value"`
  46. Category int32 `json:"category"`
  47. CatType int `json:"cat_type"`
  48. Valid int `json:"valid"`
  49. Position int `json:"position"`
  50. }
  51. // LabelList is used to list in TV CMS
  52. type LabelList struct {
  53. LabelCore
  54. Mtime string `json:"mtime"`
  55. Stime string `json:"stime,omitempty"`
  56. Etime string `json:"etime,omitempty"`
  57. }
  58. // PgcCondResp is pgc condition response structure
  59. type PgcCondResp struct {
  60. Code int `json:"code"`
  61. Message string `json:"message"`
  62. Result *PgcCond `json:"result"`
  63. }
  64. // PgcCond def.
  65. type PgcCond struct {
  66. Filter []*Cond `json:"filter"`
  67. }
  68. // Cond def.
  69. type Cond struct {
  70. ID string `json:"id"`
  71. Name string `json:"name"`
  72. Value []*CondV `json:"value"`
  73. }
  74. // CondV def.
  75. type CondV struct {
  76. ID string `json:"id"`
  77. Name string `json:"name"`
  78. }
  79. // UgcTime is used to add time labels for ugc
  80. type UgcTime struct {
  81. UTime
  82. Category int32 `form:"category" validate:"required"`
  83. Name string `form:"name" validate:"required"`
  84. }
  85. // EditUgcTime def.
  86. type EditUgcTime struct {
  87. ID int64 `form:"id" validate:"required"`
  88. Name string `form:"name" validate:"required"`
  89. UTime
  90. }
  91. // UTime is used for storage in DB by json
  92. type UTime struct {
  93. Stime int64 `form:"stime" validate:"required" json:"stime"`
  94. Etime int64 `form:"etime" validate:"required" json:"etime"`
  95. }
  96. // TimeV picks time value in Json
  97. func (tm *UTime) TimeV() string {
  98. timeV, _ := json.Marshal(tm)
  99. return string(timeV)
  100. }
  101. // ToList transforms LabelDB to LabelList
  102. func (v *LabelDB) ToList() *LabelList {
  103. res := &LabelList{
  104. LabelCore: v.LabelCore,
  105. Mtime: v.Mtime.Time().Format(mysql.TimeFormat),
  106. }
  107. if v.CatType == UgcLabel && v.Param == ParamUgctime && v.Value != "" {
  108. utime := UTime{}
  109. if err := json.Unmarshal([]byte(v.Value), &utime); err != nil {
  110. return res
  111. }
  112. res.Stime = xtime.Unix(utime.Stime, 0).Format(mysql.TimeFormat)
  113. res.Etime = xtime.Unix(utime.Etime, 0).Format(mysql.TimeFormat)
  114. }
  115. return res
  116. }
  117. // TableName tv_rank
  118. func (v LabelDB) TableName() string {
  119. return "tv_label"
  120. }
  121. // FromArcTp def.
  122. func (v *LabelDB) FromArcTp(tp *arccli.Tp, paramName string) {
  123. v.LabelCore = LabelCore{
  124. Name: tp.Name,
  125. Value: fmt.Sprintf("%d", tp.ID),
  126. Category: tp.Pid,
  127. Param: ParamTypeid,
  128. ParamName: paramName,
  129. CatType: UgcLabel,
  130. Valid: 1,
  131. }
  132. }
  133. // FromPgcCond def.
  134. func (v *LabelDB) FromPgcCond(value *CondV, cond *Cond, category int32) {
  135. v.LabelCore = LabelCore{
  136. Name: value.Name,
  137. Value: value.ID,
  138. Category: category,
  139. Param: cond.ID,
  140. ParamName: cond.Name,
  141. CatType: PgcLabel,
  142. Valid: 1,
  143. }
  144. }
  145. // FromUgcTime def.
  146. func (v *LabelDB) FromUgcTime(tm *UgcTime, paramName string) {
  147. v.LabelCore = LabelCore{
  148. Name: tm.Name,
  149. Value: tm.TimeV(),
  150. Category: tm.Category,
  151. Param: ParamUgctime,
  152. ParamName: paramName,
  153. CatType: UgcLabel,
  154. Valid: 1,
  155. }
  156. }