TransferResultDto.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 float 金额(小数形式)
  29. */
  30. public float $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. // 直接使用数据库中的小数值
  67. $dto->amount = (float)$model->amount;
  68. $dto->remark = $model->remark;
  69. $dto->createTime = $model->create_time;
  70. // 如果提供了资金类型名称映射,则设置资金类型名称
  71. if (!empty($fundNames) && isset($fundNames[$model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id])) {
  72. $dto->fundName = $fundNames[$model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id];
  73. }
  74. // 如果提供了用户名称映射,则设置用户名称
  75. if (!empty($userNames)) {
  76. if (isset($userNames[$model->from_user_id])) {
  77. $dto->fromUserName = $userNames[$model->from_user_id];
  78. }
  79. if (isset($userNames[$model->to_user_id])) {
  80. $dto->toUserName = $userNames[$model->to_user_id];
  81. }
  82. }
  83. return $dto;
  84. }
  85. /**
  86. * 获取资金类型ID的整数值
  87. *
  88. * @return int
  89. */
  90. public function getFundId(): int
  91. {
  92. if ($this->fundId instanceof FUND_TYPE) {
  93. return $this->fundId->value;
  94. }
  95. return (int)$this->fundId;
  96. }
  97. /**
  98. * 转换为数组
  99. *
  100. * @return array
  101. */
  102. public function toArray(): array
  103. {
  104. $result = [
  105. 'id' => $this->id,
  106. 'from_user_id' => $this->fromUserId,
  107. 'to_user_id' => $this->toUserId,
  108. 'fund_id' => $this->fundId instanceof FUND_TYPE ? $this->fundId->value : $this->fundId,
  109. 'amount' => $this->amount,
  110. 'remark' => $this->remark,
  111. 'create_time' => $this->createTime,
  112. ];
  113. if ($this->fundName !== null) {
  114. $result['fund_name'] = $this->fundName;
  115. }
  116. if ($this->fromUserName !== null) {
  117. $result['from_user_name'] = $this->fromUserName;
  118. }
  119. if ($this->toUserName !== null) {
  120. $result['to_user_name'] = $this->toUserName;
  121. }
  122. return $result;
  123. }
  124. }