架构变更: - Python/FastAPI → Go/Gin 重写(Python 后端保留在 backend-python/) - GORM + SQLite(兼容现有数据库表结构) - JWT 认证 + RBAC 权限中间件 Go 后端结构: - cmd/server/main.go — 入口,端口 9000 - config/config.go — 环境变量配置 - internal/model/ — GORM 模型(30张表,完全兼容 SQLite) - internal/handler/ — auth/instance/ticket/dashboard/ghost handler - internal/middleware/ — JWT 认证 + CORS - internal/ghost/ — gh-ost 集成模块(同步/异步执行,任务管理) - router/router.go — API 路由注册(/api/v1/...) gh-ost 集成: - RunGhOst() 同步执行 - RunGhOstAsync() 异步执行 + 进度追踪 - TaskManager 管理多个并发任务 - API: POST /ghost/dry-run, /ghost/execute, GET /ghost/status/:taskId 核心 API 已对齐: - POST /auth/login (JWT) - GET /dashboard/stats, /dashboard/my-tickets - GET /tickets/statistics, CRUD + approve/execute - GET/POST/PUT/DELETE /instances - GET /users, /audit-rules, /announcements, /settings 已验证: - 登录 admin/admin123 ✅ - Dashboard 99工单/110查询/4实例 ✅ - 工单统计按角色过滤 ✅
94 lines
2.5 KiB
Go
94 lines
2.5 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
"sql-platform/internal/model"
|
|
)
|
|
|
|
type InstanceService struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
func NewInstanceService(db *gorm.DB) *InstanceService {
|
|
return &InstanceService{DB: db}
|
|
}
|
|
|
|
// CreateInstance creates a new database instance
|
|
func (s *InstanceService) CreateInstance(instance *model.Instance) error {
|
|
return s.DB.Create(instance).Error
|
|
}
|
|
|
|
// GetInstanceByID retrieves an instance by ID
|
|
func (s *InstanceService) GetInstanceByID(id uint) (*model.Instance, error) {
|
|
var instance model.Instance
|
|
if err := s.DB.First(&instance, id).Error; err != nil {
|
|
return nil, errors.New("instance not found")
|
|
}
|
|
return &instance, nil
|
|
}
|
|
|
|
// ListInstances returns paginated instances
|
|
func (s *InstanceService) ListInstances(page, pageSize int, dbType, env string) ([]model.Instance, int64, error) {
|
|
var instances []model.Instance
|
|
var total int64
|
|
|
|
db := s.DB.Model(&model.Instance{})
|
|
if dbType != "" {
|
|
db = db.Where("db_type = ?", dbType)
|
|
}
|
|
if env != "" {
|
|
db = db.Where("env = ?", env)
|
|
}
|
|
|
|
db.Count(&total)
|
|
offset := (page - 1) * pageSize
|
|
if err := db.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&instances).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return instances, total, nil
|
|
}
|
|
|
|
// UpdateInstance updates an instance
|
|
func (s *InstanceService) UpdateInstance(id uint, updates map[string]interface{}) error {
|
|
result := s.DB.Model(&model.Instance{}).Where("id = ?", id).Updates(updates)
|
|
if result.RowsAffected == 0 {
|
|
return errors.New("instance not found")
|
|
}
|
|
return result.Error
|
|
}
|
|
|
|
// DeleteInstance soft-deletes an instance
|
|
func (s *InstanceService) DeleteInstance(id uint) error {
|
|
return s.DB.Delete(&model.Instance{}, id).Error
|
|
}
|
|
|
|
// TestConnection tests the database connection
|
|
func (s *InstanceService) TestConnection(instance *model.Instance) error {
|
|
// TODO: Implement actual connection test based on db_type
|
|
return nil
|
|
}
|
|
|
|
// ListInstancesByIDs returns instances matching the given IDs
|
|
func (s *InstanceService) ListInstancesByIDs(ids []uint) ([]model.Instance, error) {
|
|
var instances []model.Instance
|
|
if err := s.DB.Where("id IN ?", ids).Find(&instances).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return instances, nil
|
|
}
|
|
|
|
// GetInstanceGroups returns all instance groups
|
|
func (s *InstanceService) GetInstanceGroups() ([]model.InstanceGroup, error) {
|
|
var groups []model.InstanceGroup
|
|
err := s.DB.Find(&groups).Error
|
|
return groups, err
|
|
}
|
|
|
|
// CreateInstanceGroup creates a new instance group
|
|
func (s *InstanceService) CreateInstanceGroup(group *model.InstanceGroup) error {
|
|
return s.DB.Create(group).Error
|
|
}
|