package audit

import (
	. "jy/admin"
	. "jy/mongodbutil"
	"log"
	qu "qfw/util"
	redis "qfw/util/redis"
	"strconv"
	"time"

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

func init() {
	//列表、编辑
	Admin.GET("/audit/dataaudit", func(c *gin.Context) {
		name := c.Query("name")
		//s_coll := c.Query("s_coll")
		c.HTML(200, "audit_auditone.html", gin.H{"name": name})
	})
	Admin.POST("/audit/auditonefield", AuditOneField)
	Admin.POST("/audit/buyerclass", BuyerClass) //采购单位分类
	Admin.POST("/audit/addsave", AddSave)       //新增
	Admin.POST("/audit/auditsave", AuditSave)   //保存
}

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 AuditOneField(c *gin.Context) {
	field, _ := c.GetPostForm("field")
	limitstr, _ := c.GetPostForm("length")
	startstr, _ := c.GetPostForm("start")
	start, _ := strconv.Atoi(startstr)
	limit, _ := strconv.Atoi(limitstr)
	query := map[string]interface{}{
		"field":     field,
		"ispackage": false,
	}
	data, _ := Mgo.Find("audit_err", query, `{"_id":-1}`, nil, false, start, limit)
	for _, d := range *data {
		timeStr := time.Unix(d["l_createtime"].(int64), 0).Format(Date_Short_Layout)
		d["l_createtime"] = timeStr
	}
	count := Mgo.Count("audit_err", query)
	c.JSON(200, gin.H{"data": data, "recordsFiltered": count, "recordsTotal": count})
}

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})
	}
}
