CurrencyDto.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Module\Fund\Dto;
  3. use App\Module\Fund\Models\FundCurrencyModel;
  4. /**
  5. * 币种数据传输对象
  6. *
  7. * 用于在服务层返回币种信息,避免直接暴露模型对象
  8. */
  9. class CurrencyDto
  10. {
  11. /**
  12. * @var int 币种ID
  13. */
  14. public int $id;
  15. /**
  16. * @var string 币种标识
  17. */
  18. public string $identification;
  19. /**
  20. * @var string 币种图标
  21. */
  22. public string $icon;
  23. /**
  24. * @var string 币种名称
  25. */
  26. public string $name;
  27. /**
  28. * @var string|null 额外数据
  29. */
  30. public ?string $data1;
  31. /**
  32. * @var int 创建时间
  33. */
  34. public int $createTime;
  35. /**
  36. * @var int 更新时间
  37. */
  38. public int $updateTime;
  39. /**
  40. * @var int 精度(小数位数)
  41. */
  42. public int $precision = 2;
  43. /**
  44. * 从模型创建DTO
  45. *
  46. * @param FundCurrencyModel $model 币种模型
  47. * @return self
  48. */
  49. public static function fromModel(FundCurrencyModel $model): self
  50. {
  51. $dto = new self();
  52. $dto->id = $model->id;
  53. $dto->identification = $model->identification;
  54. $dto->icon = $model->icon;
  55. $dto->name = $model->name;
  56. $dto->data1 = $model->data1;
  57. $dto->createTime = $model->create_time;
  58. $dto->updateTime = $model->update_time;
  59. // 从data1字段中获取精度信息
  60. if (!empty($model->data1)) {
  61. $data = json_decode($model->data1, true);
  62. if (isset($data['precision'])) {
  63. $dto->precision = (int)$data['precision'];
  64. }
  65. }
  66. return $dto;
  67. }
  68. /**
  69. * 转换为数组
  70. *
  71. * @return array
  72. */
  73. public function toArray(): array
  74. {
  75. return [
  76. 'id' => $this->id,
  77. 'identification' => $this->identification,
  78. 'icon' => $this->icon,
  79. 'name' => $this->name,
  80. 'data1' => $this->data1,
  81. 'create_time' => $this->createTime,
  82. 'update_time' => $this->updateTime,
  83. 'precision' => $this->precision,
  84. ];
  85. }
  86. /**
  87. * 格式化金额显示
  88. *
  89. * @param int $amount 金额(整数形式)
  90. * @return string 格式化后的金额
  91. */
  92. public function formatAmount(int $amount): string
  93. {
  94. $divisor = pow(10, $this->precision);
  95. // 将整数形式的金额转换为带小数的形式
  96. $formattedAmount = number_format($amount / $divisor, $this->precision, '.', ',');
  97. return $this->identification . ' ' . $formattedAmount;
  98. }
  99. /**
  100. * 将用户输入的金额转换为系统存储的整数形式
  101. *
  102. * @param float $amount 用户输入的金额
  103. * @return int 系统存储的整数形式金额
  104. */
  105. public function convertToStorageAmount(float $amount): int
  106. {
  107. $multiplier = pow(10, $this->precision);
  108. return (int)round($amount * $multiplier);
  109. }
  110. /**
  111. * 将系统存储的整数形式金额转换为实际金额
  112. *
  113. * @param int $storageAmount 系统存储的整数形式金额
  114. * @return float 实际金额
  115. */
  116. public function convertFromStorageAmount(int $storageAmount): float
  117. {
  118. $divisor = pow(10, $this->precision);
  119. return $storageAmount / $divisor;
  120. }
  121. }