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.
		
		
		
		
		
			
		
			
				
					
					
						
							722 lines
						
					
					
						
							29 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							722 lines
						
					
					
						
							29 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\AdminAgent\Controllers;
							 | 
						|
								
							 | 
						|
								use App\AdminAgent\Forms\LoadSupplierSpec;
							 | 
						|
								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\DiyForm;
							 | 
						|
								use App\Models\Guide;
							 | 
						|
								use App\Models\Product;
							 | 
						|
								use App\Models\Supplier;
							 | 
						|
								use Dcat\Admin\Admin;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Form\NestedForm;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								
							 | 
						|
								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,company_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');
							 | 
						|
								
							 | 
						|
											$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->company_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('single_deposit');
							 | 
						|
											$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())
							 | 
						|
												->dot([
							 | 
						|
													ProductStatus::ON_SALE => 'success',
							 | 
						|
													ProductStatus::UNAUDITED => '',
							 | 
						|
													ProductStatus::REFUSE => 'danger',
							 | 
						|
													ProductStatus::SOLD_OUT => 'warning',
							 | 
						|
												], 'primary');
							 | 
						|
											if (Admin::user()->type != AgentType::OPERATOR) {
							 | 
						|
												$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('company_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(['category:id,name', 'product.supplier:id,company_name', 'guide:id,name']), function (Show $show) {
							 | 
						|
											//不允许查看非自己的数据
							 | 
						|
											if ($show->model()->agent_id != Admin::user()->id) {
							 | 
						|
												Admin::exit('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$show->field('id');
							 | 
						|
								            $show->field('product_id');
							 | 
						|
								            $show->field('product.supplier.company_name', '供应商');
							 | 
						|
								            $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::OPERATOR) {
							 | 
						|
												$show->field('guide.name', '地接');
							 | 
						|
											}
							 | 
						|
								            $show->field('single_deposit');
							 | 
						|
								            $show->field('title');
							 | 
						|
								            $show->field('pictures')->image('', 80, 80);
							 | 
						|
								            $show->field('know')->unescape();
							 | 
						|
								            $show->field('content')->unescape();
							 | 
						|
											if (Admin::user()->type != AgentType::OPERATOR) {
							 | 
						|
												$show->field('is_rec')->using(['未推荐', '已推荐']);
							 | 
						|
												$show->field('channel_id')->as(fn($v) => join(',', Channel::whereIn('id', explode(',', $v))->pluck('name')->toArray()));
							 | 
						|
												$show->field('earnest');
							 | 
						|
												$show->field('earnest_timeout');
							 | 
						|
												$show->field('deposit');
							 | 
						|
												$show->field('deposit_timeout');
							 | 
						|
											}
							 | 
						|
								            $show->field('created_at');
							 | 
						|
								            $show->field('updated_at');
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a form builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Form
							 | 
						|
								     */
							 | 
						|
								    protected function form()
							 | 
						|
								    {
							 | 
						|
								        return Form::make(new AgentProduct(['product:id,title', '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::OPERATOR) {
							 | 
						|
												$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 {
							 | 
						|
												if ($form->isCreating()) {
							 | 
						|
													$form->radio('type')->options([0 => '从供应商进货', 3 => '自营产品'])->default(0)
							 | 
						|
														->when(0, function (Form $form) {
							 | 
						|
															$form->selectTable('product_id', '供应商产品')
							 | 
						|
																->title('选择产品')->default(0)
							 | 
						|
																->dialogWidth('80%;min-width:825px;')
							 | 
						|
																->from(SelectProduct::make(['ids' => $form->model()->product_ids]))
							 | 
						|
																->model(Product::class)
							 | 
						|
																->script("
							 | 
						|
																$(function () {
							 | 
						|
																	$('input[name=\"product_id\"]').change(function () {
							 | 
						|
																		$('.spec-sync').click();
							 | 
						|
																	});
							 | 
						|
																});
							 | 
						|
															");
							 | 
						|
								
							 | 
						|
															//关联供应商规格
							 | 
						|
															$this->related_spec($form);
							 | 
						|
														})->when(3, function (Form $form) {
							 | 
						|
															//自营产品表单
							 | 
						|
															$this->self_form($form);
							 | 
						|
														});
							 | 
						|
												} else {
							 | 
						|
													$form->hidden('product_id');
							 | 
						|
													if ($form->model()->type == 0) {
							 | 
						|
														$form->display('product.title');
							 | 
						|
								
							 | 
						|
														//关联供应商规格
							 | 
						|
														$this->related_spec($form);
							 | 
						|
													} else if ($form->model()->type == 3) {
							 | 
						|
														//自营产品表单
							 | 
						|
														$this->self_form($form);
							 | 
						|
								
							 | 
						|
														//再加这个hasMany才能正常保存
							 | 
						|
														$form->hasMany('spec', function (Form\NestedForm $form) {
							 | 
						|
															$form->hidden('name');
							 | 
						|
															$form->hidden('date');
							 | 
						|
															$form->hidden('stock');
							 | 
						|
															$form->hidden('original_price');
							 | 
						|
															$form->hidden('price');
							 | 
						|
														})->useTable()->script('$(function(){
							 | 
						|
															$(".has-many-spec").parent().parent().remove();
							 | 
						|
														});');
							 | 
						|
													}
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
												$js = file_get_contents(resource_path('js/agent-spec-edit.js'));
							 | 
						|
												$class = str_replace('\\', '\\\\', LoadSupplierSpec::class);
							 | 
						|
												$js = str_replace(
							 | 
						|
													['`{{url}}`', '`{{class}}`', '`{{agent_product_id}}`'],
							 | 
						|
													[route(admin_api_route_name('form')), $class, $form->model()->id ?? 0],
							 | 
						|
													$js
							 | 
						|
												);
							 | 
						|
												Admin::script($js);
							 | 
						|
								
							 | 
						|
												if ($form->isCreating()) {
							 | 
						|
													Admin::style('.has-many-spec .spec-sync{display:none;}');
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
								            $form->number('sale')->min(0)->default(0);
							 | 
						|
								
							 | 
						|
											$options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
							 | 
						|
								            $form->select('category_id')
							 | 
						|
												->options(array_slice($options, 1, null, true))
							 | 
						|
												->required();
							 | 
						|
								
							 | 
						|
											$cat2publish_type = json_encode(Category::where('agent_id', $agent_id)->pluck('publish_type', 'id'));
							 | 
						|
											Admin::script(<<<JS
							 | 
						|
												var PublishType = $cat2publish_type;
							 | 
						|
												$('select[name="category_id"]').change(function () {
							 | 
						|
												    var SelectId = $(this).val();
							 | 
						|
												    $('input[name="tpl_type"][value="'+PublishType[SelectId]+'"]').click();
							 | 
						|
												});
							 | 
						|
											JS);
							 | 
						|
								
							 | 
						|
								            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();
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//计调版旅行社可以选择地接
							 | 
						|
											if (Admin::user()->type == AgentType::OPERATOR) {
							 | 
						|
												$form->selectTable('guide_id', '地接人员')
							 | 
						|
													->title('选择地接人员')
							 | 
						|
													->dialogWidth('50%;min-width:600px;') //不起作用
							 | 
						|
													->from(SelectGuide::make())
							 | 
						|
													->model(Guide::class, 'id', 'name');
							 | 
						|
											} 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('单位:分钟。超过这个时间未支付尾款,订单将自动关闭,且订金不退');
							 | 
						|
											}
							 | 
						|
								        })->saving(function (Form $form) {
							 | 
						|
											//不允许修改非自己的数据
							 | 
						|
											if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if ($form->isEditing() && $form->product_id === 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);
							 | 
						|
													$exists = Product::where([
							 | 
						|
															['status', '<>', ProductStatus::ON_SALE],
							 | 
						|
														])->whereIn('id', $ids)->exists();
							 | 
						|
													if ($exists) {
							 | 
						|
														return $form->response()->error('供应商产品已下架')->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 ($form->isCreating() && is_null($form->type)) {
							 | 
						|
												return $form->response()->error('请选择产品类型!');
							 | 
						|
											} else if ($form->isEditing()) {
							 | 
						|
												$form->type = $form->model()->type;
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//自营产品规格
							 | 
						|
											if ($form->type == 3) {
							 | 
						|
												$form->spec = $form->spec_self;
							 | 
						|
												$form->deleteInput('spec_self');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$form->hidden(['stock', 'price']);
							 | 
						|
											if (!$form->spec) {
							 | 
						|
												return $form->response()->error('请选择产品,并设置产品规格');
							 | 
						|
											}
							 | 
						|
											$spec = array_filter($form->spec, fn($v) => !$v['_remove_']);
							 | 
						|
											if (!$spec) {
							 | 
						|
												return $form->response()->error('产品规格不能为空');
							 | 
						|
											}
							 | 
						|
											$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('供应商产品 '. $product->title .' 已下架');
							 | 
						|
												/*} 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 == 3) {
							 | 
						|
												if (!$form->title || !$form->pictures || !$form->know || !$form->content) {
							 | 
						|
													return $form->response()->error('标题、产品图片、旅游须知、产品详情不能为空');
							 | 
						|
												} else if (!$form->diy_form_id) {
							 | 
						|
													return $form->response()->error('请选择信息收集表单');
							 | 
						|
												}
							 | 
						|
											} else {
							 | 
						|
												return $form->response()->error('不存在此销售方式');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$agent_id = Admin::user()->id;
							 | 
						|
								
							 | 
						|
											//处理特殊字段
							 | 
						|
											$form->hidden(['agent_id', 'status', 'longitude', 'latitude', 'address']); //表单没有的字段,必须加这句才能够重写
							 | 
						|
											$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;
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//产品模板
							 | 
						|
											if ($form->isEditing()) {
							 | 
						|
												$form->tpl_type = $form->model()->tpl_type;
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//经度,纬度,地址
							 | 
						|
											if ($form->tpl_type == 0) { //旅游线路用出发地保存
							 | 
						|
												$form->longitude = $form->extends['field_0_departure_place_longitude'] ?? 0;
							 | 
						|
												$form->latitude = $form->extends['field_0_departure_place_latitude'] ?? 0;
							 | 
						|
												$form->address = $form->extends['field_0_departure_place'] ?? '';
							 | 
						|
											} else {
							 | 
						|
												$form->longitude = $form->extends['field_'.$form->tpl_type.'_longitude'] ?? 0;
							 | 
						|
												$form->latitude = $form->extends['field_'.$form->tpl_type.'_latitude'] ?? 0;
							 | 
						|
												$form->address = $form->extends['field_'.$form->tpl_type.'_address'] ?? '';
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//组合销售需要审核,编辑时是否需要审核在saved里面判断
							 | 
						|
											if ($form->type == 1 || $form->type == 3) {
							 | 
						|
												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();
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
									/**
							 | 
						|
									 * 关联供应商规格
							 | 
						|
									 * @param Form $form
							 | 
						|
									 */
							 | 
						|
									private function related_spec(&$form)
							 | 
						|
									{
							 | 
						|
										//关联供应商规格
							 | 
						|
										$form->hasMany('spec', function (Form\NestedForm $form) {
							 | 
						|
								//			$form->hidden('id'); hasMany时,ID会自动生成
							 | 
						|
											$form->hidden('product_spec_id');
							 | 
						|
											$form->hidden('name')->saving(fn($v) => is_null($v) ? '' : $v); //必须加上这一句,自营产品规格才能正常保存
							 | 
						|
											$form->hidden('date'); //必须加上这一句,自营产品规格才能正常保存
							 | 
						|
											$form->text('supplier_name', '规格')->disable()->customFormat(fn() => $this->product_spec['name'] ?? '供应商已删除规格');
							 | 
						|
											$form->date('supplier_date', '日期')->disable()->customFormat(fn() => $this->product_spec['date'] ?? '供应商已删除规格');
							 | 
						|
											$form->text('supplier_stock', '供应商库存')->disable()->customFormat(fn() => $this->product_spec['stock'] ?? 0);
							 | 
						|
											$form->text('supplier_cost_price', '供应商成本价')->disable()->customFormat(fn() => $this->product_spec['cost_price'] ?? 0);
							 | 
						|
											$form->text('supplier_price', '建议销售价')->disable()->customFormat(fn() => $this->product_spec['price'] ?? 0);
							 | 
						|
											$form->text('stock', '您的库存')->required()
							 | 
						|
												//如果库存大于供应商库存,则取供应商库存
							 | 
						|
												->customFormat(fn() => isset($this->product_spec['stock'], $this->stock) && $this->stock > $this->product_spec['stock'] ? $this->product_spec['stock'] : $this->stock);
							 | 
						|
											$form->text('original_price', '您的市场价')->required();
							 | 
						|
											$form->text('price', '您的销售价')->required()
							 | 
						|
												//如果代理商价格小于供应商建议售价,则取供应商售价
							 | 
						|
												->customFormat(fn() => isset($this->product_spec['price'], $this->price) && $this->price < $this->product_spec['price'] ? $this->product_spec['price'] : $this->price);
							 | 
						|
										})->useTable();
							 | 
						|
								
							 | 
						|
										Admin::style('.has-many-spec .add.btn{display:none;}
							 | 
						|
											.has-many-spec .field_supplier_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;}');
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									/**
							 | 
						|
									 * 自营产品表单
							 | 
						|
									 * @param Form $form
							 | 
						|
									 */
							 | 
						|
									private function self_form(&$form)
							 | 
						|
									{
							 | 
						|
										//信息收集表单
							 | 
						|
										$options = DiyForm::where(['merchant_id' => Admin::user()->id, 'type' => 1])->pluck('name', 'id');
							 | 
						|
										if ($options->isEmpty()) {
							 | 
						|
											$form->select('diy_form_id', '信息收集表单')
							 | 
						|
												->help('提示:信息收集表单为空,请前往“<a href="' . admin_url('diy_form/create') . '">信息收集表单</a>”处新增')
							 | 
						|
												->options($options)->saving(fn($v) => is_null($v) ? 0 : $v);
							 | 
						|
										} else {
							 | 
						|
											$form->select('diy_form_id', '信息收集表单')->options($options)->saving(fn($v) => is_null($v) ? 0 : $v);
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$form->text('title');
							 | 
						|
										$form->multipleImage('pictures')->removable(false)->uniqueName();
							 | 
						|
										$form->editor('know');
							 | 
						|
										$form->editor('content');
							 | 
						|
								
							 | 
						|
										//自营产品规格
							 | 
						|
										$form->hasMany('spec_self', '产品规格', function (Form\NestedForm $form) {
							 | 
						|
											$form->text('name', '规格')->readOnly();
							 | 
						|
											$form->date('date', '日期');
							 | 
						|
											$form->text('stock', '库存');
							 | 
						|
											$form->text('original_price', '市场价');
							 | 
						|
											$form->text('price', '销售价');
							 | 
						|
										})->useTable()->customFormat(fn() => $form->model()->spec);
							 | 
						|
								
							 | 
						|
										Admin::style('.has-many-spec_self .add.btn{display:none;}
							 | 
						|
											.has-many-spec_self .field_supplier_date{width:100px!important;}
							 | 
						|
											.has-many-spec_self .col-md-12{padding:0;}
							 | 
						|
											.has-many-spec_self .form-group{margin-bottom:0;}
							 | 
						|
											.has-many-spec_self .form-group .remove{margin-top:10px;}
							 | 
						|
											.has-many-spec_self .input-group-prepend{display:none;}
							 | 
						|
											.has-many-spec_self .input-group>.form-control:not(:first-child){border-radius:.25rem;}');
							 | 
						|
								
							 | 
						|
										//0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
							 | 
						|
										$form->radio('tpl_type', '产品模板')->disable($form->isEditing())->default(0)
							 | 
						|
											->options([0 => '旅游线路', 1 => '酒店', 2 => '景区', 3 => '餐厅', 4 => '车队', 5 => '单项'])
							 | 
						|
											->when(0, function (Form $form) { //旅游线路
							 | 
						|
												if ($form->isEditing() && $form->model()->tpl_type != 0) {
							 | 
						|
													return;
							 | 
						|
												}
							 | 
						|
												$form->text('extends.field_0_departure_place', '出发地');
							 | 
						|
												$form->map('extends.field_0_departure_place_latitude', 'extends.field_0_departure_place_longitude', '出发地位置');
							 | 
						|
								
							 | 
						|
												$form->text('extends.field_0_destination', '目的地');
							 | 
						|
												$form->map('extends.field_0_destination_latitude', 'extends.field_0_destination_longitude', '目的地位置');
							 | 
						|
								
							 | 
						|
												$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()->tpl_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小时热水、干洗服务等');
							 | 
						|
												})->default($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()->tpl_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()->tpl_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', '位置');
							 | 
						|
											})->when(4, function (Form $form) { //车队
							 | 
						|
												if ($form->isEditing() && $form->model()->tpl_type != 4) {
							 | 
						|
													return;
							 | 
						|
												}
							 | 
						|
												$form->text('extends.field_4_address', '地址');
							 | 
						|
												$form->map('extends.field_4_latitude', 'extends.field_4_longitude', '位置');
							 | 
						|
											})->when(5, function (Form $form) { //单项
							 | 
						|
												if ($form->isEditing() && $form->model()->tpl_type != 5) {
							 | 
						|
													return;
							 | 
						|
												}
							 | 
						|
												$form->text('extends.field_5_address', '地址');
							 | 
						|
												$form->map('extends.field_5_latitude', 'extends.field_5_longitude', '位置');
							 | 
						|
											});
							 | 
						|
								
							 | 
						|
										$form->mobile('verify_mobile');
							 | 
						|
									}
							 | 
						|
								}
							 |