tourze/product-auto-down-bundle

商品自动下架功能

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Type:symfony-bundle

pkg:composer/tourze/product-auto-down-bundle

This package is auto-updated.

Last update: 2025-11-14 14:47:20 UTC


README

English | 中文

Product Auto Down Bundle provides automated product takedown functionality for Symfony applications. It allows you to schedule products to be automatically taken down at a specified time.

Features

  • Scheduled Takedown: Set specific times for products to be automatically taken down
  • Command Line Interface: Console commands for executing and managing takedowns
  • Admin Interface: EasyAdmin integration for managing configurations
  • Logging System: Comprehensive logging for all takedown actions
  • Batch Processing: Efficient batch execution of scheduled takedowns
  • Clean Up Tools: Automatic cleanup of old configurations

Installation

composer require tourze/product-auto-down-bundle

Configuration

1. Register the Bundle

// config/bundles.php
return [
    // ...
    Tourze\ProductAutoDownBundle\ProductAutoDownBundle::class => ['all' => true],
];

2. Run Database Migrations

# Create and run migrations
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate

3. Load Demo Fixtures (Optional)

# Load demo fixtures
php bin/console doctrine:fixtures:load --group=ProductAutoDownBundle

Usage

Console Commands

The bundle provides commands for managing automatic takedowns:

# Execute scheduled takedowns
php bin/console product:auto-take-down-spu

# Execute with specific datetime
php bin/console product:auto-take-down-spu --datetime="2024-12-31 23:59:59"

# Force execution (skip confirmation)
php bin/console product:auto-take-down-spu --force

# Verbose output
php bin/console product:auto-take-down-spu -v

Command Options:

  • --datetime : Specify execution time in format: Y-m-d H:i:s
  • --force : Force execution without confirmation prompts
  • -v|--verbose : Display detailed execution information

Service API

AutoDownService

The main service for managing automatic takedowns:

use Tourze\ProductAutoDownBundle\Service\AutoDownService;

class YourController
{
    public function __construct(
        private AutoDownService $autoDownService
    ) {}

    public function scheduleAutoDown(): void
    {
        // Configure SPU auto takedown
        $config = $this->autoDownService->configureAutoTakeDownTime(
            spu: $spuEntity,
            autoTakeDownTime: new \DateTimeImmutable('+1 week')
        );

        // Cancel SPU auto takedown
        $success = $this->autoDownService->cancelAutoTakeDown(123);

        // Execute scheduled takedowns
        $executedCount = $this->autoDownService->executeAutoTakeDown();

        // Get active configuration count
        $count = $this->autoDownService->countActiveConfigs();

        // Clean up old configurations
        $cleaned = $this->autoDownService->cleanupOldConfigs(30);
    }
}

Repository Methods

use Tourze\ProductAutoDownBundle\Repository\AutoDownTimeConfigRepository;
use Tourze\ProductAutoDownBundle\Repository\AutoDownLogRepository;

// Configuration repository
$configs = $configRepository->findActiveConfigs(); // Find active configs
$config = $configRepository->findBySpu(123);       // Find by SPU ID
$count = $configRepository->countActiveConfigs();  // Count active configs

// Log repository
$logs = $logRepository->findBySpuId(123);         // Find SPU logs
$logs = $logRepository->findByAction($action);    // Find by action type
$stats = $logRepository->countByActions();        // Action statistics

Admin Interface

The bundle integrates with EasyAdmin for configuration management:

  • Auto Down Config: /admin/product/auto-down-config

    • View and manage auto takedown configurations
    • Link to SPU entities
    • Enable/disable configurations
  • Takedown Logs: /admin/product/auto-down-logs

    • View detailed execution logs
    • Track SPU takedown history
    • Filter by action and time

Database Schema

AutoDownTimeConfig (Configuration Table)

Column Type Description
id int Primary ID
spu Spu Related SPU entity
autoTakeDownTime DateTimeImmutable Scheduled takedown time
isActive bool Whether configuration is active
createTime DateTimeImmutable Creation time
updateTime DateTimeImmutable Last update time

AutoDownLog (Log Table)

Column Type Description
id string Unique log ID
config AutoDownTimeConfig Related configuration
spuId int Product SPU ID
action AutoDownLogAction Log action type
message string Log message
context array Additional context data
createTime DateTimeImmutable Log creation time
createdFromIp string Source IP address

AutoDownLogAction (Action Types)

Value Description Meaning
SCHEDULED Scheduled Configuration created/updated
EXECUTED Executed Takedown successfully executed
CANCELED Canceled Configuration was canceled
SKIPPED Skipped Takedown skipped (already down)
ERROR Error Error occurred during execution

Error Handling

The bundle provides specific exceptions for error handling:

use Tourze\ProductAutoDownBundle\Exception\SpuNotFoundException;
use Tourze\ProductAutoDownBundle\Exception\AutoDownServiceException;

try {
    $this->autoDownService->configureAutoTakeDownTime($spu, $time);
} catch (SpuNotFoundException $e) {
    // SPU not found
} catch (AutoDownServiceException $e) {
    // General service error
}

Scheduled Execution

Configure the command to run automatically using cron or Symfony Cron Jobs Bundle:

# crontab -e
# Run every hour
0 * * * * cd /var/www/project && php bin/console product:auto-take-down-spu

Using Symfony Cron Jobs Bundle:

// config/packages/cron_jobs.yaml
cron_jobs:
    auto_take_down_spu:
        command: 'product:auto-take-down-spu'
        schedule: '0 * * * *'  # Every hour
        description: 'Execute scheduled product takedowns'

Testing

Unit Tests

# Run all tests
./vendor/bin/phpunit packages/product-auto-down-bundle/tests

# Run specific test file
./vendor/bin/phpunit packages/product-auto-down-bundle/tests/Service/AutoDownServiceTest.php

# Generate coverage report
./vendor/bin/phpunit packages/product-auto-down-bundle/tests --coverage-html=coverage

Code Quality

# PHPStan static analysis
./vendor/bin/phpstan analyse packages/product-auto-down-bundle --level=8

# Code style check
./vendor/bin/php-cs-fixer fix packages/product-auto-down-bundle/src --dry-run

License

MIT License

Support

Tourze Team