userManager.go 4.03 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package manager

import (
	"context"
	"github.com/go-redis/redis/v8"
hujiebin's avatar
hujiebin committed
6 7
	"gorm.io/gorm"
	"hilo-userCenter/common/mylogrus"
hujiebin's avatar
hujiebin committed
8 9
	"log"
	"strconv"
hujiebin's avatar
hujiebin committed
10
	"sync"
hujiebin's avatar
hujiebin committed
11 12 13 14 15 16 17 18 19
)

const (
	user_key = "user"
)

type UserManager struct {
	Ctx         context.Context
	RedisClient *redis.Client
hujiebin's avatar
hujiebin committed
20
	MysqlDB     *gorm.DB
hujiebin's avatar
hujiebin committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

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
	}
}
hujiebin's avatar
hujiebin committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

// 国家到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
}
hujiebin's avatar
hujiebin committed
138 139 140

// 获取财富等级大于某等级的用户
// 开区间
141
func (m *UserManager) GetLevelUsers(userIds []uint64, wealthLevel int32) (map[uint64]UserTinyArea, []uint64) {
hujiebin's avatar
hujiebin committed
142
	res := make(map[uint64]UserTinyArea)
143
	var resIds []uint64
hujiebin's avatar
hujiebin committed
144 145 146 147 148 149 150 151
	// 从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)
152
		return res, resIds
hujiebin's avatar
hujiebin committed
153 154 155 156 157 158 159 160 161 162 163 164
	}
	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,
		}
165
		resIds = append(resIds, u.ID)
hujiebin's avatar
hujiebin committed
166
	}
167
	return res, resIds
hujiebin's avatar
hujiebin committed
168
}