// bidamount,中标金额清洗
package service

import (
	"math"
	qu "qfw/util"
)

//返回
func (d *ResultInfo) BidAmount(data map[string]interface{}) {
	//log.Println("执行：BidAmout")
	if d.Extract == nil {
		d.Extract = &ExtractInfo{}
	}
	state := false //值未发生改变
	//todo 清洗操作
	bidamount_tab := make(map[string]interface{}) //记录每个流程的判断情况 0:无法判断;1:不满足要求;2:满足要求
	bidamount_by := ""                            //val：file通过字段修正;val：logic通过逻辑判断修正
	_id := qu.BsonIdToSId(data["_id"])
	bidamount, _ := data["bidamount"].(float64)           //中标金额
	result := bidamount                                   //结果
	budget, _ := data["budget"].(float64)                 //预算
	supervisorrate, _ := data["supervisorrate"].(float64) //费率
	//1.注册资本
	b1, n1 := ClearByRegisterCapital(bidamount, 0, 20.0)
	bidamount_tab["fund"] = n1
	//2.与预算的比例
	b2, n2 := ClearByBudretProportion(budget, bidamount)
	bidamount_tab["ratio"] = n2
	//3.中标金额费率
	b3, n3, val3 := ClearBySupervisorrate(budget, bidamount, supervisorrate)
	bidamount_tab["rate"] = n3

	if !b1 && !b2 && !b3 { //均不满足要求，更改中标金额 false:1 false:0
		if val3 > 0 { //首先应用费率算出的值
			result = val3
			bidamount_by = "file"
		} else if n1 == 1 || n2 == 1 {
			result = 0
			bidamount_by = "logic"
		}

		if bidamount != result { //排除抽取数据为空的情况
			if result == 0 {
				data["bidamount"] = ""
			} else {
				data["bidamount"] = result
			}
			data["bidamount_by"] = bidamount_by
			data["bidamount_tab"] = bidamount_tab
			if bidamount == 0 {
				data["bidamount_back"] = ""
			} else {
				data["bidamount_back"] = bidamount
			}
			//qu.Debug("bidamount---", data["bidamount"], "bidamount_back---", data["bidamount_back"])
			d.Extract.Data = data
			d.Extract.Id = _id
			state = true
		}
	}

	if state {
		d.Flag++
		d.Extract.UpParam = append(d.Extract.UpParam, "bidamount")      //更新属性
		d.Extract.UpParam = append(d.Extract.UpParam, "bidamount_by")   //更新属性
		d.Extract.UpParam = append(d.Extract.UpParam, "bidamount_tab")  //更新属性
		d.Extract.UpParam = append(d.Extract.UpParam, "bidamount_back") //更新属性
	}
}

func ClearByRegisterCapital(bidamount, fund float64, thresholdVal float64) (bool, int) { //m为注册资本，thresholdVal为阈值
	if fund == 0 || bidamount == 0 {
		return false, 0
	}
	if bidamount <= fund*thresholdVal { //中标金额满足20倍的阈值
		return true, 2
	} else {
		return false, 1
	}
}
func ClearByBudretProportion(budget, bidamount float64) (bool, int) {
	if budget == 0 || bidamount == 0 {
		return false, 0
	}
	if bidamount <= budget*1.1 && bidamount >= budget*0.7 { //数据在范围内
		return true, 2
	} else {
		return false, 1
	}
}
func ClearBySupervisorrate(budget, bidamount, supervisorrate float64) (bool, int, float64) {
	if budget == 0 || supervisorrate == 0 {
		return false, 0, 0
	}
	tb := budget * math.Pow10(6)
	ts := supervisorrate * math.Pow10(6)
	tmpBidAmount := tb * ts / math.Pow10(12) //预算*费率
	if bidamount == tmpBidAmount {
		return true, 2, bidamount
	} else {
		return false, 1, tmpBidAmount
	}
}
