exchangecode.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package models
  2. // 兑换码
  3. import (
  4. "box-gm/utils"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "log"
  9. "sync"
  10. "time"
  11. "github.com/astaxie/beego"
  12. "github.com/astaxie/beego/validation"
  13. "github.com/garyburd/redigo/redis"
  14. )
  15. type ExchangeItemsInfo struct {
  16. ItemId int `json:"id"` // 道具ID
  17. Count int `json:"count"` // 数量
  18. Weight int `json:"weight"` // 权重
  19. }
  20. type ExchangeCodeInfo struct {
  21. Pcode string `json:"pcode"` // 兑换码
  22. AwardTimes int64 `json:"awardTimes"` // 领奖次数
  23. CreateTime int64 `json:"creatTime"` // 创建时间
  24. ModifyTime int64 `json:"modifyTime"` // 修改时间
  25. ExpireTime int64 `json:"expireTime"` // 过期时间
  26. Items []*ExchangeItemsInfo `json:"items"` // 随机奖励列表
  27. }
  28. type ExchangeCodeInfoArray []*ExchangeCodeInfo
  29. var exchangeCodeList ExchangeCodeInfoArray
  30. var ecMutex sync.Mutex
  31. var MAIN_KEY = "exchange:code"
  32. // 初始化兑换码
  33. func InitExchangeCodeInfo() {
  34. pool := utils.GetAccountRedisPool()
  35. if pool == nil {
  36. fmt.Println("get redis pool fail")
  37. return
  38. }
  39. rd := pool.Get()
  40. defer rd.Close()
  41. result, err := redis.Values(rd.Do("hgetall", MAIN_KEY))
  42. if err != nil {
  43. return
  44. }
  45. for i := 0; i < len(result)/2; i++ {
  46. info := &ExchangeCodeInfo{}
  47. _err := json.Unmarshal(result[i*2+1].([]byte), info)
  48. if _err == nil {
  49. exchangeCodeList = append(exchangeCodeList, info)
  50. }
  51. }
  52. beego.Info("load exchange code info", len(exchangeCodeList))
  53. }
  54. func checkExchangeCodeInfo(hf *ExchangeCodeInfo) (err error) {
  55. valid := validation.Validation{}
  56. b, _ := valid.Valid(&hf)
  57. if !b {
  58. for _, err := range valid.Errors {
  59. log.Println(err.Key, err.Message)
  60. return errors.New(err.Message)
  61. }
  62. }
  63. return nil
  64. }
  65. // 新增兑换码
  66. func AddExchangeCodeInfo(hf *ExchangeCodeInfo) error {
  67. pool := utils.GetAccountRedisPool()
  68. if pool == nil {
  69. return fmt.Errorf("get redis pool fail")
  70. }
  71. rd := pool.Get()
  72. defer rd.Close()
  73. if err := checkExchangeCodeInfo(hf); err != nil {
  74. return err
  75. }
  76. hf.CreateTime = time.Now().Unix()
  77. hf.ModifyTime = hf.CreateTime
  78. buff, err := json.Marshal(hf)
  79. _, err = rd.Do("hset", MAIN_KEY, hf.Pcode, buff)
  80. if err == nil {
  81. ecMutex.Lock()
  82. exchangeCodeList = append(exchangeCodeList, hf)
  83. ecMutex.Unlock()
  84. }
  85. return err
  86. }
  87. // 修改兑换码
  88. func UpdateExchangeCodeInfo(hf *ExchangeCodeInfo) error {
  89. pool := utils.GetAccountRedisPool()
  90. if pool == nil {
  91. return fmt.Errorf("get redis pool fail")
  92. }
  93. rd := pool.Get()
  94. defer rd.Close()
  95. if err := checkExchangeCodeInfo(hf); err != nil {
  96. return err
  97. }
  98. hf.ModifyTime = time.Now().Unix()
  99. buff, err := json.Marshal(hf)
  100. _, err = rd.Do("hset", MAIN_KEY, hf.Pcode, buff)
  101. if err != nil {
  102. return err
  103. }
  104. ecMutex.Lock()
  105. for i := 0; i < len(exchangeCodeList); i++ {
  106. if hf.Pcode == exchangeCodeList[i].Pcode {
  107. old := exchangeCodeList[i]
  108. old.AwardTimes = hf.AwardTimes
  109. old.ModifyTime = hf.ModifyTime
  110. old.ExpireTime = hf.ExpireTime
  111. old.Items = hf.Items
  112. break
  113. }
  114. }
  115. ecMutex.Unlock()
  116. return err
  117. }
  118. // 删除兑换码
  119. func DelExchangeCodeInfoById(pcode string) error {
  120. pool := utils.GetAccountRedisPool()
  121. if pool == nil {
  122. return fmt.Errorf("get redis pool fail")
  123. }
  124. rd := pool.Get()
  125. defer rd.Close()
  126. ecMutex.Lock()
  127. defer ecMutex.Unlock()
  128. _, err := rd.Do("hdel", MAIN_KEY, pcode)
  129. if err != nil {
  130. log.Println(err)
  131. return err
  132. }
  133. for i := 0; i < len(exchangeCodeList); i++ {
  134. if pcode == exchangeCodeList[i].Pcode {
  135. ii := i + 1
  136. exchangeCodeList = append(exchangeCodeList[:i], exchangeCodeList[ii:]...)
  137. }
  138. }
  139. return nil
  140. }
  141. // 获取兑换码信息
  142. func GetExchangeCodeInfoByCode(pcode string) (hf *ExchangeCodeInfo) {
  143. ecMutex.Lock()
  144. defer ecMutex.Unlock()
  145. for i := 0; i < len(exchangeCodeList); i++ {
  146. if pcode == exchangeCodeList[i].Pcode {
  147. return exchangeCodeList[i]
  148. }
  149. }
  150. return nil
  151. }
  152. func GetExchangeCodeInfo() ExchangeCodeInfoArray {
  153. ecMutex.Lock()
  154. defer ecMutex.Unlock()
  155. return exchangeCodeList
  156. }