|
|
@@ -78,7 +78,7 @@ class UserNetwork extends \common\components\ActiveRecord
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 分页获取上级会员
|
|
|
+ * 使用父级parent_uid,分页获取上级会员
|
|
|
* @param $userId
|
|
|
* @param $offset
|
|
|
* @param $limit
|
|
|
@@ -88,33 +88,46 @@ class UserNetwork extends \common\components\ActiveRecord
|
|
|
* @return array|\yii\db\ActiveRecord[]
|
|
|
*/
|
|
|
public static function getParentsWithOffset($userId, $offset, $limit, $orderBy='DESC', $isSlaves = false, $db = 'db'){
|
|
|
- $userNetInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'PARENT_UIDS', 'LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
|
|
|
- if( !$userNetInfo ) return [];
|
|
|
-
|
|
|
- $parentUidsStr = $userNetInfo['PARENT_UIDS'];
|
|
|
- if( !$parentUidsStr ) return [];
|
|
|
-
|
|
|
- $parentUidsArr = explode(',', $parentUidsStr);
|
|
|
- unset($parentUidsStr);
|
|
|
- if( $orderBy === 'DESC' ) {
|
|
|
- $parentUidsArr = array_reverse($parentUidsArr);
|
|
|
+ $sql = "SELECT t2.USER_ID
|
|
|
+ FROM
|
|
|
+ (
|
|
|
+ SELECT
|
|
|
+ @r AS _id,
|
|
|
+ (SELECT @r := PARENT_UID FROM AR_USER_NETWORK_NEW WHERE USER_ID = _id) AS PARENT_UID,
|
|
|
+ @l := @l + 1 AS lvl
|
|
|
+ FROM
|
|
|
+ (SELECT @r := $userId, @l := 0) vars, AR_USER_NETWORK_NEW AS h
|
|
|
+ WHERE @r <> 0
|
|
|
+ ) t1
|
|
|
+ JOIN AR_USER_NETWORK_NEW t2
|
|
|
+ ON t1._id = t2.USER_ID AND t1._id != $userId;";
|
|
|
+ $parentUidsArr = \Yii::$app->db->createCommand($sql)->queryAll();
|
|
|
+ if (empty($parentUidsArr)) {
|
|
|
+ return [];
|
|
|
}
|
|
|
- if( !$parentUidsArr ) return [];
|
|
|
-
|
|
|
- $pageParentUids = array_slice($parentUidsArr, $offset, $limit);
|
|
|
- unset($parentUidsArr);
|
|
|
+ $allUserIds = array_column($parentUidsArr, 'USER_ID');
|
|
|
+ if ($orderBy === 'ASC') {
|
|
|
+ $allUserIds = array_reverse($allUserIds);
|
|
|
+ }
|
|
|
+ if(empty($allUserIds)) return [];
|
|
|
+ $topDeep = count($allUserIds);
|
|
|
+ $pageParentUids = array_slice($allUserIds, $offset, $limit);
|
|
|
+ unset($allUserIds);
|
|
|
$parentList = [];
|
|
|
foreach ($pageParentUids as $parentUid) {
|
|
|
try {
|
|
|
- $parentNetInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])->asArray()->one();
|
|
|
+ $parentNetInfo = static::find($isSlaves, $db)
|
|
|
+ ->select(['TOP_DEEP', 'RELATIVE_LOCATION', 'LOCATION_TAG', 'PARENT_UID'])
|
|
|
+ ->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])
|
|
|
+ ->asArray()
|
|
|
+ ->one();
|
|
|
+ $location = empty($parentNetInfo['PARENT_UID']) ? $parentNetInfo['LOCATION_TAG'] : $parentNetInfo['RELATIVE_LOCATION'];
|
|
|
$parentList[] = [
|
|
|
'USER_ID' => $userId,
|
|
|
- 'TOP_DEEP' => $userNetInfo['TOP_DEEP'],
|
|
|
+ 'TOP_DEEP' => $topDeep,
|
|
|
'PARENT_UID' => $parentUid,
|
|
|
'PARENT_DEEP' => $parentNetInfo['TOP_DEEP'],
|
|
|
- // 'LOCATION_TAG' => $userNetInfo['LOCATION_TAG'],
|
|
|
- // 'PARENT_LOCATION_TAG' => $parentNetInfo['LOCATION_TAG'],
|
|
|
- 'LOCATION' => substr($userNetInfo['LOCATION_TAG'], strlen($parentNetInfo['LOCATION_TAG']), 1),
|
|
|
+ 'LOCATION' => $location,
|
|
|
];
|
|
|
} catch (Exception $e) {
|
|
|
$file_name = date('Y-m-d', time()).'_usernetwork.php_error.log';
|
|
|
@@ -132,6 +145,61 @@ class UserNetwork extends \common\components\ActiveRecord
|
|
|
return $parentList;
|
|
|
}
|
|
|
|
|
|
+ // /**
|
|
|
+ // * 分页获取上级会员
|
|
|
+ // * @param $userId
|
|
|
+ // * @param $offset
|
|
|
+ // * @param $limit
|
|
|
+ // * @param string $orderBy
|
|
|
+ // * @param bool $isSlaves
|
|
|
+ // * @param string $db
|
|
|
+ // * @return array|\yii\db\ActiveRecord[]
|
|
|
+ // */
|
|
|
+ // public static function getParentsWithOffset($userId, $offset, $limit, $orderBy='DESC', $isSlaves = false, $db = 'db'){
|
|
|
+ // $userNetInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'PARENT_UIDS', 'LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
|
|
|
+ // if( !$userNetInfo ) return [];
|
|
|
+
|
|
|
+ // $parentUidsStr = $userNetInfo['PARENT_UIDS'];
|
|
|
+ // if( !$parentUidsStr ) return [];
|
|
|
+
|
|
|
+ // $parentUidsArr = explode(',', $parentUidsStr);
|
|
|
+ // unset($parentUidsStr);
|
|
|
+ // if( $orderBy === 'DESC' ) {
|
|
|
+ // $parentUidsArr = array_reverse($parentUidsArr);
|
|
|
+ // }
|
|
|
+ // if( !$parentUidsArr ) return [];
|
|
|
+
|
|
|
+ // $pageParentUids = array_slice($parentUidsArr, $offset, $limit);
|
|
|
+ // unset($parentUidsArr);
|
|
|
+ // $parentList = [];
|
|
|
+ // foreach ($pageParentUids as $parentUid) {
|
|
|
+ // try {
|
|
|
+ // $parentNetInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])->asArray()->one();
|
|
|
+ // $parentList[] = [
|
|
|
+ // 'USER_ID' => $userId,
|
|
|
+ // 'TOP_DEEP' => $userNetInfo['TOP_DEEP'],
|
|
|
+ // 'PARENT_UID' => $parentUid,
|
|
|
+ // 'PARENT_DEEP' => $parentNetInfo['TOP_DEEP'],
|
|
|
+ // // 'LOCATION_TAG' => $userNetInfo['LOCATION_TAG'],
|
|
|
+ // // 'PARENT_LOCATION_TAG' => $parentNetInfo['LOCATION_TAG'],
|
|
|
+ // 'LOCATION' => substr($userNetInfo['LOCATION_TAG'], strlen($parentNetInfo['LOCATION_TAG']), 1),
|
|
|
+ // ];
|
|
|
+ // } catch (Exception $e) {
|
|
|
+ // $file_name = date('Y-m-d', time()).'_usernetwork.php_error.log';
|
|
|
+ // file_put_contents($file_name, var_export([
|
|
|
+ // '$parentUid' => $parentUid,
|
|
|
+ // '$userId' => $userId,
|
|
|
+ // 'error' => $e->getMessage()
|
|
|
+ // ],true), FILE_APPEND);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // unset($parentUid, $parentNetInfo);
|
|
|
+ // }
|
|
|
+ // unset($pageParentUids, $userNetInfo);
|
|
|
+
|
|
|
+ // return $parentList;
|
|
|
+ // }
|
|
|
+
|
|
|
/**
|
|
|
* 分页获取上级会员结算库
|
|
|
* @param $userId
|