From ecfb2f7b172fc8152d396ff33b2ede809ad9e0ba Mon Sep 17 00:00:00 2001 From: liapples Date: Thu, 11 Nov 2021 00:23:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=9B=E5=BA=94=E5=95=86=E8=A7=84=E6=A0=BC?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=90=8C=E6=AD=A5=E5=88=B0=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E5=95=86=E4=BA=A7=E5=93=81=E8=A7=84=E6=A0=BC=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ProductController.php | 37 +------ app/Jobs/ProductSpecSync.php | 97 +++++++++++++++++++ 2 files changed, 100 insertions(+), 34 deletions(-) create mode 100644 app/Jobs/ProductSpecSync.php diff --git a/app/AdminSupplier/Controllers/ProductController.php b/app/AdminSupplier/Controllers/ProductController.php index 6a8c752..9bd7957 100644 --- a/app/AdminSupplier/Controllers/ProductController.php +++ b/app/AdminSupplier/Controllers/ProductController.php @@ -5,6 +5,7 @@ namespace App\AdminSupplier\Controllers; use App\AdminSupplier\Repositories\Product; use App\Common\OrderStatus; use App\Common\ProductStatus; +use App\Jobs\ProductSpecSync; use App\Models\AgentProduct; use App\Models\AgentProductItem; use App\Models\AgentProductSpec; @@ -373,45 +374,13 @@ class ProductController extends AdminController } })->saved(function (Form $form, $result) { if ($form->isEditing() && $result) { - $ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray(); - - if ($ap_ids) { - //修改信息同步信息到代理商产品,注:组合产品不同步 - AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([ - 'title' => $form->title, - 'pictures' => explode(',', $form->pictures), - 'know' => $form->know, - 'content' => $form->content, - ]); - } - $delete_specs = array_filter($form->spec, fn($v) => $v['_remove_'] !== null); //删除的规格 //同步删除代理商规格 AgentProductSpec::whereIn('product_spec_id', array_keys($delete_specs))->delete(); - //最新销售价同步到代理商规格 - $product_id = $form->getKey(); - - $agent_product_ids = AgentProduct::where('product_id', $product_id)->pluck('id')->toArray(); //获取当前供应商产品的所有供应商产品ID - $product_specs = ProductSpec::where('product_id', $product_id)->get(); - $product_spec_old_ids = $form->model()->spec->pluck('id')->toArray(); //旧的规格ID(不包括新增的) - - if ($agent_product_ids && $product_spec_old_ids) { - //将代理商规格价格小于供应商销售价的,设置代理商规格的价格设置为供应商的售价 - foreach ($product_specs->whereIn('id', $product_spec_old_ids) as $v) { - AgentProductSpec::whereIn('agent_product_id', $agent_product_ids) - ->where([['product_spec_id', '=', $v->id], ['price', '<', $v->price]]) - ->update(['price' => $v->price]); - } - } - - /*if ($product_spec_old_ids) { - $product_spec_new_ids = $product_specs->whereNotIn('id', $product_spec_old_ids)->get()->toArray(); //新增的规格 - if ($product_spec_new_ids) { - //暂时不处理新增的规格 - } - }*/ + //最新规格信息同步到代理商 + ProductSpecSync::dispatch($form->getKey()); } })->deleting(function (Form $form) { //不允许删除非自己的数据 diff --git a/app/Jobs/ProductSpecSync.php b/app/Jobs/ProductSpecSync.php new file mode 100644 index 0000000..aea4e8d --- /dev/null +++ b/app/Jobs/ProductSpecSync.php @@ -0,0 +1,97 @@ +product_id)->get(); + + //获取当前供应商产品的所有代理商产品ID + AgentProduct::query() + ->where('product_id', $this->product_id) + ->chunk(5, function ($agent_products) use ($product_specs) { + //当前供应商产品所有规格的ID + $product_spec_ids = $product_specs->pluck('id')->toArray(); + + //删除供应商已经删除的规格 + AgentProductSpec::query() + ->whereIn('agent_product_id', $agent_products->pluck('id')->toArray()) + ->whereNotIn('product_spec_id', $product_spec_ids) + ->delete(); + + //将代理商规格中销售价小于供应商销售价的,将代理商的销售价设置为供应商的销售价 + foreach ($product_specs as $spec) { + AgentProductSpec::query() + ->where([['product_spec_id', '=', $spec->id], ['price', '<', $spec->price]]) + ->update(['price' => $spec->price]); + } + + //供应商新增,但代理商还没有的规格,同步给代理商 + foreach ($agent_products as $ap) { + //代理商已有的规格(如果代理商删除过的规格不再同步过来,这里加上withTrashed即可) + $ap_spec_ids = AgentProductSpec::where('agent_product_id', $ap->id)->pluck('product_spec_id')->toArray(); + + //代理商没有的规格 + $agent_no_spec_ids = array_diff($product_spec_ids, $ap_spec_ids); + + AgentProductSpec::query() + ->insert( + array_map(function ($v) use ($ap, $product_specs) { + $product_spec = $product_specs->find($v); + return [ + 'agent_product_id' => $ap->id, + 'product_spec_id' => $v, + 'name' => '', + 'date' => null, + 'stock' => $product_spec->stock, + 'original_price' => $product_spec->original_price, + 'price' => $product_spec->price, + ]; + }, $agent_no_spec_ids) + ); + } + }); + } +}