tourze / credit-bundle
积分系统核心模块,包含账户管理、交易处理和货币系统
Installs: 166
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/credit-bundle
Requires
- php: ^8.1
- brick/money: ^0.10
- doctrine/collections: ^2.3
- doctrine/common: ^3.5
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- knplabs/knp-menu: ^3.7
- moneyphp/money: ^4.6
- nesbot/carbon: ^2.72 || ^3
- phpoffice/phpspreadsheet: ^3.9
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/event-dispatcher-contracts: ^2.5 | ^3
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/lock: ^6.4
- symfony/security-bundle: ^6.4
- symfony/security-core: ^6.4
- symfony/security-http: ^6.4
- symfony/serializer: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/arrayable: 0.0.*
- tourze/benifit-bundle: 0.0.*
- tourze/bundle-dependency: 0.0.*
- tourze/currency-manage-bundle: 0.0.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-ip-bundle: 0.0.*
- tourze/doctrine-snowflake-bundle: 0.1.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-track-bundle: 0.1.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.1.*
- tourze/easy-admin-menu-bundle: 0.1.*
- tourze/enum-extra: 0.1.*
- tourze/json-rpc-cache-bundle: 0.1.*
- tourze/json-rpc-core: 0.0.*
- tourze/json-rpc-paginator-bundle: 0.0.*
- tourze/lock-service-bundle: 0.1.*
- tourze/resource-manage-bundle: 0.1.*
- tourze/symfony-async-bundle: 0.0.*
- tourze/symfony-snowflake-bundle: 0.0.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-10-31 19:54:16 UTC
README
This is a credit management module.
Table of Contents
- Features
- Installation
- Dependencies
- Quick Start
- Configuration
- Documentation
- Console Commands
- Advanced Usage
- Contributing
- License
- Changelog
Features
- Manages user and system credit accounts for multiple currencies.
- Supports credit increase, decrease, and transfer operations.
- Provides services for account and transaction management.
- Includes asynchronous processing for credit operations.
- Supports credit expiration and transaction remarks.
- Allows associating transactions with other business entities.
- Manages transaction limits and adjustment requests.
- Provides command-line tools for credit management tasks.
Installation
composer require tourze/credit-bundle
Dependencies
This bundle requires:
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine ORM 2.14 or higher
- symfony/security-core for user management
- tourze/lock-service-bundle for distributed locking
Optional dependencies:
- symfony/messenger for async operations
- doctrine/doctrine-migrations-bundle for database migrations
Quick Start
<?php use CreditBundle\Service\AccountService; use CreditBundle\Service\TransactionService; use CreditBundle\Entity\Currency; // You'll need to obtain/create a Currency instance use App\Entity\User; // User instance implementing UserInterface // Prerequisites: // 1. Ensure AccountService and TransactionService are available (e.g., via Symfony DI). // 2. Obtain or create a `Currency` entity instance. // 3. Obtain or create `User` entity instances. /** * @var AccountService $accountService * @var TransactionService $transactionService * @var Currency $currencyInstance // e.g., fetched from DB or created * @var User $userOne // An instance of your User entity * @var User $userTwo // An instance of your User entity */ // Get/create credit accounts for users $accountUserOne = $accountService->getAccountByUser($userOne, $currencyInstance); $accountUserTwo = $accountService->getAccountByUser($userTwo, $currencyInstance); // Increase credit for User One $eventNoIncrease = 'YOUR_UNIQUE_EVENT_ID_1'; // Ensure this is unique per operation $transactionService->increase( $eventNoIncrease, $accountUserOne, 100.0, // Amount 'Welcome bonus' // Remark ); // Decrease credit for User One $eventNoDecrease = 'YOUR_UNIQUE_EVENT_ID_2'; // Ensure this is unique $transactionService->decrease( $eventNoDecrease, $accountUserOne, 25.0, // Amount 'Purchase item #123' // Remark ); // Transfer credit from User One to User Two $eventNoTransfer = $transactionService->transfer( $accountUserOne, $accountUserTwo, 50.0, // Amount 'Gift for User Two' ); // For asynchronous operations: // $transactionService->asyncIncrease(...); // $transactionService->asyncDecrease(...); // Note: Check the Account entity (e.g., getEndingBalance()) or related services for balance retrieval methods.
Configuration
Bundle Registration
Add the bundle to your config/bundles.php:
return [ // ... other bundles CreditBundle\CreditBundle::class => ['all' => true], ];
Database Schema
Create and run migrations for the credit system tables:
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
The bundle creates the following tables:
credit_account- Credit accounts for users/systemcredit_transaction- Credit transaction recordscredit_currency- Available credit currenciescredit_limit- Transaction limitscredit_adjust_request- Manual adjustment requestscredit_consume_log- Consumption tracking logs
Service Configuration
The bundle uses Symfony's autowiring. No manual service configuration is required.
Optional configuration in config/packages/credit.yaml:
# Currently no specific configuration parameters are required # Services are automatically configured via the bundle's services.yaml
Documentation
This bundle provides several services to manage credits:
CreditBundle\Service\AccountService: For managing credit accounts (retrieving, creating for users or system).CreditBundle\Service\TransactionService: For performing credit transactions like increase, decrease, and transfer.CreditBundle\Service\CreditIncreaseService: Handles credit increase logic and async operations.CreditBundle\Service\CreditDecreaseService: Handles credit decrease logic and consumption tracking.CreditBundle\Service\CurrencyService: For managing currencies used in the credit system.CreditBundle\Service\TransactionLimitService: For managing transaction limits.
Key Entities:
CreditBundle\Entity\Account: Represents a credit account for a user or system, tied to a currency.CreditBundle\Entity\Transaction: Represents a single credit transaction (increase or decrease).CreditBundle\Entity\Currency: Represents a currency type (e.g., points, virtual cash).CreditBundle\Entity\Limit: Defines transaction limits for accounts.CreditBundle\Entity\AdjustRequest: For managing manual credit adjustments.CreditBundle\Entity\ConsumeLog: Logs how credit from income transactions is consumed by outgoing transactions.
Configuration:
This bundle relies on Symfony's autowiring and service autoconfiguration.
No specific configuration parameters are required for basic operation.
Services are automatically configured via the bundle's services.yaml.
API Documentation:
- Detailed API documentation is planned and will be linked here. For now, please refer to the service and entity class docblocks for method signatures and descriptions.
Advanced Features
- Asynchronous Operations: Certain credit operations can be handled asynchronously
- Credit Expiration: Support for time-limited credits with automatic expiration
- Transaction Limits: Define and enforce limits on credit transactions
- Manual Adjustments: Support manual credit adjustments with approval workflow
- Consumption Tracking: Detailed audit trails for transaction consumption
Performance Optimization
- Utilize asynchronous operations for non-critical credit updates
- Implement appropriate database indexing on frequently queried columns
- Consider caching strategies for currency and system account data
Console Commands
The bundle provides several console commands for credit management:
credit:adjust
Automatically adjust credit balances to match transaction records for accounts updated in the last day.
php bin/console credit:adjust
credit:batch-adjust
Batch adjust credits using an Excel file.
php bin/console credit:batch-adjust /path/to/file.xls
Excel file format requirements:
- Headers must include: "账户名" (Account Name), "变更数值" (Change Value), "变更备注" (Change Remark), "积分名" (Credit Name)
credit:calc:expire-transaction
Calculate and process expired credits for a specific account.
php bin/console credit:calc:expire-transaction <accountId>
credit:decrease
Decrease credits for a specific user.
php bin/console credit:decrease <currency> <userId> <amount>
Parameters:
currency: Credit currency codeuserId: User IDamount: Amount to decrease
credit:increase
Increase credits for a specific user.
php bin/console credit:increase <currency> <userId> <amount>
Parameters:
currency: Credit currency codeuserId: User IDamount: Amount to increase
credit:send-notice
Send overdraft notifications (TODO - functionality not yet implemented).
php bin/console credit:send-notice
Advanced Usage
Asynchronous Operations
For high-performance scenarios, use async methods:
// Async increase $transactionService->asyncIncrease( 'event_id', $account, 100.0, 'Async bonus' ); // Async decrease $transactionService->asyncDecrease( 'event_id', $account, 50.0, 'Async purchase' );
Credit Expiration
Set expiration dates for credits:
$expireTime = new \DateTime('+30 days'); $transactionService->increase( 'event_id', $account, 100.0, 'Limited time bonus', $expireTime );
Transaction Limits
Configure transaction limits:
use CreditBundle\Entity\Limit; use CreditBundle\Enum\LimitType; $limit = new Limit(); $limit->setAccount($account); $limit->setType(LimitType::DAILY); $limit->setAmount(1000.0); $entityManager->persist($limit); $entityManager->flush();
Manual Adjustments
Create adjustment requests:
use CreditBundle\Entity\AdjustRequest; use CreditBundle\Enum\AdjustRequestType; $adjustRequest = new AdjustRequest(); $adjustRequest->setAccount($account); $adjustRequest->setType(AdjustRequestType::INCREASE); $adjustRequest->setAmount(500.0); $adjustRequest->setRemark('Manual adjustment for error correction'); $entityManager->persist($adjustRequest); $entityManager->flush();
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.
Known Issues
Test Environment Dependencies
Currently, PHPUnit tests require complex Symfony service configuration and may fail in isolation due to missing service dependencies. This is being tracked in Issue #706.
For development and testing:
- PHPStan analysis works correctly
- Individual service and entity testing can be done through integration with the full application
- Unit tests for simple entities and value objects work as expected
Changelog
- Initial README structure based on template.
- Added preliminary Features, Quick Start, and Documentation sections based on initial code review.
- Populated Documentation section with details on services, entities, and configuration.
- Added notes on API documentation, advanced features, and performance optimization suggestions.
- Created a placeholder
CONTRIBUTING.mdfile. - Updated build status badge to GitHub Actions and corrected
CONTRIBUTING.mdlink. - Added Known Issues section documenting test environment challenges.