draw.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package model
  2. import (
  3. "active/constant"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "git.jiaxianghudong.com/go/logs"
  8. "git.jiaxianghudong.com/webs/pkg/rds"
  9. )
  10. //UserDrawData 用户抽奖数据
  11. type UserDrawData struct {
  12. Special int `yaml:"special" json:"special"` // 1小王 2大王 3 双王
  13. Indexs []int `yaml:"indexs" json:"indexs"` //位置集合
  14. PropInfo []OpenCardsInfo `yaml:"propinfo" json:"propinfo"`
  15. }
  16. //OpenCardsInfo 翻牌结果
  17. type OpenCardsInfo struct {
  18. Index int `json:"index"`
  19. PropID int `json:"propid"` //道具ID
  20. Num int `json:"num"` //数量
  21. Special int `json:"special"` // 特殊 1 小王 2 大王
  22. Status int `json:"status"` //0 剩余没翻开 1 翻开
  23. }
  24. //RollData 轮播数据
  25. type RollData struct {
  26. NickName string `json:"nickname"`
  27. UserID uint32 `json:"userid"`
  28. PropID int `json:"propid"`
  29. Num int `json:"num"` //数量
  30. Special int `json:"special"` // 特殊 1 小王 2 大王 3 s双王
  31. Time string `json:"time"`
  32. }
  33. //GetUserDrawData 获取用户翻牌数据
  34. func GetUserDrawData(UserID string) *UserDrawData {
  35. data := rds.Redis.HGet(fmt.Sprintf(constant.DrawUserData, time.Now().Format("20060102")), UserID).Val()
  36. ud := &UserDrawData{}
  37. if data != "" {
  38. err := json.Unmarshal([]byte(data), &ud)
  39. if err != nil {
  40. logs.Errorf("draw[%s] GetUserDrawData json err:%v", UserID, err)
  41. return ud
  42. }
  43. }
  44. return ud
  45. }
  46. //SetUserDrawData 设置用户存储
  47. func (t *UserDrawData) SetUserDrawData(UserID string) error {
  48. da, _ := json.Marshal(t)
  49. rds.Redis.HSet(fmt.Sprintf(constant.DrawUserData, time.Now().Format("20060102")), UserID, string(da))
  50. rds.Redis.Expire(fmt.Sprintf(constant.DrawUserData, time.Now().Format("20060102")), constant.ExpireAtTime)
  51. return nil
  52. }
  53. //GetRollData 获取轮播数据
  54. func GetRollData() []*RollData {
  55. rdm := []*RollData{}
  56. data := rds.Redis.Get(constant.DrawROLL).Val()
  57. if data != "" {
  58. err := json.Unmarshal([]byte(data), &rdm)
  59. if err != nil {
  60. logs.Errorf("rolldata get json err:%v", err)
  61. return rdm
  62. }
  63. }
  64. return rdm
  65. }
  66. //AddRollData 添加滚动数据
  67. func AddRollData(rd *RollData) {
  68. rdm := GetRollData()
  69. if len(rdm) < 50 {
  70. rdm = append(rdm, rd)
  71. } else {
  72. rdm = append(rdm, rd)
  73. rdm = rdm[len(rdm)-50:]
  74. }
  75. data, err := json.Marshal(rdm)
  76. if err != nil {
  77. logs.Errorf("rolldata add json err:%v", err)
  78. }
  79. rds.Redis.Set(constant.DrawROLL, string(data), 0)
  80. }