From a3fe9a1d5f6027aee29488fd0c7956db62cdfffe Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Wed, 1 Sep 2021 15:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=B8=8A=E6=9E=B6=E8=A7=A6?= =?UTF-8?q?=E7=8A=AF=E6=97=B6=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ProductController.php | 54 ---------- app/Providers/ProductServiceProvider.php | 102 ++++++++++++++++++ config/app.php | 1 + 3 files changed, 103 insertions(+), 54 deletions(-) create mode 100644 app/Providers/ProductServiceProvider.php diff --git a/app/AdminSupplier/Controllers/ProductController.php b/app/AdminSupplier/Controllers/ProductController.php index 8c78d20..e6f0ca8 100644 --- a/app/AdminSupplier/Controllers/ProductController.php +++ b/app/AdminSupplier/Controllers/ProductController.php @@ -133,60 +133,6 @@ class ProductController extends AdminController ->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉 ->update(['status' => ProductStatus::SOLD_OUT]); } - - //自动上架 - if ($form->isCreating()) { - $agentIds = AgentProductItem::query()->withoutGlobalScope('orderById')->where('supplier_id',$form->supplier_id)->distinct()->pluck('agent_id'); - foreach ($agentIds as $v) { - //如果没开启自动上架 滚蛋 - if(empty(AgentSetting::val($v,'auto_shelves'))) { - continue; - } - DB::beginTransaction(); - try { - $agentProduct = new AgentProduct(); - $agentProduct->title = $form->title; - $agentProduct->agent_id = $v; - $agentProduct->product_id = $id; - $agentProduct->product_ids = explode(',',$id); - $agentProduct->stock = $form->stock; - $agentProduct->status = 1; - $agentProduct->pictures = explode(',',$form->pictures); - $agentProduct->content = $form->content; - $agentProduct->know = $form->know; - //计算价格 - $profit = AgentSetting::val($v, 'profit') ?? 0; - $price = bcmul($form->price, bcdiv($profit + 100, 100, 6), 2); - - $agentProduct->price = $price; - $agentProduct->original_price = $price; - - //自动添加分类 - $autoCategory = AgentSetting::val($v, 'auto_category') ?? 0; - - if (!empty($autoCategory)) { - $categoryName = Category::query()->where('id', $form->category_id)->value('name'); - $category = Category::query()->firstOrCreate(['agent_id' => $v, 'name' => $categoryName]); - $agentProduct->category_id = $category->id; - } - - $agentProduct->save(); - - //维护关联表 - $agentProductItem = AgentProductItem::query()->create([ - 'agent_id' => $v, - 'supplier_id' => $form->supplier_id, - 'agent_product_id' => $agentProduct->id, - 'product_id' => $id, - ]); - DB::commit(); - } catch (\Exception $e) { - Log::error('自动上架失败::'.$e->getTraceAsString()); - DB::rollBack(); - return $form->response->error('自动上架失败,稍后重试或联系管理员!'.$e->getMessage()); - } - } - } })->deleting(function (Form $form) { //不允许删除非自己的数据 if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) { diff --git a/app/Providers/ProductServiceProvider.php b/app/Providers/ProductServiceProvider.php new file mode 100644 index 0000000..1e8c259 --- /dev/null +++ b/app/Providers/ProductServiceProvider.php @@ -0,0 +1,102 @@ +isDirty('status') && $product->status == ProductStatus::ON_SALE) { + //自动上架 + + $agentIds = AgentProductItem::query() + ->withoutGlobalScope('orderById') + ->where('supplier_id', $product->supplier_id) + ->distinct() + ->pluck('agent_id'); + foreach ($agentIds as $v) { + //如果没开启自动上架 滚蛋 + if (empty(AgentSetting::val($v, 'auto_shelves'))) { + continue; + } + //如果已经有这个产品 滚蛋 + if(AgentProductItem::query()->where(['product_id' => $product->supplier_id,'agent_id' => $v])->exists()){ + continue; + } + //啥有没有 自动添加商品 + DB::beginTransaction(); + try { + $agentProduct = new AgentProduct(); + $agentProduct->title = $product->title; + $agentProduct->agent_id = $v; + $agentProduct->product_id = $product->id; + $agentProduct->product_ids = $product->id; + $agentProduct->stock = $product->stock; + $agentProduct->status = 1; + $agentProduct->pictures = $product->pictures; + $agentProduct->content = $product->content; + $agentProduct->know = $product->know; + //计算价格 + $profit = AgentSetting::val($v, 'profit') ?? 0; + $price = bcmul($product->price, bcdiv($profit + 100, 100, 6), 2); + + $agentProduct->price = $price; + $agentProduct->original_price = $price; + + //自动添加分类 + $autoCategory = AgentSetting::val($v, 'auto_category') ?? 0; + + if (!empty($autoCategory)) { + $categoryName = Category::query()->where('id', $product->category_id)->value('name'); + $category = Category::query()->firstOrCreate(['agent_id' => $v, 'name' => $categoryName]); + $agentProduct->category_id = $category->id; + } + + $agentProduct->save(); + + //维护关联表 + $agentProductItem = AgentProductItem::query()->create([ + 'agent_id' => $v, + 'supplier_id' => $product->supplier_id, + 'agent_product_id' => $agentProduct->id, + 'product_id' => $product->id, + ]); + DB::commit(); + } catch (\Exception $e) { + Log::error('自动上架失败::' . $e->getTraceAsString()); + DB::rollBack(); + return $product->response->error('自动上架失败,稍后重试或联系管理员!' . $e->getMessage()); + } + } + } + + }); + } +} diff --git a/config/app.php b/config/app.php index 09aecc1..d479b6b 100644 --- a/config/app.php +++ b/config/app.php @@ -175,6 +175,7 @@ return [ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, App\Providers\DemandBiddingServiceProvider::class, + App\Providers\ProductServiceProvider::class, ], /*