package service import ( "errors" "fmt" "sfbase/core" "sfbase/elastic" "sfbase/utils" "sfis/db" "sfis/model" "log" "strconv" "strings" "time" ) var ( pjt_count = 100 pjt_field = `"_id","jgtime","zbtime","projectname","buyer","buyerclass"` pjt_fields = `"_id","area","city","bidamount","budget","zbtime","jgtime","projectname","projectcode","s_winner","buyer","buyerclass","buyerperson","buyertel","package1","list","bidstatus"` pjt_sort = `{"jgtime":-1}` query = `{"query": {"bool": {"must":[%s],"should":[%s],"minimum_should_match": 1}}}` query_string = `{"constant_score": {"boost": 2,"query": {"match_phrase": {"projectname.pname": {"analyzer": "my_ngram","query": "%s","slop": 6}}}}},{"multi_match": {"query": "%s","fields": ["projectname.pname"],"analyzer": "ik","minimum_should_match": "100%%"}}` query_winner = `{"term": {"s_winner": "%s"}}` SR = strings.Replace HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}` highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}` ) func ProjectListData(productId int, appid, projectName, winner, times string, isDetail bool) (data []map[string]interface{}, httpStatus int, err error) { times = strings.TrimSpace(times) winner = strings.TrimSpace(winner) pjt_name := strings.TrimSpace(projectName) pjt_name = strings.ReplaceAll(pjt_name, `"`, `\"`) pjt_len := len([]rune(pjt_name)) httpStatus = 200 qstr := "" timestr := "" if times != "" { start := times + " 00:00:00" end := times + " 23:59:59" loc, _ := time.LoadLocation("Local") startTimes := "" endTimes := "" if startTime, err := time.ParseInLocation("2006-01-02 15:04:05", start, loc); err == nil { startTimes = fmt.Sprint(startTime.Unix()) } if endTime, err := time.ParseInLocation("2006-01-02 15:04:05", end, loc); err == nil { endTimes = fmt.Sprint(endTime.Unix()) } timestr = `{"range": {"jgtime": {"gte": ` + startTimes + `,"lte": ` + endTimes + `}}}` } if pjt_len >= 4 && winner == "" { qstr = fmt.Sprintf(query, "", fmt.Sprintf(query_string, pjt_name, pjt_name)) if timestr != "" { qstr = fmt.Sprintf(query, timestr, fmt.Sprintf(query_string, pjt_name, pjt_name)) } } else if pjt_len >= 4 && winner != "" { qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), fmt.Sprintf(query_string, pjt_name, pjt_name)) if timestr != "" { qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, fmt.Sprintf(query_string, pjt_name, pjt_name)) } } else if winner != "" { qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), "") if timestr != "" { qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, "") } } else { err = errors.New("项目名长度小于4,查询失败") return } INDEX := core.GetStringConf("es.project.index") TYPE := core.GetStringConf("es.project.itype") Es := db.GetEs() fields := "" if isDetail { fields = pjt_fields } else { fields = pjt_field } userProduct := &model.UserProduct{} db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appid, ProductID: productId}) repl, err := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, fields, 0, userProduct.DataNumLimitOneTimes, 0, false) if err != nil { err = errors.New("查询失败") return } else if repl != nil && len(*repl) > 0 { for _, i := range *repl { if isDetail { if i["list"] != nil { bidStatusStr := "中标、成交、流标、废标、合同" subStr := "中标、成交、合同" subStrs := "招标、邀标、询价、竞谈、竞价、单一" publish := int64(0) if listArr, ok := i["list"].([]interface{}); ok { if utils.ObjToString(i["bidstatus"]) == "招标" { for _, v := range listArr { if vs, oks := v.(map[string]interface{}); oks { if strings.Contains(subStrs, utils.ObjToString(vs["subrtype"])) { publishs := publish publish = utils.Int64All(vs["publishtime"]) if publishs > publish { publish = publishs } } } } } else if strings.Contains(bidStatusStr, utils.ObjToString(i["bidstatus"])) { for _, v := range listArr { if vs, oks := v.(map[string]interface{}); oks { if strings.Contains(subStr, utils.ObjToString(vs["subrtype"])) { publishs := publish publish = utils.Int64All(vs["publishtime"]) if publishs > publish { publish = publishs } } } } } for _, v := range listArr { if vs, oks := v.(map[string]interface{}); oks { publishs := utils.Int64All(vs["publishtime"]) if publishs == publish { i["url"] = vs["href"] infoid := utils.ObjToString(vs["infoid"]) i["jyUrl"] = `https://www.jianyu360.com/article/content/` + utils.CommonEncodeArticle("content", infoid) + `.html` } } } } } dataMap := map[string]interface{}{ "projectId": SE.EncodeString(utils.ObjToString(i["_id"])), "projectName": i["projectname"], "winner": i["s_winner"], "projectCode": i["projectcode"], "buyer": i["buyer"], "buyerPerson": i["buyerperson"], "buyerTel": i["buyertel"], "buyerClass": i["buyerclass"], "tenderDate": i["zbtime"], "bidDate": i["jgtime"], "bidAmount": i["bidamount"], "budget": i["budget"], "province": i["area"], "city": i["city"], "package": i["package1"], "url": i["url"], "jyUrl": i["jyUrl"], } data = append(data, dataMap) } else { dataMap := map[string]interface{}{ "projectId": SE.EncodeString(utils.ObjToString(i["_id"])), "projectName": i["projectname"], "buyer": i["buyer"], "buyerClass": i["buyerclass"], "tenderDate": i["zbtime"], "bidDate": i["jgtime"], } data = append(data, dataMap) } } } return } func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) (*[]map[string]interface{}, error) { if qstr != "" { if highlight { ws := []string{} for _, w := range strings.Split(findfields, ",") { ws = append(ws, fmt.Sprintf(highlightStr, w, count)) } qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}` } if len(fields) > 0 { qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}" } if len(order) > 0 { qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}` } if start > -1 { qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}" } log.Println("es语句", qstr) return Es.Get(index, itype, qstr) } else { return nil, nil } }