| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?php
- namespace App\Module\Game\Logics\UserLogCollectors;
- use App\Module\GameItems\Models\ItemTransactionLog;
- use App\Module\GameItems\Services\ItemService;
- /**
- * 物品日志收集器
- *
- * 收集item_transaction_logs表的新增记录,转换为用户友好的日志消息
- */
- class ItemLogCollector extends BaseLogCollector
- {
- /**
- * 源表名
- *
- * @var string
- */
- protected string $sourceTable = 'item_transaction_logs';
- /**
- * 源类型
- *
- * @var string
- */
- protected string $sourceType = 'item';
- /**
- * 获取新的记录
- *
- * @param int $lastProcessedId 上次处理的最大ID
- * @return \Illuminate\Database\Eloquent\Collection
- */
- protected function getNewRecords(int $lastProcessedId)
- {
- return ItemTransactionLog::where('id', '>', $lastProcessedId)
- ->orderBy('id')
- ->limit($this->maxRecords)
- ->get();
- }
- /**
- * 按时间获取新的记录
- *
- * @param int $lastProcessedTimestamp 上次处理的最大时间戳
- * @return \Illuminate\Database\Eloquent\Collection
- */
- protected function getNewRecordsByTime(int $lastProcessedTimestamp)
- {
- return ItemTransactionLog::where('created_at', '>', date('Y-m-d H:i:s', $lastProcessedTimestamp))
- ->orderBy('created_at')
- ->orderBy('id') // 相同时间时按ID排序
- ->limit($this->maxRecords)
- ->get();
- }
- /**
- * 获取记录的时间戳
- *
- * @param ItemTransactionLog $record 物品交易记录
- * @return int 时间戳
- */
- protected function getRecordTimestamp($record): int
- {
- return strtotime($record->created_at);
- }
- /**
- * 根据记录ID获取原始记录的时间戳
- *
- * @param int $recordId 记录ID
- * @return int 时间戳
- */
- protected function getOriginalRecordTimestamp(int $recordId): int
- {
- try {
- $record = ItemTransactionLog::find($recordId);
- return $record ? strtotime($record->created_at) : 0;
- } catch (\Exception $e) {
- \Illuminate\Support\Facades\Log::error("获取物品日志原始时间戳失败", [
- 'record_id' => $recordId,
- 'error' => $e->getMessage()
- ]);
- return 0;
- }
- }
- /**
- * 转换记录为用户日志数据
- *
- * @param ItemTransactionLog $record 物品交易记录
- * @return array|null 用户日志数据,null表示跳过
- */
- protected function convertToUserLog($record): ?array
- {
- try {
- // 生成用户友好的消息
- $message = $this->buildUserFriendlyMessage($record);
- if (!$message) {
- return null; // 跳过无法生成消息的记录
- }
- return $this->createUserLogData(
- $record->user_id,
- $message,
- $record->id,
- $record->created_at
- );
- } 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
- {
- try {
- // 尝试从物品服务获取物品信息
- $itemDto = ItemService::getItemInfo($itemId);
- if ($itemDto && $itemDto->name) {
- return $itemDto->name;
- }
- // 如果服务不可用,尝试直接查询数据库
- $itemModel = \App\Module\GameItems\Models\Item::find($itemId);
- if ($itemModel) {
- return $itemModel->name;
- }
- return "物品{$itemId}";
- } catch (\Exception $e) {
- return "物品{$itemId}";
- }
- }
- /**
- * 获取交易类型描述
- *
- * @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) . ')' : '';
- }
- }
|