123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package activity
- import (
- dataUtil "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/redis"
- "app.yhyue.com/moapp/message/handler/award"
- "encoding/json"
- "fmt"
- "github.com/gogf/gf/v2/os/gcfg"
- "github.com/gogf/gf/v2/os/gctx"
- "log"
- "time"
- )
- // WinnerInfo 当日中奖信息
- func (BP *JyActivity) WinnerInfo(startTime, entTime string) []map[string]interface{} {
- var res []map[string]interface{}
- // 加个redis缓存
- redisCache, err := redis.GetBytes(BidderPlanRedis, WinnerInfoKey)
- if err == nil {
- if err = json.Unmarshal(*redisCache, &res); err == nil {
- return res
- }
- }
- res = award.GetWinnerInfo(startTime, entTime)
- fmt.Println(res)
- if res != nil {
- b, err := json.Marshal(res)
- if err == nil && len(b) > 0 {
- if err = redis.PutBytes(BidderPlanRedis, WinnerInfoKey, &b, 1*60*60); err != nil {
- log.Println("中将人信息 缓存信息有误")
- }
- }
- }
- return res
- }
- // 问卷提交
- func (BP *JyActivity) Questionnaire(userId, phone, answers string) error {
- if inActivity, _, _ := BP.InActivity(); !inActivity {
- return fmt.Errorf("不在活动时间内")
- }
- //判断是否是符合条件的免费用户
- user := GetUserInfoRedisCache(userId)
- if user.VipStatus > 0 || user.Status > 0 || user.EntnicheStatus > 0 {
- return fmt.Errorf("用户身份不符合活动要求")
- }
- if user.Registedate < gcfg.Instance().MustGet(gctx.New(), "jyactivity.power.time", 1609430400).Int64() {
- return fmt.Errorf("用户注册时间不符合活动要求")
- }
- //保存问卷
- if ok := award.SaveQuestionnaire(userId, phone, answers); ok {
- if redis.Put(BidderPlanRedis, fmt.Sprintf(QuestionnaireSubmit, userId), "qs", BP.cacheLong()) != true {
- return fmt.Errorf("redis de22q 存储异常")
- }
- //问卷保存后--赠送7天超级订阅
- if err := BP.GetDailyBoonSVip(userId); err != nil {
- return err
- }
- } else {
- return fmt.Errorf("mongodb de22q 存储异常")
- }
- return nil
- }
- // ShareFinish 分享完成
- func (BP *JyActivity) ShareFinish(userId string) error {
- if _, inActivity, _ := BP.InActivity(); !inActivity {
- return fmt.Errorf("不在活动时间内")
- }
- if redis.Put(BidderPlanRedis, fmt.Sprintf(BidderPlanShare, userId), 1, BP.cacheLong()) != true {
- return fmt.Errorf("存储异常")
- }
- return nil
- }
- // GetDailyBoonDetail 查询领取情况及剩余
- // has 是否已经领取
- // todayBalance 今日余额
- // 允许参加预热活动 用户注册时间限制
- // err 异常
- func (BP *JyActivity) GetDailyBoonDetail(userId string) (has bool, todayBalance int, registerBool bool, err error) {
- if preHeatTime, _, _ := BP.InActivity(); !preHeatTime {
- err = fmt.Errorf("不在预热活动时间内")
- return
- }
- //查询是否领取
- hasKey := fmt.Sprintf(BidderPlanHasDailyBoonKey, userId)
- has = redis.GetInt(BidderPlanRedis, hasKey) >= 1
- ActivityLock.Lock()
- //查询剩余
- todaySumKey := fmt.Sprintf(BidderPlanDailyBoonKey, time.Now().Format(dataUtil.Date_yyyyMMdd))
- todayBalance = gcfg.Instance().MustGet(gctx.New(), "jyactivity.dailyBoon.reward.limit", 500).Int() - redis.GetInt(BidderPlanRedis, todaySumKey)
- if todayBalance < 0 {
- todayBalance = 0
- }
- ActivityLock.Unlock()
- // 注册时间也在活动允许范围内
- registerBool = true
- //是否有权限领取
- powerSwitch := gcfg.Instance().MustGet(gctx.New(), "jyactivity.power.switch", false).Bool()
- //权限判断及问卷调查开关
- if powerSwitch {
- registerTime := gcfg.Instance().MustGet(gctx.New(), "jyactivity.power.time", 1609430400).Int64()
- //注册时间是否在允许范围内
- userInfo := GetUserInfoRedisCache(userId)
- registerBool = userInfo != nil && registerTime <= userInfo.Registedate
- }
- return
- }
- // GetDailyBoonSVip 每日限量超级订阅领取
- func (BP *JyActivity) GetDailyBoonSVip(userId string) (err error) {
- var has bool = false
- var todayBalance int = -1
- var startTime time.Time
- now := time.Now()
- startTime, err = time.ParseInLocation(dataUtil.Date_Full_Layout, fmt.Sprintf("%s %s", now.Format(dataUtil.Date_Short_Layout), BP.DailyClock), time.Local)
- if err != nil {
- err = fmt.Errorf("时间格式异常")
- return
- }
- if !now.After(startTime) {
- err = fmt.Errorf("活动没有开始")
- return
- }
- qs := redis.GetStr(BidderPlanRedis, fmt.Sprintf(QuestionnaireSubmit, userId))
- if MembershipDay.DoubleEleven && qs == "" {
- err = fmt.Errorf("未提交问卷")
- return
- }
- has, todayBalance, _, err = BP.GetDailyBoonDetail(userId)
- if err != nil {
- return
- }
- if has {
- err = fmt.Errorf("已领取")
- return
- } else if todayBalance == 0 {
- err = fmt.Errorf("剩余0")
- return
- }
- //剩余数量校验
- err = func() error {
- hasKey := fmt.Sprintf(BidderPlanHasDailyBoonKey, userId)
- if redis.Incr(BidderPlanRedis, hasKey) == 1 {
- ActivityLock.Lock()
- todaySumKey := fmt.Sprintf(BidderPlanDailyBoonKey, time.Now().Format(dataUtil.Date_yyyyMMdd))
- balance := redis.Incr(BidderPlanRedis, todaySumKey)
- if balance > gcfg.Instance().MustGet(gctx.New(), "jyactivity.dailyBoon.reward.limit", 500).Int64() {
- return fmt.Errorf("剩余为零")
- }
- ActivityLock.Unlock()
- if err := award.GivenSubVip(userId, award.SubVip{
- Num: gcfg.Instance().MustGet(gctx.New(), "jyactivity.dailyBoon.reward.svip", nil).Int64(),
- ActivityCode: BP.ActivityCode,
- Date: time.Now().Unix(),
- Desc: "22年双十一预热福利",
- }); err != nil {
- redis.Del(BidderPlanRedis, hasKey)
- return fmt.Errorf("稍后重试")
- }
- if balance == 1 {
- _ = redis.SetExpire(BidderPlanRedis, todaySumKey, BP.cacheLong())
- }
- _ = redis.SetExpire(BidderPlanRedis, hasKey, BP.cacheLong())
- //领取7天超级订阅后清空权限缓存
- cacheKey := fmt.Sprintf(PowerCacheKey, userId)
- baseInfoCacheKey := fmt.Sprintf(IsGetUserBaseInfoRedisKey, userId)
- redis.Del(PowerCacheDb, cacheKey)
- redis.Del(PowerCacheDb, baseInfoCacheKey)
- }
- return nil
- }()
- return err
- }
- // GetUserInfoRedisCache 获取用户信息
- func GetUserInfoRedisCache(userId string) *award.UserBaseMsg {
- //先从缓存中获取
- cacheKey := fmt.Sprintf(PowerCacheKey, userId)
- if bytes, err := redis.GetBytes(PowerCacheDb, cacheKey); err == nil && bytes != nil {
- userPower := award.UserBaseMsg{}
- if err := json.Unmarshal(*bytes, &userPower); err == nil {
- return &userPower
- }
- }
- return award.GetUserInfo(userId)
- }
|