GenerateShopItemsJsonCommand.php 5.0 KB

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