Browse Source

修复铲除作物LastData缺少土地数据问题:添加土地状态变更事件触发

notfff 7 months ago
parent
commit
8d8e32302c

+ 15 - 2
app/Module/AppGame/Handler/Land/RemoveCropHandler.php

@@ -58,7 +58,7 @@ class RemoveCropHandler extends BaseHandler
 
             // 调用铲除作物服务
             $result = CropService::removeCrop($userId, $landId);
-            if (!$result) {
+            if (!$result['success']) {
                 throw new LogicException("铲除作物失败,请检查土地状态");
             }
 
@@ -74,10 +74,23 @@ class RemoveCropHandler extends BaseHandler
             // 提交事务
             DB::commit();
 
+            // 如果土地状态发生变更,触发土地状态变更事件(在事务外)
+            if ($result['status_changed']) {
+                event(new \App\Module\Farm\Events\LandStatusChangedEvent(
+                    $userId,
+                    $landId,
+                    $result['old_status'],
+                    $result['new_status']
+                ));
+            }
+
             Log::info('用户铲除作物成功', [
                 'user_id' => $userId,
                 'land_id' => $landId,
-                'tool_item_id' => $toolItemId
+                'tool_item_id' => $toolItemId,
+                'status_changed' => $result['status_changed'],
+                'old_status' => $result['old_status'],
+                'new_status' => $result['new_status']
             ]);
 
         }  catch (\Exception $e) {

+ 19 - 10
app/Module/Farm/Logics/CropLogic.php

@@ -552,8 +552,8 @@ class CropLogic
     public function removeCrop(int $userId, int $landId): bool
     {
         try {
-            // 开启事务
-            DB::beginTransaction();
+            // 检查是否已开启事务
+            \UCore\Db\Helper::check_tr();
 
             // 获取土地信息
             $land = FarmLand::where('id', $landId)
@@ -574,34 +574,43 @@ class CropLogic
 
             if (!$crop) {
                 // 如果没有作物但土地状态不是空闲,修正土地状态
+                $oldLandStatus = $land->status;
                 $land->status = LAND_STATUS::IDLE->value;
                 $land->save();
 
-                DB::commit();
+                // 记录状态变更信息,由调用方处理事件触发
+                Log::info('土地状态已修正', [
+                    'user_id' => $userId,
+                    'land_id' => $landId,
+                    'old_status' => $oldLandStatus,
+                    'new_status' => $land->status
+                ]);
+
                 return true;
             }
 
             // 删除作物记录
             $crop->delete();
 
+            // 记录旧状态
+            $oldLandStatus = $land->status;
+
             // 更新土地状态
             $land->status = LAND_STATUS::IDLE->value;
             $land->save();
 
-            // 提交事务
-            DB::commit();
+            // 记录状态变更信息,由调用方处理事件触发和事务提交
 
             Log::info('铲除作物成功', [
                 'user_id' => $userId,
                 'land_id' => $landId,
-                'crop_id' => $crop->id
+                'crop_id' => $crop->id,
+                'old_status' => $oldLandStatus,
+                'new_status' => $land->status
             ]);
 
             return true;
         } catch (\Exception $e) {
-            // 回滚事务
-            DB::rollBack();
-
             Log::error('铲除作物失败', [
                 'user_id' => $userId,
                 'land_id' => $landId,
@@ -609,7 +618,7 @@ class CropLogic
                 'trace' => $e->getTraceAsString()
             ]);
 
-            return false;
+            throw $e; // 重新抛出异常,由调用方处理事务回滚
         }
     }
 

+ 30 - 4
app/Module/Farm/Services/CropService.php

@@ -251,13 +251,39 @@ class CropService
      *
      * @param int $userId
      * @param int $landId
-     * @return bool
+     * @return array 返回操作结果和状态变更信息
      */
-    public static function removeCrop(int $userId, int $landId): bool
+    public static function removeCrop(int $userId, int $landId): array
     {
         try {
+            // 获取铲除前的土地状态
+            $land = \App\Module\Farm\Models\FarmLand::where('id', $landId)
+                ->where('user_id', $userId)
+                ->first();
+
+            if (!$land) {
+                throw new \Exception('土地不存在');
+            }
+
+            $oldStatus = $land->status;
+
             $cropLogic = new CropLogic();
-            return $cropLogic->removeCrop($userId, $landId);
+            $result = $cropLogic->removeCrop($userId, $landId);
+
+            if ($result) {
+                // 获取铲除后的土地状态
+                $land->refresh();
+                $newStatus = $land->status;
+
+                return [
+                    'success' => true,
+                    'status_changed' => $oldStatus !== $newStatus,
+                    'old_status' => $oldStatus,
+                    'new_status' => $newStatus
+                ];
+            }
+
+            return ['success' => false];
         } catch (\Exception $e) {
             Log::error('铲除作物失败', [
                 'user_id' => $userId,
@@ -266,7 +292,7 @@ class CropService
                 'trace' => $e->getTraceAsString()
             ]);
 
-            return false;
+            throw $e; // 重新抛出异常,由调用方处理
         }
     }
 }