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.
		
		
		
		
		
			
		
			
				
					
					
						
							631 lines
						
					
					
						
							24 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							631 lines
						
					
					
						
							24 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\AdminAgent\Controllers;
							 | 
						|
								
							 | 
						|
								use App\AdminAgent\Forms\LoadSupplierSpec;
							 | 
						|
								use App\AdminAgent\Renderable\SelectAgentCloudProduct;
							 | 
						|
								use App\AdminAgent\Renderable\SelectGuide;
							 | 
						|
								use App\AdminAgent\Renderable\SelectProduct;
							 | 
						|
								use App\AdminAgent\Repositories\AgentProduct;
							 | 
						|
								use App\Common\AgentType;
							 | 
						|
								use App\Common\ProductStatus;
							 | 
						|
								use App\Models\AgentProductItem;
							 | 
						|
								use App\Models\Category;
							 | 
						|
								use App\Models\Channel;
							 | 
						|
								use App\Models\Guide;
							 | 
						|
								use App\Models\Product;
							 | 
						|
								use App\Models\Supplier;
							 | 
						|
								use Dcat\Admin\Admin;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Dcat\Admin\Widgets\Modal;
							 | 
						|
								
							 | 
						|
								class AgentProductController extends AdminController
							 | 
						|
								{
							 | 
						|
								    /**
							 | 
						|
								     * Make a grid builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Grid
							 | 
						|
								     */
							 | 
						|
								    protected function grid()
							 | 
						|
								    {
							 | 
						|
										//下架供应商产品状态不是上架或库存不足的产品
							 | 
						|
										\App\Models\AgentProduct::where('agent_id', Admin::user()->id)
							 | 
						|
											->whereHas('agentProductItem', function ($query) {
							 | 
						|
												return $query->whereHas('product', function ($query) {
							 | 
						|
													return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
							 | 
						|
												});
							 | 
						|
											})
							 | 
						|
											->orWhere('stock', '<=', 0)
							 | 
						|
											->update(['status' => ProductStatus::SOLD_OUT]);
							 | 
						|
								
							 | 
						|
								        return Grid::make(new AgentProduct(['product.supplier:id,name', 'category:id,name']), function (Grid $grid) {
							 | 
						|
											$agent_id = Admin::user()->id;
							 | 
						|
								        	$grid->model()->where('agent_id', $agent_id);
							 | 
						|
								
							 | 
						|
								            $grid->column('id')->sortable();
							 | 
						|
								            $grid->column('picture', '产品图片')->image('', 60, 60);
							 | 
						|
								            $grid->column('title', '产品名称')->limit(15);
							 | 
						|
								            $grid->column('price');
							 | 
						|
								            $grid->column('original_price');
							 | 
						|
								            $grid->column('sale');
							 | 
						|
								            $grid->column('stock')->editable();
							 | 
						|
								
							 | 
						|
											$channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray();
							 | 
						|
											$grid->column('channel_id', '频道')
							 | 
						|
												->display(function ($modal) use ($channels) {
							 | 
						|
													$data = array_flip(explode(',', $this->channel_id));
							 | 
						|
													return join(',',array_intersect_key($channels, $data));
							 | 
						|
												})
							 | 
						|
												->limit(10);
							 | 
						|
											$grid->column('category.name', '分类');
							 | 
						|
											/*$grid->column('product_ids', '产品详情')
							 | 
						|
												->display('查看')
							 | 
						|
												->modal(function ($modal) {
							 | 
						|
													$titles = ['供应商', '产品标题', '产品图片', '原价', '现价', '销量', '库存'];
							 | 
						|
													$pic = isset($this->product->picture)
							 | 
						|
														? "<img data-action=\"preview-img\" src=\"{$this->product->picture}\" style=\"max-width:80px;max-height:200px;cursor:pointer\" class=\"img img-thumbnail\">"
							 | 
						|
														: '';
							 | 
						|
													$data = [[
							 | 
						|
														$this->product->supplier->name ?? '',
							 | 
						|
														$this->product->title ?? '',
							 | 
						|
														$pic,
							 | 
						|
														$this->product->original_price ?? '',
							 | 
						|
														$this->product->price ?? '',
							 | 
						|
														$this->product->sale ?? '',
							 | 
						|
														$this->product->stock ?? '',
							 | 
						|
													]];
							 | 
						|
													return Table::make($titles, $data);
							 | 
						|
												});*/
							 | 
						|
								
							 | 
						|
											$grid->column('status')->help('切换开关可改变上下架状态')
							 | 
						|
												->if(fn() => in_array($this->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE]))
							 | 
						|
												->using([ProductStatus::SOLD_OUT => 0, ProductStatus::ON_SALE => 1])
							 | 
						|
												->switch()
							 | 
						|
												->else()
							 | 
						|
												->using(ProductStatus::array());
							 | 
						|
											if (Admin::user()->type != AgentType::CLUSTER) {
							 | 
						|
												$grid->column('is_rec')->switch()->help('推荐后将在“我的”页面下方显示');
							 | 
						|
											}
							 | 
						|
								            $grid->column('updated_at');
							 | 
						|
								
							 | 
						|
								            $grid->filter(function (Grid\Filter $filter) {
							 | 
						|
												$filter->panel();
							 | 
						|
								
							 | 
						|
												$filter->model()->where('agent_id', Admin::user()->id);
							 | 
						|
								
							 | 
						|
												$filter->equal('id')->width(2);
							 | 
						|
												$filter->like('product.title', '产品标题')->width(3);
							 | 
						|
												$filter->equal('status')->select(ProductStatus::array())->width(2);
							 | 
						|
								
							 | 
						|
												$options = Supplier::where('status', 1)->pluck('name', 'id')->toArray();
							 | 
						|
												$filter->equal('product.supplier_Id', '供应商')->select($options)->width(2);
							 | 
						|
								            });
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a show builder.
							 | 
						|
								     *
							 | 
						|
								     * @param mixed $id
							 | 
						|
								     *
							 | 
						|
								     * @return Show
							 | 
						|
								     */
							 | 
						|
								    protected function detail($id)
							 | 
						|
								    {
							 | 
						|
								        return Show::make($id, new AgentProduct(['agent:id,name', 'category:id,name', 'product.supplier:id,name', 'guide:id,name']), function (Show $show) {
							 | 
						|
											//不允许查看非自己的数据
							 | 
						|
											if ($show->model()->agent_id != Admin::user()->id) {
							 | 
						|
												Admin::exit('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$show->field('id');
							 | 
						|
								            $show->field('agent_id');
							 | 
						|
								            $show->field('product_id');
							 | 
						|
								            $show->field('price');
							 | 
						|
								            $show->field('original_price');
							 | 
						|
								            $show->field('sale');
							 | 
						|
								            $show->field('stock');
							 | 
						|
								            $show->field('category.name', '分类');
							 | 
						|
								            $show->field('status')->using(ProductStatus::array());
							 | 
						|
											if (Admin::user()->type == AgentType::CLUSTER) {
							 | 
						|
												$show->field('guide.name', '地接');
							 | 
						|
											}
							 | 
						|
								            $show->field('title');
							 | 
						|
								            $show->field('pictures')->image('', 80, 80);
							 | 
						|
								            $show->field('know')->unescape();
							 | 
						|
								            $show->field('content')->unescape();
							 | 
						|
											if (Admin::user()->type != AgentType::CLUSTER) {
							 | 
						|
												$show->field('is_rec')->using(['未推荐', '已推荐']);
							 | 
						|
												$show->field('channel_id');
							 | 
						|
												$show->field('earnest');
							 | 
						|
												$show->field('earnest_timeout');
							 | 
						|
												$show->field('deposit');
							 | 
						|
												$show->field('deposit_timeout');
							 | 
						|
											}
							 | 
						|
								            $show->field('created_at');
							 | 
						|
								            $show->field('updated_at');
							 | 
						|
								
							 | 
						|
											/*$show->html(Alert::make(null, '供应商产品详情')->info()); 因为可能是组合销售多个产品,这里暂时不显示
							 | 
						|
								
							 | 
						|
											$show->field('product.id', '供应商产品ID');
							 | 
						|
											$show->field('product.supplier.name');
							 | 
						|
											$show->field('product.title');
							 | 
						|
											$show->field('product.pictures')->image('', 80, 80);
							 | 
						|
											$show->field('product.original_price');
							 | 
						|
											$show->field('product.price');
							 | 
						|
											$show->field('product.sale');
							 | 
						|
											$show->field('product.stock');
							 | 
						|
											$show->field('product.created_at', '创建时间');
							 | 
						|
											$show->field('product.updated_at', '更新时间');*/
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a form builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Form
							 | 
						|
								     */
							 | 
						|
								    protected function form()
							 | 
						|
								    {
							 | 
						|
								        return Form::make(new AgentProduct(['spec.productSpec']), function (Form $form) {
							 | 
						|
											$agent_id = Admin::user()->id;
							 | 
						|
											//不允许查看非自己的数据
							 | 
						|
											if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$form->display('id');
							 | 
						|
								
							 | 
						|
											//计调版旅行社不允许选择计调云产品
							 | 
						|
											if (Admin::user()->type == AgentType::CLUSTER) {
							 | 
						|
												$form->hidden('product_id')->value(0)->default(0);
							 | 
						|
												$form->hidden('type')->value(1)->default(1);
							 | 
						|
												// 组合销售
							 | 
						|
												$form->multipleSelectTable('product_ids', '供应商产品')
							 | 
						|
													->help('请选择两个或两个以上的产品组合销售')
							 | 
						|
													->title('选择产品')
							 | 
						|
													->dialogWidth('80%;min-width:825px;')
							 | 
						|
													->from(SelectProduct::make(['ids' => $form->model()->product_ids]))
							 | 
						|
													->model(Product::class);
							 | 
						|
								
							 | 
						|
												// 自定义内容
							 | 
						|
												$form->text('title');
							 | 
						|
												$form->multipleImage('pictures')->removable(false)->uniqueName();
							 | 
						|
												$form->editor('know');
							 | 
						|
												$form->editor('content');
							 | 
						|
											} else {
							 | 
						|
												$form->hidden('type')->value(0)->default(0);
							 | 
						|
												$js = file_get_contents(resource_path('js/select-supplier-product-change.js'));
							 | 
						|
												$js = str_replace(['{{url}}', '{{class}}', '\\'], [route(admin_api_route_name('form')), LoadSupplierSpec::class, '\\\\'], $js);
							 | 
						|
												$form->selectTable('product_id', '供应商产品')
							 | 
						|
													->required()
							 | 
						|
													->help('产品列表显示的是该产品的标题和图片')
							 | 
						|
													->title('选择产品')
							 | 
						|
													->dialogWidth('80%;min-width:825px;')
							 | 
						|
													->from(SelectProduct::make(['ids' => $form->model()->product_ids]))
							 | 
						|
													->model(Product::class)
							 | 
						|
													->script($js);
							 | 
						|
								
							 | 
						|
												$form->hasMany('spec', function (Form\NestedForm $form) {
							 | 
						|
													$form->hidden('id');
							 | 
						|
													$form->hidden('product_spec_id');
							 | 
						|
													$form->text('name', '规格')->readOnly();
							 | 
						|
													$form->date('date', '日期')->readOnly();
							 | 
						|
													$form->text('supplier_stock', '供应商库存')->disable()->customFormat(fn() => $this->product_spec['stock'] ?? 0);
							 | 
						|
													$form->text('supplier_price', '供应商价')->disable()->customFormat(fn() => $this->product_spec['price'] ?? 0);
							 | 
						|
													$form->text('stock', '您的库存');
							 | 
						|
													$form->text('original_price', '您的原价');
							 | 
						|
													$form->text('price', '您的售价');
							 | 
						|
													Admin::style('.has-many-spec .add.btn{display:;}
							 | 
						|
													.has-many-spec .field_date{width:100px!important;}
							 | 
						|
													.has-many-spec .col-md-12{padding:0;}
							 | 
						|
													.has-many-spec .form-group{margin-bottom:0;}
							 | 
						|
													.has-many-spec .form-group .remove{margin-top:10px;}
							 | 
						|
													.has-many-spec .input-group-prepend{display:none;}
							 | 
						|
													.has-many-spec .input-group>.form-control:not(:first-child){border-radius:.25rem;}');
							 | 
						|
												})->useTable()->required();
							 | 
						|
								
							 | 
						|
												/*$form->radio('type')
							 | 
						|
													->options(['单品销售'])
							 | 
						|
													->default(Admin::user()->type == AgentType::CLUSTER ? 1 : 0)->required()
							 | 
						|
													//->help('单品销售无需审核,组合销售需要审核才能上架')
							 | 
						|
													->help('单品销售无需审核')
							 | 
						|
													->when(0, function (Form $form) {
							 | 
						|
														// 单品销售
							 | 
						|
														$form->selectTable('product_id', '供应商产品')
							 | 
						|
															->help('产品列表显示的是该产品的标题和图片')
							 | 
						|
															->title('选择产品')
							 | 
						|
															->dialogWidth('80%;min-width:825px;')
							 | 
						|
															->from(SelectProduct::make(['ids' => $form->model()->product_ids]))
							 | 
						|
															->model(Product::class);
							 | 
						|
													})->when(1, function (Form $form) {
							 | 
						|
														// 组合销售
							 | 
						|
														$form->multipleSelectTable('product_ids', '供应商产品')
							 | 
						|
															->help('可单选或多选组合销售')
							 | 
						|
															->title('选择产品')
							 | 
						|
															->dialogWidth('80%;min-width:825px;')
							 | 
						|
															->from(SelectProduct::make(['ids' => $form->model()->product_ids]))
							 | 
						|
															->model(Product::class);
							 | 
						|
								
							 | 
						|
														// 自定义内容
							 | 
						|
														$form->text('title');
							 | 
						|
														$form->multipleImage('pictures')->removable(false)->uniqueName();
							 | 
						|
														$form->editor('know');
							 | 
						|
														$form->editor('content');
							 | 
						|
													})->when(2, function (Form $form) {
							 | 
						|
														// 计调云产品
							 | 
						|
														$form->selectTable('agent_cloud_pid', '计调云产品')
							 | 
						|
															->help('产品列表显示的是该产品的标题和图片')
							 | 
						|
															->title('选择产品')
							 | 
						|
															->dialogWidth('80%;min-width:825px;')
							 | 
						|
															->from(SelectAgentCloudProduct::make())
							 | 
						|
															->model(\App\Models\AgentProduct::class);
							 | 
						|
													});*/
							 | 
						|
											}
							 | 
						|
											/*$form->decimal('price',)->rules('required|numeric|min:0|not_in:0',[
							 | 
						|
												'*' => '金额为必填字段且必须大于0',
							 | 
						|
											])->required();
							 | 
						|
											$form->decimal('original_price')->rules('required|numeric|min:0|not_in:0',[
							 | 
						|
												'*' => '金额为必填字段且必须大于0',
							 | 
						|
											])->required();*/
							 | 
						|
								            $form->number('sale')->min(0)->default(0);
							 | 
						|
								//            $form->number('stock')->min(0)->default(8888)->required();
							 | 
						|
								
							 | 
						|
											$options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
							 | 
						|
								            $form->select('category_id')
							 | 
						|
												->options(array_slice($options, 1, null, true))
							 | 
						|
												->required();
							 | 
						|
								
							 | 
						|
								            if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::UNAUDITED, ProductStatus::REFUSE])) {
							 | 
						|
												$form->display('status')->customFormat(fn($v) => ProductStatus::array()[$form->model()->status]);
							 | 
						|
											} else {
							 | 
						|
												$form->radio('status')
							 | 
						|
													->default(ProductStatus::ON_SALE)
							 | 
						|
													->options([
							 | 
						|
														ProductStatus::ON_SALE => '上架',
							 | 
						|
														ProductStatus::SOLD_OUT => '下架',
							 | 
						|
													])
							 | 
						|
													->required();
							 | 
						|
											}
							 | 
						|
											//$form->selectTable('verifier')
							 | 
						|
											//	->title('选择核销人员')
							 | 
						|
											//	->dialogWidth('50%;min-width:600px;') //不起作用
							 | 
						|
											//	->from(SelectUser::make(['is_verify' => 1]))
							 | 
						|
											//	->model(User::class, 'id', 'nickname')
							 | 
						|
											//	->customFormat(fn($v) => !$v ? '' : $v)
							 | 
						|
											//	->required();
							 | 
						|
								
							 | 
						|
											//计调版旅行社可以选择地接
							 | 
						|
											if (Admin::user()->type == AgentType::CLUSTER) {
							 | 
						|
												$form->selectTable('guide_id', '地接人员')
							 | 
						|
													->title('选择地接人员')
							 | 
						|
													->dialogWidth('50%;min-width:600px;') //不起作用
							 | 
						|
													->from(SelectGuide::make())
							 | 
						|
													->model(Guide::class, 'id', 'name');
							 | 
						|
												if ($form->isEditing()) {
							 | 
						|
													$form->confirm('提示', '如果修改了标题、价格、产品图片、旅游须知、产品详情需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
							 | 
						|
												}
							 | 
						|
											} else {
							 | 
						|
												$form->switch('is_rec')->help('推荐后将在小程序“我的”页面下方显示');
							 | 
						|
								
							 | 
						|
												$options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
							 | 
						|
												$form->multipleSelect('channel_id')->options(array_slice($options, 1, null, true));
							 | 
						|
								
							 | 
						|
												$form->decimal('earnest')->rules('required|numeric|min:0',[
							 | 
						|
													'*' => '金额为必填字段且必须大于0',
							 | 
						|
												])->default(0);
							 | 
						|
								
							 | 
						|
												$form->number('earnest_timeout')->min(0)
							 | 
						|
													->default(0)->help('单位:分钟。超过这个时间未支付尾款,订单将自动关闭,且定金不退');
							 | 
						|
								
							 | 
						|
												$form->decimal('deposit')->rules('required|numeric|min:0',[
							 | 
						|
													'*' => '金额为必填字段且必须大于0',
							 | 
						|
												])->default(0);;
							 | 
						|
								
							 | 
						|
												$form->number('deposit_timeout')->min(0)
							 | 
						|
													->default(0)->help('单位:分钟。超过这个时间未支付尾款,订单将自动关闭,且订金不退');
							 | 
						|
								
							 | 
						|
												/*$form->tree('channel_id', '所属频道')
							 | 
						|
													->expand(false)
							 | 
						|
													->nodes(Channel::where('agent_id', Admin::user()->id)->get()->toArray())
							 | 
						|
													->setIdColumn('id')
							 | 
						|
													->setTitleColumn('name')
							 | 
						|
													->setParentColumn('pid');*/
							 | 
						|
													//->exceptParentNode(); //禁止过滤父节点的值
							 | 
						|
											}
							 | 
						|
								        })->saving(function (Form $form) {
							 | 
						|
											//不允许修改非自己的数据
							 | 
						|
											if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if ($form->isEditing() && $form->type === null) {
							 | 
						|
												//推荐按钮开关
							 | 
						|
												if ($form->is_rec !== null) {
							 | 
						|
													$form->model()->is_rec = $form->is_rec ? 1 : 0;
							 | 
						|
													$form->model()->save();
							 | 
						|
													return $form->response()->success('更新成功!')->refresh();
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
												if ($form->stock !== null || $form->status !== null) {
							 | 
						|
													$ids = explode(',', $form->model()->product_ids);
							 | 
						|
													$count = Product::where([
							 | 
						|
															['status', '=', ProductStatus::ON_SALE],
							 | 
						|
															['stock', '>=', $form->stock ?? $form->model()->stock],
							 | 
						|
														])->whereIn('id', $ids)->count();
							 | 
						|
													if (count($ids) != $count) {
							 | 
						|
														return $form->response()->error('供应商产品已下架或库存小于你设置的库存' . $form->stock)->refresh();
							 | 
						|
													}
							 | 
						|
								
							 | 
						|
													//修改库存
							 | 
						|
													if ($form->stock !== null) {
							 | 
						|
														$form->model()->stock = $form->stock;
							 | 
						|
														$form->model()->save();
							 | 
						|
														return $form->response()->success('更新成功!');
							 | 
						|
													}
							 | 
						|
													//上下架状态按钮开关
							 | 
						|
													if ($form->status !== null) {
							 | 
						|
														//待审核和拒绝的状态不允许修改
							 | 
						|
														if (in_array($form->model()->status, [ProductStatus::UNAUDITED, ProductStatus::REFUSE])) {
							 | 
						|
															return $form->response()->error('产品待审核或审核拒绝,不允许修改!')->refresh();
							 | 
						|
														}
							 | 
						|
								
							 | 
						|
														//计调云产品处理
							 | 
						|
														if ($form->type == 2) {
							 | 
						|
															$cloud_product = AgentProduct::find($form->agent_cloud_pid);
							 | 
						|
															if (!$cloud_product || $cloud_product->status != ProductStatus::ON_SALE) {
							 | 
						|
																return $form->response()->error('你选择的计调云产品状态异常,上架失败!')->refresh();
							 | 
						|
															}
							 | 
						|
														}
							 | 
						|
								
							 | 
						|
														//库存不足,上架失败
							 | 
						|
														if ($form->model()->stock <= 0 && $form->status == 1) {
							 | 
						|
															return $form->response()->error('库存不足,上架失败,请先增加库存!')->refresh();
							 | 
						|
														}
							 | 
						|
								
							 | 
						|
														$form->model()->status = $form->status == 1 ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
							 | 
						|
														$form->model()->save();
							 | 
						|
														return $form->response()->success('更新成功!')->refresh();
							 | 
						|
													}
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if (is_null($form->type)) {
							 | 
						|
												return $form->response()->error('请选择销售类型!');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$form->hidden(['stock', 'price']);
							 | 
						|
											$spec = array_filter($form->spec, fn($v) => !$v['_remove_']);
							 | 
						|
											$form->stock = array_sum(array_column($spec, 'stock'));
							 | 
						|
											$form->original_price = min(array_column($spec, 'original_price'));
							 | 
						|
											$form->price = min(array_column($spec, 'price'));
							 | 
						|
								
							 | 
						|
											//单品销售
							 | 
						|
											if ($form->type == 0) {
							 | 
						|
												$form->hidden(['product_ids', 'title', 'pictures', 'know', 'content']);
							 | 
						|
												$form->product_id = (int)$form->product_id;
							 | 
						|
												if (!$form->product_id) {
							 | 
						|
													return $form->response()->error('请选择产品');
							 | 
						|
												}
							 | 
						|
												$form->product_ids =  $form->product_id;
							 | 
						|
								
							 | 
						|
												//将供应商产品写入title,pictures,know,content
							 | 
						|
												$product = Product::find($form->product_id);
							 | 
						|
												if ($product->status != ProductStatus::ON_SALE) {
							 | 
						|
													return $form->response()->error('产品ID '. $form->product_id .' 已下架');
							 | 
						|
												} else if ($product->stock < $form->stock) {
							 | 
						|
													return $form->response()->error("供应商当前库存为{$product->stock},你设置的库存不能超过该数值");
							 | 
						|
												} else if ($form->price < $product->price) {
							 | 
						|
													return $form->response()->error("产品售价不能小于供应商售价{$product->price}");
							 | 
						|
												}
							 | 
						|
												$form->title = $product->title;
							 | 
						|
												$form->pictures = $product->pictures;
							 | 
						|
												$form->know = $product->know;
							 | 
						|
												$form->content = $product->content;
							 | 
						|
											/*}
							 | 
						|
											//组合销售
							 | 
						|
											else if ($form->type == 1) {
							 | 
						|
												if (!$form->product_ids) {
							 | 
						|
													return $form->response()->error('请选择产品');
							 | 
						|
												}
							 | 
						|
												$product_ids = explode(',', $form->product_ids);
							 | 
						|
								
							 | 
						|
												if (count($product_ids) < 2) {
							 | 
						|
													return $form->response()->error('组合销售必须选择两个以上产品,否则请选择单品销售');
							 | 
						|
												}
							 | 
						|
												$form->product_id = $product_ids[0];
							 | 
						|
								
							 | 
						|
												$required_fields = ['title', 'pictures', 'know', 'content'];
							 | 
						|
												foreach ($required_fields as $field) {
							 | 
						|
													if (!$form->$field) {
							 | 
						|
														return $form->response()->error('内容输入不完整,标题、产品图片、旅游须知、产品详情必填');
							 | 
						|
													}
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
												//判断供应商产品是否存在或下架
							 | 
						|
												$not_in_id = Product::query()
							 | 
						|
													->whereIn('id', $product_ids)
							 | 
						|
													->where(function ($query) use ($form) {
							 | 
						|
														$query->where('status', '<>', ProductStatus::ON_SALE)
							 | 
						|
															->orWhere('stock', '<', $form->stock);
							 | 
						|
													})
							 | 
						|
													->pluck('id')
							 | 
						|
													->toArray();
							 | 
						|
												if ($not_in_id) {
							 | 
						|
													return $form->response()->error('产品ID ' . join(',', $not_in_id) . ' 库存小于你设置的库存' . $form->stock . ',或已下架');
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
												$total_price = Product::query()->whereIn('id', $product_ids)->sum('price');
							 | 
						|
												if ($form->price < $total_price) {
							 | 
						|
													return $form->response()->error("产品售价不能小于供应商产品总售价{$total_price}");
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
												//如果是计调版旅行社,标记为是云产品
							 | 
						|
												if (Admin::user()->type == AgentType::CLUSTER) {
							 | 
						|
													$form->hidden('is_cloud');
							 | 
						|
													$form->is_cloud = 1;
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
											//计调云产品
							 | 
						|
											else if ($form->type == 2) {
							 | 
						|
												$form->agent_cloud_pid = (int)$form->agent_cloud_pid;
							 | 
						|
												if (!$form->agent_cloud_pid) {
							 | 
						|
													return $form->response()->error('请选择产品');
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
												//产品信息预判断
							 | 
						|
												$cloud_product = \App\Models\AgentProduct::where([
							 | 
						|
													['stock', '>', 0],
							 | 
						|
													['status', '=', ProductStatus::ON_SALE],
							 | 
						|
													['is_cloud', '=', 1],
							 | 
						|
													['type', '=', 1],
							 | 
						|
													['agent_id', '<>', Admin::user()->id],
							 | 
						|
												])->whereDoesntHave('agentProductItem', function ($query) {
							 | 
						|
													return $query->whereHas('product', function ($query) {
							 | 
						|
														return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
							 | 
						|
													});
							 | 
						|
												})->find($form->agent_cloud_pid);
							 | 
						|
								
							 | 
						|
												if (!$cloud_product) {
							 | 
						|
													return $form->response()->error('你选择的计调云产品库存不足或已下架,请重新选择');
							 | 
						|
												} else if ($cloud_product->stock < $form->stock) {
							 | 
						|
													return $form->response()->error("计调云产品当前库存为{$cloud_product->stock},你设置的库存不能超过该数值");
							 | 
						|
												} else if ($form->price < $cloud_product->price) {
							 | 
						|
													return $form->response()->error("产品售价不能小于计调云产品售价{$cloud_product->price}");
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
												//同步关键字段信息
							 | 
						|
												$form->product_id =  $cloud_product->product_id;
							 | 
						|
												$form->product_ids =  $cloud_product->product_ids;
							 | 
						|
												$form->guide_id =  $cloud_product->guide_id;
							 | 
						|
												$form->title = $cloud_product->title;
							 | 
						|
												$form->pictures = $cloud_product->pictures;
							 | 
						|
												$form->know = $cloud_product->know;
							 | 
						|
												$form->content = $cloud_product->content;*/
							 | 
						|
											} else {
							 | 
						|
												return $form->response()->error('不存在此销售方式');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$agent_id = Admin::user()->id;
							 | 
						|
								
							 | 
						|
											//处理特殊字段
							 | 
						|
											$form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
							 | 
						|
											$form->agent_id = $agent_id;
							 | 
						|
											$form->agent_cloud_pid = $form->type ==2 ? ($form->agent_cloud_pid ?? 0) : 0;
							 | 
						|
											if (array_key_exists('guide_id', $form->input())) {
							 | 
						|
												$form->guide_id = $form->guide_id ?? 0;
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//组合销售需要审核,编辑时是否需要审核在saved里面判断
							 | 
						|
											if ($form->type == 1) {
							 | 
						|
												if ($form->isCreating()) {
							 | 
						|
													$form->status = ProductStatus::UNAUDITED;
							 | 
						|
												} else if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::UNAUDITED, ProductStatus::REFUSE])) {
							 | 
						|
													$form->deleteInput('status'); //待审核和拒绝的状态不允许修改
							 | 
						|
												}
							 | 
						|
											} else if (!is_null($form->status) && ($form->type == 0 || $form->type == 2)) {
							 | 
						|
												$form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//订金
							 | 
						|
											if ($form->earnest <= 0 || $form->earnest_timeout <= 0) {
							 | 
						|
												$form->earnest = 0;
							 | 
						|
												$form->earnest_timeout = 0;
							 | 
						|
											} else if ($form->earnest > $form->price) {
							 | 
						|
												return $form->response()->error('订金不能大于商品价');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//定金
							 | 
						|
											if ($form->deposit <= 0 || $form->deposit_timeout <= 0) {
							 | 
						|
												$form->deposit = 0;
							 | 
						|
												$form->deposit_timeout = 0;
							 | 
						|
											} else if ($form->earnest > $form->price) {
							 | 
						|
												return $form->response()->error('定金不能大于商品价');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//不允许编辑的字段
							 | 
						|
											$form->ignore(['id', 'agent_id', 'created_at', 'updated_at', 'deleted_at']);
							 | 
						|
								
							 | 
						|
											//判断是否重复发布产品
							 | 
						|
											$where = [
							 | 
						|
												['type', '=', $form->type],
							 | 
						|
												['agent_id', '=', $agent_id],
							 | 
						|
												['product_id', '=', $form->product_id],
							 | 
						|
												['product_ids', '=', $form->product_ids],
							 | 
						|
											];
							 | 
						|
											if ($form->isEditing()) {
							 | 
						|
												$where[] = ['id', '<>', $form->getKey()];
							 | 
						|
											}
							 | 
						|
											if ($id = $form->repository()->model()->where($where)->value('id')) {
							 | 
						|
												return $form->response()->error("你发布的产品ID {$id} 与本产品重复,请检查");
							 | 
						|
											}
							 | 
						|
										})->saved(function (Form $form) {
							 | 
						|
											/** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
							 | 
						|
											$product_ids = explode(',', $form->product_ids);
							 | 
						|
											$agent_product_id = $form->getKey();
							 | 
						|
								
							 | 
						|
											$product = Product::whereIn('id', $product_ids)->orderBy('id')
							 | 
						|
												->get(['id AS product_id', 'supplier_id'])->toArray();
							 | 
						|
								
							 | 
						|
											$insert_data = array_map(function ($v) use ($agent_product_id) {
							 | 
						|
												$v['agent_product_id'] = $agent_product_id;
							 | 
						|
												$v['agent_id'] = Admin::user()->id;
							 | 
						|
												return $v;
							 | 
						|
											}, $product);
							 | 
						|
								
							 | 
						|
											//组合产品编辑关键字段需要审核
							 | 
						|
											if ($form->isEditing() && $form->type == 1 && $form->model()->wasChanged(['title', 'pictures', 'know', 'content'])) {
							 | 
						|
												$form->model()->update(['status' => ProductStatus::UNAUDITED]);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if ($form->isCreating()) {
							 | 
						|
												AgentProductItem::insert($insert_data);
							 | 
						|
											} else if ($form->isEditing()) {
							 | 
						|
												//删除原来有,但现在没有的数据
							 | 
						|
												AgentProductItem::query()
							 | 
						|
													->where('agent_product_id', $agent_product_id)
							 | 
						|
													->whereNotIn('product_id', $product_ids)->delete();
							 | 
						|
								
							 | 
						|
												//插入原来没有,但是现在有的数据
							 | 
						|
												foreach ($insert_data as $v) {
							 | 
						|
													AgentProductItem::query()->firstOrCreate(
							 | 
						|
														['agent_product_id' => $agent_product_id, 'product_id' => $v['product_id']],
							 | 
						|
														$v
							 | 
						|
													);
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//如果是计调云产品,且处于上架状态,同步信息到其它产品,否则下架所有关联的产品
							 | 
						|
											if ($form->is_cloud) {
							 | 
						|
												if ($form->status == ProductStatus::ON_SALE) {
							 | 
						|
													$data = [
							 | 
						|
														'product_id' => $form->product_id,
							 | 
						|
														'product_ids' => $form->product_ids,
							 | 
						|
														'guide_id' => $form->guide_id,
							 | 
						|
														'title' => $form->title,
							 | 
						|
														'pictures' => explode(',', $form->pictures),
							 | 
						|
														'know' => $form->know,
							 | 
						|
														'content' => $form->content,
							 | 
						|
													];
							 | 
						|
												} else {
							 | 
						|
													$data = ['status' => ProductStatus::SOLD_OUT];
							 | 
						|
												}
							 | 
						|
												\App\Models\AgentProduct::query()
							 | 
						|
													->where(['agent_cloud_pid' => $form->getKey(), 'type' => 2])
							 | 
						|
													->update($data);
							 | 
						|
											}
							 | 
						|
										})->deleting(function (Form $form) {
							 | 
						|
											//不允许删除非自己的数据
							 | 
						|
											if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//处理组合产品明细表
							 | 
						|
											$ids = array_column($form->model()->toArray(),'id');
							 | 
						|
											AgentProductItem::query()->whereIn('agent_product_id',$ids)->delete();
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								}
							 |