ownermonitor.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package ownermonitor
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "time"
  8. util "app.yhyue.com/moapp/jybase/common"
  9. "app.yhyue.com/moapp/jybase/logger"
  10. . "app.yhyue.com/moapp/jybase/mongodb"
  11. . "app.yhyue.com/moapp/jybase/mysql"
  12. "app.yhyue.com/moapp/jybase/redis"
  13. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  14. . "bp.jydev.jianyu360.cn/BaseService/userCenter/identity"
  15. )
  16. const (
  17. Pushspace_temp = "pushspace_ownermonitor_temp"
  18. )
  19. type OwnerMonitorInfo struct {
  20. *UserInfo
  21. OwnerMonitors []*OwnerMonitor //关注
  22. }
  23. //
  24. type OwnerMonitor struct {
  25. Id string //关注id
  26. EntName string //企业名称
  27. CreateTime int64 //关注时间
  28. Infos CSortList //匹配上的信息
  29. }
  30. //加载业主监控
  31. func LoadOwnerMonitor(mgo *MongodbSim, userId string) map[string]*OwnerMonitor {
  32. result := map[string]*OwnerMonitor{}
  33. list, _ := mgo.Find("follow_customer", map[string]interface{}{"userId": userId}, nil, `{"_id":1,"name":1,"date":1}`, false, -1, -1)
  34. if list != nil {
  35. for _, v := range *list {
  36. name, _ := v["name"].(string)
  37. date := util.Int64All(v["date"])
  38. result[name] = &OwnerMonitor{
  39. Id: BsonIdToSId(v["_id"]),
  40. EntName: name,
  41. CreateTime: date,
  42. }
  43. }
  44. }
  45. return result
  46. }
  47. //匹配
  48. func Match(msl *Mysql, mgoMain, mgoLog *MongodbSim, ui *UserInfo, sl *SortList, isEnt bool, uiSons []*UserInfo) {
  49. if sl == nil {
  50. return
  51. }
  52. userId := ui.Id
  53. if isEnt {
  54. identity := IdentityByEntUserId(msl, mgoMain, int64(ui.Entniche.UserId))
  55. if identity == nil {
  56. logger.Error("没有找到用户身份", ui.Entniche.UserId)
  57. return
  58. }
  59. userId = fmt.Sprint(identity.PositionId)
  60. }
  61. oms := LoadOwnerMonitor(mgoMain, userId)
  62. result := map[string]*OwnerMonitor{}
  63. for _, v := range *sl {
  64. buyer, _ := v.Info["buyer"].(string)
  65. if oms[buyer] == nil {
  66. continue
  67. }
  68. _id, _ := v.Info["_id"].(string)
  69. key := fmt.Sprintf("ownermonitor_%s_%s_%s", userId, buyer, _id)
  70. if exists, _ := redis.Exists("pushcache_1", key); exists {
  71. continue
  72. }
  73. if result[buyer] == nil {
  74. result[buyer] = oms[buyer]
  75. }
  76. redis.Put("pushcache_1", key, 1, ThreeDay)
  77. result[buyer].Infos = append(result[buyer].Infos, v.Info)
  78. }
  79. omis := []*OwnerMonitor{}
  80. matchBuyers := []string{}
  81. for k, v := range result {
  82. matchBuyers = append(matchBuyers, k)
  83. omis = append(omis, v)
  84. }
  85. uis := []*UserInfo{ui}
  86. if uiSons != nil && len(uiSons) > 0 {
  87. uis = append(uis, uiSons...)
  88. }
  89. for k, v := range uis {
  90. saveUserId := userId
  91. if k > 0 {
  92. saveUserId = v.Id
  93. }
  94. OwnerMonitorSplit(omis, func(list interface{}) {
  95. mgoLog.Save(Pushspace_temp, map[string]interface{}{
  96. "s_m_openid": v.S_m_openid,
  97. "jpushid": v.Jpushid,
  98. "opushid": v.Opushid,
  99. "appphonetype": v.AppPhoneType,
  100. "userid": saveUserId,
  101. "wxpush": v.PushSet.OwnerMonitor.WxPush,
  102. "apppush": v.PushSet.OwnerMonitor.AppPush,
  103. "mailpush": v.PushSet.OwnerMonitor.MailPush,
  104. "email": v.PushSet.Email,
  105. "list": list,
  106. "timestamp": time.Now().Unix(),
  107. "times": v.PushSet.OwnerMonitor.Times,
  108. "ratemode": v.PushSet.OwnerMonitor.RateMode,
  109. "vipstatus": v.VipStatus,
  110. "memberstatus": v.MemberStatus,
  111. "nichestatus": v.NicheStatus,
  112. })
  113. })
  114. }
  115. logger.Info(userId, "业主监控匹配上", len(result), "个", strings.Join(matchBuyers, ","))
  116. }
  117. //
  118. func OwnerMonitorSplit(list []*OwnerMonitor, f func(v interface{})) {
  119. l := len(list)
  120. if l == 0 {
  121. return
  122. }
  123. i := Mgo_ListSize
  124. for {
  125. if l > i {
  126. arr := list[i-Mgo_ListSize : i]
  127. f(&arr)
  128. } else if l > i-Mgo_ListSize {
  129. arr := list[i-Mgo_ListSize:]
  130. f(&arr)
  131. break
  132. }
  133. i += Mgo_ListSize
  134. }
  135. }
  136. //
  137. func ToOwnerMonitors(list interface{}) []*OwnerMonitor {
  138. sl := []*OwnerMonitor{}
  139. if list == nil {
  140. return sl
  141. }
  142. b, err := json.Marshal(list)
  143. if err != nil {
  144. return sl
  145. }
  146. err = json.Unmarshal(b, &sl)
  147. if err != nil {
  148. return sl
  149. }
  150. return sl
  151. }
  152. //第一个参数是老数据,第二个参数是新进数据
  153. func MergeOwnerMonitors(o, n interface{}, maxPushSize int) []*OwnerMonitor {
  154. of, oo := o.([]*OwnerMonitor)
  155. if !oo {
  156. of = ToOwnerMonitors(o)
  157. }
  158. nf, no := n.([]*OwnerMonitor)
  159. if !no {
  160. nf = ToOwnerMonitors(n)
  161. }
  162. key := func(e, v string) string {
  163. return fmt.Sprintf("%s_%s", e, v)
  164. }
  165. m := map[string]*OwnerMonitor{}
  166. idMap := map[string]bool{}
  167. for _, v := range nf {
  168. m[key(v.EntName, "")] = v
  169. for _, vv := range v.Infos {
  170. idMap[key(v.EntName, util.ObjToString(vv["_id"]))] = true
  171. }
  172. }
  173. //去重
  174. for _, v := range of {
  175. follow := m[key(v.EntName, "")]
  176. if follow == nil {
  177. nf = append(nf, v)
  178. continue
  179. }
  180. for _, vv := range v.Infos {
  181. if idMap[key(v.EntName, util.ObjToString(vv["_id"]))] {
  182. continue
  183. }
  184. follow.Infos = append(follow.Infos, vv)
  185. sort.Sort(follow.Infos)
  186. if len(follow.Infos) > maxPushSize {
  187. follow.Infos = follow.Infos[:maxPushSize]
  188. }
  189. }
  190. }
  191. return nf
  192. }