package main

import (
	log "github.com/donnie4w/go-logger/logger"
	"github.com/go-ego/gse"
	qu "qfw/util"
	"regexp"
	"sensitive"
)

type PurInfo struct {
	name string
	code string
}

//词名称~所有信息
var PurClassInfo = map[string]PurInfo{}

//代码对应名称
var PurCodeInfo = map[string]string{}

//记录三大类的词组
var BasicClassNameInfo = map[string][]string{}

//记录三大类划分的类别代码
var BasicClassCodeInfo = map[string][]string{
	"货物": []string{"01", "02", "03", "04", "06", "07", "08", "09", "10", "11", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "4702"},
	"工程": []string{"48", "49", "50", "4701"},
	"服务": []string{"05", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97"},
}

//清洗
var CleanItemReg1 *regexp.Regexp = regexp.MustCompile("^(其他|无|服务|台|个|套)$")
var CleanItemReg2 *regexp.Regexp = regexp.MustCompile("(发布时间|序号|产品型号|注[:：]|请点击.*查看|m3|m³|m2|㎡|人工)")
var CleanItemReg3 *regexp.Regexp = regexp.MustCompile("[(（].*")
var CleanItemReg4 *regexp.Regexp = regexp.MustCompile("(合计)$")

//符合条件
var RulePreReg1 *regexp.Regexp = regexp.MustCompile("(费|费用)(】)?$")
var RulePreReg2 *regexp.Regexp = regexp.MustCompile("(维护|维修|保养|检修|维保|修复)")
var RulePreReg3 *regexp.Regexp = regexp.MustCompile("(费|费用|手册|修缮|工程|施工|装修)")
var RulePreReg4 *regexp.Regexp = regexp.MustCompile("(修缮|工程|改造|施工|房屋)")
var RulePreReg5 *regexp.Regexp = regexp.MustCompile("(服务)$")

//拆分
var SplitItemReg1 *regexp.Regexp = regexp.MustCompile("[(（](.*)[)）]")
var SplitItemReg2 *regexp.Regexp = regexp.MustCompile("[(（)）]")

const (
	SourceColl    = "zktest_lidaxin_info"
	InitialColl   = "zktest_code_purchasing_new"
	ReplenishColl = "zktest_code_purchasing_replenish"
)

//行业对应关系
var TopScopeClass = map[string]string{}

//筛选字段
var SelectFields = map[string]interface{}{
	"purchasinglist":  1,
	"s_topscopeclass": 1,
	"s_subscopeclass": 1,
	"toptype":         1,
	"href":            1,
	"site":            1,
	"title":           1,
	"projectname":     1,
	"detail":          1,
	"basicClass":      1,
	"buyerclass":      1,
	"buyer":           1,
}

//敏感词
var SensitiveInfo *sensitive.Filter

//切词
var Gse *gse.Segmenter = &gse.Segmenter{}

var PurLength = 100
var IsSaveLog = true

func initReadyInfo() {
	initPurchasingCode()
	initScopeCode()
	initSensitiveInfo()
}

//加载标的物的分类信息
func initPurchasingCode() {
	dataArr1, _ := data_mgo.Find(InitialColl, nil, map[string]interface{}{"_id": 1}, nil)
	for _, tmp := range dataArr1 {
		name := qu.ObjToString(tmp["name"])
		code := qu.ObjToString(tmp["code"])
		basic := qu.ObjToString(tmp["basic"])
		PurClassInfo[name] = PurInfo{name, code}
		PurCodeInfo[code] = name
		if basic != "" {
			basicArr := BasicClassNameInfo[basic]
			if basicArr == nil {
				basicArr = []string{}
			}
			basicArr = append(basicArr, name)
			BasicClassNameInfo[basic] = basicArr
		}
	}

	//补充模型
	dataArr2, _ := data_mgo.Find(ReplenishColl, nil, map[string]interface{}{"_id": 1}, nil)
	for _, tmp := range dataArr2 {
		new_key := qu.ObjToString(tmp["name"])
		code := qu.ObjToString(tmp["code"])
		name := PurCodeInfo[code]
		if name == "" || code == "" {
			log.Debug("异常~", new_key, "~", code, "~", tmp["type"])
			continue
		}
		PurClassInfo[new_key] = PurInfo{name, code}
		basic := qu.ObjToString(tmp["basic"])
		if basic != "" {
			basicArr := BasicClassNameInfo[basic]
			if basicArr == nil {
				basicArr = []string{}
			}
			basicArr = append(basicArr, new_key)
			BasicClassNameInfo[basic] = basicArr
		}
	}
	log.Debug("源数据~", len(dataArr1), "~", len(dataArr2))
	log.Debug("总计~", len(PurClassInfo), "~", len(PurCodeInfo), "~", len(BasicClassNameInfo))
}

func initScopeCode() {
	nameArr := []string{"行政办公", "建筑工程", "交通工程", "弱电安防", "市政设施", "水利水电", "信息技术", "医疗卫生", "服务采购", "能源化工", "机械设备", "农林牧渔", "行政办公_办公家具", "行政办公_通用办公设备", "行政办公_专业设备", "行政办公_办公用品", "行政办公_生活用品", "建筑工程_勘察设计", "建筑工程_工程施工", "建筑工程_材料准备", "建筑工程_机电安装", "建筑工程_监理咨询", "交通工程_道路", "交通工程_轨道", "交通工程_桥梁", "交通工程_隧道", "交通工程_其他", "弱电安防_综合布线", "弱电安防_智能系统", "弱电安防_智能家居", "市政设施_道路", "市政设施_绿化", "市政设施_线路管网", "市政设施_综合项目", "水利水电_水利工程", "水利水电_发电工程", "水利水电_航运工程", "水利水电_其他工程", "信息技术_运维服务", "信息技术_软件开发", "信息技术_系统集成及安全", "信息技术_其他", "医疗卫生_设备", "医疗卫生_耗材", "医疗卫生_药品", "服务采购_法律咨询", "服务采购_会计", "服务采购_物业", "服务采购_审计", "服务采购_安保", "服务采购_仓储物流", "服务采购_广告宣传印刷", "服务采购_其他", "能源化工_新能源", "能源化工_原材料", "能源化工_化工产品", "能源化工_仪器仪表", "能源化工_设备物资", "机械设备_矿山机械", "机械设备_工程机械", "机械设备_机械零部件", "机械设备_机床相关", "机械设备_车辆", "机械设备_其他机械设备", "农林牧渔_生产物资", "农林牧渔_生产设备", "农林牧渔_相关服务"}
	codeArr := []string{"4127", "47", "470102", "4899", "4702", "470102", "", "27", "", "", "36", "", "2101", "4127", "4127", "4127", "", "7607", "4701", "4702", "480301", "760707", "470102", "470102", "470102", "470102", "470102", "4899", "4899", "4899", "4702", "4702", "4702", "4702", "470102", "470102", "470102", "470102", "61", "62", "6101", "619999", "3643", "2708", "2706", "7404", "7405", "7202", "7405", "7417", "58", "89", "", "", "07", "26", "4102", "", "3601", "3603", "369902", "3610", "37", "369901", "", "3618", "05"}
	for k, v := range nameArr {
		if v != "" && codeArr[k] != "" {
			TopScopeClass[v] = codeArr[k]
		}
	}
	log.Debug("行业对应代码~", len(nameArr), "~", len(codeArr), "~", len(TopScopeClass))
}

//加载特殊词库信息
func initSensitiveInfo() {
	SensitiveInfo = sensitive.New()
	temp_2 := map[string]interface{}{"08": 1, "09": 1, "26": 1, "27": 1, "40": 1, "41": 1, "48": 1, "49": 1, "50": 1, "61": 1, "62": 1}
	temp_4 := map[string]interface{}{"3643": 1, "3644": 1, "3645": 1, "3646": 1, "4701": 1, "3654": 1, "4801": 1, "4802": 1, "8301": 1}
	dataArr, _ := data_mgo.Find(InitialColl, nil, nil, nil)
	total := 0
	for _, v := range dataArr {
		code := qu.ObjToString(v["code"])
		level := qu.IntAll(v["level"])
		name := qu.ObjToString(v["name"])
		if name == "" {
			continue
		}
		code_2, code_4 := "", ""
		code_2 = code[:2]
		if level > 1 {
			code_4 = code[:4]
		}
		if temp_2[code_2] != nil || temp_4[code_4] != nil {
			total++
			SensitiveInfo.AddWord(name)
		}
	}
	log.Debug("is sensitive over ~~~", total)
}
