Browse Source

1feature/EK-5822

ryan 2 weeks ago
parent
commit
6ed57e22af

+ 1 - 0
frontendApi/config/urlManagerRules.php

@@ -112,6 +112,7 @@ return [
             'POST pay-password' => 'pay-password',
             'GET,POST,PUT id-card' => 'id-card',
             'GET rec-user' => 'rec-user',
+            'GET network-stats' => 'network-stats',
             'GET bind' => 'bind',
             'GET,POST bind-edit/<id>' => 'bind-edit',
             'GET,POST dec' => 'dec',

+ 46 - 0
frontendApi/modules/v1/controllers/UserController.php

@@ -36,6 +36,8 @@ use common\models\forms\UserBindForm;
 use common\models\forms\UserForm;
 use common\models\Instalment;
 use common\models\OpenBank;
+use common\models\OrderDec;
+use common\models\OriginDecPv;
 use common\models\Period;
 use common\models\ReceiveAddress;
 use common\models\Region;
@@ -146,6 +148,50 @@ class UserController extends BaseController {
         return static::notice($allData);
     }
 
+    /**
+     * 当前用户的安置网络下线统计
+     * @return mixed
+     */
+    public function actionNetworkStats() {
+        $userId = Yii::$app->user->id;
+        $networkInfo = UserNetwork::find()
+            ->select(['LOCATION_TAG', 'TOP_DEEP'])
+            ->where(['USER_ID' => $userId])
+            ->asArray()
+            ->one();
+
+        $descendants = [];
+        if ($networkInfo) {
+            $descendants = UserNetwork::find()
+                ->alias('N')
+                ->select(['N.USER_ID', 'N.PARENT_UID', 'N.TOP_DEEP'])
+                ->innerJoin(User::tableName() . ' U', 'U.ID = N.USER_ID')
+                ->where('N.LOCATION_TAG LIKE :LOCATION_TAG', [':LOCATION_TAG' => $networkInfo['LOCATION_TAG'] . '%'])
+                ->andWhere(['>', 'N.TOP_DEEP', $networkInfo['TOP_DEEP']])
+                ->andWhere(['U.DELETED' => 0])
+                ->asArray()
+                ->all();
+        }
+
+        $userIds = array_column($descendants, 'USER_ID');
+        $pcv = 0;
+        if ($userIds) {
+            $pcv += (float) DecOrder::find()->where(['TO_USER_ID' => $userIds, 'IS_DEL' => 0])->sum('DEC_PV');
+            $pcv += (float) OrderDec::find()->where(['USER_ID' => $userIds, 'IS_DELETE' => 0])->sum('PAY_PV');
+            $pcv += (float) OriginDecPv::find()->where(['USER_ID' => $userIds])->sum('DEC_PV');
+        }
+
+        $maxDeep = $descendants ? max(array_column($descendants, 'TOP_DEEP')) : $networkInfo['TOP_DEEP'] ?? 0;
+        return static::notice([
+            'DOWNLINE_COUNT' => count($descendants),
+            'DIRECT_DOWNLINE_COUNT' => count(array_filter($descendants, function ($downline) use ($userId) {
+                return $downline['PARENT_UID'] === $userId;
+            })),
+            'MAX_DOWNLINE_DEEP' => max(0, (int) $maxDeep - (int) ($networkInfo['TOP_DEEP'] ?? 0)),
+            'DOWNLINE_PCV' => $pcv,
+        ]);
+    }
+
     /**
      * 上传身份证
      * @return mixed