From 637047894ef79b766844ca60567d32047461c9c1 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Sat, 18 Nov 2023 13:42:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../store/product/SpuRepository.php | 2 +- app/listener/OrderTake.php | 38 +++++++++++-------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/app/common/repositories/store/product/SpuRepository.php b/app/common/repositories/store/product/SpuRepository.php index f77195f7..27c9918c 100644 --- a/app/common/repositories/store/product/SpuRepository.php +++ b/app/common/repositories/store/product/SpuRepository.php @@ -90,7 +90,7 @@ class SpuRepository extends BaseRepository 'keyword' => $param['keyword'] ?? '', 'image' => $param['image'], 'price' => $param['price'], - 'status' => 0, + 'status' => $param['status']??0, 'rank' => $param['rank'] ?? 0, 'temp_id' => $param['temp_id'], 'sort' => $param['sort'] ?? 0, diff --git a/app/listener/OrderTake.php b/app/listener/OrderTake.php index ad18a354..7fafc292 100644 --- a/app/listener/OrderTake.php +++ b/app/listener/OrderTake.php @@ -39,12 +39,7 @@ class OrderTake $product_arr = Db::name('store_order_product')->where('order_id', $order['order_id'])->where('is_refund', 'in', [0, 2])->field('product_id,product_sku,refund_num')->select(); foreach ($product_arr as $k => $v) { $this->import($v, $order); - - // app(ProductRepository::class)->create($find, 0); } - // $productId = $this->import($params['order_product_id'], request()->userInfo()); - // $product = $this->get($productId); - // $attrValue = ProductAttrValue::where('mer_id', $merId)->where('product_id', $productId)->find(); } } catch (\Exception $e) { Log::error($e->getMessage() . 'lien:' . $e->getLine()); @@ -73,6 +68,7 @@ class OrderTake Log::error('采购导入商品:已经导入过该规格了'); return false; } + /**查询原始商品的规格 */ $attrValue = Db::name('store_product_attr_value')->where(['product_id' => $find['product_id']]) ->where('unique', $product['product_sku']) ->field('image,price,cost,ot_price,svip_price,stock,bar_code,weight,volume,detail,sku') @@ -81,18 +77,21 @@ class OrderTake }) ->find(); $find['attr'] = []; + $is_update=false; if ($attrValue['detail']) { foreach ($attrValue['detail'] as $kk => $vv) { - - $attr_values = Db::name('store_product_attr')->where('product_id', $exist['product_id'])->where('attr_name', $kk) - ->value('attr_values'); - if ($attr_values) { + /**查询当前规格是否存在 */ + $attr_values_find = Db::name('store_product_attr')->where('product_id', $exist['product_id'])->where('attr_name', $kk) + ->find(); + if ($attr_values_find) { + $attr_values= $attr_values_find['attr_values']; $attr_values = explode('-!-', $attr_values); if (!in_array($vv, $attr_values)) { $attr_values[] = $vv; $attr_values = implode('-!-', $attr_values); Db::name('store_product_attr')->where('product_id', $exist['product_id'])->where('attr_name', $kk)->update(['attr_values' => $attr_values]); } + $is_update=true; } else { $attr_values = $vv; @@ -105,13 +104,20 @@ class OrderTake } } } - $attrValue['stock'] = $product['refund_num']; - $unique = app(ProductRepository::class)->setUnique($exist['product_id'], $attrValue['sku'], 0); - $attrValue['unique'] = $unique; - $attrValue['detail'] = json_encode($attrValue['detail']); - $attrValue['product_id'] = $exist['product_id']; - $attrValue['mer_id'] = $mer_id; - Db::name('store_product_attr_value')->insert($attrValue); + /**规格新增还是更新 */ + if($is_update==false){ + $attrValue['stock'] = $product['refund_num']; + $unique = app(ProductRepository::class)->setUnique($exist['product_id'], $attrValue['sku'], 0); + $attrValue['unique'] = $unique; + $attrValue['detail'] = json_encode($attrValue['detail']); + $attrValue['product_id'] = $exist['product_id']; + $attrValue['mer_id'] = $mer_id; + Db::name('store_product_attr_value')->insert($attrValue); + }else{ + Db::name('store_product_attr_value')->where(['unique'=>$attr_values_find['unique'],'product_id', + $attr_values_find['product_id'],'mer_id'=>$mer_id,])->update(['stock'=>$product['refund_num']+$attr_values_find['stock']]); + } + Db::name('store_product')->where('product_id',$exist['product_id'])->update(['stock'=>$product['refund_num']+$exist['stock']]); $data = [ 'order_id' => $order['order_id'], 'order_product_id' => $order['product_id'],