push.go 22 KB

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