push.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package util
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/encrypt"
  6. "app.yhyue.com/moapp/jybase/esv1"
  7. "app.yhyue.com/moapp/jybase/mongodb"
  8. "app.yhyue.com/moapp/jybase/mysql"
  9. "app.yhyue.com/moapp/jybase/redis"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. "go.mongodb.org/mongo-driver/bson/primitive"
  14. IC "jyBXSubscribe/rpc/init"
  15. "jyBXSubscribe/rpc/type/bxsubscribe"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. //
  21. const (
  22. pageSize = 50
  23. pageSizes = 10
  24. AllSubPushCacheSize = 250
  25. query = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext"],"from":0,"size":%d}`
  26. 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}`
  27. SubFreeFlag = "fType"
  28. SubVipFlag = "vType"
  29. MemberFlag = "mType"
  30. EntnicheFlag = "eType"
  31. oneDay = 24 * 60 * 60
  32. )
  33. var aboutDbMsg map[string]*AboutDbMsg = map[string]*AboutDbMsg{
  34. SubFreeFlag: &AboutDbMsg{"pushsubscribe", "subpush"},
  35. SubVipFlag: &AboutDbMsg{"pushsubscribe", "subpush"},
  36. MemberFlag: &AboutDbMsg{"pushmember", "memberpush"},
  37. EntnicheFlag: &AboutDbMsg{"pushentniche", "pushentniche"},
  38. }
  39. type AboutDbMsg struct {
  40. MysqlTable string
  41. RedisKeyFlag string
  42. }
  43. type SubPush struct {
  44. Date string
  45. Datas []*bxsubscribe.SubscribeInfo
  46. Count int64
  47. }
  48. type PushCa struct {
  49. Date int64
  50. InfoId string
  51. Visit int
  52. Index int64
  53. Keys []string
  54. Type int
  55. Isvip int
  56. FileExists bool
  57. }
  58. //查询参数
  59. type SubPushQueryParam struct {
  60. Mgo_bidding mongodb.MongodbSim //
  61. Bidding string //
  62. Bidding_back string //
  63. PushMysql *mysql.Mysql //
  64. UserId string //用户id
  65. PageNum int //页面
  66. PageSize int //每页数量
  67. SelectTime string //时间
  68. Area string //区域
  69. City string //城市
  70. Buyerclass string //采购单位行业
  71. Subtype string //信息类型 二级分类
  72. Subscopeclass string //信息行业
  73. Key string //订阅词
  74. Export bool //导出
  75. EntId int //企业id
  76. Price string //价格
  77. FileExists string //是否有附件;默认全部;1:有附件;-1:无附件
  78. }
  79. func (spqp *SubPushQueryParam) IsEmpty() bool {
  80. return spqp.SelectTime == "" && spqp.Area == "" && spqp.City == "" && spqp.Buyerclass == "" && spqp.Subscopeclass == "" && spqp.Subtype == "" && spqp.Key == "" && spqp.Price == "" && spqp.FileExists == ""
  81. }
  82. type subscribePush struct {
  83. ModuleFlag string
  84. }
  85. func NewSubscribePush(module ...string) *subscribePush {
  86. m := ""
  87. if len(module) > 0 {
  88. m = module[0]
  89. }
  90. return &subscribePush{m}
  91. }
  92. //从pushcache_2_a中取
  93. func (h *subscribePush) GetTodayCache(userId string) (*SubPush, error) {
  94. pc_a, err := redis.GetNewBytes("pushcache_2_b", h.todayKey(userId))
  95. if err != nil {
  96. return nil, err
  97. }
  98. if pc_a == nil {
  99. return nil, nil
  100. }
  101. var p *SubPush
  102. if err := json.Unmarshal(*pc_a, &p); err != nil {
  103. return nil, err
  104. }
  105. return p, nil
  106. }
  107. //往pushcache_2_a中放
  108. func (h *subscribePush) PutTodayCache(userId string, pc_a *SubPush) {
  109. redis.Put("pushcache_2_b", h.todayKey(userId), pc_a, oneDay)
  110. }
  111. //获取redis key
  112. func (s *subscribePush) todayKey(userId string) string {
  113. return fmt.Sprintf("%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, userId)
  114. }
  115. func (s *subscribePush) allKey(userId string) string {
  116. logx.Info(fmt.Sprintf("all_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, userId))
  117. return fmt.Sprintf("all_%s_%s", aboutDbMsg[s.ModuleFlag].RedisKeyFlag, userId)
  118. }
  119. //历史推送记录中单条信息格式化
  120. func (s *subscribePush) InfoFormat(p *PushCa, info *map[string]interface{}) *bxsubscribe.SubscribeInfo {
  121. area := common.ObjToString((*info)["area"])
  122. if area == "A" {
  123. area = "全国"
  124. }
  125. industry := common.ObjToString((*info)["s_subscopeclass"])
  126. scs := strings.Split(industry, ",")
  127. if len(scs) > 0 {
  128. industry = scs[0]
  129. if industry != "" {
  130. iss := strings.Split(industry, "_")
  131. if len(iss) > 0 {
  132. industry = iss[0]
  133. }
  134. }
  135. }
  136. infotype := common.ObjToString((*info)["subtype"])
  137. if infotype == "" {
  138. infotype = common.ObjToString((*info)["toptype"])
  139. }
  140. if infotype == "" {
  141. infotype = common.ObjToString((*info)["type"])
  142. if infotype == "tender" {
  143. infotype = "招标"
  144. } else if infotype == "bid" {
  145. infotype = "中标"
  146. }
  147. }
  148. _id := p.InfoId
  149. if _id == "" {
  150. _id = common.ObjToString((*info)["_id"])
  151. }
  152. return &bxsubscribe.SubscribeInfo{
  153. Id: encrypt.EncodeArticleId2ByCheck(_id),
  154. Title: common.ObjToString((*info)["title"]),
  155. Area: area,
  156. BuyerClass: common.ObjToString((*info)["buyerclass"]),
  157. Subtype: infotype,
  158. Industry: industry,
  159. PublishTime: common.Int64All((*info)["publishtime"]),
  160. CaIndex: p.Index,
  161. CaDate: p.Date,
  162. CaIsvisit: int64(p.Visit),
  163. CaIsvip: int64(p.Isvip),
  164. CaType: int64(p.Type),
  165. MatchKeys: p.Keys,
  166. Budget: common.ObjToString((*info)["budget"]),
  167. BidAmount: common.ObjToString((*info)["bidamount"]),
  168. Buyer: common.ObjToString((*info)["buyer"]),
  169. ProjectName: common.ObjToString((*info)["projectname"]),
  170. Winner: common.ObjToString((*info)["s_winner"]),
  171. BidOpenTime: common.ObjToString((*info)["bidopentime"]),
  172. CaFileExists: p.FileExists,
  173. }
  174. }
  175. func (s *subscribePush) Datas(spqp *SubPushQueryParam) (hasNextPage bool, total int64, result []*bxsubscribe.SubscribeInfo) {
  176. logx.Info(spqp.UserId, s.ModuleFlag, "subscribePush query param:", "SelectTime:", spqp.SelectTime, "Area:", spqp.Area, "City:", spqp.City, "Subtype:", spqp.Subtype, "Subscopeclass:", spqp.Subscopeclass, "Buyerclass:", spqp.Buyerclass, "Key:", spqp.Key, "PageNum:", spqp.PageNum, "Price:", spqp.Price, "FileExists:", spqp.FileExists)
  177. if spqp.UserId == "" {
  178. return
  179. }
  180. if spqp.PageNum < 1 {
  181. spqp.PageNum = 1
  182. }
  183. if spqp.PageSize < 1 || spqp.PageSize > pageSize {
  184. if !spqp.Export {
  185. spqp.PageSize = pageSize
  186. } else { //数据导出查询20000条限制
  187. if spqp.PageSize < 1 || spqp.PageSize > 20000 {
  188. spqp.PageSize = 20000
  189. }
  190. }
  191. }
  192. starttime, endtime := int64(0), int64(0)
  193. st, et := "", ""
  194. now := time.Now()
  195. logx.Info(4444)
  196. if spqp.SelectTime == "today" { //今天
  197. starttime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  198. } else if spqp.SelectTime == "yesterday" { //昨天
  199. starttime = time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local).Unix()
  200. endtime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  201. } else if spqp.SelectTime == "lately-7" { //最近7天
  202. starttime = time.Date(now.Year(), now.Month(), now.Day()-7, 0, 0, 0, 0, time.Local).Unix()
  203. } else if spqp.SelectTime == "lately-30" { //最近30天
  204. starttime = time.Date(now.Year(), now.Month(), now.Day()-30, 0, 0, 0, 0, time.Local).Unix()
  205. } else if spqp.SelectTime == "lastyear" { //去年
  206. starttime = time.Date(now.Year()-1, 1, 1, 0, 0, 0, 0, time.Local).Unix()
  207. endtime = time.Date(now.Year()-1, 12, 31, 23, 59, 59, 0, time.Local).Unix()
  208. } else if len(strings.Split(spqp.SelectTime, "_")) == 2 {
  209. st = strings.Split(spqp.SelectTime, "_")[0]
  210. starttime, _ = strconv.ParseInt(st, 0, 64)
  211. et = strings.Split(spqp.SelectTime, "_")[1]
  212. endtime, _ = strconv.ParseInt(et, 0, 64)
  213. if endtime > 0 {
  214. etTime := time.Unix(endtime, 0)
  215. endtime = time.Date(etTime.Year(), etTime.Month(), etTime.Day(), 23, 59, 59, 0, time.Local).Unix()
  216. }
  217. }
  218. logx.Info(2222)
  219. nowFormat := date.NowFormat(date.Date_Short_Layout)
  220. start := (spqp.PageNum - 1) * spqp.PageSize
  221. end := start + spqp.PageSize
  222. //时间是今天,没有别的过滤条件
  223. if nowFormat == date.FormatDateByInt64(&starttime, date.Date_Short_Layout) && spqp.Area == "" && spqp.City == "" && spqp.Buyerclass == "" && spqp.Subscopeclass == "" && spqp.Subtype == "" && spqp.Key == "" && spqp.Price == "" && spqp.FileExists == "" {
  224. logx.Info("a1")
  225. subPush, err := s.GetTodayCache(spqp.UserId)
  226. if err != nil {
  227. logx.Info(spqp.UserId, "GetTodayCache Error", err)
  228. }
  229. if err != nil || subPush == nil || subPush.Date != nowFormat || len(subPush.Datas) == 0 {
  230. list, countSearch := s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, false)
  231. subPush = &SubPush{
  232. Date: nowFormat,
  233. Datas: list,
  234. Count: countSearch,
  235. }
  236. s.PutTodayCache(spqp.UserId, subPush)
  237. }
  238. length := len(subPush.Datas)
  239. if end > length {
  240. end = length
  241. }
  242. if start < length {
  243. result = subPush.Datas[start:end]
  244. }
  245. total = int64(length)
  246. } else if spqp.IsEmpty() && (spqp.PageNum-1)*spqp.PageSize <= 250 { //全部,没有过滤条件 之前缓存5页*50条=250
  247. logx.Info("a2")
  248. allCache, err := s.GetAllCache(spqp.UserId)
  249. if err != nil {
  250. logx.Info(spqp.UserId, "GetAllCache Error", err)
  251. }
  252. if err != nil || allCache == nil || allCache.Date != nowFormat || len(allCache.Datas) == 0 {
  253. spqp.PageNum = 1
  254. logx.Info(1111)
  255. list, countSearch := s.getDatasFromMysql(spqp, starttime, endtime, AllSubPushCacheSize, true)
  256. allCache = &SubPush{
  257. Date: nowFormat,
  258. Datas: list,
  259. Count: countSearch,
  260. }
  261. s.PutAllCache(spqp.UserId, allCache)
  262. }
  263. length := len(allCache.Datas)
  264. if end > length {
  265. end = length
  266. }
  267. if start < length {
  268. result = allCache.Datas[start:end]
  269. }
  270. total = allCache.Count
  271. } else {
  272. logx.Info("a4")
  273. result, total = s.getDatasFromMysql(spqp, starttime, endtime, spqp.PageSize, true)
  274. }
  275. logx.Info("-------------------------------------------------", len(result))
  276. if result == nil {
  277. result = []*bxsubscribe.SubscribeInfo{}
  278. }
  279. hasNextPage = len(result) >= spqp.PageSize
  280. return
  281. }
  282. //
  283. func (s *subscribePush) getDatasFromMysql(spqp *SubPushQueryParam, starttime, endtime int64, size int, isLimit bool) (result []*bxsubscribe.SubscribeInfo, count int64) {
  284. querys := []string{fmt.Sprintf("userid='%s'", spqp.UserId)}
  285. //时间
  286. if starttime > 0 && endtime > 0 {
  287. querys = append(querys, fmt.Sprintf("date>=%d and date<=%d", starttime, endtime))
  288. } else if starttime > 0 && endtime == 0 {
  289. querys = append(querys, fmt.Sprintf("date>=%d", starttime))
  290. } else if starttime == 0 && endtime > 0 {
  291. querys = append(querys, fmt.Sprintf("date<=%d", endtime))
  292. }
  293. if spqp.Area != "" || spqp.City != "" {
  294. var sqlAreaCity = ""
  295. //城市
  296. city := []string{}
  297. for _, v := range strings.Split(spqp.City, ",") {
  298. if IC.PushMapping.City[v] > 0 {
  299. city = append(city, fmt.Sprint(IC.PushMapping.City[v]))
  300. } else {
  301. city = append(city, "-1")
  302. }
  303. }
  304. if len(city) == 1 {
  305. city = append(city, "9999")
  306. }
  307. if len(city) > 0 {
  308. sqlAreaCity = fmt.Sprintf("city in (%s)", strings.Join(city, ","))
  309. }
  310. //区域
  311. var sqlArea = ""
  312. area := []string{}
  313. for _, v := range strings.Split(spqp.Area, ",") {
  314. if IC.PushMapping.Area[v] > 0 {
  315. area = append(area, fmt.Sprint(IC.PushMapping.Area[v]))
  316. } else {
  317. area = append(area, "-1")
  318. }
  319. }
  320. if len(area) == 1 {
  321. area = append(area, "9999")
  322. }
  323. if len(area) > 0 {
  324. sqlArea = fmt.Sprintf("area in (%s)", strings.Join(area, ","))
  325. }
  326. if sqlAreaCity != "" && sqlArea != "" {
  327. sqlAreaCity = "( " + sqlAreaCity + " or " + sqlArea + " )"
  328. } else if sqlAreaCity == "" && sqlArea != "" {
  329. sqlAreaCity = sqlArea
  330. }
  331. if sqlAreaCity != "" {
  332. querys = append(querys, sqlAreaCity)
  333. }
  334. }
  335. //采购单位行业
  336. if spqp.Buyerclass != "" {
  337. buyerclass := []string{}
  338. for _, v := range strings.Split(spqp.Buyerclass, ",") {
  339. buyerclass = append(buyerclass, fmt.Sprint(IC.PushMapping.Buyerclass[v]))
  340. }
  341. if len(buyerclass) == 1 {
  342. buyerclass = append(buyerclass, "9999")
  343. }
  344. if len(buyerclass) > 0 {
  345. querys = append(querys, fmt.Sprintf("buyerclass in (%s)", strings.Join(buyerclass, ",")))
  346. }
  347. }
  348. //信息类型
  349. if spqp.Subtype != "" {
  350. subtype := []string{}
  351. for _, v := range strings.Split(spqp.Subtype, ",") {
  352. subtype = append(subtype, fmt.Sprint(IC.PushMapping.Subtype[v]))
  353. }
  354. if len(subtype) == 1 {
  355. subtype = append(subtype, "9999")
  356. }
  357. if len(subtype) > 0 {
  358. querys = append(querys, fmt.Sprintf("subtype in (%s)", strings.Join(subtype, ",")))
  359. }
  360. }
  361. //信息行业
  362. if spqp.Subscopeclass != "" {
  363. find_in_set := []string{}
  364. for _, v := range strings.Split(spqp.Subscopeclass, ",") {
  365. find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%d',subscopeclass)", IC.PushMapping.Subscopeclass[v]))
  366. }
  367. if len(find_in_set) == 1 {
  368. querys = append(querys, find_in_set[0])
  369. } else if len(find_in_set) > 1 {
  370. querys = append(querys, fmt.Sprintf("(%s)", strings.Join(find_in_set, " or ")))
  371. }
  372. }
  373. //关键词
  374. if spqp.Key != "" {
  375. find_in_set := []string{}
  376. for _, v := range strings.Split(spqp.Key, ",") {
  377. find_in_set = append(find_in_set, fmt.Sprintf("find_in_set('%s',replace(replace(matchkeys,'+',','),' ',','))", v))
  378. }
  379. if len(find_in_set) == 1 {
  380. querys = append(querys, find_in_set[0])
  381. } else if len(find_in_set) > 1 {
  382. querys = append(querys, fmt.Sprintf("(%s)", strings.Join(find_in_set, " or ")))
  383. }
  384. }
  385. //价格- 预算和中标金额
  386. if spqp.Price != "" && strings.Contains(spqp.Price, "-") {
  387. minPrice_str, maxPrice_str := strings.Split(spqp.Price, "-")[0], strings.Split(spqp.Price, "-")[1]
  388. minPrice := common.Int64All(common.Float64All(minPrice_str) * 10000) //换成元
  389. maxPrice := common.Int64All(common.Float64All(maxPrice_str) * 10000) //换成元
  390. querys = append(querys, fmt.Sprintf("((bidamount>=%d and bidamount<=%d) or (budget>=%d and budget<=%d and bidamount is null))", minPrice, maxPrice, minPrice, maxPrice))
  391. }
  392. //附件
  393. if spqp.FileExists != "" {
  394. if spqp.FileExists == "1" {
  395. querys = append(querys, fmt.Sprintf("attachment_count is not null"))
  396. } else if spqp.FileExists == "-1" {
  397. querys = append(querys, fmt.Sprintf("attachment_count is null"))
  398. }
  399. }
  400. searchSql := fmt.Sprintf(" from %s where %s order by id desc", aboutDbMsg[s.ModuleFlag].MysqlTable, strings.Join(querys, " and "))
  401. fmt.Println("searchSql", searchSql)
  402. //查询总数
  403. count = spqp.PushMysql.CountBySql(fmt.Sprintf("select count(id)" + searchSql))
  404. logx.Info("count:", count, "---", s.ModuleFlag)
  405. findSql := "select id,date,infoid,isvisit,matchkeys,type"
  406. if s.ModuleFlag != MemberFlag {
  407. if s.ModuleFlag == EntnicheFlag {
  408. } else {
  409. findSql += ",isvip"
  410. }
  411. }
  412. findSql += searchSql
  413. if isLimit {
  414. findSql += fmt.Sprintf(" limit %d,%d", (spqp.PageNum-1)*size, size)
  415. }
  416. logx.Info(spqp.UserId, "subscribePush query sql:", findSql)
  417. list := spqp.PushMysql.SelectBySql(findSql)
  418. if list != nil && len(*list) > 0 {
  419. pushCas := s.GetJyPushs(*list)
  420. result = s.GetInfoByIds(spqp.Mgo_bidding, spqp.Bidding, spqp.Bidding_back, pushCas)
  421. } else {
  422. result = []*bxsubscribe.SubscribeInfo{}
  423. }
  424. logx.Info("------ooooo----:", len(result))
  425. return
  426. }
  427. //根据id取内容
  428. func (s *subscribePush) GetInfoByIds(Mgo_bidding mongodb.MongodbSim, bidding, bidding_back string, pushCas []*PushCa) []*bxsubscribe.SubscribeInfo {
  429. array := make([]*bxsubscribe.SubscribeInfo, len(pushCas))
  430. if len(pushCas) == 0 {
  431. return array
  432. }
  433. m := map[string]bool{}
  434. ids := []string{}
  435. for _, v := range pushCas {
  436. if m[v.InfoId] {
  437. continue
  438. }
  439. m[v.InfoId] = true
  440. ids = append(ids, v.InfoId)
  441. }
  442. infos := map[string]map[string]interface{}{}
  443. //redis
  444. es_ids := []string{}
  445. for _, v := range ids {
  446. //剑鱼程序未找到赋值 位置;猜测在推送程序中
  447. info_i := redis.Get("pushcache_1", fmt.Sprintf("info_%s", v))
  448. if info_i != nil {
  449. info_m, _ := info_i.(map[string]interface{})
  450. info_m["_id"] = v
  451. infos[v] = info_m
  452. logx.Info("----", info_m, "----")
  453. } else {
  454. es_ids = append(es_ids, v)
  455. }
  456. }
  457. //elasticsearch
  458. if len(es_ids) > 0 {
  459. list := elastic.Get("bidding", "bidding", fmt.Sprintf(query, strings.Join(es_ids, `","`), len(es_ids)))
  460. if list != nil {
  461. for _, v := range *list {
  462. _id := common.ObjToString(v["_id"])
  463. infos[_id] = v
  464. }
  465. }
  466. }
  467. //mongodb bidding
  468. mgo_ids := []primitive.ObjectID{}
  469. for _, v := range es_ids {
  470. if infos[v] == nil {
  471. _id, _ := primitive.ObjectIDFromHex(v)
  472. mgo_ids = append(mgo_ids, _id)
  473. }
  474. }
  475. if len(mgo_ids) > 0 {
  476. list, ok := Mgo_bidding.Find(bidding, map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_ids}}, nil, mongodb_fields, false, -1, -1)
  477. if ok && *list != nil {
  478. for _, v := range *list {
  479. _id := mongodb.BsonIdToSId(v["_id"])
  480. v["_id"] = _id
  481. infos[_id] = v
  482. }
  483. }
  484. }
  485. //mongodb bidding_back
  486. mgo_back_ids := []primitive.ObjectID{}
  487. for _, v := range mgo_ids {
  488. if infos[mongodb.BsonIdToSId(v)] == nil {
  489. mgo_back_ids = append(mgo_back_ids, v)
  490. }
  491. }
  492. if len(mgo_back_ids) > 0 {
  493. list, ok := Mgo_bidding.Find(bidding_back, map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_back_ids}}, nil, mongodb_fields, false, -1, -1)
  494. if ok && *list != nil {
  495. for _, v := range *list {
  496. _id := mongodb.BsonIdToSId(v["_id"])
  497. v["_id"] = _id
  498. infos[_id] = v
  499. }
  500. }
  501. }
  502. //
  503. for k, v := range pushCas {
  504. info := infos[v.InfoId]
  505. if info == nil {
  506. info = map[string]interface{}{}
  507. }
  508. array[k] = s.InfoFormat(v, &info)
  509. }
  510. return array
  511. }
  512. //获取历史推送
  513. func (s *subscribePush) GetJyPushs(datas []map[string]interface{}) (pushCas []*PushCa) {
  514. pushCas = []*PushCa{}
  515. for _, v := range datas {
  516. keys := []string{}
  517. if matchkeys := common.ObjToString(v["matchkeys"]); matchkeys != "" {
  518. keys = strings.Split(matchkeys, " ")
  519. }
  520. pushCas = append(pushCas, &PushCa{
  521. Date: common.Int64All(v["date"]),
  522. InfoId: common.ObjToString(v["infoid"]),
  523. Visit: common.IntAll(v["isvisit"]),
  524. Index: common.Int64All(v["id"]),
  525. Keys: keys,
  526. Type: common.IntAll(v["type"]),
  527. Isvip: common.IntAll(v["isvip"]),
  528. FileExists: common.IntAll(v["attachment_count"]) > 0,
  529. })
  530. }
  531. return
  532. }
  533. //查看全部列表缓存
  534. func (s *subscribePush) PutAllCache(userId string, datas *SubPush) {
  535. redis.Put("pushcache_2_a", s.allKey(userId), datas, oneDay)
  536. }
  537. func (s *subscribePush) GetAllCache(userId string) (*SubPush, error) {
  538. return s.GetCache("pushcache_2_a", s.allKey(userId))
  539. }
  540. func (s *subscribePush) GetCache(code, key string) (*SubPush, error) {
  541. pc_a, err := redis.GetNewBytes(code, key)
  542. if err != nil {
  543. return nil, err
  544. }
  545. if pc_a == nil {
  546. return nil, nil
  547. }
  548. var p *SubPush
  549. if err := json.Unmarshal(*pc_a, &p); err != nil {
  550. return nil, err
  551. }
  552. return p, nil
  553. }
  554. //是否收藏
  555. func (s *subscribePush) MakeCollection(userId string, list []*bxsubscribe.SubscribeInfo) {
  556. if list == nil || len(list) == 0 {
  557. return
  558. }
  559. param := []interface{}{userId}
  560. wh := []string{}
  561. for _, v := range list {
  562. array := encrypt.DecodeArticleId2ByCheck(v.Id)
  563. if len(array) == 1 && array[0] != "" {
  564. param = append(param, array[0])
  565. wh = append(wh, "?")
  566. }
  567. }
  568. if len(wh) > 0 {
  569. result := IC.MainMysql.SelectBySql(`select bid from bdcollection where userid=? and bid in (`+strings.Join(wh, ",")+`)`, param...)
  570. bid_map := map[string]bool{}
  571. if result != nil {
  572. for _, v := range *result {
  573. bid_map[encrypt.EncodeArticleId2ByCheck(common.ObjToString(v["bid"]))] = true
  574. }
  575. }
  576. for _, v := range list {
  577. if bid_map[v.Id] {
  578. v.Collection = 1
  579. }
  580. }
  581. }
  582. }
  583. //仅移动端首页使用,历史推送7天信息
  584. func (s *subscribePush) sevenDayKey(userId string) string {
  585. return fmt.Sprintf("7day_subpush_%s", userId)
  586. }
  587. func (s *subscribePush) PutSevenDayCache(userId string, datas []*bxsubscribe.SubscribeInfo) {
  588. redis.Put("pushcache_2_a", s.sevenDayKey(userId), SubPush{Datas: datas}, 7*24*60*60)
  589. }
  590. //从pushcache_2_a中取
  591. func (s *subscribePush) GetSevenDayCache(userId string) ([]*bxsubscribe.SubscribeInfo, error) {
  592. allPush, _ := s.GetCache("pushcache_2_a", s.sevenDayKey(userId))
  593. if allPush != nil && allPush.Datas != nil && len(allPush.Datas) > 0 {
  594. return allPush.Datas, nil
  595. }
  596. return nil, nil
  597. }
  598. //历史推送记录中单条信息格式化
  599. func InfoFormats(info map[string]interface{}, tmp map[string]interface{}) map[string]interface{} {
  600. area := common.ObjToString(info["area"])
  601. if area == "A" {
  602. area = "全国"
  603. }
  604. industry := common.ObjToString(info["s_subscopeclass"])
  605. scs := strings.Split(industry, ",")
  606. if len(scs) > 0 {
  607. industry = scs[0]
  608. if industry != "" {
  609. iss := strings.Split(industry, "_")
  610. if len(iss) > 0 {
  611. industry = iss[0]
  612. }
  613. }
  614. }
  615. infotype := common.ObjToString(info["subtype"])
  616. if infotype == "" {
  617. infotype = common.ObjToString(info["toptype"])
  618. }
  619. if infotype == "" {
  620. infotype = common.ObjToString(info["type"])
  621. if infotype == "tender" {
  622. infotype = "招标"
  623. } else if infotype == "bid" {
  624. infotype = "中标"
  625. }
  626. }
  627. info["type"] = infotype
  628. return info
  629. }
  630. //UpdateUserPushUnread 更新app未读标识为已读
  631. func UpdateUserPushUnread(userid string, vt string) {
  632. if vt == MemberFlag {
  633. IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_member_apppushunread": 0}})
  634. } else if vt == EntnicheFlag {
  635. IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_entniche_apppushunread": 0}})
  636. } else {
  637. IC.Mgo.UpdateById("user", userid, map[string]interface{}{"$set": map[string]interface{}{"i_apppushunread": 0}})
  638. }
  639. }
  640. //
  641. //获取用户信息
  642. func (s *subscribePush) UserInfo(userId string) (*map[string]interface{}, int64) {
  643. user, ok := IC.Mgo.FindById("user", userId, `{"s_m_openid":1,"a_m_openid":1,"s_phone":1,"a_mergeorder":1,"o_jy":1,"l_firstpushtime":1,"i_vip_status":1,"l_vip_endtime":1,"o_vipjy":1,"i_member_status":1,"o_member_jy":1}`)
  644. if !ok || user == nil {
  645. return nil, 0
  646. }
  647. return user, common.Int64All((*user)["l_firstpushtime"])
  648. }
  649. //是否有订阅词
  650. func GetKeySet(t string, u *map[string]interface{}, data []string) (bool, []string) {
  651. var industry_ = []string{}
  652. if u != nil {
  653. if t == SubFreeFlag {
  654. o_jy, _ := (*u)["o_jy"].(map[string]interface{})
  655. a_key, _ := o_jy["a_key"].([]interface{})
  656. logx.Info(industry_, "--------------------------", len(a_key))
  657. return len(a_key) > 0, industry_
  658. } else {
  659. var obj map[string]interface{}
  660. if t == SubVipFlag {
  661. obj, _ = (*u)["o_vipjy"].(map[string]interface{})
  662. } else if t == MemberFlag {
  663. obj, _ = (*u)["o_member_jy"].(map[string]interface{})
  664. } else if t == EntnicheFlag {
  665. if len(data) > 0 {
  666. return true, data
  667. } else {
  668. return false, data
  669. }
  670. }
  671. if obj != nil {
  672. if buyerclassObj, ok := obj["a_buyerclass"].([]interface{}); ok {
  673. industry_ = common.ObjArrToStringArr(buyerclassObj)
  674. }
  675. itmes, _ := obj["a_items"].([]interface{})
  676. for _, v := range itmes {
  677. item, _ := v.(map[string]interface{})
  678. keys, _ := item["a_key"].([]interface{})
  679. if len(keys) > 0 {
  680. return true, industry_
  681. }
  682. }
  683. }
  684. }
  685. }
  686. return false, industry_
  687. }