user.go 3.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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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
}

func GetUserMapByIds(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]User, error) {
	rows, err := GetUsersByIds(model, userIds)
	if err != nil {
		return nil, err
	}
	result := make(map[mysql.ID]User, 0)
	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
}
109 110 111 112 113 114 115 116 117 118 119 120

// 通过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
121 122 123 124 125 126 127 128 129

//获取用户
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
}