// extpackage
package extract

import (
	"jy/clear"
	ju "jy/util"
	"log"
	qu "qfw/util"
	"reflect"
)

//处理分包信息
func PackageDetail(j *ju.Job, e *ExtractTask) {
	qu.Try(func() {
		if len(j.BlockPackage) > 0 {
			packageResult := map[string]map[string]interface{}{}
			//packagenum := len(j.BlockPackage)
			for pkName, pkg := range j.BlockPackage {
				//是否清理标记
				clearmap := map[string]bool{}
				sonJobResult := map[string]interface{}{}
				sonJobResult["text"] = pkg.Text
				sonJobResult["origin"] = pkg.Origin
				sonJobResult["type"] = pkg.Type
				sonJobResult["winnerorder"] = pkg.WinnerOrder
				//分包暂不参与选举
				/*
					for k, tags := range e.Tag {
					L:
						for _, tag := range tags {
							if pkg.TableKV != nil {
								for key, val := range pkg.TableKV.Kv {
									if tag.Key == key {
										clearmap[k] = false
										var tmpval interface{}
										if len(e.ClearFn[k]) > 0 {
											data := clear.DoClearFn(e.ClearFn[k], []interface{}{val, j.Content})
											tmpval = data[0]
										} else {
											tmpval = val
										}
										sonJobResult[k] = tmpval
										if packagenum == 1 {
											field := &ju.ExtField{
												Field:     k,
												Code:      "package",
												RuleText:  "package",
												Type:      "table",
												MatchType: "tag_string",
												ExtFrom:   "package",
												Value:     tmpval,
												Score:     0,
											}
											j.Result[k] = append(j.Result[k], field)
										}
										break L
									}
								}
							}
							if pkg.ColonKV != nil {
								for key, val := range pkg.ColonKV.Kv {
									if tag.Key == key {
										clearmap[k] = true
										var tmpval interface{}
										if len(e.ClearFn[k]) > 0 {
											data := clear.DoClearFn(e.ClearFn[k], []interface{}{val, j.Content})
											tmpval = data[0]
										} else {
											tmpval = val
										}
										sonJobResult[k] = tmpval
										if packagenum == 1 {
											field := &ju.ExtField{
												Field:     k,
												Code:      "package",
												RuleText:  "package",
												Type:      "colon",
												MatchType: "tag_string",
												ExtFrom:   "package",
												Value:     tmpval,
												Score:     0,
											}
											j.Result[k] = append(j.Result[k], field)
										}
										break L
									}
								}
							}
							if pkg.SpaceKV != nil {
								for key, val := range pkg.SpaceKV.Kv {
									if tag.Key == key {
										clearmap[k] = true
										var tmpval interface{}
										if len(e.ClearFn[k]) > 0 {
											data := clear.DoClearFn(e.ClearFn[k], []interface{}{val, j.Content})
											tmpval = data[0]
										} else {
											tmpval = val
										}
										sonJobResult[k] = tmpval
										if packagenum == 1 {
											field := &ju.ExtField{
												Field:     k,
												Code:      "package",
												RuleText:  "package",
												Type:      "space",
												MatchType: "tag_string",
												ExtFrom:   "package",
												Value:     tmpval,
												Score:     0,
											}
											j.Result[k] = append(j.Result[k], field)
										}
										break L
									}
								}
							}
						}
					}
				*/
				//如果有中标候选人排序，优先用第一中标候选人的中标单位和中标金额覆盖该包里面相应的字段的值
				if pkg.WinnerOrder != nil && len(pkg.WinnerOrder) > 0 {
					firstWinnerOrder := pkg.WinnerOrder[0]
					if qu.ObjToString(sonJobResult["winner"]) == "" || (!pkg.Accuracy && qu.ObjToString(firstWinnerOrder["entname"]) != "" && qu.Int64All(firstWinnerOrder["sort"]) == 1) {
						sonJobResult["winner"] = firstWinnerOrder["entname"]
					}
					if qu.Float64All(sonJobResult["bidamount"]) == 0 || (!pkg.Accuracy && qu.Float64All(firstWinnerOrder["price"]) > 0 && qu.Int64All(firstWinnerOrder["sort"]) == 1) {
						sonJobResult["bidamount"] = firstWinnerOrder["price"]
					}
				}
				//log.Println(pkName, sonJobResult)
				sonJobResult["clear"] = clearmap
				packageResult[pkName] = sonJobResult
			}
			if len(packageResult) > 0 {
				j.PackageInfo = packageResult
			}
		}
		extRegBackPack(j, e)
	}, func(err interface{}) {
		log.Println("PackageDetail err", err)
	})
}

