Browse Source

自动上架优化

develop
lemon 4 years ago
parent
commit
899549976d
  1. 16
      app/AdminAgent/Forms/Setting.php
  2. 62
      app/AdminGuide/Controllers/DemandController.php
  3. 59
      app/AdminSupplier/Controllers/ProductController.php
  4. 2
      app/Models/AgentProduct.php

16
app/AdminAgent/Forms/Setting.php

@ -34,7 +34,21 @@ class Setting extends Form
{ {
$this->text('order_timeout')->required()->default(60) $this->text('order_timeout')->required()->default(60)
->help('订单超时时间,单位:分钟。(订金、定金支付的超时时间将根据产品设定的超时时间)'); ->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('是否自动上架合作供应商的产品,如果选是,当合作供应商发布新产品之后,将自动上架供应商的产品'); ->help('是否自动上架合作供应商的产品,如果选是,当合作供应商发布新产品之后,将自动上架供应商的产品');
} }

62
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();
//}
} }

59
app/AdminSupplier/Controllers/ProductController.php

@ -5,6 +5,8 @@ namespace App\AdminSupplier\Controllers;
use App\AdminSupplier\Repositories\Product; use App\AdminSupplier\Repositories\Product;
use App\Common\ProductStatus; use App\Common\ProductStatus;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\AgentProductItem;
use App\Models\AgentSetting;
use App\Models\Category; use App\Models\Category;
use Dcat\Admin\Admin; use Dcat\Admin\Admin;
use Dcat\Admin\Form; use Dcat\Admin\Form;
@ -12,6 +14,7 @@ use Dcat\Admin\Grid;
use Dcat\Admin\Show; use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController; use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class ProductController extends AdminController class ProductController extends AdminController
{ {
@ -124,12 +127,66 @@ class ProductController extends AdminController
$form->supplier_id = Admin::user()->id; $form->supplier_id = Admin::user()->id;
})->saved(function (Form $form, $result) { })->saved(function (Form $form, $result) {
//下架代理商产品 //下架代理商产品
$id = $form->getKey();
if ($result) { if ($result) {
$id = $form->getKey();
AgentProduct::where('product_id', $id) AgentProduct::where('product_id', $id)
->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉 ->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉
->update(['status' => ProductStatus::SOLD_OUT]); ->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) { })->deleting(function (Form $form) {
//不允许删除非自己的数据 //不允许删除非自己的数据
if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) { if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {

2
app/Models/AgentProduct.php

@ -62,7 +62,7 @@ class AgentProduct extends BaseModel
public function setPicturesAttribute($value) public function setPicturesAttribute($value)
{ {
if (is_array($value)) { if (is_array($value)) {
$this->attributes['pictures'] = json_encode(array_filter($value));
$this->attributes['pictures'] = json_encode(array_filter($value)) ?? '';
} }
} }

Loading…
Cancel
Save