model()->where('goods_id', $goods->id); $grid->selector(function (Selector $selector) use ($goods) { $specs = $goods->spec; if ($specs) { foreach ($specs as $key => $item) { $values = array_column($item['values'], 'name'); $selector->selectOne('spec_'.$key, $item['name'], array_column($item['values'], 'name'), function ($q, $value) use ($values, $item) { $selected = array_values(Arr::only($values, $value)); if (count($selected) > 0) { $q->jsonArray([['name' => $item['name'], 'value' => $selected[0]]]); } }); } } }); $grid->column('id'); $grid->column('sn'); $grid->column('name'); $grid->column('price'); $grid->column('stock'); if ($goods->spec) { foreach ($goods->spec as $key => $item) { $grid->column('spec_'.$key, $item['name'])->display(function () use ($item) { $filtered = current(array_filter($this->spec, fn ($subItem) => $subItem['name'] === $item['name'])); $value = data_get($filtered, 'value'); $price = data_get($filtered, 'price'); return ''.$value.''; }); } } $user = Admin::user(); $grid->showCreateButton(!$goods->on_sale && $user->can('dcat.admin.goods_sku.create')); $grid->showDeleteButton(!$goods->on_sale && $user->can('dcat.admin.goods_sku.destroy')); $grid->showEditButton(!$goods->on_sale && $user->can('dcat.admin.goods_sku.edit')); $grid->showViewButton($user->can('dcat.admin.goods_sku.show')); }); return $content ->translation($this->translation) ->title($goods->name) ->description(admin_trans_label()) ->body($grid); } public function show($goods, $id, Content $content) { Admin::css([ 'vendor/dcat-admin-goods/goods.css', ]); $goods = Goods::findOrFail($goods); $info = GoodsSku::findOrFail($id); $show = Show::make($info, function (Show $show) { $show->field('sn'); $show->field('name'); $show->field('price'); $show->field('stock'); $show->field('spec')->view('dcat-admin-goods::goods.grid-spec'); $show->disableDeleteButton(); $show->disableEditButton(); }); return $content ->translation($this->translation) ->title(admin_trans_label()) ->description(trans('admin.show')) ->body($show); } protected function editForm($goods) { return Form::make(new GoodsSku(), function (Form $form) use ($goods) { $unqiue = Rule::unique('goods_sku', 'sn')->ignore($form->model()->id); $form->text('sn')->rules([$unqiue], ['unique' => '货号已经存在'])->required(); $form->text('name')->default($goods->name); $form->currency('price')->symbol('¥'); $form->number('stock')->min(0)->default($goods->stock); $form->hidden('spec')->customFormat(fn ($v) => json_encode($v)); $form->hidden('goods_id')->default($goods->id); $spec = $form->model()->spec; if ($goods->spec) { foreach ($goods->spec as $item) { $values = array_column($item['values'], 'name', 'name'); $value = null; if ($spec) { $filtered = current(array_filter($spec, fn ($subItem) => $subItem['name'] === $item['name'])); $value = array_search($filtered['value'], $values); } $form->radio($item['name'], $item['name'])->options($values)->value($value); } } $form->saving(function (Form $form) use ($goods) { $info = $form->model(); $spec = []; if ($goods->spec) { foreach ($goods->spec as $item) { array_push($spec, ['name' => $item['name'], 'value' => $form->input($item['name'])]); $form->deleteInput($item['name']); } } $form->input('spec', $spec); $query = GoodsSku::where('goods_id', $goods->id)->jsonArray($spec); if ($form->isEditing()) { $query->where('id', '!=', $info->id); } if ($query->exists()) { return $form->response()->error('该规格已经存在'); } }); $form->disableCreatingCheck(); $form->disableEditingCheck(); $form->disableViewCheck(); $form->disableResetButton(); $form->disableViewButton(); $form->disableListButton(); $form->disableDeleteButton(); }); } public function edit($goods, $id, Content $content) { $goods = Goods::findOrFail($goods); return $content ->translation($this->translation) ->title(admin_trans_label()) ->description(trans('admin.edit')) ->body($this->editForm($goods)->edit($id)); } public function create($goods, Content $content) { $goods = Goods::findOrFail($goods); return $content ->translation($this->translation) ->title(__('dcat-admin-goods::goods-sku.labels.sku')) ->description(__('dcat-admin-goods::goods-sku.labels.create')) ->body($this->createForm($goods)); } protected function createForm($goods) { return Form::make(new GoodsSku(), function (Form $form) use ($goods) { $form->text('name')->default($goods->name); if ($goods->spec) { $form->checkbox('name_append', '')->options([1 => '是否在名称上面追加属性值']); } $form->currency('price')->symbol('¥')->default($goods->price); if ($goods->spec) { $form->checkbox('price_append', '')->options([1 => '是否在价格上面追加属性的加价'])->default([1]); } $form->number('stock')->min(0)->default($goods->stock); if ($goods->spec) { foreach ($goods->spec as $item) { $values = array_column($item['values'], 'name', 'name'); $form->checkbox($item['name'], $item['name'])->options($values); // $form->embeds('input_'.$item['name'], '自定义' . $item['name'], function (EmbeddedForm $form) use ($item) { // $form->text('', '属性值')->setElementName('input_'.$item['name'].'_name'); // $form->text('', '价格')->setElementName('input_'.$item['name'].'_price')->default(0); // }); } $form->checkbox('clear', '')->options([1 => '清空现有的货品'])->default(1); } $form->disableCreatingCheck(); $form->disableEditingCheck(); $form->disableViewCheck(); $form->disableResetButton(); $form->saving(function (Form $form) use ($goods) { $price = $form->price; $name = $form->name; $spec = []; if ($goods->spec) { $goodsSpecs = $goods->spec; foreach ($goodsSpecs as &$item) { // 自定义属性 $add = ['name' => '', 'value' => '']; if ($form->input('input_'.$item['name'].'_name')) { $add = ['name' => $form->input('input_'.$item['name'].'_name'), 'value' => round($form->input('input_'.$item['name'].'_value'), 2, PHP_ROUND_HALF_DOWN)]; } $values = $form->input($item['name']); // 过滤空值 $values = array_filter($values, fn ($v) => $v); // 补充自定义属性 if ($add['name']) { array_push($item['values'], $add); array_push($values, $add['name']); } if (count($values) === 0) { return $form->response()->error('请勾选 '.$item['name']); } array_push($spec, ['name' => $item['name'], 'values' => array_filter($item['values'], fn ($v) => in_array($v['name'], $values))]); $form->deleteInput($item['name']); } $goods->update(['spec' => $goodsSpecs]); } $service = GoodsService::make(); if ($form->clear) { $service->clearSku($goods); } $service->generateSku($goods, [ 'spec' => $spec, 'price' => $price, 'name' => $name, 'name_add' => (bool) data_get($form->name_append, 0), 'price_add' => (bool) data_get($form->price_append, 0), 'stock' => $form->stock, ]); return $form->response()->success('添加成功')->redirect(admin_route('goods_sku.index', ['goods' => $goods->id])); }); }); } public function update($goods, $id) { return $this->editForm(Goods::findOrFail($goods))->update($id); } public function store($goods) { return $this->createForm(Goods::findOrFail($goods))->store(); } public function destroy($goods, $id) { GoodsSku::where('id', $id)->delete(); return $this->editForm($goods)->response()->success('删除成功'); } }