search.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package controllers
  2. import (
  3. "fmt"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hongze_clpt/models"
  6. "hongze/hongze_clpt/services"
  7. "hongze/hongze_clpt/utils"
  8. "strings"
  9. )
  10. type SearchController struct {
  11. BaseAuthController
  12. }
  13. type BaseSearchController struct {
  14. BaseCommonController
  15. }
  16. type MobileSearchController struct {
  17. BaseAuthMobileController
  18. }
  19. // @Title 搜索接口
  20. // @Description 搜索接口
  21. // @Param PageSize query int false "每页数据条数"
  22. // @Param CurrentIndex query int false "当前页页码,从1开始"
  23. // @Param KeyWord query string true "搜索关键词"
  24. // @Param OrderColumn query string false "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  25. // @Param ListType query int false "列表类型,1最新/全部,2 纪要 ,3图表 默认1"
  26. // @Success 200 {object} models.SearchResp
  27. // @router /list [get]
  28. func (this *MobileSearchController) ListHomeArtAndChart() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. pageSize, _ := this.GetInt("PageSize")
  35. currentIndex, _ := this.GetInt("CurrentIndex")
  36. listType, _ := this.GetInt("ListType")
  37. var startSize int
  38. if pageSize <= 0 {
  39. pageSize = utils.PageSize20
  40. }
  41. if currentIndex <= 0 {
  42. currentIndex = 1
  43. }
  44. if listType == 0 {
  45. listType = 1
  46. }
  47. startSize = paging.StartIndex(currentIndex, pageSize)
  48. keyWord := this.GetString("KeyWord")
  49. orderColumn := this.GetString("OrderColumn")
  50. if keyWord == "" {
  51. br.Msg = "请输入搜索词"
  52. br.ErrMsg = "请输入搜索词"
  53. return
  54. }
  55. user := this.User
  56. if user == nil {
  57. br.Msg = "请重新登录"
  58. br.Ret = 408
  59. return
  60. }
  61. //研选的五张图片
  62. detailResearch, errConfig := models.GetConfigByCode("category_research_img_url")
  63. if errConfig != nil {
  64. br.Msg = "获取数据失败"
  65. br.ErrMsg = "获取数据研选分类图片失败,Err:" + errConfig.Error()
  66. return
  67. }
  68. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  69. //对应分类的所图片
  70. detailCategoryUrl, errConfig := models.GetConfigByCode("category_map_img_url")
  71. if errConfig != nil {
  72. br.Msg = "获取数据失败"
  73. br.ErrMsg = "行业配置信息失败,Err:" + errConfig.Error()
  74. return
  75. }
  76. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  77. mapCategoryUrl := make(map[string]string)
  78. var categoryId string
  79. var imgUrlChart string
  80. for _, v := range categoryUrlList {
  81. vslice := strings.Split(v, "_")
  82. categoryId = vslice[0]
  83. imgUrlChart = vslice[len(vslice)-1]
  84. mapCategoryUrl[categoryId] = imgUrlChart
  85. }
  86. if orderColumn == "" {
  87. orderColumn = "Matching"
  88. }
  89. orderColumn = "Matching"
  90. //indexName := utils.IndexName
  91. //pageSize = 20
  92. var chartTotal int
  93. resp := new(models.SearchResp)
  94. //page := paging.GetPaging(currentIndex, pageSize, total)
  95. var chartList []*models.HomeChartListResp
  96. var err error
  97. var condition string
  98. var pars []interface{}
  99. if listType == 1 || listType == 3 {
  100. //if listType == 1 {
  101. // pageSize = 100
  102. //}
  103. if currentIndex <= 2 {
  104. condition = ` AND title LIKE '%` + keyWord + `%'`
  105. chartList, err = models.GetChartList(condition, pars, startSize, pageSize)
  106. if err != nil {
  107. br.Msg = "获取信息失败"
  108. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  109. return
  110. }
  111. chartTotal, err = models.GetChartCount(condition, pars)
  112. if err != nil {
  113. br.Msg = "获取信息失败"
  114. br.Msg = "获取帖子总数失败,Err:" + err.Error()
  115. return
  116. }
  117. }
  118. }
  119. for k, v := range chartList {
  120. chartList[k].IsNeedJump = true
  121. chartList[k].Source = 2
  122. if v.PtagName != "" {
  123. chartList[k].CtagNamePc = v.PtagName
  124. }
  125. if v.CtagName != "" {
  126. chartList[k].CtagNamePc += "," + v.CtagName
  127. }
  128. if v.PtagNameTwo != "" {
  129. chartList[k].CtagNamePc += "," + v.PtagNameTwo
  130. }
  131. if v.CtagNameTwo != "" {
  132. chartList[k].CtagNamePc += "," + v.CtagNameTwo
  133. }
  134. if v.PtagName != "" {
  135. labelItem := new(models.LabelList)
  136. labelItem.PtagName = v.PtagName
  137. labelItem.CtagName = v.CtagName
  138. chartList[k].LabelList = append(chartList[k].LabelList, labelItem)
  139. }
  140. if v.PtagNameTwo != "" {
  141. labelItemTwo := new(models.LabelList)
  142. labelItemTwo.PtagName = v.PtagNameTwo
  143. labelItemTwo.CtagName = v.CtagNameTwo
  144. chartList[k].LabelList = append(chartList[k].LabelList, labelItemTwo)
  145. }
  146. if len(chartList[k].LabelList) == 0 {
  147. chartList[k].LabelList = make([]*models.LabelList, 0)
  148. }
  149. }
  150. if len(chartList) == 0 {
  151. chartList = make([]*models.HomeChartListResp, 0)
  152. }
  153. resp.ChartList = chartList
  154. var result []*models.SearchItem
  155. var total int64
  156. if listType == 1 || listType == 2 {
  157. //if orderColumn == "PublishDate" {
  158. // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
  159. // result = tmpResult
  160. // total = tmpTotal
  161. // err = tmpErr
  162. //} else {
  163. // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
  164. // result = tmpResult
  165. // total = tmpTotal
  166. // err = tmpErr
  167. //}
  168. _, tmpTotal, err := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn, 0)
  169. if err != nil {
  170. br.Msg = "检索失败"
  171. br.ErrMsg = "检索失败,Err:" + err.Error()
  172. return
  173. }
  174. tmpResult, tmpTotalResult, err := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn, 1)
  175. if err != nil {
  176. br.Msg = "检索失败"
  177. br.ErrMsg = "检索失败,Err:" + err.Error()
  178. return
  179. }
  180. result = tmpResult
  181. if int(tmpTotalResult) < currentIndex*pageSize {
  182. fmt.Println(currentIndex * pageSize)
  183. fmt.Println(startSize)
  184. fmt.Println(tmpTotalResult)
  185. startSizeIk := startSize - int(tmpTotalResult)
  186. if startSizeIk < 0 {
  187. startSizeIk = 0
  188. }
  189. var pageSizeIk int
  190. pageSizeIk = pageSize - len(tmpResult)
  191. tmpResultIk, _, err := services.EsArticleSearch(keyWord, startSizeIk, pageSizeIk, orderColumn, 2)
  192. if err != nil {
  193. br.Msg = "检索失败"
  194. br.ErrMsg = "检索失败,Err:" + err.Error()
  195. return
  196. }
  197. for _, v := range tmpResultIk {
  198. result = append(result, v)
  199. }
  200. }
  201. total = tmpTotal
  202. if len(result) == 0 {
  203. result = make([]*models.SearchItem, 0)
  204. }
  205. for k, v := range result {
  206. //如果是研选系列的任意取五张图片的中的一张
  207. if v.CategoryId == "0" {
  208. knum := v.ArticleId % 5
  209. result[k].ImgUrlPc = researchList[knum]
  210. } else {
  211. result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  212. }
  213. result[k].Source = 1
  214. }
  215. }
  216. //ListType query int true "列表类型,1最新/全部,2 纪要 ,3图表 默认1"
  217. //记录用户搜索关键词
  218. var source int
  219. if listType == 1 {
  220. source = 3
  221. } else if listType == 2 {
  222. source = 1
  223. } else {
  224. source = 2
  225. }
  226. //如果是有效用户就记录搜索历史
  227. if user.UserId > 0 {
  228. go services.AddSearchKeyWord(user, keyWord, source)
  229. }
  230. if chartTotal > int(total) {
  231. total = int64(chartTotal)
  232. }
  233. if listType == 1 {
  234. total = total + int64(chartTotal)
  235. }
  236. if len(result) == 0 {
  237. result = make([]*models.SearchItem, 0)
  238. }
  239. page := paging.GetPaging(currentIndex, pageSize, int(total))
  240. resp.Paging = page
  241. resp.List = result
  242. br.Ret = 200
  243. br.Success = true
  244. br.Msg = "获取成功"
  245. br.Data = resp
  246. }
  247. // @Title 获取搜索推荐词
  248. // @Description 获取搜索推荐词
  249. // @Success 200 {object} models.ConfigResp
  250. // @router /keyWord [get]
  251. func (this *MobileSearchController) BrowseHistoryList() {
  252. br := new(models.BaseResponse).Init()
  253. defer func() {
  254. this.Data["json"] = br
  255. this.ServeJSON()
  256. }()
  257. user := this.User
  258. if user == nil {
  259. br.Msg = "请重新登录"
  260. br.Ret = 408
  261. return
  262. }
  263. resp := new(models.ConfigResp)
  264. hotSearch, err := models.GetHotSearch()
  265. if err != nil {
  266. br.Msg = "获取数据失败"
  267. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  268. return
  269. }
  270. slicehotSearch := strings.Split(hotSearch, ",")
  271. for _, v := range slicehotSearch {
  272. item := new(models.KeyWord)
  273. item.KeyWord = v
  274. resp.ListRecommend = append(resp.ListRecommend, item)
  275. }
  276. hotList, err := models.GetSearchKeyWordTop()
  277. if err != nil {
  278. br.Msg = "获取信息失败"
  279. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  280. return
  281. }
  282. for _, v := range hotList {
  283. item := new(models.KeyWord)
  284. item.KeyWord = v.KeyWord
  285. resp.ListHot = append(resp.ListHot, item)
  286. }
  287. //如果用户存在就展示历史搜索记录
  288. if user.UserId > 0 {
  289. historySearch, err := models.GetSearchKeyWordByUser(user.UserId)
  290. if err != nil {
  291. br.Msg = "获取数据失败"
  292. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  293. return
  294. }
  295. resp.ListHistory = historySearch
  296. }
  297. if len(resp.ListHistory) == 0 {
  298. resp.ListHistory = make([]*models.KeyWord, 0)
  299. }
  300. br.Msg = "获取成功!"
  301. br.Ret = 200
  302. br.Success = true
  303. br.Data = resp
  304. }