search.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. if listType == 1 || listType == 2 {
  164. //if orderColumn == "PublishDate" {
  165. // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
  166. // result = tmpResult
  167. // total = tmpTotal
  168. // err = tmpErr
  169. //} else {
  170. // tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
  171. // result = tmpResult
  172. // total = tmpTotal
  173. // err = tmpErr
  174. //}
  175. _, tmpTotal, err := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn, 0)
  176. if err != nil {
  177. br.Msg = "检索失败"
  178. br.ErrMsg = "检索失败,Err:" + err.Error()
  179. return
  180. }
  181. tmpResult, tmpTotalResult, err := services.EsArticleSearchBody(keyWord, startSize, pageSize, orderColumn, 1)
  182. if err != nil {
  183. br.Msg = "检索失败"
  184. br.ErrMsg = "检索失败,Err:" + err.Error()
  185. return
  186. }
  187. result = tmpResult
  188. fmt.Println(tmpTotalResult)
  189. if int(tmpTotalResult) < currentIndex*pageSize {
  190. startSizeBody := startSize - int(tmpTotalResult)
  191. if startSizeBody < 0 {
  192. startSizeBody = 0
  193. }
  194. var pageSizeBody int
  195. pageSizeBody = pageSize - len(tmpResult)
  196. tmpResultBody, tmpTotalBody, err := services.EsArticleSearchBody(keyWord, startSizeBody, pageSizeBody, orderColumn, 2)
  197. if err != nil {
  198. br.Msg = "检索失败"
  199. br.ErrMsg = "检索失败,Err:" + err.Error()
  200. return
  201. }
  202. for _, v := range tmpResultBody {
  203. result = append(result, v)
  204. }
  205. tmpTotalResult += tmpTotalBody
  206. }
  207. if int(tmpTotalResult) < currentIndex*pageSize {
  208. startSizeIk := startSize - int(tmpTotalResult)
  209. if startSizeIk < 0 {
  210. startSizeIk = 0
  211. }
  212. var pageSizeIk int
  213. pageSizeIk = pageSize - len(result)
  214. tmpResultIk, _, err := services.EsArticleSearch(keyWord, startSizeIk, pageSizeIk, orderColumn, 2)
  215. if err != nil {
  216. br.Msg = "检索失败"
  217. br.ErrMsg = "检索失败,Err:" + err.Error()
  218. return
  219. }
  220. for _, v := range tmpResultIk {
  221. result = append(result, v)
  222. }
  223. }
  224. total = tmpTotal
  225. if len(result) == 0 {
  226. result = make([]*models.SearchItem, 0)
  227. }
  228. for k, v := range result {
  229. //如果是研选系列的任意取五张图片的中的一张
  230. if v.CategoryId == "0" {
  231. knum := v.ArticleId % 5
  232. result[k].ImgUrlPc = researchList[knum]
  233. } else {
  234. result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  235. }
  236. result[k].Source = 1
  237. }
  238. }
  239. //ListType query int true "列表类型,1最新/全部,2 纪要 ,3图表 默认1"
  240. //记录用户搜索关键词
  241. //var source int
  242. //if listType == 1 {
  243. // source = 3
  244. //} else if listType == 2 {
  245. // source = 1
  246. //} else {
  247. // source = 2
  248. //}
  249. //如果是有效用户就记录搜索历史
  250. //if user.UserId > 0 {
  251. // go services.AddSearchKeyWord(user, keyWord, source)
  252. //}
  253. if chartTotal > int(total) {
  254. total = int64(chartTotal)
  255. }
  256. if listType == 1 {
  257. total = total + int64(chartTotal)
  258. }
  259. if len(result) == 0 {
  260. result = make([]*models.SearchItem, 0)
  261. }
  262. page := paging.GetPaging(currentIndex, pageSize, int(total))
  263. resp.Paging = page
  264. resp.List = result
  265. br.Ret = 200
  266. br.Success = true
  267. br.Msg = "获取成功"
  268. br.Data = resp
  269. }
  270. // @Title 获取搜索推荐词
  271. // @Description 获取搜索推荐词
  272. // @Success 200 {object} models.ConfigResp
  273. // @router /keyWord [get]
  274. func (this *MobileSearchController) BrowseHistoryList() {
  275. br := new(models.BaseResponse).Init()
  276. defer func() {
  277. this.Data["json"] = br
  278. this.ServeJSON()
  279. }()
  280. user := this.User
  281. if user == nil {
  282. br.Msg = "请重新登录"
  283. br.Ret = 408
  284. return
  285. }
  286. resp := new(models.ConfigResp)
  287. hotSearch, err := models.GetHotSearch()
  288. if err != nil {
  289. br.Msg = "获取数据失败"
  290. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  291. return
  292. }
  293. slicehotSearch := strings.Split(hotSearch, ",")
  294. for _, v := range slicehotSearch {
  295. item := new(models.KeyWord)
  296. item.KeyWord = v
  297. resp.ListRecommend = append(resp.ListRecommend, item)
  298. }
  299. var condition string
  300. var pars []interface{}
  301. currentTime := time.Now()
  302. starTime := currentTime.AddDate(0, 0, -8).Format("2006-01-02") + " 00:00:00"
  303. endTime := currentTime.AddDate(0, 0, -1).Format("2006-01-02") + " 23:59:59"
  304. condition += ` AND create_time < ` + "'" + endTime + "'" + `AND create_time > ` + "'" + starTime + "'"
  305. hotList, err := models.GetSearchKeyWordTop(condition, pars)
  306. if err != nil {
  307. br.Msg = "获取信息失败"
  308. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  309. return
  310. }
  311. for _, v := range hotList {
  312. item := new(models.KeyWord)
  313. item.KeyWord = v.KeyWord
  314. resp.ListHot = append(resp.ListHot, item)
  315. }
  316. //如果用户存在就展示历史搜索记录
  317. if user.UserId > 0 {
  318. historySearch, err := models.GetSearchKeyWordByUser(user.UserId)
  319. if err != nil {
  320. br.Msg = "获取数据失败"
  321. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  322. return
  323. }
  324. resp.ListHistory = historySearch
  325. }
  326. if len(resp.ListHistory) == 0 {
  327. resp.ListHistory = make([]*models.KeyWord, 0)
  328. }
  329. br.Msg = "获取成功!"
  330. br.Ret = 200
  331. br.Success = true
  332. br.Data = resp
  333. }
  334. // @Title 综合搜索接口
  335. // @Description 综合搜索接口
  336. // @Param PageSize query int true "每页数据条数"
  337. // @Param CurrentIndex query int true "当前页页码,从1开始"
  338. // @Param KeyWord query string true "搜索关键词"
  339. // @Success 200 {object} models.SearchItem
  340. // @router /comprehensive/list [get]
  341. func (this *MobileSearchController) ComprehensiveList() {
  342. br := new(models.BaseResponse).Init()
  343. defer func() {
  344. this.Data["json"] = br
  345. this.ServeJSON()
  346. }()
  347. pageSize, _ := this.GetInt("PageSize")
  348. currentIndex, _ := this.GetInt("CurrentIndex")
  349. var startSize int
  350. if pageSize <= 0 {
  351. pageSize = utils.PageSize20
  352. }
  353. if currentIndex <= 0 {
  354. currentIndex = 1
  355. }
  356. startSize = paging.StartIndex(currentIndex, pageSize)
  357. keyWord := this.GetString("KeyWord")
  358. if keyWord == "" {
  359. br.Msg = "请输入搜索词"
  360. br.ErrMsg = "请输入搜索词"
  361. return
  362. }
  363. user := this.User
  364. if user == nil {
  365. br.Msg = "请重新登录"
  366. br.Ret = 408
  367. return
  368. }
  369. resp := new(models.HomeResourceDataListNewResp)
  370. tmpResult, tmpTotalResult, err := services.EsComprehensiveSearch(keyWord, startSize, pageSize)
  371. if err != nil {
  372. br.Msg = "检索失败"
  373. br.ErrMsg = "检索失败,Err:" + err.Error()
  374. return
  375. }
  376. list, err := services.GetResourceDataEsList(tmpResult, user)
  377. if err != nil {
  378. br.Msg = "获取失败"
  379. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  380. return
  381. }
  382. if len(list) == 0 {
  383. list = make([]*models.CygxResourceDataNewResp, 0)
  384. }
  385. if currentIndex == 1 {
  386. go services.AddSearchKeyWord(user, keyWord, 1)
  387. }
  388. resp.List = list
  389. page := paging.GetPaging(currentIndex, pageSize, int(tmpTotalResult))
  390. resp.Paging = page
  391. br.Ret = 200
  392. br.Success = true
  393. br.Msg = "获取成功"
  394. br.Data = resp
  395. }