invite.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package model
  2. import (
  3. xtime "go-common/library/time"
  4. )
  5. const (
  6. // StatusOk ok
  7. StatusOk = 0
  8. // StatusUsed used
  9. StatusUsed = 1
  10. // StatusExpires expire
  11. StatusExpires = 2
  12. )
  13. // Invite invaite
  14. type Invite struct {
  15. Status int64 `json:"status"`
  16. Mid int64 `json:"mid"`
  17. Code string `json:"invite_code"`
  18. IP uint32 `json:"-"` // legacy IP field
  19. IPng []byte `json:"-"`
  20. Ctime xtime.Time `json:"buy_time"`
  21. Expires int64 `json:"expires"`
  22. Imid int64 `json:"invited_mid,omitempty"`
  23. UsedAt int64 `json:"used_at,omitempty"`
  24. Mtime xtime.Time `json:"-"`
  25. }
  26. // FillStatus fill status
  27. func (inv *Invite) FillStatus(now int64) {
  28. if inv.Used() {
  29. inv.Status = StatusUsed
  30. return
  31. }
  32. if inv.Expired(now) {
  33. inv.Status = StatusExpires
  34. return
  35. }
  36. inv.Status = StatusOk
  37. }
  38. // Used use
  39. func (inv *Invite) Used() bool {
  40. return inv.UsedAt > 0 && inv.Imid > 0
  41. }
  42. // Expired expire
  43. func (inv *Invite) Expired(now int64) bool {
  44. return now > inv.Expires
  45. }
  46. // SortInvitesByCtimeDesc sort
  47. type SortInvitesByCtimeDesc []*Invite
  48. // Len len
  49. func (invs SortInvitesByCtimeDesc) Len() int {
  50. return len(invs)
  51. }
  52. // Less less
  53. func (invs SortInvitesByCtimeDesc) Less(i, j int) bool {
  54. return int64(invs[i].Ctime) > int64(invs[j].Ctime)
  55. }
  56. // Swap swap
  57. func (invs SortInvitesByCtimeDesc) Swap(i, j int) {
  58. tmp := invs[i]
  59. invs[i] = invs[j]
  60. invs[j] = tmp
  61. }
  62. // InviteStat stat
  63. type InviteStat struct {
  64. Mid int64 `json:"mid"`
  65. CurrentLimit int64 `json:"current_limit"`
  66. CurrentBought int64 `json:"current_bought"`
  67. TotalBought int64 `json:"total_bought"`
  68. TotalUsed int64 `json:"total_used"`
  69. InviteCodes []*Invite `json:"invite_codes"`
  70. }