From 5814642bfb910bffc098f5e494f6cea7531259da Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Thu, 13 Jan 2022 11:20:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=A1=E7=90=86=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Actions/Grid/DealerProductManageRule.php | 38 ++++++ .../Controllers/DealerProductController.php | 4 + app/Admin/Forms/DealerProductManageRule.php | 109 ++++++++++++++++++ app/Models/DealerProduct.php | 5 + app/Models/DealerProductManageRule.php | 15 +++ ...eate_dealer_product_manage_rules_table.php | 36 ++++++ 6 files changed, 207 insertions(+) create mode 100644 app/Admin/Actions/Grid/DealerProductManageRule.php create mode 100644 app/Admin/Forms/DealerProductManageRule.php create mode 100644 app/Models/DealerProductManageRule.php create mode 100644 database/migrations/2022_01_13_110453_create_dealer_product_manage_rules_table.php diff --git a/app/Admin/Actions/Grid/DealerProductManageRule.php b/app/Admin/Actions/Grid/DealerProductManageRule.php new file mode 100644 index 00000000..f1ebdf26 --- /dev/null +++ b/app/Admin/Actions/Grid/DealerProductManageRule.php @@ -0,0 +1,38 @@ +title) { + return $this->title; + } + return ' 管理规则   '; + } + + /** + * @param Model|Authenticatable|HasPermissions|null $user + * + * @return bool + */ + protected function authorize($user): bool + { + return $user->can('dcat.admin.dealer_products.manage_rules'); + } + + public function render() + { + $form = DealerProductManageRuleForm::make()->payload(['id'=>$this->getKey()]); + return Modal::make() + ->lg() + ->title($this->title()) + ->body($form) + ->button($this->title()); + } +} diff --git a/app/Admin/Controllers/DealerProductController.php b/app/Admin/Controllers/DealerProductController.php index 8755dc05..c7ba4a2c 100644 --- a/app/Admin/Controllers/DealerProductController.php +++ b/app/Admin/Controllers/DealerProductController.php @@ -3,6 +3,7 @@ namespace App\Admin\Controllers; use App\Admin\Actions\Grid\DealerProductLvlRule as DealerProductLvlRuleAction; +use App\Admin\Actions\Grid\DealerProductManageRule as DealerProductManageRuleAction; use App\Admin\Actions\Grid\DealerProductSaleRule as DealerProductSaleRuleAction; use App\Admin\Repositories\DealerProduct; use Carbon\Carbon; @@ -60,6 +61,9 @@ class DealerProductController extends AdminController if (Admin::user()->can('dcat.admin.dealers_products.sale_rules')) { $actions->append(new DealerProductSaleRuleAction()); } + if (Admin::user()->can('dcat.admin.dealers_products.manage_rules')) { + $actions->append(new DealerProductManageRuleAction()); + } }); $grid->filter(function (Grid\Filter $filter) { $filter->panel(); diff --git a/app/Admin/Forms/DealerProductManageRule.php b/app/Admin/Forms/DealerProductManageRule.php new file mode 100644 index 00000000..1a61785d --- /dev/null +++ b/app/Admin/Forms/DealerProductManageRule.php @@ -0,0 +1,109 @@ +can('dcat.admin.dealer_products.manage_rules'); + } + + /** + * Handle the form request. + * + * @param array $input + * + * @return mixed + */ + public function handle(array $input) + { + $productId = $this->payload['id'] ?? 0; + + $product = DealerProduct::findOrFail($productId); + $manageRules = []; + $delRules = []; + foreach ($input['manageRules'] as $rule) { + if ($rule['_remove_'] == 1) { + $delRules[] = $rule['id']; + } else { + if (is_null($rule['id'])) { + $manageRules[] = new DealerProductManageRuleModel($rule); + } else { + $_rule = DealerProductManageRuleModel::find($rule['id']); + $_rule['lvl'] = $rule['id']; + $_rule['price_1st'] = $rule['price_1st']; + $_rule['price_2st'] = $rule['price_2st']; + $_rule['price_3st'] = $rule['price_3st']; + $manageRules[] = $_rule; + } + } + } + + try { + DB::beginTransaction(); + $product->lvlRules()->saveMany($manageRules); + + DealerProductManageRuleModel::whereIn('id', $delRules)->delete(); + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + return $this->response()->error('操作失败:'.$th->getMessage())->refresh(); + } + + return $this->response() + ->success(__('admin.update_succeeded')) + ->refresh(); + } + + /** + * Build a form here. + */ + public function form() + { + $this->hasMany('manageRules', '管理规则', function (NestedForm $form) { + $form->select('lvl')->options([ + 5 => '二级经销商', + 6 => '一级经销商', + ]); + $form->currency('price_1st', '价格1')->symbol('¥')->required(); + $form->currency('price_2st', '价格2')->symbol('¥')->required(); + $form->currency('price_3st', '价格3')->symbol('¥')->required(); + }); + } + + /** + * The data of the form. + * + * @return array + */ + public function default() + { + $productId = $this->payload['id'] ?? 0; + + $product = DealerProduct::findOrFail($productId); + return [ + 'manageRules' => $product->manageRules, + // 'email' => 'John.Doe@gmail.com', + ]; + } +} diff --git a/app/Models/DealerProduct.php b/app/Models/DealerProduct.php index df20db61..9a44b849 100644 --- a/app/Models/DealerProduct.php +++ b/app/Models/DealerProduct.php @@ -32,6 +32,11 @@ class DealerProduct extends Model return $this->hasMany(DealerProductSaleRule::class, 'product_id')->orderBy('qty', 'asc'); } + public function manageRules() + { + return $this->hasMany(DealerProductManageRule::class, 'product_id'); + } + public function isOnline() { return $this->is_sale; diff --git a/app/Models/DealerProductManageRule.php b/app/Models/DealerProductManageRule.php new file mode 100644 index 00000000..33c1ed3a --- /dev/null +++ b/app/Models/DealerProductManageRule.php @@ -0,0 +1,15 @@ +id(); + $table->unsignedBigInteger('product_id')->comment('商品ID'); + $table->unsignedTinyInteger('lvl')->default(0)->comment('等级'); + $table->unsignedDecimal('price_1st', 10, 2)->default(0.00)->comment('第一个价格'); + $table->unsignedDecimal('price_2st', 10, 2)->default(0.00)->comment('第二个价格'); + $table->unsignedDecimal('price_3st', 10, 2)->default(0.00)->comment('第三个价格'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('dealer_product_manage_rules'); + } +}