AdminCreditLedgerResource.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Http\Resources\Concerns\ResolvesActor;
  4. use App\Models\CreditLedgerEntry;
  5. use Illuminate\Http\Request;
  6. /**
  7. * 积分流水的一条(管理员视角)。不给自增 id:流水由 (账号, source, ref, kind, bucket) 唯一确定。
  8. * **本类不查库**:user、operator 由 Service 预加载。
  9. *
  10. * ref 的含义随 source 变:agent_run 是提问的 run_id(可在 AI 对话统计里打开研究过程),
  11. * reading / editing 是结算的日期,admin 是调整单号。
  12. *
  13. * @mixin CreditLedgerEntry
  14. */
  15. class AdminCreditLedgerResource extends BaseResource
  16. {
  17. use ResolvesActor;
  18. /**
  19. * @param Request $request
  20. * @return array{
  21. * account_type: 'user'|'guest',
  22. * account_id: string,
  23. * user: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null,
  24. * delta: int,
  25. * kind: 'earn'|'spend'|'refund'|'adjust',
  26. * bucket: 'free'|'earned',
  27. * source: 'reading'|'editing'|'agent_run'|'admin',
  28. * ref: string,
  29. * day: string,
  30. * policy_version: int,
  31. * client_ip: string|null,
  32. * note: string|null,
  33. * operator: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null,
  34. * created_at: string,
  35. * updated_at: string
  36. * }
  37. */
  38. public function toArray($request): array
  39. {
  40. return [
  41. 'account_type' => $this->account_type,
  42. 'account_id' => $this->account_id,
  43. 'user' => $this->account_type === 'user' && $this->user ? $this->actor($this->user) : null,
  44. 'delta' => $this->delta,
  45. 'kind' => $this->kind,
  46. 'bucket' => $this->bucket,
  47. 'source' => $this->source,
  48. 'ref' => $this->ref,
  49. 'day' => $this->day->toDateString(),
  50. 'policy_version' => (int) $this->policy_version,
  51. 'client_ip' => $this->client_ip,
  52. 'note' => $this->note,
  53. 'operator' => $this->operator ? $this->actor($this->operator) : null,
  54. 'created_at' => $this->created_at->toIso8601ZuluString(),
  55. 'updated_at' => $this->updated_at->toIso8601ZuluString(),
  56. ];
  57. }
  58. }