101 lines
3.0 KiB
Go
101 lines
3.0 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"log"
|
|
"videoplayer/proto"
|
|
)
|
|
|
|
type Shell struct {
|
|
gorm.Model
|
|
AuthID uint `gorm:"column:auth_id"`
|
|
Server string `gorm:"column:server"`
|
|
ShellName string `gorm:"column:shell_name"`
|
|
ShellContent string `gorm:"column:shell_content"`
|
|
Status int `gorm:"column:status"` // 0 未执行 1 执行中 2 执行完成
|
|
ShellResult string `gorm:"column:shell_result"`
|
|
}
|
|
|
|
func CreateShell(shellName, shellContent, server string, uid uint) uint {
|
|
shell := Shell{ShellName: shellName, ShellContent: shellContent, Server: server, AuthID: uid}
|
|
var res *gorm.DB
|
|
if proto.Config.SERVER_SQL_LOG {
|
|
res = DB.Debug().Create(&shell)
|
|
} else {
|
|
res = DB.Create(&shell)
|
|
}
|
|
if res.Error != nil {
|
|
return 0
|
|
}
|
|
return shell.ID
|
|
}
|
|
|
|
func FindShellByID(id, uid uint) []Shell {
|
|
var shell Shell
|
|
var result *gorm.DB
|
|
|
|
if proto.Config.SERVER_SQL_LOG {
|
|
result = DB.Debug().Where("id = ? and auth_id = ?", id, uid).First(&shell)
|
|
} else {
|
|
result = DB.Where("id = ? and auth_id = ?", id, uid).First(&shell)
|
|
}
|
|
var res []Shell
|
|
res = append(res, shell)
|
|
if result.Error != nil {
|
|
log.Printf("FindShellByID failed: %v", result.Error)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func FindShellByAuthID(auth_id int) []Shell {
|
|
var shells []Shell
|
|
if proto.Config.SERVER_SQL_LOG {
|
|
DB.Debug().Exec("select * from shells where auth_id = ? order by created_at desc limit 100", auth_id).Scan(&shells)
|
|
} else {
|
|
DB.Exec("select * from shells where auth_id = ? order by created_at desc limit 100", auth_id).Scan(&shells)
|
|
}
|
|
return shells
|
|
}
|
|
|
|
func UpdateShellByID(id, authId uint, shellName, shellContent string, status int, shellResult string) bool {
|
|
pd := FindShellByID(id, authId)
|
|
if pd[0].ID == 0 {
|
|
return false
|
|
}
|
|
var result *gorm.DB
|
|
if proto.Config.SERVER_SQL_LOG {
|
|
result = DB.Debug().Model(&Shell{}).Where("id = ? and auth_id = ?", id, authId).Updates(Shell{ShellName: shellName, ShellContent: shellContent, Status: status, ShellResult: shellResult})
|
|
} else {
|
|
result = DB.Model(&Shell{}).Where("id = ? and auth_id = ?", id, authId).Updates(Shell{ShellName: shellName, ShellContent: shellContent, Status: status, ShellResult: shellResult})
|
|
}
|
|
if result.Error != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func FindShellWillRunByServer(server string, uid uint) []Shell {
|
|
var shells []Shell
|
|
if proto.Config.SERVER_SQL_LOG {
|
|
result := DB.Debug().Where("server = ? AND auth_id = ? AND status = 0", server, uid).
|
|
Order("created_at DESC").
|
|
Limit(100).
|
|
Find(&shells)
|
|
if result.Error != nil {
|
|
// 若查询过程中出现错误,输出错误信息
|
|
log.Printf("Failed to query shells: %v\n", result.Error)
|
|
}
|
|
} else {
|
|
//DB.Exec("select * from shells where server = ? and auth_id = ? and status = 0 order by created_at desc limit 100", server, uid).Scan(&shells)
|
|
result := DB.Where("server = ? AND auth_id = ? AND status = 0", server, uid).
|
|
Order("created_at DESC").
|
|
Limit(100).
|
|
Find(&shells)
|
|
if result.Error != nil {
|
|
// 若查询过程中出现错误,输出错误信息
|
|
log.Printf("Failed to query shells: %v\n", result.Error)
|
|
}
|
|
}
|
|
return shells
|
|
}
|