2026-08-09 10:43:45 +08:00
package common
import (
"encoding/json"
"fmt"
"hash/crc64"
"sort"
2026-08-09 12:41:08 +08:00
"bsm/full/module/social/relation/internal/impl"
"bsm/full/module/social/relation/internal/models"
pb "bsm/full/module/social/relation/pb"
2026-08-09 10:43:45 +08:00
)
var (
Filed string = "relation_identity as identity,nickname,avatar,sex,province,city,area,sign"
)
// 根据identity获取会员信息卡片
func GetrelationInfoDetailCard ( identity string ) ( card * pb . RelationItem , err error ) {
err = impl . DBService . Table ( "relation_extend" ) . Select ( Filed ) . Where ( "relation_identity = ?" , identity ) . First ( & card ) . Error
return
}
2026-09-22 21:15:34 +08:00
// 根据identity数组获取多条会员信息卡片
func GetrelationInfoDetailCardById ( identities [ ] string ) ( cards [ ] * pb . RelationItem , err error ) {
err = impl . DBService . Table ( "relation_extend" ) . Select ( Filed ) . Where ( "relation_identity in ?" , identities ) . Find ( & cards ) . Error
2026-08-09 10:43:45 +08:00
return
}
// 实现将2个字符串, 不论先后顺序输入, 输出得到的唯一值都是一样的。
func UniqueSessionID ( s1 , s2 string ) string {
strs := [ ] string { s1 , s2 }
sort . Strings ( strs )
table := crc64 . MakeTable ( crc64 . ECMA )
return fmt . Sprintf ( "%x" , crc64 . Checksum ( [ ] byte ( strs [ 0 ] + strs [ 1 ] ) , table ) )
}
// 根据用户ID, 找到用户的申请好友信息
func CollectionFriendApply ( id uint ) ( reply * pb . ApplyFetchReply , err error ) {
var (
version int64
2026-09-22 21:15:34 +08:00
identities [ ] string
2026-08-09 10:43:45 +08:00
last_msg_ids [ ] uint
maxSize int = 50
applys = make ( [ ] * models . FriendApply , 0 )
replyItems = make ( [ ] * pb . ApplyItem , 0 )
mapCards = make ( map [ string ] * pb . RelationItem )
messages = make ( [ ] * models . FriendApplyMessage , 0 )
mapMessages = make ( map [ uint ] * pb . MessageItem )
)
reply = & pb . ApplyFetchReply { }
err = impl . DBService . Where ( "to_id=? " , id ) . Order ( "id desc" ) . Limit ( maxSize ) . Find ( & applys ) . Error
if err != nil {
return nil , err
}
for _ , item := range applys {
2026-09-22 21:15:34 +08:00
identities = append ( identities , item . FromIdentity )
2026-08-09 10:43:45 +08:00
last_msg_ids = append ( last_msg_ids , item . LastMessageID )
}
2026-09-22 21:15:34 +08:00
// 按申请人身份取资料卡
result , err := GetrelationInfoDetailCardById ( identities )
2026-08-09 10:43:45 +08:00
if err != nil {
return nil , err
}
for _ , item := range result {
mapCards [ item . Identity ] = item
}
err = impl . DBService . Where ( "id in ?" , last_msg_ids ) . Find ( & messages ) . Error
if err != nil {
return nil , err
}
for _ , item := range messages {
mapMessages [ item . ID ] = & pb . MessageItem {
RelationIdentity : item . PassportIdentity ,
Body : item . Body ,
CreatedAt : item . CreatedAt . Local ( ) . Format ( "2006-01-02 15:04:05" ) ,
}
}
for _ , item := range applys {
var applyItem = & pb . ApplyItem { }
applyItem . Identity = item . Identity
applyItem . CreatedAt = item . CreatedAt . Local ( ) . Format ( "2006-01-02 15:04:05" )
applyItem . Status = int32 ( item . Status )
if _ , ok := mapCards [ item . FromIdentity ] ; ok {
applyItem . From = mapCards [ item . FromIdentity ]
}
if _ , ok := mapMessages [ item . LastMessageID ] ; ok {
applyItem . Message = mapMessages [ item . LastMessageID ]
}
replyItems = append ( replyItems , applyItem )
version = int64 ( item . ID )
}
reply . Applys = replyItems
reply . Version = version
return
}
// 根据用户ID, 找到用户的好友信息和分组信息
func CollectionFriendData ( id uint ) ( reply * pb . FriendsReply , err error ) {
var (
2026-09-22 21:15:34 +08:00
version int64
identities [ ] string
2026-08-09 10:43:45 +08:00
relationFriend = make ( [ ] models . RelationFriend , 0 )
2026-09-22 21:15:34 +08:00
mapRelationFrend = make ( map [ string ] models . RelationFriend )
2026-08-09 10:43:45 +08:00
friendInTag = make ( [ ] models . FriendInTag , 0 )
mapFriendInTag = make ( map [ string ] [ ] string )
2026-09-22 21:15:34 +08:00
mapTagTotal = make ( map [ string ] int64 )
2026-08-09 10:43:45 +08:00
)
reply = & pb . FriendsReply { }
2026-09-22 21:15:34 +08:00
// 好友条目归属列是 passport_id, 好友身份是 friend_relation_identity
err = impl . DBService . Where ( "passport_id=?" , id ) . Order ( "id desc" ) . Find ( & relationFriend ) . Error
if err != nil {
return nil , err
}
2026-08-09 10:43:45 +08:00
for _ , item := range relationFriend {
2026-09-22 21:15:34 +08:00
identities = append ( identities , item . FriendrelationIdentity )
mapRelationFrend [ item . FriendrelationIdentity ] = item
2026-08-09 10:43:45 +08:00
version = int64 ( item . ID )
}
2026-09-22 21:15:34 +08:00
err = impl . DBService . Where ( "passport_id=?" , id ) . Find ( & friendInTag ) . Error
if err != nil {
return nil , err
}
2026-08-09 10:43:45 +08:00
for _ , item := range friendInTag {
mapFriendInTag [ item . FriendIdentity ] = append ( mapFriendInTag [ item . FriendIdentity ] , item . TagIdentity )
2026-09-22 21:15:34 +08:00
mapTagTotal [ item . TagIdentity ] ++
2026-08-09 10:43:45 +08:00
}
2026-09-22 21:15:34 +08:00
// 标签列表按归属过滤并补齐标签名
err = impl . DBService . Model ( models . FriendTag { } ) . Select ( "id, identity, name as tag_name" ) . Where ( "passport_id=?" , id ) . Order ( "id asc" ) . Scan ( & reply . Tags ) . Error
2026-08-09 10:43:45 +08:00
if err != nil {
return
}
2026-09-22 21:15:34 +08:00
// 补齐每个标签的成员数
for idx := range reply . Tags {
reply . Tags [ idx ] . FriendTotal = mapTagTotal [ reply . Tags [ idx ] . Identity ]
}
2026-08-09 10:43:45 +08:00
2026-09-22 21:15:34 +08:00
// 按好友身份装配资料卡,并回填备注与置顶
reply . Friends , err = GetrelationInfoDetailCardById ( identities )
if err != nil {
return
}
for idx := range reply . Friends {
if item , ok := mapRelationFrend [ reply . Friends [ idx ] . Identity ] ; ok {
reply . Friends [ idx ] . RemarkName = item . RemarkName
reply . Friends [ idx ] . Popular = int32 ( item . Popular )
}
if tags , ok := mapFriendInTag [ reply . Friends [ idx ] . Identity ] ; ok {
reply . Friends [ idx ] . Tags = tags
2026-08-09 10:43:45 +08:00
}
}
reply . Version = version
return
}
// 根据Match表获取多条会员信息卡片
func GetrelationInfoDetailCardByMatch ( relation_identity string , offset , limit int ) ( cards [ ] * pb . RelationItem , err error ) {
sql := ` Select
pe . relation_identity as identity ,
2026-09-22 21:15:34 +08:00
pe . nickname ,
2026-08-09 10:43:45 +08:00
pe . avatar ,
pe . birthday ,
pe . sex ,
pe . province ,
pe . city ,
pe . area ,
pe . sign ,
rm . status as foreign_status
2026-09-22 21:15:34 +08:00
From relation_match as rm
Join relation_extend as pe on pe . relation_identity = rm . recommend_identity
2026-08-09 10:43:45 +08:00
Where rm . relation_identity = ?
2026-09-22 21:15:34 +08:00
Order by rm . id Desc
2026-08-09 10:43:45 +08:00
Limit ?
2026-09-22 21:15:34 +08:00
Offset ? `
err = impl . DBService . Raw ( sql , relation_identity , limit , offset ) . Scan ( & cards ) . Error
2026-08-09 10:43:45 +08:00
return
}
func SetCache ( identity string , collectionName string , in interface { } ) error {
key := "/RF/" + identity + "/" + collectionName
val , err := json . Marshal ( in )
if err != nil {
return err
}
err = impl . RedisService . Client . Set ( impl . RedisService . Ctx , key , string ( val ) , 0 ) . Err ( )
if err != nil {
return err
}
return nil
}
func GetCache ( identity , collectionName string ) ( reply * pb . ApplyFetchReply , err error ) {
key := "/RF/" + identity + "/" + collectionName
collection , err := impl . RedisService . Client . Get ( impl . RedisService . Ctx , key ) . Result ( )
if err != nil {
return
}
err = json . Unmarshal ( [ ] byte ( collection ) , & reply )
if err != nil {
return
}
return
}