From 259481de46a5e7706be8f3860fa7006a9fcedb9b Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Thu, 16 Nov 2023 19:16:10 +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 --- app/event.php | 1 + app/listener/OrderTask.php | 147 +++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 app/listener/OrderTask.php diff --git a/app/event.php b/app/event.php index d63961fe..b6928bde 100644 --- a/app/event.php +++ b/app/event.php @@ -75,6 +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], ], 'subscribe' => [], diff --git a/app/listener/OrderTask.php b/app/listener/OrderTask.php new file mode 100644 index 00000000..ea076675 --- /dev/null +++ b/app/listener/OrderTask.php @@ -0,0 +1,147 @@ +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); + + // 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()); + } + } + + public function import($product, $order) + { + $mer_id = Db::name('store_service')->where('uid', $order['uid'])->where('status', 1)->value('mer_id'); + if ($mer_id == 0) { + 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('采购导入商品:已经导入过该商品了'); + return false; + } + $find['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(); + } else { + + $find['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(); + + $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['attrValue']['stock'] = $product['refund_num']; + + $find['content'] = Db::name('store_product_content')->where('product_id', $find['product_id'])->value('content'); + $find['is_show'] = 0; + $find['mer_id'] = $mer_id; + $find['temp_id'] = ""; + $find['give_coupon_ids'] = []; + $find['params'] = []; + $find['extend'] = []; + $find['param_temp_id'] = []; + $find['mer_labels'] = []; + $find['delivery_way'] = [0 => "2"]; + $find["guarantee_template_id"] = ""; + $find['product_type'] = 0; + $find['mer_cate_id'] = [0 => 0]; + $find['is_used'] = 1; + $find['status'] = 1; + $find['mer_status'] = 1; + $find['source_product_id'] = $product['product_id']; + $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 + ]; + $this->purchase_record($data); + } + } + } + + + public function purchase_record($data){ + $model = new PurchaseRecord(); + $data = [ + 'order_id' => $data['order_id'] ?? 0, + 'order_product_id' => $data['order_product_id'] ?? 0, + 'product_id' => $data['product_id'], + 'number' => $data['number'], + 'order_unique' => $data['order_unique'] ?? '', + 'unique' => $data['unique'], + 'price' => $data['price'], + 'mer_id' => $data['mer_id'], + 'supplier_mer_id' => $data['supplierMerId'], + ]; + $model->save($data); + } +}