From 2a9887e632a0b7053f627cddb29b5b8847a15260 Mon Sep 17 00:00:00 2001 From: liu <1873441552@qq.com> Date: Mon, 17 Jun 2024 11:43:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=9A=84=E5=95=86=E6=88=B7=E5=88=86=E7=B1=BB=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=9F=AD=E4=BF=A1=E9=AA=8C=E8=AF=81=E7=A0=81=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../logic/store_product/StoreProductLogic.php | 103 +++++++++++++----- .../store_product_cate/StoreProductCate.php | 14 +++ 2 files changed, 91 insertions(+), 26 deletions(-) create mode 100644 app/common/model/store_product_cate/StoreProductCate.php diff --git a/app/admin/logic/store_product/StoreProductLogic.php b/app/admin/logic/store_product/StoreProductLogic.php index e88207b5f..e8ab690b5 100644 --- a/app/admin/logic/store_product/StoreProductLogic.php +++ b/app/admin/logic/store_product/StoreProductLogic.php @@ -9,6 +9,7 @@ use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_branch_product_attr_value\StoreBranchProductAttrValue; use app\common\model\store_category\StoreCategory; use app\common\model\store_product_attr_value\StoreProductAttrValue; +use app\common\model\store_product_cate\StoreProductCate; use app\common\model\system_store\SystemStore; use app\common\model\system_store_storage\SystemStoreStorage; use app\Request; @@ -48,7 +49,7 @@ class StoreProductLogic extends BaseLogic 'purchase' => $params['purchase'], 'rose' => $params['rose'], 'is_return' => $params['is_return'], - 'swap' => $params['swap']??0, + 'swap' => $params['swap'] ?? 0, ]; // if ($params['rose'] > 0) { // $rose_price = bcmul($params['cost'], $params['rose'], 2); @@ -94,6 +95,34 @@ class StoreProductLogic extends BaseLogic } } + public static function deleteRelatedData($cate_id) + { + $data_to_delete = StoreProductCate::where('cate_id', $cate_id)->select(); + foreach ($data_to_delete as $item) { + + if ($item['pid'] != 0) { + self::deleteRelatedData($item['pid']); + StoreProductCate::where('id', $item['id'])->update(['delete_time' => time()]); + } + + if($item['pid'] == 0 && in_array($item['count'],[0,1])){ + StoreProductCate::where('id', $item['id'])->update(['delete_time' => time()]); + } + + } + } + + // 递归减少分类数据的count值 + public static function decreaseCount($cate_id) + { + $data_to_decrease = StoreProductCate::where('cate_id', $cate_id)->select(); + foreach ($data_to_decrease as $item) { + if ($item['pid'] != 0) { + self::decreaseCount($item['pid']); + } + StoreProductCate::where('id', $item['id'])->update(['count' => $item['count'] - 1]); + } + } /** * @notes 编辑商品列表 @@ -106,30 +135,51 @@ class StoreProductLogic extends BaseLogic { Db::startTrans(); try { - $data = [ - 'store_name' => $params['store_name'], - 'image' => $params['image'], - 'bar_code' => $params['bar_code'] ?? '', - 'cate_id' => $params['cate_id'], - 'unit' => $params['unit'], - 'stock' => $params['stock'], - 'cost' => $params['cost'], - 'purchase' => $params['purchase'], - 'rose' => $params['rose'], - 'is_return' => $params['is_return'], - 'price' => $params['price'], - 'vip_price' => $params['vip_price'], - 'cost' => $params['cost'], - 'swap' => $params['swap']??0, + $data = [ + 'store_name' => $params['store_name'], + 'image' => $params['image'], + 'bar_code' => $params['bar_code'] ?? '', + 'cate_id' => $params['cate_id'], + 'unit' => $params['unit'], + 'stock' => $params['stock'], + 'cost' => $params['cost'], + 'purchase' => $params['purchase'], + 'rose' => $params['rose'], + 'is_return' => $params['is_return'], + 'price' => $params['price'], + 'vip_price' => $params['vip_price'], + 'swap' => $params['swap'] ?? 0, + + ]; + + StoreProduct::where('id', $params['id'])->update($data); + $old_cate = StoreBranchProduct::where('product_id', $params['id'])->field('cate_id,store_id') + ->select(); + + // 获取分类ID + foreach ($old_cate as $vv) { + $related_data = Db::name('store_product_cate')->where('cate_id', $vv['cate_id'])->select(); + //删除之前的分类 + foreach ($related_data as $value) { + if ($value['count'] == 1) { + self::deleteRelatedData($value['cate_id']); + } elseif ($value['count'] > 1) { + self::decreaseCount($value['cate_id']); + } + //新增对应的分类 + self::updateGoodsclass($params['cate_id'], $value['store_id']); + } + } + + + //修改 + StoreBranchProduct::where('product_id', $params['id'])->update([ + 'price' => $params['price'], 'vip_price' => $params['vip_price'], + 'cost' => $params['cost'], 'cate_id' => $params['cate_id'] + ]); - ]; - StoreProduct::where('id', $params['id'])->update($data); - StoreBranchProduct::where('product_id', $params['id'])->update([ - 'price' => $params['price'], 'vip_price' => $params['vip_price'], - 'cost' => $params['cost'] - ]); Db::commit(); - return true; + return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); @@ -148,7 +198,7 @@ class StoreProductLogic extends BaseLogic public static function delete(array $params): bool { $res = StoreProduct::destroy($params['id']); - StoreBranchProduct::where('product_id', $params['id'])->update(['delete_time'=>time()]); + StoreBranchProduct::where('product_id', $params['id'])->update(['delete_time' => time()]); return $res; } @@ -192,7 +242,7 @@ class StoreProductLogic extends BaseLogic self::cate_update($cate_id, $two['id'], $store_id, 3); self::cate_update($two['id'], $two['pid'], $store_id, 2); self::cate_update($two['pid'], 0, $store_id, 1); - }else{ + } else { if ($one['pid'] == 0) { self::cate_update($one['id'], 0, $store_id, 1); } else { @@ -200,7 +250,7 @@ class StoreProductLogic extends BaseLogic self::cate_update($one['pid'], 0, $store_id, 1); } } - } + } } } @@ -213,6 +263,7 @@ class StoreProductLogic extends BaseLogic Db::name('store_product_cate')->insert(['pid' => $pid, 'store_id' => $store_id, 'cate_id' => $cate_id, 'count' => 1, 'level' => $level, 'create_time' => time(), 'update_time' => time()]); } } + /** * 复制商品到门店 */ diff --git a/app/common/model/store_product_cate/StoreProductCate.php b/app/common/model/store_product_cate/StoreProductCate.php new file mode 100644 index 000000000..41338c9e7 --- /dev/null +++ b/app/common/model/store_product_cate/StoreProductCate.php @@ -0,0 +1,14 @@ + Date: Mon, 17 Jun 2024 11:52:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/lists/cate/CateLists.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/api/lists/cate/CateLists.php b/app/api/lists/cate/CateLists.php index 87a8fb73c..e924419bf 100644 --- a/app/api/lists/cate/CateLists.php +++ b/app/api/lists/cate/CateLists.php @@ -46,6 +46,7 @@ class CateLists extends BaseAdminDataLists implements ListsSearchInterface $level = Request()->get('level', 1); $this->searchWhere[] = ['level', '=', $level]; $this->searchWhere[] = ['count', '>', 0]; + $this->searchWhere[] = ['delete_time', '=', null]; $cate_arr = Db::name('store_product_cate')->where($this->searchWhere)->column('cate_id'); $lists = []; if ($cate_arr) {