FundLogDto.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Module\Fund\Dto;
  3. use App\Module\Fund\Enums\LOG_TYPE;
  4. /**
  5. * 资金日志数据传输对象
  6. *
  7. * 用于在服务层返回资金日志信息,避免直接暴露模型对象
  8. */
  9. class FundLogDto
  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 资金类型ID
  21. */
  22. public $fundId;
  23. /**
  24. * @var int 金额
  25. */
  26. public int $amount;
  27. /**
  28. * @var int 操作ID
  29. */
  30. public int $operateId;
  31. /**
  32. * @var int|LOG_TYPE 操作类型
  33. */
  34. public $operateType;
  35. /**
  36. * @var string 备注
  37. */
  38. public string $remark;
  39. /**
  40. * @var int 创建时间
  41. */
  42. public int $createTime;
  43. /**
  44. * @var string 创建IP
  45. */
  46. public string $createIp;
  47. /**
  48. * @var int 操作后余额
  49. */
  50. public int $laterBalance;
  51. /**
  52. * @var int 操作前余额
  53. */
  54. public int $beforeBalance;
  55. /**
  56. * @var string 日期键
  57. */
  58. public string $dateKey;
  59. /**
  60. * @var string 哈希值
  61. */
  62. public string $hash;
  63. /**
  64. * @var string 前一条记录的哈希值
  65. */
  66. public string $prevHash;
  67. /**
  68. * @var string|null 资金类型名称(可选)
  69. */
  70. public ?string $fundName = null;
  71. /**
  72. * @var string|null 操作类型名称(可选)
  73. */
  74. public ?string $operateTypeName = null;
  75. /**
  76. * 从模型创建DTO
  77. *
  78. * @param mixed $model 日志模型
  79. * @param array $fundNames 资金类型名称映射(可选)
  80. * @param array $operateTypeNames 操作类型名称映射(可选)
  81. * @return self
  82. */
  83. public static function fromModel($model, array $fundNames = [], array $operateTypeNames = []): self
  84. {
  85. $dto = new self();
  86. $dto->id = $model->id;
  87. $dto->userId = $model->user_id;
  88. $dto->fundId = $model->fund_id;
  89. $dto->amount = $model->amount;
  90. $dto->operateId = $model->operate_id;
  91. $dto->operateType = $model->operate_type;
  92. $dto->remark = $model->remark;
  93. $dto->createTime = $model->create_time;
  94. $dto->createIp = $model->create_ip;
  95. $dto->laterBalance = $model->later_balance;
  96. $dto->beforeBalance = $model->before_balance;
  97. $dto->dateKey = $model->date_key;
  98. $dto->hash = $model->hash;
  99. $dto->prevHash = $model->prev_hash;
  100. // 如果提供了资金类型名称映射,则设置资金类型名称
  101. if (!empty($fundNames) && isset($fundNames[$model->fund_id])) {
  102. $dto->fundName = $fundNames[$model->fund_id];
  103. }
  104. // 如果提供了操作类型名称映射,则设置操作类型名称
  105. if (!empty($operateTypeNames) && isset($operateTypeNames[$model->operate_type instanceof LOG_TYPE ? $model->operate_type->value : $model->operate_type])) {
  106. $dto->operateTypeName = $operateTypeNames[$model->operate_type instanceof LOG_TYPE ? $model->operate_type->value : $model->operate_type];
  107. }
  108. return $dto;
  109. }
  110. /**
  111. * 转换为数组
  112. *
  113. * @return array
  114. */
  115. public function toArray(): array
  116. {
  117. $result = [
  118. 'id' => $this->id,
  119. 'user_id' => $this->userId,
  120. 'fund_id' => $this->fundId,
  121. 'amount' => $this->amount,
  122. 'operate_id' => $this->operateId,
  123. 'operate_type' => $this->operateType instanceof LOG_TYPE ? $this->operateType->value : $this->operateType,
  124. 'remark' => $this->remark,
  125. 'create_time' => $this->createTime,
  126. 'create_ip' => $this->createIp,
  127. 'later_balance' => $this->laterBalance,
  128. 'before_balance' => $this->beforeBalance,
  129. 'date_key' => $this->dateKey,
  130. 'hash' => $this->hash,
  131. 'prev_hash' => $this->prevHash,
  132. ];
  133. if ($this->fundName !== null) {
  134. $result['fund_name'] = $this->fundName;
  135. }
  136. if ($this->operateTypeName !== null) {
  137. $result['operate_type_name'] = $this->operateTypeName;
  138. }
  139. return $result;
  140. }
  141. }