|
|
@@ -0,0 +1,320 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Shop\AdminControllers;
|
|
|
+
|
|
|
+use App\Module\Shop\AdminControllers\Helper\FilterHelper;
|
|
|
+use App\Module\Shop\AdminControllers\Helper\FormHelper;
|
|
|
+use App\Module\Shop\AdminControllers\Helper\GridHelper;
|
|
|
+use App\Module\Shop\AdminControllers\Helper\ShowHelper;
|
|
|
+use App\Module\Shop\Models\ShopItem;
|
|
|
+use App\Module\Shop\Models\ShopPromotion;
|
|
|
+use App\Module\Shop\Repositorys\ShopPromotionRepository;
|
|
|
+use Dcat\Admin\Form;
|
|
|
+use Dcat\Admin\Grid;
|
|
|
+use Dcat\Admin\Show;
|
|
|
+use Dcat\Admin\Http\Controllers\AdminController;
|
|
|
+use Dcat\Admin\Layout\Content;
|
|
|
+use Dcat\Admin\Widgets\Card;
|
|
|
+use Dcat\Admin\Widgets\Table;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Spatie\RouteAttributes\Attributes\Resource;
|
|
|
+use Spatie\RouteAttributes\Attributes\Get;
|
|
|
+use Spatie\RouteAttributes\Attributes\Post;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 商店促销活动管理控制器
|
|
|
+ */
|
|
|
+#[Resource('shop/promotions', names: 'dcat.admin.shop.promotions')]
|
|
|
+class ShopPromotionController extends AdminController
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 页面标题
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $title = '促销活动';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建表格
|
|
|
+ *
|
|
|
+ * @return Grid
|
|
|
+ */
|
|
|
+ protected function grid()
|
|
|
+ {
|
|
|
+ return Grid::make(new ShopPromotionRepository(), function (Grid $grid) {
|
|
|
+ $grid->column('id', 'ID')->sortable();
|
|
|
+ $grid->column('name', '活动名称');
|
|
|
+ $grid->column('banner', '横幅图片')->image('', 100, 50);
|
|
|
+ $grid->column('discount_type', '折扣类型')->display(function ($type) {
|
|
|
+ return $type == ShopPromotion::DISCOUNT_TYPE_FIXED ? '固定折扣' : '百分比折扣';
|
|
|
+ });
|
|
|
+ $grid->column('discount_value', '折扣值')->display(function ($value) {
|
|
|
+ if ($this->discount_type == ShopPromotion::DISCOUNT_TYPE_FIXED) {
|
|
|
+ return $value . ' 元';
|
|
|
+ } else {
|
|
|
+ return $value . '%';
|
|
|
+ }
|
|
|
+ });
|
|
|
+ $grid->column('is_active', '状态')->switch();
|
|
|
+ $grid->column('sort_order', '排序权重')->sortable();
|
|
|
+ $grid->column('start_time', '开始时间')->sortable();
|
|
|
+ $grid->column('end_time', '结束时间')->sortable();
|
|
|
+ $grid->column('created_at', '创建时间')->sortable();
|
|
|
+
|
|
|
+ // 添加操作按钮
|
|
|
+ $grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
|
+ $actions->append('<a href="' . admin_url('shop/promotions/' . $actions->row->id . '/items') . '" class="btn btn-sm btn-primary">管理商品</a>');
|
|
|
+ });
|
|
|
+
|
|
|
+ // 过滤器
|
|
|
+ $grid->filter(function (Grid\Filter $filter) {
|
|
|
+ $filter->equal('id', 'ID');
|
|
|
+ $filter->like('name', '活动名称');
|
|
|
+ $filter->equal('discount_type', '折扣类型')->select([
|
|
|
+ ShopPromotion::DISCOUNT_TYPE_FIXED => '固定折扣',
|
|
|
+ ShopPromotion::DISCOUNT_TYPE_PERCENTAGE => '百分比折扣',
|
|
|
+ ]);
|
|
|
+ $filter->equal('is_active', '状态')->select([
|
|
|
+ 0 => '禁用',
|
|
|
+ 1 => '启用',
|
|
|
+ ]);
|
|
|
+ $filter->between('start_time', '开始时间')->datetime();
|
|
|
+ $filter->between('end_time', '结束时间')->datetime();
|
|
|
+ });
|
|
|
+
|
|
|
+ // 工具栏
|
|
|
+ $grid->toolsWithOutline(false);
|
|
|
+ $grid->disableViewButton();
|
|
|
+ $grid->showQuickEditButton();
|
|
|
+ $grid->enableDialogCreate();
|
|
|
+ $grid->enableDialogEdit();
|
|
|
+ $grid->setDialogFormDimensions('800px', '720px');
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建详情页
|
|
|
+ *
|
|
|
+ * @param mixed $id
|
|
|
+ * @return Show
|
|
|
+ */
|
|
|
+ protected function detail($id)
|
|
|
+ {
|
|
|
+ return Show::make($id, new ShopPromotionRepository(), function (Show $show) {
|
|
|
+ $show->field('id', 'ID');
|
|
|
+ $show->field('name', '活动名称');
|
|
|
+ $show->field('description', '活动描述');
|
|
|
+ $show->field('banner', '横幅图片')->image();
|
|
|
+ $show->field('discount_type', '折扣类型')->as(function ($type) {
|
|
|
+ return $type == ShopPromotion::DISCOUNT_TYPE_FIXED ? '固定折扣' : '百分比折扣';
|
|
|
+ });
|
|
|
+ $show->field('discount_value', '折扣值')->as(function ($value) {
|
|
|
+ if ($this->discount_type == ShopPromotion::DISCOUNT_TYPE_FIXED) {
|
|
|
+ return $value . ' 元';
|
|
|
+ } else {
|
|
|
+ return $value . '%';
|
|
|
+ }
|
|
|
+ });
|
|
|
+ $show->field('is_active', '状态')->as(function ($isActive) {
|
|
|
+ return $isActive ? '启用' : '禁用';
|
|
|
+ });
|
|
|
+ $show->field('sort_order', '排序权重');
|
|
|
+ $show->field('start_time', '开始时间');
|
|
|
+ $show->field('end_time', '结束时间');
|
|
|
+ $show->field('created_at', '创建时间');
|
|
|
+ $show->field('updated_at', '更新时间');
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建表单
|
|
|
+ *
|
|
|
+ * @return Form
|
|
|
+ */
|
|
|
+ protected function form()
|
|
|
+ {
|
|
|
+ return Form::make(new ShopPromotionRepository(), function (Form $form) {
|
|
|
+ $form->display('id', 'ID');
|
|
|
+ $form->text('name', '活动名称')->required();
|
|
|
+ $form->textarea('description', '活动描述')->rows(3);
|
|
|
+ $form->image('banner', '横幅图片')->autoUpload()->uniqueName()->help('建议尺寸:1200x300');
|
|
|
+ $form->radio('discount_type', '折扣类型')
|
|
|
+ ->options([
|
|
|
+ ShopPromotion::DISCOUNT_TYPE_FIXED => '固定折扣',
|
|
|
+ ShopPromotion::DISCOUNT_TYPE_PERCENTAGE => '百分比折扣',
|
|
|
+ ])
|
|
|
+ ->default(ShopPromotion::DISCOUNT_TYPE_PERCENTAGE)
|
|
|
+ ->required();
|
|
|
+ $form->number('discount_value', '折扣值')
|
|
|
+ ->min(0)
|
|
|
+ ->help('固定折扣为具体金额,百分比折扣为1-100的整数')
|
|
|
+ ->required();
|
|
|
+ $form->number('sort_order', '排序权重')->default(0)->help('数字越小越靠前');
|
|
|
+ $form->switch('is_active', '状态')->default(true);
|
|
|
+ $form->datetime('start_time', '开始时间')->help('留空表示不限制开始时间');
|
|
|
+ $form->datetime('end_time', '结束时间')->help('留空表示不限制结束时间');
|
|
|
+
|
|
|
+ $form->display('created_at', '创建时间');
|
|
|
+ $form->display('updated_at', '更新时间');
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 管理促销活动商品
|
|
|
+ *
|
|
|
+ * @param Content $content
|
|
|
+ * @param int $id
|
|
|
+ * @return Content
|
|
|
+ */
|
|
|
+ #[Get('shop/promotions/{id}/items')]
|
|
|
+ public function items(Content $content, $id)
|
|
|
+ {
|
|
|
+ $promotion = ShopPromotion::findOrFail($id);
|
|
|
+
|
|
|
+ $content->title('管理促销活动商品');
|
|
|
+ $content->description('为促销活动 "' . $promotion->name . '" 添加或移除商品');
|
|
|
+
|
|
|
+ // 已添加的商品
|
|
|
+ $addedItems = $promotion->items()->with('category')->get();
|
|
|
+ $addedItemsTable = $this->buildAddedItemsTable($addedItems, $promotion);
|
|
|
+
|
|
|
+ // 添加商品表单
|
|
|
+ $addItemForm = $this->buildAddItemForm($id);
|
|
|
+
|
|
|
+ $content->row(function ($row) use ($promotion) {
|
|
|
+ $row->column(12, new Card('促销活动信息', view('admin.shop.promotion_info', ['promotion' => $promotion])));
|
|
|
+ });
|
|
|
+
|
|
|
+ $content->row(function ($row) use ($addedItemsTable) {
|
|
|
+ $row->column(12, new Card('已添加商品', $addedItemsTable));
|
|
|
+ });
|
|
|
+
|
|
|
+ $content->row(function ($row) use ($addItemForm) {
|
|
|
+ $row->column(12, new Card('添加商品', $addItemForm));
|
|
|
+ });
|
|
|
+
|
|
|
+ return $content;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建已添加商品表格
|
|
|
+ *
|
|
|
+ * @param Collection $items
|
|
|
+ * @param ShopPromotion $promotion
|
|
|
+ * @return Table
|
|
|
+ */
|
|
|
+ protected function buildAddedItemsTable($items, $promotion)
|
|
|
+ {
|
|
|
+ $headers = ['ID', '商品名称', '分类', '原价', '折扣价', '自定义折扣值', '操作'];
|
|
|
+ $rows = [];
|
|
|
+
|
|
|
+ foreach ($items as $item) {
|
|
|
+ $customDiscountValue = $item->pivot->custom_discount_value;
|
|
|
+ $originalPrice = $item->price;
|
|
|
+ $discountedPrice = $promotion->calculateDiscountedPrice($originalPrice, $customDiscountValue);
|
|
|
+
|
|
|
+ $removeUrl = admin_url('shop/promotions/' . $promotion->id . '/items/remove?item_id=' . $item->id);
|
|
|
+ $removeButton = "<a href=\"{$removeUrl}\" class=\"btn btn-sm btn-danger\" onclick=\"return confirm('确定要移除该商品吗?')\">移除</a>";
|
|
|
+
|
|
|
+ $rows[] = [
|
|
|
+ $item->id,
|
|
|
+ $item->name,
|
|
|
+ $item->category->name ?? '未分类',
|
|
|
+ $originalPrice,
|
|
|
+ $discountedPrice,
|
|
|
+ $customDiscountValue ?? '使用默认值',
|
|
|
+ $removeButton
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return new Table($headers, $rows);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建添加商品表单
|
|
|
+ *
|
|
|
+ * @param int $promotionId
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ protected function buildAddItemForm($promotionId)
|
|
|
+ {
|
|
|
+ $form = new Form();
|
|
|
+ $form->action(admin_url('shop/promotions/' . $promotionId . '/items/add'));
|
|
|
+
|
|
|
+ $form->select('item_id', '选择商品')
|
|
|
+ ->options(ShopItem::where('is_active', true)->pluck('name', 'id'))
|
|
|
+ ->required();
|
|
|
+
|
|
|
+ $form->number('custom_discount_value', '自定义折扣值')
|
|
|
+ ->help('留空则使用促销活动的默认折扣值');
|
|
|
+
|
|
|
+ $form->hidden('_token')->default(csrf_token());
|
|
|
+
|
|
|
+ $form->submit('添加商品');
|
|
|
+
|
|
|
+ return $form->render();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加商品到促销活动
|
|
|
+ *
|
|
|
+ * @param Request $request
|
|
|
+ * @param int $id
|
|
|
+ * @return \Illuminate\Http\RedirectResponse
|
|
|
+ */
|
|
|
+ #[Post('shop/promotions/{id}/items/add')]
|
|
|
+ public function addItem(Request $request, $id)
|
|
|
+ {
|
|
|
+ $request->validate([
|
|
|
+ 'item_id' => 'required|integer|exists:shop_items,id',
|
|
|
+ 'custom_discount_value' => 'nullable|integer|min:0',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $promotionId = $id;
|
|
|
+ $shopItemId = $request->input('item_id');
|
|
|
+ $customDiscountValue = $request->input('custom_discount_value');
|
|
|
+
|
|
|
+ // 添加商品到促销活动
|
|
|
+ $result = \App\Module\Shop\Services\ShopService::addItemToPromotion(
|
|
|
+ $promotionId,
|
|
|
+ $shopItemId,
|
|
|
+ $customDiscountValue
|
|
|
+ );
|
|
|
+
|
|
|
+ admin_success('成功', '商品已添加到促销活动');
|
|
|
+
|
|
|
+ return redirect()->back();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从促销活动中移除商品
|
|
|
+ *
|
|
|
+ * @param Request $request
|
|
|
+ * @param int $id
|
|
|
+ * @return \Illuminate\Http\RedirectResponse
|
|
|
+ */
|
|
|
+ #[Get('shop/promotions/{id}/items/remove')]
|
|
|
+ public function removeItem(Request $request, $id)
|
|
|
+ {
|
|
|
+ $request->validate([
|
|
|
+ 'item_id' => 'required|integer|exists:shop_items,id',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $promotionId = $id;
|
|
|
+ $shopItemId = $request->input('item_id');
|
|
|
+
|
|
|
+ // 从促销活动中移除商品
|
|
|
+ $result = \App\Module\Shop\Services\ShopService::removeItemFromPromotion(
|
|
|
+ $promotionId,
|
|
|
+ $shopItemId
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ admin_success('成功', '商品已从促销活动中移除');
|
|
|
+ } else {
|
|
|
+ admin_error('失败', '移除商品失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ return redirect()->back();
|
|
|
+ }
|
|
|
+}
|