Browse Source

修改同步逻辑

develop
李可松 4 years ago
parent
commit
cd9f6d2121
  1. 14
      app/Admin/Controllers/ProductController.php
  2. 29
      app/AdminSupplier/Controllers/ProductController.php

14
app/Admin/Controllers/ProductController.php

@ -6,6 +6,8 @@ use App\Admin\Extensions\Grid\AuditProduct;
use App\Admin\Repositories\Product;
use App\Common\ProductStatus;
use App\Common\UserStatus;
use App\Models\AgentProduct;
use App\Models\AgentProductItem;
use App\Models\Category;
use App\Models\Supplier;
use Dcat\Admin\Form;
@ -138,6 +140,18 @@ class ProductController extends AdminController
$form->$k = '';
}
}
})->saved(function(Form $form) {
//如果是审核的产品,将产品同步到代理商产品
if ($form->isEditing() && $form->status == ProductStatus::ON_SALE) {
$ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray();
//同步信息到代理商产品,注:组合产品不同步
AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([
'title' => $form->title,
'pictures' => explode(',', $form->pictures),
'know' => $form->know,
'content' => $form->content,
]);
}
});
}
}

29
app/AdminSupplier/Controllers/ProductController.php

@ -122,34 +122,35 @@ class ProductController extends AdminController
}
//特殊字段处理
$form->hidden('supplier_id'); //表单没有的字段,必须加上这句才能重置值
$form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
$form->supplier_id = Admin::user()->id;
if ($form->isCreating()) {
$form->status = ProductStatus::UNAUDITED;
}
})->saved(function (Form $form, $result) {
if ($form->isEditing() && $result) {
$ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray();
DB::beginTransaction();
try {
$data = [
'title' => $form->title,
'pictures' => explode(',', $form->pictures),
'know' => $form->know,
'content' => $form->content,
];
//如果修改标题、价格、产品图片、旅游须知、产品详情,状态将变为未审核
if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) {
$form->model()->update(['status' => ProductStatus::UNAUDITED]);
//下架所有代理商产品
$data['status'] = ProductStatus::SOLD_OUT;
//下架所有代理商产品,未审核的产品,不能同步信息到代理商产品
AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update(['status' => ProductStatus::SOLD_OUT]);
} else {
//不需要审核的修改信息同步信息到代理商产品,注:组合产品不同步
AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([
'title' => $form->title,
'pictures' => explode(',', $form->pictures),
'know' => $form->know,
'content' => $form->content,
]);
}
//同步信息到代理商产品,注:组合产品不同步
AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update($data);
DB::commit();
return $form->response()->success('更新成功!');
return $form->response()->success('更新成功!')->script('history.go(-1)');
} catch (\Exception $exception) {
DB::rollBack();
return $form->response()->error($exception->getMessage());

Loading…
Cancel
Save