CurrencyDto.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 int 币种ID(FUND_CURRENCY_TYPE枚举值)
  17. */
  18. public int $currencyId;
  19. /**
  20. * @var string 币种标识
  21. */
  22. public string $identification;
  23. /**
  24. * @var string 币种图标
  25. */
  26. public string $icon;
  27. /**
  28. * @var string 币种名称
  29. */
  30. public string $name;
  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; // 数据库表ID
  53. $dto->currencyId = $model->type ? $model->type->value : 0; // 币种ID(枚举值)
  54. $dto->identification = $model->identification;
  55. $dto->icon = $model->icon;
  56. $dto->name = $model->name;
  57. $dto->createTime = $model->create_time;
  58. $dto->updateTime = $model->update_time;
  59. // 从枚举中获取精度信息
  60. if ($model->type) {
  61. $dto->precision = $model->type->getPrecision();
  62. } else {
  63. $dto->precision = 2; // 默认精度
  64. }
  65. return $dto;
  66. }
  67. /**
  68. * 转换为数组
  69. *
  70. * @return array
  71. */
  72. public function toArray(): array
  73. {
  74. return [
  75. 'id' => $this->id,
  76. 'currency_id' => $this->currencyId,
  77. 'identification' => $this->identification,
  78. 'icon' => $this->icon,
  79. 'name' => $this->name,
  80. 'create_time' => $this->createTime,
  81. 'update_time' => $this->updateTime,
  82. 'precision' => $this->precision,
  83. ];
  84. }
  85. /**
  86. * 格式化金额显示
  87. *
  88. * @param float $amount 金额(小数形式)
  89. * @return string 格式化后的金额
  90. */
  91. public function formatAmountName(float $amount): string
  92. {
  93. // 直接格式化小数金额
  94. $formattedAmount = number_format($amount, $this->precision, '.', ',');
  95. return $this->name . ' ' . $formattedAmount;
  96. }
  97. public function formatAmount(float $amount): string
  98. {
  99. // 直接格式化小数金额
  100. $formattedAmount = number_format($amount, $this->precision, '.', ',');
  101. return $formattedAmount;
  102. }
  103. /**
  104. * 验证金额精度是否符合币种要求
  105. *
  106. * @param float $amount 用户输入的金额
  107. * @return bool 是否符合精度要求
  108. */
  109. public function validateAmountPrecision(float $amount): bool
  110. {
  111. // 获取小数部分的位数
  112. $decimalPart = (string)$amount;
  113. if (strpos($decimalPart, '.') !== false) {
  114. $decimalDigits = strlen(substr($decimalPart, strpos($decimalPart, '.') + 1));
  115. return $decimalDigits <= $this->precision;
  116. }
  117. return true; // 整数总是符合要求
  118. }
  119. /**
  120. * 格式化金额到指定精度
  121. *
  122. * @param float $amount 金额
  123. * @return float 格式化后的金额
  124. */
  125. public function formatAmountToPrecision(float $amount): float
  126. {
  127. return round($amount, $this->precision);
  128. }
  129. }