package main

import (
	qu "qfw/util"
	"regexp"
	"sync"
	"time"
	"unicode/utf8"
)

var sitelock sync.Mutex         //锁
var error_reason map[string]interface{}
var abnormal_reason map[string]interface{}

func dealWithErrorRate(tmp map[string]interface{}) (int , int, map[string]interface{}, map[string]interface{}) {
	error_reason = map[string]interface{}{}
	abnormal_reason = map[string]interface{}{}
	//错误 ， 异常error_score  abnormal_score,
	err_num,ab_num:=0,0
	//金额类
	budget:=qu.Float64All(tmp["budget"])
	bidamount:=qu.Float64All(tmp["bidamount"])
	if tmp["budget"]==nil&&tmp["bidamount"]==nil {
		//均不存在
		ab_num++
		abnormal_reason["money"] = "budget-bidamount均不存在"
	}else if tmp["budget"]!=nil&&tmp["bidamount"]!=nil&&budget!=0&&bidamount!=0 {
		//均存在
		err,ab:=amountAnalysis(budget,bidamount)
		err_num = err_num+err
		ab_num = err_num+ab
	}else {
		//二者存在一个
		if budget==0 && qu.ObjToString(tmp["toptype"])=="招标"{
			ab_num++
			abnormal_reason["money"] = "招标：budget空"
		}
		if bidamount==0 && qu.ObjToString(tmp["toptype"])=="结果"{
			ab_num++
			abnormal_reason["money"] = "结果：bidamount空"
		}
	}

	//采购单位
	buyer:=qu.ObjToString(tmp["buyer"])
	if buyer=="" {
		ab_num++
		abnormal_reason["buyer"] = "buyer空"
	}else {
		if utf8.RuneCountInString(buyer)<4 {
			err_num++
			error_reason["buyer"] = "buyer长度"
		}else {
			if !buyerAnalysis(buyer) {
				ab_num++
				abnormal_reason["buyer"] = "buyer-企业无"
			}
		}
	}

	//中标单位
	if qu.ObjToString(tmp["toptype"])=="结果" {
		winner:=qu.ObjToString(tmp["winner"])
		if winner=="" {
			ab_num++
			abnormal_reason["winner"] = "winner空"
		}else {
			if utf8.RuneCountInString(winner)<4 {
				err_num++
				error_reason["winner"] = "winner长度"
			}else {
				if !winnerAnalysis(winner) {
					ab_num++
					abnormal_reason["winner"] = "winner-企业无"
				}
			}
		}
	}


	//标题，名称
	title:=qu.ObjToString(tmp["title"])
	if title=="" {
		err_num++
		error_reason["title"] = "空"
	}else {
		if utf8.RuneCountInString(title)<4 {
			ab_num++
			abnormal_reason["title"] = "title长度"
		}else {

		}
	}
	projectname:=qu.ObjToString(tmp["projectname"])
	if projectname=="" {
		err_num++
		error_reason["projectname"] = "空"
	}else {
		if utf8.RuneCountInString(projectname)<4 {
			ab_num++
			abnormal_reason["projectname"] = "projectname长度"
		}else {

		}
	}


	//编号组
	projectcode:=qu.ObjToString(tmp["projectcode"])
	contractnumber:=qu.ObjToString(tmp["contractnumber"])
	if projectcode==""&&contractnumber=="" {
		ab_num++
		abnormal_reason["code"] = "code-空"
	}else {
		if !codesAnalysis(projectcode,contractnumber) {
			ab_num++
			abnormal_reason["code"] = "code-不符"
		}
	}

	//发布时间
	publishtime:=qu.Int64All(tmp["publishtime"])
	now:=time.Now().Unix()
	if publishtime<=0||publishtime-now>0 {
		err_num++
		error_reason["publishtime"] = "publishtime-(超前)（0）"
	}

	//省份，城市
	area := qu.ObjToString(tmp["area"])
	if area == "A" {
		area = "全国"
	}
	city := qu.ObjToString(tmp["city"])
	site := qu.ObjToString(tmp["site"])
	if !citysAnalysis(area,city,site) {
		ab_num++
		abnormal_reason["city"] = "area-站点不一致"
	}

	//招标时间-地点
	if qu.Int64All(tmp["bidopentime"])==0 && qu.ObjToString(tmp["bidopenaddress"])=="" &&
		qu.ObjToString(tmp["toptype"])=="招标" {
		ab_num++
		abnormal_reason["bidopen"] = "bidopen-时间-地点-空"
	}



	//类别问题
	if qu.ObjToString(tmp["toptype"]) == ""{
		err_num++
		error_reason["toptype"] = "toptype:空"
	}else {
		if !categoryAnalysis(tmp) {
			ab_num++
			abnormal_reason["toptype"] = "toptype:内容>>"
		}
	}


	return err_num,ab_num,error_reason,abnormal_reason
}





