Browse Source

通过队列实现自动上架

master
李可松 4 years ago
parent
commit
38971c7132
  1. 134
      app/Jobs/AgentProductShelve.php
  2. 86
      app/Providers/ProductServiceProvider.php

134
app/Jobs/AgentProductShelve.php

@ -0,0 +1,134 @@
<?php
namespace App\Jobs;
use App\Models\Agent;
use App\Models\AgentProduct;
use App\Models\AgentProductItem;
use App\Models\AgentSetting;
use App\Models\Category;
use App\Models\Product;
use App\Models\ProductSpec;
use App\Service\SmsService;
use App\Traits\SmsTraits;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* 代理商产品自动上架
*/
class AgentProductShelve implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* 任务可尝试的次数
* @var int
*/
public $tries = 3;
/**
* 任务失败前允许的最大异常数
* @var int
*/
public $maxExceptions = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(private int $agent_id, private Product $product_id)
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$agent_id = $this->agent_id;
$product = Product::find($this->product_id);
//如果没开启自动上架 滚蛋
if (empty(AgentSetting::val($agent_id, 'auto_shelves'))) {
return;
}
//如果已经有这个产品 滚蛋
if(AgentProductItem::query()->where(['product_id' => $product->id,'agent_id' => $agent_id])->exists()) {
return;
}
//啥有没有 自动添加商品
DB::beginTransaction();
try {
$agentProduct = new AgentProduct();
$agentProduct->title = $product->title;
$agentProduct->agent_id = $agent_id;
$agentProduct->product_id = $product->id;
$agentProduct->product_ids = $product->id;
$agentProduct->stock = $product->stock;
$agentProduct->status = 1;
$agentProduct->pictures = $product->pictures;
$agentProduct->content = $product->content;
$agentProduct->know = $product->know;
$agentProduct->price = $product->price;
$agentProduct->original_price = $product->original_price;
//自动添加分类
$autoCategory = AgentSetting::val($agent_id, 'auto_category') ?? 0;
if (!empty($autoCategory)) {
$categoryName = Category::query()->where('id', $product->category_id)->value('name');
$category = Category::query()->firstOrCreate(['agent_id' => $agent_id, 'name' => $categoryName]);
$agentProduct->category_id = $category->id;
}
$agentProduct->save();
//维护关联表
AgentProductItem::query()->create([
'agent_id' => $agent_id,
'supplier_id' => $product->supplier_id,
'agent_product_id' => $agentProduct->id,
'product_id' => $product->id,
]);
//规格
$productSpec = ProductSpec::query()->where('product_id',$product->id)->get();
if (!empty($productSpec)) {
$agentSpecArr = [];
foreach ($productSpec as $spec) {
$agentSpecArr[] = [
'agent_product_id' => $agentProduct->id,
'product_spec_id' => $spec->id,
'stock' => $spec->stock,
'original_price' => $spec->original_price,
'price' => $spec->price,
];
}
$agentProduct->spec()->createMany($agentSpecArr);
}
DB::commit();
Log::info($agent_id . "自动上架{$product->id}成功!");
if(env('SMS_SWITCH','') == true) {
$phone = Agent::query()->where('id',$agent_id)->value('contact_mobile');
(new SmsService)->send('auto_shelves',[$product->supplier->name,now(),SmsTraits::$systeaNameText['agent']],[$phone]);
}
} catch (\Exception $e) {
DB::rollBack();
Log::error('自动上架失败::' . $e->getTraceAsString());
throw new \Exception('自动上架失败::' . $e->getTraceAsString());
}
}
}

86
app/Providers/ProductServiceProvider.php

@ -5,17 +5,10 @@ namespace App\Providers;
use App\Common\AgentType; use App\Common\AgentType;
use App\Common\ProductStatus; use App\Common\ProductStatus;
use App\Common\UserStatus; use App\Common\UserStatus;
use App\Jobs\AgentProductShelve;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\AgentProductItem;
use App\Models\AgentSetting;
use App\Models\Category;
use App\Models\Agent; use App\Models\Agent;
use App\Models\Product; use App\Models\Product;
use App\Models\ProductSpec;
use App\Service\SmsService;
use App\Traits\SmsTraits;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class ProductServiceProvider extends ServiceProvider class ProductServiceProvider extends ServiceProvider
@ -57,81 +50,8 @@ class ProductServiceProvider extends ServiceProvider
['status', '=', UserStatus::NORMAL] ['status', '=', UserStatus::NORMAL]
])->pluck('id'); ])->pluck('id');
foreach ($agentIds as $v) {
//如果没开启自动上架 滚蛋
if (empty(AgentSetting::val($v, 'auto_shelves'))) {
continue;
}
//如果已经有这个产品 滚蛋
if(AgentProductItem::query()->where(['product_id' => $product->id,'agent_id' => $v])->exists()){
continue;
}
//啥有没有 自动添加商品
DB::beginTransaction();
try {
$agentProduct = new AgentProduct();
$agentProduct->title = $product->title;
$agentProduct->agent_id = $v;
$agentProduct->product_id = $product->id;
$agentProduct->product_ids = $product->id;
$agentProduct->stock = $product->stock;
$agentProduct->status = 1;
$agentProduct->pictures = $product->pictures;
$agentProduct->content = $product->content;
$agentProduct->know = $product->know;
//计算价格
// $profit = AgentSetting::val($v, 'profit') ?? 0;
// $price = bcmul($product->price, bcdiv($profit + 100, 100, 6), 6);
$agentProduct->price = $product->price;
$agentProduct->original_price = $product->original_price;
//自动添加分类
$autoCategory = AgentSetting::val($v, 'auto_category') ?? 0;
if (!empty($autoCategory)) {
$categoryName = Category::query()->where('id', $product->category_id)->value('name');
$category = Category::query()->firstOrCreate(['agent_id' => $v, 'name' => $categoryName]);
$agentProduct->category_id = $category->id;
}
$agentProduct->save();
//维护关联表
$agentProductItem = AgentProductItem::query()->create([
'agent_id' => $v,
'supplier_id' => $product->supplier_id,
'agent_product_id' => $agentProduct->id,
'product_id' => $product->id,
]);
//规格
$productSpec = ProductSpec::query()->where('product_id',$product->id)->get();
if (!empty($productSpec)) {
$agentSpecArr = [];
foreach ($productSpec as $spec) {
$agentSpecArr[] = [
'agent_product_id' => $agentProduct->id,
'product_spec_id' => $spec->id,
'stock' => $spec->stock,
'original_price' => $spec->original_price,
'price' => $spec->price,
];
}
$agentProduct->spec()->createMany($agentSpecArr);
}
DB::commit();
Log::info($v . "自动上架{$product->id}成功!");
if(env('SMS_SWITCH','') == true) {
$phone = Agent::query()->where('id',$v)->value('contact_mobile');
(new SmsService)->send('auto_shelves',[$product->supplier->name,now(),SmsTraits::$systeaNameText['agent']],[$phone]);
}
} catch (\Exception $e) {
Log::error('自动上架失败::' . $e->getTraceAsString());
DB::rollBack();
throw new \Exception('自动上架失败::' . $e->getTraceAsString());
}
foreach ($agentIds as $agent_id) {
AgentProductShelve::dispatch($agent_id, $product->id);
} }
/** 自动上架 END */ /** 自动上架 END */
} }

Loading…
Cancel
Save