TestPickFunctionCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Services\PickService;
  4. use App\Module\Farm\Models\FarmCrop;
  5. use App\Module\Farm\Models\FarmSeed;
  6. use App\Module\Farm\Models\FarmLand;
  7. use App\Module\Farm\Models\FarmUser;
  8. use App\Module\Farm\Enums\GROWTH_STAGE;
  9. use Illuminate\Console\Command;
  10. /**
  11. * 测试摘取功能命令
  12. */
  13. class TestPickFunctionCommand extends Command
  14. {
  15. /**
  16. * 命令签名
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'farm:test-pick-function';
  21. /**
  22. * 命令描述
  23. *
  24. * @var string
  25. */
  26. protected $description = '测试农场摘取功能';
  27. /**
  28. * 执行命令
  29. *
  30. * @return int
  31. */
  32. public function handle(): int
  33. {
  34. $this->info('开始测试农场摘取功能...');
  35. try {
  36. // 测试获取摘取信息
  37. $this->testGetPickInfo();
  38. // 测试检查摘取条件
  39. $this->testCanPickCrop();
  40. $this->info('所有测试通过!');
  41. return 0;
  42. } catch (\Exception $e) {
  43. $this->error('测试失败: ' . $e->getMessage());
  44. $this->error('堆栈跟踪: ' . $e->getTraceAsString());
  45. return 1;
  46. }
  47. }
  48. /**
  49. * 测试获取摘取信息
  50. */
  51. private function testGetPickInfo(): void
  52. {
  53. $this->info('测试获取摘取信息...');
  54. // 查找一个成熟的作物
  55. $crop = FarmCrop::where('growth_stage', GROWTH_STAGE::MATURE)
  56. ->where('final_output_amount', '>', 0)
  57. ->first();
  58. if (!$crop) {
  59. $this->warn('没有找到成熟的作物,跳过测试');
  60. return;
  61. }
  62. $pickInfo = PickService::getPickInfo($crop->id);
  63. if ($pickInfo) {
  64. $this->info("作物ID: {$pickInfo->cropId}");
  65. $this->info("总产量: {$pickInfo->totalAmount}");
  66. $this->info("已摘取: {$pickInfo->pickedAmount}");
  67. $this->info("可摘取: {$pickInfo->pickableAmount}");
  68. $this->info("是否可摘取: " . ($pickInfo->canPick ? '是' : '否'));
  69. } else {
  70. $this->warn('获取摘取信息失败');
  71. }
  72. }
  73. /**
  74. * 测试检查摘取条件
  75. */
  76. private function testCanPickCrop(): void
  77. {
  78. $this->info('测试检查摘取条件...');
  79. // 查找一个作物
  80. $crop = FarmCrop::first();
  81. if (!$crop) {
  82. $this->warn('没有找到作物,跳过测试');
  83. return;
  84. }
  85. $result = PickService::canPickCrop($crop->id);
  86. $this->info("作物ID: {$crop->id}");
  87. $this->info("是否可摘取: " . ($result['can_pick'] ? '是' : '否'));
  88. $this->info("原因: {$result['reason']}");
  89. if (isset($result['pickable_amount'])) {
  90. $this->info("可摘取数量: {$result['pickable_amount']}");
  91. }
  92. }
  93. }