//分析-金额
func amountAnalysis(budget float64,bidamount float64) (int ,int) {
	err_num,ab_num:=0,0
	proportion := bidamount/budget
	if proportion>=0.1&&proportion<=10 {

	}else if (proportion>=0.01&&proportion<0.1)||(proportion>10&&proportion<=100){
		ab_num++
		abnormal_reason["money"] = "bidamount/budget间隔异常"
	}else {
		err_num++
		error_reason["money"] = "bidamount/budget-比例错误"
	}

	return err_num,ab_num
}

//分析-采购单位
func buyerAnalysis(buyer string) bool{

	q := map[string]interface{}{
		"company_name": buyer,
	}
	data :=qy_mgo.FindOne(qy_coll_name,q)
	//data,_:=qy_mgo.Find(qy_coll_name,q,nil,map[string]interface{}{"company_name":1})
	if data==nil {
		return false
	}
	return true
}


//分析-中标单位
func winnerAnalysis(winner string) bool {

	q := map[string]interface{}{
		"company_name": winner,
	}
	data :=qy_mgo.FindOne(qy_coll_name,q)
	//data,_:=qy_mgo.Find(qy_coll_name,q,nil,map[string]interface{}{"company_name":1})
	if data==nil {
		return false
	}
	return true
}

//分析-编号组
func codesAnalysis(projectcode string,contractnumber string) bool {

	if projectcode!="" {
		if utf8.RuneCountInString(projectcode)<4 {
			return false
		}
		//符合-8长度-日期格式 yyyyMMdd
		if !regAnalysis(projectcode) &&utf8.RuneCountInString(projectcode)==8 {
			return false
		}
	}

	if contractnumber!="" {
		if utf8.RuneCountInString(contractnumber)<4 {
			return false
		}

		if !regAnalysis(contractnumber)  && utf8.RuneCountInString(projectcode)==8  {
			return false
		}
	}
	return true
}

//分析-省份，城市
func citysAnalysis(area string,city string,site string) bool {

	if site != "" {//站点对比
		sitelock.Lock()
		dict := siteMap[site]
		sitelock.Unlock()
		if dict != nil {
			if (area == "全国" && dict["area"] != "") ||
				(city == "" && dict["city"] != "") {
				return false
			}
		}
	}
	return true
}




//分析-类别
func categoryAnalysis(tmp map[string]interface{}) bool {
	toptype:=qu.ObjToString(tmp["toptype"])
	if toptype=="招标" {
		winner:=qu.ObjToString(tmp["winner"])
		bidamount:=qu.Float64All(tmp["bidamount"])
		if winner!=""||bidamount!=0 {
			//结果超前
			return false
		}
	}
	return true
}













func regAnalysis(str string) bool {
	reg:=`\d{8}`
	regx,_ := regexp.Compile(reg)
	if regx.FindString(str)!="" {
		return false
	}
	return true
}













