fix(poll): Series 删除前检查关联套餐 POLL-04

- package_store.go 新增 CountBySeriesID 方法,统计指定系列下的活跃套餐数量
- package_series/service.go Service 注入 packageStore 字段,Delete 前调用 CountBySeriesID 检查
- bootstrap/services.go 更新 PackageSeries 服务初始化,传入 PackageStore
This commit is contained in:
2026-03-28 10:42:58 +08:00
parent facc766993
commit 47aa5f8d25
3 changed files with 28 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package package_series
import (
"context"
"fmt"
"time"
"gorm.io/gorm"
@@ -15,15 +16,19 @@ import (
"github.com/break/junhong_cmp_fiber/pkg/middleware"
)
// Service 套餐系列业务服务
type Service struct {
packageSeriesStore *postgres.PackageSeriesStore
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore
packageStore *postgres.PackageStore
}
func New(packageSeriesStore *postgres.PackageSeriesStore, shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore) *Service {
// New 创建套餐系列服务实例
func New(packageSeriesStore *postgres.PackageSeriesStore, shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore, packageStore *postgres.PackageStore) *Service {
return &Service{
packageSeriesStore: packageSeriesStore,
shopSeriesAllocationStore: shopSeriesAllocationStore,
packageStore: packageStore,
}
}
@@ -139,6 +144,14 @@ func (s *Service) Delete(ctx context.Context, id uint) error {
return errors.Wrap(errors.CodeInternalError, err, "获取套餐系列失败")
}
count, err := s.packageStore.CountBySeriesID(ctx, id)
if err != nil {
return err
}
if count > 0 {
return errors.New(errors.CodeInvalidParam, fmt.Sprintf("该系列下有 %d 个关联套餐,请先处理后再删除", count))
}
if err := s.packageSeriesStore.Delete(ctx, id); err != nil {
return errors.Wrap(errors.CodeInternalError, err, "删除套餐系列失败")
}