package main

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

var pnreg = regexp.MustCompile("^(及编号[：:])|(项目|采购|招标|中标|成交|结果|[_]|公告)$")
var numreg = regexp.MustCompile("^[0-9]$")

/**
0 项目名称不是多包结构,直接按项目名称查找
1 项目名称多包,截去多包查找，再按不截取的查找
2 非招标项目名称含单个子包,截取多包查找，再搂
**/
func handleprojectname(pn, buyer, toptype string) (string, int) {
	for i := 0; i < 4; i++ {
		opn := pn
		pn = pnreg.ReplaceAllString(opn, "")
		if opn == pn {
			break
		}
	}
	//return pn
	if len([]rune(pn)) > lenprojectname || strings.Index(pn, buyer) > -1 || AreaGet.CheckSensitiveWord(pn) {
		return pn, 0
	} else {
		return buyer + pn, 0
	}
}

//对字段的优先级处理
type ExtractLevel struct {
	Strarr []string
}

func InitEL(v string) (e *ExtractLevel) {
	if v == "" {
		v = "0999999999"
		//第1位一下标为0，不用
	}
	Strarr := strings.Split(v, "")
	e = &ExtractLevel{Strarr}
	return
}

func (e *ExtractLevel) PutVal(field, val string) {
	e.Strarr[extractpos[field]] = val
}

func (e *ExtractLevel) GetVal() string {
	return strings.Join(e.Strarr, "")
}

//字段优先级处理
func (e *ExtractLevel) fieldpriority(info, project, set *map[string]interface{}) {
	defer util.Catch()
	for _k, v := range extractpos {
		breplace := false
		var _infov interface{}
		_infopos := fmt.Sprintf("%d", (*info)[_k+"_w"])
		if !numreg.MatchString(_infopos) {
			_infopos = "9"
		}
		if project == nil {
			breplace = true
			_infov = (*info)[_k]
		} else {
			ty := cextractpos[_k]
			switch ty {
			case 's':
				_v := util.ObjToString((*project)[_k])
				_tv := util.ObjToString((*info)[_k])
				_infov = _tv
				if _v == "" && _tv != "" {
					breplace = true
				} else if _tv != "" { //对比
					if e.Strarr[v] > _infopos {
						breplace = true
					}
				}
			case 'l':
				_v := util.Float64All((*project)[_k])
				_tv := util.Float64All((*info)[_k])
				_infov = _tv
				if _v == 0 && _tv > 0 {
					breplace = true
				} else if _tv > 0 {
					if e.Strarr[v] > _infopos {
						breplace = true
					}
				}

			}
		}
		if breplace {
			(*set)[_k] = _infov
			e.PutVal(_k, _infopos)
		}
	}
	otherfieldpriority(info, project, set)
}

//抽取字段
var cextractpos = map[string]rune{
	"projectname":  's',
	"projectcode":  's',
	"buyer":        's',
	"winner":       's',
	"budget":       'l',
	"bidamount":    'l',
	"bidstatus":    's',
	"agency":       's',
	"projectscope": 's',
}
var extractpos = map[string]int{
	"projectname":  0,
	"projectcode":  1,
	"buyer":        2,
	"winner":       3,
	"budget":       4,
	"bidamount":    5,
	"bidstatus":    6,
	"agency":       7,
	"projectscope": 8,
}

//合并字段
//"area","city","topscopeclass","subscopeclass","bidtype","zbtime","jgtime","buyerclass"
var bidtype = map[string]string{
	"招标": "招标",
	"邀标": "邀标",
	"询价": "询价",
	"竞谈": "竞谈",
	"单一": "单一",
	"竞价": "竞价",
}

func otherfieldpriority(info, project, set *map[string]interface{}) {
	area2 := util.ObjToString((*info)["area"])
	infoformat := util.IntAll((*info)["infoformat"])
	toptype := util.ObjToString((*info)["toptype"])
	topscopeclass := util.ObjToString((*info)["topscopeclass"])
	subscopeclass := util.ObjToString((*info)["subscopeclass"])
	bt := bidtype[util.ObjToString((*info)["subtype"])]
	if project == nil {
		if area2 == "" || area2 == "全国" {
			area2 = "A"
		}
		(*set)["area"] = area2
		(*set)["city"] = util.ObjToString((*info)["city"])
		(*set)["topscopeclass"] = topscopeclass
		(*set)["subscopeclass"] = subscopeclass
		if bt == "" {
			bt = "招标"
		}
		(*set)["bidtype"] = bt
		if infoformat == 2 {
			(*set)["njtime"] = (*info)["publishtime"]
		} else {
			if toptype == "招标" {
				(*set)["zbtime"] = (*info)["publishtime"]
			} else if toptype == "结果" {
				(*set)["jgtime"] = (*info)["publishtime"]
			}
		}
		if (*set)["zbtime"] == nil {
			(*set)["zbtime"] = (*info)["publishtime"]
		}
	} else {
		area := util.ObjToString((*project)["area"])
		if area == "A" && (area2 != "" && area2 != "A" && area2 != "全国") {
			(*set)["area"] = area2
		}
		publishtime := util.Int64All((*info)["publishtime"])
		if infoformat == 2 {
			//拟建信息，先不判断
			if publishtime > 0 {
				(*set)["njtime"] = publishtime
			}
		} else {
			//招标信息
			if toptype == "招标" {
				zbtime := util.Int64All((*project)["zbtime"])
				if publishtime > 0 && publishtime < zbtime {
					(*set)["zbtime"] = publishtime
				}
			} else if toptype == "结果" {
				jgtime := util.Int64All((*project)["jgtime"])
				if publishtime > 0 && publishtime > jgtime {
					(*set)["jgtime"] = publishtime
				}
			}
		}
		if subscopeclass != "" {
			(*set)["topscopeclass"] = topscopeclass
			(*set)["subscopeclass"] = subscopeclass
		}
		if bt != "" && bt != "招标" {
			(*set)["bidtype"] = bt
		}
	}
}
