diff --git a/src/Action/BatchGoodsDown.php b/src/Action/BatchGoodsDown.php index f16526a..556a9e4 100644 --- a/src/Action/BatchGoodsDown.php +++ b/src/Action/BatchGoodsDown.php @@ -3,6 +3,9 @@ namespace Peidikeji\Goods\Action; use Dcat\Admin\Grid\BatchAction; +use Illuminate\Support\Facades\DB; +use Peidikeji\Goods\Exceptions\GoodsException; +use Peidikeji\Goods\GoodsService; use Peidikeji\Goods\Models\Goods; class BatchGoodsDown extends BatchAction @@ -12,9 +15,18 @@ class BatchGoodsDown extends BatchAction public function handle() { $ids = $this->getKey(); - Goods::whereIn('id', $ids)->update(['on_sale' => 0]); - - return $this->response()->success('操作成功')->refresh(); + try { + DB::beginTransaction(); + $service = GoodsService::make(); + foreach(Goods::whereIn('id', $ids)->get() as $item) { + $service->downSale($item); + } + DB::commit(); + return $this->response()->success('操作成功')->refresh(); + } catch (GoodsException $e) { + DB::rollBack(); + return $this->response()->error($e->getMessage()); + } } public function confirm() diff --git a/src/Action/BatchGoodsUp.php b/src/Action/BatchGoodsUp.php index ce4d803..34069c2 100644 --- a/src/Action/BatchGoodsUp.php +++ b/src/Action/BatchGoodsUp.php @@ -3,6 +3,9 @@ namespace Peidikeji\Goods\Action; use Dcat\Admin\Grid\BatchAction; +use Illuminate\Support\Facades\DB; +use Peidikeji\Goods\Exceptions\GoodsException; +use Peidikeji\Goods\GoodsService; use Peidikeji\Goods\Models\Goods; class BatchGoodsUp extends BatchAction @@ -12,9 +15,18 @@ class BatchGoodsUp extends BatchAction public function handle() { $ids = $this->getKey(); - Goods::whereIn('id', $ids)->update(['on_sale' => 1]); - - return $this->response()->success('操作成功')->refresh(); + try { + DB::beginTransaction(); + $service = GoodsService::make(); + foreach(Goods::whereIn('id', $ids)->get() as $item) { + $service->upSale($item); + } + DB::commit(); + return $this->response()->success('操作成功')->refresh(); + } catch (GoodsException $e) { + DB::rollBack(); + return $this->response()->error($e->getMessage()); + } } public function confirm() diff --git a/src/Action/RowGoodsSale.php b/src/Action/RowGoodsSale.php index ffcc4a5..2ffd62b 100644 --- a/src/Action/RowGoodsSale.php +++ b/src/Action/RowGoodsSale.php @@ -3,6 +3,8 @@ namespace Peidikeji\Goods\Action; use Dcat\Admin\Grid\RowAction; +use Peidikeji\Goods\Exceptions\GoodsException; +use Peidikeji\Goods\GoodsService; use Peidikeji\Goods\Models\Goods; class RowGoodsSale extends RowAction @@ -15,9 +17,18 @@ class RowGoodsSale extends RowAction public function handle() { $info = Goods::findOrFail($this->getKey()); - Goods::where('id', $this->getKey())->update(['on_sale' => ! $info->on_sale]); + $service = GoodsService::make(); + try { + if ($info->on_sale) { + $service->downSale($info); + } else { + $service->upSale($info); + } + return $this->response()->success('操作成功')->refresh(); + } catch(GoodsException $e) { + return $this->response()->error($e->getMessage()); + } - return $this->response()->success('操作成功')->refresh(); } public function confirm() diff --git a/src/Exceptions/GoodsException.php b/src/Exceptions/GoodsException.php new file mode 100644 index 0000000..7b188f5 --- /dev/null +++ b/src/Exceptions/GoodsException.php @@ -0,0 +1,23 @@ +error($this->message, $this->code); + } +} diff --git a/src/GoodsService.php b/src/GoodsService.php index 2268618..022d1e0 100644 --- a/src/GoodsService.php +++ b/src/GoodsService.php @@ -4,14 +4,15 @@ namespace Peidikeji\Goods; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Peidikeji\Goods\Exceptions\GoodsException; use Peidikeji\Goods\Models\Goods; use Peidikeji\Goods\Models\GoodsSku; class GoodsService { - public static function make(...$params) + public static function make() { - return new static(...$params); + return new static(); } public function generateSn() @@ -27,8 +28,8 @@ class GoodsService /** * 根据规格生成SKU * - * @param Goods $goods 商品 - * @param array $options {spec: 指定规格, price: 基础价格, name: 基础名称, stock: 默认库存, name_add: 是否在名称上面追加属性值, price_add: 是否在价格上面追加属性的加价} + * @param Goods $goods 商品 + * @param array $options {spec: 指定规格, price: 基础价格, name: 基础名称, stock: 默认库存, name_add: 是否在名称上面追加属性值, price_add: 是否在价格上面追加属性的加价} */ public function generateSku(Goods $goods, $options = []) { @@ -72,4 +73,29 @@ class GoodsService } } } + + /** + * 商品上架销售 + * + * @param Goods $goods 商品 + * @throws GoodsException + */ + public function upSale(Goods $goods) + { + if ($goods->spec && $goods->skus()->count() === 0) { + throw new GoodsException($goods->name . ' 必需生成货品'); + } + + $goods->update(['on_sale' => 1]); + } + + /** + * 商品下架 + * + * @param Goods $goods 商品 + */ + public function downSale(Goods $goods) + { + $goods->update(['on_sale' => 0]); + } } diff --git a/src/Http/Admin/GoodsController.php b/src/Http/Admin/GoodsController.php index 63e245d..cece537 100644 --- a/src/Http/Admin/GoodsController.php +++ b/src/Http/Admin/GoodsController.php @@ -132,7 +132,7 @@ class GoodsController extends AdminController }); $grid->column('price'); $grid->column('stock') - ->if(fn () => $this->skus->count() > 0) + ->if(fn () => !!$this->spec) ->display(fn () => $this->skus->sum('stock')) ->else() ->editable();