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.
197 lines
6.8 KiB
197 lines
6.8 KiB
<?php
|
|
|
|
namespace App\AdminSupplier\Controllers;
|
|
|
|
use App\AdminSupplier\Repositories\Product;
|
|
use App\Common\ProductStatus;
|
|
use App\Models\AgentProduct;
|
|
use App\Models\AgentProductItem;
|
|
use App\Models\AgentSetting;
|
|
use App\Models\Category;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ProductController extends AdminController
|
|
{
|
|
protected $title = '产品';
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Product(['category:id,name']), function (Grid $grid) {
|
|
$grid->model()->where('supplier_id', Admin::user()->id);
|
|
|
|
$grid->column('id')->sortable();
|
|
$grid->column('category.name', '产品分类');
|
|
$grid->column('title');
|
|
$grid->column('picture')->image('', 60, 60);
|
|
$grid->column('price');
|
|
$grid->column('original_price');
|
|
$grid->column('stock');
|
|
$grid->column('sale');
|
|
$grid->column('status')->using(ProductStatus::array());
|
|
$grid->column('verify_mobile','核销号码');
|
|
$grid->column('created_at');
|
|
$grid->column('updated_at');
|
|
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->equal('id');
|
|
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Product(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('supplier_id');
|
|
$show->field('category_id');
|
|
$show->field('title');
|
|
$show->field('price');
|
|
$show->field('original_price');
|
|
$show->field('pictures')->image('', 80, 80);
|
|
$show->field('stock');
|
|
$show->field('sale');
|
|
$show->field('status');
|
|
$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('verify_mobile','核销号码');
|
|
$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) {
|
|
//不允许编辑非自己数据
|
|
if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
$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->text('price')->required();
|
|
$form->text('original_price')->required();
|
|
$form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
|
|
$form->text('stock')->default(9999)->required();
|
|
$form->editor('know');
|
|
$form->editor('content')->required();
|
|
$form->text('verify_mobile','核销号码');
|
|
if ($form->isEditing()) {
|
|
$form->confirm('提示', '编辑产品需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
|
|
}
|
|
})->saving(function (Form $form) {
|
|
//不允许编辑非自己数据
|
|
if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
//不允许编辑的字段 TODO 忽略字段不起作用
|
|
$form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
|
|
|
|
//null字段转为''
|
|
foreach ($form->input() as $k => $v) {
|
|
if (is_null($v)) {
|
|
$form->$k = '';
|
|
}
|
|
}
|
|
|
|
//特殊字段处理
|
|
$form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
|
|
$form->status = ProductStatus::UNAUDITED;
|
|
$form->supplier_id = Admin::user()->id;
|
|
})->saved(function (Form $form, $result) {
|
|
//下架代理商产品
|
|
$id = $form->getKey();
|
|
if ($result) {
|
|
AgentProduct::where('product_id', $id)
|
|
->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉
|
|
->update(['status' => ProductStatus::SOLD_OUT]);
|
|
}
|
|
|
|
//自动上架
|
|
if ($form->isCreating()) {
|
|
$agentIds = AgentProductItem::query()->withoutGlobalScope('orderById')->where('supplier_id',$form->supplier_id)->distinct()->pluck('agent_id');
|
|
foreach ($agentIds as $v) {
|
|
//如果没开启自动上架 滚蛋
|
|
if(empty(AgentSetting::val($v,'auto_shelves'))) {
|
|
continue;
|
|
}
|
|
DB::beginTransaction();
|
|
try {
|
|
$agentProduct = new AgentProduct();
|
|
$agentProduct->title = $form->title;
|
|
$agentProduct->agent_id = $v;
|
|
$agentProduct->product_id = $id;
|
|
$agentProduct->product_ids = explode(',',$id);
|
|
$agentProduct->stock = $form->stock;
|
|
$agentProduct->status = 1;
|
|
$agentProduct->pictures = explode(',',$form->pictures);
|
|
$agentProduct->content = $form->content;
|
|
$agentProduct->know = $form->know;
|
|
//计算价格
|
|
$profit = AgentSetting::val($v, 'profit') ?? 0;
|
|
$price = bcmul($form->price, bcdiv($profit + 100, 100, 6), 2);
|
|
|
|
$agentProduct->price = $price;
|
|
$agentProduct->original_price = $price;
|
|
|
|
//自动添加分类
|
|
$autoCategory = AgentSetting::val($v, 'auto_category') ?? 0;
|
|
|
|
if (!empty($autoCategory)) {
|
|
$categoryName = Category::query()->where('id', $form->category_id)->value('name');
|
|
$category = Category::query()->firstOrCreate(['agent_id' => $v, 'name' => $categoryName]);
|
|
$agentProduct->category_id = $category->id;
|
|
}
|
|
|
|
$agentProduct->save();
|
|
|
|
//维护关联表
|
|
$agentProductItem = AgentProductItem::query()->create([
|
|
'agent_id' => $v,
|
|
'supplier_id' => $form->supplier_id,
|
|
'agent_product_id' => $agentProduct->id,
|
|
'product_id' => $id,
|
|
]);
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
Log::error('自动上架失败::'.$e->getTraceAsString());
|
|
DB::rollBack();
|
|
return $form->response->error('自动上架失败,稍后重试或联系管理员!'.$e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
})->deleting(function (Form $form) {
|
|
//不允许删除非自己的数据
|
|
if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
});
|
|
}
|
|
}
|