| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace App\Module\Shop\Commands;
- use App\Module\Game\DCache\ShopItemsJsonConfig;
- use App\Module\Game\Models\GameRewardItem;
- use App\Module\Game\Services\ConsumeService;
- use App\Module\Game\Services\JsonConfigService;
- use App\Module\Game\Services\RewardService;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use App\Module\Shop\Models\ShopItem;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Common\REWARD_TYPE;
- /**
- * 生成商店商品配置表JSON数据命令
- *
- * 该命令用于从数据库中的商店商品表生成商品JSON数据文件,供客户端使用。
- * 生成的JSON文件包含商品的基本信息,如ID、名称、描述、消耗组、奖励组和展示属性等。
- * 该命令通常在商店商品数据更新后运行,以确保客户端获取最新的商品数据。
- */
- class GenerateShopItemsJsonCommand extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'shop:generate-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'Generate shop_items.json from ShopItem table';
- /**
- * 执行命令
- */
- /**
- * 生成商店商品JSON数据
- *
- * @return array|bool 生成的数据或失败标志
- */
- public static function generateJson()
- {
- try {
- // 查询ShopItem表中的激活商品数据,并预加载关联数据
- $shopItems = ShopItem::query()
- ->with([
- 'category',
- 'consumeGroup.consumeItems',
- 'rewardGroup.rewardItems',
- 'activePurchaseLimits'
- ])
- ->where('is_active', 1)
- ->where(function ($query) {
- // 检查上架时间
- $query->whereNull('start_time')
- ->orWhere('start_time', '<=', now());
- })
- ->where(function ($query) {
- // 检查下架时间
- $query->whereNull('end_time')
- ->orWhere('end_time', '>=', now());
- })
- // ->orderBy('sort_order', 'desc')
- ->orderBy('id', 'asc')
- ->get()
- ->map(function (ShopItem $item) {
- // 构建商品数据
- $itemData = [
- 'id' => $item->id,
- 'name' => $item->name,
- 'description' => $item->description,
- 'category_name' => $item->category_name,
- 'max_single_buy' => $item->max_single_buy,
- 'sort_order' => $item->sort_order,
- 'display_attributes' => $item->display_attributes,
- 'start_time' => $item->start_time ? $item->start_time->toDateTimeString() : null,
- 'end_time' => $item->end_time ? $item->end_time->toDateTimeString() : null,
- ];
- // 添加消耗组信息
- if ($item->consumeGroup) {
- $consume = ConsumeService::getConsumeGroupAsDeduct($item->consume_group_id);
- $itemData['consume_group'] = json_decode($consume->serializeToJsonString(),true);
- }
- // 添加奖励组信息
- if ($item->rewardGroup) {
- $reward = RewardService::getRewardGroupAsReward($item->reward_group_id);
- $itemData['reward_group'] = json_decode($reward->serializeToJsonString(),true);
- }
- // 添加限购信息
- if ($item->activePurchaseLimits->isNotEmpty()) {
- $itemData['purchase_limits'] = $item->activePurchaseLimits->map(function ($limit) {
- return [
- 'id' => $limit->id,
- 'type' => $limit->type,
- 'period' => $limit->period,
- 'max_quantity' => $limit->max_quantity,
- 'description' => $limit->description,
- ];
- })->toArray();
- }
- return $itemData;
- })
- ->toArray();
- // dd($shopItems);
- // 准备完整数据,包含生成时间
- $data = [
- 'generated_ts' => time(),
- 'generated_at' => now()->toDateTimeString(),
- 'shop_items' => $shopItems
- ];
- return $data;
- } catch (\Exception $e) {
- Log::error('Generate shop_items.json failed: ' . $e->getMessage());
- return false;
- }
- }
- public function handle()
- {
- if (ShopItemsJsonConfig::getData([], true)) {
- $this->info('Successfully generated shop_items.json with timestamp');
- } else {
- $this->error('Failed to generate shop_items.json');
- }
- }
- }
|