306 lines
9.2 KiB
Go
306 lines
9.2 KiB
Go
package dict
|
||
|
||
import (
|
||
"context"
|
||
api_feature "dataengine/consts/api-feature"
|
||
dao "dataengine/dao/dict"
|
||
dto "dataengine/model/dto/dict"
|
||
entity "dataengine/model/entity/dict"
|
||
"strconv"
|
||
"time"
|
||
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
"github.com/olekukonko/errors"
|
||
)
|
||
|
||
type datasourcePlatformService struct{}
|
||
|
||
// DatasourcePlatform 数据源平台服务
|
||
var DatasourcePlatform = new(datasourcePlatformService)
|
||
|
||
// Create 创建数据源平台
|
||
func (s *datasourcePlatformService) Create(ctx context.Context, req *dto.CreateDatasourcePlatformReq) (res *dto.CreateDatasourcePlatformRes, err error) {
|
||
// 检查平台编码是否重复
|
||
exists, err := dao.DatasourcePlatform.ExistsByPlatformCode(ctx, req.PlatformCode)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if exists {
|
||
return nil, errors.New("平台编码已存在")
|
||
}
|
||
|
||
// 验证认证类型相关的必填字段
|
||
if err = s.validateAuthFields(req); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 设置创建时间为当前时间
|
||
if req.CreatedAt == "" {
|
||
req.CreatedAt = strconv.FormatInt(time.Now().Unix(), 10)
|
||
}
|
||
req.UpdatedAt = req.CreatedAt
|
||
|
||
// 插入数据库
|
||
id, err := dao.DatasourcePlatform.Insert(ctx, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res = &dto.CreateDatasourcePlatformRes{
|
||
Id: id,
|
||
}
|
||
return res, nil
|
||
}
|
||
|
||
// List 获取数据源平台列表
|
||
func (s *datasourcePlatformService) List(ctx context.Context, req *dto.ListDatasourcePlatformReq) (res *dto.ListDatasourcePlatformRes, err error) {
|
||
platformList, total, err := dao.DatasourcePlatform.List(ctx, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 组装响应数据
|
||
list := make([]dto.DatasourcePlatformItem, 0, len(platformList))
|
||
for _, item := range platformList {
|
||
list = append(list, dto.DatasourcePlatformItem{
|
||
Id: item.ID,
|
||
PlatformCode: item.PlatformCode,
|
||
PlatformName: item.PlatformName,
|
||
Description: item.Description,
|
||
Status: api_feature.PlatformStatus(item.Status),
|
||
StatusName: s.getStatusName(api_feature.PlatformStatus(item.Status)),
|
||
ApiBaseUrl: item.ApiBaseUrl,
|
||
AuthType: item.AuthType,
|
||
AuthTypeName: s.getAuthTypeName(item.AuthType),
|
||
RateLimitPerMinute: item.RateLimitPerMinute,
|
||
RateLimitPerHour: item.RateLimitPerHour,
|
||
ConcurrencyLimit: item.ConcurrencyLimit,
|
||
RequestTimeoutMs: item.RequestTimeoutMs,
|
||
MaxRetries: item.MaxRetries,
|
||
RetryDelayMs: item.RetryDelayMs,
|
||
CreatedBy: item.CreatedBy,
|
||
CreatedAt: item.CreatedAt.Unix(),
|
||
UpdatedBy: item.UpdatedBy,
|
||
UpdatedAt: item.UpdatedAt.Unix(),
|
||
})
|
||
}
|
||
|
||
res = &dto.ListDatasourcePlatformRes{
|
||
List: list,
|
||
Total: total,
|
||
}
|
||
return res, nil
|
||
}
|
||
|
||
// GetOne 获取单个数据源平台
|
||
func (s *datasourcePlatformService) GetOne(ctx context.Context, req *dto.GetDatasourcePlatformReq) (res *dto.GetDatasourcePlatformRes, err error) {
|
||
platform, err := dao.DatasourcePlatform.GetOne(ctx, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if platform == nil {
|
||
return nil, errors.New("数据源平台不存在")
|
||
}
|
||
|
||
var platformEntity *entity.DatasourcePlatform
|
||
if err = gconv.Struct(platform, &platformEntity); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 隐藏敏感信息
|
||
platformEntity.Token = ""
|
||
platformEntity.ClientSecret = ""
|
||
platformEntity.ApiKey = ""
|
||
|
||
return &dto.GetDatasourcePlatformRes{
|
||
DatasourcePlatform: platformEntity,
|
||
}, nil
|
||
}
|
||
|
||
// GetByPlatformCode 根据平台编码获取数据源平台
|
||
func (s *datasourcePlatformService) GetByPlatformCode(ctx context.Context, platformCode string) (res *entity.DatasourcePlatform, err error) {
|
||
platform, err := dao.DatasourcePlatform.GetByPlatformCode(ctx, platformCode)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if platform == nil {
|
||
return nil, errors.New("数据源平台不存在")
|
||
}
|
||
return platform, nil
|
||
}
|
||
|
||
// Update 更新数据源平台
|
||
func (s *datasourcePlatformService) Update(ctx context.Context, req *dto.UpdateDatasourcePlatformReq) (err error) {
|
||
// 检查平台是否存在
|
||
exist, err := dao.DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: req.Id})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if exist == nil {
|
||
return errors.New("数据源平台不存在")
|
||
}
|
||
|
||
// 如果修改了平台编码,检查新编码是否重复
|
||
if req.PlatformCode != "" && req.PlatformCode != exist.PlatformCode {
|
||
exists, err := dao.DatasourcePlatform.ExistsByPlatformCode(ctx, req.PlatformCode, req.Id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if exists {
|
||
return errors.New("平台编码已存在")
|
||
}
|
||
}
|
||
|
||
// 验证认证类型相关的必填字段
|
||
if req.AuthType != "" {
|
||
authReq := &dto.CreateDatasourcePlatformReq{
|
||
AuthType: req.AuthType,
|
||
Token: req.Token,
|
||
ApiKey: req.ApiKey,
|
||
ClientId: req.ClientId,
|
||
ClientSecret: req.ClientSecret,
|
||
}
|
||
if err = s.validateAuthFields(authReq); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
// 设置更新时间
|
||
req.UpdatedAt = strconv.FormatInt(time.Now().Unix(), 10)
|
||
_, err = dao.DatasourcePlatform.Update(ctx, req)
|
||
return err
|
||
}
|
||
|
||
// UpdateStatus 更新数据源平台状态
|
||
func (s *datasourcePlatformService) UpdateStatus(ctx context.Context, req *dto.UpdateDatasourcePlatformStatusReq) (err error) {
|
||
// 检查平台是否存在
|
||
exist, err := dao.DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: req.Id})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if exist == nil {
|
||
return errors.New("数据源平台不存在")
|
||
}
|
||
|
||
// 如果状态没有变化,直接返回
|
||
if string(exist.Status) == req.Status.String() {
|
||
return nil
|
||
}
|
||
|
||
_, err = dao.DatasourcePlatform.UpdateStatus(ctx, req.Id, req.Status.String(), req.UpdatedBy)
|
||
return err
|
||
}
|
||
|
||
// Delete 删除数据源平台
|
||
func (s *datasourcePlatformService) Delete(ctx context.Context, req *dto.DeleteDatasourcePlatformReq) (err error) {
|
||
// 检查平台是否存在
|
||
exist, err := dao.DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: req.Id})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if exist == nil {
|
||
return errors.New("数据源平台不存在")
|
||
}
|
||
|
||
// TODO: 检查是否存在关联的数据,防止误删
|
||
// 例如:检查该平台是否有关联的接口配置等
|
||
|
||
_, err = dao.DatasourcePlatform.Delete(ctx, req)
|
||
return err
|
||
}
|
||
|
||
// GetStatistics 获取平台统计信息
|
||
func (s *datasourcePlatformService) GetStatistics(ctx context.Context) (res *dto.GetPlatformStatisticsRes, err error) {
|
||
stats, err := dao.DatasourcePlatform.GetPlatformStatistics(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res = &dto.GetPlatformStatisticsRes{
|
||
TotalPlatforms: int(stats["totalPlatforms"]),
|
||
ActivePlatforms: int(stats["activePlatforms"]),
|
||
InactivePlatforms: int(stats["inactivePlatforms"]),
|
||
TokenAuthPlatforms: int(stats["TOKENAuthPlatforms"]),
|
||
ApiKeyAuthPlatforms: int(stats["API_KEYAuthPlatforms"]),
|
||
OAuth2AuthPlatforms: int(stats["OAUTH2AuthPlatforms"]),
|
||
BasicAuthPlatforms: int(stats["BASICAuthPlatforms"]),
|
||
}
|
||
return res, nil
|
||
}
|
||
|
||
// ListActivePlatforms 获取所有启用的平台
|
||
func (s *datasourcePlatformService) ListActivePlatforms(ctx context.Context) (platforms []entity.DatasourcePlatform, err error) {
|
||
return dao.DatasourcePlatform.ListActivePlatforms(ctx)
|
||
}
|
||
|
||
// validateAuthFields 验证认证类型相关的必填字段
|
||
func (s *datasourcePlatformService) validateAuthFields(req *dto.CreateDatasourcePlatformReq) error {
|
||
switch req.AuthType {
|
||
case "TOKEN":
|
||
if req.Token == "" {
|
||
return errors.New("TOKEN认证类型必须填写token字段")
|
||
}
|
||
case "API_KEY":
|
||
if req.ApiKey == "" {
|
||
return errors.New("API_KEY认证类型必须填写apiKey字段")
|
||
}
|
||
case "OAUTH2":
|
||
if req.ClientId == "" || req.ClientSecret == "" {
|
||
return errors.New("OAUTH2认证类型必须填写clientId和clientSecret字段")
|
||
}
|
||
case "BASIC":
|
||
// BASIC认证通常需要用户名和密码,这里可以添加相应验证
|
||
if req.Token == "" && req.ApiKey == "" {
|
||
return errors.New("BASIC认证类型需要填写认证信息")
|
||
}
|
||
default:
|
||
return errors.New("不支持的认证类型")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// performConnectionTest 执行连接测试(示例实现)
|
||
func (s *datasourcePlatformService) performConnectionTest(platform *entity.DatasourcePlatform) (success bool, message string) {
|
||
// 这里应该实现实际的连接测试逻辑
|
||
// 例如:发送HTTP请求到api_base_url,验证响应
|
||
|
||
// 暂时返回模拟结果
|
||
return true, "连接测试成功"
|
||
}
|
||
|
||
// getStatusName 获取状态名称
|
||
func (s *datasourcePlatformService) getStatusName(status api_feature.PlatformStatus) string {
|
||
statusNames := map[api_feature.PlatformStatus]string{
|
||
api_feature.PlatformStatusActive: "启用",
|
||
api_feature.PlatformStatusInactive: "停用",
|
||
}
|
||
if name, ok := statusNames[status]; ok {
|
||
return name
|
||
}
|
||
return string(status)
|
||
}
|
||
|
||
// getAuthTypeName 获取认证类型名称
|
||
func (s *datasourcePlatformService) getAuthTypeName(authType string) string {
|
||
authTypeNames := map[string]string{
|
||
"TOKEN": "Token认证",
|
||
"API_KEY": "API Key认证",
|
||
"OAUTH2": "OAuth2认证",
|
||
"BASIC": "Basic认证",
|
||
}
|
||
if name, ok := authTypeNames[authType]; ok {
|
||
return name
|
||
}
|
||
return authType
|
||
}
|
||
|
||
// BatchUpdateStatus 批量更新平台状态
|
||
func (s *datasourcePlatformService) BatchUpdateStatus(ctx context.Context, ids []int64, status string, updatedBy string) (err error) {
|
||
if len(ids) == 0 {
|
||
return errors.New("请选择要更新的平台")
|
||
}
|
||
|
||
_, err = dao.DatasourcePlatform.BatchUpdateStatus(ctx, ids, status, updatedBy)
|
||
return err
|
||
}
|