redis.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package scratchcard
  2. import (
  3. "active/internal/model/goldbrick"
  4. "encoding/json"
  5. "fmt"
  6. "git.jiaxianghudong.com/go/logs"
  7. "git.jiaxianghudong.com/webs/pkg/rds"
  8. "git.jiaxianghudong.com/webs/pkg/xgorm"
  9. "gorm.io/gorm/clause"
  10. "log"
  11. "strconv"
  12. "sync/atomic"
  13. "time"
  14. )
  15. var ScratchCard int32
  16. var ScratchCardDate = ""
  17. type UserInfo struct {
  18. UserData
  19. AwardList []ScratchCardAward `json:"award_list"`
  20. //IsWatch int64 `json:"is_watch"`
  21. }
  22. type UserData struct {
  23. Id int64 `json:"id" gorm:"id"`
  24. UserId int64 `json:"user_id" gorm:"user_id"`
  25. PlayerName string `json:"player_name" gorm:"player_name"`
  26. PlayerImg string `json:"player_img" gorm:"player_img"`
  27. PlayerSex int64 `json:"player_sex" gorm:"player_sex"`
  28. CardNumber int `json:"card_number" gorm:"card_number"`
  29. Flush bool `json:"flush" gorm:"flush"`
  30. FriendList []ScratchCardHelp `json:"friend_list" gorm:"-"`
  31. FriendJson string `json:"-" gorm:"friend_json"`
  32. UpdateTime int64 `json:"update_time" gorm:"update_time"`
  33. }
  34. func (this UserData) TableName() string {
  35. return "scratch_card_user"
  36. }
  37. //设置userdata的值
  38. func (this *UserData) SetData(userID string, tm string, endTm string) bool {
  39. data, _ := json.Marshal(this)
  40. rds.Redis.Set(fmt.Sprintf(DATAUSER, tm, userID), string(data), 2*time.Hour)
  41. db := xgorm.NewConn(goldbrick.GoldBrickGorm)
  42. db.Save(this)
  43. if atomic.CompareAndSwapInt32(&ScratchCard, 0, 1) {
  44. end, _ := time.Parse("2006-01-02 15:04:05", endTm)
  45. expireAt := end.Add(TIMEOUT)
  46. log.Printf("---更新scratch-card: expireAt:%v \n", expireAt)
  47. //rds.Redis.ExpireAt(fmt.Sprintf(DATAUSER, tm), expireAt)
  48. rds.Redis.ExpireAt(fmt.Sprintf(AWARDUSERCARD, tm), expireAt)
  49. rds.Redis.ExpireAt(fmt.Sprintf(ITEMSUSER, tm), expireAt)
  50. rds.Redis.ExpireAt(fmt.Sprintf(GEITEMSLIST, tm), expireAt)
  51. }
  52. return true
  53. }
  54. func (this *UserData) GetData(userID string, tm string, name string, img string, sex int64) {
  55. data := rds.Redis.Get(fmt.Sprintf(DATAUSER, tm, userID)).Val()
  56. //ln(data)
  57. if data != "" {
  58. err := json.Unmarshal([]byte(data), this)
  59. if err != nil {
  60. logs.Errorf("redpacket[%s] GetData json err:%v", userID, err)
  61. }
  62. }
  63. db := xgorm.NewConn(goldbrick.GoldBrickGorm)
  64. if data == "" || this.Flush == true {
  65. this.Flush = false
  66. userid, _ := strconv.ParseInt(userID, 10, 64)
  67. //fmt.Println("tm",tm)
  68. timestamp, _ := time.Parse("20060102150405", tm)
  69. db.Table(this.TableName()).Where("user_id = ?", userid).First(this)
  70. if this.Id == 0 || this.UpdateTime < timestamp.Unix() {
  71. this.UserId = userid
  72. this.CardNumber = 0
  73. this.PlayerName = name
  74. this.PlayerImg = img
  75. this.PlayerSex = sex
  76. this.FriendJson = ""
  77. this.UpdateTime = time.Now().Unix()
  78. db.Clauses(clause.OnConflict{
  79. Columns: []clause.Column{{Name: "user_id"}},
  80. DoUpdates: clause.AssignmentColumns([]string{"player_name", "player_img", "player_sex", "card_number", "friend_json"}),
  81. }).Create(this)
  82. }
  83. this.Flush = false
  84. this.FriendList = make([]ScratchCardHelp, 0)
  85. db.Table(ScratchCardHelp{}.TableName()).Where("date >= ? and help_userid = ?", timestamp.Unix(), userid).Order("date desc").Limit(10).Find(&this.FriendList)
  86. }
  87. if name != "" || img != "" {
  88. this.PlayerName = name
  89. this.PlayerSex = sex
  90. this.PlayerImg = img
  91. }
  92. }
  93. func (this *UserInfo) GetAwardList(tm string) {
  94. db := xgorm.NewConn(goldbrick.GoldBrickGorm)
  95. data := rds.Redis.Get(fmt.Sprintf(GEITEMSLIST, tm)).Val()
  96. if data == "" {
  97. timestamp, _ := time.Parse("20060102150405", tm)
  98. db.Table(ScratchCardAward{}.TableName()).Where("date >= ?", timestamp.Unix()).Order("date desc,level desc").Limit(20).Find(&this.AwardList)
  99. for i := range this.AwardList {
  100. json.Unmarshal([]byte(this.AwardList[i].Item), &this.AwardList[i].Items)
  101. }
  102. this.SetAwardList(tm)
  103. } else {
  104. err := json.Unmarshal([]byte(data), &this.AwardList)
  105. if err != nil {
  106. logs.Errorf(" GetData json err:%v", err)
  107. }
  108. }
  109. }
  110. func (this *UserInfo) SetAwardList(tm string) {
  111. data, _ := json.Marshal(this.AwardList)
  112. rds.Redis.Set(fmt.Sprintf(GEITEMSLIST, tm), string(data), TIMEOUT)
  113. }
  114. func ChangeDaySTL(tm string) {
  115. end, _ := time.Parse("2006-01-02", tm)
  116. expireAt := end.Add(DAYTIMEOUT)
  117. rds.Redis.ExpireAt(fmt.Sprintf(ITEMLIMITDAY, tm), expireAt)
  118. rds.Redis.ExpireAt(fmt.Sprintf(HELPLIMITDAY, tm), expireAt)
  119. rds.Redis.ExpireAt(fmt.Sprintf(SCRATCHFREEONE, tm), expireAt)
  120. rds.Redis.ExpireAt(fmt.Sprintf(FREEDRAWCARDDAY, tm), expireAt)
  121. rds.Redis.ExpireAt(fmt.Sprintf(DAYGETITEMLIMIT, tm), expireAt)
  122. ScratchCardDate = tm
  123. }