商品筛选排序

This commit is contained in:
TL 2023-01-11 15:24:55 +08:00
parent a3d2236df8
commit 1eb219cc0e
3 changed files with 64 additions and 2 deletions

View File

@ -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})");
}

View File

@ -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);
}
}
/**
* 添加更改记录
*

View File

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasColumn('products', 'sales')) {
return;
}
Schema::table('products', function (Blueprint $table) {
$table->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');
});
}
};