From 899549976d2ef7d3f2e59a773e400a51ef693647 Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Wed, 1 Sep 2021 14:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=B8=8A=E6=9E=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/AdminAgent/Forms/Setting.php | 16 ++++- .../Controllers/DemandController.php | 62 +++++++++---------- .../Controllers/ProductController.php | 59 +++++++++++++++++- app/Models/AgentProduct.php | 2 +- 4 files changed, 105 insertions(+), 34 deletions(-) diff --git a/app/AdminAgent/Forms/Setting.php b/app/AdminAgent/Forms/Setting.php index 130b996..518e3c0 100644 --- a/app/AdminAgent/Forms/Setting.php +++ b/app/AdminAgent/Forms/Setting.php @@ -34,7 +34,21 @@ class Setting extends Form { $this->text('order_timeout')->required()->default(60) ->help('订单超时时间,单位:分钟。(订金、定金支付的超时时间将根据产品设定的超时时间)'); - $this->radio('auto_shelves')->options(['否', '是'])->required()->default(1) + $this->radio('auto_shelves')->when([1], function (Form $form) { + // 值为1和4时显示文本框 + $form->number('profit','利润(%)')->min(0)->max(100)->help('请填写利润百分比 上架商品会自动根据供应商提供的价格 上调设置值的百分比'); + $form->radio('auto_category','自动添加分类')->options([ + '否', + '是', + ]) + ->default(0) + ->help('选择后 自动上架时会自动匹配分类 如无相同分类 则自动创建分类'); + }) + ->options([ + '否', + '是', + ]) + ->required()->default(0) ->help('是否自动上架合作供应商的产品,如果选是,当合作供应商发布新产品之后,将自动上架供应商的产品'); } diff --git a/app/AdminGuide/Controllers/DemandController.php b/app/AdminGuide/Controllers/DemandController.php index 90e8e49..db43f05 100755 --- a/app/AdminGuide/Controllers/DemandController.php +++ b/app/AdminGuide/Controllers/DemandController.php @@ -179,35 +179,35 @@ class DemandController extends AdminController }); } - public function binding() - { - $demandBiddingId = request('demand_bidding_id',0); - $demandBidding = DemandBidding::find($demandBiddingId); - if (empty($demandBidding)) { - return false; - } - - $demand = \App\Models\Demand::find($demandBidding->demand_id); - - if (empty($demand)) { - return false; - } - - DB::beginTransaction(); - try { - $demandBidding->state = 1; - $demandBidding->save(); - //改变订单状态 - $demand->bidding_id = $demandBidding->id; - $demand->bidding_user_id = $demandBidding->bidding_user_id; - $demand->state = DemandTraits::$stateKey[1]; - $demand->save(); - DB::commit(); - } catch (\Exception $e) { - Log::error('选中竞标失败::'.$e->getTraceAsString()); - DB::rollBack(); - return $this->jsonFail(1001,'选中竞标失败,稍后重试或联系管理员!'.$e->getMessage()); - } - return back(); - } + //public function binding() + //{ + // $demandBiddingId = request('demand_bidding_id',0); + // $demandBidding = DemandBidding::find($demandBiddingId); + // if (empty($demandBidding)) { + // return false; + // } + // + // $demand = \App\Models\Demand::find($demandBidding->demand_id); + // + // if (empty($demand)) { + // return false; + // } + // + // DB::beginTransaction(); + // try { + // $demandBidding->state = 1; + // $demandBidding->save(); + // //改变订单状态 + // $demand->bidding_id = $demandBidding->id; + // $demand->bidding_user_id = $demandBidding->bidding_user_id; + // $demand->state = DemandTraits::$stateKey[1]; + // $demand->save(); + // DB::commit(); + // } catch (\Exception $e) { + // Log::error('选中竞标失败::'.$e->getTraceAsString()); + // DB::rollBack(); + // return $this->jsonFail(1001,'选中竞标失败,稍后重试或联系管理员!'.$e->getMessage()); + // } + // return back(); + //} } diff --git a/app/AdminSupplier/Controllers/ProductController.php b/app/AdminSupplier/Controllers/ProductController.php index 2eac557..8c78d20 100644 --- a/app/AdminSupplier/Controllers/ProductController.php +++ b/app/AdminSupplier/Controllers/ProductController.php @@ -5,6 +5,8 @@ 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; @@ -12,6 +14,7 @@ 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 { @@ -124,12 +127,66 @@ class ProductController extends AdminController $form->supplier_id = Admin::user()->id; })->saved(function (Form $form, $result) { //下架代理商产品 + $id = $form->getKey(); if ($result) { - $id = $form->getKey(); 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)) { diff --git a/app/Models/AgentProduct.php b/app/Models/AgentProduct.php index d8cb9ce..6b03135 100644 --- a/app/Models/AgentProduct.php +++ b/app/Models/AgentProduct.php @@ -62,7 +62,7 @@ class AgentProduct extends BaseModel public function setPicturesAttribute($value) { if (is_array($value)) { - $this->attributes['pictures'] = json_encode(array_filter($value)); + $this->attributes['pictures'] = json_encode(array_filter($value)) ?? ''; } }