id = $model->id; $dto->fromUserId = $model->from_user_id; $dto->toUserId = $model->to_user_id; $dto->fundId = $model->fund_id; // 直接使用数据库中的小数值 $dto->amount = (float)$model->amount; $dto->remark = $model->remark; $dto->createTime = $model->create_time; // 如果提供了资金类型名称映射,则设置资金类型名称 if (!empty($fundNames) && isset($fundNames[$model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id])) { $dto->fundName = $fundNames[$model->fund_id instanceof FUND_TYPE ? $model->fund_id->value : $model->fund_id]; } // 如果提供了用户名称映射,则设置用户名称 if (!empty($userNames)) { if (isset($userNames[$model->from_user_id])) { $dto->fromUserName = $userNames[$model->from_user_id]; } if (isset($userNames[$model->to_user_id])) { $dto->toUserName = $userNames[$model->to_user_id]; } } return $dto; } /** * 获取资金类型ID的整数值 * * @return int */ public function getFundId(): int { if ($this->fundId instanceof FUND_TYPE) { return $this->fundId->value; } return (int)$this->fundId; } /** * 转换为数组 * * @return array */ public function toArray(): array { $result = [ 'id' => $this->id, 'from_user_id' => $this->fromUserId, 'to_user_id' => $this->toUserId, 'fund_id' => $this->fundId instanceof FUND_TYPE ? $this->fundId->value : $this->fundId, 'amount' => $this->amount, 'remark' => $this->remark, 'create_time' => $this->createTime, ]; if ($this->fundName !== null) { $result['fund_name'] = $this->fundName; } if ($this->fromUserName !== null) { $result['from_user_name'] = $this->fromUserName; } if ($this->toUserName !== null) { $result['to_user_name'] = $this->toUserName; } return $result; } }