ownermonitor.go 4.8 KB

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