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.
169 lines
5.4 KiB
169 lines
5.4 KiB
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Extensions\Grid\AuditProduct;
|
|
use App\Admin\Repositories\Product;
|
|
use App\Common\ProductStatus;
|
|
use App\Common\UserStatus;
|
|
use App\Models\AgentProduct;
|
|
use App\Models\AgentProductItem;
|
|
use App\Models\Category;
|
|
use App\Models\Supplier;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
class ProductController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Product(['supplier:id,name', 'category:id,name']), function (Grid $grid) {
|
|
$grid->disableCreateButton();
|
|
//如果是审核页面,多加where条件判断
|
|
if (strpos(Route::current()->uri, 'audit')) {
|
|
$grid->model()->where('status', ProductStatus::UNAUDITED);
|
|
}
|
|
|
|
$grid->column('id')->sortable();
|
|
$grid->column('category.name', '分类');
|
|
$grid->column('picture')->image('', 60, 60);
|
|
$grid->column('title')->limit(15);
|
|
$grid->column('original_price');
|
|
$grid->column('price');
|
|
$grid->column('sale');
|
|
$grid->column('stock');
|
|
$grid->column('supplier.name', '供应商');
|
|
$grid->column('status')
|
|
->if(fn() => $this->status == ProductStatus::UNAUDITED)
|
|
->display('')
|
|
->then(function ($column) {
|
|
$column->append((new AuditProduct(null, 1))->setKey($this->id))->append(' ');
|
|
$column->append((new AuditProduct(null, 2))->setKey($this->id));
|
|
})
|
|
->else()
|
|
->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->equal('id')->width(2);
|
|
$filter->like('title')->width(3);
|
|
$filter->equal('status')->select(ProductStatus::array())->width(2);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Product(['supplier:id,name', 'category:id,name']), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('category.name', '所属分类');
|
|
$show->field('title');
|
|
$show->field('pictures')->image('', 80, 80);
|
|
$show->field('original_price');
|
|
$show->field('price');
|
|
$show->field('sale');
|
|
$show->field('stock');
|
|
$show->field('status')->using(ProductStatus::array());
|
|
$show->field('supplier.name', '供应商');
|
|
$show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
|
|
$show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new Product(), function (Form $form) {
|
|
$form->display('id');
|
|
|
|
$options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
|
|
$form->select('category_id', '所属分类')
|
|
->options(array_slice($options, 1, null, true))
|
|
->required();
|
|
$form->text('title')->required();
|
|
$form->multipleImage('pictures')->required()->removable(false)->uniqueName();
|
|
$form->text('original_price')->required();
|
|
$form->text('price')->required();
|
|
$form->text('sale')->default(0);
|
|
$form->text('stock')->default(9999)->required();
|
|
$form->select('status')
|
|
->options(ProductStatus::array())
|
|
->default(ProductStatus::ON_SALE)
|
|
->required();
|
|
$form->editor('know');
|
|
$form->editor('content');
|
|
})->saving(function (Form $form) {
|
|
//不允许编辑的字段
|
|
if ($form->isEditing()) {
|
|
$form->ignore(['id', 'created_at', 'updated_at', 'deleted_at']);
|
|
}
|
|
|
|
//特殊字段处理
|
|
$form->sale = $form->sale ?? 0;
|
|
|
|
//过滤null字段
|
|
foreach ($form->input() as $k => $v) {
|
|
if (is_null($v)) {
|
|
$form->$k = '';
|
|
}
|
|
}
|
|
})->saved(function(Form $form) {
|
|
//如果是审核的产品,将产品同步到代理商产品
|
|
if ($form->isEditing() && $form->status == ProductStatus::ON_SALE) {
|
|
$ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray();
|
|
//同步信息到代理商产品,注:组合产品不同步
|
|
AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([
|
|
'title' => $form->title,
|
|
'pictures' => explode(',', $form->pictures),
|
|
'know' => $form->know,
|
|
'content' => $form->content,
|
|
]);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function audit()
|
|
{
|
|
$cloud = \App\Models\Product::query()->where('status',ProductStatus::UNAUDITED)->count();
|
|
$industry = \App\Models\IndustryProduct::query()->where('status',ProductStatus::UNAUDITED)->count();
|
|
$demand = \App\Models\DemandProduct::query()->where('status',ProductStatus::UNAUDITED)->count();
|
|
$total = $cloud + $industry + $demand;
|
|
$arr = [
|
|
'cloud' => $cloud ?? 0,
|
|
'industry' => $industry ?? 0,
|
|
'demand' => $demand ?? 0,
|
|
'total' => $total ?? 0,
|
|
];
|
|
return json_encode($arr);
|
|
}
|
|
}
|