|
|
@ -8,6 +8,7 @@ use App\AdminAgent\Renderable\SelectUser; |
|
|
use App\AdminAgent\Repositories\AgentProduct; |
|
|
use App\AdminAgent\Repositories\AgentProduct; |
|
|
use App\Common\AgentType; |
|
|
use App\Common\AgentType; |
|
|
use App\Common\ProductStatus; |
|
|
use App\Common\ProductStatus; |
|
|
|
|
|
use App\Models\AgentProductItem; |
|
|
use App\Models\Category; |
|
|
use App\Models\Category; |
|
|
use App\Models\Channel; |
|
|
use App\Models\Channel; |
|
|
use App\Models\Guide; |
|
|
use App\Models\Guide; |
|
|
@ -254,6 +255,45 @@ class AgentProductController extends AdminController |
|
|
if ($form->repository()->model()->where($where)->exists()) { |
|
|
if ($form->repository()->model()->where($where)->exists()) { |
|
|
return $form->response()->error('该产品已经存在,请勿重复发布'); |
|
|
return $form->response()->error('该产品已经存在,请勿重复发布'); |
|
|
} |
|
|
} |
|
|
|
|
|
})->saved(function (Form $form) { |
|
|
|
|
|
/** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/ |
|
|
|
|
|
$product_ids = explode(',', $form->product_ids); |
|
|
|
|
|
$product = Product::whereIn('id', $product_ids)->orderBy('id')->get(['id', 'supplier_id'])->toArray(); |
|
|
|
|
|
|
|
|
|
|
|
$agent_product_id = $form->getKey(); |
|
|
|
|
|
$insert_data = []; |
|
|
|
|
|
foreach ($product as $k => &$v) { |
|
|
|
|
|
$insert_data[$v['id']] = [ |
|
|
|
|
|
'agent_product_id' => $agent_product_id, |
|
|
|
|
|
'agent_id' => Admin::user()->id, |
|
|
|
|
|
'supplier_id' => $v['supplier_id'], |
|
|
|
|
|
'product_id' => $v['id'], |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($form->isCreating()) { |
|
|
|
|
|
AgentProductItem::insert($insert_data); |
|
|
|
|
|
} else if ($form->isEditing()) { |
|
|
|
|
|
//先查出来原来的数据
|
|
|
|
|
|
$old_data = AgentProductItem::where('agent_product_id', $agent_product_id)->pluck('id', 'product_id')->toArray(); |
|
|
|
|
|
|
|
|
|
|
|
//新ID在原来数据里面没有的,删除掉
|
|
|
|
|
|
foreach ($old_data as $k => $v) { |
|
|
|
|
|
//删除已经删除掉的product_id
|
|
|
|
|
|
if (!in_array($k, $product_ids)) { |
|
|
|
|
|
AgentProductItem::query()->find($v)->delete(); |
|
|
|
|
|
} |
|
|
|
|
|
//将新数据和旧数据中都存在的product_id unset掉
|
|
|
|
|
|
if (array_key_exists($k, $insert_data)) { |
|
|
|
|
|
unset($insert_data[$k]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//将剩余(新增)的product_id保存
|
|
|
|
|
|
if ($insert_data) { |
|
|
|
|
|
AgentProductItem::insert($insert_data); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
})->deleting(function (Form $form) { |
|
|
})->deleting(function (Form $form) { |
|
|
//不允许删除非自己的数据
|
|
|
//不允许删除非自己的数据
|
|
|
if ($form->model()[0]['agent_id'] != Admin::user()->id) { |
|
|
if ($form->model()[0]['agent_id'] != Admin::user()->id) { |
|
|
|