|
|
@@ -2,41 +2,225 @@
|
|
|
|
|
|
namespace App\Module\Fund\Services;
|
|
|
|
|
|
+
|
|
|
+use App\Module\Fund\Models\FundConfigModel;
|
|
|
use App\Module\Fund\Models\FundCurrencyModel;
|
|
|
use App\Module\System\Services\ConfigService;
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
+use UCore\Helper\Cache as CacheHelper;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+/**
|
|
|
+ * 币种服务类
|
|
|
+ *
|
|
|
+ * 提供币种相关的服务,包括币种信息获取、币种检查等功能
|
|
|
+ */
|
|
|
class CurrencyService
|
|
|
{
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 缓存时间(秒)
|
|
|
+ */
|
|
|
+ const CACHE_TTL = 3600;
|
|
|
|
|
|
/**
|
|
|
- * @param $fund_id
|
|
|
- * @param $fund_id2
|
|
|
- * @return bool
|
|
|
+ * 检查两个账户的币种是否一致
|
|
|
+ *
|
|
|
+ * @param int $fund_id 账户种类ID1
|
|
|
+ * @param int $fund_id2 账户种类ID2
|
|
|
+ * @return bool 是否一致
|
|
|
*/
|
|
|
static public function check($fund_id, $fund_id2): bool
|
|
|
{
|
|
|
- //@todo 货币种类检查
|
|
|
- return true;
|
|
|
+ // 获取两个账户种类对应的币种
|
|
|
+ $currency1 = self::getCurrencyByFundId($fund_id);
|
|
|
+ $currency2 = self::getCurrencyByFundId($fund_id2);
|
|
|
+
|
|
|
+ // 如果任一币种信息获取失败,返回false
|
|
|
+ if (!$currency1 || !$currency2) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查两个币种ID是否一致
|
|
|
+ return $currency1->id === $currency2->id;
|
|
|
}
|
|
|
|
|
|
- public function getConfig($fund_id = null)
|
|
|
+ /**
|
|
|
+ * 获取账户种类对应的币种
|
|
|
+ *
|
|
|
+ * @param int $fund_id 账户种类ID
|
|
|
+ * @return FundCurrencyModel|null 币种信息
|
|
|
+ */
|
|
|
+ static public function getCurrencyByFundId($fund_id)
|
|
|
{
|
|
|
- //@todo 获取货币种类配置
|
|
|
+ // 获取账户种类信息
|
|
|
+ $fundConfig = FundConfigModel::find($fund_id);
|
|
|
+ if (!$fundConfig) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
+ // 从账户种类信息中获取币种ID
|
|
|
+ // 注意:这里假设FundConfigModel中有currency_id字段
|
|
|
+ // 如果实际结构不同,需要根据实际情况调整
|
|
|
+ $currencyId = $fundConfig->currency_id ?? null;
|
|
|
+ if (!$currencyId) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取币种信息
|
|
|
+ return FundCurrencyModel::find($currencyId);
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取币种配置
|
|
|
+ *
|
|
|
+ * @param int|null $currency_id 币种ID,为null时返回所有币种配置
|
|
|
+ * @return array|FundCurrencyModel 币种配置
|
|
|
+ */
|
|
|
+ public function getConfig($currency_id = null)
|
|
|
+ {
|
|
|
+ if ($currency_id) {
|
|
|
+ // 获取指定币种的配置
|
|
|
+ return Cache::remember('currency_config_' . $currency_id, self::CACHE_TTL, function () use ($currency_id) {
|
|
|
+ return FundCurrencyModel::find($currency_id);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // 获取所有币种的配置
|
|
|
+ return Cache::remember('currency_config_all', self::CACHE_TTL, function () {
|
|
|
+ return FundCurrencyModel::all();
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取币种名称列表
|
|
|
+ *
|
|
|
+ * @return array 币种ID => 币种名称
|
|
|
+ */
|
|
|
static public function getValueDesc()
|
|
|
{
|
|
|
- return \UCore\Helper\Cache::cacheCall([ __CLASS__, __FUNCTION__ ], function () {
|
|
|
- $list = FundCurrencyModel::query()->pluck('name', 'id')->toArray();;
|
|
|
+ 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 ]);
|
|
|
+ }, [], 10, [ConfigService::CACHE_TAG]);
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据币种标识获取币种信息
|
|
|
+ *
|
|
|
+ * @param string $identification 币种标识
|
|
|
+ * @return FundCurrencyModel|null 币种信息
|
|
|
+ */
|
|
|
+ static public function getCurrencyByIdentification($identification)
|
|
|
+ {
|
|
|
+ return Cache::remember('currency_by_identification_' . $identification, self::CACHE_TTL, function () use ($identification) {
|
|
|
+ return FundCurrencyModel::where('identification', $identification)->first();
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取所有币种列表
|
|
|
+ *
|
|
|
+ * @param bool $refresh 是否刷新缓存
|
|
|
+ * @return array 币种列表
|
|
|
+ */
|
|
|
+ static public function getAllCurrencies($refresh = false)
|
|
|
+ {
|
|
|
+ $cacheKey = 'all_currencies';
|
|
|
+
|
|
|
+ if ($refresh) {
|
|
|
+ Cache::forget($cacheKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Cache::remember($cacheKey, self::CACHE_TTL, function () {
|
|
|
+ return FundCurrencyModel::all()->toArray();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取币种的精度
|
|
|
+ *
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
}
|