| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace App\Module\Fund\Commands;
- use App\Module\Fund\Models\FundConfigModel;
- use App\Module\Fund\Models\FundCurrencyModel;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- /**
- * 生成货币配置表JSON数据命令
- *
- * 该命令用于从数据库中的货币配置表生成JSON数据文件,供客户端使用。
- * 生成的JSON文件包含货币的基本信息,如ID、名称、标识、图标等。
- * 该命令通常在货币配置数据更新后运行,以确保客户端获取最新的配置数据。
- */
- class GenerateFundCurrencyConfigJson extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'fund:generate-currency-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '生成货币配置JSON文件';
- /**
- * 生成货币配置JSON数据
- *
- * @return array|bool 生成的数据或失败标志
- */
- public static function generateJson()
- {
- try {
- // 获取所有货币配置
- $currencies = FundCurrencyModel::orderBy('id')->get();
- // 获取所有账户种类配置
- $fundConfigs = FundConfigModel::orderBy('id')->get();
- // 准备JSON数据
- $jsonData = [
- 'generated_ts' => time(),
- 'currencies' => [],
- 'fund_configs' => []
- ];
- // 处理币种数据
- foreach ($currencies as $currency) {
- $currencyData = [
- 'id' => $currency->id,
- 'identification' => $currency->identification,
- 'name' => $currency->name,
- 'icon' => $currency->icon,
- ];
- // 如果有额外数据,解析并添加
- if (!empty($currency->data1)) {
- $extraData = json_decode($currency->data1, true);
- if (is_array($extraData)) {
- $currencyData['extra_data'] = $extraData;
- }
- }
- $jsonData['currencies'][] = $currencyData;
- }
- // 处理账户种类数据
- foreach ($fundConfigs as $fundConfig) {
- // 直接使用数据库中存储的currency_id字段
- $fundConfigData = [
- 'id' => $fundConfig->id,
- 'name' => $fundConfig->name,
- 'currency_id' => $fundConfig->currency_id, // 关联的币种ID
- ];
- $jsonData['fund_configs'][] = $fundConfigData;
- }
- return $jsonData;
- } catch (\Exception $e) {
- if (php_sapi_name() === 'cli') {
- echo "Error: Generate fund_currency.json failed: {$e->getMessage()}\n";
- }
- return false;
- }
- }
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->info('开始生成货币配置JSON文件...');
- try {
- // 直接调用静态方法生成JSON,而不通过缓存类
- $result = self::generateJson();
- if ($result !== false) {
- $this->info('货币配置JSON文件生成成功');
- $this->info('共生成 ' . count($result['currencies']) . ' 条币种配置数据');
- $this->info('共生成 ' . count($result['fund_configs']) . ' 条账户种类配置数据');
- return Command::SUCCESS;
- } else {
- $this->error('生成货币配置JSON文件失败');
- return Command::FAILURE;
- }
- } catch (\Exception $e) {
- $this->error('生成货币配置JSON文件失败: ' . $e->getMessage());
- return Command::FAILURE;
- }
- }
- }
|