notfff il y a 7 mois
Parent
commit
3745e2db47

+ 1 - 23
app/Module/AppGame/Handler/God/OpenHandler.php

@@ -9,8 +9,6 @@ use App\Module\GameItems\Services\ItemService;
 use Google\Protobuf\Internal\Message;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
-use Uraus\Kku\Common\DataGod;
-use Uraus\Kku\Common\LastData;
 use Uraus\Kku\Request\RequestGodOpen;
 use Uraus\Kku\Response\ResponseGodOpen;
 use UCore\Exception\LogicException;
@@ -71,11 +69,6 @@ class OpenHandler extends BaseHandler
             $this->response->setCode(0);
             $this->response->setMsg($result['message']);
 
-            // 设置LastData
-            if (isset($result['last_data'])) {
-                $this->response->setLastData($result['last_data']);
-            }
-
         } catch (\UCore\Exception\ValidateException $e) {
             // 验证失败,此时可能还没有开启事务
             $this->response->setCode(400);
@@ -188,20 +181,6 @@ class OpenHandler extends BaseHandler
             throw new LogicException("神像激活失败");
         }
 
-        // 创建LastData对象,用于返回神像信息
-        $lastData = new LastData();
-        $godList = [];
-
-        // 创建神像数据
-        $dataGod = new DataGod();
-        $dataGod->setId($godId);
-        $dataGod->setStatus(true);
-        $dataGod->setVaidTime($buff->expire_time->timestamp);
-        $godList[] = $dataGod;
-
-        // 设置神像列表到LastData
-        $lastData->setGods($godList);
-
         // 记录日志
         Log::info('用户激活神像成功', [
             'user_id' => $userId,
@@ -209,12 +188,11 @@ class OpenHandler extends BaseHandler
             'item_id' => $itemId,
             'duration_seconds' => $godDurationSeconds,
             'duration_hours' => $durationHours,
-            'expire_time' => $buff->expire_time->toDateTimeString()
+            'expire_time' => $buff->expire_time ? $buff->expire_time->toDateTimeString() : null
         ]);
 
         return [
             'message' => '神像激活成功',
-            'last_data' => $lastData,
             'buff' => $buff
         ];
     }

+ 4 - 44
app/Module/AppGame/Handler/Land/HarvestHandler.php

