diff --git a/beike/Repositories/ProductRepo.php b/beike/Repositories/ProductRepo.php index c54e671f..450e5f3a 100644 --- a/beike/Repositories/ProductRepo.php +++ b/beike/Repositories/ProductRepo.php @@ -77,6 +77,12 @@ class ProductRepo { $builder = Product::query()->with('description', 'skus', 'master_sku', 'attributes'); + $builder->leftJoin('product_descriptions as pd', function ($build) { + $build->whereColumn('pd.product_id', 'products.id') + ->where('locale', locale()); + }); + $builder->select(['products.*', 'pd.name', 'pd.content', 'pd.meta_title', 'pd.meta_description', 'pd.meta_keywords', 'pd.name']); + if (isset($data['category_id'])) { $builder->whereHas('categories', function ($query) use ($data) { if (is_array($data['category_id'])) { @@ -89,7 +95,7 @@ class ProductRepo $productIds = $data['product_ids'] ?? []; if ($productIds) { - $builder->whereIn('id', $productIds); + $builder->whereIn('products.id', $productIds); $productIds = implode(',', $productIds); $builder->orderByRaw("FIELD(products.id, {$productIds})"); } diff --git a/beike/Services/StateMachineService.php b/beike/Services/StateMachineService.php index 1844e1e7..e7e70dd6 100644 --- a/beike/Services/StateMachineService.php +++ b/beike/Services/StateMachineService.php @@ -14,6 +14,7 @@ namespace Beike\Services; use Beike\Models\Order; use Beike\Models\OrderHistory; use Beike\Models\OrderShipment; +use Beike\Models\Product; use Throwable; class StateMachineService @@ -54,7 +55,7 @@ class StateMachineService self::UNPAID => ['updateStatus', 'addHistory', 'notifyNewOrder'], ], self::UNPAID => [ - self::PAID => ['updateStatus', 'addHistory', 'subStock', 'notifyUpdateOrder'], + self::PAID => ['updateStatus', 'addHistory', 'updateSales', 'subStock', 'notifyUpdateOrder'], self::CANCELLED => ['updateStatus', 'addHistory', 'notifyUpdateOrder'], ], self::PAID => [ @@ -239,6 +240,21 @@ class StateMachineService $this->order->saveOrFail(); } + /** + * 更新订单商品销量 + * @return void + */ + private function updateSales() + { + $this->order->loadMissing([ + 'orderProducts' + ]); + $orderProducts = $this->order->orderProducts; + foreach ($orderProducts as $orderProduct) { + Product::query()->where('id', $orderProduct->product_id)->increment('sales', $orderProduct->quantity); + } + } + /** * 添加更改记录 * diff --git a/database/migrations/2023_01_10_115702_add_sales2_product.php b/database/migrations/2023_01_10_115702_add_sales2_product.php new file mode 100644 index 00000000..4b076755 --- /dev/null +++ b/database/migrations/2023_01_10_115702_add_sales2_product.php @@ -0,0 +1,40 @@ +integer('sales')->default(0)->after('tax_class_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + if (!Schema::hasColumn('products', 'sales')) { + return; + } + + Schema::table('products', function (Blueprint $table) { + $table->dropColumn('sales'); + }); + } +};