diff --git a/backend/internal/repository/cu_order_repository.go b/backend/internal/repository/cu_order_repository.go index 8b9fd67..5bcb745 100644 --- a/backend/internal/repository/cu_order_repository.go +++ b/backend/internal/repository/cu_order_repository.go @@ -139,12 +139,17 @@ func (r *cuOrderRepository) Delete(id string) error { func (r *cuOrderRepository) CheckTrialOrderExists(cuUserID string, currentMonth string) (bool, error) { var count int64 - startOfMonth := currentMonth + "-01" - endOfMonth := currentMonth + "-31" + startOfMonth := currentMonth + "-01 00:00:00" + endOfMonth := currentMonth + "-31 23:59:59" err := r.db.Model(&models.CuOrder{}). - Where("cu_user_id = ? AND package_id = ? AND status = ? AND created_at >= ? AND created_at <= ? AND deleted_at IS NULL", - cuUserID, "trial", "paid", startOfMonth, endOfMonth). + Where("cu_user_id = ? AND (status = 'paid' OR status = 'pending') AND created_at >= ? AND created_at <= ? AND deleted_at IS NULL", + cuUserID, startOfMonth, endOfMonth). + // We need to join with package to check package type because package_id might be a UUID, not 'trial'. + // But if the order already recorded the type somewhere or if we just assume 'trial' is the package ID? + // Actually, let's just use a subquery or join to verify package type is trial. + Joins("JOIN packages ON packages.id = cu_orders.package_id"). + Where("packages.type = ?", string(models.PackageTypeTrial)). Count(&count).Error return count > 0, err diff --git a/backend/internal/service/cu_order_service.go b/backend/internal/service/cu_order_service.go index f8569a8..19edbd7 100644 --- a/backend/internal/service/cu_order_service.go +++ b/backend/internal/service/cu_order_service.go @@ -106,6 +106,30 @@ func (s *cuOrderService) CreatePendingOrder(ctx context.Context, cuUserID, custo return nil, err } + // 对于试用版,进行特殊检查 + if pkgEntity.Type == string(models.PackageTypeTrial) { + // 试用版不支持批量购买 + if req.LicenseCount != 1 { + return nil, i18n.NewI18nError("600004", lang) // 试用版不支持批量购买 + } + + // 检查购买时间限制 + now := time.Now() + if now.Day() > 25 { + return nil, i18n.NewI18nError("600003", lang) // 试用版购买时间限制 + } + + // 每个用户每月只能有一个试用版(包括pending的) + currentMonth := now.Format("2006-01") + exists, err := s.repo.CheckTrialOrderExists(cuUserID, currentMonth) + if err != nil { + return nil, i18n.NewI18nError("900004", lang, err.Error()) + } + if exists { + return nil, i18n.NewI18nError("600005", lang) // 当月已购买试用版 + } + } + // 再次校验数量(防御性校验) if req.LicenseCount > maxDevices { return nil, i18n.NewI18nError("601005", lang)