From 24439c851b3345da5abb7e05c3f8e9f87c16c50e Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Mon, 14 Feb 2022 15:54:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B4=AD=E7=89=A9=E8=BD=A6?= =?UTF-8?q?=E4=B8=8B=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Forms/DealerProductLvlRule.php | 4 +- app/Admin/Forms/Settings/Dealer.php | 12 ++- .../Dealer/ProductLvlRuleResource.php | 2 +- app/Services/Dealer/OrderService.php | 75 ++++++++++++++++--- database/seeders/AppSettingSeeder.php | 12 +++ 5 files changed, 89 insertions(+), 16 deletions(-) diff --git a/app/Admin/Forms/DealerProductLvlRule.php b/app/Admin/Forms/DealerProductLvlRule.php index 80b9add0..f209ef19 100644 --- a/app/Admin/Forms/DealerProductLvlRule.php +++ b/app/Admin/Forms/DealerProductLvlRule.php @@ -51,7 +51,7 @@ class DealerProductLvlRule extends Form implements LazyRenderable $_rule = DealerProductLvlRuleModel::find($rule['id']); $_rule['lvl'] = $rule['lvl']; $_rule['sale_price'] = $rule['sale_price']; - $_rule['min_order_amount'] = $rule['min_order_amount']; + // $_rule['min_order_amount'] = $rule['min_order_amount']; $lvlRules[] = $_rule; } } @@ -88,7 +88,7 @@ class DealerProductLvlRule extends Form implements LazyRenderable 6 => '一级经销商', ]); $form->currency('sale_price', '等级进货单价')->symbol('¥'); - $form->currency('min_order_amount', '等级单次最低进货价')->symbol('¥'); + // $form->currency('min_order_amount', '等级单次最低进货价')->symbol('¥'); }); } diff --git a/app/Admin/Forms/Settings/Dealer.php b/app/Admin/Forms/Settings/Dealer.php index 29767556..be8d3b73 100644 --- a/app/Admin/Forms/Settings/Dealer.php +++ b/app/Admin/Forms/Settings/Dealer.php @@ -43,9 +43,15 @@ class Dealer extends Form $this->currency('withdraw_fee_rate', '提现费率')->symbol('%'); $this->number('withdraw_days', '提现间隔'); - $this->currency('upgrade_amount_'.DealerLvl::Contracted->value, '签约门槛')->symbol('¥'); - $this->currency('upgrade_amount_'.DealerLvl::Special->value, '特邀门槛')->symbol('¥'); - $this->currency('upgrade_amount_'.DealerLvl::Gold->value, '金牌门槛')->symbol('¥'); + $this->currency('upgrade_amount_'.DealerLvl::Gold->value, '金牌升级门槛')->symbol('¥'); + $this->currency('upgrade_amount_'.DealerLvl::Special->value, '特邀升级门槛')->symbol('¥'); + $this->currency('upgrade_amount_'.DealerLvl::Contracted->value, '签约升级门槛')->symbol('¥'); + + $this->currency('min_order_amount_'.DealerLvl::Gold->value, '金牌单次进货最低金额')->symbol('¥'); + $this->currency('min_order_amount_'.DealerLvl::Special->value, '特邀单次进货最低金额')->symbol('¥'); + $this->currency('min_order_amount_'.DealerLvl::Contracted->value, '签约单次进货最低金额')->symbol('¥'); + $this->currency('min_order_amount_'.DealerLvl::Secondary->value, '二级签约单次进货最低金额')->symbol('¥'); + $this->currency('min_order_amount_'.DealerLvl::Top->value, '一级签约单次进货最低金额')->symbol('¥'); $this->number('order_auto_allocate_times', '订单自动分配时间(分)')->min(1); diff --git a/app/Endpoint/Api/Http/Resources/Dealer/ProductLvlRuleResource.php b/app/Endpoint/Api/Http/Resources/Dealer/ProductLvlRuleResource.php index 9a0be60e..1130756a 100644 --- a/app/Endpoint/Api/Http/Resources/Dealer/ProductLvlRuleResource.php +++ b/app/Endpoint/Api/Http/Resources/Dealer/ProductLvlRuleResource.php @@ -17,7 +17,7 @@ class ProductLvlRuleResource extends JsonResource return [ 'lvl' => $this->lvl, 'sale_price' => $this->sale_price, - 'min_order_amount' => $this->min_order_amount, + 'min_order_amount' => app_settings('dealer.min_order_amount_'.$this->lvl, 0), ]; } } diff --git a/app/Services/Dealer/OrderService.php b/app/Services/Dealer/OrderService.php index 3370bf71..02875609 100644 --- a/app/Services/Dealer/OrderService.php +++ b/app/Services/Dealer/OrderService.php @@ -8,6 +8,7 @@ use App\Enums\DealerWalletAction; use App\Exceptions\BizException; use App\Models\DealerOrder; use App\Models\DealerOrderAllocateLog; +use App\Models\DealerOrderProduct; use App\Models\DealerProduct; use App\Models\DealerUserProductLog; use App\Models\ShippingAddress; @@ -55,13 +56,13 @@ class OrderService * @param integer $number * @return string */ - public function totalAmount(User $user, DealerProduct $product, int $number = 0) + public function totalAmount(User $user, DealerProduct $product, int $number = 0, ?int $allNumber = 0) { - return bcmul($this->getSalePrice($user, $product, $number), $number, 2); + return bcmul($this->getSalePrice($user, $product, $allNumber ?? $number), $number, 2); } /** - * 创建订单 + * 快捷创建订单(单个商品下单) * * @param User $user * @param DealerProduct $product @@ -71,13 +72,13 @@ class OrderService */ public function quickCreateOrder(User $user, DealerProduct $product, int $number = 0, int $shippingAddressId) { - //判断是否满足当前等级最低补货价 $totalAmount = $this->totalAmount($user, $product, $number); - foreach ($product->lvlRules as $rule) { - if ($user->dealer && $rule->lvl == $user->dealer->lvl->value && $totalAmount < $rule->min_order_amount) { - throw new BizException('当前单次补货价格不能低于'.$rule->min_order_amount.'元'); - } - } + // foreach ($product->lvlRules as $rule) { + // $min_order_amount = app_settings('min_order_amount'.$rule->lvl); + // if ($user->dealer && $rule->lvl == $user->dealer->lvl->value && $totalAmount < $min_order_amount) { + // throw new BizException('当前单次补货价格不能低于'.$min_order_amount.'元'); + // } + // } $order = $this->createOrder($user, $totalAmount, $shippingAddressId); @@ -100,9 +101,58 @@ class OrderService return $order; } + /** + * 购物车创建订单 + * + * @param User $user + * @param [type] $cartIds + * @param integer $shippingAddressId + * @return DealerOrder $order + */ public function cartCreateOrder(User $user, $cartIds, int $shippingAddressId) { - return null; // + //获取购物车商品 + $shoppingCartItems = $user->dealerShoppingCartItems()->findMany($cartIds); + if ($shoppingCartItems->count() !== count($cartIds)) { + throw new BizException('购物车商品已丢失'); + } + $shoppingCartItems->load('product'); + $totalQty = $shoppingCartItems->sum('quantity'); + $totalAmount = 0; + $orderProducts = []; + foreach ($shoppingCartItems as $item) { + if (!$item->product->isOnline()) { + throw new BizException('购物车商品已失效'); + } + //计算订单价格 + $totalAmount += $this->totalAmount($user, $item->product, $item->quantity, $totalQty); + //组装订单商品 + $orderProducts[] = [ + 'product_id' => $item->product_id, + 'name'=> $item->name, + 'subtitle' => $item->product->subtitle, + 'cover' => $item->cover, + 'price' => $item->sell_price, + 'sale_price' => $this->getSalePrice($user, $item->product, $totalQty), + 'qty' => $item->quantity, + ]; + } + + $order = $this->createOrder($user, $totalAmount, $shippingAddressId); + + DealerOrderProduct::insert(array_map(function ($product) use ($order) { + return array_merge($product, [ + 'order_id'=>$order->id, + 'created_at'=> $order->created_at, + 'updated_at'=> $order->updated_at, + ]); + }, $orderProducts)); + + if (!$order->consignor) {//如果订单分配给公司,则直接确认 + $this->confirmOrder($order); + } + + return $order; } /** @@ -115,6 +165,11 @@ class OrderService */ protected function createOrder(User $user, $totalAmount, int $shippingAddressId) { + //判断是否满足当前等级最低补货价 + $min_order_amount = app_settings('dealer.min_order_amount_'.$user->dealer?->lvl->value, 0); + if ($user->dealer && $totalAmount < $min_order_amount) { + throw new BizException('当前单次补货价格不能低于'.$min_order_amount.'元'); + } //找到发货人 $consignor = $this->getConsignor($user, $totalAmount); diff --git a/database/seeders/AppSettingSeeder.php b/database/seeders/AppSettingSeeder.php index 4f05c72a..23c85db7 100644 --- a/database/seeders/AppSettingSeeder.php +++ b/database/seeders/AppSettingSeeder.php @@ -190,6 +190,18 @@ class AppSettingSeeder extends Seeder // 合约经销商升级金额 'upgrade_amount_'.DealerLvl::Contracted->value => '26400', + //单次下单最低金额 + // 金牌经销商 + 'min_order_amount_'.DealerLvl::Gold->value => '1260', + // 特邀经销商 + 'min_order_amount_'.DealerLvl::Special->value => '1720', + // 签约约经销商 + 'min_order_amount_'.DealerLvl::Contracted->value => '2640', + // 二级签约约经销商 + 'min_order_amount_'.DealerLvl::Secondary->value => '2640', + // 一级签约约经销商 + 'min_order_amount_'.DealerLvl::Top->value => '2640', + // 渠道补贴规则 'channel_rules' => [ // 签约 -> 签约 ->签约