51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Actions\Store;
|
|
|
|
use App\Models\ProductSku;
|
|
use App\Models\Store\ProductSku as StoreProductSku;
|
|
use App\Models\Store\Store;
|
|
use Dcat\Admin\Show\AbstractTool;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ExportGoodsSpu extends AbstractTool
|
|
{
|
|
protected $title = '导入全站商品';
|
|
|
|
protected $style = 'btn btn-sm btn-primary mr-1';
|
|
|
|
protected $amount = 10000;
|
|
|
|
public function handle()
|
|
{
|
|
$id = $this->getKey();
|
|
$store = Store::findOrFail($id);
|
|
|
|
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,
|
|
]);
|
|
}
|
|
|
|
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 . ', 店铺已经存在的商品会被忽略'];
|
|
}
|
|
}
|