model.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package model
  2. import (
  3. smodel "go-common/app/service/main/relation/model"
  4. "go-common/library/time"
  5. "sort"
  6. )
  7. // Relation is
  8. type Relation struct {
  9. ID int64 `json:"id" gorm:"column:id"`
  10. Mid int64 `json:"mid" gorm:"column:mid"`
  11. Fid int64 `json:"fid" gorm:"column:fid"`
  12. Attribute uint32 `json:"attribute" gorm:"column:attribute"`
  13. Status int8 `json:"status" gorm:"column:status"`
  14. Source int8 `json:"source" gorm:"column:source"`
  15. CTime time.Time `json:"ctime" gorm:"column:ctime"`
  16. MTime time.Time `json:"mtime" gorm:"column:mtime"`
  17. Relation uint32 `json:"relation"`
  18. }
  19. // Stat is
  20. type Stat struct {
  21. ID int64 `json:"id" gorm:"column:id"`
  22. Mid int64 `json:"mid" gorm:"column:mid"`
  23. Following int64 `json:"following" gorm:"column:following"`
  24. Whisper int64 `json:"whisper" gorm:"column:whisper"`
  25. Black int64 `json:"black" gorm:"column:black"`
  26. Follower int64 `json:"follower" gorm:"column:follower"`
  27. CTime time.Time `json:"ctime" gorm:"column:ctime"`
  28. MTime time.Time `json:"mtime" gorm:"column:mtime"`
  29. }
  30. // ParseRelation is
  31. func (r *Relation) ParseRelation() {
  32. r.Relation = smodel.Attr(r.Attribute)
  33. }
  34. // Follower is
  35. type Follower struct {
  36. *Relation
  37. MemberName string `json:"member_name"`
  38. FollowerName string `json:"follower_name"`
  39. }
  40. // Following is
  41. type Following struct {
  42. *Relation
  43. MemberName string `json:"member_name"`
  44. FollowingName string `json:"following_name"`
  45. }
  46. // RelationList is
  47. type RelationList []*Relation
  48. // FollowersList is
  49. type FollowersList []*Follower
  50. // FollowingsList is
  51. type FollowingsList []*Following
  52. func (rl RelationList) Len() int {
  53. return len(rl)
  54. }
  55. func (rl RelationList) Swap(i, j int) {
  56. rl[i], rl[j] = rl[j], rl[i]
  57. }
  58. func (rl RelationList) Less(i, j int) bool {
  59. return rl[i].MTime < rl[j].MTime
  60. }
  61. // Paginate is
  62. func (rl RelationList) Paginate(skip int, size int) RelationList {
  63. if skip > len(rl) {
  64. skip = len(rl)
  65. }
  66. end := skip + size
  67. if end > len(rl) {
  68. end = len(rl)
  69. }
  70. return rl[skip:end]
  71. }
  72. // FilterMTimeFrom is
  73. func (rl RelationList) FilterMTimeFrom(from time.Time) RelationList {
  74. res := make(RelationList, 0)
  75. for _, r := range rl {
  76. if r.MTime >= from {
  77. res = append(res, r)
  78. }
  79. }
  80. return res
  81. }
  82. // FilterMTimeTo is
  83. func (rl RelationList) FilterMTimeTo(to time.Time) RelationList {
  84. res := make(RelationList, 0)
  85. for _, r := range rl {
  86. if r.MTime <= to {
  87. res = append(res, r)
  88. }
  89. }
  90. return res
  91. }
  92. // OrderByMTime is
  93. func (rl RelationList) OrderByMTime(desc bool) {
  94. sort.Sort(rl)
  95. }
  96. // FollowersList is
  97. func (rl RelationList) FollowersList() FollowersList {
  98. res := make(FollowersList, 0, len(rl))
  99. for _, r := range rl {
  100. res = append(res, &Follower{
  101. Relation: r,
  102. })
  103. }
  104. return res
  105. }
  106. // FollowingsList is
  107. func (rl RelationList) FollowingsList() FollowingsList {
  108. res := make(FollowingsList, 0, len(rl))
  109. for _, r := range rl {
  110. res = append(res, &Following{
  111. Relation: r,
  112. })
  113. }
  114. return res
  115. }