CurrencyService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Module\Fund\Services;
  3. use App\Module\Fund\Models\FundConfigModel;
  4. use App\Module\Fund\Models\FundCurrencyModel;
  5. use App\Module\System\Services\ConfigService;
  6. use Illuminate\Support\Facades\Cache;
  7. use UCore\Helper\Cache as CacheHelper;
  8. /**
  9. * 币种服务类
  10. *
  11. * 提供币种相关的服务,包括币种信息获取、币种检查等功能
  12. */
  13. class CurrencyService
  14. {
  15. /**
  16. * 缓存时间(秒)
  17. */
  18. const CACHE_TTL = 3600;
  19. /**
  20. * 检查两个账户的币种是否一致
  21. *
  22. * @param int $fund_id 账户种类ID1
  23. * @param int $fund_id2 账户种类ID2
  24. * @return bool 是否一致
  25. */
  26. static public function check($fund_id, $fund_id2): bool
  27. {
  28. // 获取两个账户种类对应的币种
  29. $currency1 = self::getCurrencyByFundId($fund_id);
  30. $currency2 = self::getCurrencyByFundId($fund_id2);
  31. // 如果任一币种信息获取失败,返回false
  32. if (!$currency1 || !$currency2) {
  33. return false;
  34. }
  35. // 检查两个币种ID是否一致
  36. return $currency1->id === $currency2->id;
  37. }
  38. /**
  39. * 获取账户种类对应的币种
  40. *
  41. * @param int $fund_id 账户种类ID
  42. * @return FundCurrencyModel|null 币种信息
  43. */
  44. static public function getCurrencyByFundId($fund_id)
  45. {
  46. // 获取账户种类信息
  47. $fundConfig = FundConfigModel::find($fund_id);
  48. if (!$fundConfig) {
  49. return null;
  50. }
  51. // 从账户种类信息中获取币种ID
  52. // 注意:这里假设FundConfigModel中有currency_id字段
  53. // 如果实际结构不同,需要根据实际情况调整
  54. $currencyId = $fundConfig->currency_id ?? null;
  55. if (!$currencyId) {
  56. return null;
  57. }
  58. // 获取币种信息
  59. return FundCurrencyModel::find($currencyId);
  60. }
  61. /**
  62. * 获取币种配置
  63. *
  64. * @param int|null $currency_id 币种ID,为null时返回所有币种配置
  65. * @return array|FundCurrencyModel 币种配置
  66. */
  67. public function getConfig($currency_id = null)
  68. {
  69. if ($currency_id) {
  70. // 获取指定币种的配置
  71. return Cache::remember('currency_config_' . $currency_id, self::CACHE_TTL, function () use ($currency_id) {
  72. return FundCurrencyModel::find($currency_id);
  73. });
  74. } else {
  75. // 获取所有币种的配置
  76. return Cache::remember('currency_config_all', self::CACHE_TTL, function () {
  77. return FundCurrencyModel::all();
  78. });
  79. }
  80. }
  81. /**
  82. * 获取币种名称列表
  83. *
  84. * @return array 币种ID => 币种名称
  85. */
  86. static public function getValueDesc()
  87. {
  88. return CacheHelper::cacheCall([__CLASS__, __FUNCTION__], function () {
  89. $list = FundCurrencyModel::query()->pluck('name', 'id')->toArray();
  90. return $list;
  91. }, [], 10, [ConfigService::CACHE_TAG]);
  92. }
  93. /**
  94. * 获取币种标识列表
  95. *
  96. * @return array 币种ID => 币种标识
  97. */
  98. static public function getIdentifications()
  99. {
  100. return CacheHelper::cacheCall([__CLASS__, __FUNCTION__], function () {
  101. $list = FundCurrencyModel::query()->pluck('identification', 'id')->toArray();
  102. return $list;
  103. }, [], 10, [ConfigService::CACHE_TAG]);
  104. }
  105. /**
  106. * 根据币种标识获取币种信息
  107. *
  108. * @param string $identification 币种标识
  109. * @return FundCurrencyModel|null 币种信息
  110. */
  111. static public function getCurrencyByIdentification($identification)
  112. {
  113. return Cache::remember('currency_by_identification_' . $identification, self::CACHE_TTL, function () use ($identification) {
  114. return FundCurrencyModel::where('identification', $identification)->first();
  115. });
  116. }
  117. /**
  118. * 获取所有币种列表
  119. *
  120. * @param bool $refresh 是否刷新缓存
  121. * @return array 币种列表
  122. */
  123. static public function getAllCurrencies($refresh = false)
  124. {
  125. $cacheKey = 'all_currencies';
  126. if ($refresh) {
  127. Cache::forget($cacheKey);
  128. }
  129. return Cache::remember($cacheKey, self::CACHE_TTL, function () {
  130. return FundCurrencyModel::all()->toArray();
  131. });
  132. }
  133. /**
  134. * 获取币种的精度
  135. *
  136. * @param int $currency_id 币种ID
  137. * @return int 精度(小数位数)
  138. */
  139. static public function getCurrencyPrecision($currency_id)
  140. {
  141. $currency = FundCurrencyModel::find($currency_id);
  142. if (!$currency) {
  143. return 2; // 默认精度为2
  144. }
  145. // 从data1字段中获取精度信息
  146. if (!empty($currency->data1)) {
  147. $data = json_decode($currency->data1, true);
  148. if (isset($data['precision'])) {
  149. return (int)$data['precision'];
  150. }
  151. }
  152. return 2; // 默认精度为2
  153. }
  154. /**
  155. * 格式化金额显示
  156. *
  157. * @param int $amount 金额(整数形式)
  158. * @param int $currency_id 币种ID
  159. * @return string 格式化后的金额
  160. */
  161. static public function formatAmount($amount, $currency_id)
  162. {
  163. $precision = self::getCurrencyPrecision($currency_id);
  164. $divisor = pow(10, $precision);
  165. // 将整数形式的金额转换为带小数的形式
  166. $formattedAmount = number_format($amount / $divisor, $precision, '.', ',');
  167. // 获取币种标识
  168. $currency = FundCurrencyModel::find($currency_id);
  169. $symbol = $currency ? $currency->identification : '';
  170. return $symbol . ' ' . $formattedAmount;
  171. }
  172. /**
  173. * 将用户输入的金额转换为系统存储的整数形式
  174. *
  175. * @param float $amount 用户输入的金额
  176. * @param int $currency_id 币种ID
  177. * @return int 系统存储的整数形式金额
  178. */
  179. static public function convertToStorageAmount($amount, $currency_id)
  180. {
  181. $precision = self::getCurrencyPrecision($currency_id);
  182. $multiplier = pow(10, $precision);
  183. return (int)round($amount * $multiplier);
  184. }
  185. /**
  186. * 将系统存储的整数形式金额转换为实际金额
  187. *
  188. * @param int $storageAmount 系统存储的整数形式金额
  189. * @param int $currency_id 币种ID
  190. * @return float 实际金额
  191. */
  192. static public function convertFromStorageAmount($storageAmount, $currency_id)
  193. {
  194. $precision = self::getCurrencyPrecision($currency_id);
  195. $divisor = pow(10, $precision);
  196. return $storageAmount / $divisor;
  197. }
  198. }