6
0
Fork 0
jiqu-library-server/app/Admin/Extensions/Grid/Tools/Product/BatchSkuSyncSpu.php

68 lines
1.6 KiB
PHP

<?php
namespace App\Admin\Extensions\Grid\Tools\Product;
use App\Models\ProductSku;
use Dcat\Admin\Grid\BatchAction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Throwable;
class BatchSkuSyncSpu extends BatchAction
{
protected $title = '同步主商品';
/**
* 是否有权限判断.
*
* @param Model|Authenticatable|HasPermissions|null $user
* @return bool
*/
protected function authorize($user): bool
{
return $user->can('dcat.admin.batch.sku_sync_spu');
}
// 确认弹窗信息
public function confirm()
{
return '您确定操作已选中的商品吗?';
}
// 处理请求
public function handle(Request $request)
{
// 获取选中的skuID数组
$keys = $this->getKey();
$query = ProductSku::with('spu')->whereIn('id', $keys)->where(function ($query) {
$query->where('verify_state', 0)->orWhere('verify_state', 2);
})->whereNull('release_at');
$list = $query->get();
if ($list->count() != count($keys)) {
return $this->response()->error('操作失败,所选商品含有不能操作项')->refresh();
}
try {
DB::beginTransaction();
foreach ($list as $sku) {
ProductSku::skuSyncSpu($sku);
}
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
}
$message = '操作成功';
return $this->response()->success($message)->refresh();
}
// 设置请求参数
public function parameters()
{
return [
];
}
}