search.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. package controllers
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/paging"
  4. "hongze/hongze_cygx/models"
  5. "hongze/hongze_cygx/services"
  6. "hongze/hongze_cygx/utils"
  7. "strings"
  8. "time"
  9. )
  10. type SearchController struct {
  11. BaseAuthController
  12. }
  13. type BaseSearchController struct {
  14. BaseCommonController
  15. }
  16. // @Title 搜索接口
  17. // @Description 搜索接口
  18. // @Param PageSize query int true "每页数据条数"
  19. // @Param CurrentIndex query int true "当前页页码,从1开始"
  20. // @Param KeyWord query string true "搜索关键词"
  21. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  22. // @Success 200 {object} models.SearchItem
  23. // @router /list [get]
  24. func (this *SearchController) SearchList() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. pageSize, _ := this.GetInt("PageSize")
  31. currentIndex, _ := this.GetInt("CurrentIndex")
  32. var startSize int
  33. if pageSize <= 0 {
  34. pageSize = utils.PageSize20
  35. }
  36. if currentIndex <= 0 {
  37. currentIndex = 1
  38. }
  39. startSize = paging.StartIndex(currentIndex, pageSize)
  40. keyWord := this.GetString("KeyWord")
  41. orderColumn := this.GetString("OrderColumn")
  42. if keyWord == "" {
  43. br.Msg = "请输入搜索词"
  44. br.ErrMsg = "请输入搜索词"
  45. return
  46. }
  47. user := this.User
  48. if user == nil {
  49. br.Msg = "请重新登录"
  50. br.Ret = 408
  51. return
  52. }
  53. //研选的五张图片
  54. detailResearch, errConfig := models.GetConfigByCode("category_research_img_url")
  55. if errConfig != nil {
  56. br.Msg = "获取数据失败"
  57. br.ErrMsg = "获取数据研选分类图片失败,Err:" + errConfig.Error()
  58. return
  59. }
  60. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  61. //对应分类的所图片
  62. detailCategoryUrl, errConfig := models.GetConfigByCode("category_map_img_url")
  63. if errConfig != nil {
  64. br.Msg = "获取数据失败"
  65. br.ErrMsg = "行业配置信息失败,Err:" + errConfig.Error()
  66. return
  67. }
  68. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  69. mapCategoryUrl := make(map[string]string)
  70. var categoryId string
  71. var imgUrlChart string
  72. for _, v := range categoryUrlList {
  73. vslice := strings.Split(v, "_")
  74. categoryId = vslice[0]
  75. imgUrlChart = vslice[len(vslice)-1]
  76. mapCategoryUrl[categoryId] = imgUrlChart
  77. }
  78. if orderColumn == "" {
  79. orderColumn = "Matching"
  80. }
  81. indexName := utils.IndexName
  82. pageSize = 20
  83. var result []*models.SearchItem
  84. var total int64
  85. var err error
  86. if orderColumn == "PublishDate" {
  87. tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
  88. result = tmpResult
  89. total = tmpTotal
  90. err = tmpErr
  91. } else {
  92. tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
  93. result = tmpResult
  94. total = tmpTotal
  95. err = tmpErr
  96. }
  97. if err != nil {
  98. br.Msg = "检索失败"
  99. br.ErrMsg = "检索失败,Err:" + err.Error()
  100. return
  101. }
  102. if len(result) == 0 {
  103. result = make([]*models.SearchItem, 0)
  104. }
  105. detail, errKey := models.GetNewSearchKeyWordByThisUser(user.UserId, keyWord)
  106. if errKey != nil && errKey.Error() != utils.ErrNoRow() {
  107. br.Msg = "获取信息失败"
  108. br.ErrMsg = "获取信息失败,Err:" + errKey.Error()
  109. return
  110. }
  111. //同一个用户一分钟之内搜索的词不重复记录
  112. if detail == nil || time.Now().After(detail.CreateTime.Add(+time.Minute*1)) {
  113. //fmt.Println("一分钟之内没有搜索")
  114. keyWordItem := new(models.CygxSearchKeyWord)
  115. keyWordItem.UserId = user.UserId
  116. keyWordItem.KeyWord = keyWord
  117. keyWordItem.CreateTime = time.Now()
  118. go models.AddSearchKeyWord(keyWordItem)
  119. //添加到用户搜索记录总表导出记录时使用
  120. userkeyWordItem := new(models.CygxUserSearchKeyWord)
  121. userkeyWordItem.UserId = user.UserId
  122. userkeyWordItem.KeyWord = keyWord
  123. userkeyWordItem.PageType = "HomeSearch"
  124. userkeyWordItem.CreateTime = time.Now()
  125. go models.AddUserSearchKeyWord(userkeyWordItem)
  126. }
  127. for k, v := range result {
  128. //如果是研选系列的任意取五张图片的中的一张
  129. if v.CategoryId == "0" {
  130. knum := v.ArticleId % 5
  131. result[k].ImgUrlPc = researchList[knum]
  132. } else {
  133. result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  134. }
  135. }
  136. resp := new(models.SearchResp)
  137. page := paging.GetPaging(currentIndex, pageSize, int(total))
  138. resp.Paging = page
  139. resp.List = result
  140. br.Ret = 200
  141. br.Success = true
  142. br.Msg = "获取成功"
  143. br.Data = resp
  144. }
  145. //https://blog.csdn.net/my_miuye/article/details/110496025
  146. //search
  147. // @Title 报告搜索接口
  148. // @Description 报告搜索接口
  149. // @Param PageSize query int true "每页数据条数"
  150. // @Param CurrentIndex query int true "当前页页码,从1开始"
  151. // @Param KeyWord query string true "搜索关键词"
  152. // @Success 200 {object} models.SearchItem
  153. // @router /report/list [get]
  154. func (this *SearchController) SearchReport() {
  155. br := new(models.BaseResponse).Init()
  156. defer func() {
  157. this.Data["json"] = br
  158. this.ServeJSON()
  159. }()
  160. pageSize, _ := this.GetInt("PageSize")
  161. currentIndex, _ := this.GetInt("CurrentIndex")
  162. var startSize int
  163. if pageSize <= 0 {
  164. pageSize = utils.PageSize20
  165. }
  166. if currentIndex <= 0 {
  167. currentIndex = 1
  168. }
  169. startSize = paging.StartIndex(currentIndex, pageSize)
  170. keyWord := this.GetString("KeyWord")
  171. if keyWord == "" {
  172. br.Msg = "请输入搜索词"
  173. br.ErrMsg = "请输入搜索词"
  174. return
  175. }
  176. user := this.User
  177. if user == nil {
  178. br.Msg = "请重新登录"
  179. br.Ret = 408
  180. return
  181. }
  182. //indexName := "article_list"
  183. indexName := utils.IndexName
  184. pageSize = 100
  185. var result []*models.SearchItem
  186. var total int64
  187. var err error
  188. tmpResult, tmpTotal, tmpErr := services.EsSearchReport(indexName, keyWord, startSize, pageSize, user.UserId)
  189. result = tmpResult
  190. total = tmpTotal
  191. err = tmpErr
  192. if err != nil {
  193. br.Msg = "检索失败"
  194. br.ErrMsg = "检索失败,Err:" + err.Error()
  195. return
  196. }
  197. if len(result) == 0 {
  198. result = make([]*models.SearchItem, 0)
  199. }
  200. detail, errKey := models.GetNewSearchKeyWordByThisUser(user.UserId, keyWord)
  201. if errKey != nil && errKey.Error() != utils.ErrNoRow() {
  202. br.Msg = "获取信息失败"
  203. br.ErrMsg = "获取信息失败,Err:" + errKey.Error()
  204. return
  205. }
  206. //同一个用户一分钟之内搜索的词不重复记录
  207. if detail == nil || time.Now().After(detail.CreateTime.Add(+time.Minute*1)) {
  208. //fmt.Println("一分钟之内没有搜索")
  209. keyWordItem := new(models.CygxSearchKeyWord)
  210. keyWordItem.UserId = user.UserId
  211. keyWordItem.KeyWord = keyWord
  212. keyWordItem.CreateTime = time.Now()
  213. go models.AddSearchKeyWord(keyWordItem)
  214. }
  215. resp := new(models.SearchResp)
  216. page := paging.GetPaging(currentIndex, pageSize, int(total))
  217. resp.Paging = page
  218. resp.List = result
  219. br.Ret = 200
  220. br.Success = true
  221. br.Msg = "获取成功"
  222. br.Data = resp
  223. }
  224. // @Title 搜索接口(无需token)
  225. // @Description 搜索接口(无需token)
  226. // @Param PageSize query int true "每页数据条数"
  227. // @Param CurrentIndex query int true "当前页页码,从1开始"
  228. // @Param KeyWord query string true "搜索关键词"
  229. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  230. // @Param CompanyCode query string false "机构编号"
  231. // @Param CompanyName query string false "机构名称"
  232. // @Param Email query string false "邮箱"
  233. // @Param Sign query string false "加密签名"
  234. // @Success 200 {object} models.SearchItem
  235. // @router /listPublic [get]
  236. func (this *BaseSearchController) SearchListPublic() {
  237. br := new(models.BaseResponse).Init()
  238. defer func() {
  239. this.Data["json"] = br
  240. this.ServeJSON()
  241. }()
  242. pageSize, _ := this.GetInt("PageSize")
  243. currentIndex, _ := this.GetInt("CurrentIndex")
  244. var startSize int
  245. if pageSize <= 0 {
  246. pageSize = utils.PageSize20
  247. }
  248. if currentIndex <= 0 {
  249. currentIndex = 1
  250. }
  251. startSize = paging.StartIndex(currentIndex, pageSize)
  252. keyWord := this.GetString("KeyWord")
  253. orderColumn := this.GetString("OrderColumn")
  254. if keyWord == "" {
  255. br.Msg = "请输入搜索词"
  256. br.ErrMsg = "请输入搜索词"
  257. return
  258. }
  259. companyCode := this.GetString("CompanyCode")
  260. companyNameHt := this.GetString("CompanyName")
  261. email := this.GetString("Email")
  262. sign := this.GetString("Sign")
  263. if orderColumn == "" {
  264. orderColumn = "Matching"
  265. }
  266. indexName := utils.IndexName
  267. pageSize = 20
  268. var result []*models.SearchItem
  269. var total int64
  270. var err error
  271. if orderColumn == "PublishDate" {
  272. tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, 0)
  273. result = tmpResult
  274. total = tmpTotal
  275. err = tmpErr
  276. } else {
  277. tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, 0, orderColumn)
  278. result = tmpResult
  279. total = tmpTotal
  280. err = tmpErr
  281. }
  282. if err != nil {
  283. br.Msg = "检索失败"
  284. br.ErrMsg = "检索失败,Err:" + err.Error()
  285. return
  286. }
  287. if len(result) == 0 {
  288. result = make([]*models.SearchItem, 0)
  289. }
  290. if companyCode != "" && companyNameHt != "" && email != "" {
  291. errMsg, errHt, wxUser, _ := services.CheckHtgj(companyCode, companyNameHt, email, sign)
  292. if errHt != nil {
  293. br.Msg = "获取"
  294. br.ErrMsg = "获取数据失败,Err:" + errHt.Error()
  295. return
  296. }
  297. if errMsg != "" {
  298. br.Msg = errMsg
  299. return
  300. }
  301. user := wxUser
  302. detail, errKey := models.GetNewSearchKeyWordByThisUser(user.UserId, keyWord)
  303. if errKey != nil && errKey.Error() != utils.ErrNoRow() {
  304. br.Msg = "获取信息失败"
  305. br.ErrMsg = "获取信息失败,Err:" + errKey.Error()
  306. return
  307. }
  308. //同一个用户一分钟之内搜索的词不重复记录
  309. if detail == nil || time.Now().After(detail.CreateTime.Add(+time.Minute*1)) {
  310. keyWordItem := new(models.CygxSearchKeyWord)
  311. keyWordItem.UserId = user.UserId
  312. keyWordItem.KeyWord = keyWord
  313. keyWordItem.CreateTime = time.Now()
  314. go models.AddSearchKeyWord(keyWordItem)
  315. }
  316. }
  317. resp := new(models.SearchResp)
  318. page := paging.GetPaging(currentIndex, pageSize, int(total))
  319. resp.Paging = page
  320. resp.List = result
  321. br.Ret = 200
  322. br.Success = true
  323. br.Msg = "获取成功"
  324. br.Data = resp
  325. }
  326. // @Title 搜索接口
  327. // @Description 搜索接口
  328. // @Param PageSize query int true "每页数据条数"
  329. // @Param CurrentIndex query int true "当前页页码,从1开始"
  330. // @Param KeyWord query string true "搜索关键词"
  331. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  332. // @Param ListType query int true "列表类型,1最新,2 纪要 ,3图表 默认1"
  333. // @Success 200 {object} models.SearchItem
  334. // @router /artAndChart/list [get]
  335. func (this *SearchController) ListHomeArtAndChart() {
  336. br := new(models.BaseResponse).Init()
  337. defer func() {
  338. this.Data["json"] = br
  339. this.ServeJSON()
  340. }()
  341. pageSize, _ := this.GetInt("PageSize")
  342. currentIndex, _ := this.GetInt("CurrentIndex")
  343. listType, _ := this.GetInt("ListType")
  344. var startSize int
  345. if pageSize <= 0 {
  346. pageSize = utils.PageSize20
  347. }
  348. if currentIndex <= 0 {
  349. currentIndex = 1
  350. }
  351. startSize = paging.StartIndex(currentIndex, pageSize)
  352. keyWord := this.GetString("KeyWord")
  353. orderColumn := this.GetString("OrderColumn")
  354. if keyWord == "" {
  355. br.Msg = "请输入搜索词"
  356. br.ErrMsg = "请输入搜索词"
  357. return
  358. }
  359. user := this.User
  360. if user == nil {
  361. br.Msg = "请重新登录"
  362. br.Ret = 408
  363. return
  364. }
  365. //研选的五张图片
  366. detailResearch, errConfig := models.GetConfigByCode("category_research_img_url")
  367. if errConfig != nil {
  368. br.Msg = "获取数据失败"
  369. br.ErrMsg = "获取数据研选分类图片失败,Err:" + errConfig.Error()
  370. return
  371. }
  372. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  373. //对应分类的所图片
  374. detailCategoryUrl, errConfig := models.GetConfigByCode("category_map_img_url")
  375. if errConfig != nil {
  376. br.Msg = "获取数据失败"
  377. br.ErrMsg = "行业配置信息失败,Err:" + errConfig.Error()
  378. return
  379. }
  380. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  381. mapCategoryUrl := make(map[string]string)
  382. var categoryId string
  383. var imgUrlChart string
  384. for _, v := range categoryUrlList {
  385. vslice := strings.Split(v, "_")
  386. categoryId = vslice[0]
  387. imgUrlChart = vslice[len(vslice)-1]
  388. mapCategoryUrl[categoryId] = imgUrlChart
  389. }
  390. if orderColumn == "" {
  391. orderColumn = "Matching"
  392. }
  393. indexName := utils.IndexName
  394. pageSize = 20
  395. var chartTotal int
  396. resp := new(models.SearchResp)
  397. //page := paging.GetPaging(currentIndex, pageSize, total)
  398. var chartList []*models.HomeChartListResp
  399. var err error
  400. var condition string
  401. var pars []interface{}
  402. if listType == 1 || listType == 3 {
  403. if listType == 1 {
  404. pageSize = 100
  405. }
  406. if currentIndex <= 2 {
  407. condition = ` AND title LIKE '%` + keyWord + `%'`
  408. chartList, err = models.GetChartList(condition, pars, startSize, pageSize)
  409. if err != nil {
  410. br.Msg = "获取信息失败"
  411. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  412. return
  413. }
  414. chartTotal, err = models.GetChartCount(condition, pars)
  415. if err != nil {
  416. br.Msg = "获取信息失败"
  417. br.Msg = "获取帖子总数失败,Err:" + err.Error()
  418. return
  419. }
  420. }
  421. }
  422. for k, _ := range chartList {
  423. chartList[k].IsNeedJump = true
  424. chartList[k].Source = 2
  425. }
  426. if len(chartList) == 0 {
  427. chartList = make([]*models.HomeChartListResp, 0)
  428. }
  429. resp.ChartList = chartList
  430. var result []*models.SearchItem
  431. var total int64
  432. if listType == 1 || listType == 2 {
  433. if orderColumn == "PublishDate" {
  434. tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
  435. result = tmpResult
  436. total = tmpTotal
  437. err = tmpErr
  438. } else {
  439. tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
  440. result = tmpResult
  441. total = tmpTotal
  442. err = tmpErr
  443. }
  444. if err != nil {
  445. br.Msg = "检索失败"
  446. br.ErrMsg = "检索失败,Err:" + err.Error()
  447. return
  448. }
  449. if len(result) == 0 {
  450. result = make([]*models.SearchItem, 0)
  451. }
  452. for k, v := range result {
  453. //如果是研选系列的任意取五张图片的中的一张
  454. if v.CategoryId == "0" {
  455. knum := v.ArticleId % 5
  456. result[k].ImgUrlPc = researchList[knum]
  457. } else {
  458. result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  459. }
  460. result[k].Source = 1
  461. }
  462. }
  463. detail, errKey := models.GetNewSearchKeyWordByThisUser(user.UserId, keyWord)
  464. if errKey != nil && errKey.Error() != utils.ErrNoRow() {
  465. br.Msg = "获取信息失败"
  466. br.ErrMsg = "获取信息失败,Err:" + errKey.Error()
  467. return
  468. }
  469. //同一个用户一分钟之内搜索的词不重复记录
  470. if detail == nil || time.Now().After(detail.CreateTime.Add(+time.Minute*1)) {
  471. //fmt.Println("一分钟之内没有搜索")
  472. keyWordItem := new(models.CygxSearchKeyWord)
  473. keyWordItem.UserId = user.UserId
  474. keyWordItem.KeyWord = keyWord
  475. keyWordItem.CreateTime = time.Now()
  476. go models.AddSearchKeyWord(keyWordItem)
  477. }
  478. if chartTotal > int(total) {
  479. total = int64(chartTotal)
  480. }
  481. page := paging.GetPaging(currentIndex, pageSize, int(total))
  482. resp.Paging = page
  483. resp.List = result
  484. br.Ret = 200
  485. br.Success = true
  486. br.Msg = "获取成功"
  487. br.Data = resp
  488. }