|
@@ -1,10 +1,13 @@
|
|
|
package entity
|
|
|
|
|
|
import (
|
|
|
+ "config"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
"log"
|
|
|
qutil "qfw/util"
|
|
|
"qfw/util/elastic"
|
|
|
+ "qfw/util/redis"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
@@ -26,8 +29,10 @@ type EnterpriseSearch struct {
|
|
|
}
|
|
|
|
|
|
const (
|
|
|
- searchMaxLimit = 100 //最大查询数量限制
|
|
|
- freeSearchNum = 5 //免费查询数量限制
|
|
|
+ searchMaxLimit = 100 //最大查询数量限制
|
|
|
+ freeSearchNum = 5 //免费查询数量限制
|
|
|
+ entSearchCacheDB = "other"
|
|
|
+ entSearchCacheKey = "entSearchIndexCache"
|
|
|
|
|
|
index, itype = "qyxy", "qyxy"
|
|
|
entQuery = `{"query":{"bool":{"must":[%s]}},"_source":["_id","company_name","company_status","legal_person","capital","company_address","company_shortname"]}`
|
|
@@ -103,11 +108,26 @@ func (es *EnterpriseSearch) Check() (*EnterpriseSearch, error) {
|
|
|
return es, nil
|
|
|
}
|
|
|
|
|
|
+//判断是否是空查询
|
|
|
+func (es *EnterpriseSearch) isEmptySearch() bool {
|
|
|
+ if es.Match == "" &&
|
|
|
+ es.EntArea == "" &&
|
|
|
+ es.EntCity == "" &&
|
|
|
+ es.EntCapital == "" &&
|
|
|
+ es.EntType == "" &&
|
|
|
+ es.EntStatus == "" &&
|
|
|
+ es.BiddingArea == "" &&
|
|
|
+ es.EntClass == "" &&
|
|
|
+ es.EntContact == "" {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
//GetQuerySql 获取检索语句
|
|
|
//return company_name、company_status、legal_person、capital、company_address、id、company_shortname
|
|
|
//返回字段 企业名称、企业状态、法人、注册资本、地址、企业id、企业简称
|
|
|
func (es *EnterpriseSearch) GetQuerySql() string {
|
|
|
-
|
|
|
musts := make([]string, 0, 0)
|
|
|
//输入查询
|
|
|
if es.Match != "" { //或关系 仅需满足一个
|
|
@@ -188,6 +208,10 @@ func (es *EnterpriseSearch) GetQuerySql() string {
|
|
|
|
|
|
//DoQuery 根据EnterpriseSearch参数进行企业相关查询
|
|
|
func (es *EnterpriseSearch) DoQuery() (list *[]map[string]interface{}, total int64, err error) {
|
|
|
+ if es.isEmptySearch() { //是否是空查询,返回默认企业
|
|
|
+ list = GetEntIndexShow()
|
|
|
+ return
|
|
|
+ }
|
|
|
sql := es.GetQuerySql()
|
|
|
log.Println("EnterpriseSearch DoQuery sql", sql)
|
|
|
|
|
@@ -213,21 +237,37 @@ func formatData(list *[]map[string]interface{}) *[]map[string]interface{} {
|
|
|
if list != nil {
|
|
|
for index, _ := range *list {
|
|
|
(*list)[index]["id"] = qutil.EncodeArticleId2ByCheck(qutil.ObjToString((*list)[index]["_id"]))
|
|
|
- //if shortname, ok := ((*list)[index]["company_shortname"]).(string); !ok || shortname == "" {
|
|
|
- // (*list)[index]["company_shortname"] = getCompanyShortName(qutil.ObjToString((*list)[index]["company_name"]))
|
|
|
- //}
|
|
|
+ if shortname, ok := ((*list)[index]["company_shortname"]).(string); !ok || shortname == "" {
|
|
|
+ (*list)[index]["company_shortname"] = getCompanyShortName(qutil.ObjToString((*list)[index]["company_name"]))
|
|
|
+ }
|
|
|
delete((*list)[index], "_id")
|
|
|
}
|
|
|
}
|
|
|
return list
|
|
|
}
|
|
|
|
|
|
-//func getCompanyShortName(entName string) string {
|
|
|
-// if entName == "" {
|
|
|
-// return ""
|
|
|
-// }
|
|
|
-// //去掉省、市、县、区、路
|
|
|
-//
|
|
|
-// //截取四个字符
|
|
|
-// return "shortName"
|
|
|
-//}
|
|
|
+//暂不处理前端处理
|
|
|
+func getCompanyShortName(entName string) string {
|
|
|
+ if entName == "" {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ //去掉省、市、县、区、路
|
|
|
+ //截取四个字符
|
|
|
+ return ""
|
|
|
+}
|
|
|
+
|
|
|
+func GetEntIndexShow() (list *[]map[string]interface{}) {
|
|
|
+ bytes, err := redis.GetBytes(entSearchCacheDB, entSearchCacheKey)
|
|
|
+ if err == nil {
|
|
|
+ if err = json.Unmarshal(*bytes, &list); err == nil && list != nil && len(*list) > 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sql := fmt.Sprintf(entQuery, fmt.Sprintf(`{"terms":{"_id":["%s"]}}`, strings.Join(config.Config.DefaultEntList, "\",\"")))
|
|
|
+ listTmp := elastic.Get(index, itype, sql)
|
|
|
+ if listTmp != nil && len(*listTmp) > 0 {
|
|
|
+ list = formatData(listTmp)
|
|
|
+ redis.Put(entSearchCacheDB, entSearchCacheKey, list, 60*60*10)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|