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

82 lines
2.1 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
{
/**
* @return string
*/
protected $title = '<i class="feather icon-rotate-cw grid-action-icon"></i>';
public function title()
{
if ($this->title) {
return $this->title.'&nbsp;'.__('admin_message.extensions.grid.batch.sku_sync_spu');
}
return __('admin_message.extensions.grid.batch.sku_sync_spu');
}
/**
* 是否有权限判断.
*
* @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::syncSpu($sku);
}
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
return $this->response()->error('操作失败:'.$th->getMessage())->refresh();
}
$message = '操作成功';
return $this->response()->success($message)->refresh();
}
// 设置请求参数
public function parameters()
{
return [
];
}
}