4
0
Fork 0

exception

master
panliang 2022-10-12 11:15:57 +08:00
parent 7e0c93cda1
commit d7460353ce
6 changed files with 97 additions and 13 deletions

View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -0,0 +1,23 @@
<?php
namespace Peidikeji\Goods\Exceptions;
use Dcat\Admin\Traits\JsonResponse;
use Exception;
class GoodsException extends Exception
{
use JsonResponse;
protected $code = 400;
public function report()
{
return false;
}
public function render($request)
{
return $this->error($this->message, $this->code);
}
}

View File

@ -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]);
}
}

View File

@ -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();