3 changed files with 103 additions and 54 deletions
-
54app/AdminSupplier/Controllers/ProductController.php
-
102app/Providers/ProductServiceProvider.php
-
1config/app.php
@ -0,0 +1,102 @@ |
|||
<?php |
|||
|
|||
namespace App\Providers; |
|||
|
|||
use App\Common\ProductStatus; |
|||
use App\Models\AgentProduct; |
|||
use App\Models\AgentProductItem; |
|||
use App\Models\AgentSetting; |
|||
use App\Models\Category; |
|||
use App\Models\Product; |
|||
use Illuminate\Database\Eloquent\Model; |
|||
use Illuminate\Support\Facades\DB; |
|||
use Illuminate\Support\Facades\Log; |
|||
use Illuminate\Support\ServiceProvider; |
|||
|
|||
class ProductServiceProvider extends ServiceProvider |
|||
{ |
|||
/** |
|||
* Register services. |
|||
* |
|||
* @return void |
|||
*/ |
|||
public function register() |
|||
{ |
|||
//
|
|||
} |
|||
|
|||
/** |
|||
* Bootstrap services. |
|||
* |
|||
* @return void |
|||
*/ |
|||
public function boot() |
|||
{ |
|||
Product::updated(function ($product) { |
|||
if ($product->isDirty('status') && $product->status == ProductStatus::ON_SALE) { |
|||
//自动上架
|
|||
|
|||
$agentIds = AgentProductItem::query() |
|||
->withoutGlobalScope('orderById') |
|||
->where('supplier_id', $product->supplier_id) |
|||
->distinct() |
|||
->pluck('agent_id'); |
|||
foreach ($agentIds as $v) { |
|||
//如果没开启自动上架 滚蛋
|
|||
if (empty(AgentSetting::val($v, 'auto_shelves'))) { |
|||
continue; |
|||
} |
|||
//如果已经有这个产品 滚蛋
|
|||
if(AgentProductItem::query()->where(['product_id' => $product->supplier_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), 2); |
|||
|
|||
$agentProduct->price = $price; |
|||
$agentProduct->original_price = $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, |
|||
]); |
|||
DB::commit(); |
|||
} catch (\Exception $e) { |
|||
Log::error('自动上架失败::' . $e->getTraceAsString()); |
|||
DB::rollBack(); |
|||
return $product->response->error('自动上架失败,稍后重试或联系管理员!' . $e->getMessage()); |
|||
} |
|||
} |
|||
} |
|||
|
|||
}); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue