From fffe4e7508fc831da4faf1a528ad055ae6f3fb1e Mon Sep 17 00:00:00 2001 From: liapples Date: Mon, 18 Oct 2021 23:49:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E6=A0=BC=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MySQL_change.sql | 10 +++++++ .../Controllers/ProductController.php | 30 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/MySQL_change.sql b/MySQL_change.sql index fd2349e..790f4cb 100644 --- a/MySQL_change.sql +++ b/MySQL_change.sql @@ -319,3 +319,13 @@ ALTER TABLE `products` # 10:43 ‎2021/‎10/‎18 ALTER TABLE `industry_orders` CHANGE COLUMN `trade_deposit` `trade_deposit` DECIMAL(20,2) NOT NULL DEFAULT '0.00' COMMENT '需要扣除的交易金' AFTER `prepay_timeout`; + +# 15:15 ‎2021/‎10/‎18 +ALTER TABLE `agent_product_specs` + CHANGE COLUMN `product_spec_id` `product_spec_id` BIGINT(19) UNSIGNED NOT NULL COMMENT '供应商产品规格ID' AFTER `agent_product_id`; + +# 16:20 ‎2021/‎10/‎18 +ALTER TABLE `order_product_items` + CHANGE COLUMN `price` `price` DECIMAL(20,2) NOT NULL COMMENT '供应商成本价(成本单价*数量)' AFTER `num`; + + diff --git a/app/AdminSupplier/Controllers/ProductController.php b/app/AdminSupplier/Controllers/ProductController.php index 2f6aabc..1fbe229 100644 --- a/app/AdminSupplier/Controllers/ProductController.php +++ b/app/AdminSupplier/Controllers/ProductController.php @@ -6,8 +6,10 @@ use App\AdminSupplier\Repositories\Product; use App\Common\ProductStatus; use App\Models\AgentProduct; use App\Models\AgentProductItem; +use App\Models\AgentProductSpec; use App\Models\Category; use App\Models\DiyForm; +use App\Models\ProductSpec; use Dcat\Admin\Admin; use Dcat\Admin\Form; use Dcat\Admin\Form\NestedForm; @@ -328,7 +330,33 @@ class ProductController extends AdminController ]); } - //TODO 还需要规格同步到代理商 + $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) { + //暂时不处理新增的规格 + } + }*/ } })->deleting(function (Form $form) { //不允许删除非自己的数据