video.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package model
  2. import (
  3. "errors"
  4. "go-common/library/time"
  5. )
  6. var (
  7. // ErrFavVideoExist error video has been favoured.
  8. ErrFavVideoExist = errors.New("error video has been favoured")
  9. // ErrFavVideoAlreadyDel error video has been unfavoured.
  10. ErrFavVideoAlreadyDel = errors.New("error video has been unfavoured")
  11. )
  12. const (
  13. bit1 = int8(1)
  14. bit2 = int8(1) << 1
  15. // StateDefaultPublic default public folder.
  16. StateDefaultPublic = int8(0) // binary 00 / int 0
  17. // StateDefaultNoPublic default private folder.
  18. StateDefaultNoPublic = int8(0) | bit1 // binary 01 / int 1
  19. // StateNormalPublic nomal public folder.
  20. StateNormalPublic = bit2 | int8(0) // binary 10 / int 2
  21. // StateNormalNoPublic nomal private folder.
  22. StateNormalNoPublic = bit2 | bit1 // binary 11 / int 3
  23. // DefaultFolderName name of favorite folder.
  24. DefaultFolderName = "默认收藏夹"
  25. )
  26. // Favorite .
  27. type Favorite struct {
  28. Fid int64 `json:"fid"`
  29. Mid int64 `json:"mid"`
  30. Name string `json:"name"`
  31. MaxCount int `json:"max_count"`
  32. CurCount int `json:"cur_count"`
  33. AttenCount int `json:"atten_count"`
  34. State int8 `json:"state"`
  35. CTime time.Time `json:"ctime"`
  36. MTime time.Time `json:"-"`
  37. Cover []*Cover `json:"cover,omitempty"`
  38. }
  39. // Archive .
  40. type Archive struct {
  41. ID int64 `json:"id"`
  42. Mid int64 `json:"mid"`
  43. Fid int64 `json:"fid"`
  44. Aid int64 `json:"aid"`
  45. CTime time.Time `json:"-"`
  46. MTime time.Time `json:"-"`
  47. }
  48. // IsPublic return true if folder is public.
  49. func (f *Favorite) IsPublic() bool {
  50. return f.State&bit1 == int8(0)
  51. }
  52. // IsDefault return true if folder is default.
  53. func (f *Favorite) IsDefault() bool {
  54. return f.State&bit2 == int8(0)
  55. }
  56. // StatePub return folder's public state.
  57. func (f *Favorite) StatePub() int8 {
  58. return f.State & bit1
  59. }
  60. // StateDef return folder's default state.
  61. func (f *Favorite) StateDef() int8 {
  62. return f.State & bit2
  63. }
  64. // IsDefault return true if state is default state.
  65. func IsDefault(state int8) bool {
  66. return (state&(int8(1)<<1) == int8(0))
  67. }
  68. // CheckPublic check user update public value in [0, 1].
  69. func CheckPublic(state int8) bool {
  70. return state == int8(0) || state == bit1
  71. }
  72. // Favorites .
  73. type Favorites []*Favorite
  74. func (f Favorites) Len() int { return len(f) }
  75. func (f Favorites) Less(i, j int) bool {
  76. if f[i].State < f[j].State {
  77. return true
  78. }
  79. if f[i].State == f[j].State && f[i].MaxCount > f[j].MaxCount {
  80. return true
  81. }
  82. if f[i].State == f[j].State && f[i].MaxCount <= f[j].MaxCount && f[i].CTime < f[j].CTime {
  83. return true
  84. }
  85. return false
  86. }
  87. func (f Favorites) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
  88. // Cover image
  89. type Cover struct {
  90. Aid int64 `json:"aid"`
  91. Pic string `json:"pic"`
  92. }