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.
374 lines
13 KiB
374 lines
13 KiB
<?php
|
|
|
|
namespace App\AdminAgent\Controllers;
|
|
|
|
use App\AdminAgent\Renderable\SelectGuide;
|
|
use App\AdminAgent\Renderable\SelectProduct;
|
|
use App\AdminAgent\Renderable\SelectUser;
|
|
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 App\Models\User;
|
|
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\Alert;
|
|
|
|
class AgentProductController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
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');
|
|
|
|
$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')
|
|
->using([ProductStatus::SOLD_OUT => 0, ProductStatus::ON_SALE => 1])
|
|
->switch();
|
|
$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', 'product.supplier:id,name', 'user:id,nickname', '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('channel_id');
|
|
$show->field('category_id');
|
|
$show->field('status')->using(ProductStatus::array());
|
|
$show->field('verifier.nickname', '核销人员');
|
|
if (Admin::user()->type == AgentType::CLUSTER) {
|
|
$show->field('guide.name', '地接');
|
|
}
|
|
$show->field('is_rec')->using(['未推荐', '已推荐']);
|
|
$show->field('title');
|
|
$show->field('pictures')->image('', 80, 80);
|
|
$show->field('know')->unescape();
|
|
$show->field('content')->unescape();
|
|
$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(), 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');
|
|
$form->radio('type')
|
|
->options(['单品销售', '组合销售'])
|
|
->default(0)
|
|
->when(0, function (Form $form) {
|
|
/** 单品销售 **/
|
|
$form->selectTable('product_id', '选择产品')
|
|
->help('产品列表显示的是该产品的标题和图片')
|
|
->title('选择产品')
|
|
->dialogWidth('80%;min-width:825px;')
|
|
->from(SelectProduct::make())
|
|
->model(Product::class);
|
|
})->when(1, function (Form $form) {
|
|
/** 组合销售 **/
|
|
$form->multipleSelectTable('product_ids', '选择产品')
|
|
->help('可单选或多选组合销售')
|
|
->title('选择产品')
|
|
->dialogWidth('80%;min-width:825px;')
|
|
->from(SelectProduct::make())
|
|
->model(Product::class);
|
|
|
|
/** 自定义内容 **/
|
|
$form->text('title');
|
|
$form->multipleImage('pictures')->removable(false)->uniqueName();
|
|
$form->editor('know');
|
|
$form->editor('content');
|
|
});
|
|
$form->text('title', '产品名称');
|
|
|
|
$form->text('price')->required();
|
|
$form->text('original_price')->required();
|
|
$form->text('sale')->default(0);
|
|
$form->text('stock')->default(8888);
|
|
|
|
$options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
|
|
$form->multipleSelect('channel_id')->options(array_slice($options, 1, null, true));
|
|
|
|
$options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
|
|
$form->select('category_id')
|
|
->options(array_slice($options, 1, null, true))
|
|
->required();
|
|
|
|
$form->radio('status')
|
|
->default(ProductStatus::ON_SALE)
|
|
->options([
|
|
ProductStatus::ON_SALE => '上架',
|
|
ProductStatus::SOLD_OUT => '下架',
|
|
])
|
|
->required();
|
|
$form->switch('is_rec')->help('推荐后将在“我的”页面下方显示');
|
|
$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');
|
|
}
|
|
})->saving(function (Form $form) {
|
|
//不允许修改非自己的数据
|
|
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
if ($form->product_id === null && $form->product_ids === null) {
|
|
//推荐按钮开关
|
|
if ($form->is_rec !== null) {
|
|
$form->model()->is_rec = $form->is_rec ? 1 : 0;
|
|
$form->model()->save();
|
|
return $form->response()->success('更新成功!');
|
|
}
|
|
//上下架状态按钮开关
|
|
if ($form->status !== null) {
|
|
$form->model()->status = $form->status == 1 ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
|
|
$form->model()->save();
|
|
return $form->response()->success('更新成功!');
|
|
}
|
|
}
|
|
|
|
//单品销售
|
|
if ($form->type == 0) {
|
|
$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('库存不足,你设置的库存应小于等于' . $form->stock);
|
|
}
|
|
$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 . ',或不存在、已下架');
|
|
}
|
|
} else {
|
|
return $form->response()->error('不存在此销售方式');
|
|
}
|
|
|
|
$agent_id = Admin::user()->id;
|
|
//处理特殊字段
|
|
$form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
|
|
$form->agent_id = $agent_id;
|
|
if (array_key_exists($form->status, $form->input())) {
|
|
$form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
|
|
}
|
|
if (array_key_exists($form->guide_id, $form->input())) {
|
|
$form->guide_id = $form->guide_id ?? 0;
|
|
}
|
|
|
|
//不允许编辑的字段
|
|
$form->ignore(['id', 'agent_id', 'status', 'created_at', 'updated_at', 'deleted_at']);
|
|
|
|
//判断是否重复发布产品
|
|
$where = [
|
|
['agent_id', '=', $agent_id],
|
|
['product_id', '=', $form->product_id],
|
|
['product_ids', '=', $form->product_ids],
|
|
];
|
|
if ($form->isEditing()) {
|
|
$where[] = ['id', '<>', $form->getKey()];
|
|
}
|
|
if ($form->repository()->model()->where($where)->exists()) {
|
|
return $form->response()->error('该产品已经存在,请勿重复发布');
|
|
}
|
|
})->saved(function (Form $form) {
|
|
/** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
|
|
$product_ids = explode(',', $form->product_ids);
|
|
$product = Product::whereIn('id', $product_ids)->orderBy('id')->get(['id', 'supplier_id'])->toArray();
|
|
|
|
$agent_product_id = $form->getKey();
|
|
$insert_data = [];
|
|
foreach ($product as $k => &$v) {
|
|
$insert_data[$v['id']] = [
|
|
'agent_product_id' => $agent_product_id,
|
|
'agent_id' => Admin::user()->id,
|
|
'supplier_id' => $v['supplier_id'],
|
|
'product_id' => $v['id'],
|
|
];
|
|
}
|
|
|
|
if ($form->isCreating()) {
|
|
AgentProductItem::insert($insert_data);
|
|
} else if ($form->isEditing()) {
|
|
//先查出来原来的数据
|
|
$old_data = AgentProductItem::where('agent_product_id', $agent_product_id)->pluck('id', 'product_id')->toArray();
|
|
|
|
//新ID在原来数据里面没有的,删除掉
|
|
foreach ($old_data as $k => $v) {
|
|
//删除已经删除掉的product_id
|
|
if (!in_array($k, $product_ids)) {
|
|
AgentProductItem::query()->find($v)->delete();
|
|
}
|
|
//将新数据和旧数据中都存在的product_id unset掉
|
|
if (array_key_exists($k, $insert_data)) {
|
|
unset($insert_data[$k]);
|
|
}
|
|
}
|
|
|
|
//将剩余(新增)的product_id保存
|
|
if ($insert_data) {
|
|
AgentProductItem::insert($insert_data);
|
|
}
|
|
}
|
|
})->deleting(function (Form $form) {
|
|
//不允许删除非自己的数据
|
|
if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
});
|
|
}
|
|
}
|