tourze / product-ranking-bundle
产品排行榜管理系统,支持自动计算和手动调整排名
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/product-ranking-bundle
Requires
- doctrine/collections: ^2.3
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- easycorp/easyadmin-bundle: ^4
- knplabs/knp-menu: ^3.7
- symfony/config: ^7.3
- symfony/console: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-foundation: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/security-bundle: ^7.3
- symfony/security-http: ^7.3
- symfony/serializer: ^7.3
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/doctrine-helper: 1.0.*
- tourze/doctrine-indexed-bundle: 1.0.*
- tourze/doctrine-snowflake-bundle: 1.1.*
- tourze/doctrine-timestamp-bundle: 1.1.*
- tourze/doctrine-track-bundle: 1.1.*
- tourze/doctrine-user-bundle: 1.0.*
- tourze/easy-admin-extra-bundle: 1.0.*
- tourze/easy-admin-menu-bundle: 1.0.*
- tourze/json-rpc-cache-bundle: 1.0.*
- tourze/json-rpc-core: 1.0.*
- tourze/product-core-bundle: 1.0.*
- tourze/symfony-cron-job-bundle: 1.1.*
- tourze/symfony-dependency-service-loader: 1.0.*
Requires (Dev)
This package is auto-updated.
Last update: 2025-11-18 08:36:03 UTC
README
[]
(https://packagist.org/packages/tourze/product-ranking-bundle)
[]
(https://packagist.org/packages/tourze/product-ranking-bundle)
[]
(https://github.com/tourze/php-monorepo/actions)
[]
(https://coveralls.io/github/tourze/php-monorepo?branch=master)
A Symfony bundle for managing product rankings with automatic calculation and flexible positioning.
Table of Contents
- Features
- Installation
- Quick Start
- Configuration
- Basic Usage
- Advanced Usage
- Entities
- Commands
- API Procedures
- Admin Interface
- Dependencies
- License
Features
- Dynamic Rankings: Create multiple ranking lists with custom calculation SQL
- Position Management: Assign rankings to different display positions
- Fixed Rankings: Support for manually fixed product positions
- Automatic Updates: Scheduled command to refresh rankings
- Admin Interface: EasyAdmin CRUD controllers for management
- JSON-RPC APIs: Access ranking data via API procedures
Installation
composer require tourze/product-ranking-bundle
Quick Start
-
Install the bundle:
composer require tourze/product-ranking-bundle
-
Create a ranking list:
$rankingList = new RankingList(); $rankingList->setTitle('Top Products'); $rankingList->setValid(true); $entityManager->persist($rankingList);
-
Run ranking calculation:
php bin/console product:ranking-list:calc
-
Access via API:
$lists = $jsonRpc->call('GetProductRankingLists');
Configuration
Register the bundle in your Symfony application:
// config/bundles.php return [ // ... ProductRankingBundle\ProductRankingBundle::class => ['all' => true], ];
Basic Usage
Creating a Ranking List
use ProductRankingBundle\Entity\RankingList; $rankingList = new RankingList(); $rankingList->setTitle('Top Selling Products'); $rankingList->setSubtitle('Best sellers this month'); $rankingList->setValid(true); $rankingList->setCount(10); // Top 10 products $rankingList->setScoreSql(' SELECT p.id, COUNT(o.id) as score, "High sales volume" as reason FROM product_spu p JOIN order_item oi ON oi.spu_id = p.id JOIN orders o ON o.id = oi.order_id WHERE o.created_at > DATE_SUB(NOW(), INTERVAL 30 DAY) GROUP BY p.id ORDER BY score DESC ');
Accessing Rankings via API
// Get all ranking lists $lists = $jsonRpc->call('GetProductRankingLists'); // Get rankings for homepage position $homepageLists = $jsonRpc->call('GetProductRankingLists', [ 'position' => 'homepage' ]); // Check if a product is ranked $rankings = $jsonRpc->call('GetRankingListBySpu', [ 'spuId' => '123456' ]);
Advanced Usage
Custom SQL Ranking Algorithms
You can create sophisticated ranking algorithms using SQL:
-- Sales performance ranking SELECT p.id, (COUNT(o.id) * 0.7 + AVG(r.rating) * 0.3) as score, CONCAT('Sales: ', COUNT(o.id), ', Avg Rating: ', ROUND(AVG(r.rating), 2)) as reason FROM product_spu p LEFT JOIN order_item oi ON oi.spu_id = p.id LEFT JOIN orders o ON o.id = oi.order_id AND o.status = 'completed' LEFT JOIN product_review r ON r.spu_id = p.id WHERE p.valid = 1 GROUP BY p.id HAVING COUNT(o.id) >= 5 ORDER BY score DESC
Managing Fixed Positions
Manually set fixed positions for specific products:
use ProductRankingBundle\Entity\RankingItem; $item = new RankingItem(); $item->setList($rankingList); $item->setSpuId('special-product-id'); $item->setNumber(1); // Fixed at position 1 $item->setFixed(true); $item->setScore(999); $item->setTextReason('Featured product');
Scheduling Updates
The ranking calculation is automatically scheduled but can also be triggered manually:
# Manual update php bin/console product:ranking-list:calc # Add to crontab for custom scheduling */15 * * * * php /path/to/project/bin/console product:ranking-list:calc
Performance Optimization
For large datasets, consider:
- Index optimization: Ensure proper database indexes on fields used in your SQL
- Batch processing: Use query builders for large result sets
- Caching: Implement caching for frequently accessed rankings
// Example: Optimized query with pagination $items = $this->itemRepository->createQueryBuilder('i') ->where('i.list = :list') ->setParameter('list', $list) ->setMaxResults(100) ->getQuery() ->toIterable();
Entities
RankingList
Represents a product ranking list with title, subtitle, logo, and calculation SQL.
RankingItem
Individual items within a ranking list, including product SPU, score, and position.
RankingPosition
Display positions where ranking lists can be shown (e.g., homepage, category page).
Commands
product:ranking-list:calc
Calculates and updates all active ranking lists based on their SQL formulas.
php bin/console product:ranking-list:calc
This command:
- Runs every 30 minutes via cron (configured with
@AsCronTask) - Processes all active ranking lists
- Executes custom SQL to calculate product scores
- Updates ranking positions while preserving fixed items
- Removes items exceeding the list count limit
API Procedures
GetProductRankingLists
Retrieve all active ranking lists, optionally filtered by position.
GetRankingListBySpu
Get ranking information for a specific product SPU.
Admin Interface
The bundle provides EasyAdmin CRUD controllers for:
- Managing ranking lists
- Viewing and editing ranking items
- Configuring display positions
Dependencies
- Symfony 7.3+
- Doctrine ORM 3.0+
- Doctrine DBAL 4.0+
- tourze/doctrine-snowflake-bundle
- tourze/doctrine-timestamp-bundle
- tourze/doctrine-user-bundle
- tourze/product-core-bundle
- tourze/json-rpc-core
- tourze/json-rpc-cache-bundle
- tourze/symfony-cron-job-bundle
- tourze/easy-admin-extra-bundle
License
This bundle is licensed under the MIT License.