Files
data-engine/controller/sync/platform_sync_controller.go
2026-05-29 18:39:32 +08:00

93 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package sync
import (
"context"
svc "dataengine/service/sync"
"github.com/gogf/gf/v2/frame/g"
"github.com/sirupsen/logrus"
)
var PlatformSyncController = new(syncCtrl)
type syncCtrl struct{}
// TriggerSyncReq 触发同步请求
type TriggerSyncReq struct {
g.Meta `path:"/trigger" method:"post" tags:"平台同步" summary:"触发同步" dc:"根据平台编码和接口编码触发数据同步"`
PlatformCode string `json:"platformCode" v:"required" dc:"平台编码"`
InterfaceCode string `json:"interfaceCode" v:"required" dc:"接口编码"`
FullSync bool `json:"fullSync" dc:"是否全量同步true=全量 false=增量"`
}
// TriggerSyncRes 触发同步响应
type TriggerSyncRes struct {
Success bool `json:"success"`
TableName string `json:"tableName"`
TotalRows int `json:"totalRows"`
InsertedRows int `json:"insertedRows"`
Duration string `json:"duration"`
}
// TriggerSync 触发同步
func (c *syncCtrl) TriggerSync(ctx context.Context, req *TriggerSyncReq) (*TriggerSyncRes, error) {
logrus.Infof("触发同步: platform=%s, interface=%s, fullSync=%v", req.PlatformCode, req.InterfaceCode, req.FullSync)
result, err := svc.SyncByConfig(ctx, req.PlatformCode, req.InterfaceCode, req.FullSync)
if err != nil {
return nil, err
}
return &TriggerSyncRes{
Success: true, TableName: result.TableName,
TotalRows: result.TotalRows, InsertedRows: result.InsertedRows,
Duration: result.Duration,
}, nil
}
// QueryPlatformConfigReq 查询平台配置请求
type QueryPlatformConfigReq struct {
g.Meta `path:"/config" method:"get" tags:"平台同步" summary:"查询平台配置" dc:"查看平台和接口配置"`
PlatformCode string `json:"platformCode" v:"required" dc:"平台编码"`
}
// QueryPlatformConfigRes 查询平台配置响应
type QueryPlatformConfigRes struct {
PlatformName string `json:"platformName"`
PlatformCode string `json:"platformCode"`
ApiBaseUrl string `json:"apiBaseUrl"`
AuthType string `json:"authType"`
HasToken bool `json:"hasToken"`
InterfaceCount int `json:"interfaceCount"`
Interfaces []struct {
Code string `json:"code"`
Name string `json:"name"`
Url string `json:"url"`
HasTableDef bool `json:"hasTableDef"`
} `json:"interfaces"`
}
// QueryConfig 查询配置
func (c *syncCtrl) QueryConfig(ctx context.Context, req *QueryPlatformConfigReq) (*QueryPlatformConfigRes, error) {
pm := &svc.PlatformManager{}
platform, interfaces, err := pm.GetPlatformWithInterfaces(ctx, req.PlatformCode)
if err != nil {
return nil, err
}
res := &QueryPlatformConfigRes{
PlatformName: platform.PlatformName, PlatformCode: platform.PlatformCode,
ApiBaseUrl: platform.ApiBaseUrl, AuthType: platform.AuthType,
HasToken: platform.AccessToken != "", InterfaceCount: len(interfaces),
}
for _, iface := range interfaces {
res.Interfaces = append(res.Interfaces, struct {
Code string `json:"code"`
Name string `json:"name"`
Url string `json:"url"`
HasTableDef bool `json:"hasTableDef"`
}{
Code: iface.Code, Name: iface.Name, Url: iface.Url,
HasTableDef: iface.TableDefinition != nil && len(iface.TableDefinition) > 0,
})
}
return res, nil
}