|
|
@ -5,6 +5,8 @@ namespace App\AdminSupplier\Controllers; |
|
|
use App\AdminSupplier\Repositories\Product; |
|
|
use App\AdminSupplier\Repositories\Product; |
|
|
use App\Common\ProductStatus; |
|
|
use App\Common\ProductStatus; |
|
|
use App\Models\AgentProduct; |
|
|
use App\Models\AgentProduct; |
|
|
|
|
|
use App\Models\AgentProductItem; |
|
|
|
|
|
use App\Models\AgentSetting; |
|
|
use App\Models\Category; |
|
|
use App\Models\Category; |
|
|
use Dcat\Admin\Admin; |
|
|
use Dcat\Admin\Admin; |
|
|
use Dcat\Admin\Form; |
|
|
use Dcat\Admin\Form; |
|
|
@ -12,6 +14,7 @@ use Dcat\Admin\Grid; |
|
|
use Dcat\Admin\Show; |
|
|
use Dcat\Admin\Show; |
|
|
use Dcat\Admin\Http\Controllers\AdminController; |
|
|
use Dcat\Admin\Http\Controllers\AdminController; |
|
|
use Illuminate\Support\Facades\DB; |
|
|
use Illuminate\Support\Facades\DB; |
|
|
|
|
|
use Illuminate\Support\Facades\Log; |
|
|
|
|
|
|
|
|
class ProductController extends AdminController |
|
|
class ProductController extends AdminController |
|
|
{ |
|
|
{ |
|
|
@ -124,12 +127,66 @@ class ProductController extends AdminController |
|
|
$form->supplier_id = Admin::user()->id; |
|
|
$form->supplier_id = Admin::user()->id; |
|
|
})->saved(function (Form $form, $result) { |
|
|
})->saved(function (Form $form, $result) { |
|
|
//下架代理商产品
|
|
|
//下架代理商产品
|
|
|
|
|
|
$id = $form->getKey(); |
|
|
if ($result) { |
|
|
if ($result) { |
|
|
$id = $form->getKey(); |
|
|
|
|
|
AgentProduct::where('product_id', $id) |
|
|
AgentProduct::where('product_id', $id) |
|
|
->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉
|
|
|
->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉
|
|
|
->update(['status' => ProductStatus::SOLD_OUT]); |
|
|
->update(['status' => ProductStatus::SOLD_OUT]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//自动上架
|
|
|
|
|
|
if ($form->isCreating()) { |
|
|
|
|
|
$agentIds = AgentProductItem::query()->withoutGlobalScope('orderById')->where('supplier_id',$form->supplier_id)->distinct()->pluck('agent_id'); |
|
|
|
|
|
foreach ($agentIds as $v) { |
|
|
|
|
|
//如果没开启自动上架 滚蛋
|
|
|
|
|
|
if(empty(AgentSetting::val($v,'auto_shelves'))) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
DB::beginTransaction(); |
|
|
|
|
|
try { |
|
|
|
|
|
$agentProduct = new AgentProduct(); |
|
|
|
|
|
$agentProduct->title = $form->title; |
|
|
|
|
|
$agentProduct->agent_id = $v; |
|
|
|
|
|
$agentProduct->product_id = $id; |
|
|
|
|
|
$agentProduct->product_ids = explode(',',$id); |
|
|
|
|
|
$agentProduct->stock = $form->stock; |
|
|
|
|
|
$agentProduct->status = 1; |
|
|
|
|
|
$agentProduct->pictures = explode(',',$form->pictures); |
|
|
|
|
|
$agentProduct->content = $form->content; |
|
|
|
|
|
$agentProduct->know = $form->know; |
|
|
|
|
|
//计算价格
|
|
|
|
|
|
$profit = AgentSetting::val($v, 'profit') ?? 0; |
|
|
|
|
|
$price = bcmul($form->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', $form->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' => $form->supplier_id, |
|
|
|
|
|
'agent_product_id' => $agentProduct->id, |
|
|
|
|
|
'product_id' => $id, |
|
|
|
|
|
]); |
|
|
|
|
|
DB::commit(); |
|
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
Log::error('自动上架失败::'.$e->getTraceAsString()); |
|
|
|
|
|
DB::rollBack(); |
|
|
|
|
|
return $form->response->error('自动上架失败,稍后重试或联系管理员!'.$e->getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
})->deleting(function (Form $form) { |
|
|
})->deleting(function (Form $form) { |
|
|
//不允许删除非自己的数据
|
|
|
//不允许删除非自己的数据
|
|
|
if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) { |
|
|
if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) { |
|
|
|