package audit

import (
	"encoding/json"
	. "jy/admin"
	"jy/clear"
	. "jy/mongodbutil"
	"jy/util"
	"log"
	qu "qfw/util"

	//"qfw/util/elastic"
	redis "qfw/util/redis"
	"strconv"
	"strings"
	"time"

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

func init() {
	//列表、编辑
	Admin.GET("/audit/dataaudit", func(c *gin.Context) {
		name := c.Query("name")
		coll := c.Query("coll")
		stancoll := c.Query("stancoll")
		c.HTML(200, "audit_auditone.html", gin.H{"name": name, "coll": coll, "stancoll": stancoll})
	})
	Admin.POST("/audit/auditonefield", AuditOneField)
	Admin.POST("/audit/addsave", AddSave)     //新增
	Admin.POST("/audit/auditsave", AuditSave) //保存
	Admin.POST("/audit/alldel", AllDel)       //批量删除
	Admin.POST("/audit/allaudit", AllAudit)   //批量审核
	Admin.POST("/audit/datasave", DataSave)   //标准库数据审核
}

func AuditData(c *gin.Context) {
	v, _ := Mgo.FindOne("version", `{"isuse":true,"delete":false}`)
	if len(*v) > 0 {
		vid := qu.BsonIdToSId((*v)["_id"])
		query := map[string]interface{}{
			"isaudit": true,
			"delete":  false,
			"vid":     vid,
		}
		data, _ := Mgo.Find("versioninfo", query, `{"_id":-1}`, nil, false, -1, -1)
		for _, d := range *data {
			timeStr := time.Unix(d["l_lasttime"].(int64), 0).Format(Date_Short_Layout)
			d["l_lasttime"] = timeStr
		}
		c.JSON(200, gin.H{"data": data})
	} else {
		c.JSON(200, gin.H{"data": map[string]interface{}{}})
	}
}

func AllDel(c *gin.Context) {
	coll, _ := c.GetPostForm("coll")
	ids, _ := c.GetPostForm("ids")
	idsArr := strings.Split(ids, ",")
	for _, id := range idsArr {
		query := map[string]interface{}{
			"_id": qu.StringTOBsonId(id),
		}
		Mgo.Del(coll, query)
	}
	c.JSON(200, gin.H{"rep": true})
}

func AuditOneField(c *gin.Context) {
	field, _ := c.GetPostForm("field")
	coll, _ := c.GetPostForm("coll")
	start := c.GetInt("start")
	limit := c.GetInt("length")
	auditattr, _ := c.GetPostForm("auditattr")
	check, _ := strconv.Atoi(auditattr)
	query := map[string]interface{}{}
	if check != -1 {
		query = map[string]interface{}{
			"check": check,
		}
	} else {
		query = map[string]interface{}{
			"$or": []interface{}{
				map[string]interface{}{"check": 1},
				map[string]interface{}{"check": 0},
			},
		}
	}
	data, _ := Mgo.Find(coll, query, `{"_id":1}`, `{"topscopeclass":1,"`+field+`":1}`, false, start, limit)
	count := Mgo.Count(coll, query)
	page := start / 10
	for k, d := range *data {
		d["num"] = k + 1 + page*10
		d["topscopeclass"] = strings.Join(qu.ObjArrToStringArr(d["topscopeclass"].([]interface{})), ",")
		d["text"] = d[field]
	}
	c.JSON(200, gin.H{"data": data, "recordsFiltered": count, "recordsTotal": count})
}

func AllAudit(c *gin.Context) {
	field, _ := c.GetPostForm("field")
	coll, _ := c.GetPostForm("coll")
	stancoll, _ := c.GetPostForm("stancoll")
	ids, _ := c.GetPostForm("ids")
	idsArr := strings.Split(ids, ",")
	log.Println("Audit Ids:", idsArr)
	names, _ := c.GetPostForm("names")
	namesArr := strings.Split(names, ",")
	if len(idsArr) != len(namesArr) {
		c.JSON(200, gin.H{"rep": false, "msg": "数据错误"})
	} else { //批量审核
		FieldBd := 0
		// ElasticClientIndex := ""
		// ElasticClientType := ""
		RedisName := util.QYK_RedisName
		if field == "winner" {
			FieldBd = util.WinnerDB
			// ElasticClientIndex = util.ElasticClientIndex
			// ElasticClientType = util.ElasticClientType
		} else if field == "buyer" {
			FieldBd = util.BuyerDB
			// ElasticClientIndex = util.ElasticClientBuyerIndex
			// ElasticClientType = util.ElasticClientBuyerType
		} else {
			FieldBd = util.AgencyDB
			// ElasticClientIndex = util.ElasticClientAgencyIndex
			// ElasticClientType = util.ElasticClientAgencyType
		}
		//redis
		qykredis := redis.RedisPool[RedisName].Get()
		defer qykredis.Close()
		//es
		// escon := elastic.GetEsConn()
		// defer elastic.DestoryEsConn(escon)
		for i, name := range namesArr {
			e := make(map[string]interface{})
			e["comeintime"] = time.Now().Unix()
			e["updatetime"] = time.Now().Unix()
			if field == "winner" {
				e["company_name"] = name
			} else if field == "buyer" {
				e["buyer_name"] = name
			} else {
				e["agency_name"] = name
			}
			sid := Mgo.Save(stancoll, e)
			if sid == "" {
				c.JSON(200, gin.H{"rep": false, "msg": "保存mongo出错"})
				return
			} else {
				if _, err := qykredis.Do("SELECT", FieldBd); err != nil {
					c.JSON(200, gin.H{"rep": false, "msg": "select redis出错"})
					return
				} else {
					if _, saveRedisErr := qykredis.Do("SET", name, sid); saveRedisErr != nil {
						c.JSON(200, gin.H{"rep": false, "msg": "保存redis出错"})
						return
					}
				}
				// _, err := escon.Index().Index(ElasticClientIndex).Type(ElasticClientType).Id(sid).BodyJson(e).Refresh(true).Do()
				// if err != nil {
				// 	c.JSON(200, gin.H{"rep": false, "msg": "更新es错误"})
				// 	return
				// }
			}
			//删除标记数据
			query := map[string]interface{}{
				"_id": qu.StringTOBsonId(idsArr[i]),
			}
			b := Mgo.Del(coll, query)
			if !b {
				log.Println("Del Audit Data Error 	coll:", coll, "	field:", field, "	id:", idsArr[i])
			}
		}
		c.JSON(200, gin.H{"rep": true, "msg": "审核通过"})
	}
}
func DataSave(c *gin.Context) {
	FieldBd := 0
	// ElasticClientIndex := ""
	// ElasticClientType := ""
	RedisName := util.QYK_RedisName
	//企业名称
	e := make(map[string]interface{})
	field, _ := c.GetPostForm("field")
	name, _ := c.GetPostForm("name")
	address, _ := c.GetPostForm("address")
	stancoll, _ := c.GetPostForm("stancoll")
	if field == "winner" {
		FieldBd = util.WinnerDB
		// ElasticClientIndex = util.ElasticClientIndex
		// ElasticClientType = util.ElasticClientType
		capital, _ := c.GetPostForm("capital")
		capitalfloat := clear.ObjToMoney([]interface{}{capital, ""})[0]
		business_scope, _ := c.GetPostForm("business_scope")
		e["capital"] = capitalfloat
		e["business_scope"] = business_scope
		e["company_name"] = name
		e["company_address"] = address
	} else if field == "buyer" {
		FieldBd = util.BuyerDB
		// ElasticClientIndex = util.ElasticClientBuyerIndex
		// ElasticClientType = util.ElasticClientBuyerType
		buyerclass, _ := c.GetPostForm("buyerclass")
		ranks, _ := c.GetPostForm("ranks")
		buyer_type, _ := c.GetPostForm("type")
		e["buyerclass"] = buyerclass
		e["ranks"] = ranks
		e["type"] = buyer_type
		e["buyer_name"] = name
		e["address"] = address
	} else {
		FieldBd = util.AgencyDB
		// ElasticClientIndex = util.ElasticClientAgencyIndex
		// ElasticClientType = util.ElasticClientAgencyType
		ranks, _ := c.GetPostForm("ranks")
		agency_type, _ := c.GetPostForm("type")
		e["ranks"] = ranks
		e["type"] = agency_type
		e["agency_name"] = name
		e["address"] = address
	}
	//历史名称
	history_name, _ := c.GetPostForm("history_name")
	history_name = strings.ReplaceAll(history_name, "；", ";")
	e["history_name"] = history_name
	//城市信息
	area_code, _ := c.GetPostForm("area_code")
	province, _ := c.GetPostForm("province")
	city, _ := c.GetPostForm("city")
	district, _ := c.GetPostForm("district")
	e["area_code"] = area_code
	e["province"] = province
	e["city"] = city
	e["district"] = district
	//公众号
	wechat_accounts, _ := c.GetPostForm("wechat_accounts")
	wechat_accounts = strings.ReplaceAll(wechat_accounts, "；", ";")
	if len(wechat_accounts) > 0 {
		e["wechat_accounts"] = strings.Split(wechat_accounts, ";")
	} else {
		e["wechat_accounts"] = []string{}
	}
	//网址
	website, _ := c.GetPostForm("website")
	e["website"] = website
	//联系人
	contact, _ := c.GetPostForm("contact")
	contacts := make([]map[string]interface{}, 0)
	jsonerr := json.Unmarshal([]byte(contact), &contacts)
	if jsonerr != nil {
		e["contact"] = []map[string]interface{}{}
	} else {
		for k, v := range contacts {
			if v["updatetime"] == nil {
				contacts[k]["updatetime"] = time.Now().Unix()
			}
		}
		e["contact"] = contacts
	}
	//入库时间
	e["comeintime"] = time.Now().Unix()
	e["updatetime"] = time.Now().Unix()
	//新数据保存
	sid := Mgo.Save(stancoll, e)
	if sid == "" {
		c.JSON(200, gin.H{"rep": false, "msg": "保存mongo出错"})
		return
	} else {
		qykredis := redis.RedisPool[RedisName].Get()
		defer qykredis.Close()
		if _, err := qykredis.Do("SELECT", FieldBd); err != nil {
			c.JSON(200, gin.H{"rep": false, "msg": "select redis出错"})
			return
		} else {
			if _, saveRedisErr := qykredis.Do("SET", name, sid); saveRedisErr != nil {
				c.JSON(200, gin.H{"rep": false, "msg": "保存redis出错"})
				return
			}
		}
		// escon := elastic.GetEsConn()
		// defer elastic.DestoryEsConn(escon)
		// _, err := escon.Index().Index(ElasticClientIndex).Type(ElasticClientType).Id(sid).BodyJson(e).Refresh(true).Do()
		// if err != nil {
		// 	c.JSON(200, gin.H{"rep": false, "msg": "更新es错误"})
		// 	return
		// }
	}
	//删除标记数据
	coll, _ := c.GetPostForm("coll")
	_id, _ := c.GetPostForm("_id")
	query := map[string]interface{}{
		"_id": qu.StringTOBsonId(_id),
	}
	b := Mgo.Del(coll, query)
	if !b {
		log.Println("Del Audit Data Error 	coll:", coll, "	field:", field, "	id:", _id)
	}
	c.JSON(200, gin.H{"rep": true, "msg": "保存成功"})
}
func BuyerClass(c *gin.Context) {
	data, _ := Mgo.Find("classify", `{"i_type":1}`, `{"_id":-1}`, `{"s_name":1,"child":1}`, false, -1, -1)
	c.JSON(200, gin.H{"data": data})
}

func AddSave(c *gin.Context) {
	field, _ := c.GetPostForm("field")
	auditname, _ := c.GetPostForm("auditname")
	parentclass, _ := c.GetPostForm("parentclass")
	childclass, _ := c.GetPostForm("childclass")
	update := make(map[string]interface{})
	if field == "buyer" {
		update["buyerclass"] = childclass
		update["parentclass"] = parentclass
	}
	update["name"] = auditname
	update["pici"] = time.Now().Unix()
	update["s_source"] = "手动"
	b := Mgo.Update(field, `{"name":"`+auditname+`"}`, map[string]interface{}{
		"$set": update}, true, false)
	if b { //新增的直接更新buyer redis
		p := redis.PutCKV(field, field+"_"+auditname, 1)
		log.Println("Add--Update "+field+" redis：", p)
	}
	c.JSON(200, gin.H{"rep": b})
}

func AuditSave(c *gin.Context) {
	field, _ := c.GetPostForm("field")
	coll, _ := c.GetPostForm("coll")
	name, _ := c.GetPostForm("val")
	_id, _ := c.GetPostForm("id")
	eid, _ := c.GetPostForm("eid")
	update := make(map[string]interface{})
	if field == "buyer" || field == "winner" { //更新buyer或winner库，并保存到redis
		if field == "buyer" {
			parentclass, _ := c.GetPostForm("parentclass")
			childclass, _ := c.GetPostForm("childclass")
			update["buyerclass"] = childclass
			update["parentclass"] = parentclass
		}
		update["name"] = name
		update["pici"] = time.Now().Unix()
		update["s_source"] = "抽取"
		//更新结果表数据
		b1 := Mgo.UpdateById(coll, eid, map[string]interface{}{
			"$set": map[string]interface{}{
				field: name,
			},
		})
		log.Println("update coll", coll, "id", eid, field, "after audit", b1)
		//更新原有的winner或buyer库
		b2 := Mgo.Update(field, `{"name":"`+name+`"}`, map[string]interface{}{
			"$set": update}, true, false)
		log.Println("Audit--Update ", field, "coll", b2)
		//更新redis
		b3 := redis.PutCKV(field, field+"_"+name, 1)
		log.Println("Audit--Update ", field, " redis：", b3)
		//删除audit_err数据
		b4 := Mgo.Del("audit_err", `{"_id":"`+_id+`"}`)
		log.Println("Del audit_err id", _id, b4)
		c.JSON(200, gin.H{"rep": b1})
	} else {
		b5 := Mgo.UpdateById(coll, eid, map[string]interface{}{
			"$set": map[string]interface{}{
				field: name,
			},
		})
		//删除audit_err数据
		b4 := Mgo.Del("audit_err", `{"_id":"`+_id+`"}`)
		log.Println("Del audit_err id", _id, b4)
		c.JSON(200, gin.H{"rep": b5})
	}
}
