| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace App\Console\Commands;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\Farm\Enums\LAND_STATUS;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmSeed;
- use App\Module\Farm\Services\CropService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 测试作物软删除功能的命令
- */
- class TestCropSoftDelete extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'farm:test-soft-delete {user_id} {land_id} {item_id}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '测试农场作物软删除功能';
- /**
- * Execute the console command.
- */
- public function handle()
- {
- $userId = (int) $this->argument('user_id');
- $landId = (int) $this->argument('land_id');
- $itemId = (int) $this->argument('item_id');
- $this->info("开始测试作物软删除功能...");
- $this->info("用户ID: {$userId}, 土地ID: {$landId}, 种子物品ID: {$itemId}");
- // 1. 清理测试环境
- $this->info("\n=== 1. 清理测试环境 ===");
- FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
-
- $land = FarmLand::find($landId);
- if (!$land) {
- $this->error("土地不存在");
- return 1;
- }
-
- $land->status = LAND_STATUS::IDLE->value;
- $land->has_crop = false;
- $land->save();
- $this->info("测试环境已清理");
- // 2. 种植作物
- $this->info("\n=== 2. 种植作物 ===");
- DB::beginTransaction();
- try {
- $plantResult = CropService::plantCrop($userId, $landId, $itemId);
- if ($plantResult) {
- $cropId = $plantResult['crop']->id;
- $this->info("✅ 种植成功,作物ID: {$cropId}");
- DB::commit();
- } else {
- $this->error("❌ 种植失败");
- DB::rollBack();
- return 1;
- }
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("❌ 种植异常: " . $e->getMessage());
- return 1;
- }
- // 3. 验证作物存在
- $this->info("\n=== 3. 验证作物存在 ===");
- $crop = FarmCrop::find($cropId);
- if ($crop && !$crop->deleted_at) {
- $this->info("✅ 作物存在且未被软删除");
- } else {
- $this->error("❌ 作物不存在或已被软删除");
- }
- // 4. 铲除作物(软删除)
- $this->info("\n=== 4. 铲除作物(软删除) ===");
- DB::beginTransaction();
- try {
- $removeResult = CropService::removeCrop($userId, $landId);
- if ($removeResult['success']) {
- $this->info("✅ 铲除成功");
- DB::commit();
- } else {
- $this->error("❌ 铲除失败");
- DB::rollBack();
- return 1;
- }
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("❌ 铲除异常: " . $e->getMessage());
- return 1;
- }
- // 5. 验证软删除
- $this->info("\n=== 5. 验证软删除 ===");
- $crop->refresh();
- if ($crop->deleted_at) {
- $this->info("✅ 作物已被软删除,删除时间: " . $crop->deleted_at);
- } else {
- $this->error("❌ 作物未被软删除");
- }
- // 验证正常查询不返回软删除记录
- $activeCrop = FarmCrop::where('land_id', $landId)->first();
- if (!$activeCrop) {
- $this->info("✅ 正常查询不返回软删除的作物");
- } else {
- $this->error("❌ 正常查询仍然返回作物记录");
- }
- // 验证可以查询软删除记录
- $trashedCrop = FarmCrop::onlyTrashed()->where('land_id', $landId)->first();
- if ($trashedCrop) {
- $this->info("✅ 可以查询到软删除的作物记录");
- } else {
- $this->error("❌ 无法查询到软删除的作物记录");
- }
- // 6. 测试重新种植
- $this->info("\n=== 6. 测试重新种植 ===");
- // 检查土地状态
- $land->refresh();
- $this->info("当前土地状态: " . $land->status . " (期望: " . LAND_STATUS::IDLE->value . ")");
- $this->info("土地是否有作物: " . ($land->has_crop ? '是' : '否'));
- // 检查是否还有活跃作物
- $activeCrop = FarmCrop::where('land_id', $landId)->first();
- $this->info("活跃作物: " . ($activeCrop ? "存在 (ID: {$activeCrop->id})" : '不存在'));
- // 检查种子配置
- $seed = FarmSeed::where('item_id', $itemId)->first();
- $this->info("种子配置: " . ($seed ? "存在 (ID: {$seed->id})" : '不存在'));
- // 检查当前事务状态
- $transactionLevel = DB::transactionLevel();
- $this->info("当前事务级别: {$transactionLevel}");
- // 如果没有事务,开启新事务
- if ($transactionLevel === 0) {
- DB::beginTransaction();
- $this->info("已开启新事务");
- }
- try {
- $newPlantResult = CropService::plantCrop($userId, $landId, $itemId);
- if ($newPlantResult) {
- $newCropId = $newPlantResult['crop']->id;
- $this->info("✅ 重新种植成功,新作物ID: {$newCropId}");
- if ($newCropId != $cropId) {
- $this->info("✅ 新作物ID与原作物ID不同,符合预期");
- } else {
- $this->error("❌ 新作物ID与原作物ID相同,不符合预期");
- }
- DB::commit();
- } else {
- $this->error("❌ 重新种植失败,返回null");
- DB::rollBack();
- }
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("❌ 重新种植异常: " . $e->getMessage());
- }
- // 7. 测试恢复软删除
- $this->info("\n=== 7. 测试恢复软删除 ===");
- // 先清理当前作物
- if (isset($newCropId)) {
- DB::beginTransaction();
- try {
- CropService::removeCrop($userId, $landId);
- DB::commit();
- $this->info("已清理当前作物");
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("清理当前作物失败: " . $e->getMessage());
- }
- }
- // 恢复原作物
- DB::beginTransaction();
- try {
- $restoreResult = CropService::restoreCrop($userId, $landId);
- if ($restoreResult['success']) {
- $this->info("✅ 恢复软删除作物成功");
- DB::commit();
- } else {
- $this->error("❌ 恢复软删除作物失败");
- DB::rollBack();
- }
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("❌ 恢复软删除作物异常: " . $e->getMessage());
- }
- // 8. 测试强制删除
- $this->info("\n=== 8. 测试强制删除 ===");
- DB::beginTransaction();
- try {
- $forceDeleteResult = CropService::forceDeleteCrop($userId, $landId, '测试强制删除');
- if ($forceDeleteResult['success']) {
- $this->info("✅ 强制删除成功");
- DB::commit();
- } else {
- $this->error("❌ 强制删除失败");
- DB::rollBack();
- }
- } catch (\Exception $e) {
- DB::rollBack();
- $this->error("❌ 强制删除异常: " . $e->getMessage());
- }
- // 验证物理删除
- $anyRecord = FarmCrop::withTrashed()->where('land_id', $landId)->first();
- if (!$anyRecord) {
- $this->info("✅ 作物已被物理删除,无任何记录");
- } else {
- $this->error("❌ 作物未被物理删除,仍有记录: ID={$anyRecord->id}, deleted_at=" . ($anyRecord->deleted_at ?? 'NULL'));
- }
- // 9. 清理测试数据
- $this->info("\n=== 9. 清理测试数据 ===");
- FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
- $land->status = LAND_STATUS::IDLE->value;
- $land->has_crop = false;
- $land->save();
- $this->info("测试数据已清理");
- $this->info("\n软删除功能测试完成!");
- return 0;
- }
- }
|