search.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. "time"
  10. )
  11. type SearchController struct {
  12. BaseAuthController
  13. }
  14. type BaseSearchController struct {
  15. BaseCommonController
  16. }
  17. type MobileSearchController struct {
  18. BaseAuthMobileController
  19. }
  20. // @Title 搜索接口
  21. // @Description 搜索接口
  22. // @Param PageSize query int false "每页数据条数"
  23. // @Param CurrentIndex query int false "当前页页码,从1开始"
  24. // @Param KeyWord query string true "搜索关键词"
  25. // @Param OrderColumn query string false "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  26. // @Param ListType query int false "列表类型,1最新/全部,2 纪要 ,3图表 默认1"
  27. // @Success 200 {object} models.SearchResp
  28. // @router /list [get]
  29. func (this *MobileSearchController) ListHomeArtAndChart() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. pageSize, _ := this.GetInt("PageSize")
  36. currentIndex, _ := this.GetInt("CurrentIndex")
  37. listType, _ := this.GetInt("ListType")
  38. var startSize int
  39. if pageSize <= 0 {
  40. pageSize = utils.PageSize20
  41. }
  42. if currentIndex <= 0 {
  43. currentIndex = 1
  44. }
  45. if listType == 0 {
  46. listType = 1
  47. }
  48. startSize = paging.StartIndex(currentIndex, pageSize)
  49. keyWord := this.GetString("KeyWord")
  50. orderColumn := this.GetString("OrderColumn")
  51. if keyWord == "" {
  52. br.Msg = "请输入搜索词"
  53. br.ErrMsg = "请输入搜索词"
  54. return
  55. }
  56. //添加映射关系
  57. keyWord = strings.ToUpper(keyWord)
  58. keyWordDetail, _ := models.GetCygxCygxIkWordMapDetail(keyWord)
  59. if keyWordDetail != nil {
  60. keyWord = keyWordDetail.KeyWordMap
  61. }
  62. user := this.User
  63. if user == nil {
  64. br.Msg = "请重新登录"
  65. br.Ret = 408
  66. return
  67. }
  68. //研选的五张图片
  69. detailResearch, errConfig := models.GetConfigByCode("category_research_img_url")
  70. if errConfig != nil {
  71. br.Msg = "获取数据失败"
  72. br.ErrMsg = "获取数据研选分类图片失败,Err:" + errConfig.Error()
  73. return
  74. }
  75. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  76. //对应分类的所图片
  77. detailCategoryUrl, errConfig := models.GetConfigByCode("category_map_img_url")
  78. if errConfig != nil {
  79. br.Msg = "获取数据失败"
  80. br.ErrMsg = "行业配置信息失败,Err:" + errConfig.Error()
  81. return
  82. }
  83. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  84. mapCategoryUrl := make(map[string]string)
  85. var categoryId string
  86. var imgUrlChart string
  87. for _, v := range categoryUrlList {
  88. vslice := strings.Split(v, "_")
  89. categoryId = vslice[0]
  90. imgUrlChart = vslice[len(vslice)-1]
  91. mapCategoryUrl[categoryId] = imgUrlChart
  92. }
  93. if orderColumn == "" {
  94. orderColumn = "Matching"
  95. }
  96. orderColumn = "Matching"
  97. //indexName := utils.IndexName
  98. //pageSize = 20
  99. var chartTotal int
  100. resp := new(models.SearchResp)
  101. //page := paging.GetPaging(currentIndex, pageSize, total)
  102. var chartList []*models.HomeChartListResp
  103. var err error
  104. var condition string
  105. var pars []interface{}
  106. if listType == 1 || listType == 3 {
  107. //if listType == 1 {
  108. // pageSize = 100
  109. //}
  110. if currentIndex <= 2 {
  111. condition = ` AND title LIKE '%` + keyWord + `%'`
  112. chartList, err = models.GetChartList(condition, pars, startSize, pageSize)
  113. if err != nil {
  114. br.Msg = "获取信息失败"
  115. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  116. return
  117. }
  118. chartTotal, err = models.GetChartCount(condition, pars)
  119. if err != nil {
  120. br.Msg = "获取信息失败"
  121. br.Msg = "获取帖子总数失败,Err:" + err.Error()
  122. return
  123. }
  124. }
  125. }
  126. for k, v := range chartList {
  127. chartList[k].IsNeedJump = true
  128. chartList[k].Source = 2
  129. if v.PtagName != "" {
  130. chartList[k].CtagNamePc = v.PtagName
  131. }
  132. if v.CtagName != "" {
  133. chartList[k].CtagNamePc += "," + v.CtagName
  134. }
  135. if v.PtagNameTwo != "" {
  136. chartList[k].CtagNamePc += "," + v.PtagNameTwo
  137. }
  138. if v.CtagNameTwo != "" {
  139. chartList[k].CtagNamePc += "," + v.CtagNameTwo
  140. }
  141. if v.PtagName != "" {
  142. labelItem := new(models.LabelList)
  143. labelItem.PtagName = v.PtagName
  144. labelItem.CtagName = v.CtagName
  145. chartList[k].LabelList = append(chartList[k].LabelList, labelItem)
  146. }
  147. if v.PtagNameTwo != "" {
  148. labelItemTwo := new(models.LabelList)
  149. labelItemTwo.PtagName = v.PtagNameTwo
  150. labelItemTwo.CtagName = v.CtagNameTwo
  151. chartList[k].LabelList = append(chartList[k].LabelList, labelItemTwo)
  152. }
  153. if len(chartList[k].LabelList) == 0 {
  154. chartList[k].LabelList = make([]*models.LabelList, 0)
  155. }
  156. }
  157. if len(chartList) == 0 {
  158. chartList = make([]*models.HomeChartListResp, 0)
  159. }
  160. resp.ChartList = chartList
  161. var result []*models.SearchItem
  162. var total int64
  163. var articleIds []int
  164. if listType == 1 || listType == 2 {
  165. //if orderColumn == "PublishDate" {
  166. // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
  167. // result = tmpResult
  168. // total = tmpTotal
  169. // err = tmpErr
  170. //} else {
  171. // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
  172. // result = tmpResult
  173. // total = tmpTotal
  174. // err = tmpErr
  175. //}
  176. _, tmpTotal, err := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn, 0)
  177. if err != nil {
  178. br.Msg = "检索失败"
  179. br.ErrMsg = "检索失败,Err:" + err.Error()
  180. return
  181. }
  182. tmpResult, tmpTotalResult, err := services.EsArticleSearchBody(keyWord, startSize, pageSize, orderColumn, 1)
  183. if err != nil {
  184. br.Msg = "检索失败"
  185. br.ErrMsg = "检索失败,Err:" + err.Error()
  186. return
  187. }
  188. result = tmpResult
  189. fmt.Println(tmpTotalResult)
  190. if int(tmpTotalResult) < currentIndex*pageSize {
  191. startSizeBody := startSize - int(tmpTotalResult)
  192. if startSizeBody < 0 {
  193. startSizeBody = 0
  194. }
  195. var pageSizeBody int
  196. pageSizeBody = pageSize - len(tmpResult)
  197. tmpResultBody, tmpTotalBody, err := services.EsArticleSearchBody(keyWord, startSizeBody, pageSizeBody, orderColumn, 2)
  198. if err != nil {
  199. br.Msg = "检索失败"
  200. br.ErrMsg = "检索失败,Err:" + err.Error()
  201. return
  202. }
  203. for _, v := range tmpResultBody {
  204. result = append(result, v)
  205. }
  206. tmpTotalResult += tmpTotalBody
  207. }
  208. if int(tmpTotalResult) < currentIndex*pageSize {
  209. startSizeIk := startSize - int(tmpTotalResult)
  210. if startSizeIk < 0 {
  211. startSizeIk = 0
  212. }
  213. var pageSizeIk int
  214. pageSizeIk = pageSize - len(result)
  215. tmpResultIk, _, err := services.EsArticleSearch(keyWord, startSizeIk, pageSizeIk, orderColumn, 2)
  216. if err != nil {
  217. br.Msg = "检索失败"
  218. br.ErrMsg = "检索失败,Err:" + err.Error()
  219. return
  220. }
  221. for _, v := range tmpResultIk {
  222. result = append(result, v)
  223. }
  224. }
  225. total = tmpTotal
  226. if len(result) == 0 {
  227. result = make([]*models.SearchItem, 0)
  228. }
  229. for k, v := range result {
  230. //如果是研选系列的任意取五张图片的中的一张
  231. if v.CategoryId == "0" {
  232. knum := v.ArticleId % 5
  233. result[k].ImgUrlPc = researchList[knum]
  234. } else {
  235. result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  236. }
  237. result[k].Source = 1
  238. articleIds = append(articleIds, v.ArticleId)
  239. }
  240. }
  241. articleMapPv := services.GetArticleHistoryByArticleId(articleIds) //文章Pv
  242. //ListType query int true "列表类型,1最新/全部,2 纪要 ,3图表 默认1"
  243. //记录用户搜索关键词
  244. //var source int
  245. //if listType == 1 {
  246. // source = 3
  247. //} else if listType == 2 {
  248. // source = 1
  249. //} else {
  250. // source = 2
  251. //}
  252. //如果是有效用户就记录搜索历史
  253. //if user.UserId > 0 {
  254. // go services.AddSearchKeyWord(user, keyWord, source)
  255. //}
  256. if chartTotal > int(total) {
  257. total = int64(chartTotal)
  258. }
  259. if listType == 1 {
  260. total = total + int64(chartTotal)
  261. }
  262. if len(result) == 0 {
  263. result = make([]*models.SearchItem, 0)
  264. } else {
  265. yxArticleIdMap := services.GetYxArticleIdMap(articleIds)
  266. for _, v := range result {
  267. v.IsResearch = yxArticleIdMap[v.ArticleId] // 添加是否属于研选的标识
  268. v.Pv = articleMapPv[v.ArticleId] // 添加Pv
  269. }
  270. }
  271. page := paging.GetPaging(currentIndex, pageSize, int(total))
  272. resp.Paging = page
  273. resp.List = result
  274. br.Ret = 200
  275. br.Success = true
  276. br.Msg = "获取成功"
  277. br.Data = resp
  278. }
  279. // @Title 获取搜索推荐词
  280. // @Description 获取搜索推荐词
  281. // @Success 200 {object} models.ConfigResp
  282. // @router /keyWord [get]
  283. func (this *MobileSearchController) BrowseHistoryList() {
  284. br := new(models.BaseResponse).Init()
  285. defer func() {
  286. this.Data["json"] = br
  287. this.ServeJSON()
  288. }()
  289. user := this.User
  290. if user == nil {
  291. br.Msg = "请重新登录"
  292. br.Ret = 408
  293. return
  294. }
  295. resp := new(models.ConfigResp)
  296. hotSearch, err := models.GetHotSearch()
  297. if err != nil {
  298. br.Msg = "获取数据失败"
  299. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  300. return
  301. }
  302. slicehotSearch := strings.Split(hotSearch, ",")
  303. for _, v := range slicehotSearch {
  304. item := new(models.KeyWord)
  305. item.KeyWord = v
  306. resp.ListRecommend = append(resp.ListRecommend, item)
  307. }
  308. var condition string
  309. var pars []interface{}
  310. currentTime := time.Now()
  311. starTime := currentTime.AddDate(0, 0, -8).Format("2006-01-02") + " 00:00:00"
  312. endTime := currentTime.AddDate(0, 0, -1).Format("2006-01-02") + " 23:59:59"
  313. condition += ` AND create_time < ` + "'" + endTime + "'" + `AND create_time > ` + "'" + starTime + "'"
  314. hotList, err := models.GetSearchKeyWordTop(condition, pars)
  315. if err != nil {
  316. br.Msg = "获取信息失败"
  317. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  318. return
  319. }
  320. for _, v := range hotList {
  321. item := new(models.KeyWord)
  322. item.KeyWord = v.KeyWord
  323. resp.ListHot = append(resp.ListHot, item)
  324. }
  325. //如果用户存在就展示历史搜索记录
  326. if user.UserId > 0 {
  327. historySearch, err := models.GetSearchKeyWordByUser(user.UserId)
  328. if err != nil {
  329. br.Msg = "获取数据失败"
  330. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  331. return
  332. }
  333. resp.ListHistory = historySearch
  334. }
  335. if len(resp.ListHistory) == 0 {
  336. resp.ListHistory = make([]*models.KeyWord, 0)
  337. }
  338. br.Msg = "获取成功!"
  339. br.Ret = 200
  340. br.Success = true
  341. br.Data = resp
  342. }
  343. // @Title 综合搜索接口
  344. // @Description 综合搜索接口
  345. // @Param PageSize query int true "每页数据条数"
  346. // @Param CurrentIndex query int true "当前页页码,从1开始"
  347. // @Param KeyWord query string true "搜索关键词"
  348. // @Success 200 {object} models.SearchItem
  349. // @router /comprehensive/list [get]
  350. func (this *MobileSearchController) ComprehensiveList() {
  351. br := new(models.BaseResponse).Init()
  352. defer func() {
  353. this.Data["json"] = br
  354. this.ServeJSON()
  355. }()
  356. pageSize, _ := this.GetInt("PageSize")
  357. currentIndex, _ := this.GetInt("CurrentIndex")
  358. var startSize int
  359. if pageSize <= 0 {
  360. pageSize = utils.PageSize20
  361. }
  362. if currentIndex <= 0 {
  363. currentIndex = 1
  364. }
  365. startSize = paging.StartIndex(currentIndex, pageSize)
  366. keyWord := this.GetString("KeyWord")
  367. if keyWord == "" {
  368. br.Msg = "请输入搜索词"
  369. br.ErrMsg = "请输入搜索词"
  370. return
  371. }
  372. user := this.User
  373. if user == nil {
  374. br.Msg = "请重新登录"
  375. br.Ret = 408
  376. return
  377. }
  378. resp := new(models.HomeResourceDataListNewResp)
  379. tmpResult, tmpTotalResult, err := services.SqlComprehensiveSearch(keyWord, startSize, pageSize)
  380. if err != nil {
  381. br.Msg = "检索失败"
  382. br.ErrMsg = "检索失败,Err:" + err.Error()
  383. return
  384. }
  385. list, err := services.GetResourceDataEsList(tmpResult, user)
  386. if err != nil {
  387. br.Msg = "获取失败"
  388. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  389. return
  390. }
  391. if len(list) == 0 {
  392. list = make([]*models.CygxResourceDataNewResp, 0)
  393. }
  394. if currentIndex == 1 {
  395. go services.AddSearchKeyWord(user, keyWord, 1)
  396. }
  397. resp.List = list
  398. page := paging.GetPaging(currentIndex, pageSize, int(tmpTotalResult))
  399. resp.Paging = page
  400. br.Ret = 200
  401. br.Success = true
  402. br.Msg = "获取成功"
  403. br.Data = resp
  404. }