diff --git a/app/event.php b/app/event.php index b6928bde..7ae53a8c 100644 --- a/app/event.php +++ b/app/event.php @@ -75,7 +75,7 @@ return [ 'refund.after'=>[\app\listener\AfterRefund::class], 'refund.deliver'=>[\app\listener\DeliverRefund::class], 'order.create'=>[\app\listener\OrderCreate::class], - 'order.task'=>[\app\listener\OrderTask::class], + 'order.take'=>[\app\listener\OrderTake::class], ], 'subscribe' => [], diff --git a/app/listener/OrderTask.php b/app/listener/OrderTake.php similarity index 61% rename from app/listener/OrderTask.php rename to app/listener/OrderTake.php index ea076675..ad18a354 100644 --- a/app/listener/OrderTask.php +++ b/app/listener/OrderTake.php @@ -15,11 +15,12 @@ use crmeb\utils\DingTalk; use think\facade\Db; use think\facade\Log; use app\common\model\store\product\PurchaseRecord; +use app\common\model\system\Extend; /** * 收货后的逻辑 */ -class OrderTask +class OrderTake { public $totalAmount; @@ -36,10 +37,9 @@ class OrderTask $order = $event['order']; if ($order['source'] == 200 && $order['activity_type'] == 98) { $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) { - $product_id = $this->import($v, $order); - + $this->import($v, $order); + // app(ProductRepository::class)->create($find, 0); } // $productId = $this->import($params['order_product_id'], request()->userInfo()); @@ -58,26 +58,67 @@ class OrderTask Log::error('采购导入商品:商户不存在'); return false; } + /**原始商品数据 */ $find = Db::name('store_product')->where('product_id', $product['product_id'])->find(); if ($find) { if (!in_array($find['product_type'], [0, 98])) { Log::error('采购导入商品:不是普通商品'); return false; } + /**查询是否导入过该商品 */ $exist = Db::name('store_product')->where('source_product_id', $product['product_id'])->where('mer_id', $mer_id)->find(); if ($exist) { $store_product_attr_value = Db::name('store_product_attr_value')->where('product_id', $exist['product_id'])->where('unique', $product['product_sku'])->find(); if ($store_product_attr_value) { - Log::error('采购导入商品:已经导入过该商品了'); + Log::error('采购导入商品:已经导入过该规格了'); return false; } - $find['attrValue'] = Db::name('store_product_attr_value')->where(['product_id' => $find['product_id']]) + $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') ->withAttr('detail', function ($value) { return empty($value) ? [] : json_decode($value, true); }) ->find(); + $find['attr'] = []; + 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 = 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]); + } + } else { + $attr_values = $vv; + + Db::name('store_product_attr')->insert( + [ + 'product_id' => $exist['product_id'], + 'attr_name' => $kk, 'attr_values' => $attr_values + ] + ); + } + } + } + $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); + 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'], + 'product_id' => $exist['product_id'], 'number' => $product['refund_num'], 'order_unique' => $product['product_sku'], + 'price' => $attrValue['price'], 'supplierMerId' => $order['mer_id'], 'mer_id' => $mer_id, 'unique' => $unique + ]; + $this->purchase_record($data); } else { $find['attrValue'] = Db::name('store_product_attr_value')->where(['product_id' => $find['product_id']]) @@ -88,15 +129,13 @@ class OrderTask }) ->find(); - $attr = Db::name('store_product_attr')->where('product_id', $find['product_id'])->field('attr_name,attr_values,type')->select(); - foreach ($attr as $item) { - $attr_values = explode('-!-', $item['attr_values']); - foreach ($attr_values as $value) { - if ($value == $find['attrValue']['sku']) { - $find['attr'][] = ['attr_name' => $item['attr_name'], 'detail' => $value]; - } + $find['attr'] = []; + if ($find['attrValue']['detail']) { + foreach ($find['attrValue']['detail'] as $kk => $vv) { + $find['attr'][] = ['attr_name' => $kk, 'detail' => $vv]; } } + $find['attrValue']['stock'] = $product['refund_num']; $find['content'] = Db::name('store_product_content')->where('product_id', $find['product_id'])->value('content'); @@ -119,17 +158,19 @@ class OrderTask $find['slider_image'] = explode(',', $find['slider_image']); unset($find['product_id'], $find['create_time']); $productId = app(ProductRepository::class)->create($find, 0); - $data=['order_id'=>$order['order_id'],'order_product_id'=>$order['order_product_id'], - 'product_id'=>$productId,'number'=>$product['refund_num'],'order_unique'=>$product['product_sku'], - 'price'=> $find['attrValue']['price'],'supplierMerId'=> $order['mer_id'],'mer_id'=>$mer_id - ]; + $data = [ + 'order_id' => $order['order_id'], 'order_product_id' => $order['product_id'], + 'product_id' => $productId, 'number' => $product['refund_num'], 'order_unique' => $product['product_sku'], + 'price' => $find['attrValue']['price'], 'supplierMerId' => $order['mer_id'], 'mer_id' => $mer_id + ]; $this->purchase_record($data); } } } - public function purchase_record($data){ + public function purchase_record($data) + { $model = new PurchaseRecord(); $data = [ 'order_id' => $data['order_id'] ?? 0, @@ -137,7 +178,7 @@ class OrderTask 'product_id' => $data['product_id'], 'number' => $data['number'], 'order_unique' => $data['order_unique'] ?? '', - 'unique' => $data['unique'], + 'unique' => $data['unique'] ?? '', 'price' => $data['price'], 'mer_id' => $data['mer_id'], 'supplier_mer_id' => $data['supplierMerId'],