TradeResultDto.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Module\Fund\Dto;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. /**
  5. * 贸易结果数据传输对象
  6. *
  7. * 用于在服务层返回贸易结果信息,避免直接暴露模型对象
  8. */
  9. class TradeResultDto
  10. {
  11. /**
  12. * @var int 发送用户ID
  13. */
  14. public int $fromUserId;
  15. /**
  16. * @var int 接收用户ID
  17. */
  18. public int $toUserId;
  19. /**
  20. * @var int|FUND_TYPE 资金类型ID
  21. */
  22. public $fundId;
  23. /**
  24. * @var float 金额(小数形式)
  25. */
  26. public float $amount;
  27. /**
  28. * @var string 关联类型
  29. */
  30. public string $transferType;
  31. /**
  32. * @var string 关联ID
  33. */
  34. public string $transferId;
  35. /**
  36. * @var string 备注
  37. */
  38. public string $remark;
  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 int $fromUserId 发送用户ID
  55. * @param int $toUserId 接收用户ID
  56. * @param int|FUND_TYPE $fundId 资金类型ID
  57. * @param int $amount 金额
  58. * @param string $transferType 关联类型
  59. * @param string $transferId 关联ID
  60. * @param string $remark 备注
  61. * @param array $fundNames 资金类型名称映射(可选)
  62. * @param array $userNames 用户名称映射(可选)
  63. * @return self
  64. */
  65. public static function create(
  66. int $fromUserId,
  67. int $toUserId,
  68. $fundId,
  69. int $amount,
  70. string $transferType,
  71. string $transferId,
  72. string $remark,
  73. array $fundNames = [],
  74. array $userNames = []
  75. ): self {
  76. $dto = new self();
  77. $dto->fromUserId = $fromUserId;
  78. $dto->toUserId = $toUserId;
  79. $dto->fundId = $fundId;
  80. $dto->amount = $amount;
  81. $dto->transferType = $transferType;
  82. $dto->transferId = $transferId;
  83. $dto->remark = $remark;
  84. // 如果提供了资金类型名称映射,则设置资金类型名称
  85. if (!empty($fundNames)) {
  86. $fundIdValue = $fundId instanceof FUND_TYPE ? $fundId->value : $fundId;
  87. if (isset($fundNames[$fundIdValue])) {
  88. $dto->fundName = $fundNames[$fundIdValue];
  89. }
  90. }
  91. // 如果提供了用户名称映射,则设置用户名称
  92. if (!empty($userNames)) {
  93. if (isset($userNames[$fromUserId])) {
  94. $dto->fromUserName = $userNames[$fromUserId];
  95. }
  96. if (isset($userNames[$toUserId])) {
  97. $dto->toUserName = $userNames[$toUserId];
  98. }
  99. }
  100. return $dto;
  101. }
  102. /**
  103. * 获取资金类型ID的整数值
  104. *
  105. * @return int
  106. */
  107. public function getFundId(): int
  108. {
  109. if ($this->fundId instanceof FUND_TYPE) {
  110. return $this->fundId->value;
  111. }
  112. return (int)$this->fundId;
  113. }
  114. /**
  115. * 转换为数组
  116. *
  117. * @return array
  118. */
  119. public function toArray(): array
  120. {
  121. $result = [
  122. 'from_user_id' => $this->fromUserId,
  123. 'to_user_id' => $this->toUserId,
  124. 'fund_id' => $this->fundId instanceof FUND_TYPE ? $this->fundId->value : $this->fundId,
  125. 'amount' => $this->amount,
  126. 'transfer_type' => $this->transferType,
  127. 'transfer_id' => $this->transferId,
  128. 'remark' => $this->remark,
  129. ];
  130. if ($this->fundName !== null) {
  131. $result['fund_name'] = $this->fundName;
  132. }
  133. if ($this->fromUserName !== null) {
  134. $result['from_user_name'] = $this->fromUserName;
  135. }
  136. if ($this->toUserName !== null) {
  137. $result['to_user_name'] = $this->toUserName;
  138. }
  139. return $result;
  140. }
  141. }