package main

import (
	"fmt"
	qu "qfw/util"
	"regexp"
	"strings"
)

var (
	fieldDict = map[string]interface{}{"agency":1,"buyer":1,"budget":1,"s_winner":1,"bidamount":1,"area":1,"city":1,"projectcode":1}
	fieldArr = []string{"agency","buyer","budget","s_winner","bidamount","area","city","projectcode"}
	temp_data = make(map[string]map[string]interface{},0)
	field_marked = make(map[string]int,0) //字段标注量
	field_right = make(map[string]int,0)  //字段正确量
	field_false = make(map[string]int,0)  //字段错误量
	remark_data = make(map[string]map[string]interface{},0)
	errReg *regexp.Regexp = regexp.MustCompile("^(err)")
)
//模板数据~加载
func initPrepare(){
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,total:=map[string]interface{}{
		"i_ckdata": map[string]interface{}{
			"$gt":0,
		},
	},0
	it := sess.DB(save_mgo.DbName).C("f_sourceinfo_extract_0525_moban").Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++{
		tmpid := BsonTOStringId(tmp["_id"])
		v_baseinfo := *qu.ObjToMap(tmp["v_baseinfo"])
		v_taginfo := *qu.ObjToMap(tmp["v_taginfo"])
		remark := qu.ObjToString(v_taginfo["reamrk"])
		if errReg.MatchString(remark) { //是否过滤异常标注信息
			tmp = make(map[string]interface{})
			continue

			remark_arr := strings.Split(remark,"~")
			d := map[string]interface{}{}
			for _,v := range remark_arr{
				if fieldDict[v]!=nil {
					d[v] = v
				}
			}
			remark_data[tmpid] = d
		}
		dict := map[string]interface{}{}
		for _,key := range fieldArr {
			if key=="budget" || key=="bidamount" {
				dict[key] = qu.Float64All(v_baseinfo[key])
			}else {
				dict[key] = qu.ObjToString(v_baseinfo[key])
			}
		}
		dict["subtype"] = v_baseinfo["subtype"]
		dict["toptype"] = v_baseinfo["toptype"]
		dict["is_ck"] = qu.IntAll(tmp["i_ckdata"])
		temp_data[tmpid] = dict
		tmp = make(map[string]interface{})
	}

	for _,v := range fieldArr {
		field_right[v] = 0
		field_false[v] = 0
		field_marked[v] = 0
	}

}

//数据字段准确率
func dealWithDataFieldAccuracy(coll_name string){
	//准备模板数据~
	initPrepare()
	//分析数据~逻辑
	sess := save_mgo.GetMgoConn()
	defer save_mgo.DestoryMongoConn(sess)
	q,isok:=map[string]interface{}{},0
	it := sess.DB(save_mgo.DbName).C(coll_name).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);{
		tmpid := BsonTOStringId(tmp["_id"])
		data :=  temp_data[tmpid]
		subtype := qu.ObjToString(tmp["subtype"])

		if qu.IntAll(data["is_ck"]) > 0 { //存在标注数据
			isok++
			for _,key := range fieldArr {
				is_continue:=!(subtype=="单一"||subtype==""||subtype=="中标"||subtype=="成交"||subtype=="合同") &&
					(key=="bidamount" || key=="s_winner")
				if is_continue {
					continue
				}
				remark := remark_data[tmpid]
				if remark[key] != nil {
					field_false[key] = field_false[key]+1
					field_marked[key] = field_marked[key]+1
					continue
				}
				if key=="bidamount" || key=="budget"  {
					if qu.Float64All(tmp[key]) == qu.Float64All(data[key]) {
						field_right[key] = field_right[key]+1
					}else {
						field_false[key] = field_false[key]+1
					}
				}else {
					if qu.ObjToString(tmp[key]) == qu.ObjToString(data[key]) {
						field_right[key] = field_right[key]+1
					}else {
						field_false[key] = field_false[key]+1
					}
				}
				field_marked[key] = field_marked[key]+1
			}
		}
		tmp = make(map[string]interface{})
	}

	arr0,arr1,arr2 :=[]int{},[]int{},[]int{}
	rate := []string{}
	for _,v := range fieldArr {
		arr0 = append(arr0,field_marked[v])
		arr1 = append(arr1,field_right[v])
		arr2 = append(arr2,field_false[v])

		rate = append(rate,fmt.Sprintf("%.4f",float64(field_right[v])/float64(field_marked[v])))
	}
	fmt.Println("字段标注名：",fieldArr)
	fmt.Println("字段标注量：",arr0)
	fmt.Println("字段正确量：",arr1)
	fmt.Println("字段错误量：",arr2)
	fmt.Println("字段准确率：",rate)
}
