80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package api
|
||
|
||
import (
|
||
"fmt"
|
||
"u-desk/internal/service"
|
||
"u-desk/internal/storage/models"
|
||
)
|
||
|
||
// TabAPI 标签页API
|
||
type TabAPI struct {
|
||
tabService *service.TabService
|
||
}
|
||
|
||
// NewTabAPI 创建标签页API
|
||
func NewTabAPI() (*TabAPI, error) {
|
||
tabService, err := service.NewTabService()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &TabAPI{tabService: tabService}, nil
|
||
}
|
||
|
||
// SaveSqlTabs 保存SQL标签页列表(接收 map 格式,转换为模型)
|
||
func (api *TabAPI) SaveSqlTabs(tabs []map[string]interface{}) error {
|
||
sqlTabs := make([]models.SqlTab, len(tabs))
|
||
for idx, tabData := range tabs {
|
||
tab := models.SqlTab{
|
||
Order: idx,
|
||
}
|
||
|
||
// 处理 ID
|
||
if id, ok := tabData["id"].(float64); ok && id > 0 {
|
||
tab.ID = uint(id)
|
||
}
|
||
|
||
// 处理标题
|
||
if title, ok := tabData["title"].(string); ok {
|
||
tab.Title = title
|
||
} else {
|
||
tab.Title = fmt.Sprintf("查询 %d", idx+1)
|
||
}
|
||
|
||
// 处理内容
|
||
if content, ok := tabData["content"].(string); ok {
|
||
tab.Content = content
|
||
}
|
||
|
||
// 处理连接ID
|
||
if connId, ok := tabData["connectionId"].(float64); ok && connId > 0 {
|
||
connID := uint(connId)
|
||
tab.ConnectionID = &connID
|
||
}
|
||
|
||
sqlTabs[idx] = tab
|
||
}
|
||
return api.tabService.SaveTabs(sqlTabs)
|
||
}
|
||
|
||
// ListSqlTabs 获取SQL标签页列表(返回 map 格式)
|
||
func (api *TabAPI) ListSqlTabs() ([]map[string]interface{}, error) {
|
||
tabs, err := api.tabService.ListTabs()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
result := make([]map[string]interface{}, len(tabs))
|
||
for i, tab := range tabs {
|
||
result[i] = map[string]interface{}{
|
||
"id": tab.ID,
|
||
"title": tab.Title,
|
||
"content": tab.Content,
|
||
"connectionId": tab.ConnectionID,
|
||
"order": tab.Order,
|
||
"createdAt": tab.CreatedAt.Format("2006-01-02 15:04:05"),
|
||
"updatedAt": tab.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||
}
|
||
}
|
||
return result, nil
|
||
}
|