uservitality.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package vitality
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. notification "bet24.com/servers/micros/notification/proto"
  6. )
  7. type uservitality struct {
  8. userId int //用户id
  9. Vitality
  10. }
  11. func newUserVitality(userId int) *uservitality {
  12. u := new(uservitality)
  13. u.userId = userId
  14. u.loadUserVitality()
  15. //log.Debug("newUserVitality userId=%d %+v", userId, u)
  16. return u
  17. }
  18. func (this *uservitality) loadUserVitality() {
  19. this.Vitality = getInfo(this.userId)
  20. isUpdate := false
  21. ts := common.GetTimeStamp()
  22. //天活跃度
  23. dayIndex := common.GetDayIndex(ts)
  24. if this.DayIndex != dayIndex {
  25. this.DayPoint = 0
  26. this.DayIndex = dayIndex
  27. isUpdate = true
  28. }
  29. //周活跃度
  30. weekIndex := common.GetWeekIndex(ts)
  31. if this.WeekIndex != weekIndex {
  32. this.WeekPoint = 0
  33. this.WeekIndex = weekIndex
  34. isUpdate = true
  35. }
  36. if !isUpdate {
  37. return
  38. }
  39. //去更新
  40. this.update()
  41. }
  42. func (this *uservitality) addPoint(point int) int {
  43. if point <= 0 {
  44. log.Release("vitality.addPoint userId=%d info=%+v addPoint=%d", this.userId, this.Vitality, point)
  45. return 0
  46. }
  47. ts := common.GetTimeStamp()
  48. //天活跃度
  49. dayIndex := common.GetDayIndex(ts)
  50. if this.DayIndex == dayIndex {
  51. this.DayPoint = this.DayPoint + point
  52. } else {
  53. this.DayPoint = point
  54. this.DayIndex = dayIndex
  55. }
  56. //周活跃度
  57. weekIndex := common.GetWeekIndex(ts)
  58. if this.WeekIndex == weekIndex {
  59. this.WeekPoint = this.WeekPoint + point
  60. } else {
  61. this.WeekPoint = point
  62. this.WeekIndex = weekIndex
  63. }
  64. //去更新
  65. this.update()
  66. //通知客户端
  67. go notification.AddNotification(this.userId, notification.Notification_Vitality, "")
  68. return 1
  69. }
  70. func (this *uservitality) update() {
  71. go update(this.userId, this.Vitality)
  72. }