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.
306 lines
10 KiB
306 lines
10 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;
|
|
use Dcat\Admin\Widgets\Table;
|
|
|
|
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('product.picture', '产品图片')->image('', 60, 60);
|
|
$grid->column('product.title', '产品名称')->limit(15);
|
|
$grid->column('product_id', '产品详情')
|
|
->display('查看')
|
|
->modal(function ($v) {
|
|
$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('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', '分类')->label();
|
|
$grid->column('status')
|
|
->using(ProductStatus::array())
|
|
->dot([
|
|
ProductStatus::ON_SALE => 'success',
|
|
ProductStatus::UNAUDITED => '',
|
|
ProductStatus::REFUSE => 'danger',
|
|
ProductStatus::SOLD_OUT => 'warning',
|
|
], 'primary');
|
|
$grid->column('created_at');
|
|
$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('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(['product:id,title']), 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->hidden('product_id');
|
|
$form->selectTable('product_id', '封面产品')
|
|
->help('产品列表显示的是该产品的标题和图片')
|
|
->title('选择产品')
|
|
->dialogWidth('80%;min-width:825px;')
|
|
->from(SelectProduct::make())
|
|
->model(Product::class)
|
|
->required();
|
|
$form->multipleSelectTable('product_ids', '选择产品')
|
|
->help('可单选或多选组合销售')
|
|
->title('选择产品')
|
|
->dialogWidth('80%;min-width:825px;')
|
|
->from(SelectProduct::make())
|
|
->model(Product::class)
|
|
->required();
|
|
$form->text('price')->required();
|
|
$form->text('original_price')->required();
|
|
$form->text('sale')->default(0);
|
|
$form->text('stock')->default(9999);
|
|
|
|
$options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
|
|
$form->multipleSelect('channel_id')->options(array_slice($options, 1));
|
|
|
|
$options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
|
|
$form->select('category_id')
|
|
->options(array_slice($options, 1))
|
|
->required();
|
|
|
|
$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');
|
|
}
|
|
})->saving(function (Form $form) {
|
|
//不允许修改非自己的数据
|
|
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
$agent_id = Admin::user()->id;
|
|
|
|
$product_ids = explode(',', $form->product_ids);
|
|
if (empty($product_ids)) {
|
|
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 . ',或不存在、已下架');
|
|
}
|
|
|
|
//处理特殊字段
|
|
$form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
|
|
$form->agent_id = $agent_id;
|
|
$form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
|
|
$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('数据不存在');
|
|
}
|
|
});
|
|
}
|
|
}
|