CirculationDto.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Module\Fund\Dto;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. /**
  5. * 资金流转数据传输对象
  6. *
  7. * 用于在服务层返回资金流转信息,避免直接暴露模型对象
  8. */
  9. class CirculationDto
  10. {
  11. /**
  12. * @var int 流转ID
  13. */
  14. public int $id;
  15. /**
  16. * @var int 用户ID
  17. */
  18. public int $userId;
  19. /**
  20. * @var int|FUND_TYPE 源资金类型ID
  21. */
  22. public $fromFundId;
  23. /**
  24. * @var int|FUND_TYPE 目标资金类型ID
  25. */
  26. public $toFundId;
  27. /**
  28. * @var int 金额
  29. */
  30. public int $amount;
  31. /**
  32. * @var int 关联ID
  33. */
  34. public int $relatedId;
  35. /**
  36. * @var string 关联类型
  37. */
  38. public string $relatedType;
  39. /**
  40. * @var string 备注
  41. */
  42. public string $remark;
  43. /**
  44. * @var int 创建时间
  45. */
  46. public int $createTime;
  47. /**
  48. * @var string|null 源资金类型名称(可选)
  49. */
  50. public ?string $fromFundName = null;
  51. /**
  52. * @var string|null 目标资金类型名称(可选)
  53. */
  54. public ?string $toFundName = null;
  55. /**
  56. * 从模型创建DTO
  57. *
  58. * @param mixed $model 流转模型
  59. * @param array $fundNames 资金类型名称映射(可选)
  60. * @return self
  61. */
  62. public static function fromModel($model, array $fundNames = []): self
  63. {
  64. $dto = new self();
  65. $dto->id = $model->id;
  66. $dto->userId = $model->user_id;
  67. $dto->fromFundId = $model->from_fund_id;
  68. $dto->toFundId = $model->to_fund_id;
  69. $dto->amount = $model->amount;
  70. $dto->relatedId = $model->related_id;
  71. $dto->relatedType = $model->related_type;
  72. $dto->remark = $model->remark;
  73. $dto->createTime = $model->create_time;
  74. // 如果提供了资金类型名称映射,则设置资金类型名称
  75. if (!empty($fundNames)) {
  76. $fromFundIdValue = $model->from_fund_id instanceof FUND_TYPE ? $model->from_fund_id->value : $model->from_fund_id;
  77. $toFundIdValue = $model->to_fund_id instanceof FUND_TYPE ? $model->to_fund_id->value : $model->to_fund_id;
  78. if (isset($fundNames[$fromFundIdValue])) {
  79. $dto->fromFundName = $fundNames[$fromFundIdValue];
  80. }
  81. if (isset($fundNames[$toFundIdValue])) {
  82. $dto->toFundName = $fundNames[$toFundIdValue];
  83. }
  84. }
  85. return $dto;
  86. }
  87. /**
  88. * 获取源资金类型ID的整数值
  89. *
  90. * @return int
  91. */
  92. public function getFromFundId(): int
  93. {
  94. if ($this->fromFundId instanceof FUND_TYPE) {
  95. return $this->fromFundId->value;
  96. }
  97. return (int)$this->fromFundId;
  98. }
  99. /**
  100. * 获取目标资金类型ID的整数值
  101. *
  102. * @return int
  103. */
  104. public function getToFundId(): int
  105. {
  106. if ($this->toFundId instanceof FUND_TYPE) {
  107. return $this->toFundId->value;
  108. }
  109. return (int)$this->toFundId;
  110. }
  111. /**
  112. * 转换为数组
  113. *
  114. * @return array
  115. */
  116. public function toArray(): array
  117. {
  118. $result = [
  119. 'id' => $this->id,
  120. 'user_id' => $this->userId,
  121. 'from_fund_id' => $this->fromFundId instanceof FUND_TYPE ? $this->fromFundId->value : $this->fromFundId,
  122. 'to_fund_id' => $this->toFundId instanceof FUND_TYPE ? $this->toFundId->value : $this->toFundId,
  123. 'amount' => $this->amount,
  124. 'related_id' => $this->relatedId,
  125. 'related_type' => $this->relatedType,
  126. 'remark' => $this->remark,
  127. 'create_time' => $this->createTime,
  128. ];
  129. if ($this->fromFundName !== null) {
  130. $result['from_fund_name'] = $this->fromFundName;
  131. }
  132. if ($this->toFundName !== null) {
  133. $result['to_fund_name'] = $this->toFundName;
  134. }
  135. return $result;
  136. }
  137. }