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.
182 lines
6.0 KiB
182 lines
6.0 KiB
<?php
|
|
|
|
namespace App\AdminAgent\Controllers;
|
|
|
|
use App\AdminAgent\Renderable\SelectProduct;
|
|
use App\AdminAgent\Repositories\AgentProduct;
|
|
use App\Common\ProductStatus;
|
|
use App\Models\Category;
|
|
use App\Models\Channel;
|
|
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\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');
|
|
|
|
$channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray();
|
|
$grid->column('channel_id', '频道')
|
|
->display('查看')
|
|
->modal('关联频道', function ($modal) use ($channels) {
|
|
$data = array_flip(explode(',', $this->channel_id));
|
|
$data = array_intersect_key($channels, $data);
|
|
return Table::make(['频道ID', '频道名称'], $data);
|
|
});
|
|
$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(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('agent_id');
|
|
$show->field('product_id');
|
|
$show->field('price');
|
|
$show->field('original_price');
|
|
$show->field('sale');
|
|
$show->field('channel_id');
|
|
$show->field('category_id');
|
|
$show->field('status');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new AgentProduct(), function (Form $form) {
|
|
$agent_id = Admin::user()->id;
|
|
|
|
$form->display('id');
|
|
$form->hidden('agent_id')->value($agent_id);
|
|
$form->multipleSelectTable('product_id', '供应商产品')
|
|
->title('选择产品')
|
|
->dialogWidth('80%;min-width:825px;')
|
|
->from(SelectProduct::make())
|
|
->required();
|
|
$form->text('price')->required();
|
|
$form->text('original_price')->required();
|
|
$form->text('sale')->default(0);
|
|
|
|
$options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
|
|
array_shift($options);
|
|
$form->multipleSelect('channel_id')
|
|
->options($options);
|
|
|
|
$options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
|
|
array_shift($options);
|
|
$form->select('category_id')
|
|
->options($options)
|
|
->required();
|
|
$form->select('status')
|
|
->options([
|
|
ProductStatus::ON_SALE => '上架',
|
|
ProductStatus::SOLD_OUT => '下架',
|
|
])
|
|
->default(ProductStatus::ON_SALE)
|
|
->required();
|
|
})->saving(function (Form $form) {
|
|
$agent_id = Admin::user()->id;
|
|
|
|
//判断供应商产品是否存在或下架
|
|
if (!Product::query()->where(['id' => $form->product_id, 'status' => ProductStatus::ON_SALE])->exists()) {
|
|
return $form->response()->error('供应商不存在该产品或已下架,不可销售');
|
|
}
|
|
|
|
//不允许编辑的字段
|
|
$form->ignore(['id', 'agent_id', 'created_at', 'updated_at', 'deleted_at']);
|
|
|
|
//处理特殊字段
|
|
$form->agent_id = $agent_id;
|
|
$form->status = ($form->status == ProductStatus::ON_SALE) ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
|
|
|
|
//判断是否重复
|
|
$agent_product = $form->repository()->model()->withTrashed()->where(['agent_id' => $agent_id, 'product_id' => $form->product_id])->first();
|
|
if ($agent_product) {
|
|
//如果已经软删除了,解除软删除后再更新
|
|
if ($agent_product->deleted_at) {
|
|
$agent_product->deleted_at = null;
|
|
$agent_product->update($form->input());
|
|
return $form->response()->success('保存成功');
|
|
}
|
|
return $form->response()->error('该产品已经存在,请勿重复发布');
|
|
}
|
|
});
|
|
}
|
|
}
|