4
0
Fork 0

batch goods sale

master
panliang 2022-09-29 10:48:57 +08:00
parent f013e73d61
commit c1adaf8dc4
5 changed files with 152 additions and 54 deletions

View File

@ -35,7 +35,7 @@ $menus = [
['title' => '商品分类', 'icon' => '', 'uri' => '/goods-category', 'permission' => 'goods_category'], ['title' => '商品分类', 'icon' => '', 'uri' => '/goods-category', 'permission' => 'goods_category'],
['title' => '商品品牌', 'icon' => '', 'uri' => '/goods-brand', 'permission' => 'goods_brand'], ['title' => '商品品牌', 'icon' => '', 'uri' => '/goods-brand', 'permission' => 'goods_brand'],
['title' => '商品类别', 'icon' => '', 'uri' => '/goods-type', 'permission' => 'goods_type'], ['title' => '商品类别', 'icon' => '', 'uri' => '/goods-type', 'permission' => 'goods_type'],
['title' => '商品管理', 'icon' => '', 'uri' => '/goods', 'permission' => 'goods'], ['title' => '商品管理', 'icon' => '', 'uri' => '/goods', 'permission' => 'goods', 'children' => ['import' => '导入']],
]], ]],
]; ];
``` ```

View File

@ -0,0 +1,25 @@
<?php
namespace Peidikeji\Goods\Action;
use Dcat\Admin\Grid\BatchAction;
use Peidikeji\Goods\Models\Goods;
class BatchGoodsDown extends BatchAction
{
protected $title = '下架';
public function handle()
{
$ids = $this->getKey();
Goods::whereIn('id', $ids)->update(['on_sale' => 0]);
return $this->response()->success('操作成功')->refresh();
}
public function confirm()
{
return ['是否确定?'];
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace Peidikeji\Goods\Action;
use Dcat\Admin\Grid\BatchAction;
use Peidikeji\Goods\Models\Goods;
class BatchGoodsUp extends BatchAction
{
protected $title = '上架';
public function handle()
{
$ids = $this->getKey();
Goods::whereIn('id', $ids)->update(['on_sale' => 1]);
return $this->response()->success('操作成功')->refresh();
}
public function confirm()
{
return ['是否确定?'];
}
}

View File

@ -35,20 +35,20 @@ class ImportForm extends Form
array_shift($rows); array_shift($rows);
foreach ($rows as $row) { foreach ($rows as $row) {
$index = 0; $index = 0;
$sn = $row[$index]; $sn = data_get($row, $index);
$category = $row[++$index]; $category = data_get($row, ++$index);
$brand = $row[++$index]; $brand = data_get($row, ++$index);
$type = $row[++$index]; $type = data_get($row, ++$index);
$name = $row[++$index]; $name = data_get($row, ++$index);
$description = $row[++$index]; $description = data_get($row, ++$index);
$cover = $row[++$index]; $cover = data_get($row, ++$index);
$images = $row[++$index]; $images = data_get($row, ++$index);
$content = $row[++$index]; $content = data_get($row, ++$index);
$price = $row[++$index]; $price = data_get($row, ++$index);
$stock = $row[++$index]; $stock = data_get($row, ++$index);
$attr = $row[++$index]; $attr = data_get($row, ++$index);
$spec = $row[++$index]; $spec = data_get($row, ++$index);
$part = $row[++$index]; $part = data_get($row, ++$index);
if (!$sn) { if (!$sn) {
throw new \Exception('编号必填'); throw new \Exception('编号必填');
@ -68,13 +68,13 @@ class ImportForm extends Form
} }
$attributes = [ $attributes = [
'category_id' => $category->id, 'category_id' => $category->id,
'brand_id' => $brand?->id, 'brand_id' => data_get($brand, 'id'),
'type_id' => $type?->id, 'type_id' => data_get($type, 'id'),
'name' => $name, 'name' => $name,
'description' => $description, 'description' => $description,
'cover_image' => $cover, 'cover_image' => $this->parseFile($cover),
'images' => $images, 'images' => $this->parseFiles($images),
'content' => $content, 'content' => $this->parseFiles($content),
'price' => $price, 'price' => $price,
'stock' => $stock, 'stock' => $stock,
'attr' => $this->parseAttr($attr), 'attr' => $this->parseAttr($attr),
@ -82,11 +82,7 @@ class ImportForm extends Form
'part' => $this->parseAttr($part), 'part' => $this->parseAttr($part),
]; ];
$goods = Goods::where('goods_sn', $sn)->first(); Goods::updateOrCreate(['goods_sn' => $sn], $attributes);
if (!$goods) {
$goods = new Goods();
}
$goods->updateQuietly($attributes);
} }
} }
@ -103,7 +99,7 @@ class ImportForm extends Form
$matches = [$str]; $matches = [$str];
} }
foreach($matches as $item) { foreach($matches as $item) {
preg_match_all('/(.*)\[(.*)\]/', $item[0], $itemMatch); preg_match_all('/(.*)\[(.*)\]/', $item, $itemMatch);
$name = data_get($itemMatch, '1.0'); $name = data_get($itemMatch, '1.0');
$values = []; $values = [];
foreach(explode(',', data_get($itemMatch, '2.0')) as $k) { foreach(explode(',', data_get($itemMatch, '2.0')) as $k) {
@ -118,4 +114,33 @@ class ImportForm extends Form
return $attr; return $attr;
} }
protected function parseFile($path = null)
{
if (!$path) {
return null;
}
if (Str::startsWith($path, ['http://', 'https://'])) {
return $path;
}
$disk = Storage::disk('public');
return $disk->url($path);
}
protected function parseFiles($paths = null)
{
if (!$paths) {
return null;
}
if (!is_array($paths)) {
$paths = explode(',', $paths);
}
$data = [];
foreach($paths as $path) {
array_push($data, $this->parseFile($path));
}
return $data;
}
} }

