CirculationDto.php 4.0 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 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 float 金额(小数形式)
  29. */
  30. public float $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->fund_id; // 修正字段映射
  68. $dto->toFundId = $model->to_fund_id;
  69. // 直接使用数据库中的小数值
  70. $dto->amount = (float)$model->total_fee;
  71. $dto->relatedId = $model->re_id; // 修正字段映射
  72. $dto->relatedType = $model->re_type; // 修正字段映射
  73. $dto->remark = $model->remark;
  74. $dto->createTime = $model->create_time;
  75. // 如果提供了资金类型名称映射,则设置资金类型名称
  76. if (!empty($fundNames)) {
  77. $fromFundIdValue = $model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id; // 修正字段映射
  78. $toFundIdValue = $model->to_fund_id instanceof FUND_TYPE ? $model->to_fund_id->value : $model->to_fund_id;
  79. if (isset($fundNames[$fromFundIdValue])) {
  80. $dto->fromFundName = $fundNames[$fromFundIdValue];
  81. }
  82. if (isset($fundNames[$toFundIdValue])) {
  83. $dto->toFundName = $fundNames[$toFundIdValue];
  84. }
  85. }
  86. return $dto;
  87. }
  88. /**
  89. * 获取源资金类型ID的整数值
  90. *
  91. * @return int
  92. */
  93. public function getFromFundId(): int
  94. {
  95. if ($this->fromFundId instanceof FUND_TYPE) {
  96. return $this->fromFundId->value;
  97. }
  98. return (int)$this->fromFundId;
  99. }
  100. /**
  101. * 获取目标资金类型ID的整数值
  102. *
  103. * @return int
  104. */
  105. public function getToFundId(): int
  106. {
  107. if ($this->toFundId instanceof FUND_TYPE) {
  108. return $this->toFundId->value;
  109. }
  110. return (int)$this->toFundId;
  111. }
  112. /**
  113. * 转换为数组
  114. *
  115. * @return array
  116. */
  117. public function toArray(): array
  118. {
  119. $result = [
  120. 'id' => $this->id,
  121. 'user_id' => $this->userId,
  122. 'from_fund_id' => $this->fromFundId instanceof FUND_TYPE ? $this->fromFundId->value : $this->fromFundId,
  123. 'to_fund_id' => $this->toFundId instanceof FUND_TYPE ? $this->toFundId->value : $this->toFundId,
  124. 'amount' => $this->amount,
  125. 'related_id' => $this->relatedId,
  126. 'related_type' => $this->relatedType,
  127. 'remark' => $this->remark,
  128. 'create_time' => $this->createTime,
  129. ];
  130. if ($this->fromFundName !== null) {
  131. $result['from_fund_name'] = $this->fromFundName;
  132. }
  133. if ($this->toFundName !== null) {
  134. $result['to_fund_name'] = $this->toFundName;
  135. }
  136. return $result;
  137. }
  138. }