ItemLogCollector.php 15 KB

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