id === $currency2->id; } /** * 获取账户种类对应的币种 * * @param int $fund_id 账户种类ID * @return CurrencyDto|null 币种信息DTO */ static public function getCurrencyByFundId($fund_id) { // 获取账户种类信息 $fundConfig = FundConfigModel::find($fund_id); if (!$fundConfig) { return null; } // 从账户种类信息中获取币种ID $currencyId = $fundConfig->currency_id ?? null; if (!$currencyId) { return null; } // 获取币种信息 $currency = FundCurrencyModel::find($currencyId); if (!$currency) { return null; } // 转换为DTO return CurrencyDto::fromModel($currency); } /** * 获取币种配置 * * @param int|null $currency_id 币种ID,为null时返回所有币种配置 * @return CurrencyDto|CurrencyDto[] 币种配置DTO或DTO数组 */ public function getConfig($currency_id = null) { if ($currency_id) { // 获取指定币种的配置 return Cache::remember('currency_config_dto_' . $currency_id, self::CACHE_TTL, function () use ($currency_id) { $currency = FundCurrencyModel::find($currency_id); if (!$currency) { return null; } return CurrencyDto::fromModel($currency); }); } else { // 获取所有币种的配置 return Cache::remember('currency_config_dto_all', self::CACHE_TTL, function () { $currencies = FundCurrencyModel::all(); $dtoList = []; foreach ($currencies as $currency) { $dtoList[] = CurrencyDto::fromModel($currency); } return $dtoList; }); } } /** * 获取币种名称列表 * * @return array 币种ID => 币种名称 */ static public function getValueDesc() { return CacheHelper::cacheCall([__CLASS__, __FUNCTION__], function () { $list = FundCurrencyModel::query()->pluck('name', 'id')->toArray(); return $list; }, [], 10, [ConfigService::CACHE_TAG]); } /** * 获取币种标识列表 * * @return array 币种ID => 币种标识 */ static public function getIdentifications() { return CacheHelper::cacheCall([__CLASS__, __FUNCTION__], function () { $list = FundCurrencyModel::query()->pluck('identification', 'id')->toArray(); return $list; }, [], 10, [ConfigService::CACHE_TAG]); } /** * 根据币种标识获取币种信息 * * @param string $identification 币种标识 * @return CurrencyDto|null 币种信息DTO */ static public function getCurrencyByIdentification($identification) { return Cache::remember('currency_dto_by_identification_' . $identification, self::CACHE_TTL, function () use ($identification) { $currency = FundCurrencyModel::where('identification', $identification)->first(); if (!$currency) { return null; } return CurrencyDto::fromModel($currency); }); } /** * 获取所有币种列表 * * @param bool $refresh 是否刷新缓存 * @return CurrencyDto[] 币种DTO列表 */ static public function getAllCurrencies($refresh = false) { $cacheKey = 'all_currencies_dto'; if ($refresh) { Cache::forget($cacheKey); } return Cache::remember($cacheKey, self::CACHE_TTL, function () { $currencies = FundCurrencyModel::all(); $dtoList = []; foreach ($currencies as $currency) { $dtoList[] = CurrencyDto::fromModel($currency); } return $dtoList; }); } /** * 获取币种的精度 * * @param int $currency_id 币种ID * @return int 精度(小数位数) */ static public function getCurrencyPrecision($currency_id) { $currency = FundCurrencyModel::find($currency_id); if (!$currency) { return 2; // 默认精度为2 } // 从data1字段中获取精度信息 if (!empty($currency->data1)) { $data = json_decode($currency->data1, true); if (isset($data['precision'])) { return (int)$data['precision']; } } return 2; // 默认精度为2 } /** * 格式化金额显示 * * @param int $amount 金额(整数形式) * @param int $currency_id 币种ID * @return string 格式化后的金额 */ static public function formatAmount($amount, $currency_id) { $precision = self::getCurrencyPrecision($currency_id); $divisor = pow(10, $precision); // 将整数形式的金额转换为带小数的形式 $formattedAmount = number_format($amount / $divisor, $precision, '.', ','); // 获取币种标识 $currency = FundCurrencyModel::find($currency_id); $symbol = $currency ? $currency->identification : ''; return $symbol . ' ' . $formattedAmount; } /** * 将用户输入的金额转换为系统存储的整数形式 * * @param float $amount 用户输入的金额 * @param int $currency_id 币种ID * @return int 系统存储的整数形式金额 */ static public function convertToStorageAmount($amount, $currency_id) { $precision = self::getCurrencyPrecision($currency_id); $multiplier = pow(10, $precision); return (int)round($amount * $multiplier); } /** * 将系统存储的整数形式金额转换为实际金额 * * @param int $storageAmount 系统存储的整数形式金额 * @param int $currency_id 币种ID * @return float 实际金额 */ static public function convertFromStorageAmount($storageAmount, $currency_id) { $precision = self::getCurrencyPrecision($currency_id); $divisor = pow(10, $precision); return $storageAmount / $divisor; } }