// version
package admin

import (
	. "jy/mongodbutil"
	"jy/util"
	"log"
	"net/http"
	qu "qfw/util"
	"strings"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
)

func init() {
	Admin.GET("/version", func(c *gin.Context) {
		c.HTML(
			http.StatusOK, "version.html",
			gin.H{},
		)
	})
	//任务版本
	Admin.POST("/version/data", func(c *gin.Context) {
		data, _ := Mgo.Find("version", `{"delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
		c.JSON(200, gin.H{"data": data})
	})
	//根据_id查询版本详细信息
	Admin.GET("/version/dataById", func(c *gin.Context) {
		gid, b := c.GetQuery("_id")
		if !b || !bson.IsObjectIdHex(gid) {
			c.JSON(400, gin.H{"req": false})
			return
		}

		data, _ := Mgo.FindOne("version", `{"_id":"`+gid+`","delete":false}`)
		c.JSON(200, gin.H{"req": true, "data": data})
	})
	Admin.POST("/version/save", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		data := GetPostForm(c)
		if data["s_filefileds"] != nil {
			data["s_filefileds"] = strings.Split(data["s_filefileds"].(string), ",")
		}
		if _id != "" {
			Mgo.UpdateById("version", _id, map[string]interface{}{"$set": data})
			c.JSON(200, gin.H{"rep": true})
		} else {
			version, _ := c.GetPostForm("version")
			tmp, _ := Mgo.FindOne("version", `{"version":"`+version+`","delete":false}`)
			if len(*tmp) > 0 {
				c.JSON(200, gin.H{"rep": false})
			} else {
				s_pversionid, _ := c.GetPostForm("s_pversionid")
				iscopyfiled, _ := data["iscopyfiled"].(bool)
				data["l_createtime"] = time.Now().Unix()
				s_username := sessions.Default(c).Get("username").(string)
				data["s_username"] = s_username
				data["delete"] = false
				vid := Mgo.Save("version", data)
				if s_pversionid != "" {
					copyComRules(version, s_pversionid, s_username)
				}
				if iscopyfiled {
					list, _ := Mgo.Find("versioninfo", `{"vid":"`+s_pversionid+`","delete":false}`, nil, nil, false, -1, -1)
					for _, v := range *list {
						delete(v, "_id")
						v["l_createtime"] = time.Now().Unix()
						v["s_username"] = sessions.Default(c).Get("username")
						v["l_lasttime"] = time.Now().Unix()
						v["vid"] = vid
						v["delete"] = false
						pid := Mgo.Save("versioninfo", v)
						s_field := qu.ObjToString(v["s_field"])
						//克隆属性配置
						copyFieldRules(vid, pid, s_field, s_pversionid, s_username)
					}
					//克隆分包属性
					list2, _ := Mgo.Find("pkg_info", `{"vid":"`+s_pversionid+`","delete":false}`, nil, nil, false, -1, -1)
					for _, v := range *list2 {
						delete(v, "_id")
						v["l_createtime"] = time.Now().Unix()
						v["s_username"] = sessions.Default(c).Get("username")
						v["l_lasttime"] = time.Now().Unix()
						v["vid"] = vid
						v["delete"] = false
						pid := Mgo.Save("pkg_info", v)
						s_field := qu.ObjToString(v["s_field"])
						//克隆属性配置
						copyPkgRules(vid, pid, s_field, s_pversionid, s_username)
					}
				}
				c.JSON(200, gin.H{"rep": true})
			}
		}
	})
	Admin.POST("/version/use", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		isuse, _ := c.GetPostForm("isuse")
		if isuse == "true" { //启用确保只有一个版本
			l, _ := Mgo.Find("version", `{"isuse":true,"delete":false}`, nil, nil, false, -1, -1)
			if len(*l) > 0 {
				c.JSON(200, gin.H{"rep": false})
				return
			}
		}
		bisuse := false
		if isuse == "true" {
			bisuse = true
		}
		set := map[string]interface{}{
			"$set": map[string]interface{}{
				"isuse": bisuse,
			},
		}
		b := Mgo.UpdateById("version", _id, set)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.POST("/version/del", func(c *gin.Context) { //删除版本
		_id, _ := c.GetPostForm("_id")
		s_version, _ := c.GetPostForm("version")
		b := Mgo.UpdateById("version", _id, `{"$set":{"delete":true}}`)
		//b := Mgo.Del("version", `{"_id":"`+_id+`"}`)
		//级联删除
		//刪除属性配置
		//删除分包配置
		//删除逻辑
		//删除属性配置中的前置规则
		//删除属性配置中的抽取规则
		//删除属性配置中的后置规则

		//刪除属性配置
		go DelVersionInfo(_id)
		//删除分包配置
		go DelPkgInfo(_id)
		//删除版本的通用前置规则
		go DelRulePre(s_version)
		//删除版本的通用后置规则
		go DelRuleBack(s_version)
		//删除标签库
		go DelTag(s_version)
		//删除版本的通用前置规则
		go DelCleanUp(s_version)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.GET("/version/info", func(c *gin.Context) {
		vid := c.Query("vid")
		v, _ := Mgo.Find("version", `{}`, `{"_id":-1}`, `{"version":1}`, false, -1, -1)
		c.HTML(
			http.StatusOK, "versioninfo.html",
			gin.H{"vid": vid, "versions": v},
		)
	})
	Admin.POST("/version/infodata", func(c *gin.Context) {
		vid, _ := c.GetPostForm("vid")
		list, _ := Mgo.Find("versioninfo", `{"vid":"`+vid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
		c.JSON(200, gin.H{"data": list, "vid": vid})
	})
	Admin.POST("/version/infosave", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		data := GetPostForm(c)
		if _id != "" {
			Mgo.UpdateById("versioninfo", _id, map[string]interface{}{"$set": data})
			c.JSON(200, gin.H{"rep": true})
		} else {
			s_field, _ := c.GetPostForm("s_field")
			vid, _ := c.GetPostForm("vid")
			tmp, _ := Mgo.FindOne("versioninfo", `{"s_field":"`+s_field+`","vid":"`+vid+`","delete":false}`)
			if len(*tmp) > 0 {
				c.JSON(200, gin.H{"rep": false})
			} else {
				data["l_createtime"] = time.Now().Unix()
				data["s_username"] = sessions.Default(c).Get("username")
				data["l_lasttime"] = time.Now().Unix()
				data["delete"] = false
				pid := Mgo.Save("versioninfo", data)
				fromvid, _ := data["s_pversionid"].(string)
				if fromvid != "" {
					copyFieldRules(vid, pid, s_field, fromvid, sessions.Default(c).Get("username").(string))
				}
				c.JSON(200, gin.H{"rep": true})
			}
		}
	})
	Admin.POST("/version/infouse", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		isuse, _ := c.GetPostForm("isuse")
		b := Mgo.UpdateById("versioninfo", _id, `{"$set":{"isuse":`+isuse+`}}`)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.POST("/version/infodel", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		b := Mgo.UpdateById("versioninfo", _id, `{"$set":{"delete":true}}`)
		v, _ := Mgo.FindById("versioninfo", _id, `{"vid":1}`)
		go DelLogic(qu.ObjToString((*v)["vid"]), _id)
		//b := Mgo.Del("versioninfo", `{"_id":"`+_id+`"}`)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.GET("/version/pkginfo", func(c *gin.Context) {
		vid := c.Query("vid")
		c.HTML(
			http.StatusOK, "pkg_info.html",
			gin.H{"vid": vid},
		)
	})
	Admin.POST("/version/pkginfodata", func(c *gin.Context) {
		vid, _ := c.GetPostForm("vid")
		list, _ := Mgo.Find("pkg_info", `{"vid":"`+vid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
		c.JSON(200, gin.H{"data": list, "vid": vid})
	})
	Admin.POST("/version/pkginfosave", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		data := GetPostForm(c)
		if _id != "" {
			Mgo.UpdateById("pkg_info", _id, map[string]interface{}{"$set": data})
			c.JSON(200, gin.H{"rep": true})
		} else {
			s_field, _ := c.GetPostForm("s_field")
			vid, _ := c.GetPostForm("vid")
			tmp, _ := Mgo.FindOne("pkg_info", `{"s_field":"`+s_field+`","vid":"`+vid+`","delete":false}`)
			if len(*tmp) > 0 {
				c.JSON(200, gin.H{"rep": false})
			} else {
				data["l_createtime"] = time.Now().Unix()
				data["s_username"] = sessions.Default(c).Get("username")
				data["l_lasttime"] = time.Now().Unix()
				data["delete"] = false
				pid := Mgo.Save("pkg_info", data)
				fromvid, _ := data["s_pversionid"].(string)
				if fromvid != "" {
					//克隆分包属性
					copyPkgRules(vid, pid, s_field, fromvid, sessions.Default(c).Get("username").(string))
				}
				c.JSON(200, gin.H{"rep": true})
			}
		}
	})
	Admin.POST("/version/pkginfouse", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		isuse, _ := c.GetPostForm("isuse")
		b := Mgo.UpdateById("pkg_info", _id, `{"$set":{"isuse":`+isuse+`}}`)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.POST("/version/pkginfodel", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		b := Mgo.UpdateById("pkg_info", _id, `{"$set":{"delete":true}}`)
		v, _ := Mgo.FindById("pkg_info", _id, `{"vid":1}`)
		go DelPkgLogic(qu.ObjToString((*v)["vid"]), _id)
		//b := Mgo.Del("versioninfo", `{"_id":"`+_id+`"}`)
		c.JSON(200, gin.H{"rep": b})
	})
	//分块配置
	Admin.GET("/version/blockinfo", func(c *gin.Context) {
		vid := c.Query("vid")
		c.HTML(http.StatusOK, "blockinfo.html", gin.H{"vid": vid})
	})
	Admin.POST("/version/blockinfo_list", func(c *gin.Context) {
		vid, _ := c.GetPostForm("vid")
		data, _ := Mgo.Find("block_info", bson.M{"vid": vid, "delete": false}, `{"index":-1}`, `{"block_reg":1,"title_reg":1,"index":1}`, false, -1, -1)
		for _, v := range *data {
			v["id"] = qu.BsonIdToSId(v["_id"])
		}
		c.JSON(http.StatusOK, gin.H{"data": data})
	})
	//分块配置保存
	Admin.POST("/version/blockinfo_save", func(c *gin.Context) {
		status := false
		_id, _ := c.GetPostForm("_id")
		block_reg, _ := c.GetPostForm("block_reg")
		title_reg, _ := c.GetPostForm("title_reg")
		if _id != "" {
			status = Mgo.UpdateById("block_info", _id, bson.M{
				"$set": bson.M{
					"l_updatetime": time.Now().Unix(),
					"block_reg":    block_reg,
					"title_reg":    title_reg,
				},
			})
		} else {
			vid, _ := c.GetPostForm("vid")
			list, flag := Mgo.Find("block_info", bson.M{"vid": vid}, `{"index": 1}`, `{"index":1}`, false, 0, 1)
			index := -1
			if flag && len(*list) == 1 {
				index = qu.IntAllDef((*list)[0]["index"], 1) - 1
			}
			unix := time.Now().Unix()
			status = Mgo.Save("block_info", bson.M{
				"delete":       false,
				"index":        index,
				"block_reg":    block_reg,
				"title_reg":    title_reg,
				"vid":          vid,
				"l_createtime": unix,
				"l_updatetime": unix,
				"s_username":   sessions.Default(c).Get("username"),
			}) != ""
		}
		c.JSON(http.StatusOK, gin.H{"status": status})
	})
	Admin.POST("/version/blockinfo_updateindex", func(c *gin.Context) {
		_ids := c.PostFormArray("_ids")
		indexs := c.PostFormArray("indexs")
		log.Println(_ids, indexs)
		for k, _id := range _ids {
			Mgo.UpdateById("block_info", _id, bson.M{
				"$set": bson.M{
					"index": qu.IntAll(indexs[k]),
				},
			})
		}
		c.JSON(http.StatusOK, gin.H{})
	})
	Admin.POST("/version/blockinfo_delete", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		status := Mgo.UpdateById("block_info", _id, bson.M{"$set": bson.M{"delete": true}})
		c.JSON(http.StatusOK, gin.H{"status": status})
	})
	//块的信息分类
	Admin.GET("/version/blockclassify", func(c *gin.Context) {
		vid := c.Query("vid")
		c.HTML(http.StatusOK, "blockclassify.html", gin.H{"vid": vid})
	})
	Admin.POST("/version/blockclassify_delete", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		status := Mgo.UpdateById("block_classify", _id, bson.M{"$set": bson.M{"delete": true}})
		c.JSON(http.StatusOK, gin.H{"status": status})
	})
	Admin.POST("/version/blockclassify_save", func(c *gin.Context) {
		status := false
		_id, _ := c.GetPostForm("_id")
		name, _ := c.GetPostForm("name")
		if _id != "" {
			status = Mgo.UpdateById("block_classify", _id, bson.M{
				"$set": bson.M{
					"l_updatetime": time.Now().Unix(),
					"name":         name,
				},
			})
		} else {
			vid, _ := c.GetPostForm("vid")
			unix := time.Now().Unix()
			status = Mgo.Save("block_classify", bson.M{
				"delete":       false,
				"name":         name,
				"vid":          vid,
				"l_createtime": unix,
				"l_updatetime": unix,
				"s_username":   sessions.Default(c).Get("username"),
			}) != ""
		}
		c.JSON(http.StatusOK, gin.H{"status": status})
	})
	Admin.POST("/version/blockclassify_list", func(c *gin.Context) {
		vid, _ := c.GetPostForm("vid")
		data, _ := Mgo.Find("block_classify", bson.M{"vid": vid, "delete": false}, `{"l_updatetime":-1}`, `{"name":1,"l_updatetime":1}`, false, -1, -1)
		for _, v := range *data {
			v["id"] = qu.BsonIdToSId(v["_id"])
			l_updatetime := v["l_updatetime"]
			v["l_updatetime"] = qu.FormatDateWithObj(&l_updatetime, qu.Date_Full_Layout)
		}
		c.JSON(http.StatusOK, gin.H{"data": data})
	})
	//块分类
	Admin.GET("/version/blockclassify_info", func(c *gin.Context) {
		pid := c.Query("pid")
		vid := c.Query("vid")
		c.HTML(http.StatusOK, "blockclassify_info.html", gin.H{"vid": vid, "pid": pid})
	})
	Admin.POST("/version/blockclassify_info_delete", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		status := Mgo.UpdateById("block_classify_info", _id, bson.M{"$set": bson.M{"delete": true}})
		c.JSON(http.StatusOK, gin.H{"status": status})
	})
	Admin.POST("/version/blockclassify_info_save", func(c *gin.Context) {
		status := 0
		name, _ := c.GetPostForm("name")
		code, _ := c.GetPostForm("code")
		pid, _ := c.GetPostForm("pid")
		if Mgo.Count("block_classify_info", map[string]interface{}{
			"delete": false,
			"$or": []map[string]interface{}{
				map[string]interface{}{
					"name": name,
				},
				map[string]interface{}{
					"code": code,
				},
			},
		}) == 1 {
			status = -1
		} else {
			_id, _ := c.GetPostForm("_id")
			if _id != "" {
				if Mgo.UpdateById("block_classify_info", _id, bson.M{
					"$set": bson.M{
						"l_updatetime": time.Now().Unix(),
						"name":         name,
						"code":         code,
					},
				}) {
					status = 1
				}
			} else {
				vid, _ := c.GetPostForm("vid")
				unix := time.Now().Unix()
				if Mgo.Save("block_classify_info", bson.M{
					"delete":       false,
					"name":         name,
					"code":         code,
					"vid":          vid,
					"pid":          pid,
					"l_createtime": unix,
					"l_updatetime": unix,
					"s_username":   sessions.Default(c).Get("username"),
				}) != "" {
					status = 1
				}
			}
		}
		c.JSON(http.StatusOK, gin.H{"status": status})
	})
	Admin.POST("/version/blockclassify_info_list", func(c *gin.Context) {
		pid, _ := c.GetPostForm("pid")
		data, _ := Mgo.Find("block_classify_info", bson.M{"pid": pid, "delete": false}, `{"l_updatetime":-1}`, `{"name":1,"code":1,"l_updatetime":1}`, false, -1, -1)
		for _, v := range *data {
			v["id"] = qu.BsonIdToSId(v["_id"])
			l_updatetime := v["l_updatetime"]
			v["l_updatetime"] = qu.FormatDateWithObj(&l_updatetime, qu.Date_Full_Layout)
		}
		c.JSON(http.StatusOK, gin.H{"data": data})
	})
	//块标签
	Admin.GET("/version/blockclassify_tag", func(c *gin.Context) {
		pid := c.Query("pid")
		vid := c.Query("vid")
		c.HTML(http.StatusOK, "blockclassify_tag.html", gin.H{"vid": vid, "pid": pid})
	})
	Admin.POST("/version/blockclassify_tag_delete", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		status := Mgo.UpdateById("block_classify_tag", _id, bson.M{"$set": bson.M{"delete": true}})
		c.JSON(http.StatusOK, gin.H{"status": status})
	})
	Admin.POST("/version/blockclassify_tag_save", func(c *gin.Context) {
		status := 0
		name, _ := c.GetPostForm("name")
		pid, _ := c.GetPostForm("pid")
		if Mgo.Count("block_classify_tag", map[string]interface{}{"delete": false, "name": name, "pid": pid}) == 1 {
			status = -1
		} else {
			_id, _ := c.GetPostForm("_id")
			if _id != "" {
				if Mgo.UpdateById("block_classify_tag", _id, bson.M{
					"$set": bson.M{
						"l_updatetime": time.Now().Unix(),
						"name":         name,
					},
				}) {
					status = 1
				}
			} else {
				vid, _ := c.GetPostForm("vid")
				unix := time.Now().Unix()
				if Mgo.Save("block_classify_tag", bson.M{
					"delete":       false,
					"name":         name,
					"vid":          vid,
					"pid":          pid,
					"l_createtime": unix,
					"l_updatetime": unix,
					"s_username":   sessions.Default(c).Get("username"),
				}) != "" {
					status = 1
				}
			}
		}
		c.JSON(http.StatusOK, gin.H{"status": status})
	})
	Admin.POST("/version/blockclassify_tag_list", func(c *gin.Context) {
		pid, _ := c.GetPostForm("pid")
		data, _ := Mgo.Find("block_classify_tag", bson.M{"pid": pid, "delete": false}, `{"l_updatetime":-1}`, `{"name":1,"l_updatetime":1}`, false, -1, -1)
		for _, v := range *data {
			v["id"] = qu.BsonIdToSId(v["_id"])
			l_updatetime := v["l_updatetime"]
			v["l_updatetime"] = qu.FormatDateWithObj(&l_updatetime, qu.Date_Full_Layout)
		}
		c.JSON(http.StatusOK, gin.H{"data": data})
	})
}

