package ownermonitor import ( "encoding/json" "fmt" "sort" "strings" "time" util "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/logger" . "app.yhyue.com/moapp/jybase/mongodb" . "app.yhyue.com/moapp/jybase/mysql" "app.yhyue.com/moapp/jybase/redis" . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p" . "bp.jydev.jianyu360.cn/BaseService/userCenter/identity" ) const ( Pushspace_temp = "pushspace_ownermonitor_temp" ) type OwnerMonitorInfo struct { *UserInfo OwnerMonitors []*OwnerMonitor //关注 } // type OwnerMonitor struct { Id string //关注id EntName string //企业名称 CreateTime int64 //关注时间 Infos CSortList //匹配上的信息 Size int //数量 } //加载业主监控 func LoadOwnerMonitor(mgo *MongodbSim, userId string) map[string]*OwnerMonitor { result := map[string]*OwnerMonitor{} list, _ := mgo.Find("follow_customer", map[string]interface{}{"userId": userId}, nil, `{"_id":1,"name":1,"date":1}`, false, -1, -1) if list != nil { for _, v := range *list { name, _ := v["name"].(string) date := util.Int64All(v["date"]) result[name] = &OwnerMonitor{ Id: BsonIdToSId(v["_id"]), EntName: name, CreateTime: date, } } } return result } //匹配 func Match(msl *Mysql, mgoMain, mgoLog *MongodbSim, ui *UserInfo, sl *SortList, isEnt bool, uiSons []*UserInfo) { if sl == nil { return } userId := ui.Id if isEnt { identity := IdentityByEntUserId(msl, mgoMain, int64(ui.Entniche.UserId)) if identity == nil { logger.Error("没有找到用户身份", ui.Entniche.UserId) return } userId = fmt.Sprint(identity.PositionId) } oms := LoadOwnerMonitor(mgoMain, userId) result := map[string]*OwnerMonitor{} for _, v := range *sl { buyer, _ := v.Info["buyer"].(string) if oms[buyer] == nil { continue } _id, _ := v.Info["_id"].(string) key := fmt.Sprintf("ownermonitor_%s_%s_%s", userId, buyer, _id) if exists, _ := redis.Exists("pushcache_1", key); exists { continue } if result[buyer] == nil { result[buyer] = oms[buyer] } redis.Put("pushcache_1", key, 1, ThreeDay) result[buyer].Infos = append(result[buyer].Infos, v.Info) } omis := []*OwnerMonitor{} matchBuyers := []string{} for k, v := range result { matchBuyers = append(matchBuyers, k) omis = append(omis, v) } uis := []*UserInfo{ui} if uiSons != nil && len(uiSons) > 0 { uis = append(uis, uiSons...) } for k, v := range uis { saveUserId := userId if k > 0 { saveUserId = v.Id } OwnerMonitorSplit(omis, func(list interface{}) { mgoLog.Save(Pushspace_temp, map[string]interface{}{ "s_m_openid": v.S_m_openid, "jpushid": v.Jpushid, "opushid": v.Opushid, "appphonetype": v.AppPhoneType, "userid": saveUserId, "wxpush": v.PushSet.OwnerMonitor.WxPush, "apppush": v.PushSet.OwnerMonitor.AppPush, "mailpush": v.PushSet.OwnerMonitor.MailPush, "email": v.PushSet.Email, "list": list, "timestamp": time.Now().Unix(), "times": v.PushSet.OwnerMonitor.Times, "ratemode": v.PushSet.OwnerMonitor.RateMode, "vipstatus": v.VipStatus, "memberstatus": v.MemberStatus, "nichestatus": v.NicheStatus, }) }) } logger.Info(userId, "业主监控匹配上", len(result), "个", strings.Join(matchBuyers, ",")) } // func OwnerMonitorSplit(list []*OwnerMonitor, f func(v interface{})) { l := len(list) if l == 0 { return } i := Mgo_ListSize for { if l > i { arr := list[i-Mgo_ListSize : i] f(&arr) } else if l > i-Mgo_ListSize { arr := list[i-Mgo_ListSize:] f(&arr) break } i += Mgo_ListSize } } // func ToOwnerMonitors(list interface{}) []*OwnerMonitor { sl := []*OwnerMonitor{} if list == nil { return sl } b, err := json.Marshal(list) if err != nil { return sl } err = json.Unmarshal(b, &sl) if err != nil { return sl } return sl } //第一个参数是老数据,第二个参数是新进数据 func MergeOwnerMonitors(o, n interface{}, maxPushSize int) []*OwnerMonitor { of, oo := o.([]*OwnerMonitor) if !oo { of = ToOwnerMonitors(o) } nf, no := n.([]*OwnerMonitor) if !no { nf = ToOwnerMonitors(n) } key := func(e, v string) string { return fmt.Sprintf("%s_%s", e, v) } m := map[string]*OwnerMonitor{} idMap := map[string]bool{} for _, v := range nf { m[key(v.EntName, "")] = v for _, vv := range v.Infos { idMap[key(v.EntName, util.ObjToString(vv["_id"]))] = true } } //去重 for _, v := range of { follow := m[key(v.EntName, "")] if follow == nil { nf = append(nf, v) continue } for _, vv := range v.Infos { if idMap[key(v.EntName, util.ObjToString(vv["_id"]))] { continue } follow.Infos = append(follow.Infos, vv) sort.Sort(follow.Infos) if len(follow.Infos) > maxPushSize { follow.Infos = follow.Infos[:maxPushSize] } } } return nf }