264 lines
11 KiB
PHP
264 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Repositories\Activity;
|
|
use App\Models\Activity as ActivityModel;
|
|
use App\Models\Coupon;
|
|
use App\Models\ProductPart;
|
|
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 ActivityController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Activity(), function (Grid $grid) {
|
|
$grid->column('id')->sortable();
|
|
$grid->column('title');
|
|
$grid->column('cover')->image(100);
|
|
// $grid->column('content');
|
|
$grid->column('is_use')->filter(Grid\Column\Filter\In::make([
|
|
0=>'下架',
|
|
1=>'上架',
|
|
]))
|
|
->if(function () {
|
|
return Admin::user()->can('dcat.admin.activities.edit');
|
|
})
|
|
->then(function (Column $column) {
|
|
$column->switch();
|
|
})
|
|
->else(function (Column $column) {
|
|
$column->bool();
|
|
});
|
|
$grid->column('started_at');
|
|
$grid->column('ended_at');
|
|
$grid->model()->orderBy('created_at', 'desc');
|
|
$grid->column('created_at')->sortable();
|
|
//新增
|
|
if (Admin::user()->can('dcat.admin.activities.create')) {
|
|
$grid->disableCreateButton(false);
|
|
// $grid->enableDialogCreate();
|
|
}
|
|
//删除以及自定义操作
|
|
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
$actions->disableDelete(Admin::user()->cannot('dcat.admin.activities.destroy'));
|
|
//修改
|
|
$actions->disableEdit(Admin::user()->cannot('dcat.admin.activities.edit'));
|
|
});
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->panel();
|
|
$filter->like('title')->width(3);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return function (Row $row) use ($id) {
|
|
$activity = ActivityModel::with(['parts', 'coupons', 'gifts'])->findOrFail($id);
|
|
$row->column(6, function ($column) use ($activity) {
|
|
$column->row(Show::make($activity, function (Show $show) use ($activity) {
|
|
$show->row(function (Show\Row $show) use ($activity) {
|
|
$show->field('id')->width(10, 1);
|
|
$show->field('title')->width(10, 1);
|
|
$show->field('cover')->width(10, 1)->image();
|
|
$show->field('is_use')->using([0=>'未开启', '已开启'])->dot([
|
|
'0'=>'#b3b9bf',
|
|
'1'=>'success',
|
|
])->width(10, 1);
|
|
$show->width(6)->field('started_at');
|
|
$show->width(6)->field('ended_at');
|
|
$show->width(12)->field('parts', '分区关联')->width(10, 1)->as(function ($value) {
|
|
return array_column($value, 'name');
|
|
})->label();
|
|
|
|
foreach ($activity->coupons as $coupon) {
|
|
$show->width(6)->field('coupon.name', '优惠券名称')->as(function () use ($coupon) {
|
|
return $coupon->name;
|
|
})->label();
|
|
$show->width(6)->field('coupon.qty', '赠送数量')->as(function () use ($coupon) {
|
|
return $coupon->pivot->qty;
|
|
});
|
|
};
|
|
foreach ($activity->gifts as $gift) {
|
|
$show->width(6)->field('gift.name', '赠品名称')->as(function () use ($gift) {
|
|
return $gift->name;
|
|
})->label();
|
|
$show->width(6)->field('gift.qty', '赠送数量')->as(function () use ($gift) {
|
|
return $gift->pivot->qty;
|
|
});
|
|
};
|
|
|
|
$show->width(6)->field('created_at');
|
|
$show->width(6)->field('updated_at');
|
|
});
|
|
}));
|
|
});
|
|
$row->column(6, function ($column) use ($activity) {
|
|
$column->row(Show::make($activity, function (Show $show) use ($activity) {
|
|
$show->panel()
|
|
->tools(function ($tools) {
|
|
$tools->disableEdit();
|
|
$tools->disableList();
|
|
$tools->disableDelete();
|
|
});
|
|
$show->row(function (Show\Row $show) {
|
|
$show->width(12)->field('coupons_rule')->as(function ($value) {
|
|
$str = '';
|
|
// dd($value);
|
|
foreach ($value as $key => $v) {
|
|
switch ($key) {
|
|
case 'type':
|
|
if ($v) {
|
|
$str .= '其他活动来源-';
|
|
} else {
|
|
$str .= '按订单赠送-';
|
|
}
|
|
break;
|
|
case 'value':
|
|
$str .= '满'.$v.'赠送-';
|
|
break;
|
|
case 'times':
|
|
if ($v) {
|
|
$str .= '不限次数-';
|
|
} else {
|
|
$str .= '仅首单-';
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return rtrim($str, '-');
|
|
})->width(10, 1);
|
|
$show->width(12)->field('gifts_rule')->as(function ($value) {
|
|
$str = '';
|
|
foreach ($value as $key => $v) {
|
|
switch ($key) {
|
|
case 'type':
|
|
if ($v) {
|
|
$str .= '其他活动来源-';
|
|
} else {
|
|
$str .= '按订单赠送-';
|
|
}
|
|
break;
|
|
case 'value':
|
|
$str .= '满'.$v.'赠送-';
|
|
break;
|
|
case 'times':
|
|
if ($v) {
|
|
$str .= '不限次数-';
|
|
} else {
|
|
$str .= '仅首单-';
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return rtrim($str, '-');
|
|
})->width(10, 1);
|
|
$show->field('content')->unescape()->width(10, 1);
|
|
});
|
|
}));
|
|
});
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
$builder = Activity::with(['parts', 'couponSet', 'giftSet']);
|
|
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('title')->required();
|
|
$form->image('cover')
|
|
->move('activities/'.Carbon::now()->toDateString())
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->autoUpload()->retainable();
|
|
$form->switch('is_use');
|
|
$form->dateRange('started_at', 'ended_at', '活动时间');
|
|
$form->multipleSelect('parts', '商品分区关联')->options(ProductPart::all()->pluck('name', 'id'))->customFormat(function ($v) {
|
|
if (! $v) {
|
|
return [];
|
|
}
|
|
// 从数据库中查出的二维数组中转化成ID
|
|
return array_column($v, 'id');
|
|
});
|
|
$form->hasMany('couponSet', '优惠券关联', function ($form) {
|
|
$form->select('coupon_id', '优惠券')->options(function ($id) {
|
|
$coupon = Coupon::find($id);
|
|
if ($coupon) {
|
|
return [$coupon->id => $coupon->name];
|
|
}
|
|
})->ajax(admin_route('api.coupons'));
|
|
$form->number('qty', '数量');
|
|
})->useTable();
|
|
$form->hasMany('giftSet', '赠品关联', function ($form) {
|
|
$form->select('sku_id', '赠品')->options(function ($id) {
|
|
$sku = ProductSku::find($id);
|
|
if ($sku) {
|
|
return [$sku->id => $sku->name];
|
|
}
|
|
})->ajax(admin_route('api.product_skus'));
|
|
$form->number('qty', '数量');
|
|
})->useTable();
|
|
|
|
$form->showFooter();
|
|
});
|
|
$form->block(6, function (Form\BlockForm $form) {
|
|
$form->title('活动内容');
|
|
//优惠券规则
|
|
$form->embeds('coupons_rule', function ($form) {
|
|
$form->radio('type')->options([
|
|
0=>'按订单赠送',
|
|
1=>'其他活动来源',
|
|
])->default(0);
|
|
$form->number('value', '门槛')->help('每N元赠送一份');
|
|
$form->radio('times', '可领次数')->options([
|
|
'0'=>'仅首单',
|
|
'1'=>'不限',
|
|
])->default(0);
|
|
});
|
|
//赠品规则
|
|
$form->embeds('gifts_rule', function ($form) {
|
|
$form->radio('type')->options([
|
|
0=>'按订单赠送',
|
|
1=>'其他活动来源',
|
|
])->default(0);
|
|
$form->number('value', '门槛')->help('每N元赠送一份');
|
|
$form->radio('times', '可领次数')->options([
|
|
'0'=>'仅首单',
|
|
'1'=>'不限',
|
|
])->default(0);
|
|
});
|
|
$form->editor('content')->height('600');
|
|
});
|
|
});
|
|
}
|
|
}
|