// cleareids
package main

import (
	"encoding/json"
	"log"
	"qfw/util"
	"qfw/util/redis"
	"sync"

	"github.com/robfig/cron"
)

var (
	c            *cron.Cron
	projectcycle int64
)

func clearedis() {
	if credis, ok := Sysconfig["clearedis"].(map[string]interface{}); ok {
		clearcron := util.ObjToString(credis["clearcron"])
		projectcycle = util.Int64All(credis["projectcycle"])
		log.Println(credis)
		if credis["open"].(bool) {
			c = cron.New()
			c.AddFunc(clearcron, clearPKey)
			c.Start()
		}
	}
}

func clearPKey() {
	log.Println("开始清理")
	nowtime := currentMegerTime //time.Now().Unix() int64(1461204000)
	wg := sync.WaitGroup{}
	for _, pncb := range []*KeyMap{PNKey, PCKey, PBKey} {
		wg.Add(1)
		go func(pncb *KeyMap) {
			defer wg.Done()
			clearPNCBKey(pncb, nowtime)
		}(pncb)
	}
	wg.Wait()
	log.Println("清理结束")
}

func clearPNCBKey(pncb *KeyMap, nowtime int64) {
	delkey := clearIdsKeys(pncb, nowtime)
	pncb.Lock.Lock()
	for _, k := range delkey {
		delete(pncb.Map, k)
	}
	pncb.Lock.Unlock()
}

func clearIdsKeys(pKey *KeyMap, nowtime int64) []string {
	defer util.Catch()
	delkey := []string{}
	pKey.Lock.Lock()
	for k, ma := range pKey.Map {
		ids := ma.Arr
		delids := []interface{}{}
		if ids == nil {
			continue
		}
		res := redis.Mget(REDISIDS, *ids)
		for _, b1 := range res {
			if b1 != nil {
				var info ProjectInfo
				err := json.Unmarshal(b1.([]byte), &info)
				if err != nil {
					log.Println(err)
					continue
				}
				publistime := info.Publistime[len(info.Publistime)-1]
				if projectcycle < (nowtime-publistime)/86400 { //项目周期超时
					delids = append(delids, info.Id)
				}
			}
		}
		if len(delids) < 1 {
			continue
		}
		//删除redis相关信息
		b := redis.Del(REDISIDS, delids...)
		if b {
			for _, id := range delids {
				ids = deleteSliceId(*ids, id.(string))
			}
			//log.Println(ids, *ma.Arr)
			ma.Arr = ids
			if len(*ma.Arr) < 1 {
				redis.Del(REDISKEYS, k)
				delkey = append(delkey, k)
			} else {
				//更新REDISKEYS
				redis.Put(REDISKEYS, k, *ma.Arr, 0)
			}
		}
	}
	pKey.Lock.Unlock()
	return delkey
}

func deleteSliceId(a []string, id string) *[]string {
	ret := make([]string, 0)
	for _, val := range a {
		if val != id {
			ret = append(ret, val)
		}
	}
	return &ret
}
