diff --git a/app/admin/logic/store_product/StoreProductLogic.php b/app/admin/logic/store_product/StoreProductLogic.php index 3ca4152de..a26b5bf96 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; @@ -95,6 +96,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 编辑商品列表 @@ -124,14 +153,36 @@ class StoreProductLogic extends BaseLogic 'batch' => $params['batch'], 'swap' => $params['swap']??0, - ]; - 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'] - ]); + ]; + + 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'] + ]); + Db::commit(); - return true; + return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); @@ -150,7 +201,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; } @@ -194,7 +245,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 { @@ -202,7 +253,7 @@ class StoreProductLogic extends BaseLogic self::cate_update($one['pid'], 0, $store_id, 1); } } - } + } } } @@ -215,6 +266,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/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) { 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 @@ +