//清理分包信息
func extRegBackPack(j *ju.Job, e *ExtractTask) {
	defer qu.Catch()
	//正则清理
	if j.CategorySecond == "" {
		for _, rc1 := range e.RuleCores[j.Category] {
			for _, rc := range rc1 {
				for pk, pack := range j.PackageInfo {
					clear, _ := pack["clear"].(map[string]interface{})
					for k, val := range pack {
						if b, ok := clear[k].(bool); ok && b {
							if rc.Field == k {
								text := qu.ObjToString(val)
								for _, in := range rc.RuleBacks {
									if text != "" && !in.IsLua {
										text = in.RegPreBac.Reg.ReplaceAllString(text, in.RegPreBac.Replace)
									}
								}
								pack[k] = text
							}
						}
					}
					j.PackageInfo[pk] = pack
				}
			}
		}
	} else {
		for _, rc1 := range e.RuleCores[j.Category+"_"+j.CategorySecond] {
			for _, rc := range rc1 {
				for pk, pack := range j.PackageInfo {
					clear, _ := pack["clear"].(map[string]interface{})
					for k, val := range pack {
						if b, ok := clear[k].(bool); ok && b {
							if rc.Field == k {
								text := qu.ObjToString(val)
								for _, in := range rc.RuleBacks {
									if text != "" && !in.IsLua {
										text = in.RegPreBac.Reg.ReplaceAllString(text, in.RegPreBac.Replace)
									}
								}
								pack[k] = text
							}
						}
					}
					j.PackageInfo[pk] = pack
				}
			}
		}
	}
	//通用正则清理
	for _, in := range e.RuleBacks {
		for _, pack := range j.PackageInfo {
			for k, val := range pack {
				if in.Field == k {
					text := qu.ObjToString(val)
					if text != "" && !in.IsLua {
						text = in.RegPreBac.Reg.ReplaceAllString(text, in.RegPreBac.Replace)
					}
					pack[k] = text
				}
			}
		}
	}
	//函数清理
	for _, pack := range j.PackageInfo {
		for key, val := range pack {
			if reflect.TypeOf(val) != nil && (reflect.TypeOf(val).String() == "float64" || reflect.TypeOf(val).String() == "int64") {
				continue
			} else {
				lock.Lock()
				cfn := e.ClearFn[key]
				lock.Unlock()
				data := clear.DoClearFn(cfn, []interface{}{val, j.Content})
				pack[key] = data[0]
			}
		}
	}

	//特殊属性的分包清理
	for _, rc := range e.PkgRuleCores {
		for pk, pack := range j.PackageInfo {
			for k, val := range pack {
				if rc.Field == k {
					text := qu.ObjToString(val)
					for _, in := range rc.RuleBacks {
						if text != "" {
							if !in.IsLua { //正则
								text = in.RegPreBac.Reg.ReplaceAllString(text, in.RegPreBac.Replace)
							} else { //lua
								result := GetResultMapForLua(j)
								lua := ju.LuaScript{Code: in.Code, Name: in.Name, Result: result, Script: in.RuleText}
								if j != nil {
									lua.Block = j.Block
								}
								extinfo := lua.RunScript("back")
								if extinfo["value"] != nil {
									text = qu.ObjToString(extinfo["value"])
								}
							}
						}
					}
					pack[k] = text
				}
			}
			j.PackageInfo[pk] = pack
		}
	}
}
