ItemLogCollector.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 'land_sow':
  135. return $this->buildLandSowMessage($record, $itemName, $quantity, $isGain);
  136. case 'land_remove_crop':
  137. return $this->buildLandRemoveCropMessage($record, $itemName, $quantity, $isGain);
  138. case 'shop_buy':
  139. return $this->buildShopBuyMessage($record, $itemName, $quantity, $isGain);
  140. case 'task_reward':
  141. return $this->buildTaskRewardMessage($record, $itemName, $quantity, $isGain);
  142. case 'admin_add':
  143. return $this->buildAdminMessage($record, $itemName, $quantity, $isGain);
  144. default:
  145. return $this->buildDefaultMessage($record, $itemName, $quantity, $isGain);
  146. }
  147. }
  148. /**
  149. * 构建房屋升级消息
  150. *
  151. * @param ItemTransactionLog $record
  152. * @param string $itemName
  153. * @param int $quantity
  154. * @param bool $isGain
  155. * @return string
  156. */
  157. private function buildHouseUpgradeMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  158. {
  159. if ($isGain) {
  160. return "房屋升级获得{$itemName} {$quantity}";
  161. } else {
  162. return "房屋升级消耗{$itemName} {$quantity}";
  163. }
  164. }
  165. /**
  166. * 构建土地升级消息
  167. *
  168. * @param ItemTransactionLog $record
  169. * @param string $itemName
  170. * @param int $quantity
  171. * @param bool $isGain
  172. * @return string
  173. */
  174. private function buildLandUpgradeMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  175. {
  176. if ($isGain) {
  177. return "土地升级获得{$itemName} {$quantity}";
  178. } else {
  179. return "土地升级消耗{$itemName} {$quantity}";
  180. }
  181. }
  182. /**
  183. * 构建播种消息
  184. *
  185. * @param ItemTransactionLog $record
  186. * @param string $itemName
  187. * @param int $quantity
  188. * @param bool $isGain
  189. * @return string
  190. */
  191. private function buildLandSowMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  192. {
  193. if ($isGain) {
  194. return "播种获得{$itemName} {$quantity}";
  195. } else {
  196. return "播种消耗{$itemName} {$quantity}";
  197. }
  198. }
  199. /**
  200. * 构建清除作物消息
  201. *
  202. * @param ItemTransactionLog $record
  203. * @param string $itemName
  204. * @param int $quantity
  205. * @param bool $isGain
  206. * @return string
  207. */
  208. private function buildLandRemoveCropMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  209. {
  210. if ($isGain) {
  211. return "清除作物获得{$itemName} {$quantity}";
  212. } else {
  213. return "清除作物消耗{$itemName} {$quantity}";
  214. }
  215. }
  216. /**
  217. * 构建商店购买消息
  218. *
  219. * @param ItemTransactionLog $record
  220. * @param string $itemName
  221. * @param int $quantity
  222. * @param bool $isGain
  223. * @return string
  224. */
  225. private function buildShopBuyMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  226. {
  227. if ($isGain) {
  228. return "购买{$itemName} {$quantity}";
  229. } else {
  230. return "购买消耗{$itemName} {$quantity}";
  231. }
  232. }
  233. /**
  234. * 构建任务奖励消息
  235. *
  236. * @param ItemTransactionLog $record
  237. * @param string $itemName
  238. * @param int $quantity
  239. * @param bool $isGain
  240. * @return string
  241. */
  242. private function buildTaskRewardMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  243. {
  244. if ($isGain) {
  245. return "任务奖励{$itemName} {$quantity}";
  246. } else {
  247. return "任务消耗{$itemName} {$quantity}";
  248. }
  249. }
  250. /**
  251. * 构建管理员操作消息
  252. *
  253. * @param ItemTransactionLog $record
  254. * @param string $itemName
  255. * @param int $quantity
  256. * @param bool $isGain
  257. * @return string
  258. */
  259. private function buildAdminMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  260. {
  261. if ($isGain) {
  262. return "管理员发放{$itemName} {$quantity}";
  263. } else {
  264. return "管理员扣除{$itemName} {$quantity}";
  265. }
  266. }
  267. /**
  268. * 构建默认消息
  269. *
  270. * @param ItemTransactionLog $record
  271. * @param string $itemName
  272. * @param int $quantity
  273. * @param bool $isGain
  274. * @return string
  275. */
  276. private function buildDefaultMessage(ItemTransactionLog $record, string $itemName, int $quantity, bool $isGain): string
  277. {
  278. $action = $isGain ? '获得' : '消耗';
  279. if (!empty($record->source_type)) {
  280. $sourceDesc = $this->getSourceTypeDesc($record->source_type);
  281. if ($sourceDesc) {
  282. return "{$sourceDesc}{$action}{$itemName} {$quantity}";
  283. }
  284. }
  285. return "{$action}{$itemName} {$quantity}";
  286. }
  287. /**
  288. * 获取物品名称(带缓存)
  289. *
  290. * @param int $itemId 物品ID
  291. * @return string
  292. */
  293. private function getItemName(int $itemId): string
  294. {
  295. // 检查缓存
  296. if (isset($this->itemNameCache[$itemId])) {
  297. return $this->itemNameCache[$itemId];
  298. }
  299. try {
  300. // 直接查询数据库,避免调用可能有问题的服务
  301. $itemModel = \App\Module\GameItems\Models\Item::find($itemId);
  302. if ($itemModel && $itemModel->name) {
  303. $this->itemNameCache[$itemId] = $itemModel->name;
  304. return $itemModel->name;
  305. }
  306. $defaultName = "物品{$itemId}";
  307. $this->itemNameCache[$itemId] = $defaultName;
  308. return $defaultName;
  309. } catch (\Exception $e) {
  310. $defaultName = "物品{$itemId}";
  311. $this->itemNameCache[$itemId] = $defaultName;
  312. return $defaultName;
  313. }
  314. }
  315. /**
  316. * 获取交易类型描述
  317. *
  318. * @param int $transactionType 交易类型
  319. * @return string
  320. */
  321. private function getTransactionTypeDesc(int $transactionType): string
  322. {
  323. $typeMap = [
  324. 1 => '获取',
  325. 2 => '消耗',
  326. 3 => '交易获得',
  327. 4 => '交易失去',
  328. 5 => '过期失效',
  329. ];
  330. return $typeMap[$transactionType] ?? '';
  331. }
  332. /**
  333. * 获取来源类型描述
  334. *
  335. * @param string $sourceType 来源类型
  336. * @return string
  337. */
  338. private function getSourceTypeDesc(string $sourceType): string
  339. {
  340. $sourceMap = [
  341. 'task' => '任务奖励',
  342. 'shop' => '商店购买',
  343. 'chest' => '宝箱开启',
  344. 'craft' => '物品合成',
  345. 'farm' => '农场收获',
  346. 'pet' => '宠物获得',
  347. 'system' => '系统发放',
  348. 'admin' => '管理员操作',
  349. ];
  350. return $sourceMap[$sourceType] ?? $sourceType;
  351. }
  352. /**
  353. * 是否应该记录此日志
  354. *
  355. * @param ItemTransactionLog $record
  356. * @return bool
  357. */
  358. private function shouldLogRecord(ItemTransactionLog $record): bool
  359. {
  360. // 跳过数量为0的记录
  361. if ($record->quantity == 0) {
  362. return false;
  363. }
  364. // 可以添加更多过滤条件
  365. // 例如:跳过某些物品类型
  366. // 例如:跳过临时物品
  367. return true;
  368. }
  369. /**
  370. * 获取物品的额外信息
  371. *
  372. * @param ItemTransactionLog $record
  373. * @return string
  374. */
  375. private function getExtraInfo(ItemTransactionLog $record): string
  376. {
  377. $extraInfo = [];
  378. // 如果有实例ID,说明是有特殊属性的物品
  379. if ($record->instance_id) {
  380. $extraInfo[] = '特殊属性';
  381. }
  382. // 如果有过期时间
  383. if ($record->expire_at) {
  384. $expireTime = \Carbon\Carbon::parse($record->expire_at);
  385. if ($expireTime->isFuture()) {
  386. $extraInfo[] = '有效期至' . $expireTime->format('m-d H:i');
  387. }
  388. }
  389. return !empty($extraInfo) ? '(' . implode(',', $extraInfo) . ')' : '';
  390. }
  391. /**
  392. * 根据操作类型获取合适的source_type
  393. *
  394. * @param string|null $sourceType 原始来源类型
  395. * @return string
  396. */
  397. private function getSourceTypeByOperation(?string $sourceType): string
  398. {
  399. // 如果source_type为null,返回默认的系统类型
  400. if ($sourceType === null) {
  401. return REWARD_SOURCE_TYPE::SYSTEM->value;
  402. }
  403. switch ($sourceType) {
  404. case 'house_upgrade':
  405. case 'land_upgrade':
  406. return REWARD_SOURCE_TYPE::FARM_UPGRADE->value;
  407. case 'land_sow':
  408. return REWARD_SOURCE_TYPE::FARM_PLANT->value;
  409. case 'land_remove_crop':
  410. return REWARD_SOURCE_TYPE::LAND_REMOVE_CROP->value;
  411. case 'shop_buy':
  412. return REWARD_SOURCE_TYPE::SHOP_PURCHASE->value;
  413. case 'task_reward':
  414. return REWARD_SOURCE_TYPE::TASK->value;
  415. case 'admin_add':
  416. return REWARD_SOURCE_TYPE::ADMIN_GRANT->value;
  417. case 'chest':
  418. return REWARD_SOURCE_TYPE::CHEST->value;
  419. case 'craft':
  420. return REWARD_SOURCE_TYPE::CRAFT->value;
  421. case 'farm':
  422. return REWARD_SOURCE_TYPE::FARM_HARVEST->value;
  423. case 'pet':
  424. return REWARD_SOURCE_TYPE::SYSTEM->value;
  425. default:
  426. return REWARD_SOURCE_TYPE::SYSTEM->value;
  427. }
  428. }
  429. /**
  430. * 创建用户日志数据数组(带自定义source_type)
  431. *
  432. * @param int $userId 用户ID
  433. * @param string $message 日志消息
  434. * @param int $sourceId 来源记录ID
  435. * @param string|null $originalTime 原始时间(业务发生时间),null则使用当前时间
  436. * @param string $sourceType 来源类型
  437. * @return array
  438. */
  439. protected function createUserLogDataWithSourceType(
  440. int $userId,
  441. string $message,
  442. int $sourceId,
  443. ?string $originalTime = null,
  444. string $sourceType = null
  445. ): array {
  446. $now = now()->toDateTimeString();
  447. $originalTime = $originalTime ?? $now;
  448. $sourceType = $sourceType ?? $this->sourceType;
  449. return [
  450. 'user_id' => $userId,
  451. 'message' => $message,
  452. 'source_type' => $sourceType,
  453. 'source_id' => $sourceId,
  454. 'source_table' => $this->sourceTable,
  455. 'original_time' => $originalTime, // 原始业务时间
  456. 'collected_at' => $now, // 收集时间
  457. 'created_at' => $now, // 兼容字段
  458. ];
  459. }
  460. }