GenerateShopItemsJsonCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Module\Shop\Commands;
  3. use App\Module\Game\DCache\ShopItemsJsonConfig;
  4. use App\Module\Game\Models\GameRewardItem;
  5. use App\Module\Game\Services\JsonConfigService;
  6. use Carbon\Carbon;
  7. use Illuminate\Console\Command;
  8. use App\Module\Shop\Models\ShopItem;
  9. use Illuminate\Support\Facades\Log;
  10. use Uraus\Kku\Common\REWARD_TYPE;
  11. /**
  12. * 生成商店商品配置表JSON数据命令
  13. *
  14. * 该命令用于从数据库中的商店商品表生成商品JSON数据文件,供客户端使用。
  15. * 生成的JSON文件包含商品的基本信息,如ID、名称、描述、消耗组、奖励组和展示属性等。
  16. * 该命令通常在商店商品数据更新后运行,以确保客户端获取最新的商品数据。
  17. */
  18. class GenerateShopItemsJsonCommand extends Command
  19. {
  20. /**
  21. * 命令名称和签名
  22. *
  23. * @var string
  24. */
  25. protected $signature = 'shop:generate-json';
  26. /**
  27. * 命令描述
  28. *
  29. * @var string
  30. */
  31. protected $description = 'Generate shop_items.json from ShopItem table';
  32. /**
  33. * 执行命令
  34. */
  35. /**
  36. * 生成商店商品JSON数据
  37. *
  38. * @return array|bool 生成的数据或失败标志
  39. */
  40. public static function generateJson()
  41. {
  42. try {
  43. // 查询ShopItem表中的激活商品数据,并预加载关联数据
  44. $shopItems = ShopItem::query()
  45. ->with([
  46. 'category',
  47. 'consumeGroup.consumeItems',
  48. 'rewardGroup.rewardItems',
  49. 'activePurchaseLimits'
  50. ])
  51. ->where('is_active', 1)
  52. ->where(function ($query) {
  53. // 检查上架时间
  54. $query->whereNull('start_time')
  55. ->orWhere('start_time', '<=', now());
  56. })
  57. ->where(function ($query) {
  58. // 检查下架时间
  59. $query->whereNull('end_time')
  60. ->orWhere('end_time', '>=', now());
  61. })
  62. // ->orderBy('sort_order', 'desc')
  63. ->orderBy('id', 'asc')
  64. ->get()
  65. ->map(function ($item) {
  66. // 构建商品数据
  67. $itemData = [
  68. 'id' => $item->id,
  69. 'name' => $item->name,
  70. 'description' => $item->description,
  71. 'category_name' => $item->category_name,
  72. 'max_single_buy' => $item->max_single_buy,
  73. 'sort_order' => $item->sort_order,
  74. 'display_attributes' => $item->display_attributes,
  75. 'start_time' => $item->start_time ? $item->start_time->toDateTimeString() : null,
  76. 'end_time' => $item->end_time ? $item->end_time->toDateTimeString() : null,
  77. ];
  78. // 添加消耗组信息
  79. if ($item->consumeGroup) {
  80. $itemData['consume_group'] = [
  81. 'id' => $item->consumeGroup->id,
  82. 'name' => $item->consumeGroup->name,
  83. 'description' => $item->consumeGroup->description,
  84. 'items' => $item->consumeGroup->consumeItems->map(function ( \App\Module\Game\Models\GameConsumeItem $consumeItem) {
  85. $consume = $consumeItem->toDeductObject();
  86. // 转换为 \Uraus\Kku\Common\Deduct 格式的数组(JSON可序列化)
  87. return json_decode($consume->serializeToJsonString(),true);
  88. })->toArray()
  89. ];
  90. $itemData['consume_group'] = $itemData['consume_group'] ['items'];
  91. }
  92. // 添加奖励组信息
  93. if ($item->rewardGroup) {
  94. $itemData['reward_group'] = [
  95. 'id' => $item->rewardGroup->id,
  96. 'name' => $item->rewardGroup->name,
  97. 'description' => $item->rewardGroup->description,
  98. 'items' => $item->rewardGroup->rewardItems->map(function ( GameRewardItem $rewardItem) {
  99. // 转换为 \Uraus\Kku\Common\Reward 格式的数组(JSON可序列化)
  100. $reward = $rewardItem->toRewardObject();
  101. return json_decode($reward->serializeToJsonString(),true);
  102. })->toArray()
  103. ];
  104. $itemData['reward_group'] = $itemData['reward_group'] ['items'];
  105. }
  106. // 添加限购信息
  107. if ($item->activePurchaseLimits->isNotEmpty()) {
  108. $itemData['purchase_limits'] = $item->activePurchaseLimits->map(function ($limit) {
  109. return [
  110. 'id' => $limit->id,
  111. 'type' => $limit->type,
  112. 'period' => $limit->period,
  113. 'max_quantity' => $limit->max_quantity,
  114. 'description' => $limit->description,
  115. ];
  116. })->toArray();
  117. }
  118. return $itemData;
  119. })
  120. ->toArray();
  121. // dd($shopItems);
  122. // 准备完整数据,包含生成时间
  123. $data = [
  124. 'generated_ts' => time(),
  125. 'generated_at' => now()->toDateTimeString(),
  126. 'shop_items' => $shopItems
  127. ];
  128. return $data;
  129. } catch (\Exception $e) {
  130. Log::error('Generate shop_items.json failed: ' . $e->getMessage());
  131. return false;
  132. }
  133. }
  134. public function handle()
  135. {
  136. if (ShopItemsJsonConfig::getData([], true)) {
  137. $this->info('Successfully generated shop_items.json with timestamp');
  138. } else {
  139. $this->error('Failed to generate shop_items.json');
  140. }
  141. }
  142. }