109 lines
3.6 KiB
PHP
109 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace App\Admin\Extensions\Grid\Tools\Product;
|
||
|
||
use App\Models\ProductSku;
|
||
use App\Models\ProductSpu;
|
||
use Dcat\Admin\Grid\Tools\AbstractTool;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Arr;
|
||
|
||
class InitSkuBySpecs extends AbstractTool
|
||
{
|
||
public function __construct($spu_id = 0, $title=null)
|
||
{
|
||
parent::__construct($title);
|
||
$this->setKey($spu_id);
|
||
}
|
||
|
||
protected function authorize($user): bool
|
||
{
|
||
return $user->can('dcat.admin.product_spus.init_sku_by_specs');
|
||
}
|
||
|
||
/**
|
||
* 按钮样式定义,默认 btn btn-white waves-effect
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $style = 'btn btn-primary';
|
||
|
||
/**
|
||
* 按钮文本
|
||
*
|
||
* @return string|void
|
||
*/
|
||
public function title()
|
||
{
|
||
return __('admin_message.extensions.grid.tools.init_sku_by_specs');
|
||
}
|
||
|
||
public function confirm()
|
||
{
|
||
// 显示标题和内容
|
||
return [__('admin_message.extensions.grid.init_sku_by_specs.confirm.title'), __('admin_message.extensions.grid.init_sku_by_specs.confirm.message')];
|
||
}
|
||
|
||
/**
|
||
* 处理请求
|
||
* 如果你的类中包含了此方法,则点击按钮后会自动向后端发起ajax请求,并且会通过此方法处理请求逻辑
|
||
*
|
||
* @param Request $request
|
||
*/
|
||
public function handle(Request $request)
|
||
{
|
||
$nowTime = now();
|
||
//获取SPU
|
||
$spu = ProductSpu::with('specs')->findOrFail($this->getKey());
|
||
$skuSpecs = [];
|
||
foreach ($spu->specs as $spec) {
|
||
$_items = [];
|
||
foreach ($spec->items as $item) {
|
||
$_items[] = [
|
||
'id' => $spec->id,
|
||
'name'=>$item['name'],
|
||
'price'=>$item['value'],
|
||
];
|
||
}
|
||
$skuSpecs[] = $_items;
|
||
}
|
||
//生成规格笛卡尔集
|
||
$skuSpecs = Arr::crossJoin(...$skuSpecs);
|
||
$insertData = [];
|
||
//根据规格笛卡尔集生成预插入数据
|
||
foreach ($skuSpecs as $skuSpec) {
|
||
$_skuSpecs = [];
|
||
$_price = 0;
|
||
foreach ($skuSpec as $value) {
|
||
$_skuSpecs[$value['id']] = $value['name'];
|
||
$_price += $value['price'];
|
||
}
|
||
$insertData[] = [
|
||
'spu_id' => $spu->id,
|
||
'name' => trim($spu->name.' '.implode(' ', $_skuSpecs)),
|
||
'subtitle' => $spu->subtitle,
|
||
'category_id' => $spu->category_id,
|
||
'cover' => $spu->cover,
|
||
'images' => json_encode($spu->images),
|
||
'description' => $spu->description,
|
||
'sell_price' => bcadd($spu->sell_price, bcmul($_price, 100)),
|
||
'market_price' => $spu->market_price,
|
||
'cost_price' => $spu->cost_price,
|
||
'vip_price' => !is_null($spu->vip_price) ? bcadd($spu->vip_price, bcmul($_price, 100)) : null,
|
||
'media' => $spu->media,
|
||
'weight' => $spu->weight,
|
||
'attrs' => json_encode($spu->attrs),
|
||
'specs' => json_encode($_skuSpecs),
|
||
'stock' => $spu->stock,
|
||
'sales' => $spu->sales,
|
||
'buynote_id' => $spu->buynote_id,
|
||
'created_at' => $nowTime,
|
||
'updated_at' => $nowTime,
|
||
];
|
||
}
|
||
count($insertData)>0 && ProductSku::insert($insertData);
|
||
|
||
return $this->response()->success(__('admin.update_succeeded'))->refresh();
|
||
}
|
||
}
|