//克隆版本通用属性
func copyComRules(version, pvid, s_username string) {
	tmp, _ := Mgo.FindById("version", pvid, nil)
	oldversion := (*tmp)["version"].(string)
	//克隆前置规则
	plist, _ := Mgo.Find("rule_pre", `{"s_version":"`+oldversion+`","delete":false}`, nil, nil, false, -1, -1)
	for _, v := range *plist {
		delete(v, "_id")
		v["s_version"] = version
		v["s_code"] = util.GetSyncIndex(getCode(v["s_code"].(string)))
		v["s_username"] = s_username
		v["l_createtime"] = time.Now().Unix()
		v["l_lasttime"] = time.Now().Unix()
		Mgo.Save("rule_pre", v)
	}
	//克隆后置规则
	blist, _ := Mgo.Find("rule_back", `{"s_version":"`+oldversion+`","delete":false}`, nil, nil, false, -1, -1)
	for _, v := range *blist {
		delete(v, "_id")
		v["s_version"] = version
		v["s_code"] = util.GetSyncIndex(getCode(v["s_code"].(string)))
		v["s_username"] = s_username
		v["l_createtime"] = time.Now().Unix()
		v["l_lasttime"] = time.Now().Unix()
		Mgo.Save("rule_back", v)
	}
	//克隆tag
	tlist, _ := Mgo.Find("tag", `{"s_version":"`+oldversion+`","delete":false}`, nil, nil, false, -1, -1)
	for _, v := range *tlist {
		oldId := qu.BsonIdToSId(v["_id"])
		delete(v, "_id")
		v["s_version"] = version
		v["s_creater"] = s_username
		v["l_intime"] = time.Now().Unix()
		newId := Mgo.Save("tag", v) //克隆父标签
		td, _ := Mgo.Find("tagdetailinfo", `{"s_parentid":"`+oldId+`","delete":false}`, nil, nil, false, -1, -1)
		for _, v2 := range *td {
			delete(v2, "_id")
			v2["s_version"] = version
			v2["s_creater"] = s_username
			v2["l_intime"] = time.Now().Unix()
			v2["s_parentid"] = newId
			Mgo.Save("tagdetailinfo", v2) //克隆详细标签
		}
	}
	//克隆函数
	clist, _ := Mgo.Find("cleanup", `{"s_version":"`+oldversion+`","delete":false}`, nil, nil, false, -1, -1)
	for _, v := range *clist {
		delete(v, "_id")
		v["s_version"] = version
		v["s_creater"] = s_username
		v["l_intime"] = time.Now().Unix()
		Mgo.Save("cleanup", v)
	}
}

