代码初始化
This commit is contained in:
305
service/dict/api_datasource_platform_service.go
Normal file
305
service/dict/api_datasource_platform_service.go
Normal file
@@ -0,0 +1,305 @@
|
||||
package dict
|
||||
|
||||
import (
|
||||
api_feature "cid/consts/api-feature"
|
||||
dao "cid/dao/dict"
|
||||
dto "cid/model/dto/dict"
|
||||
entity "cid/model/entity/dict"
|
||||
"context"
|
||||
"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
|
||||
}
|
||||
396
service/dict/api_field_mapping_config_service.go
Normal file
396
service/dict/api_field_mapping_config_service.go
Normal file
@@ -0,0 +1,396 @@
|
||||
package dict
|
||||
|
||||
import (
|
||||
consts "cid/consts/dict"
|
||||
"cid/dao/dict"
|
||||
dto "cid/model/dto/dict"
|
||||
entity "cid/model/entity/dict"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
)
|
||||
|
||||
type fieldMappingConfigService struct{}
|
||||
|
||||
// FieldMappingConfig 字段映射配置服务
|
||||
var FieldMappingConfig = new(fieldMappingConfigService)
|
||||
|
||||
// Create 创建字段映射配置
|
||||
func (s *fieldMappingConfigService) Create(ctx context.Context, req *dto.CreateFieldMappingConfigReq) (res *dto.CreateFieldMappingConfigRes, err error) {
|
||||
// 验证必填字段
|
||||
if err = s.validateRequiredFields(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查重复配置
|
||||
exists, err := dict.FieldMappingConfig.CheckDuplicate(ctx, req.VendorName, req.ApiName, req.SourceField, req.TargetField, 0)
|
||||
if err != nil {
|
||||
return nil, errors.New("检查配置重复性失败")
|
||||
}
|
||||
if exists {
|
||||
return nil, errors.New("相同厂商、接口、源字段和目标字段的配置已存在")
|
||||
}
|
||||
|
||||
// 验证转换参数
|
||||
if err = s.validateTransformParams(req.TransformType, req.TransformParams); err != nil {
|
||||
return nil, fmt.Errorf("转换参数验证失败: %v", err)
|
||||
}
|
||||
|
||||
// 验证业务域
|
||||
if req.BusinessDomain != "" && !s.isValidBusinessDomain(req.BusinessDomain) {
|
||||
return nil, errors.New("无效的业务域")
|
||||
}
|
||||
|
||||
// 验证生效时间和失效时间
|
||||
if req.EffectiveDate != nil && req.ExpiryDate != nil && req.EffectiveDate.After(*req.ExpiryDate) {
|
||||
return nil, errors.New("生效时间不能晚于失效时间")
|
||||
}
|
||||
|
||||
// 插入数据库
|
||||
id, err := dict.FieldMappingConfig.Insert(ctx, req)
|
||||
if err != nil {
|
||||
return nil, errors.New("创建配置失败")
|
||||
}
|
||||
|
||||
res = &dto.CreateFieldMappingConfigRes{
|
||||
Id: id,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取字段映射配置列表
|
||||
func (s *fieldMappingConfigService) List(ctx context.Context, req *dto.ListFieldMappingConfigReq) (res *dto.ListFieldMappingConfigRes, err error) {
|
||||
configs, total, err := dict.FieldMappingConfig.List(ctx, req)
|
||||
if err != nil {
|
||||
return nil, errors.New("查询配置列表失败")
|
||||
}
|
||||
|
||||
// 组装响应数据
|
||||
list := make([]dto.FieldMappingConfigItem, 0, len(configs))
|
||||
for _, item := range configs {
|
||||
list = append(list, dto.FieldMappingConfigItem{
|
||||
Id: item.Id,
|
||||
ConfigName: item.ConfigName,
|
||||
VendorName: item.VendorName,
|
||||
ApiName: item.ApiName,
|
||||
ApiVersion: item.ApiVersion,
|
||||
SourceField: item.SourceField,
|
||||
TargetField: item.TargetField,
|
||||
TargetFieldType: item.TargetFieldType,
|
||||
TransformType: item.TransformType,
|
||||
TransformTypeName: s.getTransformTypeName(item.TransformType),
|
||||
IsActive: item.IsActive,
|
||||
Priority: item.Priority,
|
||||
BusinessDomain: item.BusinessDomain,
|
||||
BusinessDomainName: s.getBusinessDomainName(item.BusinessDomain),
|
||||
FieldGroup: item.FieldGroup,
|
||||
ConfigVersion: item.ConfigVersion,
|
||||
CreatedBy: item.CreatedBy,
|
||||
CreatedTime: item.CreatedTime,
|
||||
UpdatedBy: item.UpdatedBy,
|
||||
UpdatedTime: item.UpdatedTime,
|
||||
})
|
||||
}
|
||||
|
||||
res = &dto.ListFieldMappingConfigRes{
|
||||
List: list,
|
||||
Total: total,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个字段映射配置
|
||||
func (s *fieldMappingConfigService) GetOne(ctx context.Context, req *dto.GetFieldMappingConfigReq) (res *dto.GetFieldMappingConfigRes, err error) {
|
||||
config, err := dict.FieldMappingConfig.GetOne(ctx, req)
|
||||
if err != nil {
|
||||
return nil, errors.New("获取配置详情失败")
|
||||
}
|
||||
if config == nil {
|
||||
return nil, errors.New("配置不存在")
|
||||
}
|
||||
|
||||
return &dto.GetFieldMappingConfigRes{
|
||||
FieldMappingConfig: config,
|
||||
TransformTypeName: s.getTransformTypeName(config.TransformType),
|
||||
BusinessDomainName: s.getBusinessDomainName(config.BusinessDomain),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update 更新字段映射配置
|
||||
func (s *fieldMappingConfigService) Update(ctx context.Context, req *dto.UpdateFieldMappingConfigReq) (err error) {
|
||||
// 检查配置是否存在
|
||||
exist, err := dict.FieldMappingConfig.GetOne(ctx, &dto.GetFieldMappingConfigReq{Id: req.Id})
|
||||
if err != nil || exist == nil {
|
||||
return errors.New("配置不存在")
|
||||
}
|
||||
|
||||
// 如果修改了关键字段,检查重复性
|
||||
if (req.VendorName != "" && req.VendorName != exist.VendorName) ||
|
||||
(req.ApiName != "" && req.ApiName != exist.ApiName) ||
|
||||
(req.SourceField != "" && req.SourceField != exist.SourceField) ||
|
||||
(req.TargetField != "" && req.TargetField != exist.TargetField) {
|
||||
|
||||
vendorName := req.VendorName
|
||||
if vendorName == "" {
|
||||
vendorName = exist.VendorName
|
||||
}
|
||||
apiName := req.ApiName
|
||||
if apiName == "" {
|
||||
apiName = exist.ApiName
|
||||
}
|
||||
sourceField := req.SourceField
|
||||
if sourceField == "" {
|
||||
sourceField = exist.SourceField
|
||||
}
|
||||
targetField := req.TargetField
|
||||
if targetField == "" {
|
||||
targetField = exist.TargetField
|
||||
}
|
||||
|
||||
exists, err := dict.FieldMappingConfig.CheckDuplicate(ctx, vendorName, apiName, sourceField, targetField, req.Id)
|
||||
if err != nil {
|
||||
return errors.New("检查配置重复性失败")
|
||||
}
|
||||
if exists {
|
||||
return errors.New("相同厂商、接口、源字段和目标字段的配置已存在")
|
||||
}
|
||||
}
|
||||
|
||||
// 验证转换参数
|
||||
if req.TransformType != "" && req.TransformParams != nil {
|
||||
if err = s.validateTransformParams(req.TransformType, req.TransformParams); err != nil {
|
||||
return fmt.Errorf("转换参数验证失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 验证生效时间和失效时间
|
||||
if req.EffectiveDate != nil && req.ExpiryDate != nil && req.EffectiveDate.After(*req.ExpiryDate) {
|
||||
return errors.New("生效时间不能晚于失效时间")
|
||||
}
|
||||
|
||||
// 更新数据库
|
||||
_, err = dict.FieldMappingConfig.Update(ctx, req)
|
||||
if err != nil {
|
||||
return errors.New("更新配置失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateStatus 更新字段映射配置状态
|
||||
func (s *fieldMappingConfigService) UpdateStatus(ctx context.Context, req *dto.UpdateFieldMappingConfigStatusReq) (err error) {
|
||||
_, err = dict.FieldMappingConfig.UpdateStatus(ctx, req.Id, req.IsActive)
|
||||
if err != nil {
|
||||
return errors.New("更新配置状态失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除字段映射配置
|
||||
func (s *fieldMappingConfigService) Delete(ctx context.Context, req *dto.DeleteFieldMappingConfigReq) (err error) {
|
||||
_, err = dict.FieldMappingConfig.Delete(ctx, req)
|
||||
if err != nil {
|
||||
return errors.New("删除配置失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryByVendorApi 根据厂商和接口查询字段映射
|
||||
func (s *fieldMappingConfigService) QueryByVendorApi(ctx context.Context, req *dto.QueryFieldMappingByVendorApiReq) (res *dto.QueryFieldMappingByVendorApiRes, err error) {
|
||||
configs, err := dict.FieldMappingConfig.GetByVendorAndApi(ctx, req.VendorName, req.ApiName, req.ApiVersion, req.IsActive)
|
||||
if err != nil {
|
||||
return nil, errors.New("查询字段映射配置失败")
|
||||
}
|
||||
|
||||
// 过滤掉已过期的配置
|
||||
var validConfigs []*entity.FieldMappingConfig
|
||||
now := time.Now()
|
||||
for _, config := range configs {
|
||||
if (config.EffectiveDate == nil || !config.EffectiveDate.After(now)) &&
|
||||
(config.ExpiryDate == nil || config.ExpiryDate.After(now)) {
|
||||
validConfigs = append(validConfigs, config)
|
||||
}
|
||||
}
|
||||
|
||||
res = &dto.QueryFieldMappingByVendorApiRes{
|
||||
List: validConfigs,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Validate 验证字段映射配置
|
||||
func (s *fieldMappingConfigService) Validate(ctx context.Context, req *dto.ValidateFieldMappingReq) (res *dto.ValidateFieldMappingRes, err error) {
|
||||
ctx = context.WithValue(ctx, "user", &beans.User{UserName: "admin"})
|
||||
res = &dto.ValidateFieldMappingRes{
|
||||
IsValid: false,
|
||||
}
|
||||
|
||||
// 验证基本配置
|
||||
if req.ConfigName == "" || req.VendorName == "" || req.ApiName == "" || req.SourceField == "" || req.TargetField == "" {
|
||||
res.Error = "配置名称、厂商名称、接口名称、源字段和目标字段不能为空"
|
||||
return
|
||||
}
|
||||
|
||||
// 检查重复配置
|
||||
exists, err := dict.FieldMappingConfig.CheckDuplicate(ctx, req.VendorName, req.ApiName, req.SourceField, req.TargetField, 0)
|
||||
if err != nil {
|
||||
res.Error = "检查配置重复性失败"
|
||||
return
|
||||
}
|
||||
if exists {
|
||||
res.Error = "相同厂商、接口、源字段和目标字段的配置已存在"
|
||||
return
|
||||
}
|
||||
|
||||
// 如果有测试值,尝试转换
|
||||
if req.TestValue != nil {
|
||||
// 这里可以实现具体的转换逻辑
|
||||
// 例如:根据转换类型和参数进行值转换
|
||||
res.TransformedValue = req.TestValue
|
||||
res.Warnings = []string{"转换逻辑需要根据具体业务实现"}
|
||||
}
|
||||
|
||||
res.IsValid = true
|
||||
return
|
||||
}
|
||||
|
||||
// validateRequiredFields 验证必填字段
|
||||
func (s *fieldMappingConfigService) validateRequiredFields(req *dto.CreateFieldMappingConfigReq) error {
|
||||
if req.ConfigName == "" {
|
||||
return errors.New("配置名称不能为空")
|
||||
}
|
||||
if req.VendorName == "" {
|
||||
return errors.New("厂商名称不能为空")
|
||||
}
|
||||
if req.ApiName == "" {
|
||||
return errors.New("接口名称不能为空")
|
||||
}
|
||||
if req.SourceField == "" {
|
||||
return errors.New("源字段不能为空")
|
||||
}
|
||||
if req.TargetField == "" {
|
||||
return errors.New("目标字段不能为空")
|
||||
}
|
||||
if req.TargetFieldType == "" {
|
||||
return errors.New("目标字段类型不能为空")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateRequiredFieldsForBatch 批量创建的字段验证
|
||||
func (s *fieldMappingConfigService) validateRequiredFieldsForBatch(config *dto.BatchFieldMappingConfigItem) error {
|
||||
if config.ConfigName == "" {
|
||||
return errors.New("配置名称不能为空")
|
||||
}
|
||||
if config.VendorName == "" {
|
||||
return errors.New("厂商名称不能为空")
|
||||
}
|
||||
if config.ApiName == "" {
|
||||
return errors.New("接口名称不能为空")
|
||||
}
|
||||
if config.SourceField == "" {
|
||||
return errors.New("源字段不能为空")
|
||||
}
|
||||
if config.TargetField == "" {
|
||||
return errors.New("目标字段不能为空")
|
||||
}
|
||||
if config.TargetFieldType == "" {
|
||||
return errors.New("目标字段类型不能为空")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateTransformParams 验证转换参数
|
||||
func (s *fieldMappingConfigService) validateTransformParams(transformType string, params map[string]interface{}) error {
|
||||
if params == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch transformType {
|
||||
case consts.TransformTypeFormatDate:
|
||||
if _, ok := params["source_format"]; !ok {
|
||||
return errors.New("日期格式化转换需要source_format参数")
|
||||
}
|
||||
if _, ok := params["target_format"]; !ok {
|
||||
return errors.New("日期格式化转换需要target_format参数")
|
||||
}
|
||||
case consts.TransformTypeMapValue:
|
||||
if len(params) == 0 {
|
||||
return errors.New("值映射转换需要映射规则参数")
|
||||
}
|
||||
case consts.TransformTypeConcat:
|
||||
if _, ok := params["fields"]; !ok {
|
||||
return errors.New("拼接转换需要fields参数")
|
||||
}
|
||||
case consts.TransformTypeRegex:
|
||||
if _, ok := params["pattern"]; !ok {
|
||||
return errors.New("正则提取转换需要pattern参数")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// isValidBusinessDomain 验证业务域是否有效
|
||||
func (s *fieldMappingConfigService) isValidBusinessDomain(domain string) bool {
|
||||
validDomains := []string{
|
||||
consts.BusinessDomainUser,
|
||||
consts.BusinessDomainOrder,
|
||||
consts.BusinessDomainProduct,
|
||||
consts.BusinessDomainPayment,
|
||||
}
|
||||
for _, valid := range validDomains {
|
||||
if domain == valid {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// getTransformTypeName 获取转换类型名称
|
||||
func (s *fieldMappingConfigService) getTransformTypeName(transformType string) string {
|
||||
typeNames := map[string]string{
|
||||
consts.TransformTypeDirect: "直接映射",
|
||||
consts.TransformTypeFormatDate: "日期格式化",
|
||||
consts.TransformTypeMapValue: "值映射",
|
||||
consts.TransformTypeConcat: "拼接",
|
||||
consts.TransformTypeCalc: "计算",
|
||||
consts.TransformTypeRegex: "正则提取",
|
||||
}
|
||||
if name, ok := typeNames[transformType]; ok {
|
||||
return name
|
||||
}
|
||||
return transformType
|
||||
}
|
||||
|
||||
// getBusinessDomainName 获取业务域名称
|
||||
func (s *fieldMappingConfigService) getBusinessDomainName(domain string) string {
|
||||
domainNames := map[string]string{
|
||||
consts.BusinessDomainUser: "用户",
|
||||
consts.BusinessDomainOrder: "订单",
|
||||
consts.BusinessDomainProduct: "商品",
|
||||
consts.BusinessDomainPayment: "支付",
|
||||
}
|
||||
if name, ok := domainNames[domain]; ok {
|
||||
return name
|
||||
}
|
||||
return domain
|
||||
}
|
||||
|
||||
// GetActiveConfigsByBusinessDomain 根据业务域获取启用的配置
|
||||
func (s *fieldMappingConfigService) GetActiveConfigsByBusinessDomain(ctx context.Context, businessDomain string) ([]entity.FieldMappingConfig, error) {
|
||||
return dict.FieldMappingConfig.GetActiveConfigsByBusinessDomain(ctx, businessDomain)
|
||||
}
|
||||
|
||||
// GetFieldGroupsByVendorApi 获取指定厂商接口的字段分组
|
||||
func (s *fieldMappingConfigService) GetFieldGroupsByVendorApi(ctx context.Context, vendorName, apiName string) ([]string, error) {
|
||||
return dict.FieldMappingConfig.GetFieldGroupsByVendorApi(ctx, vendorName, apiName)
|
||||
}
|
||||
|
||||
// CleanExpiredConfigs 清理过期配置
|
||||
func (s *fieldMappingConfigService) CleanExpiredConfigs(ctx context.Context) (int64, error) {
|
||||
return dict.FieldMappingConfig.DeleteExpiredConfigs(ctx)
|
||||
}
|
||||
183
service/dict/api_interface_service.go
Normal file
183
service/dict/api_interface_service.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package dict
|
||||
|
||||
import (
|
||||
consts "cid/consts/api-feature"
|
||||
"cid/dao/dict"
|
||||
dto "cid/model/dto/dict"
|
||||
entity "cid/model/entity/dict"
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type apiInterfaceService struct{}
|
||||
|
||||
// ApiInterface 接口服务
|
||||
var ApiInterface = new(apiInterfaceService)
|
||||
|
||||
// Create 创建接口
|
||||
func (s *apiInterfaceService) Create(ctx context.Context, req *dto.CreateApiInterfaceReq) (res *dto.CreateApiInterfaceRes, err error) {
|
||||
_, err = DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: req.PlatformId})
|
||||
if err != nil {
|
||||
return nil, errors.New("平台不存在")
|
||||
}
|
||||
|
||||
// 检查接口编码在同一平台下是否重复
|
||||
interfaces, _, err := dict.ApiInterface.List(ctx, &dto.ListApiInterfaceReq{
|
||||
PlatformId: req.PlatformId,
|
||||
Code: req.Code,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(interfaces) > 0 {
|
||||
return nil, errors.New("接口编码在该平台下已存在")
|
||||
}
|
||||
|
||||
// 插入数据库
|
||||
id, err := dict.ApiInterface.Insert(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = &dto.CreateApiInterfaceRes{
|
||||
Id: id,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取接口列表
|
||||
func (s *apiInterfaceService) List(ctx context.Context, req *dto.ListApiInterfaceReq) (res *dto.ListApiInterfaceRes, err error) {
|
||||
apiList, total, err := dict.ApiInterface.List(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
platformIds := make([]int64, 0)
|
||||
for _, item := range apiList {
|
||||
if item.PlatformId > 0 {
|
||||
platformIds = append(platformIds, item.PlatformId)
|
||||
}
|
||||
}
|
||||
|
||||
platformMap := make(map[int64]string)
|
||||
if len(platformIds) > 0 {
|
||||
res, err := DatasourcePlatform.List(ctx, &dto.ListDatasourcePlatformReq{})
|
||||
if err == nil && res != nil {
|
||||
for _, p := range res.List {
|
||||
platformMap[p.Id] = p.PlatformName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
list := make([]dto.ApiInterfaceItem, 0, len(apiList))
|
||||
for _, item := range apiList {
|
||||
platformName := ""
|
||||
if name, ok := platformMap[item.PlatformId]; ok {
|
||||
platformName = name
|
||||
}
|
||||
|
||||
list = append(list, dto.ApiInterfaceItem{
|
||||
Id: item.Id,
|
||||
PlatformId: item.PlatformId,
|
||||
PlatformName: platformName,
|
||||
Name: item.Name,
|
||||
Code: item.Code,
|
||||
Url: item.Url,
|
||||
Method: item.Method,
|
||||
Status: item.Status,
|
||||
StatusName: s.getStatusName(item.Status),
|
||||
CreatedAt: item.CreatedAt.Unix(),
|
||||
UpdatedAt: item.UpdatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
res = &dto.ListApiInterfaceRes{
|
||||
List: list,
|
||||
Total: total,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个接口
|
||||
func (s *apiInterfaceService) GetOne(ctx context.Context, req *dto.GetApiInterfaceReq) (res *dto.GetApiInterfaceRes, err error) {
|
||||
apiInterface, err := dict.ApiInterface.GetOne(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var platformName string
|
||||
if apiInterface.PlatformId > 0 {
|
||||
platform, _ := DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: apiInterface.PlatformId})
|
||||
if platform != nil {
|
||||
platformName = platform.PlatformName
|
||||
}
|
||||
}
|
||||
|
||||
return &dto.GetApiInterfaceRes{
|
||||
ApiInterface: apiInterface,
|
||||
PlatformName: platformName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update 更新接口
|
||||
func (s *apiInterfaceService) Update(ctx context.Context, req *dto.UpdateApiInterfaceReq) (err error) {
|
||||
exist, err := dict.ApiInterface.GetOne(ctx, &dto.GetApiInterfaceReq{Id: req.Id})
|
||||
if err != nil || exist == nil {
|
||||
return errors.New("接口不存在")
|
||||
}
|
||||
|
||||
if req.PlatformId > 0 && req.PlatformId != exist.PlatformId {
|
||||
_, err := DatasourcePlatform.GetOne(ctx, &dto.GetDatasourcePlatformReq{Id: req.PlatformId})
|
||||
if err != nil {
|
||||
return errors.New("平台不存在")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Code != "" && req.Code != exist.Code {
|
||||
platformId := req.PlatformId
|
||||
if platformId == 0 {
|
||||
platformId = exist.PlatformId
|
||||
}
|
||||
interfaces, _, err := dict.ApiInterface.List(ctx, &dto.ListApiInterfaceReq{
|
||||
PlatformId: platformId,
|
||||
Code: req.Code,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(interfaces) > 0 {
|
||||
return errors.New("接口编码在该平台下已存在")
|
||||
}
|
||||
}
|
||||
|
||||
_, err = dict.ApiInterface.Update(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新接口状态
|
||||
func (s *apiInterfaceService) UpdateStatus(ctx context.Context, req *dto.UpdateApiInterfaceStatusReq) (err error) {
|
||||
_, err = dict.ApiInterface.UpdateStatus(ctx, req.Id, req.Status.String())
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除接口
|
||||
func (s *apiInterfaceService) Delete(ctx context.Context, req *dto.DeleteApiInterfaceReq) (err error) {
|
||||
_, err = dict.ApiInterface.Delete(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByIds 根据ID列表获取接口
|
||||
func (s *apiInterfaceService) GetByIds(ctx context.Context, ids []int64) (res []entity.ApiInterface, err error) {
|
||||
return dict.ApiInterface.GetByIds(ctx, ids)
|
||||
}
|
||||
|
||||
// getStatusName 获取状态名称
|
||||
func (s *apiInterfaceService) getStatusName(status consts.PlatformStatus) string {
|
||||
statusNames := map[consts.PlatformStatus]string{
|
||||
consts.PlatformStatusActive: "启用",
|
||||
consts.PlatformStatusInactive: "停用",
|
||||
}
|
||||
if name, ok := statusNames[status]; ok {
|
||||
return name
|
||||
}
|
||||
return string(status)
|
||||
}
|
||||
Reference in New Issue
Block a user