View File

@ -6,6 +6,7 @@ use Dcat\Admin\Admin;
use Dcat\Admin\Form; use Dcat\Admin\Form;
use Dcat\Admin\Grid; use Dcat\Admin\Grid;
use Dcat\Admin\Grid\Displayers\Actions; use Dcat\Admin\Grid\Displayers\Actions;
use Dcat\Admin\Grid\Tools\BatchActions;
use Dcat\Admin\Grid\Tools\Selector; use Dcat\Admin\Grid\Tools\Selector;
use Dcat\Admin\Http\Controllers\AdminController; use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content; use Dcat\Admin\Layout\Content;
@ -14,6 +15,8 @@ use Dcat\Admin\Show;
use Dcat\Admin\Show\Tools; use Dcat\Admin\Show\Tools;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use Peidikeji\Goods\Action\BatchGoodsDown;
use Peidikeji\Goods\Action\BatchGoodsUp;
use Peidikeji\Goods\Action\GridImportGoods; use Peidikeji\Goods\Action\GridImportGoods;
use Peidikeji\Goods\Action\RowGoodsSale; use Peidikeji\Goods\Action\RowGoodsSale;
use Peidikeji\Goods\Form\Goods\AttrForm; use Peidikeji\Goods\Form\Goods\AttrForm;
@ -123,9 +126,9 @@ class GoodsController extends AdminController
$grid->column('brand.name'); $grid->column('brand.name');
$grid->column('type.name')->label(); $grid->column('type.name')->label();
$grid->column('name')->display(function () { $grid->column('name')->display(function () {
return ($this->cover_image ? '<img src="'.$this->cover_image.'" width="60" class="img-thumbnail"/>&nbsp;' : '').'<a href="'.admin_url( return ($this->cover_image ? '<img src="' . $this->cover_image . '" width="60" class="img-thumbnail"/>&nbsp;' : '') . '<a href="' . admin_url(
'goods/'.$this->id 'goods/' . $this->id
).'">'.$this->name.'</a>'; ) . '">' . $this->name . '</a>';
}); });
$grid->column('price'); $grid->column('price');
$grid->column('stock') $grid->column('stock')
@ -137,7 +140,6 @@ class GoodsController extends AdminController
$grid->column('is_recommend')->switch(); $grid->column('is_recommend')->switch();
$grid->column('sold_count'); $grid->column('sold_count');
$grid->disableRowSelector();
$grid->createMode(Grid::CREATE_MODE_DEFAULT); $grid->createMode(Grid::CREATE_MODE_DEFAULT);
$user = Admin::user(); $user = Admin::user();
@ -149,24 +151,35 @@ class GoodsController extends AdminController
$actions->view($user->can('dcat.admin.goods.show')); $actions->view($user->can('dcat.admin.goods.show'));
$actions->edit($user->can('dcat.admin.goods.edit') && ! $row->on_sale); $actions->edit($user->can('dcat.admin.goods.edit') && !$row->on_sale);
if ($user->can('dcat.admin.goods.edit') && ! $row->on_sale) { if ($user->can('dcat.admin.goods.edit') && !$row->on_sale) {
$actions->append('<a href="'.admin_route('goods.attr', ['goods' => $row->id]).'" class="">属性介绍</a>'); $actions->append('<a href="' . admin_route('goods.attr', ['goods' => $row->id]) . '" class="">属性介绍</a>');
$actions->append('<a href="'.admin_route('goods.spec', ['goods' => $row->id]).'" class="">商品规格</a>'); $actions->append('<a href="' . admin_route('goods.spec', ['goods' => $row->id]) . '" class="">商品规格</a>');
$actions->append('<a href="'.admin_route('goods.part', ['goods' => $row->id]).'" class="">商品配件</a>'); $actions->append('<a href="' . admin_route('goods.part', ['goods' => $row->id]) . '" class="">商品配件</a>');
} }
if ($row->spec) { if ($row->spec) {
$actions->append('<a href="'.admin_route('goods_sku.index', ['goods' => $row->id]).'" class="">货品列表</a>'); $actions->append('<a href="' . admin_route('goods_sku.index', ['goods' => $row->id]) . '" class="">货品列表</a>');
} }
if ($user->can('dcat.admin.goods.edit')) { if ($user->can('dcat.admin.goods.edit')) {
$actions->append(new RowGoodsSale()); $actions->append(new RowGoodsSale());
} }
$actions->delete($user->can('dcat.admin.goods.destroy') && ! $row->on_sale); $actions->delete($user->can('dcat.admin.goods.destroy') && !$row->on_sale);
});
$grid->tools(function (Grid\Tools $tools) use ($user) {
if ($user->can('dcat.admin.goods.import')) {
$tools->append(new GridImportGoods());
}
});
$grid->batchActions(function (BatchActions $batch) use ($user) {
if ($user->can('dcat.admin.goods.edit')) {
$batch->add(new BatchGoodsUp());
$batch->add(new BatchGoodsDown());
}
$batch->disableDelete($user->cannot('dcat.admin.goods.destroy'));
}); });
$grid->tools(new GridImportGoods());
}); });
} }
@ -210,7 +223,7 @@ class GoodsController extends AdminController
$tools->disableDelete(); $tools->disableDelete();
$tools->disableBack(); $tools->disableBack();
}); });
$show->field('goods_sn'); $show->field('goods_sn');
$show->field('brand.name'); $show->field('brand.name');
$show->field('type.name'); $show->field('type.name');
@ -243,27 +256,37 @@ class GoodsController extends AdminController
$form->select('brand_id')->options(GoodsBrand::pluck('name', 'id')); $form->select('brand_id')->options(GoodsBrand::pluck('name', 'id'));
$form->text('name')->required(); $form->text('name')->required();
$form->text('description'); $form->text('description');
$form->text('goods_sn')->rules([$unique], [ $form->text('goods_sn')->rules([$unique], ['unique' => '商品编号已经存在']);
'unique' => '商品编号已经存在',
]);
$form->image('cover_image') $form->image('cover_image')
->autoUpload() ->uniqueName()
->saveFullUrl() ->autoUpload()
->move('goods/cover-image') ->saveFullUrl()
->required(); ->retainable()
->removable(false)
->autoSave(false)
->move('goods/cover-image')
->required();
$form->multipleImage('images') $form->multipleImage('images')
->autoUpload() ->uniqueName()
->saveFullUrl() ->autoUpload()
->move('goods/images'); ->saveFullUrl()
->retainable()
->removable(false)
->autoSave(false)
->move('goods/images');
$form->multipleImage('content') $form->multipleImage('content')
->autoUpload() ->uniqueName()
->saveFullUrl() ->autoUpload()
->move('goods/content'); ->saveFullUrl()
->retainable()
->removable(false)
->autoSave(false)
->move('goods/content');
if ($isCreating || !$model->spec) { if ($isCreating || !$model->spec) {
$form->currency('price')->symbol('¥'); $form->currency('price')->symbol('¥');
} else { } else {
$form->display('help', '提示')->value('商品其他信息, 请到 <a href="'.admin_route('goods_sku.index', ['goods' => $model->id]).'" target="_blank">货品列表<a/> 去修改'); $form->display('help', '提示')->value('商品其他信息, 请到 <a href="' . admin_route('goods_sku.index', ['goods' => $model->id]) . '" target="_blank">货品列表<a/> 去修改');
} }
$form->hidden('stock')->default(0); $form->hidden('stock')->default(0);
@ -276,7 +299,7 @@ class GoodsController extends AdminController
$form->disableViewButton(); $form->disableViewButton();
$form->creating(function (Form $form) { $form->creating(function (Form $form) {
if (! $form->goods_sn) { if (!$form->goods_sn) {
$form->goods_sn = GoodsService::make()->generateSn(); $form->goods_sn = GoodsService::make()->generateSn();
} }
}); });