func copyPkgRules(vid, pid, s_field, oldvid, s_username string) {
	tmp, _ := Mgo.FindOne("pkg_info", `{"vid":"`+oldvid+`","s_field":"`+s_field+`","delete":false}`)
	oldpid := qu.BsonIdToSId((*tmp)["_id"])
	//克隆逻辑列表
	logicList, _ := Mgo.Find("pkg_logic", `{"vid":"`+oldvid+`","pid":"`+oldpid+`","delete":false}`, `{"_id":1}`, nil, false, -1, -1)
	for _, logic := range *logicList {
		if len(logic) > 0 {
			oldsid := qu.BsonIdToSId(logic["_id"])
			delete(logic, "_id")
			logic["vid"] = vid
			logic["pid"] = pid
			logic["l_createtime"] = time.Now().Unix()
			logic["l_lasttime"] = time.Now().Unix()
			logic["s_username"] = s_username
			sid := Mgo.Save("pkg_logic", logic)
			//克隆过滤规则
			blist, _ := Mgo.Find("pkg_logicback", `{"sid":"`+oldsid+`","delete":false}`, `{"_id":1}`, nil, false, -1, -1)
			for _, v := range *blist {
				delete(v, "_id")
				v["vid"] = vid
				v["pid"] = pid
				v["sid"] = sid
				v["s_code"] = util.GetSyncIndex(getCode(v["s_code"].(string)))
				v["s_username"] = s_username
				v["l_createtime"] = time.Now().Unix()
				v["l_lasttime"] = time.Now().Unix()
				Mgo.Save("pkg_logicback", v)
			}
		}
	}

}

