瀏覽代碼

Merge remote-tracking branch 'origin/master'

wangshan 9 月之前
父節點
當前提交
7b1485b33e
共有 1 個文件被更改,包括 80 次插入8 次删除
  1. 80 8
      common/src/qfw/util/jy/ad.go

+ 80 - 8
common/src/qfw/util/jy/ad.go

@@ -37,14 +37,16 @@ var (
 )
 
 type AdFunc struct {
-	AdCodes   []string            //广告位code
-	UserInfo  *BigVipBaseMsg      //用户信息
-	Mgo       *mongodb.MongodbSim //mongo
-	MySql     *mysql.Mysql        //tidb 订单表
-	Host      string              //域名
-	Ads       *[]byte             //
-	OrderCode string              //订单code
-	IsLogin   bool                //是否登录
+	AdCodes       []string            //广告位code
+	UserInfo      *BigVipBaseMsg      //用户信息
+	Mgo           *mongodb.MongodbSim //mongo
+	MySql         *mysql.Mysql        //tidb 订单表
+	Host          string              //域名
+	Ads           *[]byte             //
+	OrderCode     string              //订单code
+	IsLogin       bool                //是否登录
+	SessionId     string              // sessionId
+	CurrentAdCode string              // 当前遍历的广告位code
 }
 
 // AdInfo 广告信息
@@ -67,7 +69,9 @@ type AdInfo struct {
 		Tab            string `json:"tab"`            //tab切换
 		UserAttribute  string `json:"userAttribute"`  //用户身份  g-未登录 f-免费 m-大会员 v-超级订阅 e-商机管理 多个时使用英文逗号分割
 		OrderAttribute string `json:"orderAttribute"` //订单类型 vb:vip购买;vr:vip续费;vu:vip升级;mb:大会员购买;
+		Frequency      string `json:"frequency"`      //周期频率  "周期-展示次数"   (周期可选值:0-活动期间内 ;数字-xx天 ;周- 自然周 月-自然月) 例: 活动周期内展示1次:0_1 ; 自然月展示1次:月_1 ; 3天展示1次:3_1 4.自然周展示1次:周_1
 	} `json:"o_extend"` //拓展属性
+	S_Uid    string `json:"s_uid"`    //该组唯一标识id
 	S_script string `json:"s_script"` //脚本
 }
 
@@ -79,6 +83,7 @@ func (a *AdFunc) GetAdInfos() (adInfoMap map[string][]AdInfo) {
 			obj, err := redis.GetBytes("other", "ad_"+sCode)
 			if err == nil && obj != nil && len(*obj) > 0 {
 				a.Ads = obj
+				a.CurrentAdCode = sCode
 				adInfoMap[sCode] = a.Handle()
 			} else {
 				res, ok := a.Mgo.FindOneByField("ad", `{"s_code":"`+sCode+`"}`, `{"a_son":1}`)
@@ -90,6 +95,7 @@ func (a *AdFunc) GetAdInfos() (adInfoMap map[string][]AdInfo) {
 							if err = redis.PutBytes("other", "ad_"+sCode, a.Ads, int(a.GetLastTime())); err != nil {
 								log.Println(fmt.Sprintf("广告位缓存 %s保存异常 err:%s", sCode, err.Error()))
 							}
+							a.CurrentAdCode = sCode
 							adInfoMap[sCode] = a.Handle()
 						}
 					}
@@ -161,6 +167,12 @@ func (a *AdFunc) Handle() (adInfos []AdInfo) {
 					continue
 				}
 			}
+			// 频次
+			if v.O_extend.Frequency != "" && a.SessionId != "" {
+				if !a.CheckFrequency(v) {
+					continue
+				}
+			}
 			adInfos = append(adInfos, v)
 		}
 	}
@@ -257,3 +269,63 @@ func (a *AdFunc) GetUserAttribute() (attributesMap map[string]bool) {
 	}
 	return
 }
+
+// CheckFrequency 校验频次
+func (a *AdFunc) CheckFrequency(v AdInfo) bool {
+	frequency := []string{}
+	if frequency = strings.Split(v.O_extend.Frequency, "_"); len(frequency) != 2 {
+		log.Println("频次属性格式有误:", frequency)
+		return true
+	}
+	cycle := frequency[0]
+	times := qutil.IntAll(frequency[1])
+	key := "frequency_ad_%s_%s_%s"
+	key = fmt.Sprintf(key, a.CurrentAdCode, v.S_Uid, a.SessionId)
+	// 存在则判断次数
+	if b, _ := redis.Exists("other", key); b {
+		count := qutil.IntAll(redis.Get("other", key))
+		if count >= times {
+			return false
+		} else {
+			redis.Incr("other", key)
+		}
+		return true
+	}
+	// 不存在则新增
+	ttl := 0
+	now := time.Now()
+	switch cycle {
+	case "0":
+		if v.O_extend.EndTime != "" {
+			t, err := time.ParseInLocation("2006-01-02-15-04-05", v.O_extend.EndTime, time.Local)
+			if err == nil {
+				ttl = int(t.Unix() - now.Unix())
+			} else {
+				log.Println("广告位配置结束时间格式有误", err)
+				return true
+			}
+		} else {
+			log.Println("广告位未配置结束时间")
+			return true
+		}
+	case "周":
+		end := now.AddDate(0, 0, 7-int(now.Weekday()))
+		endOfWeek := time.Date(end.Year(), end.Month(), end.Day()+1, 0, 0, 0, -1, now.Location())
+		ttl = int(endOfWeek.Unix() - now.Unix())
+	case "月":
+		lastDayOfMonth := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, -1, now.Location())
+		ttl = int(lastDayOfMonth.Unix() - now.Unix())
+	default:
+		// 按天处理
+		cycleInt := qutil.IntAll(cycle)
+		if cycleInt <= 0 {
+			log.Println("广告位频次属性无效:", cycle)
+			return true
+		}
+		endOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, -1, now.Location()).AddDate(0, 0, cycleInt)
+		ttl = int(endOfDay.Unix() - now.Unix())
+	}
+	redis.Put("other", key, 1, ttl)
+
+	return true
+}