', $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) . ')' : ''; } }