763 lines
26 KiB
PHP
763 lines
26 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Dealer;
|
|
|
|
use App\Enums\DealerDeliveryBillStatus;
|
|
use App\Enums\DealerLvl;
|
|
use App\Enums\DealerOrderStatus;
|
|
use App\Enums\DealerWalletAction;
|
|
use App\Enums\PayWay;
|
|
use App\Enums\WxpayTradeType;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\DealerDeliveryBill;
|
|
use App\Models\DealerOrder;
|
|
use App\Models\DealerOrderAllocateLog;
|
|
use App\Models\DealerOrderProduct;
|
|
use App\Models\DealerProduct;
|
|
use App\Models\DealerUserProduct;
|
|
use App\Models\DealerUserProductLog;
|
|
use App\Models\ShippingAddress;
|
|
use App\Models\User;
|
|
use App\Services\Payment\WxpayService;
|
|
use App\Services\PayService;
|
|
use Illuminate\Database\QueryException;
|
|
|
|
class OrderService
|
|
{
|
|
/**
|
|
* 获取单个商品实际价格
|
|
*
|
|
* @param User $user
|
|
* @param DealerProduct $product
|
|
* @param integer $number
|
|
*/
|
|
public function getSalePrice(User $user, DealerProduct $product, int $number = 0)
|
|
{
|
|
//获取等级规则,判断当前用户等级是否配置等级价格
|
|
$salePrice = $product->price;
|
|
if ($user->dealer) {
|
|
foreach ($product->lvlRules as $rule) {
|
|
if ($rule->lvl == $user->dealer->lvl->value) {
|
|
$salePrice = $rule->sale_price;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//获取销售规则,判断当前用户购买数量价格
|
|
foreach ($product->saleRules as $rule) {
|
|
if ($number >= $rule->qty) {
|
|
if ($salePrice > $rule->price) {
|
|
$salePrice = $rule->price;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return $salePrice;
|
|
}
|
|
|
|
/**
|
|
* 计算订单价格
|
|
*
|
|
* @param DealerProduct $product
|
|
* @param integer $number
|
|
* @return string
|
|
*/
|
|
public function totalAmount(User $user, DealerProduct $product, int $number = 0, ?int $allNumber = null)
|
|
{
|
|
return bcmul($this->getSalePrice($user, $product, $allNumber ?? $number), $number, 2);
|
|
}
|
|
|
|
/**
|
|
* 快捷创建订单(单个商品下单)
|
|
*
|
|
* @param User $user
|
|
* @param DealerProduct $product
|
|
* @param integer $number
|
|
* @param integer $shippingAddressId
|
|
* @return DealerOrder $order
|
|
*/
|
|
public function quickCreateOrder(User $user, DealerProduct $product, int $number = 0, int $shippingAddressId)
|
|
{
|
|
$totalAmount = $this->totalAmount($user, $product, $number);
|
|
// 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);
|
|
|
|
//保存订单商品-----一个订单对应一个商品
|
|
$order->products()->create([
|
|
'order_id' => $order->id,
|
|
'product_id'=> $product->id,
|
|
'name'=> $product->name,
|
|
'subtitle'=> $product->subtitle,
|
|
'cover'=> $product->cover,
|
|
'price' => $product->price,
|
|
'sale_price'=> bcdiv($totalAmount, $number, 2),
|
|
'qty'=> $number,
|
|
]);
|
|
|
|
if (!$order->consignor) {//如果订单分配给公司,则直接确认
|
|
$this->confirmOrder($order);
|
|
}
|
|
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 购物车创建订单
|
|
*
|
|
* @param User $user
|
|
* @param [type] $cartIds
|
|
* @param integer $shippingAddressId
|
|
* @return DealerOrder $order
|
|
*/
|
|
public function cartCreateOrder(User $user, $cartIds, int $shippingAddressId)
|
|
{
|
|
//获取购物车商品
|
|
$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));
|
|
|
|
//清除购物车对应商品
|
|
$user->dealerShoppingCartItems()->whereIn('id', $cartIds)->delete();
|
|
|
|
if (!$order->consignor) {//如果订单分配给公司,则直接确认
|
|
$this->confirmOrder($order);
|
|
}
|
|
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 创建订单
|
|
*
|
|
* @param User $user
|
|
* @param [type] $totalAmount
|
|
* @param integer $shippingAddressId
|
|
* @return DealerOrder $order
|
|
*/
|
|
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);
|
|
|
|
//找到收货地址
|
|
$shippingAddress = $this->getShippingAddress($user, $shippingAddressId);
|
|
|
|
//保存订单
|
|
$order = new DealerOrder();
|
|
do {
|
|
try {
|
|
$order->sn = serial_number();
|
|
$order->user_id = $user->id;
|
|
$order->consignor_id = $consignor?->user_id;
|
|
$order->total_amount = $totalAmount;
|
|
$order->consignee_name = $shippingAddress->consignee;
|
|
$order->consignee_telephone = $shippingAddress->telephone;
|
|
$order->consignee_zone = $shippingAddress->zone;
|
|
$order->consignee_address = $shippingAddress->address;
|
|
$order->allocated_at = now();//记录分配时间
|
|
$order->save();
|
|
break;
|
|
} catch (QueryException $e) {
|
|
if (strpos($e->getMessage(), 'Duplicate entry') === false) {
|
|
throw $e;
|
|
}
|
|
}
|
|
} while (true);
|
|
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 确认接单
|
|
*
|
|
* @param DealerOrder $order
|
|
* @return DealerOrder $order
|
|
*/
|
|
public function confirmOrder(DealerOrder $order)
|
|
{
|
|
if (!$order->isPending()) {
|
|
throw new BizException('无法接单:订单状态异常,请刷新后再试');
|
|
}
|
|
$order->update([
|
|
'status' => DealerOrderStatus::Paying,
|
|
]);
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 确认打款
|
|
*
|
|
* @return DealerOrder $order
|
|
*/
|
|
public function payOrder(DealerOrder $order, string $payWay, ?string $payImage = null)
|
|
{
|
|
if (empty($payWay)) {
|
|
throw new BizException('请选择付款方式');
|
|
}
|
|
if (!$order->isPendinged()) {
|
|
throw new BizException('无法付款:订单状态异常,请刷新后再试');
|
|
}
|
|
switch ($payWay) {
|
|
case PayWay::Wallet->value:
|
|
/** 付款以及完成确认收款动作 **/
|
|
$walletService = new WalletService();
|
|
//付款
|
|
$walletService->changeBalance($order->user, 0 - $order->total_amount, DealerWalletAction::OrderPaid, '订单:'.$order->sn, $order);
|
|
$order->update([
|
|
'status'=>DealerOrderStatus::Confirming,
|
|
'pay_time' => now(),
|
|
'pay_way' => PayWay::Wallet,
|
|
]);
|
|
//收款
|
|
if ($order->consignor) {
|
|
$walletService->changeBalance($order->consignor, $order->total_amount, DealerWalletAction::OrderIncome, '订单:'.$order->sn, $order);
|
|
}
|
|
$this->paidOrder($order);
|
|
break;
|
|
case PayWay::Offline->value:
|
|
$order->update([
|
|
'status' => DealerOrderStatus::Confirming,
|
|
'pay_image' => $payImage,
|
|
'pay_info' => $order->getConsignorPayInfo() ?? null,
|
|
'pay_time' => now(),
|
|
'pay_way' => PayWay::Offline,
|
|
]);
|
|
break;
|
|
}
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 支付订单
|
|
*
|
|
* @param DealerOrder $order
|
|
* @param \App\Enums\PayWay $payWay
|
|
* @param string|null $payImage
|
|
* @return array
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
*/
|
|
public function pay(DealerOrder $order, PayWay $payWay, ?string $payImage = null): array
|
|
{
|
|
if (! $order->isPendinged()) {
|
|
throw new BizException('订单状态不是待付款');
|
|
}
|
|
|
|
$payLog = $order->payLogs()->create([
|
|
'pay_way' => $payWay,
|
|
]);
|
|
|
|
$data = [
|
|
'pay_sn' => $payLog->pay_sn,
|
|
];
|
|
|
|
switch ($payLog->pay_way) {
|
|
case PayWay::Offline:
|
|
(new PayService())->handleSuccess($payLog, [
|
|
'pay_image' => $payImage,
|
|
'pay_info' => $order->getConsignorPayInfo() ?? null,
|
|
'pay_at' => now(),
|
|
]);
|
|
|
|
$data = null;
|
|
|
|
break;
|
|
|
|
case PayWay::Wallet:
|
|
$walletService = new WalletService();
|
|
|
|
// 扣除打款人余额
|
|
$walletService->changeBalance(
|
|
$order->user,
|
|
bcmul($order->total_amount, '-1', 2),
|
|
DealerWalletAction::OrderPaid,
|
|
"订单:{$order->sn}",
|
|
$order
|
|
);
|
|
|
|
// 增加收款人余额
|
|
if ($order->consignor) {
|
|
$walletService->changeBalance(
|
|
$order->consignor,
|
|
$order->total_amount,
|
|
DealerWalletAction::OrderIncome,
|
|
"订单:{$order->sn}",
|
|
$order
|
|
);
|
|
}
|
|
|
|
(new PayService())->handleSuccess($payLog, [
|
|
'pay_at' => now(),
|
|
]);
|
|
|
|
$data = null;
|
|
|
|
break;
|
|
|
|
case PayWay::WxpayH5:
|
|
case PayWay::WxpayJsApi:
|
|
if (is_null($tradeType = WxpayTradeType::tryFromPayWay($payLog->pay_way))) {
|
|
throw new BizException('支付方式 非法');
|
|
}
|
|
|
|
$params = [
|
|
'body' => app_settings('app.app_name').'-批零订单',
|
|
'out_trade_no' => $payLog->pay_sn,
|
|
'total_fee' => bcmul($order->total_amount, '100'),
|
|
'trade_type' => $tradeType->value,
|
|
];
|
|
|
|
$data = (new WxpayService())->pay($params, 'yzk_h5');
|
|
|
|
break;
|
|
|
|
default:
|
|
throw new BizException('支付方式不支持');
|
|
break;
|
|
}
|
|
|
|
return [
|
|
'pay_way' => $payLog->pay_way,
|
|
'data' => $data,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 确认收款
|
|
*
|
|
* @param DealerOrder $order
|
|
* @return DealerOrder $order
|
|
*/
|
|
public function paidOrder(DealerOrder $order)
|
|
{
|
|
if (!$order->isPay()) {
|
|
throw new BizException('无法收款:订单状态异常,请刷新后再试');
|
|
}
|
|
$order->update([
|
|
'status' => DealerOrderStatus::Paid,
|
|
'paied_time' => now(),
|
|
]);
|
|
//签约单,云库存直接发货
|
|
if ($order->consignor === null) {
|
|
$this->orderInDepositstock($order);
|
|
}
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 确认发货
|
|
*
|
|
* @param DealerOrder $order
|
|
* @param string $action
|
|
* @return DealerOrder $order
|
|
*/
|
|
public function shippingOrder(DealerOrder $order, ?string $action = 'qty')
|
|
{
|
|
if (!$order->isPaid()) {
|
|
throw new BizException('无法发货:订单状态异常,请刷新后再试');
|
|
}
|
|
//扣减发货人库存
|
|
if ($order->consignor) {
|
|
if ($action == 'deposit_qty') {
|
|
$this->orderOutDepositQty($order);
|
|
} else {
|
|
$this->orderOutQty($order);
|
|
}
|
|
}
|
|
|
|
$order->update([
|
|
'status' => DealerOrderStatus::Shipped,
|
|
'shipping_time' => now(),
|
|
]);
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 使用云仓发货
|
|
*/
|
|
public function shippingOrderByDeposit(DealerOrder $order)
|
|
{
|
|
$depositProducts = [];
|
|
$deliveryBill = null;
|
|
//判断这个订单是否已经有待支付的云仓发货单
|
|
if ($deliveryBill = DealerDeliveryBill::where([
|
|
'user_id' => $order->consignor_id,
|
|
'order_id' => $order->id,
|
|
'status' => DealerDeliveryBillStatus::Pending,
|
|
])->first()) {
|
|
return $deliveryBill;
|
|
}
|
|
//判断本地库存是否足够,不足则生成云仓提货单,并唤起支付。
|
|
foreach ($order->products as $product) {
|
|
//记录需要生成云仓发货单的商品信息以及数量
|
|
if ($_userProduct = DealerUserProduct::where([
|
|
'user_id'=>$order->consignor_id,
|
|
'product_id'=>$product->product_id,
|
|
])->first()) {
|
|
if ($product->qty > $_userProduct->stock) {
|
|
if ($product->qty > $_userProduct->stock + $_userProduct->deposit_stock) {
|
|
throw new BizException('当前可发货库存不足');
|
|
}
|
|
//记录
|
|
$depositProducts[$product->id] = [
|
|
'id'=> $product->product_id,
|
|
'qty'=> $product->qty - $_userProduct->stock,
|
|
];
|
|
}
|
|
} else {
|
|
throw new BizException('当前可发货库存不足');
|
|
}
|
|
}
|
|
|
|
if ($depositProducts) {
|
|
$dealerDeliveryBillService = new DealerDeliveryBillService();
|
|
$deliveryBill = $dealerDeliveryBillService->create($order->consignor, $depositProducts, [
|
|
'name'=>$order->consignee_name,
|
|
'telephone'=>$order->consignee_telephone,
|
|
'zone'=>$order->consignee_zone,
|
|
'address'=>$order->consignee_address,
|
|
], '订单发货:【'.$order->sn.'】', $order);
|
|
//更新订单相关的库存发货情况
|
|
if ($order) {
|
|
foreach ($order->products as $product) {
|
|
if (isset($depositProducts[$product->id])) {
|
|
DealerOrderProduct::where('id', $product->id)->update([
|
|
'qty' => $product->qty - $depositProducts[$product->id]['qty'],
|
|
'deposit_qty' => $depositProducts[$product->id]['qty'],
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $deliveryBill;
|
|
}
|
|
|
|
public function shippingedOrder(DealerOrder $order, ?string $action = 'qty')
|
|
{
|
|
if (!$order->isShipping()) {
|
|
throw new BizException('无法收货:订单状态异常,请刷新后再试');
|
|
}
|
|
//增加自己的库存
|
|
$this->orderInQty($order);
|
|
|
|
$order->update([
|
|
'status' => DealerOrderStatus::Completed,
|
|
'shippinged_time' => now(),
|
|
]);
|
|
return $order;
|
|
}
|
|
|
|
public function cancelOrder(DealerOrder $order)
|
|
{
|
|
if (!($order->isPending() || $order->isPendinged() || $order->isPay())) {
|
|
throw new BizException('无法取消:订单状态异常,请刷新后再试');
|
|
}
|
|
$order->update([
|
|
'status' => DealerOrderStatus::Cancelled,
|
|
]);
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 更新订单发货人
|
|
*
|
|
* @return void
|
|
*/
|
|
public function updateOrderConsignor(DealerOrder $order)
|
|
{
|
|
//只处理当前订单有发货人的情况
|
|
if ($order->consignor) {
|
|
$consignor = $this->getConsignor($order->user, $order->total_amount, $order->consignor);
|
|
$oldConsignor = $order->consignor;
|
|
$order->update([
|
|
'allocated_at' => now(),
|
|
'consignor_id' => $consignor?->user_id,
|
|
]);
|
|
//记录分配日志
|
|
DealerOrderAllocateLog::create([
|
|
'order_id'=>$order->id,
|
|
'last_consignor_id'=>$oldConsignor->id,
|
|
'new_consignor_id' =>$order->consignor_id,
|
|
]);
|
|
}
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* 获取收货地址
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param int|null $shippingAddressId
|
|
* @return \App\Models\ShippingAddress|null
|
|
*
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
|
*/
|
|
protected function getShippingAddress(User $user, ?int $shippingAddressId = null): ?ShippingAddress
|
|
{
|
|
if ($shippingAddressId) {
|
|
return $user->shippingAddresses()->findOrFail($shippingAddressId);
|
|
}
|
|
|
|
return $user->shippingAddresses()->where('is_default', true)->first();
|
|
}
|
|
|
|
/**
|
|
* 用户通过下单新增库存
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function orderInQty(DealerOrder $order)
|
|
{
|
|
foreach ($order->products as $product) {
|
|
//增加本地库存
|
|
$userProduct = $order->user->dealerProducts()->firstOrCreate([
|
|
'product_id'=> $product->product_id,
|
|
]);
|
|
//如果云仓已收货
|
|
if ($order->local_status < 2 && $order->deposit_status !== 1) {
|
|
$userProduct->increment('stock', $product->qty);
|
|
|
|
DealerUserProductLog::create([
|
|
'user_id'=> $order->user_id,
|
|
'product_id'=> $product->product_id,
|
|
'type' => DealerUserProductLog::TYPE_ORDER_IN,
|
|
'qty'=>$product->qty,
|
|
'remark'=>'订单:'.$order->sn,
|
|
]);
|
|
} else {
|
|
$userProduct->increment('stock', $product->qty + $product->deposit_qty);
|
|
|
|
DealerUserProductLog::create([
|
|
'user_id'=> $order->user_id,
|
|
'product_id'=> $product->product_id,
|
|
'type' => DealerUserProductLog::TYPE_ORDER_IN,
|
|
'qty'=>$product->qty + $product->deposit_qty,
|
|
'remark'=>'订单:'.$order->sn,
|
|
]);
|
|
}
|
|
}
|
|
$order->update([
|
|
'local_status' => 2,
|
|
'deposit_status'=> 2,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 用户通过订单云仓库增加本地库存
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function orderInAllQty(DealerOrder $order)
|
|
{
|
|
foreach ($order->products as $product) {
|
|
$userProduct = $order->user->dealerProducts()->firstOrCreate([
|
|
'product_id'=> $product->product_id,
|
|
]);
|
|
$userProduct->increment('stock', $product->qty + $product->deposit_qty);
|
|
|
|
DealerUserProductLog::create([
|
|
'user_id'=> $order->user_id,
|
|
'product_id'=> $product->product_id,
|
|
'type' => DealerUserProductLog::TYPE_ORDER_IN,
|
|
'qty'=>$product->qty + $product->deposit_qty,
|
|
'remark'=>'订单:'.$order->sn,
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户通过订单扣减本地库存发货
|
|
*
|
|
* @return void
|
|
*/
|
|
public function orderOutQty(DealerOrder $order)
|
|
{
|
|
foreach ($order->products as $product) {
|
|
if ($product->qty > 0 && $order->local_status == 0) {
|
|
$userProduct = $order->consignor->dealerProducts()->firstOrCreate([
|
|
'product_id'=> $product->product_id,
|
|
]);
|
|
$userProduct->decrement('stock', $product->qty);
|
|
DealerUserProductLog::create([
|
|
'user_id'=> $order->consignor_id,
|
|
'product_id'=> $product->product_id,
|
|
'type' => DealerUserProductLog::TYPE_ORDER_OUT,
|
|
'qty'=>$product->qty,
|
|
'remark' =>'订单:'.$order->sn,
|
|
]);
|
|
}
|
|
}
|
|
$order->update([
|
|
'local_status'=>1,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 用户通过订单扣减云库存发货
|
|
*
|
|
* @return void
|
|
*/
|
|
public function orderOutDepositQty(DealerOrder $order)
|
|
{
|
|
foreach ($order->products as $product) {
|
|
if ($product->deposit_qty > 0 && $order->deposit_status == 0) {
|
|
$userProduct = $order->consignor->dealerProducts()->firstOrCreate([
|
|
'product_id'=> $product->product_id,
|
|
]);
|
|
$userProduct->decrement('stock', $product->deposit_qty);
|
|
|
|
DealerUserProductLog::create([
|
|
'user_id'=> $order->consignor_id,
|
|
'product_id'=> $product->product_id,
|
|
'type' => DealerUserProductLog::TYPE_ORDER_OUT,
|
|
'qty'=>$product->deposit_qty,
|
|
'remark' =>'订单:'.$order->sn,
|
|
]);
|
|
}
|
|
}
|
|
$order->update([
|
|
'deposit_status'=>1,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 用户通过订单获取云库存
|
|
*
|
|
* @param DealerOrder $order
|
|
* @return void
|
|
*/
|
|
public function orderInDepositstock(DealerOrder $order)
|
|
{
|
|
foreach ($order->products as $product) {
|
|
if ($product->deposit_qty > 0 && $order->deposit_status == 0) {
|
|
$userProduct = $order->user->dealerProducts()->firstOrCreate([
|
|
'product_id'=> $product->product_id,
|
|
]);
|
|
$userProduct->increment('deposit_stock', $product->deposit_qty);
|
|
|
|
DealerUserProductLog::create([
|
|
'is_deposit'=>true,
|
|
'user_id'=> $order->user_id,
|
|
'product_id'=> $product->product_id,
|
|
'type' => DealerUserProductLog::TYPE_ORDER_IN,
|
|
'qty'=>$product->deposit_qty,
|
|
'remark' =>'订单:'.$order->sn,
|
|
]);
|
|
}
|
|
}
|
|
$order->update([
|
|
'deposit_status'=>2,
|
|
]);
|
|
}
|
|
|
|
private function getConsignor(User $user, $totalAmount, ?User $lastConsignor = null)
|
|
{
|
|
$rules = [
|
|
[
|
|
'amount' => app_settings('dealer.upgrade_amount_'.DealerLvl::Contracted->value),
|
|
'lvl' => DealerLvl::Contracted,
|
|
],
|
|
[
|
|
'amount' => app_settings('dealer.upgrade_amount_'.DealerLvl::Special->value),
|
|
'lvl' => DealerLvl::Special,
|
|
],
|
|
[
|
|
'amount' => app_settings('dealer.upgrade_amount_'.DealerLvl::Gold->value),
|
|
'lvl' => DealerLvl::Gold,
|
|
],
|
|
];
|
|
$lvl = $user->dealer->lvl;
|
|
//计算通过这个订单可能升级成为的身份
|
|
foreach ($rules as $rule) {
|
|
if ($totalAmount >= $rule['amount'] && $lvl->value < $rule['lvl']->value) {
|
|
$lvl = $rule['lvl'];
|
|
}
|
|
}
|
|
//如果是签约单,直接抛到公司后台发货
|
|
if ($lvl->value >= DealerLvl::Contracted->value) {
|
|
return null;
|
|
}
|
|
// //老逻辑;
|
|
// $query = UserInfo::with('dealer');
|
|
// if ($lastConsignor) {
|
|
// $query->whereIn('user_id', $lastConsignor->userInfo->real_parent_ids);//上个发货人的上级
|
|
// } else {
|
|
// $query->whereIn('user_id', $user->userInfo->real_parent_ids);//自己的上级
|
|
// }
|
|
// $consignor = $query->whereHas('dealer', function ($q) use ($lvl) {
|
|
// return $q->where('is_sale', true)->where('lvl', '>', $lvl);//可销售的经销商,且身份大于自己的
|
|
// })->orderBy('depth', 'desc')->first();//深度逆序第一个
|
|
// if (!$consignor) {
|
|
// $consignor = $lastConsignor;
|
|
// }
|
|
//新逻辑
|
|
$consignor = null;
|
|
$_lastConsignor = $lastConsignor;
|
|
do {
|
|
$query = User::with(['userInfo', 'dealer']);
|
|
if ($_lastConsignor) {
|
|
$query->where('id', $_lastConsignor->userInfo->real_inviter_id);
|
|
} else {
|
|
$query->where('id', $user->userInfo->real_inviter_id);
|
|
}
|
|
$consignor = $query->first();
|
|
if ($consignor) {//找到老上级
|
|
if ($consignor->dealer->is_sale == true && $consignor->dealer->lvl->value > $lvl->value) {
|
|
break;
|
|
} else {
|
|
$_lastConsignor = $consignor;
|
|
$consignor = null;
|
|
}
|
|
} else {//如果找不到人了
|
|
$consignor = $lastConsignor;
|
|
break;
|
|
}
|
|
} while (empty($consignor));
|
|
|
|
return $consignor?->userInfo;
|
|
}
|
|
}
|