You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/relation/user_model.go

210 lines
5.1 KiB

2 years ago
package relation
import (
2 years ago
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
"Open_IM/pkg/utils"
"fmt"
"gorm.io/gorm"
"time"
)
var (
BlackListDB *gorm.DB
UserDB *gorm.DB
)
2 years ago
func InitManager() {
for k, v := range config.Config.Manager.AppManagerUid {
_, err := GetUserByUserID(v)
if err != nil {
} else {
continue
}
var appMgr User
appMgr.UserID = v
if k == 0 {
appMgr.Nickname = config.Config.Manager.AppSysNotificationName
} else {
appMgr.Nickname = "AppManager" + utils.IntToString(k+1)
}
appMgr.AppMangerLevel = constant.AppAdmin
err = UserRegister(appMgr)
if err != nil {
fmt.Println("AppManager insert error ", err.Error(), appMgr)
} else {
fmt.Println("AppManager insert ", appMgr)
}
}
}
func UserRegister(user User) error {
3 years ago
user.CreateTime = time.Now()
3 years ago
if user.AppMangerLevel == 0 {
user.AppMangerLevel = constant.AppOrdinaryUsers
}
3 years ago
if user.Birth.Unix() < 0 {
user.Birth = utils.UnixSecondToTime(0)
}
2 years ago
err := UserDB.Table("users").Create(&user).Error
if err != nil {
return err
}
return nil
}
func GetAllUser() ([]User, error) {
var userList []User
2 years ago
err := UserDB.Table("users").Find(&userList).Error
return userList, err
}
func TakeUserByUserID(userID string) (*User, error) {
var user User
2 years ago
err := UserDB.Table("users").Where("user_id=?", userID).Take(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}
func GetUserByUserID(userID string) (*User, error) {
var user User
2 years ago
err := UserDB.Table("users").Where("user_id=?", userID).Take(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}
func GetUsersByUserIDList(userIDList []string) ([]*User, error) {
var userList []*User
2 years ago
err := UserDB.Table("users").Where("user_id in (?)", userIDList).Find(&userList).Error
2 years ago
return userList, err
}
3 years ago
func GetUserNameByUserID(userID string) (string, error) {
var user User
2 years ago
err := UserDB.Table("users").Select("name").Where("user_id=?", userID).First(&user).Error
3 years ago
if err != nil {
return "", err
}
3 years ago
return user.Nickname, nil
3 years ago
}
func UpdateUserInfo(user User) error {
2 years ago
return UserDB.Where("user_id=?", user.UserID).Updates(&user).Error
}
func UpdateUserInfoByMap(user User, m map[string]interface{}) error {
2 years ago
err := UserDB.Where("user_id=?", user.UserID).Updates(m).Error
3 years ago
return err
}
3 years ago
func SelectAllUserID() ([]string, error) {
var resultArr []string
2 years ago
err := UserDB.Pluck("user_id", &resultArr).Error
if err != nil {
3 years ago
return nil, err
}
3 years ago
return resultArr, nil
}
3 years ago
func SelectSomeUserID(userIDList []string) ([]string, error) {
var resultArr []string
2 years ago
err := UserDB.Pluck("user_id", &resultArr).Error
if err != nil {
3 years ago
return nil, err
}
return resultArr, nil
}
3 years ago
func GetUsers(showNumber, pageNumber int32) ([]User, error) {
var users []User
2 years ago
err := UserDB.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
3 years ago
if err != nil {
return users, err
}
3 years ago
return users, err
}
func AddUser(userID string, phoneNumber string, name string, email string, gender int32, faceURL string, birth string) error {
_birth, err := utils.TimeStringToTime(birth)
if err != nil {
return err
}
user := User{
2 years ago
UserID: userID,
Nickname: name,
FaceURL: faceURL,
Gender: gender,
PhoneNumber: phoneNumber,
Birth: _birth,
Email: email,
Ex: "",
CreateTime: time.Now(),
3 years ago
}
2 years ago
result := UserDB.Create(&user)
3 years ago
return result.Error
3 years ago
}
2 years ago
func UsersIsBlock(userIDList []string) (inBlockUserIDList []string, err error) {
2 years ago
err = BlackListDB.Where("uid in (?) and end_disable_time > now()", userIDList).Pluck("uid", &inBlockUserIDList).Error
2 years ago
return inBlockUserIDList, err
3 years ago
}
3 years ago
type BlockUserInfo struct {
User User
3 years ago
BeginDisableTime time.Time
3 years ago
EndDisableTime time.Time
3 years ago
}
func GetUserByName(userName string, showNumber, pageNumber int32) ([]User, error) {
var users []User
2 years ago
err := UserDB.Where(" name like ?", fmt.Sprintf("%%%s%%", userName)).Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
3 years ago
return users, err
}
func GetUsersByNameAndID(content string, showNumber, pageNumber int32) ([]User, int64, error) {
var users []User
2 years ago
var count int64
2 years ago
db := UserDB.Where(" name like ? or user_id = ? ", fmt.Sprintf("%%%s%%", content), content)
2 years ago
if err := db.Count(&count).Error; err != nil {
return nil, 0, err
}
err := db.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&users).Error
return users, count, err
}
2 years ago
func GetUserIDsByEmailAndID(phoneNumber, email string) ([]string, error) {
if phoneNumber == "" && email == "" {
return nil, nil
}
2 years ago
db := UserDB
2 years ago
if phoneNumber != "" {
db = db.Where("phone_number = ? ", phoneNumber)
}
if email != "" {
db = db.Where("email = ? ", email)
}
var userIDList []string
err := db.Pluck("user_id", &userIDList).Error
return userIDList, err
}
2 years ago
func GetUsersCount(userName string) (int32, error) {
2 years ago
var count int64
2 years ago
if err := UserDB.Where(" name like ? ", fmt.Sprintf("%%%s%%", userName)).Count(&count).Error; err != nil {
3 years ago
return 0, err
}
2 years ago
return int32(count), nil
3 years ago
}
func GetBlockUsersNumCount() (int32, error) {
2 years ago
var count int64
2 years ago
if err := BlackListDB.Count(&count).Error; err != nil {
3 years ago
return 0, err
}
2 years ago
return int32(count), nil
3 years ago
}