diff --git a/app/Services/Dealer/OrderService.php b/app/Services/Dealer/OrderService.php index 9a2805e0..bd2b5fdd 100644 --- a/app/Services/Dealer/OrderService.php +++ b/app/Services/Dealer/OrderService.php @@ -327,18 +327,43 @@ class OrderService 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; - } - return $consignor; + // //老逻辑; + // $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; } }