Admin 店铺导入商品 选择商品分类
parent
1089e782fd
commit
1a2ae7e17b
|
|
@ -2,49 +2,28 @@
|
|||
|
||||
namespace App\Admin\Actions\Store;
|
||||
|
||||
use App\Models\ProductSku;
|
||||
use App\Models\Store\ProductSku as StoreProductSku;
|
||||
use App\Models\Store\Store;
|
||||
use App\Admin\Forms\Store\ExportGoodsForm;
|
||||
use Dcat\Admin\Show\AbstractTool;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
|
||||
class ExportGoodsSpu extends AbstractTool
|
||||
{
|
||||
protected $title = '导入全站商品';
|
||||
protected $title = '导入商品';
|
||||
|
||||
protected $style = 'btn btn-sm btn-primary mr-1';
|
||||
|
||||
protected $amount = 10000;
|
||||
|
||||
public function handle()
|
||||
public function html()
|
||||
{
|
||||
$id = $this->getKey();
|
||||
$store = Store::findOrFail($id);
|
||||
$form = ExportGoodsForm::make(['store_id' => $this->getKey()]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$skuIds = StoreProductSku::where('store_id', $store->id)->pluck('product_sku_id');
|
||||
$skus = ProductSku::online()->whereNotIn('id', $skuIds)->get();
|
||||
foreach($skus as $sku) {
|
||||
StoreProductSku::create([
|
||||
'store_id' => $store->id,
|
||||
'product_sku_id' => $sku->id,
|
||||
'status' => 1,
|
||||
'amount' => $this->amount,
|
||||
]);
|
||||
}
|
||||
$form->disableResetButton();
|
||||
|
||||
DB::commit();
|
||||
return $this->response()->success('导入 '.$skus->count().' 个商品')->refresh();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
report($e);
|
||||
return $this->response()->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function confirm()
|
||||
{
|
||||
return ['是否确定?', '导入全部上架的商品, 库存为 ' . $this->amount . ', 店铺已经存在的商品会被忽略'];
|
||||
return Modal::make()
|
||||
->lg()
|
||||
->title($this->title)
|
||||
->body($form)
|
||||
->button("<a href=\"javascript:void(0)\" class=\"{$this->style}\">{$this->title}</a>");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Forms\Store;
|
||||
|
||||
use App\Models\ProductSku as ModelsProductSku;
|
||||
use App\Models\Store\ProductSku;
|
||||
use App\Models\Store\Store;
|
||||
use Dcat\Admin\Contracts\LazyRenderable;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ExportGoodsForm extends Form
|
||||
{
|
||||
public function handle(array $input)
|
||||
{
|
||||
$store = Store::findOrFail($input['store_id']);
|
||||
$categotyId = $input['category_id'];
|
||||
$amount = $input['amount'];
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
// 店铺已有的商品
|
||||
$skuIds = ProductSku::where('store_id', $store->id)->pluck('product_sku_id');
|
||||
|
||||
$query = ModelsProductSku::online();
|
||||
// 过滤掉已有的商品
|
||||
$query->whereNotIn('id', $skuIds);
|
||||
|
||||
// 选择某一分类
|
||||
if ($categotyId) {
|
||||
$query->where('category_id', $categotyId);
|
||||
}
|
||||
|
||||
$skus = $query->get();
|
||||
foreach($skus as $sku) {
|
||||
ProductSku::create([
|
||||
'store_id' => $store->id,
|
||||
'product_sku_id' => $sku->id,
|
||||
'status' => 1,
|
||||
'amount' => $amount,
|
||||
]);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
return $this->response()->success('导入 '.$skus->count().' 个商品')->refresh();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
report($e);
|
||||
return $this->response()->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
$storeId = $this->model()->store_id;
|
||||
$this->hidden('store_id')->default($storeId);
|
||||
$this->select('category_id', '分类')->options(admin_route('api.product_categories', ['level' => 2]));
|
||||
$this->number('amount', '默认库存')->min(0)->default(1000);
|
||||
// $this->display('product', '已选商品数量')->value(1000);
|
||||
$this->display('store_product', '店铺已有商品')->value(ProductSku::where('store_id', $storeId)->count())->help('导入商品会忽略已存在的商品');
|
||||
// $this->display('export_product', '即将导入商品')->value(1000);
|
||||
}
|
||||
}
|
||||
|
|
@ -26,8 +26,8 @@ class OrderProfitResource extends JsonResource
|
|||
|
||||
'growth_value' => (float)$this->growth_value,
|
||||
'ratio' => $this->ratio,
|
||||
'money' => (int)$this->money,
|
||||
'sub_money' => (int)$this->sub_money,
|
||||
'money' => (float)$this->money,
|
||||
'sub_money' => (float)$this->sub_money,
|
||||
'status' => $this->status,
|
||||
'created_at' => $this->created_at->timestamp,
|
||||
'paid_at' => $this->paid_at?->timestamp,
|
||||
|
|
|
|||
Loading…
Reference in New Issue