4
0
Fork 0

Compare commits

..

No commits in common. "e3b3dc221f297162127bbc91a463ef76a4b3e0a4" and "7e0c93cda13bb471e76aa05c2b2278e73386eabf" have entirely different histories.

7 changed files with 13 additions and 97 deletions

Binary file not shown.

View File

@ -3,9 +3,6 @@
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
@ -15,18 +12,9 @@ class BatchGoodsDown extends BatchAction
public function handle()
{
$ids = $this->getKey();
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());
}
Goods::whereIn('id', $ids)->update(['on_sale' => 0]);
return $this->response()->success('操作成功')->refresh();
}
public function confirm()

View File

@ -3,9 +3,6 @@
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
@ -15,18 +12,9 @@ class BatchGoodsUp extends BatchAction
public function handle()
{
$ids = $this->getKey();
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());
}
Goods::whereIn('id', $ids)->update(['on_sale' => 1]);
return $this->response()->success('操作成功')->refresh();
}
public function confirm()

View File

@ -3,8 +3,6 @@
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
@ -17,18 +15,9 @@ class RowGoodsSale extends RowAction
public function handle()
{
$info = Goods::findOrFail($this->getKey());
$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());
}
Goods::where('id', $this->getKey())->update(['on_sale' => ! $info->on_sale]);
return $this->response()->success('操作成功')->refresh();
}
public function confirm()

View File

@ -1,23 +0,0 @@
<?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,15 +4,14 @@ 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()
public static function make(...$params)
{
return new static();
return new static(...$params);
}
public function generateSn()
@ -28,8 +27,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 = [])
{
@ -73,29 +72,4 @@ 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->spec)
->if(fn () => $this->skus->count() > 0)
->display(fn () => $this->skus->sum('stock'))
->else()
->editable();