videoplayer/dao/shell.go

134 lines
3.7 KiB
Go
Raw Permalink Normal View History

2025-02-22 14:44:26 +08:00
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 执行完成 3 执行出错
ShellRuntime float64 `gorm:"column:shell_runtime"` // 执行时间,单位秒
ShellDuration float64 `gorm:"column:shell_duration"` // 执行时长,单位秒
ShellResult string `gorm:"column:shell_result"`
2025-02-22 14:44:26 +08:00
}
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 {
2025-03-05 19:15:48 +08:00
result = DB.Debug().Where("id = ? and auth_id = ?", id, uid).First(&shell)
2025-02-22 14:44:26 +08:00
} else {
2025-03-05 19:15:48 +08:00
result = DB.Where("id = ? and auth_id = ?", id, uid).First(&shell)
2025-02-22 14:44:26 +08:00
}
var res []Shell
res = append(res, shell)
if result.Error != nil {
2025-03-05 19:24:23 +08:00
log.Printf("FindShellByID failed: %v", result.Error)
2025-02-22 14:44:26 +08:00
}
return res
}
func FindShellByAuthID(auth_id int) []Shell {
var shells []Shell
if proto.Config.SERVER_SQL_LOG {
2025-03-11 14:32:39 +08:00
DB.Debug().Where("auth_id = ?", auth_id).Order("created_at DESC").Limit(100).Find(&shells)
2025-02-22 14:44:26 +08:00
} else {
2025-03-11 14:32:39 +08:00
DB.Where("auth_id = ?", auth_id).Order("created_at DESC").Limit(100).Find(&shells)
2025-02-22 14:44:26 +08:00
}
return shells
}
func UpdateShellByID(id, authId uint, shellName, shellContent string, status int, shellResult string, shellRuntime, shellDuration float64) bool {
2025-02-22 14:44:26 +08:00
var result *gorm.DB
shell := Shell{ShellName: shellName, ShellContent: shellContent, Status: status, ShellResult: shellResult, ShellRuntime: shellRuntime, ShellDuration: shellDuration}
var db2 *gorm.DB
2025-02-22 14:44:26 +08:00
if proto.Config.SERVER_SQL_LOG {
db2 = DB.Debug()
2025-02-22 14:44:26 +08:00
} else {
db2 = DB
2025-02-22 14:44:26 +08:00
}
result = db2.Model(&Shell{}).Where("id = ? and auth_id = ?", id, authId).Updates(shell)
2025-02-22 14:44:26 +08:00
if result.Error != nil {
return false
}
return true
}
func FindShellWillRunByServer(server string, uid uint) []Shell {
var shells []Shell
if proto.Config.SERVER_SQL_LOG {
2025-03-05 19:21:42 +08:00
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)
}
2025-02-22 14:44:26 +08:00
} else {
2025-03-05 19:21:42 +08:00
//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)
}
2025-02-22 14:44:26 +08:00
}
return shells
}
2025-06-06 14:16:44 +08:00
func DeleteShellByID(id, authId uint) bool {
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB
} else {
db2 = DB.Debug()
}
result := db2.Where("id = ? and auth_id = ?", id, authId).Delete(&Shell{})
if result.Error != nil {
log.Printf("DeleteShellByID failed: %v", result.Error)
return false
}
return true
}
func DeleteShellByIDV2(id uint) bool {
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB
} else {
db2 = DB.Debug()
}
result := db2.Where("id = ? ", id).Delete(&Shell{})
if result.Error != nil {
log.Printf("DeleteShellByID failed: %v", result.Error)
return false
}
return true
}