ownermonitor.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package ownermonitor
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. util "app.yhyue.com/moapp/jybase/common"
  8. "app.yhyue.com/moapp/jybase/logger"
  9. . "app.yhyue.com/moapp/jybase/mongodb"
  10. . "app.yhyue.com/moapp/jybase/mysql"
  11. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  12. . "bp.jydev.jianyu360.cn/BaseService/userCenter/identity"
  13. )
  14. const (
  15. Pushspace_temp = "pushspace_ownermonitor_temp"
  16. )
  17. type OwnerMonitorInfo struct {
  18. *UserInfo
  19. OwnerMonitors []*OwnerMonitor //关注
  20. }
  21. //
  22. type OwnerMonitor struct {
  23. Id string //关注id
  24. EntName string //企业名称
  25. CreateTime int64 //关注时间
  26. Infos CSortList //匹配上的信息
  27. }
  28. //加载业主监控
  29. func LoadOwnerMonitor(mgo *MongodbSim, userId string) map[string]*OwnerMonitor {
  30. result := map[string]*OwnerMonitor{}
  31. list, _ := mgo.Find("follow_customer", map[string]interface{}{"userId": userId}, nil, `{"_id":1,"name":1,"date":1}`, false, -1, -1)
  32. if list != nil {
  33. for _, v := range *list {
  34. name, _ := v["name"].(string)
  35. date := util.Int64All(v["date"])
  36. result[name] = &OwnerMonitor{
  37. Id: BsonIdToSId(v["_id"]),
  38. EntName: name,
  39. CreateTime: date,
  40. }
  41. }
  42. }
  43. return result
  44. }
  45. //匹配
  46. func Match(msl *Mysql, mgoMain, mgoLog *MongodbSim, ui *UserInfo, sl *SortList, isEnt bool, uiSons []*UserInfo) {
  47. if sl == nil {
  48. return
  49. }
  50. userId := ui.Id
  51. if isEnt {
  52. identity := IdentityByEntUserId(msl, mgoMain, int64(ui.Entniche.UserId))
  53. if identity == nil {
  54. logger.Error("没有找到用户身份", ui.Entniche.UserId)
  55. return
  56. }
  57. userId = fmt.Sprint(identity.PositionId)
  58. }
  59. oms := LoadOwnerMonitor(mgoMain, userId)
  60. result := map[string]*OwnerMonitor{}
  61. for _, v := range *sl {
  62. buyer, _ := v.Info["buyer"].(string)
  63. if oms[buyer] == nil {
  64. continue
  65. }
  66. _id, _ := v.Info["_id"].(string)
  67. key := fmt.Sprintf("ownermonitor_%s_%s_%s", userId, buyer, _id)
  68. if exists, _ := redis.Exists("pushcache_1", key); exists {
  69. continue
  70. }
  71. if result[buyer] == nil {
  72. result[buyer] = oms[buyer]
  73. }
  74. redis.Put("pushcache_1", key, 1, ThreeDay)
  75. result[buyer].Infos = append(result[buyer].Infos, v.Info)
  76. }
  77. omis := []*OwnerMonitor{}
  78. matchBuyers := []string{}
  79. for k, v := range result {
  80. matchBuyers = append(matchBuyers, k)
  81. omis = append(omis, v)
  82. }
  83. uis := []*UserInfo{ui}
  84. if uiSons != nil && len(uiSons) > 0 {
  85. uis = append(uis, uiSons...)
  86. }
  87. for k, v := range uis {
  88. saveUserId := userId
  89. if k > 0 {
  90. saveUserId = v.Id
  91. }
  92. OwnerMonitorSplit(omis, func(list interface{}) {
  93. mgoLog.Save(Pushspace_temp, map[string]interface{}{
  94. "s_m_openid": v.S_m_openid,
  95. "jpushid": v.Jpushid,
  96. "opushid": v.Opushid,
  97. "appphonetype": v.AppPhoneType,
  98. "userid": saveUserId,
  99. "wxpush": v.PushSet.OwnerMonitor.WxPush,
  100. "apppush": v.PushSet.OwnerMonitor.AppPush,
  101. "mailpush": v.PushSet.OwnerMonitor.MailPush,
  102. "email": v.PushSet.Email,
  103. "list": list,
  104. "timestamp": time.Now().Unix(),
  105. "times": v.PushSet.OwnerMonitor.Times,
  106. "ratemode": v.PushSet.OwnerMonitor.RateMode,
  107. "vipstatus": v.VipStatus,
  108. "memberstatus": v.MemberStatus,
  109. "nichestatus": v.NicheStatus,
  110. })
  111. })
  112. }
  113. logger.Info(userId, "业主监控匹配上", len(result), "个", strings.Join(matchBuyers, ","))
  114. }
  115. //
  116. func OwnerMonitorSplit(list []*OwnerMonitor, f func(v interface{})) {
  117. l := len(list)
  118. if l == 0 {
  119. return
  120. }
  121. i := Mgo_ListSize
  122. for {
  123. if l > i {
  124. arr := list[i-Mgo_ListSize : i]
  125. f(&arr)
  126. } else if l > i-Mgo_ListSize {
  127. arr := list[i-Mgo_ListSize:]
  128. f(&arr)
  129. break
  130. }
  131. i += Mgo_ListSize
  132. }
  133. }