108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package service
|
|
|
|
import (
|
|
"cid/dao"
|
|
"cid/model/config"
|
|
"cid/model/dto"
|
|
"cid/model/entity"
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
type adSource struct{}
|
|
|
|
// AdSource 广告源服务
|
|
var AdSource = new(adSource)
|
|
|
|
// GetAvailableSources 获取可用的广告源列表
|
|
func (s *adSource) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
|
|
return dao.AdSource.GetAvailableSources(ctx)
|
|
}
|
|
|
|
// GetSourcesByProvider 根据提供商获取广告源
|
|
func (s *adSource) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
|
|
return dao.AdSource.GetSourcesByProvider(ctx, provider)
|
|
}
|
|
|
|
// CreateAdSource 创建广告源
|
|
func (s *adSource) CreateAdSource(ctx context.Context, req *dto.CreateAdSourceReq) (id string, err error) {
|
|
// 检查广告源名称是否已存在
|
|
existingSource, err := dao.AdSource.GetByName(ctx, req.Name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if existingSource != nil {
|
|
return "", gerror.New("广告源名称已存在")
|
|
}
|
|
|
|
adSource := &entity.AdSource{
|
|
Name: req.Name,
|
|
Code: req.Code,
|
|
Provider: req.Provider,
|
|
Type: req.Type,
|
|
APIConfig: config.APIConfig{
|
|
Endpoint: req.APIEndpoint,
|
|
},
|
|
}
|
|
|
|
// 设置状态
|
|
adSource.Status = "active" // 默认状态
|
|
|
|
return dao.AdSource.Create(ctx, adSource)
|
|
}
|
|
|
|
// UpdateAdSource 更新广告源
|
|
func (s *adSource) UpdateAdSource(ctx context.Context, id string, req *dto.UpdateAdSourceReq) (affected int64, err error) {
|
|
|
|
// 检查广告源是否存在
|
|
existingSource, err := dao.AdSource.GetByID(ctx, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if existingSource == nil {
|
|
return 0, gerror.New("广告源不存在")
|
|
}
|
|
|
|
// 如果更新名称,检查是否与其他广告源冲突
|
|
if req.Name != "" && req.Name != existingSource.Name {
|
|
conflictSource, err := dao.AdSource.GetByName(ctx, req.Name)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if conflictSource != nil {
|
|
return 0, gerror.New("广告源名称已存在")
|
|
}
|
|
}
|
|
|
|
// 构建更新数据
|
|
updateData := &entity.AdSource{}
|
|
if req.Name != "" {
|
|
updateData.Name = req.Name
|
|
}
|
|
if req.APIEndpoint != "" {
|
|
updateData.APIConfig.Endpoint = req.APIEndpoint
|
|
}
|
|
|
|
return dao.AdSource.UpdateFields(ctx, id, updateData)
|
|
}
|
|
|
|
// DeleteAdSource 删除广告源
|
|
func (s *adSource) DeleteAdSource(ctx context.Context, id string) (affected int64, err error) {
|
|
// 检查广告源是否存在
|
|
existingSource, err := dao.AdSource.GetByID(ctx, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if existingSource == nil {
|
|
return 0, gerror.New("广告源不存在")
|
|
}
|
|
|
|
return dao.AdSource.Delete(ctx, id)
|
|
}
|
|
|
|
// GetAdSourceByID 根据ID获取广告源
|
|
func (s *adSource) GetAdSourceByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
|
|
return dao.AdSource.GetByID(ctx, id)
|
|
}
|