TestRegistrationPoints.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Longyi\RewardPoints\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Longyi\RewardPoints\Repositories\RewardPointRepository;
  5. use Longyi\RewardPoints\Models\RewardActiveRule;
  6. use Webkul\Customer\Models\Customer;
  7. class TestRegistrationPoints extends Command
  8. {
  9. protected $signature = 'reward-points:test-registration {email?}';
  10. protected $description = 'Test registration points for a customer';
  11. protected $rewardPointRepository;
  12. public function __construct(RewardPointRepository $rewardPointRepository)
  13. {
  14. parent::__construct();
  15. $this->rewardPointRepository = $rewardPointRepository;
  16. }
  17. public function handle()
  18. {
  19. $email = $this->argument('email');
  20. if (!$email) {
  21. // 获取最后一个注册的客户
  22. $customer = Customer::orderBy('id', 'desc')->first();
  23. } else {
  24. $customer = Customer::where('email', $email)->first();
  25. }
  26. if (!$customer) {
  27. $this->error('Customer not found!');
  28. return 1;
  29. }
  30. $this->info("Testing registration points for customer: {$customer->name} ({$customer->email})");
  31. $this->info("Customer ID: {$customer->id}");
  32. // 检查是否有注册规则
  33. $registrationRule = RewardActiveRule::where('type_of_transaction', RewardActiveRule::TYPE_REGISTRATION)
  34. ->where('status', 1)
  35. ->first();
  36. if ($registrationRule) {
  37. $this->info("Found registration rule:");
  38. $this->table(
  39. ['ID', 'Name', 'Points', 'Status'],
  40. [[
  41. $registrationRule->id,
  42. $registrationRule->name,
  43. $registrationRule->reward_point,
  44. $registrationRule->status ? 'Active' : 'Inactive'
  45. ]]
  46. );
  47. } else {
  48. $this->warn("No active registration rule found. Using config value.");
  49. $pointsFromConfig = config('rewardpoints.registration.points_per_registration', 100);
  50. $this->info("Points from config: {$pointsFromConfig}");
  51. }
  52. // 手动触发注册积分
  53. $this->info("\nManually adding registration points...");
  54. try {
  55. $event = new \stdClass();
  56. $event->id = $customer->id;
  57. $listener = new \Longyi\RewardPoints\Listeners\CustomerEvents($this->rewardPointRepository);
  58. $listener->handleCustomerRegistration($customer);
  59. $this->info("✓ Points added successfully!");
  60. // 显示当前积分
  61. $currentPoints = $this->rewardPointRepository->getCustomerPoints($customer->id);
  62. $this->info("Current points balance: {$currentPoints}");
  63. } catch (\Exception $e) {
  64. $this->error("Error: " . $e->getMessage());
  65. $this->error($e->getTraceAsString());
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. }