ItemLogCollector.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. namespace App\Module\Game\Logics\UserLogCollectors;
  3. use App\Module\GameItems\Models\ItemTransactionLog;
  4. use App\Module\GameItems\Services\ItemService;
  5. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  6. /**
  7. * 物品日志收集器
  8. *
  9. * 收集item_transaction_logs表的新增记录,转换为用户友好的日志消息
  10. */
  11. class ItemLogCollector extends BaseLogCollector
  12. {
  13. /**
  14. * 源表名
  15. *
  16. * @var string
  17. */
  18. protected string $sourceTable = 'item_transaction_logs';
  19. /**
  20. * 默认源类型
  21. *
  22. * @var string
  23. */
  24. protected string $sourceType = 'system';
  25. /**
  26. * 物品名称缓存
  27. *
  28. * @var array
  29. */
  30. private array $itemNameCache = [];
  31. /**
  32. * 预加载物品信息
  33. *
  34. * @param \Illuminate\Database\Eloquent\Collection $records 记录集合
  35. * @return void
  36. */
  37. private function preloadItems($records): void
  38. {
  39. $itemIds = [];
  40. foreach ($records as $record) {
  41. if ($record->item_id) {
  42. $itemIds[] = $record->item_id;
  43. }
  44. }
  45. if (!empty($itemIds)) {
  46. $itemIds = array_unique($itemIds);
  47. $items = \App\Module\GameItems\Models\Item::whereIn('id', $itemIds)->get();
  48. foreach ($items as $item) {
  49. $this->itemNameCache[$item->id] = $item->name;
  50. }
  51. // 为未找到的物品设置默认名称
  52. foreach ($itemIds as $itemId) {
  53. if (!isset($this->itemNameCache[$itemId])) {
  54. $this->itemNameCache[$itemId] = "物品{$itemId}";
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * 获取新的记录
  61. *
  62. * @param int $lastProcessedId 上次处理的最大ID
  63. * @return \Illuminate\Database\Eloquent\Collection
  64. */
  65. protected function getNewRecords(int $lastProcessedId)
  66. {
  67. $records = ItemTransactionLog::where('id', '>', $lastProcessedId)
  68. ->orderBy('id')
  69. ->limit($this->maxRecords)
  70. ->get();
  71. // 预加载物品信息
  72. $this->preloadItems($records);
  73. return $records;
  74. }
  75. /**
  76. * 获取源表的最大ID
  77. *
  78. * 重写父类方法,使用模型查询以获得更好的性能
  79. *
  80. * @return int
  81. */
  82. public function getSourceTableMaxId(): int
  83. {
  84. return ItemTransactionLog::max('id') ?: 0;
  85. }
  86. /**
  87. * 转换记录为用户日志数据
  88. *
  89. * @param ItemTransactionLog $record 物品交易记录
  90. * @return array|null 用户日志数据,null表示跳过
  91. */
  92. protected function convertToUserLog($record): ?array
  93. {
  94. try {
  95. // 生成用户友好的消息
  96. $message = $this->buildUserFriendlyMessage($record);
  97. if (!$message) {
  98. return null; // 跳过无法生成消息的记录
  99. }
  100. // 根据操作类型选择合适的source_type
  101. $sourceType = $this->getSourceTypeByOperation($record->source_type);
  102. return $this->createUserLogDataWithSourceType(
  103. $record->user_id,
  104. $message,
  105. $record->id,
  106. $record->created_at,
  107. $sourceType
  108. );
  109. } catch (\Exception $e) {
  110. \Illuminate\Support\Facades\Log::error("转换物品日志失败", [
  111. 'record_id' => $record->id,
  112. 'error' => $e->getMessage()
  113. ]);
  114. return null;
  115. }
  116. }
  117. /**
  118. * 构建用户友好的消息
  119. *
  120. * @param ItemTransactionLog $record
  121. * @return string|null
  122. */
  123. private function buildUserFriendlyMessage(ItemTransactionLog $record): ?string
  124. {
  125. $itemName = $this->getItemName($record->item_id);
  126. $quantity = abs($record->quantity);
  127. $isGain = $record->quantity > 0;
  128. // 根据来源类型生成不同的消息格式
  129. switch ($record->source_type) {
  130. case 'house_upgrade':
  131. return $this->buildHouseUpgradeMessage($record, $itemName, $quantity, $isGain);
  132. case 'land_upgrade':
  133. return $this->buildLandUpgradeMessage($record, $itemName, $quantity, $isGain);
  134. case 'pet_steal':
  135. return $this->buildpet_stealMessage($record, $itemName, $quantity, $isGain);
  136. case 'land_sow':
  137. return $this->buildLandSowMessage($record, $itemName, $quantity, $isGain);
  138. case 'land_remove_crop':
  139. return $this->buildLandRemoveCropMessage($record, $itemName, $quantity, $isGain);
  140. case 'shop_buy':
  141. return $this->buildShopBuyMessage($record, $itemName, $quantity, $isGain);
  142. case 'task_reward':
  143. return $this->buildTaskRewardMessage($record, $itemName, $quantity, $isGain);
  144. case 'admin_add':
  145. return $this->buildAdminMessage($record, $itemName, $quantity, $isGain);
  146. default:
  147. return $this->buildDefaultMessage($record, $itemName, $quantity, $isGain);
  148. }
  149. }
  150. /**
  151. * 构建房屋升级消息
  152. *
  153. * @param ItemTransactionLog $record
  154. * @param string $itemName
  155. * @param int $quantity
  156. * @param bool $isGain
  157. * @return string
  158. */
  159. private function buildHouseUpgradeMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  160. {
  161. if ($isGain) {
  162. return "房屋升级获得{$itemName} {$quantity}";
  163. } else {
  164. return "房屋升级消耗{$itemName} {$quantity}";
  165. }
  166. }
  167. /**
  168. * 构建土地升级消息
  169. *
  170. * @param ItemTransactionLog $record
  171. * @param string $itemName
  172. * @param int $quantity
  173. * @param bool $isGain
  174. * @return string
  175. */
  176. private function buildLandUpgradeMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  177. {
  178. if ($isGain) {
  179. return "土地升级获得{$itemName} {$quantity}";
  180. } else {
  181. return "土地升级消耗{$itemName} {$quantity}";
  182. }
  183. }
  184. private function buildpet_stealMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  185. {
  186. if ($isGain) {
  187. return "偷菜获得{$itemName} {$quantity}";
  188. } else {
  189. return "偷菜消耗{$itemName} {$quantity}";
  190. }
  191. }
  192. /**
  193. * 构建播种消息
  194. *
  195. * @param ItemTransactionLog $record
  196. * @param string $itemName
  197. * @param int $quantity
  198. * @param bool $isGain
  199. * @return string
  200. */
  201. private function buildLandSowMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  202. {
  203. if ($isGain) {
  204. return "播种获得{$itemName} {$quantity}";
  205. } else {
  206. return "播种消耗{$itemName} {$quantity}";
  207. }
  208. }
  209. /**
  210. * 构建清除作物消息
  211. *
  212. * @param ItemTransactionLog $record
  213. * @param string $itemName
  214. * @param int $quantity
  215. * @param bool $isGain
  216. * @return string
  217. */
  218. private function buildLandRemoveCropMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  219. {
  220. if ($isGain) {
  221. return "清除作物获得{$itemName} {$quantity}";
  222. } else {
  223. return "清除作物消耗{$itemName} {$quantity}";
  224. }
  225. }
  226. /**
  227. * 构建商店购买消息
  228. *
  229. * @param ItemTransactionLog $record
  230. * @param string $itemName
  231. * @param int $quantity
  232. * @param bool $isGain
  233. * @return string
  234. */
  235. private function buildShopBuyMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  236. {
  237. if ($isGain) {
  238. return "购买{$itemName} {$quantity}";
  239. } else {
  240. return "购买消耗{$itemName} {$quantity}";
  241. }
  242. }
  243. /**
  244. * 构建任务奖励消息
  245. *
  246. * @param ItemTransactionLog $record
  247. * @param string $itemName
  248. * @param int $quantity
  249. * @param bool $isGain
  250. * @return string
  251. */
  252. private function buildTaskRewardMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  253. {
  254. if ($isGain) {
  255. return "任务奖励{$itemName} {$quantity}";
  256. } else {
  257. return "任务消耗{$itemName} {$quantity}";
  258. }
  259. }
  260. /**
  261. * 构建管理员操作消息
  262. *
  263. * @param ItemTransactionLog $record
  264. * @param string $itemName
  265. * @param int $quantity
  266. * @param bool $isGain
  267. * @return string
  268. */
  269. private function buildAdminMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  270. {
  271. if ($isGain) {
  272. return "管理员发放{$itemName} {$quantity}";
  273. } else {
  274. return "管理员扣除{$itemName} {$quantity}";
  275. }
  276. }
  277. /**
  278. * 构建默认消息
  279. *
  280. * @param ItemTransactionLog $record
  281. * @param string $itemName
  282. * @param int $quantity
  283. * @param bool $isGain
  284. * @return string
  285. */
  286. private function buildDefaultMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  287. {
  288. $action = $isGain ? '获得' : '消耗';
  289. if (!empty($record->source_type)) {
  290. $sourceDesc = $this->getSourceTypeDesc($record->source_type);
  291. if ($sourceDesc) {
  292. return "{$sourceDesc}{$action}{$itemName} {$quantity}";
  293. }
  294. }
  295. return "{$action}{$itemName} {$quantity}";
  296. }
  297. /**
  298. * 获取物品名称(带缓存)
  299. *
  300. * @param int $itemId 物品ID
  301. * @return string
  302. */
  303. private function getItemName(int $itemId): string
  304. {
  305. // 检查缓存
  306. if (isset($this->itemNameCache[$itemId])) {
  307. return $this->itemNameCache[$itemId];
  308. }
  309. try {
  310. // 直接查询数据库,避免调用可能有问题的服务
  311. $itemModel = \App\Module\GameItems\Models\Item::find($itemId);
  312. if ($itemModel && $itemModel->name) {
  313. $this->itemNameCache[$itemId] = $itemModel->name;
  314. return $itemModel->name;
  315. }
  316. $defaultName = "物品{$itemId}";
  317. $this->itemNameCache[$itemId] = $defaultName;
  318. return $defaultName;
  319. } catch (\Exception $e) {
  320. $defaultName = "物品{$itemId}";
  321. $this->itemNameCache[$itemId] = $defaultName;
  322. return $defaultName;
  323. }
  324. }
  325. /**
  326. * 获取交易类型描述
  327. *
  328. * @param int $transactionType 交易类型
  329. * @return string
  330. */
  331. private function getTransactionTypeDesc(int $transactionType): string
  332. {
  333. $typeMap = [
  334. 1 => '获取',
  335. 2 => '消耗',
  336. 3 => '交易获得',
  337. 4 => '交易失去',
  338. 5 => '过期失效',
  339. ];
  340. return $typeMap[$transactionType] ?? '';
  341. }
  342. /**
  343. * 获取来源类型描述
  344. *
  345. * @param string $sourceType 来源类型
  346. * @return string
  347. */
  348. private function getSourceTypeDesc(string $sourceType): string
  349. {
  350. $sourceMap = [
  351. 'task' => '任务奖励',
  352. 'shop' => '商店购买',
  353. 'chest' => '宝箱开启',
  354. 'craft' => '物品合成',
  355. 'farm' => '农场收获',
  356. 'pet' => '宠物获得',
  357. 'system' => '系统发放',
  358. 'admin' => '管理员操作',
  359. ];
  360. return $sourceMap[$sourceType] ?? $sourceType;
  361. }
  362. /**
  363. * 是否应该记录此日志
  364. *
  365. * @param ItemTransactionLog $record
  366. * @return bool
  367. */
  368. private function shouldLogRecord(ItemTransactionLog $record): bool
  369. {
  370. // 跳过数量为0的记录
  371. if ($record->quantity == 0) {
  372. return false;
  373. }
  374. // 可以添加更多过滤条件
  375. // 例如:跳过某些物品类型
  376. // 例如:跳过临时物品
  377. return true;
  378. }
  379. /**
  380. * 获取物品的额外信息
  381. *
  382. * @param ItemTransactionLog $record
  383. * @return string
  384. */
  385. private function getExtraInfo(ItemTransactionLog $record): string
  386. {
  387. $extraInfo = [];
  388. // 如果有实例ID,说明是有特殊属性的物品
  389. if ($record->instance_id) {
  390. $extraInfo[] = '特殊属性';
  391. }
  392. // 如果有过期时间
  393. if ($record->expire_at) {
  394. $expireTime = \Carbon\Carbon::parse($record->expire_at);
  395. if ($expireTime->isFuture()) {
  396. $extraInfo[] = '有效期至' . $expireTime->format('m-d H:i');
  397. }
  398. }
  399. return !empty($extraInfo) ? '(' . implode(',', $extraInfo) . ')' : '';
  400. }
  401. /**
  402. * 根据操作类型获取合适的source_type
  403. *
  404. * @param string|null $sourceType 原始来源类型
  405. * @return string
  406. */
  407. private function getSourceTypeByOperation(?string $sourceType): string
  408. {
  409. // 如果source_type为null,返回默认的系统类型
  410. if ($sourceType === null) {
  411. return REWARD_SOURCE_TYPE::SYSTEM->value;
  412. }
  413. switch ($sourceType) {
  414. case 'house_upgrade':
  415. case 'land_upgrade':
  416. return REWARD_SOURCE_TYPE::FARM_UPGRADE->value;
  417. case 'land_sow':
  418. return REWARD_SOURCE_TYPE::FARM_PLANT->value;
  419. case 'land_remove_crop':
  420. return REWARD_SOURCE_TYPE::LAND_REMOVE_CROP->value;
  421. case 'shop_buy':
  422. return REWARD_SOURCE_TYPE::SHOP_PURCHASE->value;
  423. case 'task_reward':
  424. return REWARD_SOURCE_TYPE::TASK->value;
  425. case 'admin_add':
  426. return REWARD_SOURCE_TYPE::ADMIN_GRANT->value;
  427. case 'chest':
  428. return REWARD_SOURCE_TYPE::CHEST->value;
  429. case 'craft':
  430. return REWARD_SOURCE_TYPE::CRAFT->value;
  431. case 'farm':
  432. return REWARD_SOURCE_TYPE::FARM_HARVEST->value;
  433. case 'pet':
  434. return REWARD_SOURCE_TYPE::SYSTEM->value;
  435. default:
  436. return REWARD_SOURCE_TYPE::SYSTEM->value;
  437. }
  438. }
  439. /**
  440. * 创建用户日志数据数组(带自定义source_type)
  441. *
  442. * @param int $userId 用户ID
  443. * @param string $message 日志消息
  444. * @param int $sourceId 来源记录ID
  445. * @param string|null $originalTime 原始时间(业务发生时间),null则使用当前时间
  446. * @param string $sourceType 来源类型
  447. * @return array
  448. */
  449. protected function createUserLogDataWithSourceType(
  450. int $userId,
  451. string $message,
  452. int $sourceId,
  453. ?string $originalTime = null,
  454. string $sourceType = null
  455. ): array {
  456. $now = now()->toDateTimeString();
  457. $originalTime = $originalTime ?? $now;
  458. $sourceType = $sourceType ?? $this->sourceType;
  459. return [
  460. 'user_id' => $userId,
  461. 'message' => $message,
  462. 'source_type' => $sourceType,
  463. 'source_id' => $sourceId,
  464. 'source_table' => $this->sourceTable,
  465. 'original_time' => $originalTime, // 原始业务时间
  466. 'collected_at' => $now, // 收集时间
  467. 'created_at' => $now, // 兼容字段
  468. ];
  469. }
  470. }