Explorar el Código

relation modify parent_uid

root hace 3 años
padre
commit
5ec1e1b295
Se han modificado 1 ficheros con 72 adiciones y 16 borrados
  1. 72 16
      common/models/UserRelation.php

+ 72 - 16
common/models/UserRelation.php

@@ -72,7 +72,6 @@ class UserRelation extends \common\components\ActiveRecord
         return intval(UserInfo::find()->where('REC_UID=:REC_UID', [':REC_UID'=>$userId])->count());
     }
 
-
     /**
      * 分页获取上级会员
      * @param $userId
@@ -84,26 +83,41 @@ class UserRelation extends \common\components\ActiveRecord
      * @return array|\yii\db\ActiveRecord[]
      */
     public static function getParentsWithOffset($userId, $offset, $limit, $orderBy = 'DESC', $isSlaves = false, $db = 'db'){
-        $userRelationInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'PARENT_UIDS'])->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
-        if( !$userRelationInfo ) return [];
-
-        $parentUidsStr = $userRelationInfo['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_RELATION_NEW WHERE USER_ID = _id) AS PARENT_UID,
+                @l := @l + 1 AS lvl
+                FROM
+                (SELECT @r := $userId, @l := 0) vars, AR_USER_RELATION_NEW AS h
+                WHERE @r <> 0 
+            ) t1
+        JOIN AR_USER_RELATION_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) {
-            $parentRelationInfo = static::find($isSlaves, $db)->select(['TOP_DEEP'])->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])->asArray()->one();
+            $parentRelationInfo = static::find($isSlaves, $db)
+            ->select(['TOP_DEEP'])
+            ->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])
+            ->asArray()
+            ->one();
             $parentList[] = [
                 'USER_ID' => $userId,
-                'TOP_DEEP' => $userRelationInfo['TOP_DEEP'],
+                'TOP_DEEP' => $topDeep,
                 'PARENT_UID' => $parentUid,
                 'PARENT_DEEP' => $parentRelationInfo['TOP_DEEP'],
             ];
@@ -115,6 +129,48 @@ class UserRelation 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'){
+    //     $userRelationInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'PARENT_UIDS'])->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
+    //     if( !$userRelationInfo ) return [];
+
+    //     $parentUidsStr = $userRelationInfo['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) {
+    //         $parentRelationInfo = static::find($isSlaves, $db)->select(['TOP_DEEP'])->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])->asArray()->one();
+    //         $parentList[] = [
+    //             'USER_ID' => $userId,
+    //             'TOP_DEEP' => $userRelationInfo['TOP_DEEP'],
+    //             'PARENT_UID' => $parentUid,
+    //             'PARENT_DEEP' => $parentRelationInfo['TOP_DEEP'],
+    //         ];
+
+    //         unset($parentUid, $parentRelationInfo);
+    //     }
+    //     unset($pageParentUids, $userRelationInfo);
+
+    //     return $parentList;
+    // }
+
     /**
      * 分页获取上级会员结算库
      * @param $userId