usertag.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package dao
  2. import (
  3. "fmt"
  4. "runtime/debug"
  5. "sort"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "bet24.com/database"
  10. "bet24.com/log"
  11. )
  12. var tagMgr *usertagManager
  13. type info struct {
  14. UserId int // 用户id
  15. NickName string // 昵称
  16. VipLevel int // vip等级
  17. PayMoney float64 // 累计充值额度
  18. ColorValue int // 色值
  19. }
  20. type usertagManager struct {
  21. list []*info
  22. lock *sync.RWMutex
  23. }
  24. func RunTagManager() {
  25. tagMgr = new(usertagManager)
  26. tagMgr.lock = &sync.RWMutex{}
  27. go tagMgr.doRefresh()
  28. }
  29. func (this *usertagManager) doRefresh() {
  30. this.loadData()
  31. ticker := time.NewTicker(3 * time.Minute)
  32. go func(t *time.Ticker) {
  33. for {
  34. select {
  35. case <-t.C:
  36. this.loadData()
  37. }
  38. }
  39. }(ticker)
  40. }
  41. func (this *usertagManager) loadData() {
  42. list := this.doAction()
  43. this.lock.Lock()
  44. defer this.lock.Unlock()
  45. this.list = list
  46. }
  47. func (this *usertagManager) getInfo(userId int) *info {
  48. this.lock.RLock()
  49. defer this.lock.RUnlock()
  50. for _, v := range this.list {
  51. if v.UserId == userId {
  52. return v
  53. }
  54. }
  55. return nil
  56. }
  57. func GetUserTagsList(colorValue, pageIndex, pageSize int) (int, []*info) {
  58. return tagMgr.getList(colorValue, pageIndex, pageSize)
  59. }
  60. func (this *usertagManager) getList(colorValue, pageIndex, pageSize int) (int, []*info) {
  61. var list []*info
  62. this.lock.RLock()
  63. for _, v := range this.list {
  64. if v.ColorValue == colorValue {
  65. list = append(list, v)
  66. }
  67. }
  68. this.lock.RUnlock()
  69. total := len(list)
  70. if total <= 0 {
  71. return total, nil
  72. }
  73. start := (pageIndex - 1) * pageSize
  74. end := pageIndex * pageSize
  75. if start < 0 || start > end {
  76. return 0, nil
  77. }
  78. if end > total {
  79. end = total
  80. }
  81. return total, list[start:end]
  82. }
  83. func (this *usertagManager) getColor(nickName string, colorValue, vipLevel int) string {
  84. colorName := "black"
  85. switch colorValue {
  86. case 3:
  87. colorName = "red"
  88. case 2:
  89. colorName = "orange"
  90. case 1:
  91. colorName = "#3bb4f2"
  92. }
  93. return fmt.Sprintf("<font color='%s'><b>%s</b></font>(%d)", colorName, nickName, vipLevel)
  94. }
  95. func (this *usertagManager) doAction() []*info {
  96. defer func() {
  97. if err := recover(); err != nil {
  98. log.Error("transaction recover err %v", err)
  99. log.Error("%s", debug.Stack())
  100. }
  101. }()
  102. var list []*info
  103. statement := database.NewStatement()
  104. statement.SetNeedReturnValue(false)
  105. statement.SetOpenRecordSet(true)
  106. statement.SetProcName("Manage_AllUser_GetTags")
  107. sqlstring := statement.GenSql()
  108. retRows := CenterDB.ExecSql(sqlstring)
  109. rowLen := len(retRows)
  110. if rowLen <= 0 {
  111. return list
  112. }
  113. for i := 0; i < rowLen; i++ {
  114. ret := retRows[i]
  115. var out info
  116. out.UserId = int((*ret[0].(*interface{})).(int64))
  117. out.VipLevel = int((*ret[1].(*interface{})).(int64))
  118. payMoney := string((*ret[2].(*interface{})).([]byte))
  119. out.PayMoney, _ = strconv.ParseFloat(payMoney, 64)
  120. out.ColorValue = int((*ret[3].(*interface{})).(int64))
  121. out.NickName = (*ret[4].(*interface{})).(string)
  122. out.NickName = this.getColor(out.NickName, out.ColorValue, out.VipLevel)
  123. list = append(list, &out)
  124. }
  125. sort.SliceStable(list, func(i, j int) bool {
  126. return list[i].PayMoney > list[j].PayMoney
  127. })
  128. sort.SliceStable(list, func(i, j int) bool {
  129. return list[i].VipLevel > list[j].VipLevel
  130. })
  131. sort.SliceStable(list, func(i, j int) bool {
  132. return list[i].ColorValue > list[j].ColorValue
  133. })
  134. return list
  135. }