disableCreateButton(); //如果是审核页面,多加where条件判断 if (strpos(Route::current()->uri, 'audit')) { $grid->model()->where('status', ProductStatus::UNAUDITED); } $category_id = request()->input('cid'); if ($category_id) { $grid->model()->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]); } /*$grid->batchActions([ new BatchAuditProduct(null, 1), new BatchAuditProduct(null, 2), ]);*/ $grid->column('id')->sortable(); $grid->column('category.name', '分类'); $grid->column('picture')->image('', 60, 60); $grid->column('title')->limit(15); $grid->column('original_price'); $grid->column('price'); $grid->column('sale'); $grid->column('stock'); $grid->column('supplier.company_name', '供应商'); $grid->column('status') ->if(fn() => $this->status == ProductStatus::UNAUDITED) ->display('') ->then(function ($column) { $column->append((new AuditProduct(null, 1))->setKey($this->id))->append(' '); $column->append((new AuditProduct(null, 2))->setKey($this->id)); }) ->else() ->using(ProductStatus::array()) ->dot([ ProductStatus::ON_SALE => 'success', ProductStatus::UNAUDITED => '', ProductStatus::REFUSE => 'danger', ProductStatus::SOLD_OUT => 'warning', ], 'primary'); $grid->column('single_deposit')->editable(); $grid->column('created_at'); $grid->column('updated_at'); $grid->filter(function (Grid\Filter $filter) { $filter->panel(); $filter->equal('id')->width(2); $filter->like('title')->width(3); $filter->equal('status')->select(ProductStatus::array())->width(2); $options = array_slice(Category::selectOptions(fn($query) => $query->where('agent_id', 0)), 1, null, true); $filter->equal('cid', '产品分类')->ignore()->select($options)->width(3); $filter->equal('type')->select(admin_trans('product.options.publish_type'))->width(2); }); }); } //递归获取指定分类下的所有子分类 private function get_category_child_ids($category_id): array { static $category = null; if ($category === null) { $category = Category::where('agent_id', 0)->get()->toArray(); } $child = []; foreach ($category as $cat) { if ($cat['pid'] == $category_id) { $child[] = $cat['id']; $child = array_merge($child, $this->get_category_child_ids($cat['id'])); } } return $child; } /** * Make a show builder. * * @param mixed $id * * @return Show */ protected function detail($id) { return Show::make($id, new Product(['supplier:id,company_name', 'category:id,name']), function (Show $show) { $show->field('id'); $show->field('category.name', '所属分类'); $show->field('title'); $show->field('pictures')->image('', 80, 80); $show->field('original_price'); $show->field('price'); $show->field('sale'); $show->field('stock'); $show->field('status')->using(ProductStatus::array()); $show->field('supplier.company_name', '供应商'); $show->field('know')->unescape()->as(fn($v) => preg_replace('/.*?<\/script>/is', '', $v)); $show->field('content')->unescape()->as(fn($v) => preg_replace('/.*?<\/script>/is', '', $v)); $show->field('created_at'); $show->field('updated_at'); }); } /** * Make a form builder. * * @return Form */ protected function form() { return Form::make(new Product(), function (Form $form) { $form->display('id'); $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0)); $form->select('category_id', '所属分类') ->options(array_slice($options, 1, null, true)); $form->text('title')->required(); $form->multipleImage('pictures')->required()->removable(false)->uniqueName(); $form->text('original_price')->required(); $form->text('price')->required(); $form->text('sale')->default(0); $form->text('stock')->default(9999)->required(); $form->select('status') ->options(ProductStatus::array()) ->default(ProductStatus::ON_SALE) ->required(); $form->editor('know'); $form->editor('content'); $form->display('verify_mobile')->required(); $form->radio('type', '产品类型') ->options(admin_trans('product.options.publish_type'))->disable($form->isEditing()) ->default(current(admin_trans('product.options.publish_type'))) ->when(0, function (Form $form) { //旅游线路 if ($form->isEditing() && $form->model()->type != 0) { return; } $form->table('extends.field_0_project', '包含项目', function (NestedForm $table) { $table->text('name', '字段1'); $table->text('num', '字段2'); $table->text('price', '字段3'); })->help('第一行数据默认是表头,如:项目名称、数量、额外费用'); $form->dateRange('extends.field_0_date.start', 'extends.field_0_date.end', '行程时间'); })->when(1, function (Form $form) { //酒店 if ($form->isEditing() && $form->model()->type != 1) { return; } $default = [ ['tag' => '行李寄存'], ['tag' => '24小时前台'], ['tag' => '前台保险柜'], ['tag' => '唤醒服务'], ['tag' => '早餐'], ['tag' => '送餐服务'], ['tag' => '电梯'], ['tag' => '空调'], ['tag' => '新风系统'], ['tag' => '24小时热水'], ['tag' => '吹风机'], ['tag' => '加湿器'], ['tag' => '自动售货机'], ['tag' => '健身房'], ['tag' => '桌球室'], ['tag' => '洗衣服务'] ]; $form->table('extends.field_1_tags', '酒店设施', function (NestedForm $table) { $table->text('tag', '包含项目')->placeholder('如:24小时热水、干洗服务等'); })->value($default)->help('首次创建时,系统会默认填充基本服务,请根据本酒店情况进行删减或新增'); $form->text('extends.field_1_name', '酒店名'); $form->text('extends.field_1_address', '地址'); $form->map('extends.field_1_latitude', 'extends.field_1_longitude', '位置'); })->when(2, function (Form $form) { //景区 if ($form->isEditing() && $form->model()->type != 2) { return; } $form->table('extends.field_2_open_time', '开放时间', function (NestedForm $table) { $table->text('node', '字段1')->placeholder('如:周一至周五'); $table->text('summer', '字段2')->placeholder('如:08:00~19:00'); $table->text('winter', '字段3')->placeholder('如:08:00~18:00'); })->help('第一行数据默认是表头,如:项目名称、数量、额外费用'); $form->table('extends.field_2_project', '包含项目', function (NestedForm $table) { $table->text('name', '字段1'); $table->text('num', '字段2'); $table->text('price', '字段3'); })->help('第一行数据默认是表头,如:项目名称、数量、额外费用'); $form->text('extends.field_2_name', '景区名'); $form->text('extends.field_2_address', '地址'); $form->map('extends.field_2_latitude', 'extends.field_2_longitude', '位置'); })->when(3, function (Form $form) { //餐厅 if ($form->isEditing() && $form->model()->type != 3) { return; } $form->table('extends.field_3_open_time', '开放时间', function (NestedForm $table) { $table->text('week', '字段1')->placeholder('如:周一至周五'); $table->text('section', '字段2')->placeholder('如:上午/下午'); $table->text('time', '字段3')->placeholder('如:08:00~18:00'); })->help('第一行数据默认是表头,如:项目名称、数量、额外费用'); $form->table('extends.field_3_package', '包含套餐', function (NestedForm $table) { $table->text('name', '字段1')->placeholder('如:清蒸鱿鱼'); $table->text('num', '字段2')->placeholder('如:1条'); $table->text('price', '字段3')->placeholder('如:99元'); })->help('第一行数据默认是表头,如:项目名称、数量、额外费用'); $form->text('extends.field_3_name', '餐厅名'); $form->text('extends.field_3_address', '地址'); $form->map('extends.field_3_latitude', 'extends.field_3_longitude', '位置'); }); })->saving(function (Form $form) { //不允许编辑的字段 if ($form->isEditing()) { $form->ignore(['id', 'created_at', 'updated_at', 'deleted_at']); //列表页编辑交易金 if (!is_null($form->single_deposit)) { $form->model()->update(['single_deposit' => $form->single_deposit]); return $form->response()->success('设置交易金成功'); } } //特殊字段处理 $form->sale = $form->sale ?? 0; //过滤null字段 foreach ($form->input() as $k => $v) { if (is_null($v)) { $form->$k = ''; } } })->saved(function(Form $form) { //如果是审核的产品,将产品同步到代理商产品 if ($form->isEditing() && $form->status == ProductStatus::ON_SALE) { $ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray(); //同步信息到代理商产品,注:组合产品不同步 AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([ 'title' => $form->title, 'pictures' => explode(',', $form->pictures), 'know' => $form->know, 'content' => $form->content, ]); } }); } public function audit() { $cloud = \App\Models\Product::query()->where('status',ProductStatus::UNAUDITED)->count(); $industry = \App\Models\IndustryProduct::query()->where('status',ProductStatus::UNAUDITED)->count(); $demand = \App\Models\DemandProduct::query()->where('status',ProductStatus::UNAUDITED)->count(); $total = $cloud + $industry + $demand; $arr = [ 'cloud' => $cloud ?? 0, 'industry' => $industry ?? 0, 'demand' => $demand ?? 0, 'total' => $total ?? 0, ]; return json_encode($arr); } }