| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- namespace App\Module\UrsPromotion\Commands;
- use Illuminate\Console\Command;
- use App\Module\Farm\Events\CropHarvestedEvent;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Models\FarmHarvestLog;
- use App\Module\UrsPromotion\Models\UrsUserReferral;
- use App\Module\UrsPromotion\Models\UrsUserTalent;
- use App\Module\UrsPromotion\Models\UrsTalentConfig;
- use App\Module\UrsPromotion\Models\UrsProfit;
- use App\Module\UrsPromotion\Enums\UrsProfitType;
- use Illuminate\Support\Facades\DB;
- /**
- * 测试Farm模块与UrsPromotion模块集成
- */
- class TestFarmIntegrationCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'urs:test-farm-integration {--clean : 清理测试数据}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试Farm模块与UrsPromotion模块的事件集成';
- /**
- * 执行命令
- */
- public function handle(): int
- {
- if ($this->option('clean')) {
- $this->cleanTestData();
- $this->info('测试数据清理完成');
- return 0;
- }
- $this->info('开始测试Farm模块与UrsPromotion模块集成...');
- try {
- // 1. 准备测试数据
- $this->info('1. 准备测试数据...');
- $testData = $this->prepareTestData();
- // 2. 模拟Farm收获事件
- $this->info('2. 模拟Farm收获事件...');
- $this->simulateHarvestEvent($testData);
- // 3. 验证URS收益分发
- $this->info('3. 验证URS收益分发...');
- $this->verifyProfitDistribution($testData);
- $this->info('✅ Farm模块与UrsPromotion模块集成测试通过!');
- return 0;
- } catch (\Exception $e) {
- $this->error('❌ 集成测试失败: ' . $e->getMessage());
- $this->error('错误详情: ' . $e->getTraceAsString());
- return 1;
- }
- }
- /**
- * 准备测试数据
- */
- private function prepareTestData(): array
- {
- DB::beginTransaction();
- try {
- // 创建测试用户推荐关系
- $userId = 3001; // 收获用户
- $directReferrerId = 3002; // 直推
- $indirectReferrerId = 3003; // 间推
- // 清理可能存在的测试数据
- UrsUserReferral::whereIn('user_id', [$userId, $directReferrerId])->delete();
- UrsUserTalent::whereIn('user_id', [$userId, $directReferrerId, $indirectReferrerId])->delete();
- UrsProfit::where('source_type', 'farm_harvest_test')->delete();
- // 创建推荐关系:3003 -> 3002 -> 3001
- UrsUserReferral::create([
- 'user_id' => $directReferrerId,
- 'referrer_id' => $indirectReferrerId,
- 'referral_code' => 'TEST_' . $indirectReferrerId,
- 'direct_count' => 1,
- 'indirect_count' => 0,
- 'third_count' => 0,
- ]);
- UrsUserReferral::create([
- 'user_id' => $userId,
- 'referrer_id' => $directReferrerId,
- 'referral_code' => 'TEST_' . $directReferrerId,
- 'direct_count' => 0,
- 'indirect_count' => 0,
- 'third_count' => 0,
- ]);
- // 创建达人等级
- UrsUserTalent::create([
- 'user_id' => $directReferrerId,
- 'talent_level' => 1, // 初级达人
- 'direct_count' => 1,
- 'indirect_count' => 0,
- 'third_count' => 0,
- 'total_count' => 1,
- ]);
- UrsUserTalent::create([
- 'user_id' => $indirectReferrerId,
- 'talent_level' => 2, // 中级达人
- 'direct_count' => 1,
- 'indirect_count' => 1,
- 'third_count' => 0,
- 'total_count' => 2,
- ]);
- // 创建模拟的Farm数据
- $mockLand = new FarmLand();
- $mockLand->id = 9999;
- $mockLand->user_id = $userId;
- $mockCrop = new FarmCrop();
- $mockCrop->id = 9999;
- $mockCrop->user_id = $userId;
- $mockHarvestLog = new FarmHarvestLog();
- $mockHarvestLog->id = 9999;
- $mockHarvestLog->user_id = $userId;
- $mockHarvestLog->crop_id = $mockCrop->id;
- DB::commit();
- $this->info(" - 创建推荐关系: {$indirectReferrerId} -> {$directReferrerId} -> {$userId}");
- $this->info(" - 设置达人等级: 用户{$directReferrerId}=初级达人, 用户{$indirectReferrerId}=中级达人");
- return [
- 'user_id' => $userId,
- 'direct_referrer_id' => $directReferrerId,
- 'indirect_referrer_id' => $indirectReferrerId,
- 'mock_land' => $mockLand,
- 'mock_crop' => $mockCrop,
- 'mock_harvest_log' => $mockHarvestLog,
- 'output_amount' => 1000,
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- throw $e;
- }
- }
- /**
- * 模拟Farm收获事件
- */
- private function simulateHarvestEvent(array $testData): void
- {
- $event = new CropHarvestedEvent(
- $testData['user_id'],
- $testData['mock_land'],
- $testData['mock_crop'],
- $testData['mock_harvest_log'],
- 19, // 输出物品ID(普通化肥)
- $testData['output_amount']
- );
- // 触发事件
- event($event);
- $this->info(" - 触发收获事件: 用户{$testData['user_id']}收获{$testData['output_amount']}个物品");
- }
- /**
- * 验证收益分发
- */
- private function verifyProfitDistribution(array $testData): void
- {
- // 等待事件处理完成
- sleep(1);
- // 查询生成的收益记录
- $profits = UrsProfit::where('source_type', 'farm_harvest')
- ->where('source_id', $testData['mock_harvest_log']->id)
- ->where('profit_type', UrsProfitType::PLANTING_REWARD->value)
- ->get();
- $this->info(" - 生成收益记录数量: " . $profits->count());
- if ($profits->isEmpty()) {
- throw new \Exception('未生成任何收益记录');
- }
- foreach ($profits as $profit) {
- $this->info(" - 收益记录: 用户{$profit->user_id}, 层级{$profit->relation_level}, 金额{$profit->profit_amount}, 比例{$profit->profit_rate}");
- }
- // 验证收益记录的正确性
- $directProfit = $profits->where('user_id', $testData['direct_referrer_id'])->first();
- $indirectProfit = $profits->where('user_id', $testData['indirect_referrer_id'])->first();
- if (!$directProfit) {
- throw new \Exception('直推收益记录不存在');
- }
- if (!$indirectProfit) {
- throw new \Exception('间推收益记录不存在');
- }
- $this->info(" ✅ 直推收益: {$directProfit->profit_amount} (比例: {$directProfit->profit_rate})");
- $this->info(" ✅ 间推收益: {$indirectProfit->profit_amount} (比例: {$indirectProfit->profit_rate})");
- }
- /**
- * 清理测试数据
- */
- private function cleanTestData(): void
- {
- UrsUserReferral::whereIn('user_id', [3001, 3002])->delete();
- UrsUserTalent::whereIn('user_id', [3001, 3002, 3003])->delete();
- UrsProfit::where('source_type', 'farm_harvest')->where('source_id', 9999)->delete();
- }
- }
|