getsearchlistlogic.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package logic
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
  6. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init"
  7. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/service"
  8. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/util"
  9. "context"
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/internal/svc"
  15. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/type/bxcore"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. "log"
  18. )
  19. type GetSearchListLogic struct {
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. logx.Logger
  23. }
  24. func NewGetSearchListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSearchListLogic {
  25. return &GetSearchListLogic{
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. Logger: logx.WithContext(ctx),
  29. }
  30. }
  31. const (
  32. TaskActiveId = 16
  33. TaskActivityCacheKey = "activity_day_task_%v_%v_%v"
  34. TaskActivityCacheTimeout = 60 * 60 * 3
  35. TaskIdSearch = 3
  36. )
  37. // GetSearchList 标讯搜索结果列表数据
  38. func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.SearchResp, error) {
  39. defer MC.Catch()
  40. res := &bxcore.SearchData{
  41. Count: 0,
  42. List: []*bxcore.SearchList{},
  43. InterceptLimit: int64(MC.IntAllDef(IC.C.KeywordsLimit, 35)),
  44. }
  45. //初始化搜索对象
  46. ks := service.NewKeyWordsSearch()
  47. log.Println("req:", in)
  48. // 未登录用户只允许翻页和筛选搜索范围 其他参数无效
  49. if in.UserId == "" {
  50. in = &bxcore.SearchReq{
  51. AppId: in.AppId,
  52. PageNum: in.PageNum,
  53. PageSize: in.PageSize,
  54. SelectType: in.SelectType,
  55. KeyWords: in.KeyWords,
  56. Platform: in.Platform,
  57. UserType: in.UserType,
  58. SearchGroup: in.SearchGroup,
  59. Subtype: in.Subtype,
  60. TopType: in.TopType,
  61. }
  62. // 搜索范围只允许筛选 标题和正文 其他的过滤掉
  63. var selectType []string
  64. selectTypeSplit := strings.Split(in.SelectType, ",")
  65. // 未登录用户只能搜标题和正文Spli
  66. for i := 0; i < len(selectTypeSplit); i++ {
  67. if selectTypeSplit[i] == "title" || selectTypeSplit[i] == "content" {
  68. selectType = append(selectType, selectTypeSplit[i])
  69. }
  70. }
  71. if len(selectType) == 0 {
  72. selectType = []string{"title", "content"}
  73. }
  74. //
  75. in.SelectType = strings.Join(selectType, ",")
  76. }
  77. isWhite, _ := service.IsOnTheWhitelist(in.UserId, in.MgoUserId)
  78. //模糊检索数重新格式化
  79. nin := &bxcore.SearchReq{
  80. AppId: in.AppId,
  81. UserId: in.UserId,
  82. Phone: in.Phone,
  83. NewUserId: in.NewUserId,
  84. EntId: in.EntId,
  85. EntUserId: in.EntUserId,
  86. PageNum: in.PageNum,
  87. PageSize: in.PageSize,
  88. Province: in.Province,
  89. City: in.City,
  90. Subtype: in.Subtype,
  91. TopType: in.TopType,
  92. PublishTime: in.PublishTime,
  93. SelectType: in.SelectType,
  94. Price: in.Price,
  95. Industry: in.Industry,
  96. BuyerClass: in.BuyerClass,
  97. BuyerTel: in.BuyerTel,
  98. WinnerTel: in.WinnerTel,
  99. FileExists: in.FileExists,
  100. SearchGroup: in.SearchGroup,
  101. SearchMode: 1,
  102. WordsMode: in.WordsMode,
  103. KeyWords: in.KeyWords,
  104. AdditionalWords: in.AdditionalWords,
  105. ExclusionWords: in.ExclusionWords,
  106. UserType: in.UserType,
  107. Platform: in.Platform,
  108. BidField: in.BidField,
  109. PositionType: in.PositionType,
  110. PositionId: in.PositionId,
  111. AccountId: in.AccountId,
  112. MgoUserId: in.MgoUserId,
  113. LimitFlag: in.LimitFlag,
  114. IsNew: in.IsNew,
  115. District: in.District,
  116. Buyer: in.Buyer,
  117. Winner: in.Winner,
  118. Agency: in.Agency,
  119. IsFuzzyNumber: true,
  120. PropertyForm: in.PropertyForm,
  121. ExpireTime: in.ExpireTime,
  122. SubInformation: in.SubInformation,
  123. Period: in.Period,
  124. ChangeHand: in.ChangeHand,
  125. Scale: in.Scale,
  126. MobileTag: in.MobileTag,
  127. EntAccountId: in.EntAccountId,
  128. UserAgent: in.UserAgent,
  129. }
  130. //处理搜索条件
  131. heightWords := ks.SearchParamsHandle(in, isWhite)
  132. if (in.PageNum < 0 && in.PageSize < 0) || ks.IsSearch == "F" { //没有权限
  133. return &bxcore.SearchResp{
  134. Data: res,
  135. ErrMsg: "",
  136. ErrCode: 0,
  137. }, nil
  138. }
  139. //判断是否是空搜索,如果是空搜索,查缓存数据
  140. if ks.IsEmptySearch(in, isWhite) {
  141. log.Println("空查询分组", in.SearchGroup)
  142. res.List, res.Count, res.Total = ks.GetBidSearchListByCache(in)
  143. return &bxcore.SearchResp{
  144. Data: res,
  145. ErrMsg: "",
  146. ErrCode: 0,
  147. }, nil
  148. } else {
  149. log.Println("userId", in.UserName, in.UserId)
  150. if in.UserId != "" {
  151. key := fmt.Sprintf(TaskActivityCacheKey, in.PositionId, TaskActiveId, TaskIdSearch)
  152. if exist, _ := redis.Exists("other", key); !exist {
  153. redis.Put("other", key, 1, GetLastTime())
  154. nsqNode := jy.Jyweb_node2
  155. if in.Platform == "WX" {
  156. nsqNode = jy.Jywx_node1
  157. } else if in.Platform == "APP" || in.Platform == "H5" {
  158. nsqNode = jy.Jyapp_node1
  159. }
  160. positionId, _ := strconv.ParseInt(in.PositionId, 10, 64)
  161. log.Println(IC.MgoLog, IC.C.Nsq, IC.C.NsqTopic)
  162. err := jy.Publish(IC.MgoLog, IC.C.Nsq, IC.C.NsqTopic, "lottery_draw_task", in.UserId, nsqNode, map[string]interface{}{
  163. "phone": in.Phone,
  164. "userId": in.UserId,
  165. "mgoUserId": in.MgoUserId,
  166. "positionId": positionId,
  167. "activeId": TaskActiveId,
  168. "taskInfoId": TaskIdSearch,
  169. "nickName": in.UserName,
  170. })
  171. if err != nil {
  172. log.Println("nsq队列写入失败-->", err, "lottery_draw_task", TaskIdSearch, in.UserId)
  173. } else {
  174. log.Println("nsq队列写入成功-->", "lottery_draw_task", TaskIdSearch, in.UserId, map[string]interface{}{
  175. "phone": in.Phone,
  176. "userId": in.UserId,
  177. "mgoUserId": in.MgoUserId,
  178. "positionId": in.PositionId,
  179. "activeId": TaskActiveId,
  180. "taskInfoId": TaskIdSearch,
  181. "nickName": in.UserName,
  182. })
  183. }
  184. }
  185. }
  186. }
  187. //异常付费用户 参数不是免费fType,但是又不是付费用户;还有一种是未登录用户 稍后处理
  188. if in.UserType != "fType" && !in.IsPay {
  189. return &bxcore.SearchResp{
  190. ErrCode: -1,
  191. ErrMsg: "无权限",
  192. }, nil
  193. }
  194. t := time.Now()
  195. //招标信息有效查询
  196. res.IsLimit = 1
  197. //查询数据
  198. searchLimit := util.IsSearchLimit(strings.Split(in.SelectType, ","))
  199. //全文检索限制
  200. if searchLimit {
  201. var warnMsg string
  202. //res.IsLimit = util.IsLimited(in.LimitFlag, in.UserId, in.UserType != "fType", in.IsNew)
  203. res.IsLimit, warnMsg = util.CheckLimit(util.UserIdentMap[func(userId string, isPay bool) string {
  204. switch {
  205. case isPay:
  206. return "payer"
  207. case userId != "":
  208. return "free"
  209. default:
  210. return "noLogin"
  211. }
  212. }(in.UserId, in.IsPay)], in.LimitFlag, in.IsNew)
  213. if res.IsLimit == 1 { //没有被限制
  214. defer util.Limit()
  215. }
  216. if warnMsg != "" {
  217. util.SendMsgByWXURL(warnMsg)
  218. }
  219. }
  220. //无限制
  221. if res.IsLimit == 1 {
  222. //付费用户搜索优化--默认搜索5年数据,数据量太多,接口反应太慢,前两页数据 时间范围根据配置缩小查询以达到快速查询的目的。
  223. publishTime := in.PublishTime
  224. if b := util.IsOptimize(IC.C, in); b {
  225. t1 := time.Now()
  226. res.Count, res.Total, res.List = ks.GetBidSearchList(in) // util.GetBidSearchData(in)
  227. log.Println("1查询耗时:", time.Since(t1))
  228. }
  229. //如果优化查询数据量太少,和配置数据量作比较,不够的话走原始查询
  230. if res.Count < IC.C.PaySearchLimit.PageSize {
  231. t2 := time.Now()
  232. in.PublishTime = publishTime
  233. res.Count, res.Total, res.List = ks.GetBidSearchList(in)
  234. log.Println("2查询耗时:", time.Since(t2))
  235. }
  236. // 精准模式不足50条的非空标讯查询 匹配模糊查询数
  237. ikWords := util.HttpEs(in.KeyWords, "ik_smart", IC.DB.Es.Addr)
  238. log.Println(ikWords)
  239. var isReset bool
  240. if res.Count < IC.C.PaySearchLimit.PrecisionNum && in.SearchMode == 0 && in.KeyWords != "" {
  241. isReset = true
  242. ks.SearchParamsHandle(nin, isWhite)
  243. res.BCount = service.GetBidSearchCount(nin)
  244. log.Println("精准查询数据不足 匹配模糊查询数:", res.BCount)
  245. }
  246. //4.3.新增:“项目名称/标的物”搜索结果广告
  247. if (in.UserType == "fType" || !in.IsPay) && res.Count > IC.C.SearchLimit {
  248. if !isReset {
  249. ks.SearchParamsHandle(nin, isWhite)
  250. }
  251. nin.SelectType = "purchasing,projectname.pname"
  252. nin.PageSize, nin.PageNum = 1, 1
  253. _, total, data := ks.GetBidSearchList(nin)
  254. if total > IC.C.SubjectLimit {
  255. res.SCount, res.SList = total, data
  256. }
  257. }
  258. res.KeyWords = strings.Join(heightWords, " ")
  259. res.InterceptOtherWords = in.InterceptOtherWords
  260. res.InterceptKeyWords = in.InterceptKeyWords
  261. }
  262. log.Println("关键词 -全部- 查询耗时:", time.Since(t).Seconds())
  263. return &bxcore.SearchResp{
  264. Data: res,
  265. ErrMsg: "",
  266. ErrCode: 0,
  267. }, nil
  268. }
  269. func GetLastTime() int {
  270. n := TaskActivityCacheTimeout
  271. t := time.Now()
  272. midnight := time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  273. if midnight.After(t) {
  274. if int(midnight.Unix()-t.Unix()) > n {
  275. return n
  276. } else {
  277. return int(midnight.Unix() - t.Unix())
  278. }
  279. }
  280. return n
  281. }