Browse Source

增加组合产品需要审核,修改AgentProductItem的处理逻辑

develop
李可松 4 years ago
parent
commit
d129e72c50
  1. 72
      app/AdminAgent/Controllers/AgentProductController.php

72
app/AdminAgent/Controllers/AgentProductController.php

@ -163,6 +163,7 @@ class AgentProductController extends AdminController
$form->radio('type')
->options(['单品销售', '组合销售'])
->default(0)->required()
->help('单品销售无需审核,组合销售需要审核才能上架')
->when(0, function (Form $form) {
/** 单品销售 **/
$form->selectTable('product_id', '选择产品')
@ -186,8 +187,6 @@ class AgentProductController extends AdminController
$form->editor('know');
$form->editor('content');
});
$form->text('title', '产品名称');
$form->text('price')->required();
$form->text('original_price')->required();
$form->text('sale')->default(0);
@ -226,10 +225,10 @@ class AgentProductController extends AdminController
->model(Guide::class, 'id', 'name');
}
$form->text('earnest')->default(0)->help('单位:元。不输入或输入 0 则不支持定金支付,必须和定金超时时间同时设置才会生效');
$form->text('earnest_timeout')->default(0)->help('单位:分钟。超过这个时间未支付,订单将自动关闭');
$form->text('deposit')->default(0)->help('单位:元。不输入或输入 0 则不支持订金支付,必须和订金超时时间同时设置才会生效');
$form->text('deposit_timeout')->default(0)->help('单位:分钟。超过这个时间未支付,订单将自动关闭');
$form->number('earnest')->width(2)->default(0)->help('单位:元。不输入或输入 0 则不支持定金支付,必须和定金超时时间同时设置才会生效');
$form->number('earnest_timeout')->width(2)->default(0)->help('单位:分钟。超过这个时间未支付,订单将自动关闭');
$form->number('deposit')->default(0)->help('单位:元。不输入或输入 0 则不支持订金支付,必须和订金超时时间同时设置才会生效');
$form->number('deposit_timeout')->default(0)->help('单位:分钟。超过这个时间未支付,订单将自动关闭');
})->saving(function (Form $form) {
//不允许修改非自己的数据
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
@ -329,12 +328,15 @@ class AgentProductController extends AdminController
//处理特殊字段
$form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
$form->agent_id = $agent_id;
if (array_key_exists('status', $form->input())) {
$form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
}
if (array_key_exists('guide_id', $form->input())) {
$form->guide_id = $form->guide_id ?? 0;
}
//组合销售需要审核,编辑时是否需要审核在saved里面判断
if ($form->isCreating() && $form->type == 1) {
$form->status = ProductStatus::UNAUDITED;
} else {
$form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
}
//订金
if ($form->earnest <= 0 || $form->earnest_timeout <= 0) {
@ -370,40 +372,36 @@ class AgentProductController extends AdminController
})->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();
$product = Product::whereIn('id', $product_ids)->orderBy('id')
->get(['id AS product_id', 'supplier_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]);
}
$insert_data = array_map(function ($v) use ($agent_product_id) {
$v['agent_product_id'] = $agent_product_id;
$v['agent_id'] = Admin::user()->id;
return $v;
}, $product);
//组合产品编辑关键字段需要审核
if ($form->isEditing() && $form->model()->wasChanged(['title', 'pictures', 'know', 'content'])) {
$form->model()->update(['status' => ProductStatus::UNAUDITED]);
}
//将剩余(新增)的product_id保存
if ($insert_data) {
if ($form->isCreating()) {
AgentProductItem::insert($insert_data);
} else if ($form->isEditing()) {
//删除原来有,但现在没有的数据
AgentProductItem::query()
->where('agent_product_id', $agent_product_id)
->whereNotIn('product_id', $product_ids)->delete();
//插入原来没有,但是现在有的数据
foreach ($insert_data as $v) {
AgentProductItem::query()->firstOrCreate(
['agent_product_id' => $agent_product_id, 'product_id' => $v['product_id']],
$v
);
}
}
})->deleting(function (Form $form) {

Loading…
Cancel
Save