6
0
Fork 0
jiqu-library-server/app/Admin/Forms/Store/ExportGoodsForm.php

64 lines
2.2 KiB
PHP

<?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);
}
}