search.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. for k, v := range result {
  121. //如果是研选系列的任意取五张图片的中的一张
  122. if v.CategoryId == "0" {
  123. knum := v.ArticleId % 5
  124. result[k].ImgUrlPc = researchList[knum]
  125. } else {
  126. result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  127. }
  128. }
  129. resp := new(models.SearchResp)
  130. page := paging.GetPaging(currentIndex, pageSize, int(total))
  131. resp.Paging = page
  132. resp.List = result
  133. br.Ret = 200
  134. br.Success = true
  135. br.Msg = "获取成功"
  136. br.Data = resp
  137. }
  138. //https://blog.csdn.net/my_miuye/article/details/110496025
  139. //search
  140. // @Title 报告搜索接口
  141. // @Description 报告搜索接口
  142. // @Param PageSize query int true "每页数据条数"
  143. // @Param CurrentIndex query int true "当前页页码,从1开始"
  144. // @Param KeyWord query string true "搜索关键词"
  145. // @Success 200 {object} models.SearchItem
  146. // @router /report/list [get]
  147. func (this *SearchController) SearchReport() {
  148. br := new(models.BaseResponse).Init()
  149. defer func() {
  150. this.Data["json"] = br
  151. this.ServeJSON()
  152. }()
  153. pageSize, _ := this.GetInt("PageSize")
  154. currentIndex, _ := this.GetInt("CurrentIndex")
  155. var startSize int
  156. if pageSize <= 0 {
  157. pageSize = utils.PageSize20
  158. }
  159. if currentIndex <= 0 {
  160. currentIndex = 1
  161. }
  162. startSize = paging.StartIndex(currentIndex, pageSize)
  163. keyWord := this.GetString("KeyWord")
  164. if keyWord == "" {
  165. br.Msg = "请输入搜索词"
  166. br.ErrMsg = "请输入搜索词"
  167. return
  168. }
  169. user := this.User
  170. if user == nil {
  171. br.Msg = "请重新登录"
  172. br.Ret = 408
  173. return
  174. }
  175. //indexName := "article_list"
  176. indexName := utils.IndexName
  177. pageSize = 100
  178. var result []*models.SearchItem
  179. var total int64
  180. var err error
  181. tmpResult, tmpTotal, tmpErr := services.EsSearchReport(indexName, keyWord, startSize, pageSize, user.UserId)
  182. result = tmpResult
  183. total = tmpTotal
  184. err = tmpErr
  185. if err != nil {
  186. br.Msg = "检索失败"
  187. br.ErrMsg = "检索失败,Err:" + err.Error()
  188. return
  189. }
  190. if len(result) == 0 {
  191. result = make([]*models.SearchItem, 0)
  192. }
  193. detail, errKey := models.GetNewSearchKeyWordByThisUser(user.UserId, keyWord)
  194. if errKey != nil && errKey.Error() != utils.ErrNoRow() {
  195. br.Msg = "获取信息失败"
  196. br.ErrMsg = "获取信息失败,Err:" + errKey.Error()
  197. return
  198. }
  199. //同一个用户一分钟之内搜索的词不重复记录
  200. if detail == nil || time.Now().After(detail.CreateTime.Add(+time.Minute*1)) {
  201. //fmt.Println("一分钟之内没有搜索")
  202. keyWordItem := new(models.CygxSearchKeyWord)
  203. keyWordItem.UserId = user.UserId
  204. keyWordItem.KeyWord = keyWord
  205. keyWordItem.CreateTime = time.Now()
  206. go models.AddSearchKeyWord(keyWordItem)
  207. }
  208. resp := new(models.SearchResp)
  209. page := paging.GetPaging(currentIndex, pageSize, int(total))
  210. resp.Paging = page
  211. resp.List = result
  212. br.Ret = 200
  213. br.Success = true
  214. br.Msg = "获取成功"
  215. br.Data = resp
  216. }
  217. // @Title 搜索接口(无需token)
  218. // @Description 搜索接口(无需token)
  219. // @Param PageSize query int true "每页数据条数"
  220. // @Param CurrentIndex query int true "当前页页码,从1开始"
  221. // @Param KeyWord query string true "搜索关键词"
  222. // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
  223. // @Param CompanyCode query string false "机构编号"
  224. // @Param CompanyName query string false "机构名称"
  225. // @Param Email query string false "邮箱"
  226. // @Param Sign query string false "加密签名"
  227. // @Success 200 {object} models.SearchItem
  228. // @router /listPublic [get]
  229. func (this *BaseSearchController) SearchListPublic() {
  230. br := new(models.BaseResponse).Init()
  231. defer func() {
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. pageSize, _ := this.GetInt("PageSize")
  236. currentIndex, _ := this.GetInt("CurrentIndex")
  237. var startSize int
  238. if pageSize <= 0 {
  239. pageSize = utils.PageSize20
  240. }
  241. if currentIndex <= 0 {
  242. currentIndex = 1
  243. }
  244. startSize = paging.StartIndex(currentIndex, pageSize)
  245. keyWord := this.GetString("KeyWord")
  246. orderColumn := this.GetString("OrderColumn")
  247. if keyWord == "" {
  248. br.Msg = "请输入搜索词"
  249. br.ErrMsg = "请输入搜索词"
  250. return
  251. }
  252. companyCode := this.GetString("CompanyCode")
  253. companyNameHt := this.GetString("CompanyName")
  254. email := this.GetString("Email")
  255. sign := this.GetString("Sign")
  256. if orderColumn == "" {
  257. orderColumn = "Matching"
  258. }
  259. indexName := utils.IndexName
  260. pageSize = 20
  261. var result []*models.SearchItem
  262. var total int64
  263. var err error
  264. if orderColumn == "PublishDate" {
  265. tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, 0)
  266. result = tmpResult
  267. total = tmpTotal
  268. err = tmpErr
  269. } else {
  270. tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, 0, orderColumn)
  271. result = tmpResult
  272. total = tmpTotal
  273. err = tmpErr
  274. }
  275. if err != nil {
  276. br.Msg = "检索失败"
  277. br.ErrMsg = "检索失败,Err:" + err.Error()
  278. return
  279. }
  280. if len(result) == 0 {
  281. result = make([]*models.SearchItem, 0)
  282. }
  283. if companyCode != "" && companyNameHt != "" && email != "" {
  284. errMsg, errHt, wxUser, _ := services.CheckHtgj(companyCode, companyNameHt, email, sign)
  285. if errHt != nil {
  286. br.Msg = "获取"
  287. br.ErrMsg = "获取数据失败,Err:" + errHt.Error()
  288. return
  289. }
  290. if errMsg != "" {
  291. br.Msg = errMsg
  292. return
  293. }
  294. user := wxUser
  295. detail, errKey := models.GetNewSearchKeyWordByThisUser(user.UserId, keyWord)
  296. if errKey != nil && errKey.Error() != utils.ErrNoRow() {
  297. br.Msg = "获取信息失败"
  298. br.ErrMsg = "获取信息失败,Err:" + errKey.Error()
  299. return
  300. }
  301. //同一个用户一分钟之内搜索的词不重复记录
  302. if detail == nil || time.Now().After(detail.CreateTime.Add(+time.Minute*1)) {
  303. keyWordItem := new(models.CygxSearchKeyWord)
  304. keyWordItem.UserId = user.UserId
  305. keyWordItem.KeyWord = keyWord
  306. keyWordItem.CreateTime = time.Now()
  307. go models.AddSearchKeyWord(keyWordItem)
  308. }
  309. }
  310. resp := new(models.SearchResp)
  311. page := paging.GetPaging(currentIndex, pageSize, int(total))
  312. resp.Paging = page
  313. resp.List = result
  314. br.Ret = 200
  315. br.Success = true
  316. br.Msg = "获取成功"
  317. br.Data = resp
  318. }