//克隆版本Field
func copyFieldRules(vid, pid, s_field, oldvid, s_username string) {
	tmp, _ := Mgo.FindOne("versioninfo", `{"vid":"`+oldvid+`","s_field":"`+s_field+`","delete":false}`)
	oldpid := qu.BsonIdToSId((*tmp)["_id"])
	//克隆逻辑列表
	logicList, _ := Mgo.Find("rule_logic", `{"vid":"`+oldvid+`","pid":"`+oldpid+`","delete":false}`, `{"_id":1}`, nil, false, -1, -1)
	for _, logic := range *logicList {
		if len(logic) > 0 {
			oldsid := qu.BsonIdToSId(logic["_id"])
			delete(logic, "_id")
			logic["vid"] = vid
			logic["pid"] = pid
			logic["l_createtime"] = time.Now().Unix()
			logic["l_lasttime"] = time.Now().Unix()
			logic["s_username"] = s_username
			sid := Mgo.Save("rule_logic", logic)
			//克隆前置规则
			plist, _ := Mgo.Find("rule_logicpre", `{"sid":"`+oldsid+`","delete":false}`, nil, nil, false, -1, -1)
			for _, v := range *plist {
				delete(v, "_id")
				v["vid"] = vid
				v["pid"] = pid
				v["sid"] = sid
				v["s_code"] = util.GetSyncIndex(getCode(v["s_code"].(string)))
				v["s_username"] = s_username
				v["l_createtime"] = time.Now().Unix()
				v["l_lasttime"] = time.Now().Unix()
				Mgo.Save("rule_logicpre", v)
			}
			//克隆抽取规则
			elist, _ := Mgo.Find("rule_logicore", `{"sid":"`+oldsid+`","delete":false}`, nil, nil, false, -1, -1)
			for _, v := range *elist {
				delete(v, "_id")
				v["vid"] = vid
				v["pid"] = pid
				v["sid"] = sid
				v["s_code"] = util.GetSyncIndex(getCode(v["s_code"].(string)))
				v["s_username"] = s_username
				v["l_createtime"] = time.Now().Unix()
				v["l_lasttime"] = time.Now().Unix()
				Mgo.Save("rule_logicore", v)
			}
			//克隆过滤规则
			blist, _ := Mgo.Find("rule_logicback", `{"sid":"`+oldsid+`","delete":false}`, nil, nil, false, -1, -1)
			for _, v := range *blist {
				delete(v, "_id")
				v["vid"] = vid
				v["pid"] = pid
				v["sid"] = sid
				v["s_code"] = util.GetSyncIndex(getCode(v["s_code"].(string)))
				v["s_username"] = s_username
				v["l_createtime"] = time.Now().Unix()
				v["l_lasttime"] = time.Now().Unix()
				Mgo.Save("rule_logicback", v)
			}
		}
	}

}

//克隆清理版本Field
func copyClearRuleLogic(vid, pid, s_field, oldvid, s_username string) {
	tmp, _ := Mgo.FindOne("clearversioninfo", `{"vid":"`+oldvid+`","s_field":"`+s_field+`","delete":false}`)
	oldpid := qu.BsonIdToSId((*tmp)["_id"])
	elist, _ := Mgo.Find("clearulelogic", `{"vid":"`+oldvid+`","pid":"`+oldpid+`","delete":false}`, nil, nil, false, -1, -1)
	for _, v := range *elist {
		delete(v, "_id")
		v["vid"] = vid
		v["pid"] = pid
		v["s_code"] = util.GetSyncIndex(getCode(v["s_code"].(string)))
		v["s_username"] = s_username
		v["l_createtime"] = time.Now().Unix()
		v["l_lasttime"] = time.Now().Unix()
		Mgo.Save("clearulelogic", v)
	}

}

//获取代码
func getCode(code string) string {
	tmps := strings.Split(code, "_")
	tmp := tmps[:len(tmps)-1]
	return strings.Join(tmp, "_")
}
