UserNetwork.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. namespace common\models;
  3. use common\components\ActiveRecord;
  4. use common\helpers\Cache;
  5. use common\helpers\NetPoint;
  6. use common\helpers\user\Info;
  7. use common\helpers\user\Perf;
  8. use Exception;
  9. use Yii;
  10. use yii\data\Pagination;
  11. use yii\helpers\Json;
  12. /**
  13. * This is the model class for table "{{%USER_NETWORK}}".
  14. *
  15. * @property string $ID
  16. * @property string $USER_ID 会员ID
  17. * @property string $PARENT_UID 相对上级会员ID
  18. * @property int $LOCATION_TAG 网体标记
  19. * @property int $RELATIVE_LOCATION 相对上级会员区位
  20. * @property string $TOP_UID 顶端会员ID
  21. * @property string $PARENT_UIDS 所有上级ID
  22. * @property int $TOP_DEEP 距离顶端会员深度
  23. * @property int $CREATED_AT 创建时间
  24. * @property int $UPDATED_AT 更新时间
  25. */
  26. class UserNetwork extends \common\components\ActiveRecord
  27. {
  28. /**
  29. * @inheritdoc
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%USER_NETWORK_NEW}}';
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['USER_ID', 'PARENT_UID', 'LOCATION', 'TOP_UID', 'CREATED_AT'], 'required'],
  42. [['PERIOD_NUM', 'RELATIVE_LOCATION', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
  43. [['ID', 'USER_ID', 'PARENT_UID', 'TOP_UID'], 'string', 'max' => 32],
  44. [['ID'], 'unique'],
  45. ];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'ID' => 'ID',
  54. 'USER_ID' => '会员ID',
  55. 'PARENT_UID' => '相对上级会员ID',
  56. 'LOCATION_TAG' => '网体标记',
  57. 'RELATIVE_LOCATION' => '相对父级的区位',
  58. 'TOP_UID' => '顶端会员ID',
  59. 'PARENT_UIDS' => '所有上级ID',
  60. 'CREATED_AT' => '创建时间',
  61. 'UPDATED_AT' => '更新时间',
  62. ];
  63. }
  64. /**
  65. * 获取一层下级数量
  66. * @param $userId
  67. * @return int|string
  68. */
  69. public static function firstFloorChildNum($userId){
  70. return intval(UserInfo::find()->where('CON_UID=:CON_UID AND DELETED=0', [':CON_UID'=>$userId])->count());
  71. }
  72. /**
  73. * 使用父级parent_uid,分页获取上级会员
  74. * @param $userId
  75. * @param $offset
  76. * @param $limit
  77. * @param string $orderBy
  78. * @param bool $isSlaves
  79. * @param string $db
  80. * @return array|\yii\db\ActiveRecord[]
  81. */
  82. public static function getParentsWithOffset($userId, $offset, $limit, $orderBy='DESC', $isSlaves = false, $db = 'db'){
  83. $sql = "SELECT t2.USER_ID
  84. FROM
  85. (
  86. SELECT
  87. @r AS _id,
  88. (SELECT @r := PARENT_UID FROM AR_USER_NETWORK_NEW WHERE USER_ID = _id) AS PARENT_UID,
  89. @l := @l + 1 AS lvl
  90. FROM
  91. (SELECT @r := '".$userId."', @l := 0) vars, AR_USER_NETWORK_NEW AS h
  92. WHERE @r <> 0
  93. ) t1
  94. JOIN AR_USER_NETWORK_NEW t2
  95. ON t1._id = t2.USER_ID AND t1._id != '".$userId."';";
  96. $parentUidsArr = \Yii::$app->db->createCommand($sql)->queryAll();
  97. if (empty($parentUidsArr)) {
  98. return [];
  99. }
  100. $allUserIds = array_column($parentUidsArr, 'USER_ID');
  101. if ($orderBy === 'ASC') {
  102. $allUserIds = array_reverse($allUserIds);
  103. }
  104. if(empty($allUserIds)) return [];
  105. $topDeep = count($allUserIds);
  106. $pageParentUids = array_slice($allUserIds, $offset, $limit);
  107. unset($allUserIds);
  108. $parentList = [];
  109. foreach ($pageParentUids as $parentUid) {
  110. try {
  111. $parentNetInfo = static::find($isSlaves, $db)
  112. ->select(['RELATIVE_LOCATION', 'PARENT_UID'])
  113. ->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])
  114. ->asArray()
  115. ->one();
  116. $location = $parentNetInfo['RELATIVE_LOCATION'];
  117. $parentList[] = [
  118. 'USER_ID' => $userId,
  119. 'TOP_DEEP' => $topDeep,
  120. 'PARENT_UID' => $parentUid,
  121. 'LOCATION' => $location,
  122. ];
  123. } catch (Exception $e) {
  124. $file_name = date('Y-m-d', time()).'_usernetwork.php_error.log';
  125. file_put_contents($file_name, var_export([
  126. '$parentUid' => $parentUid,
  127. '$userId' => $userId,
  128. 'error' => $e->getMessage()
  129. ],true), FILE_APPEND);
  130. }
  131. unset($parentUid, $parentNetInfo);
  132. }
  133. unset($pageParentUids, $userNetInfo);
  134. return $parentList;
  135. }
  136. // /**
  137. // * 分页获取上级会员
  138. // * @param $userId
  139. // * @param $offset
  140. // * @param $limit
  141. // * @param string $orderBy
  142. // * @param bool $isSlaves
  143. // * @param string $db
  144. // * @return array|\yii\db\ActiveRecord[]
  145. // */
  146. // public static function getParentsWithOffset($userId, $offset, $limit, $orderBy='DESC', $isSlaves = false, $db = 'db'){
  147. // $userNetInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'PARENT_UIDS', 'LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
  148. // if( !$userNetInfo ) return [];
  149. // $parentUidsStr = $userNetInfo['PARENT_UIDS'];
  150. // if( !$parentUidsStr ) return [];
  151. // $parentUidsArr = explode(',', $parentUidsStr);
  152. // unset($parentUidsStr);
  153. // if( $orderBy === 'DESC' ) {
  154. // $parentUidsArr = array_reverse($parentUidsArr);
  155. // }
  156. // if( !$parentUidsArr ) return [];
  157. // $pageParentUids = array_slice($parentUidsArr, $offset, $limit);
  158. // unset($parentUidsArr);
  159. // $parentList = [];
  160. // foreach ($pageParentUids as $parentUid) {
  161. // try {
  162. // $parentNetInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])->asArray()->one();
  163. // $parentList[] = [
  164. // 'USER_ID' => $userId,
  165. // 'TOP_DEEP' => $userNetInfo['TOP_DEEP'],
  166. // 'PARENT_UID' => $parentUid,
  167. // 'PARENT_DEEP' => $parentNetInfo['TOP_DEEP'],
  168. // // 'LOCATION_TAG' => $userNetInfo['LOCATION_TAG'],
  169. // // 'PARENT_LOCATION_TAG' => $parentNetInfo['LOCATION_TAG'],
  170. // 'LOCATION' => substr($userNetInfo['LOCATION_TAG'], strlen($parentNetInfo['LOCATION_TAG']), 1),
  171. // ];
  172. // } catch (Exception $e) {
  173. // $file_name = date('Y-m-d', time()).'_usernetwork.php_error.log';
  174. // file_put_contents($file_name, var_export([
  175. // '$parentUid' => $parentUid,
  176. // '$userId' => $userId,
  177. // 'error' => $e->getMessage()
  178. // ],true), FILE_APPEND);
  179. // }
  180. // unset($parentUid, $parentNetInfo);
  181. // }
  182. // unset($pageParentUids, $userNetInfo);
  183. // return $parentList;
  184. // }
  185. /**
  186. * 分页获取上级会员结算库
  187. * @param $userId
  188. * @param $callbackFunc
  189. * @param $limit
  190. * @param int $offset
  191. * @param string $orderBy
  192. */
  193. public static function getParentsWithOffsetFromDbCalc($userId, $callbackFunc, $limit, $offset = 0, $orderBy='DESC'){
  194. $allData = self::getParentsWithOffset($userId, $offset, $limit, $orderBy, false, 'dbCalc');
  195. if($allData){
  196. foreach($allData as $data){
  197. $callbackFunc($data);
  198. unset($data);
  199. }
  200. unset($allData);
  201. self::getParentsWithOffsetFromDbCalc($userId, $callbackFunc, $limit, $offset + $limit);
  202. }
  203. }
  204. /**
  205. * 获取上一级父级的会员信息
  206. * @param $userId
  207. * @return array|null
  208. */
  209. public static function getFirstParentUserInfo($userId){
  210. $oneUserInfo = UserInfo::findOneAsArray(['USER_ID'=>$userId]);
  211. if($oneUserInfo['CON_UID']){
  212. return UserInfo::findOneAsArray(['USER_ID'=>$oneUserInfo['CON_UID']]);
  213. } else {
  214. return null;
  215. }
  216. }
  217. /**
  218. * 获取指定深度的子会员
  219. * @param $userId
  220. * @param $deep
  221. * @param string $orderBy
  222. * @return array
  223. */
  224. public static function getChildrenWithDeep($userId, $deep, $orderBy='TOP_DEEP ASC'){
  225. $userNetInfo = static::find()->select(['LOCATION_TAG', 'TOP_DEEP'])->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
  226. $childrenNetInfo = static::find()->select(['USER_ID', 'LOCATION_TAG', 'TOP_DEEP'])->where('LOCATION_TAG LIKE :LOCATION_TAG AND TOP_DEEP<=:TOP_DEEP', ['LOCATION_TAG'=>$userNetInfo['LOCATION_TAG'] . '%', 'TOP_DEEP'=>$userNetInfo['TOP_DEEP'] + $deep])->orderBy($orderBy)->asArray()->all();
  227. foreach ($childrenNetInfo as $key => $childNetInfo) {
  228. $childrenNetInfo[$key]['LOCATION'] = substr($childNetInfo['LOCATION_TAG'], strlen($userNetInfo['LOCATION_TAG']), 1);
  229. unset($key, $childNetInfo);
  230. }
  231. unset($userNetInfo);
  232. return $childrenNetInfo;
  233. }
  234. /**
  235. * 获取下一层会员的接点信息
  236. * @param $userId
  237. * @return array|\yii\db\ActiveRecord[]
  238. */
  239. public static function getFirstFloorChildren($userId) {
  240. $childrenNetInfo = static::find()->select(['USER_ID', 'RELATIVE_LOCATION', 'LOCATION_TAG', 'TOP_DEEP'])->where('PARENT_UID=:PARENT_UID', ['PARENT_UID'=>$userId])->asArray()->all();
  241. unset($userNetInfo);
  242. return $childrenNetInfo;
  243. }
  244. /**
  245. * 查看所传父级节点会员是不是所传会员的父级
  246. * @param $userId
  247. * @param $parentUserId
  248. * @param null $periodNum
  249. * @return bool
  250. * @throws \yii\db\Exception
  251. */
  252. public static function isParentUser($userId, $parentUserId, $periodNum = null){
  253. $table = self::getTableNameFromPeriod($periodNum);
  254. $db = $table['db'];
  255. $tableName = $table['tableName'];
  256. $data = $db->createCommand("SELECT PARENT_UIDS FROM {$tableName} WHERE USER_ID=:USER_ID")->bindValues([':USER_ID'=>$userId])->queryOne();
  257. if( !$data ) {
  258. unset($table, $db, $tableName, $data);
  259. return false;
  260. }
  261. $parentListArr = explode(',', $data['PARENT_UIDS']);
  262. unset($table, $db, $tableName, $data);
  263. $existStatus = in_array($parentUserId, $parentListArr);
  264. unset($parentListArr);
  265. return $existStatus;
  266. }
  267. /**
  268. * 获取会员相较于父级的区位
  269. * @param $userId
  270. * @param $parentUserId
  271. * @param $periodNum
  272. * @return mixed|null
  273. */
  274. public static function getLocation($userId, $parentUserId, $periodNum = null){
  275. $table = self::getTableNameFromPeriod($periodNum);
  276. $db = $table['db'];
  277. $tableName = $table['tableName'];
  278. $userNetInfo = $db->createCommand("SELECT LOCATION_TAG FROM $tableName WHERE USER_ID=:USER_ID")->bindValues([':USER_ID'=>$userId])->queryOne();
  279. if( !$userNetInfo || !isset($userNetInfo['LOCATION_TAG']) ) return 0;
  280. $parentNetInfo = $db->createCommand("SELECT LOCATION_TAG FROM $tableName WHERE USER_ID=:USER_ID")->bindValues([':USER_ID'=>$userId])->queryOne();
  281. if( !$parentNetInfo || !isset($parentNetInfo['LOCATION_TAG']) ) return 0;
  282. $location = substr($userNetInfo['LOCATION_TAG'], strlen($parentNetInfo['LOCATION_TAG']), 1);
  283. return $location ? $location : 0;
  284. }
  285. /**
  286. * 获取子会员节点带着总深度和循环的子节点层级关系
  287. * @param $userId
  288. * @param $deep
  289. * @param int $loopedDeep
  290. * @param null $periodNum
  291. * @return mixed
  292. * @throws \yii\base\Exception
  293. * @throws \yii\db\Exception
  294. */
  295. public static function getChildrenWithDeepAndLayer($userId, $deep, $loopedDeep = 1, $periodNum=null){
  296. $allData = self::getChildrenFromPeriod($userId, $periodNum);
  297. if($allData){
  298. $decLevelConfig = Cache::getDecLevelConfig();
  299. $empLevelConfig = Cache::getEmpLevelConfig();
  300. $crownLevelConfig = Cache::getStarCrownLevelConfig();
  301. foreach($allData as $key=>$data){
  302. // 获取用户的基本信息
  303. $baseInfo = Info::baseInfo($data['USER_ID'], $periodNum);
  304. $allData[$key] = array_merge($data, [
  305. 'USER_NAME' => $baseInfo['USER_NAME'],
  306. 'TOP_NETWORK_DEEP' => $data['TOP_DEEP'],
  307. 'REAL_NAME' => $baseInfo['REAL_NAME'],
  308. 'DEC_LV_NAME' => $decLevelConfig[$baseInfo['DEC_LV']]['LEVEL_NAME'],
  309. 'EMP_LV_NAME' => isset($empLevelConfig[$baseInfo['EMP_LV']])?$empLevelConfig[$baseInfo['EMP_LV']]['LEVEL_NAME']:'',
  310. 'CROWN_LV_NAME' => isset($crownLevelConfig[$baseInfo['CROWN_LV']])?$crownLevelConfig[$baseInfo['CROWN_LV']]['LEVEL_NAME']:'',
  311. // 'MOBILE' => $baseInfo['MOBILE'],
  312. 'PERIOD_AT' => $baseInfo['PERIOD_AT'],
  313. ]);
  314. // 获取字节点数量
  315. $childNum = self::firstFloorChildNumFromPeriod($data['USER_ID'], $periodNum);
  316. if($childNum > 0 && $loopedDeep < $deep){
  317. $child = self::getChildrenWithDeepAndLayer($data['USER_ID'], $deep, $loopedDeep + 1, $periodNum);
  318. $leaf = false;
  319. $icon = 'el-icon-user-solid';
  320. }
  321. elseif($childNum > 0){
  322. $child = null;
  323. $leaf = false;
  324. $icon = 'el-icon-user-solid';
  325. }
  326. else {
  327. $child = null;
  328. $leaf = true;
  329. $icon = 'el-icon-user';
  330. }
  331. $allData[$key]['children'] = $child;
  332. $allData[$key]['leaf'] = $leaf;
  333. $allData[$key]['icon'] = $icon;
  334. $allData[$key]['isExpanded'] = false;
  335. $allData[$key]['displayNone'] = 'display-none';
  336. $allData[$key]['RELATIVE_LOCATION'] = $data['RELATIVE_LOCATION'] == 1 ? 'L' : 'R';
  337. }
  338. }
  339. return $allData;
  340. }
  341. /**
  342. * 通过期数获取应该查询哪个表和库
  343. * @param $periodNum
  344. * @return array
  345. */
  346. public static function getTableNameFromPeriod($periodNum = null){
  347. $db = self::getDb();
  348. $tableName = self::tableName();
  349. // if($periodNum !== null){
  350. // // 获取当前期数
  351. // $period = Period::instance();
  352. // $nowPeriodNum = $period->getNowPeriodNum();
  353. // if($nowPeriodNum != $periodNum){
  354. // // 从备份库里找到期数对应的网络
  355. // if(ActiveRecord::isExistsTable('{{%USER_NETWORK_'.$periodNum.'}}', 'dbNetPoint')){
  356. // $db = Yii::$app->dbNetPoint;
  357. // $tableName = '{{%USER_NETWORK_'.$periodNum.'}}';
  358. // }
  359. // }
  360. // }
  361. return [
  362. 'db' => $db,
  363. 'tableName' => $tableName,
  364. ];
  365. }
  366. /**
  367. * 获取指定深度的子会员从指定的期数中
  368. * @param $userId
  369. * @param $periodNum
  370. * @return array|\yii\db\DataReader
  371. * @throws \yii\db\Exception
  372. */
  373. public static function getChildrenFromPeriod($userId, $periodNum=null){
  374. $table = self::getTableNameFromPeriod($periodNum);
  375. $db = $table['db'];
  376. $tableName = $table['tableName'];
  377. return $db->createCommand("SELECT USER_ID,TOP_DEEP,RELATIVE_LOCATION FROM {$tableName} WHERE PARENT_UID=:PARENT_UID")->bindValues([':PARENT_UID'=>$userId])->queryAll();
  378. }
  379. /**
  380. * 获取指定层数的这一层的会员以分页的方式
  381. * @param $userId
  382. * @param $deep
  383. * @param null $periodNum
  384. * @param array $params
  385. * @return array
  386. */
  387. public static function getChildrenInDeepFromPeriodWithPage($userId, $deep, $periodNum=null, $params=[]){
  388. $pageSize = method_exists(\Yii::$app->request,'get')?\Yii::$app->request->get('pageSize', \Yii::$app->params['pageSize']):\Yii::$app->params['pageSize'];
  389. if(isset($params['pageSize'])) $pageSize=$params['pageSize'];
  390. $page = null;
  391. if( isset($params['page']) ) $page=$params['page'];
  392. $orderBy = 'TOP_DEEP ASC, ID ASC';
  393. if( isset($params['orderBy']) ) $orderBy = $params['orderBy'];
  394. $table = self::getTableNameFromPeriod($periodNum);
  395. $db = $table['db'];
  396. $tableName = $table['tableName'];
  397. $userNetInfo = $db->createCommand("SELECT LOCATION_TAG,TOP_DEEP FROM {$tableName} WHERE USER_ID=:USER_ID")->bindValues([':USER_ID'=>$userId])->queryOne();
  398. $userDeep = $userNetInfo['TOP_DEEP'];
  399. $userLocationTag = $userNetInfo['LOCATION_TAG'];
  400. $totalCountSql = "SELECT COUNT(ID) AS TOTAL_COUNT FROM {$tableName} WHERE LOCATION_TAG LIKE :LOCATION_TAG AND TOP_DEEP<=:TOP_DEEP AND INSTR(`PARENT_UIDS`,'{$userId}')>0 ORDER BY {$orderBy}";
  401. $totalCount = $db->createCommand($totalCountSql)->bindValues([':LOCATION_TAG'=>$userLocationTag . '%', ':TOP_DEEP'=>$userDeep + $deep])->queryOne();
  402. $count = $totalCount['TOTAL_COUNT'];
  403. $pagination = new Pagination(['totalCount' => $count]);
  404. $pagination->setPageSize($pageSize);
  405. if( $page !== null ) {
  406. $pagination->setPage($page);
  407. }
  408. $offset = $pagination->offset;
  409. $limit = $pagination->limit;
  410. // $end = $offset + $limit;
  411. $listSql = "SELECT * FROM $tableName WHERE LOCATION_TAG LIKE :LOCATION_TAG AND TOP_DEEP<=:TOP_DEEP AND INSTR(`PARENT_UIDS`,'{$userId}')>0 ORDER BY {$orderBy} LIMIT {$limit} OFFSET {$offset}";
  412. $lists = $db->createCommand($listSql)->bindValues([':LOCATION_TAG'=>$userLocationTag . '%', ':TOP_DEEP'=>$userDeep + $deep])->queryAll();
  413. return [
  414. 'list' => $lists ? $lists : [],
  415. 'pagination' => $pagination,
  416. 'currentPage'=>$pagination->page,
  417. 'totalPages'=>$pagination->pageCount,
  418. 'totalCount' => $pagination->totalCount,
  419. 'pageSize' => $pagination->pageSize,
  420. ];
  421. }
  422. /**
  423. * 获取所有的上级会员带着分页和期数
  424. * @param $userId
  425. * @param null $periodNum
  426. * @param null $deep
  427. * @return array
  428. */
  429. public static function getAllParentFromPeriodWithPage($userId, $periodNum=null, $deep=null){
  430. $pageSize = method_exists(\Yii::$app->request,'get')?\Yii::$app->request->get('pageSize', \Yii::$app->params['pageSize']):\Yii::$app->params['pageSize'];
  431. $table = self::getTableNameFromPeriod($periodNum);
  432. $db = $table['db'];
  433. $tableName = $table['tableName'];
  434. $sql = "SELECT * FROM {$tableName} WHERE USER_ID=:USER_ID";
  435. $oneData = $db->createCommand($sql)->bindValues([':USER_ID'=>$userId])->queryOne();
  436. $userDeep = $oneData['TOP_DEEP'];
  437. $parentUidsStr = $oneData['PARENT_UIDS'] ?? "";
  438. if( !$parentUidsStr ) return [];
  439. $parentUidsArr = explode(',', $parentUidsStr);
  440. $parentUidsFlip = array_reverse($parentUidsArr);
  441. if ($deep != null) {
  442. $parentUidsFlip = array_slice($parentUidsFlip, 0, $deep);
  443. }
  444. $count = count($parentUidsFlip);
  445. $pagination = new Pagination(['totalCount' => $count]);
  446. $pagination->setPageSize($pageSize);
  447. $offset = $pagination->offset;
  448. $limit = $pagination->limit;
  449. $parentUidList = array_slice($parentUidsFlip, $offset, $limit);
  450. $lists = [];
  451. foreach ($parentUidList as $parentUid) {
  452. $sql = "SELECT * FROM {$tableName} WHERE USER_ID=:USER_ID";
  453. $data = $db->createCommand($sql)->bindValues([':USER_ID' => $parentUid])->queryOne();
  454. if( !$data ) continue;
  455. $data['LOCATION'] = substr($oneData['LOCATION_TAG'], strlen($data['LOCATION_TAG']), 1);
  456. $lists[] = $data;
  457. }
  458. return [
  459. 'list' => $lists ? $lists : [],
  460. 'pagination' => $pagination,
  461. 'currentPage' => $pagination->page,
  462. 'totalPages' => $pagination->pageCount,
  463. 'totalCount' => $pagination->totalCount,
  464. 'pageSize' => $pagination->pageSize,
  465. 'listTopDeep' => $userDeep,
  466. ];
  467. }
  468. /**
  469. * 获取一层下级数量从期数
  470. * @param $userId
  471. * @param null $periodNum
  472. * @return int
  473. */
  474. public static function firstFloorChildNumFromPeriod($userId, $periodNum=null){
  475. $table = self::getTableNameFromPeriod($periodNum);
  476. $db = $table['db'];
  477. $tableName = $table['tableName'];
  478. $count = $db->createCommand("SELECT COUNT(ID) AS ID_COUNT FROM {$tableName} WHERE PARENT_UID=:PARENT_UID")->bindValues([':PARENT_UID'=>$userId])->queryOne();
  479. return intval($count['ID_COUNT']);
  480. }
  481. /**
  482. * 从缓存中获取会员的全部父级(主要用于结算时的处理,能够提高效率不去查库)
  483. * @param $userId
  484. * @return array|mixed
  485. */
  486. public static function getAllParentsFromRedis($userId){
  487. $key = Cache::USER_NETWORK_PARENTS;
  488. $data = Yii::$app->redis->hget($key, $userId);
  489. if(!$data){
  490. $data = [];
  491. self::getParentsWithOffsetFromDbCalc($userId, function($oneData) use(&$data){
  492. $data[] = $oneData;
  493. }, 100);
  494. $data = Json::encode($data);
  495. Yii::$app->redis->hset($key, $userId, $data);
  496. }
  497. return $data ? Json::decode($data) : [];
  498. }
  499. /**
  500. * 判断在某个区位是否存在会员
  501. * @param $userId
  502. * @param $location
  503. * @return bool
  504. */
  505. public static function issetUserInLocation($userId, $location){
  506. $childrenNetList = UserNetwork::find()->select(['LOCATION_TAG'])->where('PARENT_UID=:PARENT_UID', [':PARENT_UID' => $userId])->asArray()->all();
  507. foreach ($childrenNetList as $everyData) {
  508. //取最后一位数
  509. $everyLocation = substr($everyData['LOCATION_TAG'], -1);
  510. if( $everyLocation == $location ) return true;
  511. }
  512. return false;
  513. }
  514. }