|
|
@@ -215,29 +215,67 @@ class StoreOrderSuccessServices extends BaseServices
|
|
|
|
|
|
if ($hasSubordinate) {
|
|
|
// 直推上级已有下级,获取所有下级
|
|
|
- $groupMembers = $giftGroupService->getList([
|
|
|
+ $allSubordinates = $giftGroupService->getList([
|
|
|
'product_id' => $product_id,
|
|
|
'pid' => $spread_uid
|
|
|
], '*', 0, 0, 'create_time ASC');
|
|
|
- @file_put_contents('quanju4.txt', count($groupMembers) . "-小组人数\r\n", 8);
|
|
|
+
|
|
|
+ // 获取最后一个下级的group_pid来判断小组是否满人
|
|
|
+ $lastMember = $allSubordinates[count($allSubordinates) - 1];
|
|
|
+ $lastGroupPid = $lastMember['group_pid'];
|
|
|
+
|
|
|
+ // 统计小组人数:从最后一个成员开始,沿group_pid向上追溯到根节点
|
|
|
+ // 然后统计所有pid=spread_uid且group_pid在链条上的成员
|
|
|
+ $groupChain = [];
|
|
|
+ $currentGroupPid = $lastGroupPid;
|
|
|
+
|
|
|
+ // 追溯group_pid链条
|
|
|
+ while ($currentGroupPid > 0) {
|
|
|
+ $groupChain[] = $currentGroupPid;
|
|
|
+ // 查找这个group_pid对应的成员
|
|
|
+ $chainMember = $giftGroupService->getOne([
|
|
|
+ 'product_id' => $product_id,
|
|
|
+ 'uid' => $currentGroupPid
|
|
|
+ ]);
|
|
|
+ if ($chainMember) {
|
|
|
+ $currentGroupPid = $chainMember['group_pid'];
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 统计小组人数:pid=spread_uid 且 group_pid 在链条中或为0
|
|
|
+ $groupCount = 0;
|
|
|
+ foreach ($allSubordinates as $sub) {
|
|
|
+ if (in_array($sub['group_pid'], $groupChain) || $sub['group_pid'] == 0) {
|
|
|
+ $groupCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $isGroupFull = $groupCount >= $groupMaxNum;
|
|
|
+
|
|
|
+ @file_put_contents('quanju4.txt', $groupCount . "-小组人数\r\n", 8);
|
|
|
@file_put_contents('quanju4.txt', $groupMaxNum . "-系统设置人数\r\n", 8);
|
|
|
+ @file_put_contents('quanju4.txt', $lastGroupPid . "-最后一个人的group_pid\r\n", 8);
|
|
|
+ @file_put_contents('quanju4.txt', json_encode($groupChain) . "-group_pid链条\r\n", 8);
|
|
|
+ @file_put_contents('quanju4.txt', ($isGroupFull ? '小组已满' : '小组未满') . "-小组状态\r\n", 8);
|
|
|
|
|
|
// 判断小组是否满人
|
|
|
- if (count($groupMembers) >= $groupMaxNum) {
|
|
|
+ if ($isGroupFull) {
|
|
|
// 小组满人,从后往前查找未满的直推下级小组
|
|
|
$foundGroup = false;
|
|
|
|
|
|
// 倒序遍历小组成员,查找未满的直推下级小组
|
|
|
- for ($i = count($groupMembers) - 1; $i >= 0; $i--) {
|
|
|
- $member = $groupMembers[$i];
|
|
|
-
|
|
|
- // 查找该成员的直推下级
|
|
|
+ for ($i = count($allSubordinates) - 1; $i >= 0; $i--) {
|
|
|
+ $member = $allSubordinates[$i];
|
|
|
+
|
|
|
+ // 查找该成员作为小组长的小组成员(group_pid等于该成员uid的所有人)
|
|
|
$subGroupMembers = $giftGroupService->getList([
|
|
|
'product_id' => $product_id,
|
|
|
- 'pid' => $member['uid']
|
|
|
+ 'group_pid' => $member['uid']
|
|
|
], '*', 0, 0, 'create_time ASC');
|
|
|
-
|
|
|
- // 如果该成员没有下级,或者下级小组未满,可以加入
|
|
|
+
|
|
|
+ // 如果该成员的小组未满(包括自己作为组长的0或多人),可以加入
|
|
|
if (count($subGroupMembers) < $groupMaxNum) {
|
|
|
$data['group_pid'] = $member['uid'];
|
|
|
$data['fake_pid'] = $spread_uid;
|
|
|
@@ -248,13 +286,13 @@ class StoreOrderSuccessServices extends BaseServices
|
|
|
|
|
|
// 如果所有成员的直推下级小组都满了,建立新小组
|
|
|
if (!$foundGroup) {
|
|
|
- $lastMember = $groupMembers[count($groupMembers) - 1];
|
|
|
+ $lastMember = $allSubordinates[count($allSubordinates) - 1];
|
|
|
$data['group_pid'] = 0;
|
|
|
$data['fake_pid'] = $lastMember['uid'];
|
|
|
}
|
|
|
} else {
|
|
|
- // 小组未满,新成员的group_pid是最后一个下级
|
|
|
- $lastMember = $groupMembers[count($groupMembers) - 1];
|
|
|
+ // 小组未满,新成员应该加入当前小组
|
|
|
+ // 新成员的group_pid是最后一个下级的uid(成为小组长)
|
|
|
$data['group_pid'] = $lastMember['uid'];
|
|
|
$data['fake_pid'] = $spread_uid;
|
|
|
}
|