105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"cidService/dao"
|
|
"cidService/model/dto"
|
|
"cidService/model/entity"
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
var (
|
|
AdSource = adSourceService{}
|
|
)
|
|
|
|
type adSourceService struct{}
|
|
|
|
// GetAvailableSources 获取可用的广告源列表
|
|
func (s *adSourceService) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
|
|
return dao.AdSource.GetAvailableSources(ctx)
|
|
}
|
|
|
|
// GetSourcesByProvider 根据提供商获取广告源
|
|
func (s *adSourceService) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
|
|
return dao.AdSource.GetSourcesByProvider(ctx, provider)
|
|
}
|
|
|
|
// CreateAdSource 创建广告源
|
|
func (s *adSourceService) CreateAdSource(ctx context.Context, req *dto.CreateAdSourceReq) (id int64, err error) {
|
|
// 检查广告源名称是否已存在
|
|
existingSource, err := dao.AdSource.GetByName(ctx, req.Name)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if existingSource != nil {
|
|
return 0, gerror.New("广告源名称已存在")
|
|
}
|
|
|
|
adSource := &entity.AdSource{
|
|
Name: req.Name,
|
|
Code: req.Code,
|
|
Provider: req.Provider,
|
|
Type: req.Type,
|
|
APIEndpoint: req.APIEndpoint,
|
|
Status: "active", // 默认状态
|
|
Priority: 1, // 默认优先级
|
|
}
|
|
|
|
return dao.AdSource.Create(ctx, adSource)
|
|
}
|
|
|
|
// UpdateAdSource 更新广告源
|
|
func (s *adSourceService) UpdateAdSource(ctx context.Context, id int64, 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.APIEndpoint = req.APIEndpoint
|
|
}
|
|
|
|
return dao.AdSource.UpdateFields(ctx, id, updateData)
|
|
}
|
|
|
|
// DeleteAdSource 删除广告源
|
|
func (s *adSourceService) DeleteAdSource(ctx context.Context, id int64) (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 *adSourceService) GetAdSourceByID(ctx context.Context, id int64) (adSource *entity.AdSource, err error) {
|
|
return dao.AdSource.GetByID(ctx, id)
|
|
}
|