model.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package model
  2. import (
  3. "encoding/json"
  4. // "fmt"
  5. // "time"
  6. "go-common/library/log"
  7. "github.com/pkg/errors"
  8. )
  9. // Trend enum
  10. const (
  11. TrendHold = 0
  12. TrendUp = 1
  13. TrendDown = 2
  14. )
  15. // Charge 充电,amount: 总充电数
  16. func (e *RankElecPrepUPProto) Charge(payMID int64, amount int64, newElecer bool) {
  17. // 检查是否在榜,如果在榜直接更新
  18. ele := e.Find(payMID)
  19. if ele != nil {
  20. ele.Amount = amount
  21. e.update(ele)
  22. return
  23. }
  24. if newElecer {
  25. e.Count++
  26. e.CountUPTotalElec++
  27. }
  28. // 排行榜列表为最大长度 且 榜单末位的充电数 >= 新的充电数,返回
  29. if len(e.List) >= e.Size_ && e.List[len(e.List)-1].Amount >= amount {
  30. return
  31. }
  32. // 没有在榜且可以上榜则插入原榜
  33. newEle := &RankElecPrepElementProto{
  34. Rank: -1,
  35. MID: payMID,
  36. TrendType: TrendHold,
  37. Amount: amount,
  38. }
  39. e.insert(newEle)
  40. }
  41. // UpdateMessage 更新留言
  42. func (e *RankElecPrepUPProto) UpdateMessage(payMID int64, message string, hidden bool) {
  43. ele := e.Find(payMID)
  44. if ele != nil {
  45. ele.Message = &ElecMessageProto{
  46. Message: message,
  47. Hidden: hidden,
  48. }
  49. }
  50. }
  51. // Find 获得榜单中payMID的排名信息,如果不存在则返回nil
  52. func (e *RankElecPrepUPProto) Find(payMID int64) (ele *RankElecPrepElementProto) {
  53. for _, r := range e.List {
  54. if r.MID == payMID {
  55. ele = r
  56. return
  57. }
  58. }
  59. return nil
  60. }
  61. // 更新排名
  62. func (e *RankElecPrepUPProto) update(ele *RankElecPrepElementProto) {
  63. for i := range e.List {
  64. if e.List[i] == nil {
  65. log.Error("ElecPrepUPRank: %s, index: %d, ele: %+v", e, i, ele)
  66. }
  67. if e.List[i].MID != ele.MID && e.List[i].Amount >= ele.Amount {
  68. continue
  69. }
  70. newRank := e.List[i].Rank
  71. if err := e.shift(i, ele.Rank-1); err != nil {
  72. log.Error("ElecPrepUPRank.update err: %+v, ele: %+v in rank: %s ", err, ele, e)
  73. return
  74. }
  75. ele.Rank = newRank
  76. e.List[i] = ele
  77. break
  78. }
  79. }
  80. // 插入新排名到榜单
  81. func (e *RankElecPrepUPProto) insert(ele *RankElecPrepElementProto) {
  82. for i := range e.List {
  83. if e.List[i].Amount >= ele.Amount {
  84. continue
  85. }
  86. // 找到新排名的位置,并插入
  87. ele.Rank = e.List[i].Rank
  88. if len(e.List) >= e.Size_ { // 榜单已满
  89. if err := e.shift(i, len(e.List)-1); err != nil {
  90. log.Error("ElecPrepUPRank.insert err: %+v, ele: %+v in rank: %s ", err, ele, e)
  91. return
  92. }
  93. } else { // 榜单未满
  94. tailEle := e.List[len(e.List)-1]
  95. if err := e.shift(i, len(e.List)-1); err != nil {
  96. log.Error("ElecPrepUPRank.insert err: %+v, ele: %+v in rank: %s ", err, ele, e)
  97. return
  98. }
  99. tailEle.Rank++
  100. e.List = append(e.List, tailEle)
  101. }
  102. e.List[i] = ele
  103. break
  104. }
  105. // 插入到末位
  106. if ele.Rank < 0 {
  107. e.List = append(e.List, ele)
  108. ele.Rank = len(e.List)
  109. }
  110. }
  111. // shift 将排名从 fromRank 起后移一位到 toRank,原 toRank 会被丢弃,原 fromRank 将会空出
  112. func (e *RankElecPrepUPProto) shift(fromIndex, toIndex int) (err error) {
  113. if fromIndex > toIndex {
  114. err = errors.Errorf("shift from(%d) > to(%d)", fromIndex, toIndex)
  115. return
  116. }
  117. if len(e.List)-1 < toIndex {
  118. err = errors.Errorf("shift out of range [%d,%d], just have: %d", fromIndex, toIndex, len(e.List))
  119. return
  120. }
  121. lastEle := e.List[fromIndex]
  122. e.List[fromIndex] = nil
  123. for i := fromIndex + 1; i <= toIndex; i++ {
  124. lastEle.Rank++
  125. e.List[i], lastEle = lastEle, e.List[i]
  126. }
  127. return
  128. }
  129. // Message binlog databus msg.
  130. type Message struct {
  131. Action string `json:"action"`
  132. Table string `json:"table"`
  133. New json.RawMessage `json:"new"`
  134. Old json.RawMessage `json:"old"`
  135. }
  136. // ElecUserSetting .
  137. type ElecUserSetting int32
  138. // ShowMessage 充电榜单是否显示留言
  139. func (e ElecUserSetting) ShowMessage() bool {
  140. return (e & 0x1) > 0
  141. }