user.go 2.85 KB
Newer Older
chenweijian's avatar
chenweijian 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 (
	"hilo-user/_const/enum/user_e"
	"hilo-user/domain"
	"hilo-user/myerr"
	"hilo-user/myerr/bizerr"
	"hilo-user/resource/mysql"
)

//用户信息
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
}