| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- <?php
- namespace App\Module\Game\Logics\UserLogCollectors;
- use App\Module\GameItems\Models\ItemTransactionLog;
- use App\Module\GameItems\Services\ItemService;
- use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
- /**
- * 物品日志收集器
- *
- * 收集item_transaction_logs表的新增记录,转换为用户友好的日志消息
- */
- class ItemLogCollector extends BaseLogCollector
- {
- /**
- * 源表名
- *
- * @var string
- */
- protected string $sourceTable = 'item_transaction_logs';
- /**
- * 默认源类型
- *
- * @var string
- */
- protected string $sourceType = 'system';
- /**
- * 物品名称缓存
- *
- * @var array
- */
- private array $itemNameCache = [];
- /**
- * 预加载物品信息
- *
- * @param \Illuminate\Database\Eloquent\Collection $records 记录集合
- * @return void
- */
- private function preloadItems($records): void
- {
- $itemIds = [];
- foreach ($records as $record) {
- if ($record->item_id) {
- $itemIds[] = $record->item_id;
- }
- }
- if (!empty($itemIds)) {
- $itemIds = array_unique($itemIds);
- $items = \App\Module\GameItems\Models\Item::whereIn('id', $itemIds)->get();
- foreach ($items as $item) {
- $this->itemNameCache[$item->id] = $item->name;
- }
- // 为未找到的物品设置默认名称
- foreach ($itemIds as $itemId) {
- if (!isset($this->itemNameCache[$itemId])) {
- $this->itemNameCache[$itemId] = "物品{$itemId}";
- }
- }
- }
- }
- /**
- * 获取新的记录
- *
- * @param int $lastProcessedId 上次处理的最大ID
- * @return \Illuminate\Database\Eloquent\Collection
- */
- protected function getNewRecords(int $lastProcessedId)
- {
- $records = ItemTransactionLog::where('id', '>', $lastProcessedId)
- ->orderBy('id')
- ->limit($this->maxRecords)
- ->get();
- // 预加载物品信息
- $this->preloadItems($records);
- return $records;
- }
- /**
- * 转换记录为用户日志数据
- *
- * @param ItemTransactionLog $record 物品交易记录
- * @return array|null 用户日志数据,null表示跳过
- */
- protected function convertToUserLog($record): ?array
- {
- try {
- // 生成用户友好的消息
- $message = $this->buildUserFriendlyMessage($record);
- if (!$message) {
- return null; // 跳过无法生成消息的记录
- }
- // 根据操作类型选择合适的source_type
- dump($record);
- $sourceType = $this->getSourceTypeByOperation($record->source_type);
- return $this->createUserLogDataWithSourceType(
- $record->user_id,
- $message,
- $record->id,
- $record->created_at,
- $sourceType
- );
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error("转换物品日志失败", [
- 'record_id' => $record->id,
- 'error' => $e->getMessage()
- ]);
- return null;
- }
- }
- /**
- * 构建用户友好的消息
- *
- * @param ItemTransactionLog $record
- * @return string|null
- */
- private function buildUserFriendlyMessage(ItemTransactionLog $record): ?string
- {
- $itemName = $this->getItemName($record->item_id);
- $quantity = abs($record->quantity);
- $isGain = $record->quantity > 0;
- // 根据来源类型生成不同的消息格式
- switch ($record->source_type) {
- case 'house_upgrade':
- return $this->buildHouseUpgradeMessage($record, $itemName, $quantity, $isGain);
- case 'land_upgrade':
- return $this->buildLandUpgradeMessage($record, $itemName, $quantity, $isGain);
- case 'land_sow':
- return $this->buildLandSowMessage($record, $itemName, $quantity, $isGain);
- case 'land_remove_crop':
- return $this->buildLandRemoveCropMessage($record, $itemName, $quantity, $isGain);
- case 'shop_buy':
- return $this->buildShopBuyMessage($record, $itemName, $quantity, $isGain);
- case 'task_reward':
- return $this->buildTaskRewardMessage($record, $itemName, $quantity, $isGain);
- case 'admin_add':
- return $this->buildAdminMessage($record, $itemName, $quantity, $isGain);
- default:
- return $this->buildDefaultMessage($record, $itemName, $quantity, $isGain);
- }
- }
- /**
- * 构建房屋升级消息
- *
- * @param ItemTransactionLog $record
- * @param string $itemName
- * @param int $quantity
- * @param bool $isGain
- * @return string
- */
- private function buildHouseUpgradeMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
- {
- if ($isGain) {
- return "房屋升级获得{$itemName} {$quantity}";
- } else {
- return "房屋升级消耗{$itemName} {$quantity}";
- }
- }
- /**
- * 构建土地升级消息
- *
- * @param ItemTransactionLog $record
- * @param string $itemName
- * @param int $quantity
- * @param bool $isGain
- * @return string
- */
- private function buildLandUpgradeMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
- {
- if ($isGain) {
- return "土地升级获得{$itemName} {$quantity}";
- } else {
- return "土地升级消耗{$itemName} {$quantity}";
- }
- }
- /**
- * 构建播种消息
- *
- * @param ItemTransactionLog $record
- * @param string $itemName
- * @param int $quantity
- * @param bool $isGain
- * @return string
- */
- private function buildLandSowMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
- {
- if ($isGain) {
- return "播种获得{$itemName} {$quantity}";
- } else {
- return "播种消耗{$itemName} {$quantity}";
- }
- }
- /**
- * 构建清除作物消息
- *
- * @param ItemTransactionLog $record
- * @param string $itemName
- * @param int $quantity
- * @param bool $isGain
- * @return string
- */
- private function buildLandRemoveCropMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
- {
- if ($isGain) {
- return "清除作物获得{$itemName} {$quantity}";
- } else {
- return "清除作物消耗{$itemName} {$quantity}";
- }
- }
- /**
- * 构建商店购买消息
- *
- * @param ItemTransactionLog $record
- * @param string $itemName
- * @param int $quantity
- * @param bool $isGain
- * @return string
- */
- private function buildShopBuyMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
- {
- if ($isGain) {
- return "购买{$itemName} {$quantity}";
- } else {
- return "购买消耗{$itemName} {$quantity}";
- }
- }
- /**
- * 构建任务奖励消息
- *
- * @param ItemTransactionLog $record
- * @param string $itemName
- * @param int $quantity
- * @param bool $isGain
- * @return string
- */
- private function buildTaskRewardMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
- {
- if ($isGain) {
- return "任务奖励{$itemName} {$quantity}";
- } else {
- return "任务消耗{$itemName} {$quantity}";
- }
- }
- /**
- * 构建管理员操作消息
- *
- * @param ItemTransactionLog $record
- * @param string $itemName
- * @param int $quantity
- * @param bool $isGain
- * @return string
- */
- private function buildAdminMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
- {
- if ($isGain) {
- return "管理员发放{$itemName} {$quantity}";
- } else {
- return "管理员扣除{$itemName} {$quantity}";
- }
- }
- /**
- * 构建默认消息
- *
- * @param ItemTransactionLog $record
- * @param string $itemName
- * @param int $quantity
- * @param bool $isGain
- * @return string
- */
- private function buildDefaultMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
- {
- $action = $isGain ? '获得' : '消耗';
- if (!empty($record->source_type)) {
- $sourceDesc = $this->getSourceTypeDesc($record->source_type);
- if ($sourceDesc) {
- return "{$sourceDesc}{$action}{$itemName} {$quantity}";
- }
- }
- return "{$action}{$itemName} {$quantity}";
- }
- /**
- * 获取物品名称(带缓存)
- *
- * @param int $itemId 物品ID
- * @return string
- */
- private function getItemName(int $itemId): string
- {
- // 检查缓存
- if (isset($this->itemNameCache[$itemId])) {
- return $this->itemNameCache[$itemId];
- }
- try {
- // 直接查询数据库,避免调用可能有问题的服务
- $itemModel = \App\Module\GameItems\Models\Item::find($itemId);
- if ($itemModel && $itemModel->name) {
- $this->itemNameCache[$itemId] = $itemModel->name;
- return $itemModel->name;
- }
- $defaultName = "物品{$itemId}";
- $this->itemNameCache[$itemId] = $defaultName;
- return $defaultName;
- } catch (\Exception $e) {
- $defaultName = "物品{$itemId}";
- $this->itemNameCache[$itemId] = $defaultName;
- return $defaultName;
- }
- }
- /**
- * 获取交易类型描述
- *
- * @param int $transactionType 交易类型
- * @return string
- */
- private function getTransactionTypeDesc(int $transactionType): string
- {
- $typeMap = [
- 1 => '获取',
- 2 => '消耗',
- 3 => '交易获得',
- 4 => '交易失去',
- 5 => '过期失效',
- ];
- return $typeMap[$transactionType] ?? '';
- }
- /**
- * 获取来源类型描述
- *
- * @param string $sourceType 来源类型
- * @return string
- */
- private function getSourceTypeDesc(string $sourceType): string
- {
- $sourceMap = [
- 'task' => '任务奖励',
- 'shop' => '商店购买',
- 'chest' => '宝箱开启',
- 'craft' => '物品合成',
- 'farm' => '农场收获',
- 'pet' => '宠物获得',
- 'system' => '系统发放',
- 'admin' => '管理员操作',
- ];
- return $sourceMap[$sourceType] ?? $sourceType;
- }
- /**
- * 是否应该记录此日志
- *
- * @param ItemTransactionLog $record
- * @return bool
- */
- private function shouldLogRecord(ItemTransactionLog $record): bool
- {
- // 跳过数量为0的记录
- if ($record->quantity == 0) {
- return false;
- }
- // 可以添加更多过滤条件
- // 例如:跳过某些物品类型
- // 例如:跳过临时物品
- return true;
- }
- /**
- * 获取物品的额外信息
- *
- * @param ItemTransactionLog $record
- * @return string
- */
- private function getExtraInfo(ItemTransactionLog $record): string
- {
- $extraInfo = [];
- // 如果有实例ID,说明是有特殊属性的物品
- if ($record->instance_id) {
- $extraInfo[] = '特殊属性';
- }
- // 如果有过期时间
- if ($record->expire_at) {
- $expireTime = \Carbon\Carbon::parse($record->expire_at);
- if ($expireTime->isFuture()) {
- $extraInfo[] = '有效期至' . $expireTime->format('m-d H:i');
- }
- }
- return !empty($extraInfo) ? '(' . implode(',', $extraInfo) . ')' : '';
- }
- /**
- * 根据操作类型获取合适的source_type
- *
- * @param string $sourceType 原始来源类型
- * @return string
- */
- private function getSourceTypeByOperation(string $sourceType): string
- {
- switch ($sourceType) {
- case 'house_upgrade':
- case 'land_upgrade':
- return REWARD_SOURCE_TYPE::FARM_UPGRADE->value;
- case 'land_sow':
- return REWARD_SOURCE_TYPE::FARM_PLANT->value;
- case 'land_remove_crop':
- return REWARD_SOURCE_TYPE::LAND_REMOVE_CROP->value;
- case 'shop_buy':
- return REWARD_SOURCE_TYPE::SHOP_PURCHASE->value;
- case 'task_reward':
- return REWARD_SOURCE_TYPE::TASK->value;
- case 'admin_add':
- return REWARD_SOURCE_TYPE::ADMIN_GRANT->value;
- case 'chest':
- return REWARD_SOURCE_TYPE::CHEST->value;
- case 'craft':
- return REWARD_SOURCE_TYPE::CRAFT->value;
- case 'farm':
- return REWARD_SOURCE_TYPE::FARM_HARVEST->value;
- case 'pet':
- return REWARD_SOURCE_TYPE::SYSTEM->value;
- default:
- return REWARD_SOURCE_TYPE::SYSTEM->value;
- }
- }
- /**
- * 创建用户日志数据数组(带自定义source_type)
- *
- * @param int $userId 用户ID
- * @param string $message 日志消息
- * @param int $sourceId 来源记录ID
- * @param string|null $originalTime 原始时间(业务发生时间),null则使用当前时间
- * @param string $sourceType 来源类型
- * @return array
- */
- protected function createUserLogDataWithSourceType(
- int $userId,
- string $message,
- int $sourceId,
- ?string $originalTime = null,
- string $sourceType = null
- ): array {
- $now = now()->toDateTimeString();
- $originalTime = $originalTime ?? $now;
- $sourceType = $sourceType ?? $this->sourceType;
- return [
- 'user_id' => $userId,
- 'message' => $message,
- 'source_type' => $sourceType,
- 'source_id' => $sourceId,
- 'source_table' => $this->sourceTable,
- 'original_time' => $originalTime, // 原始业务时间
- 'collected_at' => $now, // 收集时间
- 'created_at' => $now, // 兼容字段
- ];
- }
- }
|