@@ -36,8 +36,6 @@ class HarvestHandler extends BaseHandler
     {
         // 创建响应对象
         $response = new ResponseLandHarvest();
-
-        try {
             // 获取请求参数
             $landId = $data->getLandId();
             $userId = $this->user_id;
@@ -50,6 +48,8 @@ class HarvestHandler extends BaseHandler
 
             // 验证数据
             $validation->validated();
+        try {
+           
 
             // 验证通过后,开启事务
             DB::beginTransaction();
@@ -63,55 +63,15 @@ class HarvestHandler extends BaseHandler
             // 提交事务
             DB::commit();
 
-            // 获取更新后的土地信息并返回
-            $updatedLand = LandService::getUserLand($userId, $landId);
-            if ($updatedLand) {
-                // 创建LastData对象
-                $lastData = new LastData();
-                $landData = LandInfoDto::toDataLand($updatedLand);
-                $lastData->setLands([$landData]);
-
-                // 设置LastData到响应
-                $this->response->setLastData($lastData);
-            }
-
-            // 设置响应状态
-            $this->response->setCode(0);
-            $this->response->setMsg('收获成功');
 
+            
             Log::info('用户收获成功', [
                 'user_id' => $userId,
                 'land_id' => $landId,
                 'harvest_result' => $harvestResult->data ?? []
             ]);
 
-        } catch (\UCore\Exception\ValidateException $e) {
-            // 验证失败,此时可能还没有开启事务
-            $this->response->setCode(400);
-            $this->response->setMsg($e->getMessage());
-
-            Log::warning('收获验证失败', [
-                'user_id' => $userId ?? null,
-                'land_id' => $landId ?? null,
-                'error' => $e->getMessage()
-            ]);
-
-        } catch (LogicException $e) {
-            // 业务逻辑异常,需要回滚事务
-            if (DB::transactionLevel() > 0) {
-                DB::rollBack();
-            }
-
-            $this->response->setCode(400);
-            $this->response->setMsg($e->getMessage());
-
-            Log::warning('用户收获失败', [
-                'user_id' => $userId ?? null,
-                'land_id' => $landId ?? null,
-                'error' => $e->getMessage()
-            ]);
-
-        } catch (\Exception $e) {
+        }catch (\Exception $e) {
             // 系统异常,需要回滚事务
             if (DB::transactionLevel() > 0) {
                 DB::rollBack();

+ 3 - 32
app/Module/AppGame/Handler/Land/PesticideHandler.php

@@ -36,7 +36,6 @@ class PesticideHandler extends BaseHandler
         // 创建响应对象
         $response = new ResponseLandPesticide();
 
-        try {
             // 获取请求参数
             $landId = $data->getLandId();
             $userItemId = $data->getItemId();
@@ -52,6 +51,7 @@ class PesticideHandler extends BaseHandler
 
             // 验证数据
             $validation->validated();
+        try {
 
             // 验证通过后,开启事务
             DB::beginTransaction();
@@ -68,39 +68,10 @@ class PesticideHandler extends BaseHandler
             // 提交事务
             DB::commit();
 
-            // 设置响应状态
-            $this->response->setCode(0);
-            $this->response->setMsg($result['message']);
-
-        } catch (\UCore\Exception\ValidateException $e) {
-            // 验证失败,此时可能还没有开启事务
-            $this->response->setCode(400);
-            $this->response->setMsg($e->getMessage());
-
-            Log::warning('用户除虫验证失败', [
-                'user_id' => $this->user_id,
-                'land_id' => $landId ?? null,
-                'item_id' => $userItemId ?? null,
-                'error' => $e->getMessage()
-            ]);
-        } catch (LogicException $e) {
-            // 业务逻辑异常,需要回滚事务
-            if (DB::transactionLevel() > 0) {
-                DB::rollBack();
-            }
 
-            // 设置错误响应
-            $this->response->setCode(400);
-            $this->response->setMsg($e->getMessage());
+        
 
-            Log::warning('用户除虫失败', [
-                'user_id' => $this->user_id,
-                'land_id' => $landId ?? null,
-                'item_id' => $userItemId ?? null,
-                'error' => $e->getMessage(),
-                'trace' => $e->getTraceAsString()
-            ]);
-        } catch (\Exception $e) {
+        }  catch (\Exception $e) {
             // 系统异常,需要回滚事务
             if (DB::transactionLevel() > 0) {
                 DB::rollBack();

+ 3 - 44
app/Module/AppGame/Handler/Land/SowHandler.php

@@ -38,7 +38,6 @@ class SowHandler extends BaseHandler
         // 创建响应对象
         $response = new ResponseLandSow();
 
-        try {
             // 获取请求参数
             $landId = $data->getLandId();
             $itemId = $data->getItemId();
@@ -56,6 +55,8 @@ class SowHandler extends BaseHandler
             // 验证数据
             $validation->validated();
 
+        try {
+
             // 验证通过后,开启事务
             DB::beginTransaction();
 
@@ -81,21 +82,7 @@ class SowHandler extends BaseHandler
             // 提交事务
             DB::commit();
 
-            // 获取更新后的土地信息并返回
-            $updatedLand = LandService::getUserLand($userId, $landId);
-            if ($updatedLand) {
-                // 创建LastData对象
-                $lastData = new LastData();
-                $landData = LandInfoDto::toDataLand($updatedLand);
-                $lastData->setLands([$landData]);
-
-                // 设置LastData到响应
-                $this->response->setLastData($lastData);
-            }
-
-            // 设置响应状态
-            $this->response->setCode(0);
-            $this->response->setMsg('种植成功');
+            // LastData 由中间件处理
 
             Log::info('用户种植成功', [
                 'user_id' => $userId,
@@ -106,34 +93,6 @@ class SowHandler extends BaseHandler
                 'sow_log_id' => $sowLogId
             ]);
 
-        } catch (\UCore\Exception\ValidateException $e) {
-            // 验证失败,此时可能还没有开启事务
-            $this->response->setCode(400);
-            $this->response->setMsg($e->getMessage());
-
-            Log::warning('种植验证失败', [
-                'user_id' => $userId ?? null,
-                'land_id' => $landId ?? null,
-                'item_id' => $itemId ?? null,
-                'error' => $e->getMessage()
-            ]);
-
-        } catch (LogicException $e) {
-            // 业务逻辑异常,需要回滚事务
-            if (DB::transactionLevel() > 0) {
-                DB::rollBack();
-            }
-
-            $this->response->setCode(400);
-            $this->response->setMsg($e->getMessage());
-
-            Log::warning('用户种植失败', [
-                'user_id' => $userId ?? null,
-                'land_id' => $landId ?? null,
-                'item_id' => $itemId ?? null,
-                'error' => $e->getMessage()
-            ]);
-
         } catch (\Exception $e) {
             // 系统异常,需要回滚事务
             if (DB::transactionLevel() > 0) {

+ 145 - 0
app/Module/Farm/Commands/FixCropFinalOutputCommand.php

@@ -0,0 +1,145 @@
+<?php
+
+namespace App\Module\Farm\Commands;
+
+use App\Module\Farm\Enums\GROWTH_STAGE;
+use App\Module\Farm\Logics\CropLogic;
+use App\Module\Farm\Models\FarmCrop;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * 修复作物final_output_item_id的命令
+ * 
+ * 这个命令用于修复那些应该有final_output_item_id但没有设置的作物
+ */
+class FixCropFinalOutputCommand extends Command
+{
+    /**
+     * 命令名称
+     *
+     * @var string
+     */
+    protected $signature = 'farm:fix-crop-final-output {--dry-run : 只显示需要修复的数据,不实际修复}';
+
+    /**
+     * 命令描述
+     *
+     * @var string
+     */
+    protected $description = '修复作物的final_output_item_id字段';
+
+    /**
+     * 执行命令
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $isDryRun = $this->option('dry-run');
+        
+        if ($isDryRun) {
+            $this->info('执行干运行模式,只显示需要修复的数据...');
+        } else {
+            $this->info('开始修复作物final_output_item_id...');
+        }
+
+        try {
+            $cropLogic = new CropLogic();
+
+            // 查找所有发芽期及以后但没有final_output_item_id的作物
+            $problematicCrops = FarmCrop::whereNull('final_output_item_id')
+                ->where('growth_stage', '>=', GROWTH_STAGE::SPROUT->value)
+                ->with(['seed', 'land'])
+                ->get();
+
+            $this->info("找到 {$problematicCrops->count()} 个需要修复的作物");
+
+            if ($problematicCrops->isEmpty()) {
+                $this->info('没有需要修复的作物');
+                return 0;
+            }
+
+            // 显示详细信息
+            $this->table(
+                ['作物ID', '用户ID', '土地ID', '种子ID', '当前阶段', '种植时间'],
+                $problematicCrops->map(function ($crop) {
+                    return [
+                        $crop->id,
+                        $crop->user_id,
+                        $crop->land_id,
+                        $crop->seed_id,
+                        GROWTH_STAGE::getName($crop->growth_stage),
+                        $crop->plant_time
+                    ];
+                })->toArray()
+            );
+
+            if ($isDryRun) {
+                $this->warn('这是干运行模式,没有实际修复数据');
+                return 0;
+            }
+
+            // 确认是否继续
+            if (!$this->confirm('确定要修复这些作物吗?')) {
+                $this->info('操作已取消');
+                return 0;
+            }
+
+            $fixedCount = 0;
+            $errorCount = 0;
+
+            foreach ($problematicCrops as $crop) {
+                try {
+                    // 为作物确定最终产出果实ID
+                    $outputInfo = $cropLogic->getRandomOutput($crop->seed_id);
+                    $crop->final_output_item_id = $outputInfo['item_id'];
+                    $crop->save();
+
+                    $fixedCount++;
+
+                    Log::info('修复作物final_output_item_id', [
+                        'crop_id' => $crop->id,
+                        'user_id' => $crop->user_id,
+                        'seed_id' => $crop->seed_id,
+                        'growth_stage' => $crop->growth_stage,
+                        'final_output_item_id' => $crop->final_output_item_id
+                    ]);
+
+                    $this->info("修复作物 ID: {$crop->id}, 确定产出果实 ID: {$crop->final_output_item_id}");
+
+                } catch (\Exception $e) {
+                    $errorCount++;
+                    
+                    Log::error('修复作物final_output_item_id失败', [
+                        'crop_id' => $crop->id,
+                        'user_id' => $crop->user_id,
+                        'seed_id' => $crop->seed_id,
+                        'error' => $e->getMessage()
+                    ]);
+
+                    $this->error("修复作物 ID: {$crop->id} 失败: {$e->getMessage()}");
+                }
+            }
+
+            $this->info("修复完成!成功修复 {$fixedCount} 个作物,失败 {$errorCount} 个");
+
+            Log::info('作物final_output_item_id修复完成', [
+                'total' => $problematicCrops->count(),
+                'fixed' => $fixedCount,
+                'errors' => $errorCount
+            ]);
+
+            return 0;
+        } catch (\Exception $e) {
+            $this->error('修复过程中发生错误: ' . $e->getMessage());
+
+            Log::error('作物final_output_item_id修复失败', [
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return 1;
+        }
+    }
+}