添加管理规则
parent
d053ec7072
commit
5814642bfb
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Actions\Grid;
|
||||
|
||||
use App\Admin\Forms\DealerProductManageRule as DealerProductManageRuleForm;
|
||||
use Dcat\Admin\Grid\RowAction;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
|
||||
class DealerProductManageRule extends RowAction
|
||||
{
|
||||
public function title()
|
||||
{
|
||||
if ($this->title) {
|
||||
return $this->title;
|
||||
}
|
||||
return '<i class="feather icon-bookmark grid-action-icon"></i> 管理规则 ';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Forms;
|
||||
|
||||
use App\Models\DealerProduct;
|
||||
use App\Models\DealerProductManageRule as DealerProductManageRuleModel;
|
||||
use Dcat\Admin\Contracts\LazyRenderable;
|
||||
use Dcat\Admin\Form\NestedForm;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
class DealerProductManageRule extends Form implements LazyRenderable
|
||||
{
|
||||
use LazyWidget;
|
||||
|
||||
/**
|
||||
* 权限判断,如不需要可以删除此方法
|
||||
*
|
||||
* @param Model|Authenticatable|HasPermissions|null $user
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorize($user): bool
|
||||
{
|
||||
return $user->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',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class DealerProductManageRule extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'lvl', 'price_1st', 'price_2st', 'price_3st',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateDealerProductManageRulesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('dealer_product_manage_rules', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue