|
@@ -170,7 +170,7 @@ func (a *AdFunc) Handle() (adInfos []AdInfo) {
|
|
|
// 频次
|
|
|
if v.O_extend.Frequency != "" && a.SessionId != "" {
|
|
|
if !a.CheckFrequency(v) {
|
|
|
- return
|
|
|
+ continue
|
|
|
}
|
|
|
}
|
|
|
adInfos = append(adInfos, v)
|
|
@@ -272,44 +272,60 @@ func (a *AdFunc) GetUserAttribute() (attributesMap map[string]bool) {
|
|
|
|
|
|
// CheckFrequency 校验频次
|
|
|
func (a *AdFunc) CheckFrequency(v AdInfo) bool {
|
|
|
- if frequency := strings.Split(v.O_extend.Frequency, "_"); len(frequency) == 2 {
|
|
|
- cycle := frequency[0]
|
|
|
- times := qutil.IntAll(frequency[1])
|
|
|
- key := "frequency_ad_%s_%s_%s"
|
|
|
- key = fmt.Sprintf(key, a.CurrentAdCode, v.S_Uid)
|
|
|
- timeout := 0
|
|
|
- now := time.Now()
|
|
|
- switch cycle {
|
|
|
- case "0":
|
|
|
- t, _ := time.ParseInLocation("2006-01-02-15-04-05", v.O_extend.EndTime, time.Local)
|
|
|
- timeout = int(t.Unix() - now.Unix())
|
|
|
- case "周":
|
|
|
- endOfWeek := now.AddDate(0, 0, 7-int(now.Weekday())).Add(-time.Second * 1)
|
|
|
- timeOut = endOfWeek.Unix() - now.Unix()
|
|
|
- case "月":
|
|
|
- lastDayOfMonth := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, -1, now.Location()).AddDate(0, 0, -1)
|
|
|
- timeout = int(lastDayOfMonth.Unix() - now.Unix())
|
|
|
- default:
|
|
|
- // 按天处理
|
|
|
- cycleInt := qutil.IntAll(cycle)
|
|
|
- if cycleInt <= 0 {
|
|
|
- log.Println("广告位频次属性无效:", cycle)
|
|
|
- return true
|
|
|
- }
|
|
|
- endOfDay := now.AddDate(0, 0, cycleInt)
|
|
|
- timeout = int(endOfDay.Unix() - now.Unix())
|
|
|
+ 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)
|
|
|
}
|
|
|
- // 判断次数
|
|
|
- if b, _ := redis.Exists("other", key); b {
|
|
|
- count := qutil.IntAll(redis.Get("other", key))
|
|
|
- if count >= times {
|
|
|
- return false
|
|
|
+ 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 {
|
|
|
- redis.Incr("other", key)
|
|
|
+ log.Println("广告位配置结束时间格式有误", err)
|
|
|
+ return true
|
|
|
}
|
|
|
} else {
|
|
|
- redis.Put("other", key, 1, timeout)
|
|
|
+ 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
|
|
|
}
|