package manager import ( "context" "github.com/go-redis/redis/v8" "gorm.io/gorm" "hilo-userCenter/common/mylogrus" "log" "strconv" "sync" ) const ( user_key = "user" ) type UserManager struct { Ctx context.Context RedisClient *redis.Client MysqlDB *gorm.DB } func (m *UserManager) GetUser(uid uint64) *string { field := strconv.FormatUint(uid, 10) //ctx, _ := context.WithTimeout(m.Ctx, time.Millisecond*500) //result := m.RedisClient.Get(ctx, field) result, err := m.RedisClient.HGet(m.Ctx, user_key, field).Result() if err != nil { log.Printf("HGet error %s\n", err.Error()) return nil } return &result } func (m *UserManager) AddUser(uid uint64, value string) error { field := strconv.FormatUint(uid, 10) result, err := m.RedisClient.HSet(m.Ctx, user_key, field, value).Result() if err != nil { log.Printf("HSet error %s\n") } else { log.Printf("HSet %s:%s result: %d\n", user_key, field, result) } return err } func (m *UserManager) RemoveUser(uid uint64) error { field := strconv.FormatUint(uid, 10) result, err := m.RedisClient.HDel(m.Ctx, user_key, field).Result() if err != nil { log.Printf("HDel error: %s\n", err.Error()) } else { log.Printf("HDel %s return %d\n", field, result) } return err } func (m *UserManager) GetAll() *map[string]string { //ctx, _ := context.WithTimeout(m.Ctx, time.Millisecond*500) //result := m.RedisClient.Get(ctx, field) result, err := m.RedisClient.HGetAll(m.Ctx, user_key).Result() if err != nil { log.Printf("HGetAll error: %s\n", err.Error()) return nil } else { return &result } } // 国家到area映射 country->area 1:阿语 2:非阿语 var countryArea sync.Map type ResCountry struct { Name string Area int8 } // 定期同步国家到area映射 func (m *UserManager) SyncArea() { var rows []ResCountry if err := m.MysqlDB.Model(ResCountry{}).Find(&rows).Error; err != nil { mylogrus.MyLog.Errorf("SyncArea fail:%v", err) return } for _, v := range rows { countryArea.Store(v.Name, v.Area) } } // 获取地区 func (m *UserManager) GetArea(country string) (area int8) { area = 1 // 默认阿语 if data, ok := countryArea.Load(country); ok { return data.(int8) } return area } type UserTinyArea struct { ID uint64 Avatar string ExternalId string Sex uint8 Code string Country string Area int8 `gorm:"-"` } // 获取指定区域用户 func (m *UserManager) GetAreaUsers(userIds []uint64, area int8) map[uint64]UserTinyArea { res := make(map[uint64]UserTinyArea) // 从db中读,暂时不缓存(几千个) var users []UserTinyArea if err := m.MysqlDB.Table("user"). Select("id,external_id,sex,code,country,avatar"). Where("id IN (?)", userIds).Find(&users).Error; err != nil { mylogrus.MyLog.Errorf("GetUserArea fail:%v", err) return res } for _, u := range users { a := m.GetArea(u.Country) if a != area { continue } res[u.ID] = UserTinyArea{ ID: u.ID, ExternalId: u.ExternalId, Sex: u.Sex, Code: u.Code, Country: u.Country, Area: a, Avatar: u.Avatar, } } return res } // 获取财富等级大于某等级的用户 // 开区间 func (m *UserManager) GetLevelUsers(userIds []uint64, wealthLevel int32) (map[uint64]UserTinyArea, []uint64) { res := make(map[uint64]UserTinyArea) var resIds []uint64 // 从db中读,暂时不缓存(几千个) var users []UserTinyArea if err := m.MysqlDB.Table("user").Joins("JOIN match_wealth_user_score ON match_wealth_user_score.user_id = user.id"). Select("user.id,external_id,sex,code,country,avatar"). Where("user.id IN (?)", userIds). Where("match_wealth_user_score.grade > ?", wealthLevel). Find(&users).Error; err != nil { mylogrus.MyLog.Errorf("GetLevelUsers fail:%v", err) return res, resIds } for _, u := range users { a := m.GetArea(u.Country) res[u.ID] = UserTinyArea{ ID: u.ID, ExternalId: u.ExternalId, Sex: u.Sex, Code: u.Code, Country: u.Country, Area: a, Avatar: u.Avatar, } resIds = append(resIds, u.ID) } return res, resIds }