You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							139 lines
						
					
					
						
							4.3 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							139 lines
						
					
					
						
							4.3 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\AdminSupplier\Controllers;
							 | 
						|
								
							 | 
						|
								use App\AdminSupplier\Repositories\DemandProduct;
							 | 
						|
								use App\Common\ProductStatus;
							 | 
						|
								use App\Models\AgentProduct;
							 | 
						|
								use App\Models\Category;
							 | 
						|
								use Dcat\Admin\Admin;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Repositories\EloquentRepository;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Illuminate\Support\Facades\DB;
							 | 
						|
								
							 | 
						|
								class DemandProductController extends AdminController
							 | 
						|
								{
							 | 
						|
									protected $title = '产品';
							 | 
						|
									/**
							 | 
						|
									 * Make a grid builder.
							 | 
						|
									 *
							 | 
						|
									 * @return Grid
							 | 
						|
									 */
							 | 
						|
									protected function grid()
							 | 
						|
									{
							 | 
						|
										return Grid::make(new DemandProduct(['category:id,name']), function (Grid $grid) {
							 | 
						|
											$grid->model()->where('supplier_id', Admin::user()->id);
							 | 
						|
								
							 | 
						|
											$grid->column('id')->sortable();
							 | 
						|
											$grid->column('category.name', '产品分类');
							 | 
						|
											$grid->column('title');
							 | 
						|
											$grid->column('picture')->image('', 60, 60);
							 | 
						|
											//$grid->column('price');
							 | 
						|
											//$grid->column('original_price');
							 | 
						|
											//$grid->column('stock');
							 | 
						|
											$grid->column('sale');
							 | 
						|
											$grid->column('status')->using(ProductStatus::array());
							 | 
						|
											$grid->column('created_at');
							 | 
						|
											$grid->column('updated_at');
							 | 
						|
											$grid->disableRowSelector();
							 | 
						|
											$grid->filter(function (Grid\Filter $filter) {
							 | 
						|
												$filter->equal('id');
							 | 
						|
								
							 | 
						|
											});
							 | 
						|
										});
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									/**
							 | 
						|
									 * Make a show builder.
							 | 
						|
									 *
							 | 
						|
									 * @param mixed $id
							 | 
						|
									 *
							 | 
						|
									 * @return Show
							 | 
						|
									 */
							 | 
						|
									protected function detail($id)
							 | 
						|
									{
							 | 
						|
										return Show::make($id, new DemandProduct(), function (Show $show) {
							 | 
						|
											$show->field('id');
							 | 
						|
											$show->field('supplier_id');
							 | 
						|
											$show->field('category_id');
							 | 
						|
											$show->field('title');
							 | 
						|
											//$show->field('price');
							 | 
						|
											//$show->field('original_price');
							 | 
						|
											$show->field('pictures')->image('', 80, 80);
							 | 
						|
											//$show->field('stock');
							 | 
						|
											$show->field('sale');
							 | 
						|
											$show->field('status');
							 | 
						|
											$show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
							 | 
						|
											$show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
							 | 
						|
											$show->field('created_at');
							 | 
						|
											$show->field('updated_at');
							 | 
						|
										});
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									/**
							 | 
						|
									 * Make a form builder.
							 | 
						|
									 *
							 | 
						|
									 * @return Form
							 | 
						|
									 */
							 | 
						|
									protected function form()
							 | 
						|
									{
							 | 
						|
										return Form::make(new DemandProduct(), function (Form $form) {
							 | 
						|
											//不允许编辑非自己数据
							 | 
						|
											if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$form->display('id');
							 | 
						|
								
							 | 
						|
											$options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
							 | 
						|
											$form->select('category_id')->options(array_slice($options, 1, null, true))->required();
							 | 
						|
											$form->text('title')->required();
							 | 
						|
											//$form->text('price')->required();
							 | 
						|
											//$form->text('original_price')->required();
							 | 
						|
											$form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
							 | 
						|
											//$form->text('stock')->default(9999)->required();
							 | 
						|
											$form->editor('know');
							 | 
						|
											$form->editor('content')->required();
							 | 
						|
								
							 | 
						|
											if ($form->isEditing()) {
							 | 
						|
												$form->confirm('提示', '编辑产品需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
							 | 
						|
											}
							 | 
						|
										})->saving(function (Form $form) {
							 | 
						|
											//不允许编辑非自己数据
							 | 
						|
											if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//不允许编辑的字段 TODO 忽略字段不起作用
							 | 
						|
											$form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
							 | 
						|
								
							 | 
						|
											//null字段转为''
							 | 
						|
											foreach ($form->input() as $k => $v) {
							 | 
						|
												if (is_null($v)) {
							 | 
						|
													$form->$k = '';
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//特殊字段处理
							 | 
						|
											$form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
							 | 
						|
											$form->status = ProductStatus::UNAUDITED;
							 | 
						|
											$form->supplier_id = Admin::user()->id;
							 | 
						|
										})->saved(function (Form $form, $result) {
							 | 
						|
											//下架代理商产品
							 | 
						|
											if ($result) {
							 | 
						|
												$id = $form->getKey();
							 | 
						|
												AgentProduct::where('product_id', $id)
							 | 
						|
													->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉
							 | 
						|
													->update(['status' => ProductStatus::SOLD_OUT]);
							 | 
						|
											}
							 | 
						|
										})->deleting(function (Form $form) {
							 | 
						|
											//不允许删除非自己的数据
							 | 
						|
											if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
										});
							 | 
						|
									}
							 | 
						|
								}
							 |