diff --git a/app/Console/Commands/Swoole.php b/app/Console/Commands/Swoole.php index 1967e06..c58160a 100644 --- a/app/Console/Commands/Swoole.php +++ b/app/Console/Commands/Swoole.php @@ -48,7 +48,7 @@ class Swoole extends Command $endTime = DateTimeUtils::getMicroTime(); $beginTime = $endTime - 10000; foreach ($shops as $shop) { - BusinessFactory::init()->make($shop->plat_id)->setShop($shop)->downloadOrdersAndSave($beginTime, $endTime, 'increment', 1); + BusinessFactory::init()->make($shop->plat_id)->setShop($shop)->downloadOrdersAndSave($beginTime, $endTime, 'increment'); } }); Event::wait(); diff --git a/app/Events/BusinessOrdersUpdate.php b/app/Events/BusinessOrdersUpdate.php index abf9823..08b3393 100644 --- a/app/Events/BusinessOrdersUpdate.php +++ b/app/Events/BusinessOrdersUpdate.php @@ -36,9 +36,9 @@ class BusinessOrdersUpdate { [$goodsCode, $skuCode] = explode('_', $this->businessOrderItem['external_sku_id']); $this->goodsSku = GoodsSku::query()->where('sku_code', $skuCode) - ->with(['goods' => function ($query) use ($goodsCode) { + ->whereHas('goods', function ($query) use ($goodsCode) { $query->where('goods_code', $goodsCode); - }]) + }) ->first(); if ($this->goodsSku) { $this->goodsSku->stock += $this->num; diff --git a/app/Http/Controllers/Shop/ShopsController.php b/app/Http/Controllers/Shop/ShopsController.php index b1acbcc..ff62287 100644 --- a/app/Http/Controllers/Shop/ShopsController.php +++ b/app/Http/Controllers/Shop/ShopsController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Shop; use App\Http\Controllers\Controller; +use App\Models\GoodsSku; use App\Models\Shop; use App\Http\Resources\ShopsResource; use Illuminate\Http\Request; @@ -113,7 +114,7 @@ class ShopsController extends Controller $fields = implode(',', [ 'shop_id', 'external_sku_id', - 'count(id) as count', + 'sum(goods_number) as num', ]); $res = BusinessOrderItem::query() ->select(DB::raw($fields)) @@ -123,9 +124,23 @@ class ShopsController extends Controller ->get(); $data = []; foreach ($res as $item) { - $data[$item->external_sku_id][] = [ + [$goodsCode, $skuCode] = explode('_', $item['external_sku_id']); + $sku = GoodsSku::query()->where('sku_code', $skuCode) + ->whereHas('goods', function ($query) use ($goodsCode) { + $query->where('goods_code', $goodsCode); + }) + ->first(); + if (empty($sku)) { + continue; + } + $sku = $sku->toArray(); + if (!isset($data[$sku['id']]['total'])) { + $data[$sku['id']]['total'] = 0; + } + $data[$sku['id']]['total'] += $item->num; + $data[$sku['id']]['items'][] = [ 'shop_name' => $item->shop->name, - 'count' => $item->count, + 'num' => $item->num, ]; } diff --git a/app/Listeners/StockUpdateListener.php b/app/Listeners/StockUpdateListener.php index 0897976..e9c8d97 100644 --- a/app/Listeners/StockUpdateListener.php +++ b/app/Listeners/StockUpdateListener.php @@ -29,7 +29,7 @@ class StockUpdateListener */ public function handle(StockUpdateEvent $event) { - $shops = Shop::query()->where('status', 1)->get(['id', 'plat_id']); + $shops = Shop::query()->whereNotIn('status', [0, 3])->get(['id', 'plat_id']); if (empty($shops)) { return; } diff --git a/app/Listeners/UpdateBusinessGoodsStock.php b/app/Listeners/UpdateBusinessGoodsStock.php index e29d2ac..e60d100 100644 --- a/app/Listeners/UpdateBusinessGoodsStock.php +++ b/app/Listeners/UpdateBusinessGoodsStock.php @@ -45,7 +45,7 @@ class UpdateBusinessGoodsStock implements ShouldQueue return; } - $shops = Shop::query()->where('id', '<>', $event->businessOrderItem['shop_id'])->where('status', 1)->get(['id', 'plat_id']); + $shops = Shop::query()->where('id', '<>', $event->businessOrderItem['shop_id'])->whereNotIn('status', [0, 3])->get(['id', 'plat_id']); if (empty($shops)) { return; } diff --git a/app/Services/Business/BusinessClient.php b/app/Services/Business/BusinessClient.php index 2d83682..d9372ec 100644 --- a/app/Services/Business/BusinessClient.php +++ b/app/Services/Business/BusinessClient.php @@ -139,21 +139,19 @@ abstract class BusinessClient $res = (new Client())->request($method, $url, $headers); $size = $res->getBody()->getSize(); $res = json_decode($res->getBody()->getContents(), true); - if ('pdd.ktt.increment.order.query' === $params['type']) { - $log = Log::query()->where('user_id', $this->getShop()->id)->where('target_field', $params['type'])->first(); - } else { + if (!in_array($params['type'], ['pdd.ktt.increment.order.query', 'pdd.ktt.order.list'], true)) { $log = new Log(); + $log->module = 'plat'; + $log->action = $method; + $log->target_type = $this->getShop()->plat_id; + $log->target_id = $this->getSkuId(); + $log->target_field = $params['type']; + $log->user_id = $this->getShop()->id; + if ($size < 64000) { + $log->message = json_encode($res, 256); + } + $log->save(); } - $log->module = 'plat'; - $log->action = $method; - $log->target_type = $this->getShop()->plat_id; - $log->target_id = $this->getSkuId(); - $log->target_field = $params['type']; - $log->user_id = $this->getShop()->id; - if ($size < 64000) { - $log->message = json_encode($res, 256); - } - $log->save(); return $res; } diff --git a/app/Services/Business/KuaiTuanTuan/Goods.php b/app/Services/Business/KuaiTuanTuan/Goods.php index 9de4778..f3613b6 100644 --- a/app/Services/Business/KuaiTuanTuan/Goods.php +++ b/app/Services/Business/KuaiTuanTuan/Goods.php @@ -32,7 +32,7 @@ class Goods $sku['spec_list'] = json_encode($sku['spec_list'], 256); $data = array_merge($businessGood, $sku); BusinessGoodsSku::updateOrCreate( - ['shop_id' => $shopId, 'activity_no' => $businessGood['activity_no'], 'goods_id' => $businessGood['goods_id'], 'sku_id' => $sku['sku_id']], + ['shop_id' => $shopId, 'goods_id' => $businessGood['goods_id'], 'sku_id' => $sku['sku_id']], $data ); } diff --git a/app/Services/Business/MiaoXuan/Goods.php b/app/Services/Business/MiaoXuan/Goods.php index 8276fa6..79cac25 100644 --- a/app/Services/Business/MiaoXuan/Goods.php +++ b/app/Services/Business/MiaoXuan/Goods.php @@ -18,13 +18,14 @@ class Goods public static function incrQuantity($shopId, $quantity, $businessGoods) { + [$goodsCode, $skuCode] = explode('_', $businessGoods['external_sku_id']); return [ 'data' => [ 'stock' => $quantity, 'business_sku_id' => $businessGoods['sku_id'], 'business_goods_id' => $businessGoods['goods_id'], 'erp_shop_id' => $shopId, - 'erp_sku_id' => $businessGoods['external_sku_id'], + 'erp_sku_id' => $skuCode, ], 'type' => '更新库存', ]; diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index c26b9f5..305ba38 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -16,16 +16,15 @@ class CreateUsersTable extends Migration Schema::defaultStringLength(191); Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); - $table->string('name'); + $table->string('name')->unique(); $table->string('email')->nullable()->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); - $table->string('api_token', 80)->unique()->nullable(false); + $table->string('api_token', 80)->unique(); $table->softDeletes(); $table->rememberToken(); $table->timestamps(); // 索引 - $table->unique('name'); }); } diff --git a/database/migrations/2022_07_26_061712_create_goods_types_table.php b/database/migrations/2022_07_26_061712_create_goods_types_table.php index 0ef0220..d923fa7 100644 --- a/database/migrations/2022_07_26_061712_create_goods_types_table.php +++ b/database/migrations/2022_07_26_061712_create_goods_types_table.php @@ -16,11 +16,10 @@ class CreateGoodsTypesTable extends Migration Schema::defaultStringLength(191); Schema::create('goods_types', function (Blueprint $table) { $table->bigIncrements('id'); - $table->string('name')->nullable(false); + $table->string('name')->unique(); $table->softDeletes(); $table->timestamps(); // 索引 - $table->unique('name'); }); } diff --git a/database/migrations/2022_07_26_085847_create_goods_brands_table.php b/database/migrations/2022_07_26_085847_create_goods_brands_table.php index 6e44c7c..482bd44 100644 --- a/database/migrations/2022_07_26_085847_create_goods_brands_table.php +++ b/database/migrations/2022_07_26_085847_create_goods_brands_table.php @@ -16,11 +16,10 @@ class CreateGoodsBrandsTable extends Migration Schema::defaultStringLength(191); Schema::create('goods_brands', function (Blueprint $table) { $table->bigIncrements('id'); - $table->string('name')->nullable(false); + $table->string('name')->unique(); $table->softDeletes(); $table->timestamps(); // 索引 - $table->unique('name'); }); } diff --git a/database/migrations/2022_07_26_090143_create_goods_table.php b/database/migrations/2022_07_26_090143_create_goods_table.php index 1fdf2da..71ad099 100644 --- a/database/migrations/2022_07_26_090143_create_goods_table.php +++ b/database/migrations/2022_07_26_090143_create_goods_table.php @@ -16,14 +16,13 @@ class CreateGoodsTable extends Migration Schema::defaultStringLength(191); Schema::create('goods', function (Blueprint $table) { $table->bigIncrements('id'); - $table->string('title')->nullable(false); + $table->string('title'); $table->string('img_url')->nullable()->comment('商品图片'); - $table->unsignedBigInteger('type_id')->nullable(false)->comment('商品种类id'); + $table->unsignedBigInteger('type_id')->comment('商品种类id'); $table->unsignedBigInteger('brand_id')->nullable()->comment('商品品牌id'); - $table->string('goods_code', 32)->nullable(false)->comment('商品编码'); + $table->string('goods_code', 32)->unique()->comment('商品编码'); $table->timestamps(); // 索引 - $table->unique('goods_code'); }); } diff --git a/database/migrations/2022_07_26_090150_create_goods_skus_table.php b/database/migrations/2022_07_26_090150_create_goods_skus_table.php index 468e37b..3b0f07a 100644 --- a/database/migrations/2022_07_26_090150_create_goods_skus_table.php +++ b/database/migrations/2022_07_26_090150_create_goods_skus_table.php @@ -16,9 +16,9 @@ class CreateGoodsSkusTable extends Migration Schema::defaultStringLength(191); Schema::create('goods_skus', function (Blueprint $table) { $table->bigIncrements('id'); - $table->unsignedBigInteger('goods_id')->nullable(false)->comment('商品id'); - $table->string('title')->nullable(false)->comment('商品规格'); - $table->string('sku_code', 32)->nullable(false)->comment('规格编码'); + $table->unsignedBigInteger('goods_id')->comment('商品id'); + $table->string('title')->comment('商品规格'); + $table->string('sku_code', 32)->comment('规格编码'); $table->unsignedTinyInteger('status')->default(0)->comment('规格状态(0-下架,1在售,2预警)'); $table->unsignedInteger('num')->default(0)->comment('总量'); $table->unsignedInteger('stock')->default(0)->comment('库存'); diff --git a/database/migrations/2022_07_26_103559_create_daily_stock_records_table.php b/database/migrations/2022_07_26_103559_create_daily_stock_records_table.php index 3b1c792..6d985ce 100644 --- a/database/migrations/2022_07_26_103559_create_daily_stock_records_table.php +++ b/database/migrations/2022_07_26_103559_create_daily_stock_records_table.php @@ -15,8 +15,8 @@ class CreateDailyStockRecordsTable extends Migration { Schema::create('daily_stock_records', function (Blueprint $table) { $table->bigIncrements('id'); - $table->bigInteger('sku_id')->nullable(false); - $table->date('day')->nullable(false); + $table->bigInteger('sku_id'); + $table->date('day'); $table->unsignedInteger('arrived_today_num')->default(0)->comment('今日到货'); $table->unsignedInteger('loss_num')->default(0)->comment('损耗'); $table->unsignedInteger('inventory')->default(0)->comment('库存盘点'); diff --git a/database/migrations/2022_07_26_105818_create_logs_table.php b/database/migrations/2022_07_26_105818_create_logs_table.php index da9c3c2..aa67ef1 100644 --- a/database/migrations/2022_07_26_105818_create_logs_table.php +++ b/database/migrations/2022_07_26_105818_create_logs_table.php @@ -24,8 +24,9 @@ class CreateLogsTable extends Migration $table->text('after_update')->nullable()->comment('更新后数据'); $table->text('message')->nullable()->comment('备注信息'); $table->bigInteger('user_id')->comment('操作人id'); - $table->index(['target_type', 'target_id', 'target_field']); $table->timestamps(); + // 索引 + $table->index(['target_type', 'target_id', 'target_field']); }); } diff --git a/database/migrations/2022_07_28_095523_create_menus_table.php b/database/migrations/2022_07_28_095523_create_menus_table.php index 18bdaea..5019185 100644 --- a/database/migrations/2022_07_28_095523_create_menus_table.php +++ b/database/migrations/2022_07_28_095523_create_menus_table.php @@ -16,14 +16,13 @@ class CreateMenusTable extends Migration Schema::defaultStringLength(191); Schema::create('menus', function (Blueprint $table) { $table->bigIncrements('id'); - $table->string('code', 32)->nullable(false)->comment('菜单编码'); - $table->string('name', 32)->nullable(false)->comment('菜单名称'); + $table->string('code', 32)->unique()->comment('菜单编码'); + $table->string('name', 32)->comment('菜单名称'); $table->unsignedBigInteger('parent_id')->default(0); $table->unsignedInteger('seq')->default(0)->comment('排序序号'); $table->softDeletes(); $table->timestamps(); // 索引 - $table->unique('code'); }); } diff --git a/database/migrations/2022_08_02_022448_create_shops_table.php b/database/migrations/2022_08_02_022448_create_shops_table.php index 8364f24..e56de65 100644 --- a/database/migrations/2022_08_02_022448_create_shops_table.php +++ b/database/migrations/2022_08_02_022448_create_shops_table.php @@ -28,9 +28,10 @@ class CreateShopsTable extends Migration $table->unsignedInteger('refresh_token_expires_in')->nullable()->comment('refresh_token过期时间段,10表示10秒后过期'); $table->text('scope')->nullable()->comment('接口列表'); $table->text('pop_auth_token_create_response')->nullable()->comment('授权认证信息'); - $table->unsignedTinyInteger('status')->default(0)->comment('状态'); + $table->unsignedTinyInteger('status')->index()->default(0)->comment('状态'); $table->softDeletes(); $table->timestamps(); + //索引 }); } diff --git a/database/migrations/2022_08_05_030834_create_business_goods_skus_table.php b/database/migrations/2022_08_05_030834_create_business_goods_skus_table.php index 7a84e15..c768bc7 100644 --- a/database/migrations/2022_08_05_030834_create_business_goods_skus_table.php +++ b/database/migrations/2022_08_05_030834_create_business_goods_skus_table.php @@ -41,6 +41,8 @@ class CreateBusinessGoodsSkusTable extends Migration $table->string('thumb_url')->nullable(); $table->bigInteger('total_quantity')->nullable(); $table->timestamps(); + $table->index(['shop_id', 'goods_id', 'sku_id']); + $table->index(['shop_id', 'external_sku_id']); }); } diff --git a/database/migrations/2022_08_05_093629_create_business_orders_table.php b/database/migrations/2022_08_05_093629_create_business_orders_table.php index d82f287..94ef6ef 100644 --- a/database/migrations/2022_08_05_093629_create_business_orders_table.php +++ b/database/migrations/2022_08_05_093629_create_business_orders_table.php @@ -55,6 +55,8 @@ class CreateBusinessOrdersTable extends Migration $table->string('transaction_id')->nullable(); $table->integer('verification_status')->nullable(); $table->timestamps(); + // 索引 + $table->unique(['shop_id', 'order_sn']); }); } diff --git a/database/migrations/2022_08_05_093658_create_business_order_items_table.php b/database/migrations/2022_08_05_093658_create_business_order_items_table.php index 0495831..e4bc837 100644 --- a/database/migrations/2022_08_05_093658_create_business_order_items_table.php +++ b/database/migrations/2022_08_05_093658_create_business_order_items_table.php @@ -39,6 +39,8 @@ class CreateBusinessOrderItemsTable extends Migration $table->string('thumb_url')->nullable(); $table->integer('verification_number')->nullable(); $table->timestamps(); + // 索引 + $table->index(['shop_id', 'business_order_id', 'goods_id', 'sku_id']); }); }