rebate.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package model
  2. import (
  3. "active/constant"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "math"
  8. "sync/atomic"
  9. "time"
  10. "git.jiaxianghudong.com/go/logs"
  11. "git.jiaxianghudong.com/webs/pkg/rds"
  12. )
  13. var rebateExpireAt int32
  14. //RebateData 投资返利存储
  15. type RebateData struct {
  16. Levels map[int]*LevelData `json:"levels"` //档次
  17. }
  18. //LevelData 等级数据
  19. type LevelData struct {
  20. Tm time.Time `json:"paytime"` //支付时间 时间戳
  21. Status int `json:"status"` //领取状态 位运算1+2+4
  22. }
  23. //RebatePropData 投资奖励
  24. type RebatePropData struct {
  25. Investment []RebatePropInfos `json:"investment_rebate"`
  26. }
  27. //RebatePropInfos 奖励具体信息
  28. type RebatePropInfos struct {
  29. Amount int64 `json:"investment_amount"`
  30. LoginDay1 []RebatePropDetails `json:"login_day1"`
  31. LoginDay2 []RebatePropDetails `json:"login_day2"`
  32. LoginDay3 []RebatePropDetails `json:"login_day3"`
  33. }
  34. //RebatePropDetails 奖励具体详细信息
  35. type RebatePropDetails struct {
  36. Prop int32 `json:"props_id"`
  37. Num int64 `json:"num"`
  38. }
  39. //GetRebateData 获取累充用户存储数据
  40. func GetRebateData(UserID string, tm string) *RebateData {
  41. data := rds.Redis.HGet(fmt.Sprintf(constant.REBATEDATA, tm), UserID).Val()
  42. rd := new(RebateData)
  43. if data != "" {
  44. err := json.Unmarshal([]byte(data), &rd)
  45. if err != nil {
  46. logs.Errorf("redpacket[%s] GetRebateData json err:%v", UserID, err)
  47. return rd
  48. }
  49. }
  50. return rd
  51. }
  52. //SetRebateData 设置用户存储
  53. func (t *RebateData) SetRebateData(UserID, tm, endtm string) bool {
  54. da, _ := json.Marshal(t)
  55. rds.Redis.HSet(fmt.Sprintf(constant.REBATEDATA, tm), UserID, string(da))
  56. if atomic.CompareAndSwapInt32(&rebateExpireAt, 0, 1) {
  57. end, _ := time.Parse("2006-01-02 15:04:05", endtm)
  58. expireAt := end.Add(constant.REBATEDATATIMEOUT)
  59. log.Printf("---更新投资返利数据过期时间: expireAt:%v", expireAt)
  60. rds.Redis.ExpireAt(fmt.Sprintf(constant.REBATEDATA, tm), expireAt)
  61. }
  62. return true
  63. }
  64. //GetPropByLevel 获取奖励
  65. func GetPropByLevel(info string, typ, level int) map[int32]int64 {
  66. r, err := parseRebate(info)
  67. if err != nil {
  68. return nil
  69. }
  70. if len(r.Investment) < typ {
  71. return nil
  72. }
  73. rd := []RebatePropDetails{}
  74. switch level {
  75. case 1:
  76. rd = r.Investment[typ-1].LoginDay1
  77. case 2:
  78. rd = r.Investment[typ-1].LoginDay2
  79. case 3:
  80. rd = r.Investment[typ-1].LoginDay3
  81. }
  82. data := make(map[int32]int64, 0)
  83. for _, v := range rd {
  84. data[v.Prop] = v.Num
  85. }
  86. return data
  87. }
  88. //ConsumePropByType 消耗道具
  89. func ConsumePropByType(info string, typ int) map[int32]int64 {
  90. r, err := parseRebate(info)
  91. if err != nil {
  92. return nil
  93. }
  94. if len(r.Investment) < typ {
  95. return nil
  96. }
  97. data := make(map[int32]int64, 0)
  98. amout := r.Investment[typ-1].Amount
  99. if amout < 0 {
  100. amout = -amout
  101. }
  102. data[17] = -amout
  103. return data
  104. }
  105. func parseRebate(info string) (RebatePropData, error) {
  106. var r RebatePropData
  107. if err := json.Unmarshal([]byte(info), &r); err != nil {
  108. logs.Errorf("parseRebate json data:%s err:%v", info, err)
  109. return r, err
  110. }
  111. return r, nil
  112. }
  113. //CheckSubDay 计算两个时间点的相差天数
  114. func CheckSubDay(tm time.Time) int {
  115. dt1 := time.Date(tm.Year(), tm.Month(), tm.Day(), 0, 0, 0, 0, time.Local)
  116. dt2 := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.Local)
  117. return int(math.Ceil(dt2.Sub(dt1).Hours() / 24))
  118. }