// rule
package admin

import (
	"fmt"
	. "jy/mongodbutil"
	"jy/util"
	qu "qfw/util"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

var Date_Short_Layout = "2006-01-02"

const (
	F_E  = "F_E"  //前置正则
	F_L  = "F_L"  //前置脚本
	B_E  = "B_E"  //后置正则
	B_L  = "B_L"  //后置脚本
	E_FE = "E_FE" //抽取前置正则
	E_FL = "E_FL" //抽取前置脚本
	E_BE = "E_BE" //抽取后置正则
	E_BL = "E_BL" //抽取后置脚本
	E_CE = "E_CE" //抽取逻辑正则
	E_CL = "E_CL" //抽取逻辑脚本
	C_CL = "C_CL" //清理逻辑脚本
)

func init() {
	Admin.GET("/rulepre", func(c *gin.Context) {
		version := c.Query("version")
		c.HTML(200, "rule_prelist.html", gin.H{"version": version})
	})
	Admin.POST("/rulepre/data", RulePreData)
	Admin.POST("/rulepre/save", RulePreSave)
	Admin.POST("/rulepre/searchbyid", RulePreSearchById)
	Admin.POST("/rulepre/del", RulePreDel)
	Admin.GET("/rulepre/luastr", RulePreLua)

	//ruleback
	Admin.GET("/ruleback", func(c *gin.Context) {
		version := c.Query("version")
		c.HTML(200, "rule_backlist.html", gin.H{"version": version})
	})
	Admin.POST("/ruleback/data", RuleBackData)
	Admin.POST("/ruleback/save", RuleBackSave)
	Admin.POST("/ruleback/searchbyid", RuleBackSearchById)
	Admin.POST("/ruleback/del", RuleBackDel)
	Admin.GET("/ruleback/luastr", RuleBackLua)

	//rulelogic
	Admin.GET("/rulelogic", func(c *gin.Context) {
		vid := c.Query("vid")
		pid := c.Query("pid")
		c.HTML(200, "rule_logiclist.html", gin.H{"vid": vid, "pid": pid})
	})
	Admin.POST("/rulelogic/data", RuleLogicData)
	Admin.POST("/rulelogic/save", RuleLogicSave)
	Admin.POST("/rulelogic/del", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		//b := Mgo.Del("rule_logic", `{"_id":"`+_id+`"}`)
		b := Mgo.UpdateById("rule_logic", _id, `{"$set":{"delete":true}}`)
		go DelLogicPre(_id, false)
		go DelLogicCore(_id, false)
		go DelLogicBack(_id, false)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.POST("/rulelogic/use", RuleLogicUse)

	//pkglogic
	//Admin.GET("/pkglogic", func(c *gin.Context) {
	//	vid := c.Query("vid")
	//	pid := c.Query("pid")
	//	c.HTML(200, "pkg_logiclist.html", gin.H{"vid": vid, "pid": pid})
	//})
	//Admin.POST("/pkglogic/data", PkgLogicData)
	//Admin.POST("/pkglogic/save", PkgLogicSave)
	//Admin.POST("/pkgrulelogic/del", func(c *gin.Context) {
	//	_id, _ := c.GetPostForm("_id")
	//	b := Mgo.UpdateById("pkg_logic", _id, `{"$set":{"delete":true}}`)
	//	go DelPkgLogicBack(_id)
	//	c.JSON(200, gin.H{"rep": b})
	//})
	//Admin.POST("/pkglogic/use", PkgLogicUse)

	Admin.GET("/logicpre", func(c *gin.Context) {
		vid := c.Query("vid")
		pid := c.Query("pid")
		sid := c.Query("sid")
		c.HTML(200, "rule_logicprelist.html", gin.H{"vid": vid, "pid": pid, "sid": sid})
	})
	Admin.POST("/logicpre/data", RuleLogicPreData)
	Admin.POST("/logicpre/save", RuleLogicPreSave)
	Admin.POST("/logicpre/del", RuleLogicPreDel)

	Admin.GET("/logicback", func(c *gin.Context) {
		vid := c.Query("vid")
		pid := c.Query("pid")
		sid := c.Query("sid")
		//查询属性字段
		tmp, _ := Mgo.FindById("versioninfo", pid, `{"s_field":1}`)
		c.HTML(200, "rule_logicbacklist.html", gin.H{"vid": vid, "pid": pid, "sid": sid, "field": (*tmp)["s_field"]})
	})
	Admin.POST("/logicback/data", RuleLogicBackData)
	Admin.POST("/logicback/save", RuleLogicBackSave)
	Admin.POST("/logicback/del", RuleLogicBackDel)
	Admin.GET("/logicore", func(c *gin.Context) {
		vid := c.Query("vid")
		pid := c.Query("pid")
		sid := c.Query("sid")
		//查询属性字段
		tmp, _ := Mgo.FindById("versioninfo", pid, `{"s_field":1}`)
		c.HTML(200, "rule_logicore.html", gin.H{"vid": vid, "pid": pid, "sid": sid, "field": (*tmp)["s_field"]})
	})
	Admin.POST("/logicore/data", RuleLogicCoreData)
	Admin.POST("/logicore/save", RuleLogicCoreSave)
	Admin.POST("/logicore/del", RuleLogicCoreDel)
	Admin.POST("/logicore/use", RuleLogicCoreUse)

	Admin.GET("/logickv", func(c *gin.Context) {
		vid := c.Query("vid")
		pid := c.Query("pid")
		sid := c.Query("sid")
		//查询属性字段
		tmp, _ := Mgo.FindById("versioninfo", pid, `{"s_field":1}`)
		c.HTML(200, "rule_logickv.html", gin.H{"vid": vid, "pid": pid, "sid": sid, "field": (*tmp)["s_field"]})
	})
	Admin.POST("/logickv/data", RuleLogicCoreDataKv)
	Admin.POST("/logickv/save", RuleLogicCoreSaveKv)
	Admin.POST("/logickv/del", RuleLogicCoreDelKv)
	Admin.POST("/logickv/use", RuleLogicCoreUseKv)

	Admin.GET("/pkglogicback", func(c *gin.Context) {
		vid := c.Query("vid")
		s_field := c.Query("s_field")
		sid := c.Query("sid")
		//查询属性字段
		//tmp, _ := Mgo.FindById("pkg_info", pid, `{"s_field":1}`)
		c.HTML(200, "pkg_logicbacklist.html", gin.H{"vid": vid, "sid": sid, "field": s_field, "s_field": s_field})
	})
	Admin.POST("/pkglogicback/data", PkgLogicBackData)
	Admin.POST("/pkglogicback/save", PkgLogicBackSave)
	Admin.POST("/pkglogicback/del", PkgRuleLogicBackDel)

	//clearule
	Admin.GET("/clearulelogic", func(c *gin.Context) {
		vid := c.Query("vid")
		pid := c.Query("pid")
		//查询属性字段
		tmp, _ := Mgo.FindById("clearversioninfo", pid, `{"s_field":1}`)
		c.HTML(200, "rule_clearlogiclist.html", gin.H{"vid": vid, "pid": pid, "field": (*tmp)["s_field"]})
	})
	Admin.POST("/clearulelogic/data", ClearRuleLogicData)
	Admin.POST("/clearulelogic/save", ClearRuleLogicSave)
	Admin.POST("/clearulelogic/use", ClearRuleLogicUse)
	Admin.POST("/clearulelogic/del", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		b := Mgo.UpdateById("clearulelogic", _id, `{"$set":{"delete":true}}`)
		c.JSON(200, gin.H{"rep": b})
	})
}

func RulePreData(c *gin.Context) {
	version, _ := c.GetPostForm("version")
	data, _ := Mgo.Find("rule_pre", `{"s_version":"`+version+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "version": version})
}
func RulePreSave(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		s_type := data["s_type"]
		code := ""
		if s_type == "0" { //前置正则
			code = util.GetSyncIndex(F_E)
		} else { //前置脚本
			code = util.GetSyncIndex(F_L)
		}
		data["s_code"] = code
		b = Mgo.Save("rule_pre", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("rule_pre", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func RulePreSearchById(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	data, _ := Mgo.FindById("rule_pre", _id, nil)
	c.JSON(200, gin.H{"rep": data})
}
func RulePreDel(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	//b := Mgo.Del("rule_pre", `{"_id":"`+_id+`"}`)
	b := Mgo.UpdateById("rule_pre", _id, `{"$set":{"delete":true}}`)
	c.JSON(200, gin.H{"rep": b})
}
func RulePreLua(c *gin.Context) {
	version := c.Query("version")
	_id := c.Query("_id")
	data := map[string]interface{}{}
	if _id != "" {
		tmp, _ := Mgo.FindById("rule_pre", _id, nil)
		data = *tmp
		if version == "" {
			version = fmt.Sprint((*tmp)["s_version"])
		}
		data["_id"] = _id
	}
	c.HTML(200, "rule_prelua.html", gin.H{"version": version, "data": data})
}

func RuleBackData(c *gin.Context) {
	version, _ := c.GetPostForm("version")
	data, _ := Mgo.Find("rule_back", `{"s_version":"`+version+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "version": version})
}
func RuleBackSave(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		s_type := data["s_type"]
		code := ""
		if s_type == "0" { //前置正则
			code = util.GetSyncIndex(B_E)
		} else { //前置lua脚本
			code = util.GetSyncIndex(B_L)
		}
		data["s_code"] = code
		b = Mgo.Save("rule_back", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("rule_back", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func RuleBackSearchById(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	data, _ := Mgo.FindById("rule_back", _id, nil)
	c.JSON(200, gin.H{"rep": data})
}
func RuleBackDel(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	//b := Mgo.Del("rule_back", `{"_id":"`+_id+`"}`)
	b := Mgo.UpdateById("rule_back", _id, `{"$set":{"delete":true}}`)
	c.JSON(200, gin.H{"rep": b})
}
func RuleBackLua(c *gin.Context) {
	version := c.Query("version")
	_id := c.Query("_id")
	data := map[string]interface{}{}
	if _id != "" {
		tmp, _ := Mgo.FindById("rule_back", _id, nil)
		data = *tmp
		if version == "" {
			version = fmt.Sprint((*tmp)["s_version"])
		}
		data["_id"] = _id
	}
	c.HTML(200, "rule_backlua.html", gin.H{"version": version, "data": data})
}

func RuleLogicData(c *gin.Context) {
	vid, _ := c.GetPostForm("vid")
	pid, _ := c.GetPostForm("pid")
	data, _ := Mgo.Find("rule_logic", `{"vid":"`+vid+`","pid":"`+pid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "vid": vid, "pid": pid})
}
func PkgLogicData(c *gin.Context) {
	vid, _ := c.GetPostForm("vid")
	pid, _ := c.GetPostForm("pid")
	data, _ := Mgo.Find("pkg_logic", `{"vid":"`+vid+`","pid":"`+pid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "vid": vid, "pid": pid})
}
func RuleLogicSave(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		b = Mgo.Save("rule_logic", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("rule_logic", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func PkgLogicSave(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		b = Mgo.Save("pkg_logic", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("pkg_logic", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicUse(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	isuse, _ := c.GetPostForm("isuse")
	b := Mgo.UpdateById("rule_logic", _id, `{"$set":{"isuse":`+isuse+`}}`)
	c.JSON(200, gin.H{"rep": b})
}
func PkgLogicUse(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	isuse, _ := c.GetPostForm("isuse")
	b := Mgo.UpdateById("pkg_logic", _id, `{"$set":{"isuse":`+isuse+`}}`)
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicPreData(c *gin.Context) {
	vid, _ := c.GetPostForm("vid")
	pid, _ := c.GetPostForm("pid")
	sid, _ := c.GetPostForm("sid")
	data, _ := Mgo.Find("rule_logicpre", `{"sid":"`+sid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "vid": vid, "pid": pid, "sid": sid})
}
func RuleLogicPreSave(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		s_type := data["s_type"]
		code := ""
		if s_type == "0" { //抽取前置正则E_FE
			code = util.GetSyncIndex(E_FE)
		} else { //抽取前置脚本
			code = util.GetSyncIndex(E_FL)
		}
		data["s_code"] = code
		b = Mgo.Save("rule_logicpre", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("rule_logicpre", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicPreDel(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	//b := Mgo.Del("rule_logicpre", `{"_id":"`+_id+`"}`)
	b := Mgo.UpdateById("rule_logicpre", _id, `{"$set":{"delete":true}}`)
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicBackData(c *gin.Context) {
	vid, _ := c.GetPostForm("vid")
	pid, _ := c.GetPostForm("pid")
	sid, _ := c.GetPostForm("sid")
	data, _ := Mgo.Find("rule_logicback", `{"sid":"`+sid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "vid": vid, "pid": pid, "sid": sid})
}
func PkgLogicBackData(c *gin.Context) {
	vid, _ := c.GetPostForm("vid")
	s_field, _ := c.GetPostForm("s_field")
	sid, _ := c.GetPostForm("sid")
	data, _ := Mgo.Find("pkg_logicback", `{"sid":"`+sid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "vid": vid, "s_field": s_field, "sid": sid})
}
func RuleLogicBackSave(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		s_type := data["s_type"]
		code := ""
		if s_type == "0" { //抽取后置正则E_FE
			code = util.GetSyncIndex(E_BE)
		} else { //抽取后置脚本
			code = util.GetSyncIndex(E_BL)
		}
		data["s_code"] = code
		b = Mgo.Save("rule_logicback", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("rule_logicback", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func PkgLogicBackSave(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		s_type := data["s_type"]
		code := ""
		if s_type == "0" { //抽取后置正则E_FE
			code = util.GetSyncIndex(E_BE)
		} else { //抽取后置脚本
			code = util.GetSyncIndex(E_BL)
		}
		data["s_code"] = code
		b = Mgo.Save("pkg_logicback", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("pkg_logicback", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicBackDel(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	b := Mgo.UpdateById("rule_logicback", _id, `{"$set":{"delete":true}}`)
	c.JSON(200, gin.H{"rep": b})
}

func PkgRuleLogicBackDel(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	b := Mgo.UpdateById("pkg_logicback", _id, `{"$set":{"delete":true}}`)
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicCoreData(c *gin.Context) {
	vid, _ := c.GetPostForm("vid")
	pid, _ := c.GetPostForm("pid")
	sid, _ := c.GetPostForm("sid")
	data, _ := Mgo.Find("rule_logicore", `{"sid":"`+sid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "vid": vid, "pid": pid, "sid": sid})
}
func RuleLogicCoreDataKv(c *gin.Context) {
	vid, _ := c.GetPostForm("vid")
	pid, _ := c.GetPostForm("pid")
	sid, _ := c.GetPostForm("sid")
	data, _ := Mgo.Find("rule_logickv", `{"sid":"`+sid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "vid": vid, "pid": pid, "sid": sid})
}
func RuleLogicCoreSave(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		score := qu.Float64All(data["s_default_score"])
		data["s_default_score"] = score
		s_type := data["s_type"]
		code := ""
		if s_type == "0" { //抽取逻辑正则
			code = util.GetSyncIndex(E_CE)
		} else { //抽取逻辑脚本
			code = util.GetSyncIndex(E_CL)
		}
		data["s_code"] = code
		b = Mgo.Save("rule_logicore", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		score := qu.Float64All(data["s_default_score"])
		data["s_default_score"] = score
		b = Mgo.Update("rule_logicore", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicCoreSaveKv(c *gin.Context) {
	data := GetPostForm(c)
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		s_type := data["s_type"]
		code := ""
		if s_type == "0" { //抽取逻辑正则
			code = util.GetSyncIndex(E_CE)
		} else { //抽取逻辑脚本
			code = util.GetSyncIndex(E_CL)
		}
		data["s_code"] = code
		b = Mgo.Save("rule_logickv", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("rule_logickv", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicCoreDel(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	//b := Mgo.Del("rule_logicore", `{"_id":"`+_id+`"}`)
	b := Mgo.UpdateById("rule_logicore", _id, `{"$set":{"delete":true}}`)
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicCoreDelKv(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	//b := Mgo.Del("rule_logicore", `{"_id":"`+_id+`"}`)
	b := Mgo.UpdateById("rule_logickv", _id, `{"$set":{"delete":true}}`)
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicCoreUse(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	isuse, _ := c.GetPostForm("isuse")
	b := Mgo.UpdateById("rule_logicore", _id, `{"$set":{"isuse":`+isuse+`}}`)
	c.JSON(200, gin.H{"rep": b})
}
func RuleLogicCoreUseKv(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	isuse, _ := c.GetPostForm("isuse")
	b := Mgo.UpdateById("rule_logickv", _id, `{"$set":{"isuse":`+isuse+`}}`)
	c.JSON(200, gin.H{"rep": b})
}

func DelVersionInfo(vid string, isSite bool) { //刪除属性配置
	var versioninfodb string
	if isSite {
		versioninfodb = "site_versioninfo"
	} else {
		versioninfodb = "versioninfo"
	}
	vInfo, _ := Mgo.Find(versioninfodb, `{"vid":"`+vid+`","delete":false}`, nil, `{"_id":1}`, false, -1, -1)
	Mgo.Update(versioninfodb, `{"vid":"`+vid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
	for _, v := range *vInfo {
		DelLogic(vid, qu.BsonIdToSId((v)["_id"]), isSite)
	}
}

func DelPkgInfo(vid string) { //刪除分包配置
	vInfo, _ := Mgo.Find("pkg_info", `{"vid":"`+vid+`","delete":false}`, nil, `{"_id":1}`, false, -1, -1)
	Mgo.Update("pkg_info", `{"vid":"`+vid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
	for _, v := range *vInfo {
		DelPkgLogic(vid, qu.BsonIdToSId((v)["_id"]))
	}
}

func DelLogic(vid, pid string, isSite bool) { //删除逻辑
	var rule_logic string
	if isSite {
		rule_logic = "site_rule_logic"
	} else {
		rule_logic = "rule_logic"
	}
	logic, _ := Mgo.Find(rule_logic, `{"vid":"`+vid+`","pid":"`+pid+`","delete":false}`, nil, `{"_id":1}`, false, -1, -1)
	Mgo.Update(rule_logic, `{"vid":"`+vid+`","pid":"`+pid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
	for _, l := range *logic {
		DelLogicPre(qu.BsonIdToSId(l["_id"]), isSite)
		DelLogicCore(qu.BsonIdToSId(l["_id"]), isSite)
		DelLogicBack(qu.BsonIdToSId(l["_id"]), isSite)
	}
}

func DelPkgLogic(vid, pid string) { //删除分包逻辑
	logic, _ := Mgo.Find("pkg_logic", `{"vid":"`+vid+`","pid":"`+pid+`","delete":false}`, nil, `{"_id":1}`, false, -1, -1)
	Mgo.Update("pkg_logic", `{"vid":"`+vid+`","pid":"`+pid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
	for _, l := range *logic {
		DelPkgLogicBack(qu.BsonIdToSId(l["_id"]))
	}
}

func DelLogicPre(sid string, isSite bool) { //删除属性配置中的前置规则
	var rule_logicpre string
	if isSite {
		rule_logicpre = "site_rule_logicpre"
	} else {
		rule_logicpre = "rule_logicpre"
	}
	Mgo.Update(rule_logicpre, `{"sid":"`+sid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
}
func DelLogicCore(sid string, isSite bool) { //删除属性配置中的抽取规则
	var rule_logicore string
	if isSite {
		rule_logicore = "site_rule_logicore"
	} else {
		rule_logicore = "rule_logicore"
	}
	Mgo.Update(rule_logicore, `{"sid":"`+sid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
}
func DelLogicBack(sid string, isSite bool) { //删除属性配置中的后置规则
	var rule_logicback string
	if isSite {
		rule_logicback = "site_rule_logicback"
	} else {
		rule_logicback = "rule_logicback"
	}
	Mgo.Update(rule_logicback, `{"sid":"`+sid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
}
func DelPkgLogicBack(sid string) { //删除分包属性配置中的后置规则
	Mgo.Update("pkg_logicback", `{"sid":"`+sid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
}

func DelRulePre(s_version string) { //删除通用前置过滤
	Mgo.Update("rule_pre", `{"s_version":"`+s_version+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
}

func DelRuleBack(s_version string, isSite bool) { //删除通用后置过滤
	var rule_backdb string
	if isSite {
		rule_backdb = "site_rule_back"
	} else {
		rule_backdb = "rule_back"
	}
	Mgo.Update(rule_backdb, `{"s_version":"`+s_version+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
}
func DelTag(s_version string, isSite bool) {
	var tagdb, tagdetailinfodb string
	if isSite {
		tagdb = "site_tag"
		tagdetailinfodb = "site_tagdetailinfo"
	} else {
		tagdb = "tag"
		tagdetailinfodb = "tagdetailinfo"
	}
	tag, _ := Mgo.Find(tagdb, `{"s_version":"`+s_version+`","delete":false}`, nil, `{"_id":1}`, false, -1, -1)
	Mgo.Update(tagdb, `{"s_version":"`+s_version+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
	for _, t := range *tag {
		s_parentid := qu.BsonIdToSId(t["_id"])
		Mgo.Update(tagdetailinfodb, `{"s_version":"`+s_version+`","s_parentid":"`+s_parentid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
	}
}
func DelCleanUp(s_version string, isSite bool) {
	var cleanupdb string
	if isSite {
		cleanupdb = "site_cleanup"
	} else {
		cleanupdb = "cleanup"
	}
	Mgo.Update(cleanupdb, `{"s_version":"`+s_version+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
}

func ClearRuleLogicData(c *gin.Context) {
	vid, _ := c.GetPostForm("vid")
	pid, _ := c.GetPostForm("pid")
	data, _ := Mgo.Find("clearulelogic", `{"vid":"`+vid+`","pid":"`+pid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	c.JSON(200, gin.H{"data": data, "vid": vid, "pid": pid})
}
func ClearRuleLogicSave(c *gin.Context) {
	data := GetPostForm(c)
	delete(data, "resultcoll")
	delete(data, "rid")
	_id, _ := c.GetPostForm("_id")
	b := false
	if _id == "" {
		data["l_createtime"] = time.Now().Unix()
		data["s_username"] = sessions.Default(c).Get("username")
		data["delete"] = false
		data["s_code"] = util.GetSyncIndex(C_CL)
		b = Mgo.Save("clearulelogic", data) != ""
	} else {
		data["l_lasttime"] = time.Now().Unix()
		b = Mgo.Update("clearulelogic", `{"_id":"`+_id+`"}`, map[string]interface{}{
			"$set": data,
		}, true, false)
	}
	c.JSON(200, gin.H{"rep": b})
}
func ClearRuleLogicUse(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	isuse, _ := c.GetPostForm("isuse")
	b := Mgo.UpdateById("clearulelogic", _id, `{"$set":{"isuse":`+isuse+`}}`)
	c.JSON(200, gin.H{"rep": b})
}
func DelClearVersionInfo(vid string) { //刪除清理的属性配置
	vInfo, _ := Mgo.Find("clearversioninfo", `{"vid":"`+vid+`","delete":false}`, nil, `{"_id":1}`, false, -1, -1)
	Mgo.Update("clearversioninfo", `{"vid":"`+vid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
	for _, v := range *vInfo {
		DelClearLogic(vid, qu.BsonIdToSId((v)["_id"]))
	}
}
func DelClearLogic(vid, pid string) { //删除清理逻辑
	Mgo.Update("clearulelogic", `{"vid":"`+vid+`","pid":"`+pid+`","delete":false}`, `{"$set":{"delete":true}}`, false, true)
}
