// thisinfo
package main

import (
	"crypto/sha1"
	"encoding/json"
	"fmt"
	"io"
	du "jy/util"
	"qfw/util"
	"strings"

	"gopkg.in/mgo.v2/bson"
)

//抽取信息映射实体类
type Info struct {
	Id            string                 `json:"_id"`
	Href          string                 `json:"href"`
	Publishtime   int64                  `json:"publishtime"`
	Title         string                 `json:"title"`
	TopType       string                 `json:"toptype"`
	SubType       string                 `json:"subtype"`
	ProjectName   string                 `json:"projectname"`
	ProjectCode   string                 `json:"projectcode"`
	Buyer         string                 `json:"buyer"`
	Buyerperson   string                 `json:"buyerperson"`
	Buyertel      string                 `json:"buyertel"`
	Agency        string                 `json:"agency"`
	Area          string                 `json:"area"`
	City          string                 `json:"city"`
	HasPackage    bool                   `json:"haspackage"`
	Package       map[string]interface{} `json:"package"`
	PNum          string                 `json:"pnum"`
	Topscopeclass []string               `json:"topscopeclass"`
	Subscopeclass []string               `json:"subscopeclass"`
	Winners       []string
	dealtype      int

	Buyerclass  string `json:"buyerclass"`
	Bidopentime int64  `json:"bidopentime"`
	District    string `json:"district"`
	Winnerorder []string

	NewPNKey string
	PNKey    string
	PCKey    string
	PBKey    string

	MererInc int //增量合并标记 0三选二合并 1三选一合并 2孤立核查项目 3无效信息
	Data     map[string]interface{}
}

//pcb三选值
type PCBV struct {
	Val    int  //1一项有效值,2二项有效值,3三项有效值
	Pname  bool //有项目名称
	Pcode  bool //有项目编号
	Buyer  bool //有采购单位
	Area   bool //区域
	City   bool //市
	Agency bool //代理机构

	PnameLen int //值长度
	PcodeLen int //值长度
	BuyerLen int //值长度
}

func PreThisInfo(tmp map[string]interface{} /*新信息*/) *Info {
	bys, _ := json.Marshal(tmp)
	var thisinfo *Info
	json.Unmarshal(bys, &thisinfo)
	if thisinfo == nil {
		return nil
	}
	if len(thisinfo.Topscopeclass) == 0 {
		thisinfo.Topscopeclass = []string{}
	}
	if len(thisinfo.Subscopeclass) == 0 {
		thisinfo.Subscopeclass = []string{}
	}
	//去重
	thisinfo.Subscopeclass = RemoveDup(thisinfo.Subscopeclass)

	if len(thisinfo.Package) > 0 { //信息是否分包
		thisinfo.HasPackage = true
	} else if thisinfo.TopType == "结果" && TitleReg.MatchString(thisinfo.Title) {
		//当信息类型是结果时，并且标题中包含分包字样，找到包号，用以后面比较打分
		res := TitleReg.FindAllStringSubmatch(thisinfo.Title, -1)
		pnum := du.PackageNumberConvert(res[0][0])
		//du.Debug(pnum, res)
		thisinfo.PNum = pnum
	}
	if thisinfo.SubType == "变更" || strings.Index(thisinfo.Title, "变更公告") > -1 || strings.Index(thisinfo.Title, "更正公告") > -1 {
		//当信息类型是变更或标题中含变更时
		if thisinfo.TopType == "招标" {
			//招标的变更公告过，不作处理
			//du.Debug(thisinfo.Id, thisinfo.Href, thisinfo.ProjectName, thisinfo.ProjectCode)
			return nil
		} else if thisinfo.TopType == "结果" {
			thisinfo.SubType = "变更"
		}
	}

	//计算中标人
	winner, _ := tmp["winner"].(string)
	m1 := map[string]bool{}
	winners := []string{}
	if winner != "" {
		m1[winner] = true
		winners = append(winners, winner)
	}
	if thisinfo.HasPackage {
		packageM, _ := tmp["package"].(bson.M)
		for _, p := range packageM {
			pm, _ := p.(map[string]interface{})
			pw, _ := pm["winner"].(string)
			if pw != "" {
				m1[pw] = true
				winners = append(winners, pw)
			}
		}
	}
	thisinfo.Winners = winners
	m1 = nil
	//中标候选人
	winnerorder := []string{}
	if winorders, ok := tmp["winnerorder"].([]interface{}); ok {
		for _, wins := range winorders {
			if win, ok := wins.(map[string]interface{}); ok {
				entname := util.ObjToString(win["entname"])
				if entname != "" && len([]rune(entname)) > 6 {
					winnerorder = append(winnerorder, entname)
				}
			}
		}
	}
	thisinfo.Winnerorder = winnerorder

	new_pn, _ := handleprojectname(thisinfo.ProjectName, thisinfo.Buyer, thisinfo.TopType) //作为判断依据
	thisinfo.NewPNKey = new_pn
	thisinfo.PNKey = "pn_" + new_pn
	thisinfo.PCKey = "pc_" + thisinfo.ProjectCode
	thisinfo.PBKey = "pb_" + thisinfo.Buyer
	return thisinfo
}

//获取hascode
func GetHas1(key string) string {
	t := sha1.New()
	io.WriteString(t, key)
	return fmt.Sprintf("%x", t.Sum(nil))
}
