Files
ai_dianshang/server/internal/service/platform.go

151 lines
3.6 KiB
Go
Raw Normal View History

2025-11-28 15:18:10 +08:00
package service
import (
"dianshang/internal/model"
"dianshang/internal/repository"
"dianshang/pkg/utils"
"errors"
)
// PlatformService 平台服务
type PlatformService struct {
platformRepo *repository.PlatformRepository
productRepo *repository.ProductRepository
}
// NewPlatformService 创建平台服务
func NewPlatformService(platformRepo *repository.PlatformRepository, productRepo *repository.ProductRepository) *PlatformService {
return &PlatformService{
platformRepo: platformRepo,
productRepo: productRepo,
}
}
// GetPlatformList 获取平台列表
func (s *PlatformService) GetPlatformList(page, pageSize int, status *int, name string) ([]model.Platform, *utils.Pagination, error) {
if page <= 0 {
page = 1
}
if pageSize <= 0 || pageSize > 100 {
pageSize = 10
}
// 如果不需要分页,获取所有平台
if page == 0 && pageSize == 0 {
platforms, err := s.platformRepo.GetAll()
return platforms, nil, err
}
offset := (page - 1) * pageSize
// TODO: 实现带筛选的分页查询
// 暂时先返回所有平台
platforms, err := s.platformRepo.GetAll()
if err != nil {
return nil, nil, err
}
// 简单筛选
var filteredPlatforms []model.Platform
for _, p := range platforms {
if status != nil && p.Status != *status {
continue
}
if name != "" && p.Name != name && p.Code != name {
continue
}
filteredPlatforms = append(filteredPlatforms, p)
}
total := len(filteredPlatforms)
// 分页
start := offset
end := offset + pageSize
if start > total {
start = total
}
if end > total {
end = total
}
result := filteredPlatforms[start:end]
pagination := utils.NewPagination(page, pageSize)
pagination.Total = int64(total)
return result, pagination, nil
}
// GetPlatformByID 根据ID获取平台
func (s *PlatformService) GetPlatformByID(id uint) (*model.Platform, error) {
return s.platformRepo.GetByID(id)
}
// GetPlatformByCode 根据代码获取平台
func (s *PlatformService) GetPlatformByCode(code string) (*model.Platform, error) {
return s.platformRepo.GetByCode(code)
}
// CreatePlatform 创建平台
func (s *PlatformService) CreatePlatform(platform *model.Platform) error {
// 检查平台代码是否已存在
existing, _ := s.platformRepo.GetByCode(platform.Code)
if existing != nil {
return errors.New("平台代码已存在")
}
return s.platformRepo.Create(platform)
}
// UpdatePlatform 更新平台
func (s *PlatformService) UpdatePlatform(id uint, updates map[string]interface{}) error {
// 检查平台是否存在
_, err := s.platformRepo.GetByID(id)
if err != nil {
return errors.New("平台不存在")
}
// 如果更新代码,检查是否与其他平台冲突
if code, ok := updates["code"].(string); ok {
existing, _ := s.platformRepo.GetByCode(code)
if existing != nil && existing.ID != id {
return errors.New("平台代码已被其他平台使用")
}
}
return s.platformRepo.Update(id, updates)
}
// DeletePlatform 删除平台
func (s *PlatformService) DeletePlatform(id uint) error {
// 检查平台是否存在
_, err := s.platformRepo.GetByID(id)
if err != nil {
return errors.New("平台不存在")
}
// TODO: 检查是否有分类关联到该平台
// 可以选择级联删除或禁止删除
return s.platformRepo.Delete(id)
}
// GetAllActivePlatforms 获取所有启用的平台
func (s *PlatformService) GetAllActivePlatforms() ([]model.Platform, error) {
platforms, err := s.platformRepo.GetAll()
if err != nil {
return nil, err
}
// 过滤启用的平台
var activePlatforms []model.Platform
for _, p := range platforms {
if p.Status == 1 {
activePlatforms = append(activePlatforms, p)
}
}
return activePlatforms, nil
}