package algo import ( "context" "encoding/json" "github.com/go-redis/redis/v8" "hilo-algoCenter/common/config" "hilo-algoCenter/common/mylogrus" "hilo-algoCenter/common/redisCli" "strconv" "strings" "time" ) type RecommendUserCache struct { UserId uint64 Sex uint8 Country string ExternalId string } // MatchCycle 匹配周期 type MatchCycle struct { cycle string } type MatchTradeUnionCache struct { UserId uint64 Sex uint8 Country string ExternalId string } // MatchUserCache 用户信息 type MatchUserCache struct { UserId uint64 Sex uint8 Country string ExternalId string IsVip bool } // MatchRelation 用户关系分数 type MatchRelation struct { UserId uint64 RelationUserId uint64 Score float64 } func GetMatchCycle() int64 { return time.Now().Unix() / int64(config.GetMatchConfig().MATCH_CYCLE) } func SetCycle(cycle int64) (bool, error) { return redisCli.RedisClient.SetNX(context.Background(), GetPrefixMatchCycle(cycle), cycle, time.Hour).Result() } // InitMatchCycle 初始化匹配周期 func InitMatchCycle(cycle int64) *MatchCycle { return &MatchCycle{cycle: strconv.Itoa(int(cycle))} } func (matchCycle *MatchCycle) GetCycle() string { return matchCycle.cycle } func (matchCycle *MatchCycle) GetUser(userIds []string) (map[string]MatchUserCache, error) { key := GetPrefixMatchUser(matchCycle.cycle) dList, err := redisCli.RedisClient.HMGet(context.Background(), key, userIds...).Result() if err != nil { return nil, err } userCacheMap := map[string]MatchUserCache{} for _, v := range dList { //转换成json matchUserCache := MatchUserCache{} if err := json.Unmarshal([]byte(v.(string)), &matchUserCache); err != nil { mylogrus.MyLog.Errorf("match cache user 转换成结构失败,string:%v, 错误:%v", v.(string), err) } userCacheMap[strconv.Itoa(int(matchUserCache.UserId))] = matchUserCache } return userCacheMap, nil } // GetExcellentData 获取某个周期下,获取所有的质量分数 func (matchCycle *MatchCycle) GetExcellentData() ([]redis.Z, error) { key := GetPrefixMatchExcellent(matchCycle.cycle) zList, err := redisCli.RedisClient.ZRangeWithScores(context.Background(), key, 0, -1).Result() if err != nil { return nil, err } return zList, nil } // GetPriorityData 获取某个周期下,获取的排序数据 func (matchCycle *MatchCycle) GetPriorityData() ([]redis.Z, error) { key := GetPrefixMatchPriority(matchCycle.cycle) zList, err := redisCli.RedisClient.ZRangeWithScores(context.Background(), key, 0, -1).Result() if err != nil { return nil, err } return zList, nil } // GetRelationData 获取关系分数 func (matchCycle *MatchCycle) GetRelationData(userId string) ([]MatchRelation, error) { key := GetPrefixMatchRelation(matchCycle.cycle, userId) values, err := redisCli.RedisClient.HVals(context.Background(), key).Result() if err != nil { return nil, err } var matchRelations []MatchRelation for _, v := range values { var matchRelation MatchRelation if err := json.Unmarshal([]byte(v), &matchRelation); err != nil { mylogrus.MyLog.Errorf("match cache matchRelations 转换成结构失败,string:%v, 错误:%v", v, err) } matchRelations = append(matchRelations, matchRelation) } return matchRelations, nil } //匹配第一帧 const matchCallReady = "match_call_ready_{matchUid}" func GetPrefixMatchCycle(cycle int64) string { return strings.Replace(matchCallReady, "{matchUid}", strconv.Itoa(int(cycle)), -1) } //推荐用户, 记录起来,用户算法中心获取。 const recommendUser = "recommend_user" func GetPreRecommendUser() string { return recommendUser } //匹配的工会用户 const matchTradeUnion = "match_trade_union_{version}" func GetPreMatchTradeUnion(version string) string { return strings.Replace(matchTradeUnion, "{version}", version, -1) } //匹配,用户信息 const matchUserKey = "match_user_{version}" func GetPrefixMatchUser(version string) string { return strings.Replace(matchUserKey, "{version}", version, -1) } //匹配,质量分数 const matchExcellent = "match_excellent_{version}" func GetPrefixMatchExcellent(version string) string { return strings.Replace(matchExcellent, "{version}", version, -1) } //匹配,用户优先度排序 const matchPriority = "match_priority_{version}" func GetPrefixMatchPriority(version string) string { return strings.Replace(matchPriority, "{version}", version, -1) } //匹配,关系分数 const matchRelation = "match_relation_{version}_{userId}" func GetPrefixMatchRelation(version string, userId string) string { return strings.Replace(strings.Replace(matchRelation, "{version}", version, -1), "{userId}", userId, -1) } //匹配,拉黑名单 const matchBlock = "match_block_{version}" func GetPrefixMatchBlock(version string) string { return strings.Replace(matchBlock, "{version}", version, -1) } //匹配,条件性别 const matchConditionSex = "match_condition_sex_{version}" func GetPrefixMatchConditionSex(version string) string { return strings.Replace(matchConditionSex, "{version}", version, -1) } //匹配,条件国家 const matchConditionCountry = "match_condition_country_{version}" func GetPrefixMatchConditionCountry(version string) string { return strings.Replace(matchConditionCountry, "{version}", version, -1) } // GetRecommendCache 获取推荐人信息 func GetRecommendCache() ([]RecommendUserCache, error) { //获取推荐人信息缓存 key := GetPreRecommendUser() strs, err := redisCli.RedisClient.HVals(context.Background(), key).Result() if err != nil { return nil, err } var recommendUserCaches []RecommendUserCache for i := 0; i < len(strs); i++ { var recommendUserCache RecommendUserCache if err := json.Unmarshal([]byte(strs[i]), &recommendUserCache); err != nil { mylogrus.MyLog.Errorf("match cache recommendUserCache 转换成结构失败,string:%v, 错误:%v", strs[i], err) } recommendUserCaches = append(recommendUserCaches, recommendUserCache) } return recommendUserCaches, nil } func GetMatchTradeUnionCache(version string) ([]MatchTradeUnionCache, error) { key := GetPreMatchTradeUnion(version) strs, err := redisCli.RedisClient.HVals(context.Background(), key).Result() if err != nil { return nil, err } var matchTradeUnionCaches []MatchTradeUnionCache for i := 0; i < len(strs); i++ { var matchTradeUnionCache MatchTradeUnionCache if err := json.Unmarshal([]byte(strs[i]), &matchTradeUnionCache); err != nil { mylogrus.MyLog.Errorf("match cache matchTradeUnionCache 转换成结构失败,string:%v, 错误:%v", strs[i], err) } matchTradeUnionCaches = append(matchTradeUnionCaches, matchTradeUnionCache) } return matchTradeUnionCaches, nil } func (matchCycle *MatchCycle) CheckBlock(userId string, otherUserId string) (bool, error) { key := GetPrefixMatchBlock(matchCycle.cycle) u, _ := strconv.ParseUint(userId, 10, 64) o, _ := strconv.ParseUint(otherUserId, 10, 64) if u > o { flag, err := redisCli.RedisClient.SIsMember(context.Background(), key, userId+"_"+otherUserId).Result() if err != nil { return flag, err } return flag, err } else { flag, err := redisCli.RedisClient.SIsMember(context.Background(), key, otherUserId+"_"+userId).Result() if err != nil { return flag, err } return flag, err } } func (matchCycle *MatchCycle) GetConditionSex(userId string) (string, error) { key := GetPrefixMatchConditionSex(matchCycle.cycle) str, err := redisCli.RedisClient.HGet(context.Background(), key, userId).Result() if err != nil { return "", nil } return str, nil } func (matchCycle *MatchCycle) GetConditionCountry(userId string) (string, error) { key := GetPrefixMatchConditionCountry(matchCycle.cycle) str, err := redisCli.RedisClient.HGet(context.Background(), key, userId).Result() if err != nil { return "", nil } return str, nil }