user.go 4.48 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
package user_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"hilo-group/_const/enum/user_e"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
)

//用户信息
type User struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	ExternalId    mysql.Str
	Avatar        mysql.Str
	DefaultAvatar bool
	Nick          mysql.Str
	Sex           mysql.Sex
	Birthday      mysql.Timestamp
	Country       mysql.Str
	CountryIcon   mysql.Str
	Language      mysql.Str
	Description   mysql.Str
	Code          mysql.Str
	OriginCode    mysql.Str
	IsPush        mysql.OpenClose
	IsShowAge     mysql.OpenClose
	Status        user_e.UserStatus
	DeviceType    mysql.Str
	LogoutTime    int64
}

type UserTiny struct {
	ID           uint64 `json:"id,omitempty"`
	ExternalId   string `json:"externalId"`
	Avatar       string `json:"avatar"`
	Nick         string `json:"nick"`
	Sex          uint8  `json:"sex"` // 1男2女
	Code         string `json:"code"`
	Description  string `json:"description"`
	Country      string `json:"country"`
	CountryIcon  string `json:"countryIcon"`
	IsPrettyCode bool   `json:"isPrettyCode"` // 是否靓号
	IsLogout     bool   `json:"isLogout"`     //是否注销 true:已经注销, false:没有注销
	//生日,如果是其它人用户信息,年龄则按照是否展示显示,如果是本人,年龄则按照是否存在展示
	Birthday *uint64 `json:"birthday"`
}

func (u User) IsPrettyCode() bool {
	return u.Code != u.OriginCode
}

//获取用户
func GetUser(model *domain.Model, id mysql.ID) (*User, error) {
	var user User
	if err := model.Db.WithContext(model.Context).Where(&User{
		Entity: mysql.Entity{ID: id},
	}).First(&user).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	user.Model = model
	return &user, nil
}

// 通过code获取用户
func GetUserByCode(model *domain.Model, code string) (*User, error) {
	if code == "" {
		return nil, bizerr.InvalidParameter
	}
	var user User
	if err := model.Db.Where(&User{Code: code}).First(&user).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	user.Model = model
	return &user, nil
}

//获取用户信息
func GetUsersByIds(model *domain.Model, userIds []mysql.ID) ([]User, error) {
	var users []User
	if err := model.Db.Model(User{}).Where("id IN (?)", userIds).Find(&users).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	return users, nil
}

hujiebin's avatar
hujiebin committed
88
// 分批获取userIds
hujiebin's avatar
hujiebin committed
89 90
func GetUserMapByIds(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]User, error) {
	result := make(map[mysql.ID]User, 0)
hujiebin's avatar
hujiebin committed
91 92 93 94 95 96
	end := 500
	if end > len(userIds) {
		end = len(userIds)
	}
	var rows []User
	start := 0
hujiebin's avatar
hujiebin committed
97
	for start < len(userIds) {
hujiebin's avatar
hujiebin committed
98 99 100 101 102 103 104 105
		tmp, err := GetUsersByIds(model, userIds[start:end])
		if err != nil {
			return result, err
		} else {
			rows = append(rows, tmp...)
		}
		start += 500
		end += 500
hujiebin's avatar
hujiebin committed
106 107 108 109
		if end > len(userIds) {
			end = len(userIds)
		}
		//model.Log.Infof("GetUserMapByIds start:%v-end:%v", start, end)
hujiebin's avatar
hujiebin committed
110
	}
hujiebin's avatar
hujiebin committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	for _, i := range rows {
		result[i.ID] = i
	}
	return result, nil
}

// 通过externalId获取用户
func GetUserByExtId(model *domain.Model, externalId string) (*User, error) {
	var user User
	if err := model.Db.Where(&User{ExternalId: externalId}).First(&user).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	user.Model = model
	return &user, nil
}
126 127 128 129 130 131 132 133 134 135 136 137

// 通过externalId批量获取用户
func BatchGetUserByExtIds(model *domain.Model, extIds []string) ([]User, error) {
	users := make([]User, 0)
	if err := model.Db.Where("external_id IN ?", extIds).Find(&users).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	for _, i := range users {
		i.Model = model
	}
	return users, nil
}
hujiebin's avatar
hujiebin committed
138 139 140 141 142 143 144 145 146

//获取用户
func GetUsers(model *domain.Model, ids []mysql.ID) ([]*User, error) {
	res := make([]*User, 0)
	if err := model.Db.WithContext(model.Context).Where("id in (?)", ids).Find(&res).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	return res, nil
}
iamhujiebin's avatar
iamhujiebin committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

// 获取用户externalIds
// return []externalIds map[userId]extId map[userId]code
func GetUserExternalIds(model *domain.Model, userIds []mysql.ID) ([]string, map[uint64]string, map[uint64]string) {
	var res []string
	var m = make(map[uint64]string)
	var c = make(map[uint64]string)
	if len(userIds) <= 0 {
		return res, m, c
	}
	var users []User
	if err := model.DB().Model(User{}).Where("id in (?)", userIds).Select("id,external_id").Find(&users).Error; err != nil {
		model.Log.Errorf("GetUserExternalIds fail:%v", err)
	}
	for _, user := range users {
		res = append(res, user.ExternalId)
		m[user.ID] = user.ExternalId
		c[user.ID] = user.Code
	}
	return res, m, c
}