package util

import (
	"fmt"
	"io"
	. "jy/mongodbutil"
	"log"
	"os"
	qu "qfw/util"

	. "gopkg.in/mgo.v2/bson"
	"gopkg.in/natefinch/lumberjack.v2"
)

var syncint chan bool //获取下标锁
var Config map[string]interface{}
var Se = qu.SimpleEncrypt{Key: "topnet@extract"}

func init() {
	//输出日志配置,多输出源
	filelog := &lumberjack.Logger{
		Filename:   "./out.log",
		MaxSize:    500, // megabytes
		MaxBackups: 3,
		MaxAge:     20,   //days
		Compress:   true, // disabled by default
	}
	writers := []io.Writer{
		filelog,
		os.Stdout,
	}
	fileAndStdoutWriter := io.MultiWriter(writers...)
	log.SetOutput(fileAndStdoutWriter)

	syncint = make(chan bool, 1)
}

func InitMgoPool() {
	initCap := qu.IntAll(Config["dbsize"])
	addr := qu.ObjToString(Config["mgodb"])
	dbname := qu.ObjToString(Config["dbname"])
	Mgo = MgoFactory(initCap, initCap*3, 120, addr, dbname)
}

func GetSyncIndex(code string) string {
	tmp := ""
	syncint <- true
	Mgo.Update("rule_code", `{"code":"`+code+`"}`, M{"$inc": M{"index": 1}}, true, false)
	data, _ := Mgo.FindOne("rule_code", `{"code":"`+code+`"}`)
	<-syncint
	if len(*data) > 0 {
		tmp = code + "_" + fmt.Sprint((*data)["index"])
	}
	return tmp
}

//nfields非复制字段集
func DeepCopy(value interface{}) interface{} {
	if valueMap, ok := value.(map[string]interface{}); ok {
		newMap := make(map[string]interface{})
		for k, v := range valueMap {
			newMap[k] = DeepCopy(v)
		}
		return newMap
	} else if valueSlice, ok := value.([]interface{}); ok {
		newSlice := make([]interface{}, len(valueSlice))
		for k, v := range valueSlice {
			newSlice[k] = DeepCopy(v)
		}
		return newSlice
	}
	return value
}
