GenerateFundCurrencyConfigJson.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Module\Fund\Commands;
  3. use App\Module\Fund\Models\FundConfigModel;
  4. use App\Module\Fund\Models\FundCurrencyModel;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. * 生成货币配置表JSON数据命令
  9. *
  10. * 该命令用于从数据库中的货币配置表生成JSON数据文件,供客户端使用。
  11. * 生成的JSON文件包含货币的基本信息,如ID、名称、标识、图标等。
  12. * 该命令通常在货币配置数据更新后运行,以确保客户端获取最新的配置数据。
  13. */
  14. class GenerateFundCurrencyConfigJson extends Command
  15. {
  16. /**
  17. * 命令名称
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'fund:generate-currency-json';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = '生成货币配置JSON文件';
  28. /**
  29. * 生成货币配置JSON数据
  30. *
  31. * @return array|bool 生成的数据或失败标志
  32. */
  33. public static function generateJson()
  34. {
  35. try {
  36. // 获取所有货币配置
  37. $currencies = FundCurrencyModel::orderBy('id')->get();
  38. // 获取所有账户种类配置
  39. $fundConfigs = FundConfigModel::orderBy('id')->get();
  40. // 准备JSON数据
  41. $jsonData = [
  42. 'generated_ts' => time(),
  43. 'currencies' => [],
  44. 'fund_configs' => []
  45. ];
  46. // 处理币种数据
  47. foreach ($currencies as $currency) {
  48. $currencyData = [
  49. 'id' => $currency->id,
  50. 'identification' => $currency->identification,
  51. 'name' => $currency->name,
  52. 'icon' => $currency->icon,
  53. ];
  54. // 如果有额外数据,解析并添加
  55. if (!empty($currency->data1)) {
  56. $extraData = json_decode($currency->data1, true);
  57. if (is_array($extraData)) {
  58. $currencyData['extra_data'] = $extraData;
  59. }
  60. }
  61. $jsonData['currencies'][] = $currencyData;
  62. }
  63. // 处理账户种类数据
  64. foreach ($fundConfigs as $fundConfig) {
  65. // 直接使用数据库中存储的currency_id字段
  66. $fundConfigData = [
  67. 'id' => $fundConfig->id,
  68. 'name' => $fundConfig->name,
  69. 'currency_id' => $fundConfig->currency_id, // 关联的币种ID
  70. ];
  71. $jsonData['fund_configs'][] = $fundConfigData;
  72. }
  73. return $jsonData;
  74. } catch (\Exception $e) {
  75. if (php_sapi_name() === 'cli') {
  76. echo "Error: Generate fund_currency.json failed: {$e->getMessage()}\n";
  77. }
  78. return false;
  79. }
  80. }
  81. /**
  82. * 执行命令
  83. *
  84. * @return int
  85. */
  86. public function handle()
  87. {
  88. $this->info('开始生成货币配置JSON文件...');
  89. try {
  90. // 直接调用静态方法生成JSON,而不通过缓存类
  91. $result = self::generateJson();
  92. if ($result !== false) {
  93. $this->info('货币配置JSON文件生成成功');
  94. $this->info('共生成 ' . count($result['currencies']) . ' 条币种配置数据');
  95. $this->info('共生成 ' . count($result['fund_configs']) . ' 条账户种类配置数据');
  96. return Command::SUCCESS;
  97. } else {
  98. $this->error('生成货币配置JSON文件失败');
  99. return Command::FAILURE;
  100. }
  101. } catch (\Exception $e) {
  102. $this->error('生成货币配置JSON文件失败: ' . $e->getMessage());
  103. return Command::FAILURE;
  104. }
  105. }
  106. }