xingzai 1 tahun lalu
induk
melakukan
29f1f16beb
3 mengubah file dengan 86 tambahan dan 2 penghapusan
  1. 2 1
      controllers/search.go
  2. 13 1
      models/resource_data.go
  3. 71 0
      services/es_comprehensive.go

+ 2 - 1
controllers/search.go

@@ -743,7 +743,8 @@ func (this *SearchController) ComprehensiveList() {
 		return
 	}
 	resp := new(models.HomeResourceDataListResp)
-	tmpResult, tmpTotalResult, err := services.EsComprehensiveSearch(keyWord, startSize, pageSize)
+
+	tmpResult, tmpTotalResult, err := services.SqlComprehensiveSearch(keyWord, startSize, pageSize)
 	if err != nil {
 		br.Msg = "检索失败"
 		br.ErrMsg = "检索失败,Err:" + err.Error()

+ 13 - 1
models/resource_data.go

@@ -61,7 +61,19 @@ func GetResourceDataList(condition string, pars []interface{}, startSize, pageSi
 	return
 }
 
-// 获取用户报名成功数量
+// 列表
+func GetResourceDataListCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxResourceData, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT	 *   FROM cygx_resource_data  WHERE 1= 1 `
+	if condition != "" {
+		sql += condition
+	}
+	sql += `   LIMIT ?,? `
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+// 获取数量
 func GetResourceDataCount(condition string, pars []interface{}) (count int, err error) {
 	sqlCount := `SELECT COUNT(1) AS count FROM cygx_resource_data    WHERE 1= 1 ` + condition
 	o := orm.NewOrm()

+ 71 - 0
services/es_comprehensive.go

@@ -1522,3 +1522,74 @@ func GetResourceDataEsList(list []*SearchComprehensiveItem, user *models.WxUserI
 	}
 	return
 }
+
+func SqlComprehensiveSearch(keyWord string, startSize, pageSize int) (result []*SearchComprehensiveItem, total int, err error) {
+	keyWord = "%" + keyWord + "%"
+	var conditionTitle string
+	var parsTitle []interface{}
+	conditionTitle = " AND search_title LIKE ? "
+	parsTitle = append(parsTitle, keyWord)
+	totalTitle, e := models.GetResourceDataCount(conditionTitle, parsTitle)
+	if e != nil {
+		err = errors.New("GetResourceDataCount, Err: " + e.Error())
+		return
+	}
+
+	var conditionContent string
+	var parsContent []interface{}
+
+	conditionContent = " AND search_content LIKE ? AND search_title  NOT LIKE ?  "
+	parsContent = append(parsContent, keyWord, keyWord)
+	totalContent, e := models.GetResourceDataCount(conditionContent, parsContent)
+	if e != nil {
+		err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error())
+		return
+	}
+	var searchTotal int
+	searchTotal = (startSize + 1) * pageSize
+	var list []*models.CygxResourceData
+	fmt.Println(totalTitle)
+	fmt.Println(searchTotal)
+	if totalTitle >= searchTotal {
+		fmt.Println("1")
+		//全部都是标题搜索
+		list, e = models.GetResourceDataListCondition(conditionTitle, parsTitle, startSize, pageSize)
+		if e != nil && e.Error() != utils.ErrNoRow() {
+			err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
+			return
+		}
+	} else if totalTitle <= searchTotal-pageSize {
+		fmt.Println("2")
+		//全部都是内容搜索
+		list, e = models.GetResourceDataListCondition(conditionContent, parsContent, startSize, pageSize)
+		if e != nil && e.Error() != utils.ErrNoRow() {
+			err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
+			return
+		}
+	} else {
+		fmt.Println("3")
+		//一半标题搜索,一半内容搜索
+		list, e = models.GetResourceDataListCondition(conditionTitle, parsTitle, startSize, pageSize)
+		if e != nil && e.Error() != utils.ErrNoRow() {
+			err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
+			return
+		}
+		listContent, e := models.GetResourceDataListCondition(conditionContent, parsContent, 0, pageSize-totalContent%pageSize)
+		if e != nil && e.Error() != utils.ErrNoRow() {
+			err = errors.New("GetResourceDataListCondition, Err: " + e.Error())
+			return
+		}
+		for _, v := range listContent {
+			list = append(list, v)
+		}
+	}
+
+	for _, v := range list {
+		item := new(SearchComprehensiveItem)
+		item.SourceId = v.SourceId
+		item.Source = v.Source
+		result = append(result, item)
+	}
+	total = totalTitle + totalContent
+	return
+}