Skip to content

Commit 3b2b2a9

Browse files
committed
fix: 因重构分组逻辑导致无法删除主控的bug
1 parent 75ab4a3 commit 3b2b2a9

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

internal/api/tunnel.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,11 @@ func (h *TunnelHandler) HandleDeleteTunnel(c *gin.Context) {
417417
var endpointID int64
418418
if err := h.tunnelService.DB().QueryRow(`SELECT id, endpoint_id FROM tunnels WHERE instance_id = ?`, req.InstanceID).Scan(&tunnelID, &endpointID); err != nil {
419419
} else {
420-
// 清理隧道标签关联
421-
if _, err := h.tunnelService.DB().Exec("DELETE FROM tunnel_tags WHERE tunnel_id = ?", tunnelID); err != nil {
422-
log.Warnf("[API] 删除隧道标签关联失败: tunnelID=%d, err=%v", tunnelID, err)
420+
// 清理隧道分组关联
421+
if _, err := h.tunnelService.DB().Exec("DELETE FROM tunnel_groups WHERE tunnel_id = ?", tunnelID); err != nil {
422+
log.Warnf("[API] 删除隧道分组关联失败: tunnelID=%d, err=%v", tunnelID, err)
423423
} else {
424-
log.Infof("[API] 已删除隧道标签关联: tunnelID=%d", tunnelID)
424+
log.Infof("[API] 已删除隧道分组关联: tunnelID=%d", tunnelID)
425425
}
426426
}
427427

internal/endpoint/service.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (s *Service) UpdateEndpoint(req UpdateEndpointRequest) (*Endpoint, error) {
250250
// 修改配置:更新名称、URL、API路径,可选的API密钥
251251
updates := make(map[string]interface{})
252252
needUpdateCache := false
253-
253+
254254
if req.Name != "" {
255255
// 检查新名称是否已存在
256256
var count int64
@@ -262,7 +262,7 @@ func (s *Service) UpdateEndpoint(req UpdateEndpointRequest) (*Endpoint, error) {
262262
}
263263
updates["name"] = req.Name
264264
}
265-
265+
266266
if req.URL != "" && req.URL != endpoint.URL {
267267
// 检查URL是否重复
268268
var count int64
@@ -279,17 +279,17 @@ func (s *Service) UpdateEndpoint(req UpdateEndpointRequest) (*Endpoint, error) {
279279
}
280280
needUpdateCache = true
281281
}
282-
282+
283283
if req.APIPath != "" && req.APIPath != endpoint.APIPath {
284284
updates["api_path"] = req.APIPath
285285
needUpdateCache = true
286286
}
287-
287+
288288
if req.APIKey != "" && req.APIKey != endpoint.APIKey {
289289
updates["api_key"] = req.APIKey
290290
needUpdateCache = true
291291
}
292-
292+
293293
updates["updated_at"] = time.Now()
294294

295295
if err := s.db.Model(&endpoint).Updates(updates).Error; err != nil {
@@ -311,7 +311,7 @@ func (s *Service) UpdateEndpoint(req UpdateEndpointRequest) (*Endpoint, error) {
311311
if req.APIKey == "" {
312312
return nil, errors.New("API密钥不能为空")
313313
}
314-
314+
315315
updates := map[string]interface{}{
316316
"api_key": req.APIKey,
317317
"updated_at": time.Now(),
@@ -336,11 +336,11 @@ func (s *Service) UpdateEndpoint(req UpdateEndpointRequest) (*Endpoint, error) {
336336
// DeleteEndpoint 删除端点
337337
func (s *Service) DeleteEndpoint(id int64) error {
338338
return s.db.Transaction(func(tx *gorm.DB) error {
339-
// 1) 删除隧道标签关联表记录(通过隧道ID关联)
340-
if err := tx.Exec("DELETE FROM tunnel_tags WHERE tunnel_id IN (SELECT id FROM tunnels WHERE endpoint_id = ?)", id).Error; err != nil {
339+
// 1) 删除隧道分组关联表记录(通过隧道ID关联)
340+
if err := tx.Exec("DELETE FROM tunnel_groups WHERE tunnel_id IN (SELECT id FROM tunnels WHERE endpoint_id = ?)", id).Error; err != nil {
341341
// 忽略记录不存在的错误
342342
if !errors.Is(err, gorm.ErrRecordNotFound) {
343-
return fmt.Errorf("删除隧道标签关联失败: %v", err)
343+
return fmt.Errorf("删除隧道分组关联失败: %v", err)
344344
}
345345
}
346346

internal/tunnel/service.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (s *Service) GetTunnels() ([]TunnelWithStats, error) {
4646
}
4747

4848
query := `
49-
SELECT
49+
SELECT
5050
t.id, t.instance_id, t.name, t.endpoint_id, t.type,
5151
t.tunnel_address, t.tunnel_port, t.target_address, t.target_port,
5252
t.tls_mode, t.cert_path, t.key_path, t.log_level, t.command_line,
@@ -57,8 +57,8 @@ func (s *Service) GetTunnels() ([]TunnelWithStats, error) {
5757
tag.id AS tag_id, tag.name AS tag_name
5858
FROM tunnels t
5959
LEFT JOIN endpoints e ON t.endpoint_id = e.id
60-
LEFT JOIN tunnel_tags tt ON t.id = tt.tunnel_id
61-
LEFT JOIN tags tag ON tt.tag_id = tag.id
60+
LEFT JOIN tunnel_groups tt ON t.id = tt.tunnel_id
61+
LEFT JOIN groups tag ON tt.group_id = tag.id
6262
ORDER BY t.created_at DESC
6363
`
6464

@@ -2590,8 +2590,8 @@ func (s *Service) getGroupsByTunnelIDs(tunnelIDs []int64) (map[int64][]models.Gr
25902590

25912591
query := fmt.Sprintf(`
25922592
SELECT tt.tunnel_id, t.id, t.name
2593-
FROM tunnel_tags tt
2594-
JOIN tags t ON tt.tag_id = t.id
2593+
FROM tunnel_groups tt
2594+
JOIN groups t ON tt.group_id = t.id
25952595
WHERE tt.tunnel_id IN (%s)
25962596
`, placeholders)
25972597

0 commit comments

Comments
 (0)