VerifyFundDecimal.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. use App\Module\Fund\Services\FundService;
  5. use App\Module\Fund\Services\AccountService;
  6. use App\Module\Fund\Services\CurrencyService;
  7. use Illuminate\Console\Command;
  8. class VerifyFundDecimal extends Command
  9. {
  10. protected $signature = 'fund:verify-decimal';
  11. protected $description = '验证Fund模块小数适配';
  12. public function handle()
  13. {
  14. $this->info('=== Fund模块小数适配验证 ===');
  15. try {
  16. $testUserId = 10001;
  17. // 确保测试用户有账户
  18. AccountService::check4user($testUserId);
  19. // 测试1: 验证balance方法返回类型
  20. $this->info('1. 测试balance方法返回类型...');
  21. $fund = new FundService($testUserId, FUND_TYPE::FUND2->value);
  22. $balance = $fund->balance();
  23. $this->line(" 余额类型: " . gettype($balance));
  24. $this->line(" 余额值: " . $balance);
  25. // 测试2: 验证CurrencyService的小数处理和币种ID概念
  26. $this->info('2. 测试CurrencyService小数处理和币种ID概念...');
  27. $currency = CurrencyService::getCurrencyByFundId(FUND_TYPE::FUND2->value);
  28. if ($currency) {
  29. $this->line(" 数据库表ID: " . $currency->id);
  30. $this->line(" 币种ID(枚举值): " . $currency->currencyId);
  31. $this->line(" 币种精度: " . $currency->precision);
  32. // 测试精度验证(使用币种ID,不是数据库表ID)
  33. $testAmount = 100.123456789;
  34. $isValid = CurrencyService::validateAmountPrecision($testAmount, $currency->currencyId);
  35. $formatted = CurrencyService::formatAmountToPrecision($testAmount, $currency->currencyId);
  36. $this->line(" 原始金额: " . $testAmount);
  37. $this->line(" 精度验证: " . ($isValid ? '通过' : '失败'));
  38. $this->line(" 格式化后: " . $formatted);
  39. $this->line(" 格式化显示: " . CurrencyService::formatAmount($formatted, $currency->currencyId));
  40. } else {
  41. $this->line(" 无法获取币种信息");
  42. }
  43. // 测试3: 验证FundAccountDto
  44. $this->info('3. 测试FundAccountDto...');
  45. $account = $fund->getAccount();
  46. if ($account) {
  47. $this->line(" 账户余额类型: " . gettype($account->balance));
  48. $this->line(" 账户余额值: " . $account->balance);
  49. } else {
  50. $this->line(" 无法获取账户信息");
  51. }
  52. $this->info('=== 验证完成 ===');
  53. } catch (\Exception $e) {
  54. $this->error("错误: " . $e->getMessage());
  55. $this->error("文件: " . $e->getFile() . ":" . $e->getLine());
  56. }
  57. }
  58. }