FundChangeTempDto.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. use UCore\Dto\BaseDto;
  5. /**
  6. * 资金变更临时数据传输对象
  7. *
  8. * 用于存储和传输资金变更的临时数据
  9. */
  10. class FundChangeTempDto extends BaseDto
  11. {
  12. /**
  13. * 资金类型ID
  14. *
  15. * @var int
  16. */
  17. public int $fundId;
  18. /**
  19. * 旧余额
  20. *
  21. * @var int
  22. */
  23. public int $oldBalance;
  24. /**
  25. * 新余额
  26. *
  27. * @var int
  28. */
  29. public int $newBalance;
  30. /**
  31. * 变化量
  32. *
  33. * @var int
  34. */
  35. public int $changeAmount;
  36. /**
  37. * 更新时间戳
  38. *
  39. * @var int
  40. */
  41. public int $updatedAt;
  42. /**
  43. * 从数组创建DTO
  44. *
  45. * @param array $data 数据数组
  46. * @return self
  47. */
  48. public static function fromArray(array $data): static
  49. {
  50. $dto = new self();
  51. $dto->fundId = $data['fund_id'] ?? 0;
  52. $dto->oldBalance = $data['old_balance'] ?? 0;
  53. $dto->newBalance = $data['new_balance'] ?? 0;
  54. $dto->changeAmount = $data['change_amount'] ?? 0;
  55. $dto->updatedAt = $data['updated_at'] ?? time();
  56. return $dto;
  57. }
  58. /**
  59. * 转换为数组
  60. *
  61. * @return array
  62. */
  63. public function toArray(): array
  64. {
  65. return [
  66. 'fund_id' => $this->fundId instanceof FUND_TYPE ? $this->fundId->value : $this->fundId,
  67. 'old_balance' => $this->oldBalance,
  68. 'new_balance' => $this->newBalance,
  69. 'change_amount' => $this->changeAmount,
  70. 'updated_at' => $this->updatedAt,
  71. ];
  72. }
  73. }