1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021 |
- package util
- import (
- "JySEPlatform/models"
- sql "JySEPlatform/sqlmodel"
- "app.yhyue.com/moapp/jybase/mongodb"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "log"
- "strings"
- "sync"
- "time"
- qu "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/encrypt"
- elastic "app.yhyue.com/moapp/jybase/es"
- es "github.com/olivere/elastic/v7"
- "go.mongodb.org/mongo-driver/bson"
- )
- const (
- TitleMatchType = "1" //标题匹配
- DetailMatchType = "2" //全文匹配
- PurchasingMatchType = "3" //标的物匹配
- AttachmentsMatchType = "4" //附件匹配
- ProjectnameMatchType = "5" //项目名称匹配
- BuyerMatchType = "6" //采购单位匹配
- SWinnerMatchType = "7" //中标单位匹配
- BuyerMatchType1 = "8" //采购单位(正则)匹配
- SWinnerMatchType1 = "9" //中标单位(正则)匹配
- TitleMatchTypeStr = "title" //标题匹配
- DetailMatchTypeStr = "detail" //全文匹配
- PurchasingMatchTypeStr = "purchasing" //标的物匹配
- AttachmentsMatchTypeStr = "filetext" //附件匹配
- ProjectnameMatchTypeStr = "projectname.pname" //项目名称匹配
- BuyerMatchTypeStr = "buyer" //项目名称匹配
- BuyerRegexpMatchTypeStr = "buyer.mbuyer" //采购单位正则匹配
- SWinnerMatchTypeStr = "s_winner" //项目名称匹配
- SWinnerRegexpMatchTypeStr = "s_winner.mwinner" //中标单位正则匹配
- TitleMatchTypeField = "title" //标题匹配
- DetailMatchTypeField = "detail" //全文匹配
- PurchasingMatchTypeField = "purchasing" //标的物匹配
- AttachmentsMatchTypeField = "filetext" //附件匹配
- ProjectnameMatchTypeField = "projectname.pname" //项目名称匹配
- BuyerMatchTypeField = "buyer" //项目名称匹配
- SWinnerMatchTypeField = "s_winner" //项目名称匹配
- MultiMatchType = "phrase"
- Standard = 1 //1:标准字段包,
- Advanced = 2 //2:高级字段包
- Standardstr = "standard" //1:标准字段包
- Advancedstr = "advanced" //2:高级字段包
- Url = "https://www.jianyu360.com/article/content/%s.html"
- )
- func UtilEsSaveData(sdataid string, datas *[]map[string]interface{}) error {
- defer qu.Catch()
- if !Mgo.Del("tagsdata", bson.M{"s_dataid": sdataid}) {
- return errors.New("mongo del err")
- }
- for _, v := range *datas {
- Mgo.Save("tagsdata", v)
- }
- // if Mgo.SaveBulk("tagsdata", *datas...) {
- // return nil
- // } else {
- // return errors.New("mongo save err")
- // }
- return nil
- }
- type MySource struct {
- Querys string
- }
- func (m *MySource) Source() (interface{}, error) {
- mp := make(map[string]interface{})
- json.Unmarshal([]byte(m.Querys), &mp)
- return mp["query"], nil
- }
- // 客户规则
- func UtilEsFind1(tags map[string]interface{}) (error, int64) {
- defer qu.Catch()
- ch := make(chan bool, 20)
- wg := &sync.WaitGroup{}
- arrsync := &sync.RWMutex{}
- sdataid := qu.ObjToString(tags["s_dataid"])
- esquery := qu.ObjToString(tags["s_esquery"])
- if IsNewSql != 0 {
- esquery = qu.ObjToString(tags["s_esquery_search"])
- }
- clearKey := qu.ObjToString(tags["s_globalclearkey"])
- clearKeyMatch := qu.ObjToString(tags["s_globalclearkeymatch"])
- if len(esquery) < 1 || len(sdataid) < 1 {
- return errors.New("s_esquery or s_dataid no found"), 0
- }
- i_maxnum := qu.Int64All(tags["i_maxnum"])
- if i_maxnum <= 0 {
- i_maxnum = 100
- }
- //
- maths := make([]map[string]string, 0)
- if orules, ok := tags["o_rules"].([]interface{}); ok {
- for _, v := range orules {
- orule, _ := v.(map[string]interface{})
- maths = append(maths, map[string]string{
- "s_matchkey": qu.ObjToString(orule["s_matchkey"]),
- "s_keymatch": qu.ObjToString(orule["s_keymatch"]),
- })
- }
- } else if orules, ok := tags["o_rules"].([]map[string]interface{}); ok {
- for _, v := range orules {
- maths = append(maths, map[string]string{
- "s_matchkey": qu.ObjToString(v["s_matchkey"]),
- "s_keymatch": qu.ObjToString(v["s_keymatch"]),
- })
- }
- } else {
- return errors.New("o_rules no found"), 0
- }
- ctx, _ := context.WithTimeout(context.Background(), 1*time.Hour)
- esCon := elastic.VarEs.(*elastic.EsV7)
- client := esCon.GetEsConn()
- defer esCon.DestoryEsConn(client)
- // esquery = esquery[:len(esquery)-1] + `,"size":` + fmt.Sprintf("%d", i_maxnum) + `}`
- cc := &MySource{
- Querys: esquery,
- }
- searchResult, err := client.Search(EsIndex).Query(cc).Size(int(i_maxnum)).Do(ctx)
- if err == nil && searchResult.Hits != nil {
- datas := make([]map[string]interface{}, 0)
- log.Println("es查询到的数量:", searchResult.Hits.TotalHits.Value)
- log.Println("es查询到的数量(实际):", len(searchResult.Hits.Hits))
- for _, v := range searchResult.Hits.Hits {
- wg.Add(1)
- ch <- true
- go func(tmpHit *es.SearchHit) {
- defer func() {
- <-ch
- arrsync.Unlock()
- wg.Done()
- }()
- arrsync.Lock()
- item := make(map[string]interface{})
- if json.Unmarshal(tmpHit.Source, &item) == nil {
- item["appid"] = tags["appid"]
- item["rulename"] = tags["s_name"]
- info_id := qu.ObjToString(item["id"])
- item["info_id"] = info_id
- item["s_dataid"] = sdataid
- // item["s_jyhref"] = fmt.Sprintf(Url, util.CommonEncodeArticle("content", info_id))
- //
- subType := qu.ObjToString(item["subtype"])
- subTypeStr := "拟建,采购意向,预告,预审,预审结果,论证意见,需求公示"
- if strings.Contains(subTypeStr, subType) {
- item["s_jyhref"] = `https://www.jianyu360.cn/article/bdprivate/` + encrypt.CommonEncodeArticle("bdprivate", info_id) + `.html`
- } else {
- // item["s_jyhref"] = `https://www.jianyu360.cn/article/content/` + qu.CommonEncodeArticle("content", info_id) + `.html`
- item["s_jyhref"] = `https://www.jianyu360.cn/article/entservice/` + encrypt.SE3.EncodeString(info_id) + `.html?appid`
- }
- //
- item["i_createtime"] = time.Now().Unix()
- delete(item, "_id")
- //清理词清空
- for _, v := range strings.Split(clearKey, ",") {
- ClearKeys(clearKeyMatch, v, item)
- }
- //二次匹配关键词
- matchKeys := GetMactchKey(maths, item)
- if len(maths) > 0 {
- if matchKeys != "" {
- item["s_matchkey"] = matchKeys
- }
- }
- findwinner := strings.TrimSpace(qu.ObjToString(item["winner"]))
- if findwinner != "" {
- finddata, _ := MgoEnps.FindOne(EnpsColl, bson.M{"company_name": findwinner})
- if finddata != nil {
- if legal_person := qu.ObjToString((*finddata)["legal_person"]); legal_person != "" {
- item["legal_person"] = legal_person
- }
- if email := qu.ObjToString((*finddata)["company_email"]); email != "" {
- item["company_email"] = email
- }
- if phone := qu.ObjToString((*finddata)["company_phone"]); phone != "" {
- item["company_phone"] = phone
- }
- item["qyk"] = finddata
- if company_area := qu.ObjToString((*finddata)["company_area"]); company_area != "" {
- item["company_area"] = company_area
- }
- if company_city := qu.ObjToString((*finddata)["company_city"]); company_city != "" {
- item["company_city"] = company_city
- }
- if company_district := qu.ObjToString((*finddata)["company_district"]); company_district != "" {
- item["company_district"] = company_district
- }
- //中标单位的社会征信代码
- if credit_no := qu.ObjToString((*finddata)["credit_no"]); credit_no != "" {
- item["winner_credit_no"] = credit_no
- }
- }
- }
- agency := strings.TrimSpace(qu.ObjToString(item["agency"]))
- if agency != "" {
- finddata, _ := MgoEnps.FindOne(EnpsColl, bson.M{"company_name": agency})
- if finddata != nil {
- //社会征信代码
- if credit_no := qu.ObjToString((*finddata)["credit_no"]); credit_no != "" {
- item["agency_credit_no"] = credit_no
- }
- }
- }
- buyer := strings.TrimSpace(qu.ObjToString(item["buyer"]))
- if buyer != "" {
- finddata, _ := MgoBuyer.FindOne(BuyerEntColl, bson.M{"company_name": buyer})
- if finddata != nil {
- //社会征信代码
- if credit_no := qu.ObjToString((*finddata)["credit_no"]); credit_no != "" {
- item["buyer_credit_no"] = credit_no
- }
- }
- }
- //匹配公告附件
- info, _ := MgoBidding.FindOne("bidding", bson.M{"_id": mongodb.StringTOBsonId(info_id)})
- if info != nil {
- log.Println("匹配附件, id-", item["info_id"])
- if (*info)["projectinfo"] != nil {
- projectInfo := qu.ObjToMap((*info)["projectinfo"])
- if projectInfo != nil && len(*projectInfo) > 0 {
- if (*projectInfo)["attachments"] != nil {
- filesArr := []map[string]interface{}{}
- fileUrl := qu.ObjToString(Sysconfig["file_downloadurl"])
- if attachments, oks := (*projectInfo)["attachments"].(map[string]interface{}); oks {
- for _, v := range attachments {
- if atta, ok := v.(map[string]interface{}); ok {
- if atta["fid"] != nil && qu.ObjToString(atta["fid"]) != "" {
- fileMap := map[string]interface{}{
- "filename": atta["filename"],
- "url": fileUrl + qu.ObjToString(atta["fid"]),
- }
- filesArr = append(filesArr, fileMap)
- } else {
- if atta["org_url"] != nil {
- fileMap := map[string]interface{}{
- "filename": atta["filename"],
- "url": qu.ObjToString(atta["org_url"]),
- }
- filesArr = append(filesArr, fileMap)
- }
- }
- }
- }
- }
- log.Println("查询附件结果:", info_id, "附件数量:", len(filesArr))
- if len(filesArr) > 0 {
- item["filehref"] = filesArr
- }
- }
- }
- }
- } else {
- log.Println("id", item["info_id"], "bidding为空")
- }
- //项目id
- //projectId := GetProjectId(info_id)
- //if projectId != "" {
- // item["projectId"] = SE2.EncodeString(projectId)
- //}
- datas = append(datas, item)
- } else {
- log.Println("id", item["_id"], "信息为空")
- }
- }(v)
- }
- wg.Wait()
- GetProjectId(&datas)
- count := Es.Count(EsIndex, EsType, esquery)
- Mgo.Update("entniche_rule", bson.M{"_id": tags["_id"]}, bson.M{
- "$set": bson.M{
- "i_estotal": count,
- }}, false, false)
- return UtilEsSaveData(sdataid, &datas), count
- } else {
- log.Println(err)
- return err, 0
- }
- }
- func ClearKeys(types, key string, data map[string]interface{}) {
- if strings.Contains(types, "1") {
- title := qu.ObjToString(data["title"])
- data["title"] = strings.ReplaceAll(title, key, "")
- }
- if strings.Contains(types, "2") {
- detail := qu.ObjToString(data["detail"])
- data["detail"] = strings.ReplaceAll(detail, key, "")
- }
- if strings.Contains(types, "3") {
- purchasing := qu.ObjToString(data["purchasing"])
- data["purchasing"] = strings.ReplaceAll(purchasing, key, "")
- }
- if strings.Contains(types, "4") {
- filetext := qu.ObjToString(data["filetext"])
- data["filetext"] = strings.ReplaceAll(filetext, key, "")
- }
- if strings.Contains(types, "5") {
- projectname := qu.ObjToString(data["projectname"])
- data["projectname"] = strings.ReplaceAll(projectname, key, "")
- }
- if strings.Contains(types, "6") || strings.Contains(types, "8") {
- buyer := qu.ObjToString(data["buyer"])
- data["buyer"] = strings.ReplaceAll(buyer, key, "")
- }
- if strings.Contains(types, "7") || strings.Contains(types, "9") {
- winner := qu.ObjToString(data["s_winner"])
- data["s_winner"] = strings.ReplaceAll(winner, key, "")
- }
- }
- func GetTimeInterval(startTime, entTime time.Time, n int) []map[string]interface{} {
- timeList := []map[string]interface{}{}
- endTimeTmp := startTime
- for endTimeTmp.Unix() < entTime.Unix() {
- var endTime_ time.Time
- startTime = endTimeTmp
- endTimeTmp = startTime.AddDate(0, n, 0)
- if endTimeTmp.Unix() > entTime.Unix() {
- endTime_ = entTime
- timeList = append(timeList, map[string]interface{}{"startTime": startTime.Unix(), "endTime": endTime_.Unix()})
- break
- } else {
- endTime_ = endTimeTmp
- timeList = append(timeList, map[string]interface{}{"startTime": startTime.Unix(), "endTime": endTime_.Unix()})
- }
- }
- //log.Println(timeList)
- return timeList
- }
- func Utiltags(tag map[string]interface{}) string {
- defer qu.Catch()
- tmpbyte, _ := json.Marshal(tag)
- tab := models.Tag{}
- err := json.Unmarshal(tmpbyte, &tab)
- if err != nil {
- return "json err:" + err.Error()
- }
- //log.Println("前端数据", tab)
- QueryObjecct := sql.QueryObjecct{}
- ffBoolObject := sql.BoolObject{}
- adsBoolObect := sql.NewEsObject{}
- if tab.Sarea != "" {
- adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{Area: strings.Split(tab.Sarea, ",")}})
- }
- if tab.Scity != "" {
- adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{City: strings.Split(tab.Scity, ",")}})
- }
- if tab.Sdistrict != "" {
- //城市——区县
- cityds := strings.Split(tab.Sdistrict, ",")
- var ds []string
- for _, v := range cityds {
- ds = append(ds, strings.Split(v, "-")[1])
- }
- adsBoolObect.Bool.Should = append(adsBoolObect.Bool.Should, sql.AreaCityDistrictMust{AreaCityDistrict: &sql.AreaCityDistrict{District: ds}})
- }
- if len(adsBoolObect.Bool.Should) > 0 {
- ffBoolObject.Must = append(ffBoolObject.Must, adsBoolObect)
- }
- if tab.Stoptype != "" || tab.Ssubtype != "" {
- if len(tab.Stoptype) > 0 {
- toptypeSubtype := sql.ToptypeSubtype{}
- toptypeSubtype.Toptype = strings.Split(tab.Stoptype, ",")
- ffBoolObject.Must = append(ffBoolObject.Must, sql.ToptypeSubtypeMust{&toptypeSubtype})
- }
- if len(tab.Ssubtype) > 0 {
- toptypeSubtype := sql.ToptypeSubtype{}
- toptypeSubtype.Subtype = strings.Split(tab.Ssubtype, ",")
- ffBoolObject.Must = append(ffBoolObject.Must, sql.ToptypeSubtypeMust{&toptypeSubtype})
- }
- }
- if tab.Istarttime > 0 && tab.Iendtime > 0 {
- ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime: &sql.Publishtime{
- Gte: tab.Istarttime,
- Lt: tab.Iendtime,
- }}})
- } else if tab.Istarttime > 0 {
- ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime1: &sql.Publishtime1{
- Gte: tab.Istarttime,
- }}})
- } else if tab.Iendtime > 0 {
- ffBoolObject.Must = append(ffBoolObject.Must, sql.PublishtimeMust{PublishtimeObject: &sql.PublishtimeObject{Publishtime2: &sql.Publishtime2{
- Lt: tab.Iendtime,
- }}})
- }
- if tab.Sglobalbuyerclass != "" {
- if len(tab.Sglobalbuyerclass) > 0 {
- buyerclass := sql.BuyerclassObject{
- Terms: struct {
- Buyerclass []string `json:"buyerclass,omitempty"`
- }{
- Buyerclass: strings.Split(tab.Sglobalbuyerclass, ","),
- },
- }
- ffBoolObject.Must = append(ffBoolObject.Must, buyerclass)
- }
- }
- if tab.Sglobaltopscopeclass != "" || tab.Sglobalsubscopeclass != "" {
- if len(tab.Sglobaltopscopeclass) > 0 {
- topScopeclass := sql.Scopeclass{}
- topScopeclass.Globaltopscopeclass = strings.Split(tab.Sglobaltopscopeclass, ",")
- ffBoolObject.Must = append(ffBoolObject.Must, sql.ScopeclassMust{&topScopeclass})
- } else if len(tab.Sglobalsubscopeclass) > 0 {
- subScopeclass := sql.Scopeclass{}
- subScopeclass.Globalsubscopeclass = strings.Split(tab.Sglobalsubscopeclass, ",")
- ffBoolObject.Must = append(ffBoolObject.Must, sql.ScopeclassMust{&subScopeclass})
- }
- }
- fqBoolObject := sql.BoolObject{}
- if len(tab.Sexistfields) > 0 {
- tmpsfields := strings.Split(tab.Sexistfields, ",")
- for _, v := range tmpsfields {
- ffBoolObject.Must = append(ffBoolObject.Must, sql.ExistfieldsObjectMust{
- ExistfieldsObject: &sql.ExistfieldsObject{Filter: struct {
- Exists struct {
- Field string `json:"field,omitempty"`
- } `json:"exists,omitempty"`
- }{
- Exists: struct {
- Field string `json:"field,omitempty"`
- }{Field: v},
- },
- },
- })
- }
- }
- newEsObject := sql.NewEsObject{}
- if tab.Sglobaladdkey != "" && tab.Sglobaladdkeymatch != "" {
- if tmps := strsToArr(tab.Sglobaladdkeymatch, "str"); tmps != nil {
- newEsObject = method1(newEsObject, tab.Sglobaladdkey, tab.Sglobaladdkeymatch, tmps)
- }
- }
- if len(newEsObject.Bool.Should) > 0 || len(newEsObject.Bool.MustNot) > 0 || len(newEsObject.Bool.Must) > 0 {
- fqBoolObject.Must = append(fqBoolObject.Must, newEsObject)
- }
- nots := sql.NewEsObject{}
- if tab.Sglobalnotkey != "" && tab.Sglobalnotkeymatch != "" {
- if tmps := strsToArr(tab.Sglobalnotkeymatch, "str"); tmps != nil {
- nots = method1(nots, tab.Sglobalnotkey, tab.Sglobalnotkeymatch, tmps)
- }
- }
- if len(nots.Bool.Should) > 0 {
- fqBoolObject.MustNot = append(fqBoolObject.MustNot, nots)
- }
- torules := sql.BoolObject{}
- for _, v := range tab.Orules {
- tmpses := sql.NewEsObject{}
- if skeymatchs := strsToArr(v.Skeymatch, "str"); skeymatchs != nil {
- tmpnewEsObject := sql.NewEsObject{}
- tmpnewEsObject = method1(tmpnewEsObject, v.Smatchkey, v.Skeymatch, skeymatchs)
- if len(tmpnewEsObject.Bool.Should) > 0 || len(tmpnewEsObject.Bool.Must) > 0 || len(tmpnewEsObject.Bool.MustNot) > 0 {
- tmpses.Bool.Must = append(tmpses.Bool.Must, tmpnewEsObject)
- }
- }
- if saddkeymatchs := strsToArr(v.Saddkeymatch, "str"); saddkeymatchs != nil {
- addkeyarr := sql.NewEsObject{}
- addkeyarr = method1(addkeyarr, v.Saddkey, v.Saddkeymatch, saddkeymatchs)
- if len(addkeyarr.Bool.Should) > 0 {
- tmpses.Bool.Must = append(tmpses.Bool.Must, addkeyarr)
- }
- }
- if snotkeymatchs := strsToArr(v.Snotkeymatch, "str"); snotkeymatchs != nil {
- tmpses = method2(tmpses, v.Snotkey, v.Snotkeymatch, snotkeymatchs)
- }
- if len(v.Stopscopeclass) > 0 {
- sArr := strings.Split(v.Stopscopeclass, ",")
- tmpses.Bool.Must = append(tmpses.Bool.Must, sql.TopscopeclassObject{
- Terms: struct {
- Topscopeclass []string `json:"s_topscopeclass,omitempty"`
- }{
- Topscopeclass: sArr,
- },
- })
- } else if len(v.Ssubscopeclass) > 0 {
- tmpses.Bool.Must = append(tmpses.Bool.Must, sql.SubscopeclassObject{
- Terms: struct {
- Subscopeclass []string `json:"s_subscopeclass,omitempty"`
- }{
- Subscopeclass: strings.Split(v.Ssubscopeclass, ","),
- },
- })
- }
- if len(v.Sbuyerclass) > 0 {
- tmpses.Bool.Must = append(tmpses.Bool.Must, sql.BuyerclassObject{
- Terms: struct {
- Buyerclass []string `json:"buyerclass,omitempty"`
- }{
- Buyerclass: strings.Split(v.Sbuyerclass, ","),
- },
- })
- }
- torules.Should = append(torules.Should, tmpses)
- }
- bidamountSql := sql.NewEsObject{}
- if tab.Sbidamount != "" {
- if strings.Contains(tab.Sbidamount, "大于") && strings.Contains(tab.Sbidamount, "小于") {
- arr := strings.Split(tab.Sbidamount, ",")
- limit := &sql.BudgetOrBidamount{
- Gte: qu.Float64All(strings.Replace(arr[0], "大于", "", -1)),
- Lt: qu.Float64All(strings.Replace(arr[1], "小于", "", -1)),
- }
- bidamountSql.Bool.Should = append(bidamountSql.Bool.Should, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}})
- } else if strings.Contains(tab.Sbidamount, "大于") {
- limit := &sql.BudgetOrBidamount{
- Gte: qu.Float64All(strings.Replace(tab.Sbidamount, "大于", "", -1)),
- }
- bidamountSql.Bool.Should = append(bidamountSql.Bool.Should, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}})
- } else if strings.Contains(tab.Sbidamount, "小于") {
- limit := &sql.BudgetOrBidamount{
- Lt: qu.Float64All(strings.Replace(tab.Sbidamount, "小于", "", -1)),
- }
- bidamountSql.Bool.Should = append(bidamountSql.Bool.Should, sql.BidamountMust{BidamountObj: &sql.BidamountObj{Bidamount: limit}})
- }
- if tab.SbidamountFieldExist == 1 {
- SbidamountFieldExistBool := sql.NewEsObject{}
- SbidamountFieldExistBool.Bool.MustNot = append(SbidamountFieldExistBool.Bool.MustNot, sql.ExistfieldsObjectMust{
- ExistfieldsObject: &sql.ExistfieldsObject{Filter: struct {
- Exists struct {
- Field string `json:"field,omitempty"`
- } `json:"exists,omitempty"`
- }{
- Exists: struct {
- Field string `json:"field,omitempty"`
- }{Field: "bidamount"},
- },
- },
- })
- bidamountSql.Bool.Should = append(bidamountSql.Bool.Should, SbidamountFieldExistBool)
- }
- }
- budgetSql := sql.NewEsObject{}
- if tab.Sbudget != "" {
- if strings.Contains(tab.Sbudget, "大于") && strings.Contains(tab.Sbudget, "小于") {
- arr := strings.Split(tab.Sbudget, ",")
- limit := &sql.BudgetOrBidamount{
- Gte: qu.Float64All(strings.Replace(arr[0], "大于", "", -1)),
- Lt: qu.Float64All(strings.Replace(arr[1], "小于", "", -1)),
- }
- budgetSql.Bool.Should = append(budgetSql.Bool.Should, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}})
- } else if strings.Contains(tab.Sbudget, "大于") {
- limit := &sql.BudgetOrBidamount{
- Gte: qu.Float64All(strings.Replace(tab.Sbudget, "大于", "", -1)),
- }
- budgetSql.Bool.Should = append(budgetSql.Bool.Should, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}})
- } else if strings.Contains(tab.Sbudget, "小于") {
- limit := &sql.BudgetOrBidamount{
- Lt: qu.Float64All(strings.Replace(tab.Sbudget, "小于", "", -1)),
- }
- budgetSql.Bool.Should = append(budgetSql.Bool.Should, sql.BudgetMust{BudgetObj: &sql.BudgetObj{Budget: limit}})
- }
- if tab.SbudgetFieldExist == 1 {
- SbudgetFieldExistBool := sql.NewEsObject{}
- SbudgetFieldExistBool.Bool.MustNot = append(SbudgetFieldExistBool.Bool.MustNot, sql.ExistfieldsObjectMust{
- ExistfieldsObject: &sql.ExistfieldsObject{Filter: struct {
- Exists struct {
- Field string `json:"field,omitempty"`
- } `json:"exists,omitempty"`
- }{
- Exists: struct {
- Field string `json:"field,omitempty"`
- }{Field: "budget"},
- },
- },
- })
- budgetSql.Bool.Should = append(budgetSql.Bool.Should, SbudgetFieldExistBool)
- }
- }
- if len(ffBoolObject.Must) > 0 || len(ffBoolObject.MustNot) > 0 || len(ffBoolObject.Should) > 0 {
- QueryObjecct.Bool = &ffBoolObject
- }
- if len(fqBoolObject.Must) > 0 || len(fqBoolObject.MustNot) > 0 || len(fqBoolObject.Should) > 0 {
- if QueryObjecct.Bool == nil {
- QueryObjecct.Bool = &fqBoolObject
- } else {
- if fqBoolObject.Must != nil {
- QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, fqBoolObject.Must...)
- }
- if fqBoolObject.Should != nil {
- QueryObjecct.Bool.Should = append(QueryObjecct.Bool.Should, fqBoolObject.Should...)
- }
- if fqBoolObject.MustNot != nil {
- QueryObjecct.Bool.MustNot = append(QueryObjecct.Bool.MustNot, fqBoolObject.MustNot...)
- }
- }
- if len(torules.Should) > 0 {
- QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, map[string]interface{}{
- "bool": torules,
- })
- }
- } else if len(torules.Should) > 0 {
- if QueryObjecct.Bool == nil {
- QueryObjecct.Bool = &sql.BoolObject{}
- }
- QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, map[string]interface{}{
- "bool": torules,
- })
- }
- if (len(budgetSql.Bool.Should) > 0 || len(bidamountSql.Bool.Should) > 0) && (len(fqBoolObject.Must) > 0 || len(fqBoolObject.MustNot) > 0 || len(fqBoolObject.Should) > 0) {
- if QueryObjecct.Bool == nil {
- QueryObjecct.Bool = &sql.BoolObject{}
- }
- if len(budgetSql.Bool.Should) > 0 {
- QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, budgetSql)
- }
- if len(bidamountSql.Bool.Should) > 0 {
- QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, bidamountSql)
- }
- } else {
- if QueryObjecct.Bool == nil {
- QueryObjecct.Bool = &sql.BoolObject{}
- }
- if len(budgetSql.Bool.Should) > 0 {
- QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, budgetSql)
- }
- if len(bidamountSql.Bool.Should) > 0 {
- QueryObjecct.Bool.Must = append(QueryObjecct.Bool.Must, bidamountSql)
- }
- }
- rdata := make(map[string]interface{})
- rdata["query"] = QueryObjecct
- esbytes, _ := json.Marshal(rdata)
- esStr := strings.Replace(string(esbytes), "regexp1", "regexp", -1)
- esStr = strings.Replace(esStr, "publishtime1", "publishtime", -1)
- esStr = strings.Replace(esStr, "publishtime2", "publishtime", -1)
- return strings.Replace(esStr, "regexp2", "regexp", -1)
- }
- func strsToArr(strs string, typestr string) []string {
- if len(strs) > 0 {
- tmps := strings.Split(strs, ",")
- switch typestr {
- case "str":
- for index, vint := range tmps {
- switch vint {
- case TitleMatchType:
- tmps[index] = TitleMatchTypeStr
- case DetailMatchType:
- tmps[index] = DetailMatchTypeStr
- case PurchasingMatchType:
- tmps[index] = PurchasingMatchTypeStr
- case AttachmentsMatchType:
- tmps[index] = AttachmentsMatchTypeStr
- case ProjectnameMatchType:
- tmps[index] = ProjectnameMatchTypeStr
- case BuyerMatchType:
- tmps[index] = BuyerMatchTypeStr
- case SWinnerMatchType:
- tmps[index] = SWinnerMatchTypeStr
- case BuyerMatchType1:
- tmps[index] = BuyerRegexpMatchTypeStr
- case SWinnerMatchType1:
- tmps[index] = SWinnerRegexpMatchTypeStr
- }
- }
- return tmps
- case "field":
- for index, vint := range tmps {
- switch vint {
- case TitleMatchType:
- tmps[index] = TitleMatchTypeField
- case DetailMatchType:
- tmps[index] = DetailMatchTypeField
- case PurchasingMatchType:
- tmps[index] = PurchasingMatchTypeField
- case AttachmentsMatchType:
- tmps[index] = AttachmentsMatchTypeField
- case ProjectnameMatchType:
- tmps[index] = ProjectnameMatchTypeField
- case BuyerMatchType:
- tmps[index] = BuyerMatchTypeField
- case SWinnerMatchType:
- tmps[index] = SWinnerMatchTypeField
- case BuyerMatchType1:
- tmps[index] = BuyerMatchTypeField
- case SWinnerMatchType1:
- tmps[index] = SWinnerMatchTypeField
- }
- }
- return tmps
- }
- }
- return nil
- }
- func init() {
- EsIndex = qu.ObjToString(Sysconfig["elasticsearch_index"])
- EsType = qu.ObjToString(Sysconfig["elasticsearch_type"])
- }
- func method1(newEsObject sql.NewEsObject, keyword, keymatch string, tmps []string) sql.NewEsObject {
- keywordArr := []string{}
- for _, vv := range strings.Split(keyword, ",") {
- if vv == "" {
- continue
- }
- //log.Println(vv, tmps, keymatch)
- if len(tmps) == 1 && (strings.Contains(keymatch, "8") || strings.Contains(keymatch, "9")) {
- //中标单位、采购单位、中标单位(正则)、采购单位(正则) 单选
- strs := ""
- if strings.Contains(vv, "&&") {
- //strs = ".*" + strings.Replace(vv, "&&", ".*", -1) + ".*"
- strs = strings.Replace(vv, "&&", ".*", -1)
- } else {
- strs = vv
- }
- if tmps[0] == "buyer.mbuyer" {
- newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.ShouldObj{
- MultiMatch: &sql.MultiMatch{
- Query: strs,
- Type: MultiMatchType,
- Fields: tmps,
- },
- })
- } else if tmps[0] == "s_winner.mwinner" {
- newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.ShouldObj{
- MultiMatch: &sql.MultiMatch{
- Query: strs,
- Type: MultiMatchType,
- Fields: tmps,
- },
- })
- }
- } else if len(tmps) == 1 && (strings.Contains(keymatch, "6") || strings.Contains(keymatch, "7")) {
- addkeylines := strings.Split(vv, "&&")
- keywordArr = append(keywordArr, addkeylines[0])
- } else {
- addkeylines := strings.Split(vv, "&&")
- //log.Println(addkeylines, "addkeylines")
- if len(addkeylines) > 1 {
- addkeyline := sql.NewEsObject{}
- for _, vvv := range addkeylines {
- if vvv == "" {
- continue
- }
- addkeyline.Bool.Must = append(addkeyline.Bool.Must, sql.ShouldObj{
- MultiMatch: &sql.MultiMatch{
- Query: vvv,
- Type: MultiMatchType,
- Fields: tmps,
- },
- })
- }
- if len(addkeyline.Bool.Must) > 0 {
- newEsObject.Bool.Should = append(newEsObject.Bool.Should, addkeyline)
- }
- } else {
- newEsObject.Bool.Should = append(newEsObject.Bool.Should, sql.ShouldObj{
- MultiMatch: &sql.MultiMatch{
- Query: vv,
- Type: MultiMatchType,
- Fields: tmps,
- },
- })
- }
- }
- }
- if len(tmps) == 1 && (strings.Contains(keymatch, "6") || strings.Contains(keymatch, "7")) {
- addkeyline := sql.NewEsObject{}
- if tmps[0] == "buyer" {
- addkeyline.Bool.Must = append(addkeyline.Bool.Must, sql.BuyerMatch{
- Buyer: &sql.Buyer{Buyer: keywordArr},
- })
- } else if tmps[0] == "s_winner" {
- addkeyline.Bool.Must = append(addkeyline.Bool.Must, sql.WinnerMatch{
- Winner: &sql.Winner{Winner: keywordArr},
- })
- }
- if len(addkeyline.Bool.Must) > 0 {
- newEsObject.Bool.Should = append(newEsObject.Bool.Should, addkeyline)
- }
- }
- return newEsObject
- }
- func method2(newEsObject sql.NewEsObject, keyword, keymatch string, tmps []string) sql.NewEsObject {
- notKeyWordArr := []string{}
- for _, vv := range strings.Split(keyword, ",") {
- if vv == "" {
- continue
- }
- if len(tmps) == 1 && (strings.Contains(keymatch, "8") || strings.Contains(keymatch, "9")) {
- //中标单位、采购单位、中标单位(正则)、采购单位(正则) 单选
- //strs := ""
- //if strings.Contains(vv, "&&") {
- // strs = ".*" + strings.Replace(vv, "&&", ".*", -1) + ".*"
- //} else {
- // strs = ".*" + vv + ".*"
- //}
- if tmps[0] == "buyer.mbuyer" {
- newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.ShouldObj{
- MultiMatch: &sql.MultiMatch{
- Query: vv,
- Type: MultiMatchType,
- Fields: tmps,
- },
- })
- } else if tmps[0] == "s_winner.mwinner" {
- newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.ShouldObj{
- MultiMatch: &sql.MultiMatch{
- Query: vv,
- Type: MultiMatchType,
- Fields: tmps,
- },
- })
- }
- } else if len(tmps) == 1 && (strings.Contains(keymatch, "6") || strings.Contains(keymatch, "7")) {
- notkeylines := strings.Split(vv, "&&")
- notKeyWordArr = append(notKeyWordArr, notkeylines[0])
- } else {
- notkeylines := strings.Split(vv, "&&")
- if len(notkeylines) > 1 {
- notkeyline := sql.NewEsObject{}
- for _, vvv := range notkeylines {
- if vvv == "" {
- continue
- }
- notkeyline.Bool.Must = append(notkeyline.Bool.Must, sql.ShouldObj{
- MultiMatch: &sql.MultiMatch{
- Query: vvv,
- Type: MultiMatchType,
- Fields: tmps,
- },
- })
- }
- if len(notkeyline.Bool.Must) > 0 {
- newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, notkeyline)
- }
- } else {
- newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, sql.ShouldObj{
- MultiMatch: &sql.MultiMatch{
- Query: vv,
- Type: MultiMatchType,
- Fields: tmps,
- },
- })
- }
- }
- }
- if len(tmps) == 1 && (strings.Contains(keymatch, "6") || strings.Contains(keymatch, "7")) {
- var notkeyline interface{}
- if tmps[0] == "buyer" {
- notkeyline = sql.BuyerMatch{
- Buyer: &sql.Buyer{Buyer: notKeyWordArr},
- }
- } else if tmps[0] == "s_winner" {
- notkeyline = sql.WinnerMatch{
- Winner: &sql.Winner{Winner: notKeyWordArr},
- }
- }
- if notkeyline != nil {
- newEsObject.Bool.MustNot = append(newEsObject.Bool.MustNot, notkeyline)
- }
- }
- return newEsObject
- }
- /**
- * 一级公告行业处理 d、t、p
- */
- func method3(arr []string) []string {
- var sArr []string
- for _, v := range arr {
- sArr = append(sArr, v+"d")
- sArr = append(sArr, v+"t")
- sArr = append(sArr, v+"p")
- }
- return sArr
- }
- func GetMactchKey(match []map[string]string, data map[string]interface{}) string {
- keyWord := []string{}
- for _, keys := range match {
- types := keys["s_keymatch"]
- key := keys["s_matchkey"]
- if strings.Contains(types, "1") {
- title := qu.ObjToString(data["title"])
- keyWord = KeyWordToData(types, title, key, keyWord)
- }
- if strings.Contains(types, "2") {
- detail := qu.ObjToString(data["detail"])
- keyWord = KeyWordToData(types, detail, key, keyWord)
- }
- if strings.Contains(types, "3") {
- purchasing := qu.ObjToString(data["purchasing"])
- keyWord = KeyWordToData(types, purchasing, key, keyWord)
- }
- if strings.Contains(types, "4") {
- filetext := qu.ObjToString(data["filetext"])
- keyWord = KeyWordToData(types, filetext, key, keyWord)
- }
- if strings.Contains(types, "5") {
- projectname := qu.ObjToString(data["projectname"])
- keyWord = KeyWordToData(types, projectname, key, keyWord)
- }
- if strings.Contains(types, "6") || strings.Contains(types, "8") {
- buyer := qu.ObjToString(data["buyer"])
- keyWord = KeyWordToData(types, buyer, key, keyWord)
- }
- if strings.Contains(types, "7") || strings.Contains(types, "9") {
- winner := qu.ObjToString(data["s_winner"])
- keyWord = KeyWordToData(types, winner, key, keyWord)
- }
- }
- keyMap := map[string]bool{}
- keyArr := []string{}
- for _, key := range keyWord {
- keyMap[key] = true
- }
- for k, _ := range keyMap {
- keyArr = append(keyArr, k)
- }
- return strings.Join(keyArr, ",")
- }
- func KeyWordToData(types, item, key string, keyWord []string) []string {
- for _, mk := range strings.Split(key, ",") {
- if strings.Contains(mk, "&&") {
- arr := strings.Split(mk, "&&")
- isok := true
- for _, s := range arr {
- if s != "" {
- if !strings.Contains(strings.ToUpper(item), strings.ToUpper(s)) {
- isok = false
- }
- }
- }
- if isok {
- keyWord = append(keyWord, mk)
- }
- } else {
- if strings.Contains(strings.ToUpper(item), strings.ToUpper(mk)) {
- keyWord = append(keyWord, mk)
- }
- }
- }
- return keyWord
- }
- func GetProjectId_1(id string) string {
- query := `{"query": {"bool": {"must": [{"term": {"ids": "%s"}}],"must_not": [],"should": []}}}`
- querys := fmt.Sprintf(query, id)
- projectId := ""
- projectIdArr := []string{}
- data := Es.Get("projectset", "projectset", querys)
- if data != nil && *data != nil && len(*data) > 0 {
- for _, v := range *data {
- projectIdArr = append(projectIdArr, qu.ObjToString(v["_id"]))
- }
- projectId = strings.Join(projectIdArr, ",")
- log.Println("projectId", projectId)
- } else {
- log.Println("ES未查到项目id", id)
- }
- return projectId
- }
- func chunkArray(arr []string, size int) [][]string {
- var chunks [][]string
- for i := 0; i < len(arr); i += size {
- end := i + size
- if end > len(arr) {
- end = len(arr)
- }
- chunks = append(chunks, arr[i:end])
- }
- return chunks
- }
- func GetProjectId(datas *[]map[string]interface{}) (pidMap map[string]interface{}) {
- var ids []string
- pidMap = make(map[string]interface{})
- for _, d1 := range *datas {
- id := qu.ObjToString(d1["info_id"])
- ids = append(ids, id)
- pidMap[id] = ""
- }
- query := `{"query": {"bool": {"must": [{"terms": {"ids": ["%s"]}}]}},"_source": ["ids","id"]}`
- for _, array := range chunkArray(ids, 200) {
- query = fmt.Sprintf(query, strings.Join(array, "\",\""))
- data := Es.Get("projectset", "projectset", query)
- if data != nil && *data != nil && len(*data) > 0 {
- for _, v := range *data {
- pid := qu.ObjToString(v["id"])
- for _, id := range strings.Split(qu.ObjToString(v["ids"]), ",") {
- if pidMap[id] != nil {
- pidMap[id] = pid
- }
- }
- }
- } else {
- log.Println("ES未查到项目id", query)
- }
- }
- for _, m := range *datas {
- id := qu.ObjToString(m["info_id"])
- if pid := qu.ObjToString(pidMap[id]); pid != "" {
- log.Println("id: ", id, "pid: ", pid)
- m["projectId"] = SE2.EncodeString(pid)
- } else {
- log.Println("id: ", id, "pid: -")
- }
- }
- return
- }
|