package controllers import ( "fmt" "github.com/rdlucklib/rdluck_tools/paging" "hongze/hongze_clpt/models" "hongze/hongze_clpt/services" "hongze/hongze_clpt/utils" "strings" "time" ) type SearchController struct { BaseAuthController } type BaseSearchController struct { BaseCommonController } type MobileSearchController struct { BaseAuthMobileController } // @Title 搜索接口 // @Description 搜索接口 // @Param PageSize query int false "每页数据条数" // @Param CurrentIndex query int false "当前页页码,从1开始" // @Param KeyWord query string true "搜索关键词" // @Param OrderColumn query string false "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 " // @Param ListType query int false "列表类型,1最新/全部,2 纪要 ,3图表 默认1" // @Success 200 {object} models.SearchResp // @router /list [get] func (this *MobileSearchController) ListHomeArtAndChart() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") listType, _ := this.GetInt("ListType") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } if listType == 0 { listType = 1 } startSize = paging.StartIndex(currentIndex, pageSize) keyWord := this.GetString("KeyWord") orderColumn := this.GetString("OrderColumn") if keyWord == "" { br.Msg = "请输入搜索词" br.ErrMsg = "请输入搜索词" return } //添加映射关系 keyWord = strings.ToUpper(keyWord) keyWordDetail, _ := models.GetCygxCygxIkWordMapDetail(keyWord) if keyWordDetail != nil { keyWord = keyWordDetail.KeyWordMap } user := this.User if user == nil { br.Msg = "请重新登录" br.Ret = 408 return } //研选的五张图片 detailResearch, errConfig := models.GetConfigByCode("category_research_img_url") if errConfig != nil { br.Msg = "获取数据失败" br.ErrMsg = "获取数据研选分类图片失败,Err:" + errConfig.Error() return } researchList := strings.Split(detailResearch.ConfigValue, "{|}") //对应分类的所图片 detailCategoryUrl, errConfig := models.GetConfigByCode("category_map_img_url") if errConfig != nil { br.Msg = "获取数据失败" br.ErrMsg = "行业配置信息失败,Err:" + errConfig.Error() return } categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}") mapCategoryUrl := make(map[string]string) var categoryId string var imgUrlChart string for _, v := range categoryUrlList { vslice := strings.Split(v, "_") categoryId = vslice[0] imgUrlChart = vslice[len(vslice)-1] mapCategoryUrl[categoryId] = imgUrlChart } if orderColumn == "" { orderColumn = "Matching" } orderColumn = "Matching" //indexName := utils.IndexName //pageSize = 20 var chartTotal int resp := new(models.SearchResp) //page := paging.GetPaging(currentIndex, pageSize, total) var chartList []*models.HomeChartListResp var err error var condition string var pars []interface{} if listType == 1 || listType == 3 { //if listType == 1 { // pageSize = 100 //} if currentIndex <= 2 { condition = ` AND title LIKE '%` + keyWord + `%'` chartList, err = models.GetChartList(condition, pars, startSize, pageSize) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取用户信息失败,Err:" + err.Error() return } chartTotal, err = models.GetChartCount(condition, pars) if err != nil { br.Msg = "获取信息失败" br.Msg = "获取帖子总数失败,Err:" + err.Error() return } } } for k, v := range chartList { chartList[k].IsNeedJump = true chartList[k].Source = 2 if v.PtagName != "" { chartList[k].CtagNamePc = v.PtagName } if v.CtagName != "" { chartList[k].CtagNamePc += "," + v.CtagName } if v.PtagNameTwo != "" { chartList[k].CtagNamePc += "," + v.PtagNameTwo } if v.CtagNameTwo != "" { chartList[k].CtagNamePc += "," + v.CtagNameTwo } if v.PtagName != "" { labelItem := new(models.LabelList) labelItem.PtagName = v.PtagName labelItem.CtagName = v.CtagName chartList[k].LabelList = append(chartList[k].LabelList, labelItem) } if v.PtagNameTwo != "" { labelItemTwo := new(models.LabelList) labelItemTwo.PtagName = v.PtagNameTwo labelItemTwo.CtagName = v.CtagNameTwo chartList[k].LabelList = append(chartList[k].LabelList, labelItemTwo) } if len(chartList[k].LabelList) == 0 { chartList[k].LabelList = make([]*models.LabelList, 0) } } if len(chartList) == 0 { chartList = make([]*models.HomeChartListResp, 0) } resp.ChartList = chartList var result []*models.SearchItem var total int64 var articleIds []int if listType == 1 || listType == 2 { //if orderColumn == "PublishDate" { // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId) // result = tmpResult // total = tmpTotal // err = tmpErr //} else { // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn) // result = tmpResult // total = tmpTotal // err = tmpErr //} _, tmpTotal, err := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn, 0) if err != nil { br.Msg = "检索失败" br.ErrMsg = "检索失败,Err:" + err.Error() return } tmpResult, tmpTotalResult, err := services.EsArticleSearchBody(keyWord, startSize, pageSize, orderColumn, 1) if err != nil { br.Msg = "检索失败" br.ErrMsg = "检索失败,Err:" + err.Error() return } result = tmpResult fmt.Println(tmpTotalResult) if int(tmpTotalResult) < currentIndex*pageSize { startSizeBody := startSize - int(tmpTotalResult) if startSizeBody < 0 { startSizeBody = 0 } var pageSizeBody int pageSizeBody = pageSize - len(tmpResult) tmpResultBody, tmpTotalBody, err := services.EsArticleSearchBody(keyWord, startSizeBody, pageSizeBody, orderColumn, 2) if err != nil { br.Msg = "检索失败" br.ErrMsg = "检索失败,Err:" + err.Error() return } for _, v := range tmpResultBody { result = append(result, v) } tmpTotalResult += tmpTotalBody } if int(tmpTotalResult) < currentIndex*pageSize { startSizeIk := startSize - int(tmpTotalResult) if startSizeIk < 0 { startSizeIk = 0 } var pageSizeIk int pageSizeIk = pageSize - len(result) tmpResultIk, _, err := services.EsArticleSearch(keyWord, startSizeIk, pageSizeIk, orderColumn, 2) if err != nil { br.Msg = "检索失败" br.ErrMsg = "检索失败,Err:" + err.Error() return } for _, v := range tmpResultIk { result = append(result, v) } } total = tmpTotal if len(result) == 0 { result = make([]*models.SearchItem, 0) } for k, v := range result { //如果是研选系列的任意取五张图片的中的一张 if v.CategoryId == "0" { knum := v.ArticleId % 5 result[k].ImgUrlPc = researchList[knum] } else { result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId] } result[k].Source = 1 articleIds = append(articleIds, v.ArticleId) } } articleMapPv := services.GetArticleHistoryByArticleId(articleIds) //文章Pv //ListType query int true "列表类型,1最新/全部,2 纪要 ,3图表 默认1" //记录用户搜索关键词 //var source int //if listType == 1 { // source = 3 //} else if listType == 2 { // source = 1 //} else { // source = 2 //} //如果是有效用户就记录搜索历史 //if user.UserId > 0 { // go services.AddSearchKeyWord(user, keyWord, source) //} if chartTotal > int(total) { total = int64(chartTotal) } if listType == 1 { total = total + int64(chartTotal) } if len(result) == 0 { result = make([]*models.SearchItem, 0) } else { yxArticleIdMap := services.GetYxArticleIdMap(articleIds) for _, v := range result { v.IsResearch = yxArticleIdMap[v.ArticleId] // 添加是否属于研选的标识 v.Pv = articleMapPv[v.ArticleId] // 添加Pv } } page := paging.GetPaging(currentIndex, pageSize, int(total)) resp.Paging = page resp.List = result br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @Title 获取搜索推荐词 // @Description 获取搜索推荐词 // @Success 200 {object} models.ConfigResp // @router /keyWord [get] func (this *MobileSearchController) BrowseHistoryList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请重新登录" br.Ret = 408 return } resp := new(models.ConfigResp) hotSearch, err := models.GetHotSearch() if err != nil { br.Msg = "获取数据失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } slicehotSearch := strings.Split(hotSearch, ",") for _, v := range slicehotSearch { item := new(models.KeyWord) item.KeyWord = v resp.ListRecommend = append(resp.ListRecommend, item) } var condition string var pars []interface{} currentTime := time.Now() starTime := currentTime.AddDate(0, 0, -8).Format("2006-01-02") + " 00:00:00" endTime := currentTime.AddDate(0, 0, -1).Format("2006-01-02") + " 23:59:59" condition += ` AND create_time < ` + "'" + endTime + "'" + `AND create_time > ` + "'" + starTime + "'" hotList, err := models.GetSearchKeyWordTop(condition, pars) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取用户信息失败,Err:" + err.Error() return } for _, v := range hotList { item := new(models.KeyWord) item.KeyWord = v.KeyWord resp.ListHot = append(resp.ListHot, item) } //如果用户存在就展示历史搜索记录 if user.UserId > 0 { historySearch, err := models.GetSearchKeyWordByUser(user.UserId) if err != nil { br.Msg = "获取数据失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } resp.ListHistory = historySearch } if len(resp.ListHistory) == 0 { resp.ListHistory = make([]*models.KeyWord, 0) } br.Msg = "获取成功!" br.Ret = 200 br.Success = true br.Data = resp } // @Title 综合搜索接口 // @Description 综合搜索接口 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param KeyWord query string true "搜索关键词" // @Success 200 {object} models.SearchItem // @router /comprehensive/list [get] func (this *MobileSearchController) ComprehensiveList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) keyWord := this.GetString("KeyWord") if keyWord == "" { br.Msg = "请输入搜索词" br.ErrMsg = "请输入搜索词" return } user := this.User if user == nil { br.Msg = "请重新登录" br.Ret = 408 return } resp := new(models.HomeResourceDataListNewResp) tmpResult, tmpTotalResult, err := services.SqlComprehensiveSearch(keyWord, startSize, pageSize) if err != nil { br.Msg = "检索失败" br.ErrMsg = "检索失败,Err:" + err.Error() return } list, err := services.GetResourceDataEsList(tmpResult, user) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取数据失败,Err:" + err.Error() return } if len(list) == 0 { list = make([]*models.CygxResourceDataNewResp, 0) } if currentIndex == 1 { go services.AddSearchKeyWord(user, keyWord, 1) } resp.List = list page := paging.GetPaging(currentIndex, pageSize, int(tmpTotalResult)) resp.Paging = page br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp }