TestCropSoftDelete.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\Farm\Enums\LAND_STATUS;
  5. use App\Module\Farm\Models\FarmCrop;
  6. use App\Module\Farm\Models\FarmLand;
  7. use App\Module\Farm\Models\FarmSeed;
  8. use App\Module\Farm\Services\CropService;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. /**
  13. * 测试作物软删除功能的命令
  14. */
  15. class TestCropSoftDelete extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'farm:test-soft-delete {user_id} {land_id} {item_id}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '测试农场作物软删除功能';
  29. /**
  30. * Execute the console command.
  31. */
  32. public function handle()
  33. {
  34. $userId = (int) $this->argument('user_id');
  35. $landId = (int) $this->argument('land_id');
  36. $itemId = (int) $this->argument('item_id');
  37. $this->info("开始测试作物软删除功能...");
  38. $this->info("用户ID: {$userId}, 土地ID: {$landId}, 种子物品ID: {$itemId}");
  39. // 1. 清理测试环境
  40. $this->info("\n=== 1. 清理测试环境 ===");
  41. FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
  42. $land = FarmLand::find($landId);
  43. if (!$land) {
  44. $this->error("土地不存在");
  45. return 1;
  46. }
  47. $land->status = LAND_STATUS::IDLE->value;
  48. $land->has_crop = false;
  49. $land->save();
  50. $this->info("测试环境已清理");
  51. // 2. 种植作物
  52. $this->info("\n=== 2. 种植作物 ===");
  53. DB::beginTransaction();
  54. try {
  55. $plantResult = CropService::plantCrop($userId, $landId, $itemId);
  56. if ($plantResult) {
  57. $cropId = $plantResult['crop']->id;
  58. $this->info("✅ 种植成功,作物ID: {$cropId}");
  59. DB::commit();
  60. } else {
  61. $this->error("❌ 种植失败");
  62. DB::rollBack();
  63. return 1;
  64. }
  65. } catch (\Exception $e) {
  66. DB::rollBack();
  67. $this->error("❌ 种植异常: " . $e->getMessage());
  68. return 1;
  69. }
  70. // 3. 验证作物存在
  71. $this->info("\n=== 3. 验证作物存在 ===");
  72. $crop = FarmCrop::find($cropId);
  73. if ($crop && !$crop->deleted_at) {
  74. $this->info("✅ 作物存在且未被软删除");
  75. } else {
  76. $this->error("❌ 作物不存在或已被软删除");
  77. }
  78. // 4. 铲除作物(软删除)
  79. $this->info("\n=== 4. 铲除作物(软删除) ===");
  80. DB::beginTransaction();
  81. try {
  82. $removeResult = CropService::removeCrop($userId, $landId);
  83. if ($removeResult['success']) {
  84. $this->info("✅ 铲除成功");
  85. DB::commit();
  86. } else {
  87. $this->error("❌ 铲除失败");
  88. DB::rollBack();
  89. return 1;
  90. }
  91. } catch (\Exception $e) {
  92. DB::rollBack();
  93. $this->error("❌ 铲除异常: " . $e->getMessage());
  94. return 1;
  95. }
  96. // 5. 验证软删除
  97. $this->info("\n=== 5. 验证软删除 ===");
  98. $crop->refresh();
  99. if ($crop->deleted_at) {
  100. $this->info("✅ 作物已被软删除,删除时间: " . $crop->deleted_at);
  101. } else {
  102. $this->error("❌ 作物未被软删除");
  103. }
  104. // 验证正常查询不返回软删除记录
  105. $activeCrop = FarmCrop::where('land_id', $landId)->first();
  106. if (!$activeCrop) {
  107. $this->info("✅ 正常查询不返回软删除的作物");
  108. } else {
  109. $this->error("❌ 正常查询仍然返回作物记录");
  110. }
  111. // 验证可以查询软删除记录
  112. $trashedCrop = FarmCrop::onlyTrashed()->where('land_id', $landId)->first();
  113. if ($trashedCrop) {
  114. $this->info("✅ 可以查询到软删除的作物记录");
  115. } else {
  116. $this->error("❌ 无法查询到软删除的作物记录");
  117. }
  118. // 6. 测试重新种植
  119. $this->info("\n=== 6. 测试重新种植 ===");
  120. // 检查土地状态
  121. $land->refresh();
  122. $this->info("当前土地状态: " . $land->status . " (期望: " . LAND_STATUS::IDLE->value . ")");
  123. $this->info("土地是否有作物: " . ($land->has_crop ? '是' : '否'));
  124. // 检查是否还有活跃作物
  125. $activeCrop = FarmCrop::where('land_id', $landId)->first();
  126. $this->info("活跃作物: " . ($activeCrop ? "存在 (ID: {$activeCrop->id})" : '不存在'));
  127. // 检查种子配置
  128. $seed = FarmSeed::where('item_id', $itemId)->first();
  129. $this->info("种子配置: " . ($seed ? "存在 (ID: {$seed->id})" : '不存在'));
  130. // 检查当前事务状态
  131. $transactionLevel = DB::transactionLevel();
  132. $this->info("当前事务级别: {$transactionLevel}");
  133. // 如果没有事务,开启新事务
  134. if ($transactionLevel === 0) {
  135. DB::beginTransaction();
  136. $this->info("已开启新事务");
  137. }
  138. try {
  139. $newPlantResult = CropService::plantCrop($userId, $landId, $itemId);
  140. if ($newPlantResult) {
  141. $newCropId = $newPlantResult['crop']->id;
  142. $this->info("✅ 重新种植成功,新作物ID: {$newCropId}");
  143. if ($newCropId != $cropId) {
  144. $this->info("✅ 新作物ID与原作物ID不同,符合预期");
  145. } else {
  146. $this->error("❌ 新作物ID与原作物ID相同,不符合预期");
  147. }
  148. DB::commit();
  149. } else {
  150. $this->error("❌ 重新种植失败,返回null");
  151. DB::rollBack();
  152. }
  153. } catch (\Exception $e) {
  154. DB::rollBack();
  155. $this->error("❌ 重新种植异常: " . $e->getMessage());
  156. }
  157. // 7. 测试恢复软删除
  158. $this->info("\n=== 7. 测试恢复软删除 ===");
  159. // 先清理当前作物
  160. if (isset($newCropId)) {
  161. DB::beginTransaction();
  162. try {
  163. CropService::removeCrop($userId, $landId);
  164. DB::commit();
  165. $this->info("已清理当前作物");
  166. } catch (\Exception $e) {
  167. DB::rollBack();
  168. $this->error("清理当前作物失败: " . $e->getMessage());
  169. }
  170. }
  171. // 恢复原作物
  172. DB::beginTransaction();
  173. try {
  174. $restoreResult = CropService::restoreCrop($userId, $landId);
  175. if ($restoreResult['success']) {
  176. $this->info("✅ 恢复软删除作物成功");
  177. DB::commit();
  178. } else {
  179. $this->error("❌ 恢复软删除作物失败");
  180. DB::rollBack();
  181. }
  182. } catch (\Exception $e) {
  183. DB::rollBack();
  184. $this->error("❌ 恢复软删除作物异常: " . $e->getMessage());
  185. }
  186. // 8. 测试强制删除
  187. $this->info("\n=== 8. 测试强制删除 ===");
  188. DB::beginTransaction();
  189. try {
  190. $forceDeleteResult = CropService::forceDeleteCrop($userId, $landId, '测试强制删除');
  191. if ($forceDeleteResult['success']) {
  192. $this->info("✅ 强制删除成功");
  193. DB::commit();
  194. } else {
  195. $this->error("❌ 强制删除失败");
  196. DB::rollBack();
  197. }
  198. } catch (\Exception $e) {
  199. DB::rollBack();
  200. $this->error("❌ 强制删除异常: " . $e->getMessage());
  201. }
  202. // 验证物理删除
  203. $anyRecord = FarmCrop::withTrashed()->where('land_id', $landId)->first();
  204. if (!$anyRecord) {
  205. $this->info("✅ 作物已被物理删除,无任何记录");
  206. } else {
  207. $this->error("❌ 作物未被物理删除,仍有记录: ID={$anyRecord->id}, deleted_at=" . ($anyRecord->deleted_at ?? 'NULL'));
  208. }
  209. // 9. 清理测试数据
  210. $this->info("\n=== 9. 清理测试数据 ===");
  211. FarmCrop::withTrashed()->where('land_id', $landId)->forceDelete();
  212. $land->status = LAND_STATUS::IDLE->value;
  213. $land->has_crop = false;
  214. $land->save();
  215. $this->info("测试数据已清理");
  216. $this->info("\n软删除功能测试完成!");
  217. return 0;
  218. }
  219. }