TransferResultDto.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Module\Fund\Dto;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. /**
  5. * 转账结果数据传输对象
  6. *
  7. * 用于在服务层返回转账结果信息,避免直接暴露模型对象
  8. */
  9. class TransferResultDto
  10. {
  11. /**
  12. * @var int 转账ID
  13. */
  14. public int $id;
  15. /**
  16. * @var int 发送用户ID
  17. */
  18. public int $fromUserId;
  19. /**
  20. * @var int 接收用户ID
  21. */
  22. public int $toUserId;
  23. /**
  24. * @var int|FUND_TYPE 资金类型ID
  25. */
  26. public $fundId;
  27. /**
  28. * @var int 金额
  29. */
  30. public int $amount;
  31. /**
  32. * @var string 备注
  33. */
  34. public string $remark;
  35. /**
  36. * @var int 创建时间
  37. */
  38. public int $createTime;
  39. /**
  40. * @var string|null 资金类型名称(可选)
  41. */
  42. public ?string $fundName = null;
  43. /**
  44. * @var string|null 发送用户名称(可选)
  45. */
  46. public ?string $fromUserName = null;
  47. /**
  48. * @var string|null 接收用户名称(可选)
  49. */
  50. public ?string $toUserName = null;
  51. /**
  52. * 从模型创建DTO
  53. *
  54. * @param mixed $model 转账模型
  55. * @param array $fundNames 资金类型名称映射(可选)
  56. * @param array $userNames 用户名称映射(可选)
  57. * @return self
  58. */
  59. public static function fromModel($model, array $fundNames = [], array $userNames = []): self
  60. {
  61. $dto = new self();
  62. $dto->id = $model->id;
  63. $dto->fromUserId = $model->from_user_id;
  64. $dto->toUserId = $model->to_user_id;
  65. $dto->fundId = $model->fund_id;
  66. $dto->amount = $model->amount;
  67. $dto->remark = $model->remark;
  68. $dto->createTime = $model->create_time;
  69. // 如果提供了资金类型名称映射,则设置资金类型名称
  70. if (!empty($fundNames) && isset($fundNames[$model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id])) {
  71. $dto->fundName = $fundNames[$model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id];
  72. }
  73. // 如果提供了用户名称映射,则设置用户名称
  74. if (!empty($userNames)) {
  75. if (isset($userNames[$model->from_user_id])) {
  76. $dto->fromUserName = $userNames[$model->from_user_id];
  77. }
  78. if (isset($userNames[$model->to_user_id])) {
  79. $dto->toUserName = $userNames[$model->to_user_id];
  80. }
  81. }
  82. return $dto;
  83. }
  84. /**
  85. * 获取资金类型ID的整数值
  86. *
  87. * @return int
  88. */
  89. public function getFundId(): int
  90. {
  91. if ($this->fundId instanceof FUND_TYPE) {
  92. return $this->fundId->value;
  93. }
  94. return (int)$this->fundId;
  95. }
  96. /**
  97. * 转换为数组
  98. *
  99. * @return array
  100. */
  101. public function toArray(): array
  102. {
  103. $result = [
  104. 'id' => $this->id,
  105. 'from_user_id' => $this->fromUserId,
  106. 'to_user_id' => $this->toUserId,
  107. 'fund_id' => $this->fundId instanceof FUND_TYPE ? $this->fundId->value : $this->fundId,
  108. 'amount' => $this->amount,
  109. 'remark' => $this->remark,
  110. 'create_time' => $this->createTime,
  111. ];
  112. if ($this->fundName !== null) {
  113. $result['fund_name'] = $this->fundName;
  114. }
  115. if ($this->fromUserName !== null) {
  116. $result['from_user_name'] = $this->fromUserName;
  117. }
  118. if ($this->toUserName !== null) {
  119. $result['to_user_name'] = $this->toUserName;
  120. }
  121. return $result;
  122. }
  123. }