FundLogCollector.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace App\Module\Game\Logics\UserLogCollectors;
  3. use App\Module\Fund\Models\FundLogModel;
  4. use App\Module\Fund\Services\AccountService;
  5. /**
  6. * 资金日志收集器
  7. *
  8. * 收集fund_logs表的新增记录,转换为用户友好的日志消息
  9. */
  10. class FundLogCollector extends BaseLogCollector
  11. {
  12. /**
  13. * 源表名
  14. *
  15. * @var string
  16. */
  17. protected string $sourceTable = 'fund_logs';
  18. /**
  19. * 源类型
  20. *
  21. * @var string
  22. */
  23. protected string $sourceType = 'fund';
  24. /**
  25. * 获取新的记录
  26. *
  27. * @param int $lastProcessedId 上次处理的最大ID
  28. * @return \Illuminate\Database\Eloquent\Collection
  29. */
  30. protected function getNewRecords(int $lastProcessedId)
  31. {
  32. return FundLogModel::where('id', '>', $lastProcessedId)
  33. ->orderBy('id')
  34. ->limit($this->maxRecords)
  35. ->get();
  36. }
  37. /**
  38. * 转换记录为用户日志数据
  39. *
  40. * @param FundLogModel $record 资金日志记录
  41. * @return array|null 用户日志数据,null表示跳过
  42. */
  43. protected function convertToUserLog($record): ?array
  44. {
  45. try {
  46. // 获取资金名称
  47. $fundName = $this->getFundName($record->fund_id);
  48. // 判断是获得还是消耗
  49. $amount = abs($record->amount);
  50. $action = $record->amount > 0 ? '获得' : '消耗';
  51. // 解析备注信息,生成用户友好的消息
  52. $message = $this->buildUserFriendlyMessage($record, $fundName, $action, $amount);
  53. return $this->createUserLogData(
  54. $record->user_id,
  55. $message,
  56. $record->id
  57. );
  58. } catch (\Exception $e) {
  59. \Illuminate\Support\Facades\Log::error("转换资金日志失败", [
  60. 'record_id' => $record->id,
  61. 'error' => $e->getMessage()
  62. ]);
  63. return null;
  64. }
  65. }
  66. /**
  67. * 构建用户友好的消息
  68. *
  69. * @param FundLogModel $record 资金日志记录
  70. * @param string $fundName 资金名称
  71. * @param string $action 操作类型(获得/消耗)
  72. * @param int $amount 金额
  73. * @return string
  74. */
  75. private function buildUserFriendlyMessage(FundLogModel $record, string $fundName, string $action, int $amount): string
  76. {
  77. // 解析备注信息
  78. $remarkInfo = $this->parseRemark($record->remark);
  79. // 根据来源类型生成不同的消息格式
  80. if (isset($remarkInfo['source']) && isset($remarkInfo['id'])) {
  81. $sourceMessage = $this->getSourceMessage($remarkInfo['source'], $remarkInfo['id'], $action);
  82. if ($sourceMessage) {
  83. return "{$sourceMessage}{$action}{$fundName} {$amount}";
  84. }
  85. }
  86. // 如果无法解析来源信息,使用默认格式
  87. return "{$action}{$fundName} {$amount}";
  88. }
  89. /**
  90. * 获取资金名称
  91. *
  92. * @param mixed $fundId 资金ID
  93. * @return string
  94. */
  95. private function getFundName($fundId): string
  96. {
  97. try {
  98. // 获取资金类型描述
  99. $fundNames = AccountService::getFundsDesc();
  100. // 如果是枚举对象,获取其值
  101. $fundKey = is_object($fundId) ? $fundId->value : $fundId;
  102. return $fundNames[$fundKey] ?? "资金{$fundKey}";
  103. } catch (\Exception $e) {
  104. return "未知资金";
  105. }
  106. }
  107. /**
  108. * 解析备注信息
  109. *
  110. * @param string $remark 备注内容
  111. * @return array 解析后的信息数组
  112. */
  113. private function parseRemark(string $remark): array
  114. {
  115. $info = [];
  116. // 解析格式:币种消耗:2,消耗组:16,来源:shop_buy,ID:7
  117. // 使用更宽松的正则表达式来匹配中文和英文字符
  118. if (preg_match_all('/([^:,]+):([^,]+)/', $remark, $matches)) {
  119. for ($i = 0; $i < count($matches[1]); $i++) {
  120. $key = trim($matches[1][$i]);
  121. $value = trim($matches[2][$i]);
  122. // 转换中文键名为英文
  123. switch ($key) {
  124. case '来源':
  125. $info['source'] = $value;
  126. break;
  127. case 'ID':
  128. $info['id'] = (int)$value;
  129. break;
  130. case '消耗组':
  131. $info['consume_group'] = (int)$value;
  132. break;
  133. case '币种消耗':
  134. $info['fund_type'] = (int)$value;
  135. break;
  136. default:
  137. $info[$key] = $value;
  138. }
  139. }
  140. }
  141. return $info;
  142. }
  143. /**
  144. * 根据来源信息获取操作描述
  145. *
  146. * @param string $source 来源类型
  147. * @param int $id 来源ID
  148. * @param string $action 操作类型
  149. * @return string|null 操作描述,null表示使用默认格式
  150. */
  151. private function getSourceMessage(string $source, int $id, string $action): ?string
  152. {
  153. switch ($source) {
  154. case 'shop_buy':
  155. $itemName = $this->getShopItemName($id);
  156. return "购买{$itemName}";
  157. case '开启宝箱':
  158. case 'chest_open':
  159. return "开启宝箱";
  160. case 'house_upgrade':
  161. return "房屋升级";
  162. case 'land_upgrade':
  163. return "土地升级";
  164. case 'task_reward':
  165. return "任务奖励";
  166. case 'system_gift':
  167. return "系统赠送";
  168. case 'admin_operation':
  169. return "管理员操作";
  170. case 'test_command':
  171. return "测试操作";
  172. default:
  173. return null;
  174. }
  175. }
  176. /**
  177. * 获取商店商品名称
  178. *
  179. * @param int $itemId 商品ID
  180. * @return string
  181. */
  182. private function getShopItemName(int $itemId): string
  183. {
  184. try {
  185. $shopItem = \App\Module\Shop\Models\ShopItem::find($itemId);
  186. if ($shopItem && $shopItem->name) {
  187. return $shopItem->name;
  188. }
  189. return "商品{$itemId}";
  190. } catch (\Exception $e) {
  191. return "商品{$itemId}";
  192. }
  193. }
  194. /**
  195. * 是否应该记录此日志
  196. *
  197. * @param FundLogModel $record
  198. * @return bool
  199. */
  200. private function shouldLogRecord(FundLogModel $record): bool
  201. {
  202. // 可以在这里添加过滤规则
  203. // 例如:只记录金额大于某个值的变更
  204. // 跳过金额为0的记录
  205. if ($record->amount == 0) {
  206. return false;
  207. }
  208. // 可以添加更多过滤条件
  209. // 例如:跳过某些操作类型
  210. // if (in_array($record->operate_type, [...])) {
  211. // return false;
  212. // }
  213. return true;
  214. }
  215. /**
  216. * 重写转换方法,添加过滤逻辑
  217. *
  218. * @param FundLogModel $record 资金日志记录
  219. * @return array|null 用户日志数据,null表示跳过
  220. */
  221. protected function convertToUserLogWithFilter($record): ?array
  222. {
  223. // 检查是否应该记录此日志
  224. if (!$this->shouldLogRecord($record)) {
  225. return null;
  226. }
  227. return $this->convertToUserLog($record);
  228. }
  229. }