This commit is contained in:
mkm 2023-11-16 19:16:10 +08:00
parent f2dee282cc
commit 259481de46
2 changed files with 148 additions and 0 deletions

View File

@ -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' => [],

147
app/listener/OrderTask.php Normal file
View File

@ -0,0 +1,147 @@
<?php
declare(strict_types=1);
namespace app\listener;
use app\common\dao\store\order\StoreCartDao;
use app\common\dao\system\merchant\MerchantDao;
use app\common\model\system\merchant\Merchant;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\system\merchant\FinancialRecordRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use crmeb\utils\DingTalk;
use think\facade\Db;
use think\facade\Log;
use app\common\model\store\product\PurchaseRecord;
/**
* 收货后的逻辑
*/
class OrderTask
{
public $totalAmount;
public $event;
public $finance = [];
public $streetId;
public $financeSn;
public $index = 1;
public $remain;
public function handle($event)
{
try {
$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);
// 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);
}
}