jyActivityOther.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package activity
  2. import (
  3. dataUtil "app.yhyue.com/moapp/jybase/date"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "app.yhyue.com/moapp/message/handler/award"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/gogf/gf/v2/os/gcfg"
  9. "github.com/gogf/gf/v2/os/gctx"
  10. "log"
  11. "time"
  12. )
  13. // WinnerInfo 当日中奖信息
  14. func (BP *JyActivity) WinnerInfo(startTime, entTime string) []map[string]interface{} {
  15. var res []map[string]interface{}
  16. // 加个redis缓存
  17. redisCache, err := redis.GetBytes(BidderPlanRedis, WinnerInfoKey)
  18. if err == nil {
  19. if err = json.Unmarshal(*redisCache, &res); err == nil {
  20. return res
  21. }
  22. }
  23. res = award.GetWinnerInfo(startTime, entTime)
  24. fmt.Println(res)
  25. if res != nil {
  26. b, err := json.Marshal(res)
  27. if err == nil && len(b) > 0 {
  28. if err = redis.PutBytes(BidderPlanRedis, WinnerInfoKey, &b, 1*60*60); err != nil {
  29. log.Println("中将人信息 缓存信息有误")
  30. }
  31. }
  32. }
  33. return res
  34. }
  35. // 问卷提交
  36. func (BP *JyActivity) Questionnaire(userId, phone, answers string) error {
  37. if inActivity, _, _ := BP.InActivity(); !inActivity {
  38. return fmt.Errorf("不在活动时间内")
  39. }
  40. //判断是否是符合条件的免费用户
  41. user := GetUserInfoRedisCache(userId)
  42. if user.VipStatus > 0 || user.Status > 0 || user.EntnicheStatus > 0 {
  43. return fmt.Errorf("用户身份不符合活动要求")
  44. }
  45. if user.Registedate < gcfg.Instance().MustGet(gctx.New(), "jyactivity.power.time", 1609430400).Int64() {
  46. return fmt.Errorf("用户注册时间不符合活动要求")
  47. }
  48. //保存问卷
  49. if ok := award.SaveQuestionnaire(userId, phone, answers); ok {
  50. if redis.Put(BidderPlanRedis, fmt.Sprintf(QuestionnaireSubmit, userId), "qs", BP.cacheLong()) != true {
  51. return fmt.Errorf("redis de22q 存储异常")
  52. }
  53. //问卷保存后--赠送7天超级订阅
  54. if err := BP.GetDailyBoonSVip(userId); err != nil {
  55. return err
  56. }
  57. } else {
  58. return fmt.Errorf("mongodb de22q 存储异常")
  59. }
  60. return nil
  61. }
  62. // ShareFinish 分享完成
  63. func (BP *JyActivity) ShareFinish(userId string) error {
  64. if _, inActivity, _ := BP.InActivity(); !inActivity {
  65. return fmt.Errorf("不在活动时间内")
  66. }
  67. if redis.Put(BidderPlanRedis, fmt.Sprintf(BidderPlanShare, userId), 1, BP.cacheLong()) != true {
  68. return fmt.Errorf("存储异常")
  69. }
  70. return nil
  71. }
  72. // GetDailyBoonDetail 查询领取情况及剩余
  73. // has 是否已经领取
  74. // todayBalance 今日余额
  75. // 允许参加预热活动 用户注册时间限制
  76. // err 异常
  77. func (BP *JyActivity) GetDailyBoonDetail(userId string) (has bool, todayBalance int, registerBool bool, err error) {
  78. if preHeatTime, _, _ := BP.InActivity(); !preHeatTime {
  79. err = fmt.Errorf("不在预热活动时间内")
  80. return
  81. }
  82. //查询是否领取
  83. hasKey := fmt.Sprintf(BidderPlanHasDailyBoonKey, userId)
  84. has = redis.GetInt(BidderPlanRedis, hasKey) >= 1
  85. ActivityLock.Lock()
  86. //查询剩余
  87. todaySumKey := fmt.Sprintf(BidderPlanDailyBoonKey, time.Now().Format(dataUtil.Date_yyyyMMdd))
  88. todayBalance = gcfg.Instance().MustGet(gctx.New(), "jyactivity.dailyBoon.reward.limit", 500).Int() - redis.GetInt(BidderPlanRedis, todaySumKey)
  89. if todayBalance < 0 {
  90. todayBalance = 0
  91. }
  92. ActivityLock.Unlock()
  93. // 注册时间也在活动允许范围内
  94. registerBool = true
  95. //是否有权限领取
  96. powerSwitch := gcfg.Instance().MustGet(gctx.New(), "jyactivity.power.switch", false).Bool()
  97. //权限判断及问卷调查开关
  98. if powerSwitch {
  99. registerTime := gcfg.Instance().MustGet(gctx.New(), "jyactivity.power.time", 1609430400).Int64()
  100. //注册时间是否在允许范围内
  101. userInfo := GetUserInfoRedisCache(userId)
  102. registerBool = userInfo != nil && registerTime <= userInfo.Registedate
  103. }
  104. return
  105. }
  106. // GetDailyBoonSVip 每日限量超级订阅领取
  107. func (BP *JyActivity) GetDailyBoonSVip(userId string) (err error) {
  108. var has bool = false
  109. var todayBalance int = -1
  110. var startTime time.Time
  111. now := time.Now()
  112. startTime, err = time.ParseInLocation(dataUtil.Date_Full_Layout, fmt.Sprintf("%s %s", now.Format(dataUtil.Date_Short_Layout), BP.DailyClock), time.Local)
  113. if err != nil {
  114. err = fmt.Errorf("时间格式异常")
  115. return
  116. }
  117. if !now.After(startTime) {
  118. err = fmt.Errorf("活动没有开始")
  119. return
  120. }
  121. qs := redis.GetStr(BidderPlanRedis, fmt.Sprintf(QuestionnaireSubmit, userId))
  122. if MembershipDay.DoubleEleven && qs == "" {
  123. err = fmt.Errorf("未提交问卷")
  124. return
  125. }
  126. has, todayBalance, _, err = BP.GetDailyBoonDetail(userId)
  127. if err != nil {
  128. return
  129. }
  130. if has {
  131. err = fmt.Errorf("已领取")
  132. return
  133. } else if todayBalance == 0 {
  134. err = fmt.Errorf("剩余0")
  135. return
  136. }
  137. //剩余数量校验
  138. err = func() error {
  139. hasKey := fmt.Sprintf(BidderPlanHasDailyBoonKey, userId)
  140. if redis.Incr(BidderPlanRedis, hasKey) == 1 {
  141. ActivityLock.Lock()
  142. todaySumKey := fmt.Sprintf(BidderPlanDailyBoonKey, time.Now().Format(dataUtil.Date_yyyyMMdd))
  143. balance := redis.Incr(BidderPlanRedis, todaySumKey)
  144. if balance > gcfg.Instance().MustGet(gctx.New(), "jyactivity.dailyBoon.reward.limit", 500).Int64() {
  145. return fmt.Errorf("剩余为零")
  146. }
  147. ActivityLock.Unlock()
  148. if err := award.GivenSubVip(userId, award.SubVip{
  149. Num: gcfg.Instance().MustGet(gctx.New(), "jyactivity.dailyBoon.reward.svip", nil).Int64(),
  150. ActivityCode: BP.ActivityCode,
  151. Date: time.Now().Unix(),
  152. Desc: "22年双十一预热福利",
  153. }); err != nil {
  154. redis.Del(BidderPlanRedis, hasKey)
  155. return fmt.Errorf("稍后重试")
  156. }
  157. if balance == 1 {
  158. _ = redis.SetExpire(BidderPlanRedis, todaySumKey, BP.cacheLong())
  159. }
  160. _ = redis.SetExpire(BidderPlanRedis, hasKey, BP.cacheLong())
  161. //领取7天超级订阅后清空权限缓存
  162. cacheKey := fmt.Sprintf(PowerCacheKey, userId)
  163. baseInfoCacheKey := fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId)
  164. redis.Del(PowerCacheDb, cacheKey)
  165. redis.Del(PowerCacheDb, baseInfoCacheKey)
  166. }
  167. return nil
  168. }()
  169. return err
  170. }
  171. // GetUserInfoRedisCache 获取用户信息
  172. func GetUserInfoRedisCache(userId string) *award.UserBaseMsg {
  173. //先从缓存中获取
  174. cacheKey := fmt.Sprintf(PowerCacheKey, userId)
  175. if bytes, err := redis.GetBytes(PowerCacheDb, cacheKey); err == nil && bytes != nil {
  176. userPower := award.UserBaseMsg{}
  177. if err := json.Unmarshal(*bytes, &userPower); err == nil {
  178. return &userPower
  179. }
  180. }
  181. return award.GetUserInfo(userId)
  182. }