ItemLogCollector.php 14 KB

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