newestBidding.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package model
  2. import (
  3. "app.yhyue.com/moapp/jybase/redis"
  4. "fmt"
  5. "jyBXBase/rpc/bxbase"
  6. IC "jyBXBase/rpc/init"
  7. "strings"
  8. "time"
  9. MC "app.yhyue.com/moapp/jybase/common"
  10. ME "app.yhyue.com/moapp/jybase/encrypt"
  11. elastic "app.yhyue.com/moapp/jybase/es"
  12. "app.yhyue.com/moapp/jybase/mongodb"
  13. "app.yhyue.com/moapp/jybase/mysql"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. "go.mongodb.org/mongo-driver/bson/primitive"
  16. )
  17. const (
  18. search_index = "bidding"
  19. search_type = "bidding"
  20. mongodb_fields = `{"_id":1,"area":1,"publishtime":1,"s_subscopeclass":1,"subtype":1,"title":1,"toptype":1,"type":1, "buyerclass":1,"budget":1,"bidamount":1,"s_winner":1,"bidopentime":1,"buyer":1,"projectname":1,"spidercode":1,"site":1}`
  21. query = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","spidercode","site"],"from":0,"size":%d}`
  22. multi_match = `{"multi_match": {"query": %s,"type": "phrase", "fields": ["title"]}}`
  23. query_bool_must = `{"terms":{"%s":[%s]}}`
  24. query_bool_must_and = `{"bool":{"must":[%s],"must_not":[%s]}}`
  25. search_field = `"_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","isValidFile","spidercode","site"`
  26. query_city_hkeys = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match":%d}},"highlight": {"pre_tags": ["<a>"],"post_tags": ["</a>"],"fields": {"title": {"fragment_size": 0,"number_of_fragments": 1}}},"_source":[` + search_field + `],"sort":[{"publishtime":"desc"},{"budget":"desc"}],"from":0,"size":50}`
  27. Pushbidding = "global_common_data.dws_f_bid_baseinfo"
  28. Province = "province"
  29. )
  30. type NewestInfo struct {
  31. TableName string
  32. UserId string
  33. MysqlDb *mysql.Mysql
  34. NewUserId int64
  35. PushMysql *mysql.Mysql
  36. }
  37. var mysqlTables = map[string]string{
  38. "f": "push.pushsubscribe",
  39. "v": "push.pushsubscribe",
  40. "m": "push.pushmember",
  41. "e": "push.pushentniche",
  42. }
  43. func GetNewestInfo(userId, userType string, newUserId int64) *NewestInfo {
  44. nt := &NewestInfo{
  45. UserId: userId,
  46. TableName: mysqlTables[userType],
  47. MysqlDb: IC.BaseServiceMysql,
  48. NewUserId: newUserId,
  49. }
  50. return nt
  51. }
  52. // GetPushHistoryCount 缓存code 配置最新的redis code;可以感觉redis内存大小 调节redis存储;
  53. // 根据GetPushHistoryCount 返回值 作为列表缓存key中一个参数
  54. func (n *NewestInfo) GetPushHistoryCount() int64 {
  55. countKey := fmt.Sprintf("push_count_%s_%s", n.TableName, n.UserId)
  56. if b := redis.GetInt("other", countKey); b > 0 {
  57. return int64(b)
  58. }
  59. countSql := "select count(1) from %s a where a.userid=%d order by a.id desc"
  60. countSql = fmt.Sprintf(countSql, n.TableName, n.NewUserId)
  61. if c := n.MysqlDb.CountBySql(countSql); c > 0 {
  62. redis.Put("other", countKey, c, 60) //过期时间走配置,比最新标讯列表缓存时间 要短1/10 尽量不超过两分钟
  63. return c
  64. }
  65. return 0
  66. }
  67. func (n *NewestInfo) GetPushHistory() (res []*bxbase.NewestList) {
  68. findSQL := "select a.infoid,REPLACE(a.matchkeys,'+',' ') as matchkeys,a.attachment_count,a.budget,a.bidamount from %s a where a.userid=%d and a.date>=%d order by a.id desc limit 50"
  69. findSQL = fmt.Sprintf(findSQL, n.TableName, n.NewUserId, time.Now().AddDate(0, 0, -7).Unix())
  70. logx.Info(n.TableName, "-------", n.NewUserId, ",findSQL:", findSQL)
  71. list := n.MysqlDb.SelectBySql(findSQL)
  72. if len(*list) > 0 && list != nil {
  73. m := map[string]bool{}
  74. es_ids := []string{}
  75. infos := map[string]*bxbase.NewestList{}
  76. for _, v := range *list {
  77. infoId := MC.ObjToString(v["infoid"])
  78. if m[infoId] {
  79. continue
  80. }
  81. es_ids = append(es_ids, infoId)
  82. m[MC.ObjToString(v["infoid"])] = true
  83. //
  84. infos[infoId] = &bxbase.NewestList{
  85. Id: ME.EncodeArticleId2ByCheck(MC.ObjToString(v["infoid"])),
  86. Matchkeys: strings.Split(MC.ObjToString(v["matchkeys"]), " "),
  87. Budget: MC.Int64All(v["budget"]),
  88. Bidamount: MC.Int64All(v["bidamount"]),
  89. FileExists: MC.Int64All(v["attachment_count"]) > 0,
  90. }
  91. }
  92. if len(es_ids) > 0 {
  93. list := elastic.Get(search_index, search_type, fmt.Sprintf(query, strings.Join(es_ids, `","`), len(es_ids)))
  94. if list != nil {
  95. for _, v := range *list {
  96. _id := MC.ObjToString(v["_id"])
  97. bn := infos[_id]
  98. bn.Title = MC.ObjToString(v["title"])
  99. bn.PublishTime = MC.Int64All(v["publishtime"])
  100. bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
  101. bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
  102. bn.Buyerclass = MC.ObjToString(v["buyerclass"])
  103. bn.City = MC.ObjToString(v["city"])
  104. bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
  105. bn.SpiderCode = MC.ObjToString(v["spidercode"])
  106. bn.Site = MC.ObjToString(v["site"])
  107. }
  108. }
  109. }
  110. //mongodb bidding
  111. mgo_ids := []primitive.ObjectID{}
  112. for _, v := range es_ids {
  113. if infos[v].Title == "" {
  114. mgo_ids = append(mgo_ids, mongodb.StringTOBsonId(v))
  115. }
  116. }
  117. if len(mgo_ids) > 0 {
  118. list, ok := IC.MgoBidding.Find("bidding", map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_ids}}, nil, mongodb_fields, false, -1, -1)
  119. if ok && *list != nil {
  120. for _, v := range *list {
  121. _id := mongodb.BsonIdToSId(v["_id"])
  122. bn := infos[_id]
  123. bn.Title = MC.ObjToString(v["title"])
  124. bn.PublishTime = MC.Int64All(v["publishtime"])
  125. bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
  126. bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
  127. bn.Buyerclass = MC.ObjToString(v["buyerclass"])
  128. bn.City = MC.ObjToString(v["city"])
  129. bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
  130. bn.SpiderCode = MC.ObjToString(v["spidercode"])
  131. bn.Site = MC.ObjToString(v["site"])
  132. }
  133. }
  134. }
  135. //mongodb bidding_back
  136. mgo_back_ids := []primitive.ObjectID{}
  137. for _, v := range mgo_ids {
  138. if infos[mongodb.BsonIdToSId(v)].Title == "" {
  139. mgo_back_ids = append(mgo_back_ids, v)
  140. }
  141. }
  142. if len(mgo_back_ids) > 0 {
  143. list, ok := IC.MgoBidding.Find("bidding_back", map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_back_ids}}, nil, mongodb_fields, false, -1, -1)
  144. if ok && *list != nil {
  145. for _, v := range *list {
  146. _id := mongodb.BsonIdToSId(v["_id"])
  147. bn := infos[_id]
  148. bn.Title = MC.ObjToString(v["title"])
  149. bn.PublishTime = MC.Int64All(v["publishtime"])
  150. bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string)
  151. bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string)
  152. bn.Buyerclass = MC.ObjToString(v["buyerclass"])
  153. bn.City = MC.ObjToString(v["city"])
  154. bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string)
  155. bn.SpiderCode = MC.ObjToString(v["spidercode"])
  156. bn.Site = MC.ObjToString(v["site"])
  157. }
  158. }
  159. }
  160. //
  161. for _, v := range infos {
  162. res = append(res, v)
  163. }
  164. }
  165. return
  166. }
  167. // 根据定位或者搜索历史 查es
  168. func NewestQuery(city, keys, subtype string) (str string) {
  169. var musts, bools []string
  170. if keys != "" {
  171. for _, v := range strings.Split(keys, ",") {
  172. keys := strings.Split(v, " ") //历史搜索 空格划分
  173. must_tmp := []string{}
  174. for _, key := range keys {
  175. must_tmp = append(must_tmp, fmt.Sprintf(multi_match, "\""+key+"\""))
  176. }
  177. bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(must_tmp, ","), ""))
  178. }
  179. }
  180. if city != "" {
  181. musts = append(musts, fmt.Sprintf(query_bool_must, "city", `"`+city+`"`))
  182. }
  183. //未登录首页推送数据限制
  184. if subtype != "" {
  185. musts = append(musts, fmt.Sprintf(query_bool_must, "subtype", subtype))
  186. }
  187. minimum_should_match := 0
  188. if len(bools) > 0 {
  189. minimum_should_match = 1
  190. }
  191. str = fmt.Sprintf(query_city_hkeys, strings.Join(musts, ","), strings.Join(bools, ","), minimum_should_match)
  192. logx.Info("str:", str)
  193. return
  194. }
  195. // es查询
  196. func NewestES(doSearchStr string) (res []*bxbase.NewestList) {
  197. list := elastic.Get(search_index, search_type, doSearchStr)
  198. if list != nil && len(*list) > 0 {
  199. for _, v := range *list {
  200. _id := mongodb.BsonIdToSId(v["_id"])
  201. isValidFile, _ := v["isValidFile"].(bool)
  202. res = append(res, &bxbase.NewestList{
  203. Id: ME.EncodeArticleId2ByCheck(_id),
  204. Title: MC.ObjToString(v["title"]),
  205. Subtype: MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string),
  206. Area: MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string),
  207. Buyerclass: MC.ObjToString(v["buyerclass"]),
  208. City: MC.ObjToString(v["city"]),
  209. Industry: MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string),
  210. Budget: MC.Int64All(v["budget"]),
  211. Bidamount: MC.Int64All(v["bidamount"]),
  212. FileExists: isValidFile, //附件
  213. PublishTime: MC.Int64All(v["publishtime"]),
  214. Site: MC.ObjToString(v["site"]),
  215. SpiderCode: MC.ObjToString(v["spidercode"]),
  216. })
  217. }
  218. }
  219. return
  220. }