2025-03-18 13:22:20 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"StuAcaWorksAI/proto"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
|
|
gorm.Model
|
|
|
|
|
|
Name string `gorm:"column:name"`
|
|
|
|
|
|
Age int `gorm:"column:age"`
|
|
|
|
|
|
Email string `gorm:"column:email"`
|
|
|
|
|
|
Password string `gorm:"column:password"`
|
|
|
|
|
|
Gender string `gorm:"column:gender"`
|
|
|
|
|
|
Role string `gorm:"column:role"`
|
|
|
|
|
|
Redis bool `gorm:"column:redis"`
|
|
|
|
|
|
Run bool `gorm:"column:run"`
|
|
|
|
|
|
Upload bool `gorm:"column:upload"`
|
|
|
|
|
|
VideoFunc bool `gorm:"column:video_func"` //视频功能
|
|
|
|
|
|
DeviceFunc bool `gorm:"column:device_func"` //设备功能
|
|
|
|
|
|
CIDFunc bool `gorm:"column:cid_func"` //持续集成功能
|
|
|
|
|
|
Avatar string `gorm:"column:avatar"`
|
|
|
|
|
|
CreateTime string `gorm:"column:create_time"`
|
2025-04-24 20:07:44 +08:00
|
|
|
|
QQ int64 `gorm:"column:qq"`
|
|
|
|
|
|
QQOpenID string `gorm:"column:qq_openid"`
|
2025-03-18 13:22:20 +08:00
|
|
|
|
UpdateTime string `gorm:"column:update_time"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-27 10:00:19 +08:00
|
|
|
|
// 存储第三方统一信息
|
|
|
|
|
|
type ThirdPartyUserInfo struct {
|
|
|
|
|
|
gorm.Model
|
|
|
|
|
|
UserID int `json:"user_id"` // 用户ID,本系统的用户id
|
|
|
|
|
|
ThirdPartyID int `json:"third_party_id"` // 第三方用户ID
|
|
|
|
|
|
ThirdPartyPlatform string `json:"third_party_platform"` // 第三方平台名称,qq,github
|
|
|
|
|
|
ThirdPartyUserName string `json:"third_party_user_name"` // 第三方用户名
|
|
|
|
|
|
ThirdPartyUserAvatar string `json:"third_party_user_avatar"` // 第三方用户头像
|
|
|
|
|
|
ThirdPartyUserUrl string `json:"third_party_user_url"` // 第三方用户主页,可选
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-18 13:22:20 +08:00
|
|
|
|
func CreateUser(name, password, email, gender string, age int) uint {
|
|
|
|
|
|
user := User{Name: name, Email: email, Password: password, Gender: gender, Age: age}
|
|
|
|
|
|
res := DB.Create(&user)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return user.ID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DeleteUserByID(id int) int {
|
|
|
|
|
|
res := DB.Delete(&User{}, id)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return id
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-20 13:12:30 +08:00
|
|
|
|
func FindUserByID(id int) []User {
|
|
|
|
|
|
var users []User
|
|
|
|
|
|
//不查询密码
|
2025-03-18 13:22:20 +08:00
|
|
|
|
DB.Where("id = ?", id).First(&users)
|
|
|
|
|
|
return users
|
|
|
|
|
|
}
|
|
|
|
|
|
func FindUserByID2(id int) User {
|
|
|
|
|
|
var user User
|
|
|
|
|
|
DB.Where("id = ?", id).First(&user)
|
|
|
|
|
|
return user
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func FindUserByUserID(id int) User {
|
|
|
|
|
|
var user User
|
|
|
|
|
|
DB.Where("id = ?", id).First(&user)
|
|
|
|
|
|
return user
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func FindUserByName(name string) User {
|
|
|
|
|
|
var user User
|
|
|
|
|
|
fmt.Println("name:", name)
|
|
|
|
|
|
DB.Where("name = ?", name).First(&user)
|
|
|
|
|
|
return user
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据name模糊查询,邮箱也是,不查询密码
|
2025-03-20 13:12:30 +08:00
|
|
|
|
func FindUserByNameLike(name string) []User {
|
|
|
|
|
|
var users []User
|
2025-03-18 13:22:20 +08:00
|
|
|
|
DB.Where("name LIKE ? OR email LIKE ?", "%"+name+"%", "%"+name+"%").Find(&users).Limit(32)
|
|
|
|
|
|
return users
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func FindUserByEmail(email string) User {
|
|
|
|
|
|
var user User
|
|
|
|
|
|
DB.Where("email = ?", email).First(&user)
|
|
|
|
|
|
return user
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func UpdateUserByID(id int, name, password, email string) {
|
|
|
|
|
|
DB.Model(&User{}).Where("id = ?", id).Updates(User{Name: name, Password: password, Email: email})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 管理员修改用户信息
|
|
|
|
|
|
func UpdateUserByID2(id int, req proto.UpdateUserInfoReq) error {
|
|
|
|
|
|
updateData := make(map[string]interface{})
|
|
|
|
|
|
updateData["Name"] = req.Username
|
|
|
|
|
|
updateData["Age"] = req.Age
|
|
|
|
|
|
updateData["Role"] = req.Role
|
|
|
|
|
|
updateData["Run"] = req.Run
|
|
|
|
|
|
updateData["Redis"] = req.Redis
|
|
|
|
|
|
updateData["Upload"] = req.Upload
|
|
|
|
|
|
updateData["VideoFunc"] = req.VideoFunc
|
|
|
|
|
|
updateData["DeviceFunc"] = req.DeviceFunc
|
|
|
|
|
|
updateData["CIDFunc"] = req.CIDFunc
|
|
|
|
|
|
updateData["Avatar"] = req.Avatar
|
|
|
|
|
|
updateData["Gender"] = req.Gender
|
2025-04-24 20:07:44 +08:00
|
|
|
|
updateData["QQ"] = req.QQ
|
2025-03-18 13:22:20 +08:00
|
|
|
|
res := DB.Model(&User{}).Where("id =?", id).Updates(updateData)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return res.Error
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用户修改自己的信息
|
|
|
|
|
|
func UpdateUserByID3(id int, req proto.UpdateUserInfoReq) error {
|
2025-04-24 20:07:44 +08:00
|
|
|
|
res := DB.Model(&User{}).Where("id = ?", id).Updates(User{Name: req.Username, Age: req.Age, Avatar: req.Avatar, Gender: req.Gender, QQ: req.QQ})
|
2025-03-18 13:22:20 +08:00
|
|
|
|
return res.Error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用户数据同步-添加
|
|
|
|
|
|
func AddUserSync(req proto.UserAddOrUpdate) uint {
|
|
|
|
|
|
res := DB.Exec("insert into users (id, created_at, updated_at, deleted_at, name, age, email, password,gender,role,redis,run,upload,video_func,device_func,cid_func,avatar,create_time,update_time) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", req.ID, req.CreatedAt, req.UpdatedAt, req.DeletedAt, req.Name, req.Age, req.Email, req.Password, req.Gender, req.Role, req.Redis, req.Run, req.Upload, req.VideoFunc, req.DeviceFunc, req.CIDFunc, req.Avatar, req.CreateTime, req.UpdateTime)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
res = DB.Debug().Exec("update users set deleted_at=null where id=?", req.ID)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return req.ID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用户数据同步-更新
|
|
|
|
|
|
func UpdateUserSync(req proto.UserAddOrUpdate) uint {
|
|
|
|
|
|
//事务
|
|
|
|
|
|
res := DB.Exec("update users set created_at=?, updated_at=?, deleted_at=?, name=?, age=?, email=?, password=?,gender=?,role=?,redis=?,run=?,upload=?,video_func=?,device_func=?,cid_func=?,avatar=?,create_time=?,update_time=? where id=?", req.CreatedAt, req.UpdatedAt, req.DeletedAt, req.Name, req.Age, req.Email, req.Password, req.Gender, req.Role, req.Redis, req.Run, req.Upload, req.VideoFunc, req.DeviceFunc, req.CIDFunc, req.Avatar, req.CreateTime, req.UpdateTime, req.ID)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
res = DB.Debug().Exec("update users set deleted_at=null where id=?", req.ID)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return req.ID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用户数据同步-删除
|
|
|
|
|
|
func DeleteUserSync(req proto.UserDelID) uint {
|
|
|
|
|
|
res := DB.Delete(&User{}, req.ID)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return req.ID
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取所有用户
|
|
|
|
|
|
func GetAllUser() []User {
|
|
|
|
|
|
var users []User
|
|
|
|
|
|
DB.Find(&users)
|
|
|
|
|
|
return users
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用户数据同步
|
|
|
|
|
|
type UserSyncResp struct {
|
|
|
|
|
|
Update []User `json:"update" form:"update"` //更新用户
|
|
|
|
|
|
Add []User `json:"add" form:"add"` //添加用户
|
|
|
|
|
|
Delete []proto.UserDelID `json:"delete" form:"delete"` //删除用户
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空用户表
|
|
|
|
|
|
func ClearAllUsers() error {
|
|
|
|
|
|
res := DB.Exec("TRUNCATE TABLE users")
|
|
|
|
|
|
return res.Error
|
|
|
|
|
|
}
|
2025-03-20 13:12:30 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取前20个用户
|
|
|
|
|
|
func FindUsersDefault() []User {
|
|
|
|
|
|
var users []User
|
|
|
|
|
|
DB.Limit(20).Find(&users)
|
|
|
|
|
|
return users
|
|
|
|
|
|
}
|
2025-03-30 13:22:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 用户的信息统计数据
|
|
|
|
|
|
type UserStatistics struct {
|
|
|
|
|
|
SessionCount int64 `json:"session_count" form:"session_count"` //会话数量
|
|
|
|
|
|
FileCount int64 `json:"file_count" form:"file_count"` //文件数量
|
|
|
|
|
|
MessageCount int64 `json:"message_count" form:"message_count"` //消息数量,提问数量
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func UserStatisticsData(userID int) UserStatistics {
|
|
|
|
|
|
userStatistics := UserStatistics{}
|
2025-03-30 13:32:56 +08:00
|
|
|
|
DB.Model(&Session{}).Where("user_id = ? ", userID).Count(&userStatistics.SessionCount)
|
|
|
|
|
|
DB.Model(&FileAuth{}).Where("auth_id = ?", userID).Count(&userStatistics.FileCount)
|
|
|
|
|
|
DB.Model(&Message{}).Where("from_id = ?", userID).Count(&userStatistics.MessageCount)
|
2025-03-30 13:22:40 +08:00
|
|
|
|
return userStatistics
|
|
|
|
|
|
}
|
2025-03-31 15:36:34 +08:00
|
|
|
|
|
|
|
|
|
|
func FindUserNum() int64 {
|
|
|
|
|
|
var count int64
|
|
|
|
|
|
DB.Model(&User{}).Count(&count)
|
|
|
|
|
|
return count
|
|
|
|
|
|
}
|
2025-04-27 10:00:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据用户id获取第三方平台信息
|
|
|
|
|
|
func FindThirdPartyUserInfoByUserID(userID int) []ThirdPartyUserInfo {
|
|
|
|
|
|
var thirdPartyUserInfos []ThirdPartyUserInfo
|
|
|
|
|
|
DB.Where("user_id = ?", userID).Find(&thirdPartyUserInfos)
|
|
|
|
|
|
return thirdPartyUserInfos
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据平台用户id获取信息
|
|
|
|
|
|
func FindThirdPartyUserInfoByThirdPartyID(thirdPartyID int) []ThirdPartyUserInfo {
|
|
|
|
|
|
var thirdPartyUserInfo []ThirdPartyUserInfo
|
|
|
|
|
|
DB.Where("third_party_id = ?", thirdPartyID).First(&thirdPartyUserInfo)
|
|
|
|
|
|
return thirdPartyUserInfo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据第三方平台名称和用户id获取信息
|
|
|
|
|
|
func FindThirdPartyUserInfoByPlatformAndUserID(thirdPartyPlatform string, userID int) []ThirdPartyUserInfo {
|
|
|
|
|
|
var thirdPartyUserInfo []ThirdPartyUserInfo
|
|
|
|
|
|
DB.Where("third_party_platform = ? and user_id = ?", thirdPartyPlatform, userID).First(&thirdPartyUserInfo)
|
|
|
|
|
|
return thirdPartyUserInfo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CreateThirdPartyUserInfo(userID, thirdPartyID int, thirdPartyPlatform, thirdPartyUserName, thirdPartyUserAvatar, thirdPartyUserUrl string) uint {
|
|
|
|
|
|
thirdPartyUserInfo := ThirdPartyUserInfo{UserID: userID, ThirdPartyID: thirdPartyID, ThirdPartyPlatform: thirdPartyPlatform, ThirdPartyUserName: thirdPartyUserName, ThirdPartyUserAvatar: thirdPartyUserAvatar, ThirdPartyUserUrl: thirdPartyUserUrl}
|
|
|
|
|
|
res := DB.Create(&thirdPartyUserInfo)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return thirdPartyUserInfo.ID
|
|
|
|
|
|
}
|
2025-04-27 10:35:41 +08:00
|
|
|
|
func CreateThirdPartyUserInfoV2(thirdPartyUserInfo *ThirdPartyUserInfo) uint {
|
|
|
|
|
|
db2 := DB
|
|
|
|
|
|
if proto.Config.SERVER_SQL_LOG {
|
|
|
|
|
|
db2 = DB.Debug()
|
|
|
|
|
|
}
|
|
|
|
|
|
res := db2.Create(thirdPartyUserInfo)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return thirdPartyUserInfo.ID
|
|
|
|
|
|
}
|
2025-04-27 10:00:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除
|
|
|
|
|
|
func DeleteThirdPartyUserInfoByID(id int) int {
|
|
|
|
|
|
res := DB.Delete(&ThirdPartyUserInfo{}, id)
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
return id
|
|
|
|
|
|
}
|
2025-04-27 11:26:39 +08:00
|
|
|
|
|
|
|
|
|
|
func DeleteThirdPartyLoginByID(id int, userID int) error {
|
|
|
|
|
|
res := DB.Where("id = ? and user_id = ?", id, userID).Delete(&ThirdPartyUserInfo{})
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
|
return res.Error
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|