178 lines
6.9 KiB
PHP
178 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Renderable\ProductSkuSimpleTable;
|
|
use App\Admin\Repositories\BargainActivity;
|
|
use App\Models\BargainActivity as BargainActivityModel;
|
|
use App\Models\ProductSku;
|
|
use Carbon\Carbon;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Grid\Column;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Layout\Row;
|
|
use Dcat\Admin\Show;
|
|
|
|
class BargainActivityController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new BargainActivity(), function (Grid $grid) {
|
|
$grid->column('id')->sortable();
|
|
$grid->column('name');
|
|
// $grid->column('description');
|
|
$grid->column('is_enable')
|
|
->if(function () {
|
|
return Admin::user()->can('dcat.admin.bargain_activities.edit');
|
|
})
|
|
->then(function (Column $column) {
|
|
$column->switch();
|
|
})
|
|
->else(function (Column $column) {
|
|
$column->bool();
|
|
});
|
|
// $grid->column('rules');
|
|
$grid->column('times');
|
|
// $grid->column('expire_hours');
|
|
$grid->column('start_at');
|
|
$grid->column('end_at');
|
|
$grid->column('created_at');
|
|
$grid->column('updated_at')->sortable();
|
|
|
|
//新增
|
|
if (Admin::user()->can('dcat.admin.bargain_activities.create')) {
|
|
$grid->disableCreateButton(false);
|
|
// $grid->enableDialogCreate();
|
|
}
|
|
|
|
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
$actions->disableView(Admin::user()->cannot('dcat.admin.bargain_activities.show'));
|
|
$actions->disableDelete(Admin::user()->cannot('dcat.admin.bargain_activities.destroy'));
|
|
//修改
|
|
$actions->disableEdit(Admin::user()->cannot('dcat.admin.bargain_activities.edit'));
|
|
});
|
|
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->panel();
|
|
$filter->like('name')->width(3);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return function (Row $row) use ($id) {
|
|
$activity = BargainActivityModel::with(['skus'])->findOrFail($id);
|
|
$row->column(6, function ($column) use ($activity) {
|
|
$column->row(Show::make($activity, function (Show $show) use ($activity) {
|
|
$show->panel()
|
|
->tools(function ($tools) {
|
|
$tools->disableEdit(Admin::user()->cannot('dcat.admin.bargain_activities.edit'));
|
|
// $tools->disableList();
|
|
$tools->disableDelete(Admin::user()->cannot('dcat.admin.bargain_activities.destroy'));
|
|
});
|
|
$show->row(function (Show\Row $show) use ($activity) {
|
|
$show->field('id')->width(10, 1);
|
|
$show->field('name')->width(10, 1);
|
|
$show->field('is_enable')->using([0=>'未开启', '已开启'])->dot([
|
|
'0'=>'#b3b9bf',
|
|
'1'=>'success',
|
|
])->width(10, 1);
|
|
$show->width(6)->field('start_at');
|
|
$show->width(6)->field('end_at');
|
|
$show->width(6)->field('times')->append('刀');
|
|
$show->width(6)->field('expire_hours')->append('h');
|
|
|
|
$show->width(12)->field('skus')->width(10, 1)->as(function ($value) {
|
|
return array_column($value, 'name');
|
|
})->label();
|
|
|
|
$show->field('description')->unescape()->width(10, 1);
|
|
|
|
$show->width(6)->field('created_at');
|
|
$show->width(6)->field('updated_at');
|
|
});
|
|
}));
|
|
});
|
|
$row->column(6, function ($column) use ($activity) {
|
|
//砍价记录-todo
|
|
});
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
$builder = BargainActivity::with(['skus']);
|
|
return Form::make($builder, function (Form $form) {
|
|
$form->display('id');
|
|
$form->display('created_at');
|
|
$form->display('updated_at');
|
|
$form->block(6, function (Form\BlockForm $form) {
|
|
$form->text('name')->required();
|
|
$form->switch('is_enable');
|
|
$form->dateRange('start_at', 'end_at', '活动时间')->required();
|
|
$form->multipleSelectTable('skus')
|
|
->from(ProductSkuSimpleTable::make())
|
|
->model(ProductSku::class, 'id', 'name')
|
|
->customFormat(function ($v) {
|
|
if (!$v) {
|
|
return [];
|
|
}
|
|
// 这一步非常重要,需要把数据库中查出来的二维数组转化成一维数组
|
|
return array_column($v, 'id');
|
|
})->required();
|
|
$form->number('times')->min(0);
|
|
$form->number('expire_hours')->min(0);
|
|
$form->textarea('rules')->customFormat(function ($value) {
|
|
return implode(',', json_decode($value));
|
|
})->saving(function ($value) {
|
|
// dd($value, explode(',', $value));
|
|
return json_encode(explode(',', $value));
|
|
});
|
|
$form->showFooter();
|
|
});
|
|
$form->block(6, function (Form\BlockForm $form) {
|
|
$form->multipleImage('images')
|
|
->move('bargain/images/'.Carbon::now()->toDateString())
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->autoUpload()->retainable()->sortable();
|
|
$form->editor('description')->height('600');
|
|
});
|
|
$form->saving(function ($form) {
|
|
if ($form->is_enable) {
|
|
//查询是否有除了自己以外开启的活动
|
|
if ($form->id) {
|
|
if (BargainActivityModel::where('id', '<>', $form->id)->isEnable()->exists()) {
|
|
return $form->response()->error('当前已有开启的活动');
|
|
}
|
|
} else {
|
|
if (BargainActivityModel::isEnable()->exists()) {
|
|
return $form->response()->error('当前已有开启的活动');
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|