PointLogCollector.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Module\Game\Logics\UserLogCollectors;
  3. use App\Module\Point\Models\PointLogModel;
  4. use App\Module\Point\Enums\LOG_TYPE;
  5. /**
  6. * 积分日志收集器
  7. *
  8. * 收集point_logs表的新增记录,转换为用户友好的日志消息
  9. */
  10. class PointLogCollector extends BaseLogCollector
  11. {
  12. /**
  13. * 源表名
  14. *
  15. * @var string
  16. */
  17. protected string $sourceTable = 'point_logs';
  18. /**
  19. * 源类型
  20. *
  21. * @var string
  22. */
  23. protected string $sourceType = 'point';
  24. /**
  25. * 获取新的记录
  26. *
  27. * @param int $lastProcessedId 上次处理的最大ID
  28. * @return \Illuminate\Database\Eloquent\Collection
  29. */
  30. protected function getNewRecords(int $lastProcessedId)
  31. {
  32. return PointLogModel::where('id', '>', $lastProcessedId)
  33. ->orderBy('id')
  34. ->limit($this->maxRecords)
  35. ->get();
  36. }
  37. /**
  38. * 转换记录为用户日志数据
  39. *
  40. * @param PointLogModel $record 积分日志记录
  41. * @return array|null 用户日志数据,null表示跳过
  42. */
  43. protected function convertToUserLog($record): ?array
  44. {
  45. try {
  46. // 检查是否应该记录此日志
  47. if (!$this->shouldLogRecord($record)) {
  48. return null;
  49. }
  50. // 获取积分类型名称
  51. $pointTypeName = $this->getPointTypeName($record->point_type);
  52. // 判断是获得还是消耗
  53. $amount = abs($record->amount);
  54. $action = $record->amount > 0 ? '获得' : '消耗';
  55. // 解析备注信息,生成用户友好的消息
  56. $message = $this->buildUserFriendlyMessage($record, $pointTypeName, $action, $amount);
  57. // 使用原始记录的时间
  58. $createdAt = date('Y-m-d H:i:s', $record->create_time);
  59. return $this->createUserLogData(
  60. $record->user_id,
  61. $message,
  62. $record->id,
  63. $createdAt
  64. );
  65. } catch (\Exception $e) {
  66. \Illuminate\Support\Facades\Log::error("转换积分日志失败", [
  67. 'record_id' => $record->id,
  68. 'error' => $e->getMessage()
  69. ]);
  70. return null;
  71. }
  72. }
  73. /**
  74. * 构建用户友好的消息
  75. *
  76. * @param PointLogModel $record 积分日志记录
  77. * @param string $pointTypeName 积分类型名称
  78. * @param string $action 操作类型(获得/消耗)
  79. * @param int $amount 积分数量
  80. * @return string 用户友好的消息
  81. */
  82. private function buildUserFriendlyMessage(PointLogModel $record, string $pointTypeName, string $action, int $amount): string
  83. {
  84. // 获取操作类型名称
  85. $operateTypeName = $this->getOperateTypeName($record->operate_type);
  86. // 根据操作类型生成不同的消息格式
  87. $message = match($record->operate_type) {
  88. LOG_TYPE::TASK_COMPLETE => "完成任务{$action} {$amount} {$pointTypeName}",
  89. LOG_TYPE::CHECKIN_REWARD => "签到{$action} {$amount} {$pointTypeName}",
  90. LOG_TYPE::ACTIVITY_REWARD => "活动奖励{$action} {$amount} {$pointTypeName}",
  91. LOG_TYPE::ACHIEVEMENT_REWARD => "成就奖励{$action} {$amount} {$pointTypeName}",
  92. LOG_TYPE::REFERRAL_REWARD => "推荐奖励{$action} {$amount} {$pointTypeName}",
  93. LOG_TYPE::PLANTING_REWARD => "种植作物{$action} {$amount} {$pointTypeName}",
  94. LOG_TYPE::POINT_CONSUME => "消费{$action} {$amount} {$pointTypeName}",
  95. LOG_TYPE::POINT_EXCHANGE => "兑换{$action} {$amount} {$pointTypeName}",
  96. LOG_TYPE::TRANSFER => $this->buildTransferMessage($record, $pointTypeName, $action, $amount),
  97. LOG_TYPE::CIRCULATION => "积分流转{$action} {$amount} {$pointTypeName}",
  98. LOG_TYPE::ADMIN_OPERATE => "管理员操作{$action} {$amount} {$pointTypeName}",
  99. LOG_TYPE::FREEZE => "冻结 {$amount} {$pointTypeName}",
  100. LOG_TYPE::UNFREEZE => "解冻 {$amount} {$pointTypeName}",
  101. LOG_TYPE::REFUND => "退还 {$amount} {$pointTypeName}",
  102. LOG_TYPE::DEDUCT => "扣除 {$amount} {$pointTypeName}",
  103. LOG_TYPE::SYSTEM_REWARD => "系统奖励{$action} {$amount} {$pointTypeName}",
  104. default => "{$operateTypeName}{$action} {$amount} {$pointTypeName}",
  105. };
  106. // 如果有备注且不是默认备注,添加到消息中
  107. if (!empty($record->remark) && !$this->isDefaultRemark($record->remark)) {
  108. $message .= "({$record->remark})";
  109. }
  110. return $message;
  111. }
  112. /**
  113. * 构建转账消息
  114. *
  115. * @param PointLogModel $record 积分日志记录
  116. * @param string $pointTypeName 积分类型名称
  117. * @param string $action 操作类型(未使用,保持接口一致性)
  118. * @param int $amount 积分数量
  119. * @return string 转账消息
  120. */
  121. private function buildTransferMessage(PointLogModel $record, string $pointTypeName, string $action, int $amount): string
  122. {
  123. // 根据金额正负判断是转入还是转出
  124. if ($record->amount > 0) {
  125. return "收到转账 {$amount} {$pointTypeName}";
  126. } else {
  127. return "转账给他人 {$amount} {$pointTypeName}";
  128. }
  129. }
  130. /**
  131. * 获取操作类型名称
  132. *
  133. * @param LOG_TYPE $operateType 操作类型
  134. * @return string 操作类型名称
  135. */
  136. private function getOperateTypeName(LOG_TYPE $operateType): string
  137. {
  138. try {
  139. // 使用静态映射避免调用可能有问题的方法
  140. $operateTypeNames = [
  141. 'TASK_COMPLETE' => '任务完成',
  142. 'CHECKIN_REWARD' => '签到奖励',
  143. 'ACTIVITY_REWARD' => '活动奖励',
  144. 'ACHIEVEMENT_REWARD' => '成就奖励',
  145. 'REFERRAL_REWARD' => '推荐奖励',
  146. 'PLANTING_REWARD' => '种植奖励',
  147. 'POINT_CONSUME' => '积分消费',
  148. 'POINT_EXCHANGE' => '积分兑换',
  149. 'TRANSFER' => '积分转账',
  150. 'CIRCULATION' => '积分流转',
  151. 'ADMIN_OPERATE' => '管理员操作',
  152. 'FREEZE' => '积分冻结',
  153. 'UNFREEZE' => '积分解冻',
  154. 'REFUND' => '积分退还',
  155. 'DEDUCT' => '积分扣除',
  156. 'SYSTEM_REWARD' => '系统奖励',
  157. ];
  158. $typeName = $operateType->name ?? (string)$operateType;
  159. return $operateTypeNames[$typeName] ?? "未知操作";
  160. } catch (\Exception $e) {
  161. return "未知操作";
  162. }
  163. }
  164. /**
  165. * 判断是否为默认备注
  166. *
  167. * @param string $remark 备注内容
  168. * @return bool 是否为默认备注
  169. */
  170. private function isDefaultRemark(string $remark): bool
  171. {
  172. $defaultRemarks = [
  173. '系统操作',
  174. '自动操作',
  175. '默认备注',
  176. '',
  177. ];
  178. return in_array(trim($remark), $defaultRemarks);
  179. }
  180. /**
  181. * 检查是否应该记录此日志
  182. *
  183. * @param PointLogModel $record 积分日志记录
  184. * @return bool 是否应该记录
  185. */
  186. private function shouldLogRecord(PointLogModel $record): bool
  187. {
  188. // 过滤掉测试操作
  189. if ($record->operate_type === LOG_TYPE::TEST) {
  190. return false;
  191. }
  192. // 过滤掉金额为0的记录
  193. if ($record->amount == 0) {
  194. return false;
  195. }
  196. // 过滤掉系统内部操作(可根据需要调整)
  197. $internalOperations = [
  198. // 可以在这里添加需要过滤的内部操作类型
  199. ];
  200. if (in_array($record->operate_type, $internalOperations)) {
  201. return false;
  202. }
  203. return true;
  204. }
  205. /**
  206. * 获取积分类型名称
  207. *
  208. * @param mixed $pointType 积分类型
  209. * @return string
  210. */
  211. private function getPointTypeName($pointType): string
  212. {
  213. // 使用静态映射避免调用可能有问题的方法
  214. $pointTypeNames = [
  215. 1 => '经验积分',
  216. 2 => '活动积分',
  217. 3 => '任务积分',
  218. 4 => '签到积分',
  219. // 可以根据需要添加更多
  220. ];
  221. $pointKey = is_object($pointType) ? $pointType->value : $pointType;
  222. return $pointTypeNames[$pointKey] ?? "积分{$pointKey}";
  223. }
  224. }