preference.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package model
  2. import (
  3. "active/constant"
  4. "encoding/json"
  5. "time"
  6. "git.jiaxianghudong.com/go/logs"
  7. )
  8. //PreferentialData 特惠礼包存储数据
  9. type PreferentialData struct {
  10. PayTime string `yaml:"pay_time" json:"pay_time"`
  11. Continuous int `yaml:"continuous" json:"continuous"`
  12. }
  13. //PreferenceTh 特惠礼包
  14. type PreferenceTh struct {
  15. Th1 []int `json:"th_1"`
  16. Th2 []int `json:"th_2"`
  17. Th3 []int `json:"th_3"`
  18. Th4 []int `json:"th_4"`
  19. }
  20. // //PreferenceDay 特惠连续天数
  21. // type PreferenceDay struct {
  22. // Level1 int `json:"levle_1"` //1 待领取 3已领取 5未能领取
  23. // Level2 int `json:"levle_2"`
  24. // Level3 int `json:"levle_3"`
  25. // }
  26. //PreferenceRspData 特惠参数
  27. type PreferenceRspData struct {
  28. LastTag string `json:"last_pay_th"`
  29. Data *PreferenceTh `json:"data"`
  30. }
  31. //GetPreferenceDay 获取连续日数领取状态
  32. func (pd *PreferentialData) GetPreferenceDay() []int {
  33. if pd.PayTime != "" {
  34. if time.Now().Format("2006-01-02") == pd.PayTime {
  35. //今日参与
  36. return GetPayTodayStaus(pd.Continuous)
  37. } else if time.Unix(time.Now().Unix()-86400, 0).Format("2006-01-02") == pd.PayTime {
  38. //昨日参与
  39. return GetPayYesterDayStaus(pd.Continuous)
  40. }
  41. }
  42. //未参与,前天之前参与重新计算
  43. return []int{1, 5, 5}
  44. }
  45. //GetPayTodayStaus 获取状态
  46. func GetPayTodayStaus(continuous int) []int {
  47. if continuous == 1 {
  48. return []int{3, 5, 5}
  49. } else if continuous == 2 {
  50. return []int{3, 3, 5}
  51. }
  52. return []int{3, 3, 3}
  53. }
  54. //GetPayYesterDayStaus 获取昨日状态
  55. func GetPayYesterDayStaus(continuous int) []int {
  56. if continuous == 1 {
  57. return []int{3, 1, 5}
  58. }
  59. return []int{3, 3, 1}
  60. }
  61. //GetPreferential 获取存储
  62. func (pd *PreferentialData) GetPreferential(data string) {
  63. if len(data) > 0 {
  64. err := json.Unmarshal([]byte(data), &pd)
  65. if err != nil {
  66. logs.Errorf("GetPreferential: json data:%s err:%v", data, err)
  67. }
  68. }
  69. }
  70. //GetPreference 获取特惠等级数据
  71. func GetPreference(userRedisInfo map[string]string) *PreferenceRspData {
  72. pt := &PreferenceTh{}
  73. for _, tag := range constant.PreferenceGoods {
  74. pd := &PreferentialData{}
  75. pd.GetPreferential(userRedisInfo[tag])
  76. switch tag {
  77. case "th_1":
  78. pt.Th1 = pd.GetPreferenceDay()
  79. case "th_2":
  80. pt.Th2 = pd.GetPreferenceDay()
  81. case "th_3":
  82. pt.Th3 = pd.GetPreferenceDay()
  83. case "th_4":
  84. pt.Th4 = pd.GetPreferenceDay()
  85. }
  86. }
  87. lastTag := "th_1"
  88. if v, ok := userRedisInfo["last_pay_th"]; ok {
  89. lastTag = v
  90. }
  91. return &PreferenceRspData{
  92. Data: pt,
  93. LastTag: lastTag,
  94. }
  95. }