FundAdminDto.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Module\Fund\Dto;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. use App\Module\Fund\Models\FundAdminModel;
  5. /**
  6. * 管理员资金操作数据传输对象
  7. *
  8. * 用于在服务层返回管理员资金操作信息,避免直接暴露模型对象
  9. */
  10. class FundAdminDto
  11. {
  12. /**
  13. * @var int 操作ID
  14. */
  15. public int $id;
  16. /**
  17. * @var int 用户ID
  18. */
  19. public int $userId;
  20. /**
  21. * @var int|FUND_TYPE 资金类型ID
  22. */
  23. public $fundId;
  24. /**
  25. * @var float 总金额(小数形式)
  26. */
  27. public float $totalFee;
  28. /**
  29. * @var int 状态
  30. */
  31. public int $status;
  32. /**
  33. * @var int 管理员ID
  34. */
  35. public int $adminId;
  36. /**
  37. * @var string 备注
  38. */
  39. public string $remark;
  40. /**
  41. * @var int 创建时间
  42. */
  43. public int $createTime;
  44. /**
  45. * @var string|null 资金类型名称(可选)
  46. */
  47. public ?string $fundName = null;
  48. /**
  49. * 从模型创建DTO
  50. *
  51. * @param FundAdminModel $model 管理员操作模型
  52. * @param array $fundNames 资金类型名称映射(可选)
  53. * @return self
  54. */
  55. public static function fromModel(FundAdminModel $model, array $fundNames = []): self
  56. {
  57. $dto = new self();
  58. $dto->id = $model->id;
  59. $dto->userId = $model->user_id;
  60. $dto->fundId = $model->fund_id;
  61. // 直接使用数据库中的小数值
  62. $dto->totalFee = (float)$model->total_fee;
  63. $dto->status = $model->status;
  64. $dto->adminId = $model->admin_id;
  65. $dto->remark = $model->remark;
  66. $dto->createTime = $model->create_time;
  67. // 如果提供了资金类型名称映射,则设置资金类型名称
  68. if (!empty($fundNames) && isset($fundNames[$model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id])) {
  69. $dto->fundName = $fundNames[$model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id];
  70. }
  71. return $dto;
  72. }
  73. /**
  74. * 获取资金类型ID的整数值
  75. *
  76. * @return int
  77. */
  78. public function getFundId(): int
  79. {
  80. if ($this->fundId instanceof FUND_TYPE) {
  81. return $this->fundId->value;
  82. }
  83. return (int)$this->fundId;
  84. }
  85. /**
  86. * 转换为数组
  87. *
  88. * @return array
  89. */
  90. public function toArray(): array
  91. {
  92. $result = [
  93. 'id' => $this->id,
  94. 'user_id' => $this->userId,
  95. 'fund_id' => $this->fundId instanceof FUND_TYPE ? $this->fundId->value : $this->fundId,
  96. 'total_fee' => $this->totalFee,
  97. 'status' => $this->status,
  98. 'admin_id' => $this->adminId,
  99. 'remark' => $this->remark,
  100. 'create_time' => $this->createTime,
  101. ];
  102. if ($this->fundName !== null) {
  103. $result['fund_name'] = $this->fundName;
  104. }
  105. return $result;
  106. }
  107. }