111 lines
3.1 KiB
PHP
111 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Models\DealerProduct;
|
|
use App\Models\DealerProductLvlRule as DealerProductLvlRuleModel;
|
|
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 DealerProductLvlRule 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.lvl_rules');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$productId = $this->payload['id'] ?? 0;
|
|
|
|
$product = DealerProduct::findOrFail($productId);
|
|
$lvlRules = [];
|
|
$delRules = [];
|
|
foreach ($input['lvlRules'] as $rule) {
|
|
if ($rule['_remove_'] == 1) {
|
|
$delRules[] = $rule['id'];
|
|
} else {
|
|
if (is_null($rule['id'])) {
|
|
$lvlRules[] = new DealerProductLvlRuleModel($rule);
|
|
} else {
|
|
$_rule = DealerProductLvlRuleModel::find($rule['id']);
|
|
$_rule['lvl'] = $rule['lvl'];
|
|
$_rule['sale_price'] = $rule['sale_price'];
|
|
// $_rule['min_order_amount'] = $rule['min_order_amount'];
|
|
$lvlRules[] = $_rule;
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$product->lvlRules()->saveMany($lvlRules);
|
|
|
|
DealerProductLvlRuleModel::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('lvlRules', '等级规则', function (NestedForm $form) {
|
|
$form->select('lvl')->options([
|
|
2 => '金牌经销商',
|
|
3 => '特邀经销商',
|
|
4 => '签约经销商',
|
|
5 => '二级经销商',
|
|
6 => '一级经销商',
|
|
]);
|
|
$form->currency('sale_price', '等级进货单价')->symbol('¥');
|
|
// $form->currency('min_order_amount', '等级单次最低进货价')->symbol('¥');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* The data of the form.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function default()
|
|
{
|
|
$productId = $this->payload['id'] ?? 0;
|
|
|
|
$product = DealerProduct::findOrFail($productId);
|
|
return [
|
|
'lvlRules' => $product->lvlRules,
|
|
// 'email' => 'John.Doe@gmail.com',
|
|
];
|
|
}
|
|
}
|