advertisebean.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package model
  2. import (
  3. "active/constant"
  4. "active/tools"
  5. "encoding/json"
  6. "fmt"
  7. "git.jiaxianghudong.com/go/logs"
  8. "git.jiaxianghudong.com/webs/pkg/rds"
  9. "log"
  10. "time"
  11. )
  12. var AdvertiseBeanConf = &AdvertiseBeanConfig{}
  13. var AdvertiseBeanRecordTime = ""
  14. type AdvertiseBeanConfig struct {
  15. Times int64 `json:"times" yaml:"times"`
  16. Total int64 `json:"total" yaml:"total"`
  17. Second int64 `json:"second" yaml:"second"`
  18. List []struct {
  19. Ball int64 `json:"ball" yaml:"ball"`
  20. Rate int64 `json:"rate" yaml:"rate"`
  21. } `json:"list" yaml:"list"`
  22. }
  23. func AdvertisePretreatment() {
  24. tools.ReloadYaml("advertisebean.yaml", AdvertiseBeanConf)
  25. var rate int64
  26. for i := range AdvertiseBeanConf.List {
  27. rate += AdvertiseBeanConf.List[i].Rate
  28. AdvertiseBeanConf.List[i].Rate = rate
  29. }
  30. fmt.Printf("处理后数据 %+v \n", AdvertiseBeanConf)
  31. }
  32. type AdvertiseBeanUserInfo struct {
  33. OpenTime int64 `json:"open_time"` //开启时间
  34. Times int64 `json:"times"` //剩余次数
  35. BeanNumber int64 `json:"bean_number"` //领取豆豆数
  36. }
  37. func (this *AdvertiseBeanUserInfo) GetData(UserID string, tm string) {
  38. data := rds.Redis.HGet(fmt.Sprintf(constant.ADVERTISEBEAN, tm), UserID).Val()
  39. if data != "" {
  40. err := json.Unmarshal([]byte(data), this)
  41. if err != nil {
  42. logs.Errorf("AdvertiseBeanUserInfo[%s] json err:%v", UserID, err)
  43. }
  44. } else {
  45. this.Times = AdvertiseBeanConf.Times
  46. //this.OpenTime = time.Now().Unix()
  47. }
  48. }
  49. func (this AdvertiseBeanUserInfo) SetData(UserID string, tm string) bool {
  50. data, _ := json.Marshal(this)
  51. rds.Redis.HSet(fmt.Sprintf(constant.ADVERTISEBEAN, tm), UserID, string(data))
  52. if AdvertiseBeanRecordTime == "" || AdvertiseBeanRecordTime != tm {
  53. end, _ := time.Parse("2006-01-02", tm)
  54. expireAt := end.Add(constant.ADVERTISETIMEOUT)
  55. log.Printf("---更新看广告领取豆数据过期时间: expireAt:%v \n", expireAt)
  56. rds.Redis.ExpireAt(fmt.Sprintf(constant.ADVERTISEBEAN, tm), expireAt)
  57. AdvertiseBeanRecordTime = tm
  58. }
  59. return true
  60. }