UserNetwork.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. $allUserIds = array_reverse($allUserIds);
  102. $allkey = array_values($allUserIds);
  103. $allvalue = array_keys($allUserIds);
  104. $resourceData = array_combine($allkey, $allvalue);
  105. if(empty($allUserIds)) return [];
  106. $topDeep = count($allUserIds);
  107. $pageParentUids = array_slice($allUserIds, $offset, $limit);
  108. $parentList = [];
  109. foreach ($pageParentUids as $key => $parentUid) {
  110. try {
  111. // RELATIVE_LOCATION 自己所在位置的节点,即所在父级下的哪个区位
  112. // location字段获取有问题 以前是起始用户的location_tag和此父级的location_tag取一位
  113. // 'LOCATION' => substr($userNetInfo['LOCATION_TAG'], strlen($parentNetInfo['LOCATION_TAG']), 1),
  114. $nowKey = $resourceData[$parentUid];
  115. $nearSon = $nowKey+1;
  116. if ($nearSon == $topDeep) {
  117. $locationInfoUid = $userId;
  118. } else {
  119. $locationInfoUid = $allUserIds[$nearSon];
  120. }
  121. $parentNetInfo = static::find($isSlaves, $db)
  122. ->select(['RELATIVE_LOCATION', 'PARENT_UID'])
  123. ->where('USER_ID=:USER_ID', ['USER_ID'=>$locationInfoUid])
  124. ->asArray()
  125. ->one();
  126. $location = $parentNetInfo['RELATIVE_LOCATION'] > 0 ? $parentNetInfo['RELATIVE_LOCATION'] : 1;
  127. $parentList[] = [
  128. 'USER_ID' => $userId,
  129. 'TOP_DEEP' => $topDeep,
  130. 'PARENT_UID' => $parentUid,
  131. 'LOCATION' => $location,
  132. ];
  133. } catch (Exception $e) {
  134. $file_name = date('Y-m-d', time()).'_usernetwork.php_error.log';
  135. file_put_contents($file_name, var_export([
  136. '$parentUid' => $parentUid,
  137. '$userId' => $userId,
  138. 'error' => $e->getMessage()
  139. ],true), FILE_APPEND);
  140. }
  141. unset($parentUid, $parentNetInfo);
  142. }
  143. // if ($userId == '376748282036228096') {
  144. // var_dump($parentList, $userId);exit;
  145. // }
  146. unset($pageParentUids, $userNetInfo);
  147. return $parentList;
  148. }
  149. // /**
  150. // * 分页获取上级会员
  151. // * @param $userId
  152. // * @param $offset
  153. // * @param $limit
  154. // * @param string $orderBy
  155. // * @param bool $isSlaves
  156. // * @param string $db
  157. // * @return array|\yii\db\ActiveRecord[]
  158. // */
  159. // public static function getParentsWithOffset($userId, $offset, $limit, $orderBy='DESC', $isSlaves = false, $db = 'db'){
  160. // $userNetInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'PARENT_UIDS', 'LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
  161. // if( !$userNetInfo ) return [];
  162. // $parentUidsStr = $userNetInfo['PARENT_UIDS'];
  163. // if( !$parentUidsStr ) return [];
  164. // $parentUidsArr = explode(',', $parentUidsStr);
  165. // unset($parentUidsStr);
  166. // if( $orderBy === 'DESC' ) {
  167. // $parentUidsArr = array_reverse($parentUidsArr);
  168. // }
  169. // if( !$parentUidsArr ) return [];
  170. // $pageParentUids = array_slice($parentUidsArr, $offset, $limit);
  171. // unset($parentUidsArr);
  172. // $parentList = [];
  173. // foreach ($pageParentUids as $parentUid) {
  174. // try {
  175. // $parentNetInfo = static::find($isSlaves, $db)->select(['TOP_DEEP', 'LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$parentUid])->asArray()->one();
  176. // $parentList[] = [
  177. // 'USER_ID' => $userId,
  178. // 'TOP_DEEP' => $userNetInfo['TOP_DEEP'],
  179. // 'PARENT_UID' => $parentUid,
  180. // 'PARENT_DEEP' => $parentNetInfo['TOP_DEEP'],
  181. // // 'LOCATION_TAG' => $userNetInfo['LOCATION_TAG'],
  182. // // 'PARENT_LOCATION_TAG' => $parentNetInfo['LOCATION_TAG'],
  183. // 'LOCATION' => substr($userNetInfo['LOCATION_TAG'], strlen($parentNetInfo['LOCATION_TAG']), 1),
  184. // ];
  185. // } catch (Exception $e) {
  186. // $file_name = date('Y-m-d', time()).'_usernetwork.php_error.log';
  187. // file_put_contents($file_name, var_export([
  188. // '$parentUid' => $parentUid,
  189. // '$userId' => $userId,
  190. // 'error' => $e->getMessage()
  191. // ],true), FILE_APPEND);
  192. // }
  193. // unset($parentUid, $parentNetInfo);
  194. // }
  195. // unset($pageParentUids, $userNetInfo);
  196. // return $parentList;
  197. // }
  198. /**
  199. * 分页获取上级会员结算库
  200. * @param $userId
  201. * @param $callbackFunc
  202. * @param $limit
  203. * @param int $offset
  204. * @param string $orderBy
  205. */
  206. public static function getParentsWithOffsetFromDbCalc($userId, $callbackFunc, $limit, $offset = 0, $orderBy='DESC'){
  207. $allData = self::getParentsWithOffset($userId, $offset, $limit, $orderBy, false, 'dbCalc');
  208. if($allData){
  209. foreach($allData as $data){
  210. $callbackFunc($data);
  211. unset($data);
  212. }
  213. unset($allData);
  214. self::getParentsWithOffsetFromDbCalc($userId, $callbackFunc, $limit, $offset + $limit);
  215. }
  216. }
  217. /**
  218. * 获取上一级父级的会员信息
  219. * @param $userId
  220. * @return array|null
  221. */
  222. public static function getFirstParentUserInfo($userId){
  223. $oneUserInfo = UserInfo::findOneAsArray(['USER_ID'=>$userId]);
  224. if($oneUserInfo['CON_UID']){
  225. return UserInfo::findOneAsArray(['USER_ID'=>$oneUserInfo['CON_UID']]);
  226. } else {
  227. return null;
  228. }
  229. }
  230. /**
  231. * 获取指定深度的子会员
  232. * @param $userId
  233. * @param $deep
  234. * @param string $orderBy
  235. * @return array
  236. */
  237. public static function getChildrenWithDeep($userId, $deep, $orderBy='TOP_DEEP ASC'){
  238. $userNetInfo = static::find()->select(['LOCATION_TAG', 'TOP_DEEP'])->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
  239. $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();
  240. foreach ($childrenNetInfo as $key => $childNetInfo) {
  241. $childrenNetInfo[$key]['LOCATION'] = substr($childNetInfo['LOCATION_TAG'], strlen($userNetInfo['LOCATION_TAG']), 1);
  242. unset($key, $childNetInfo);
  243. }
  244. unset($userNetInfo);
  245. return $childrenNetInfo;
  246. }
  247. /**
  248. * 获取下一层会员的接点信息
  249. * @param $userId
  250. * @return array|\yii\db\ActiveRecord[]
  251. */
  252. public static function getFirstFloorChildren($userId) {
  253. $childrenNetInfo = static::find()->select(['USER_ID', 'RELATIVE_LOCATION', 'LOCATION_TAG', 'TOP_DEEP'])->where('PARENT_UID=:PARENT_UID', ['PARENT_UID'=>$userId])->asArray()->all();
  254. unset($userNetInfo);
  255. return $childrenNetInfo;
  256. }
  257. /**
  258. * 查看所传父级节点会员是不是所传会员的父级
  259. * @param $userId
  260. * @param $parentUserId
  261. * @param null $periodNum
  262. * @return bool
  263. * @throws \yii\db\Exception
  264. */
  265. public static function isParentUser($userId, $parentUserId, $periodNum = null){
  266. $table = self::getTableNameFromPeriod($periodNum);
  267. $db = $table['db'];
  268. $tableName = $table['tableName'];
  269. $data = $db->createCommand("SELECT PARENT_UIDS FROM {$tableName} WHERE USER_ID=:USER_ID")->bindValues([':USER_ID'=>$userId])->queryOne();
  270. if( !$data ) {
  271. unset($table, $db, $tableName, $data);
  272. return false;
  273. }
  274. $parentListArr = explode(',', $data['PARENT_UIDS']);
  275. unset($table, $db, $tableName, $data);
  276. $existStatus = in_array($parentUserId, $parentListArr);
  277. unset($parentListArr);
  278. return $existStatus;
  279. }
  280. /**
  281. * 获取会员相较于父级的区位
  282. * @param $userId
  283. * @param $parentUserId
  284. * @param $periodNum
  285. * @return mixed|null
  286. */
  287. public static function getLocation($userId, $parentUserId, $periodNum = null){
  288. $table = self::getTableNameFromPeriod($periodNum);
  289. $db = $table['db'];
  290. $tableName = $table['tableName'];
  291. $userNetInfo = $db->createCommand("SELECT LOCATION_TAG FROM $tableName WHERE USER_ID=:USER_ID")->bindValues([':USER_ID'=>$userId])->queryOne();
  292. if( !$userNetInfo || !isset($userNetInfo['LOCATION_TAG']) ) return 0;
  293. $parentNetInfo = $db->createCommand("SELECT LOCATION_TAG FROM $tableName WHERE USER_ID=:USER_ID")->bindValues([':USER_ID'=>$userId])->queryOne();
  294. if( !$parentNetInfo || !isset($parentNetInfo['LOCATION_TAG']) ) return 0;
  295. $location = substr($userNetInfo['LOCATION_TAG'], strlen($parentNetInfo['LOCATION_TAG']), 1);
  296. return $location ? $location : 0;
  297. }
  298. /**
  299. * 获取子会员节点带着总深度和循环的子节点层级关系
  300. * @param $userId
  301. * @param $deep
  302. * @param int $loopedDeep
  303. * @param null $periodNum
  304. * @return mixed
  305. * @throws \yii\base\Exception
  306. * @throws \yii\db\Exception
  307. */
  308. public static function getChildrenWithDeepAndLayer($userId, $deep, $loopedDeep = 1, $periodNum=null){
  309. $allData = self::getChildrenFromPeriod($userId, $periodNum);
  310. if($allData){
  311. $decLevelConfig = Cache::getDecLevelConfig();
  312. $empLevelConfig = Cache::getEmpLevelConfig();
  313. $crownLevelConfig = Cache::getStarCrownLevelConfig();
  314. foreach($allData as $key=>$data){
  315. // 获取用户的基本信息
  316. $baseInfo = Info::baseInfo($data['USER_ID'], $periodNum);
  317. $allData[$key] = array_merge($data, [
  318. 'USER_NAME' => $baseInfo['USER_NAME'],
  319. 'TOP_NETWORK_DEEP' => $data['TOP_DEEP'],
  320. 'REAL_NAME' => $baseInfo['REAL_NAME'],
  321. 'DEC_LV_NAME' => $decLevelConfig[$baseInfo['DEC_LV']]['LEVEL_NAME'],
  322. 'EMP_LV_NAME' => isset($empLevelConfig[$baseInfo['EMP_LV']])?$empLevelConfig[$baseInfo['EMP_LV']]['LEVEL_NAME']:'',
  323. 'CROWN_LV_NAME' => isset($crownLevelConfig[$baseInfo['CROWN_LV']])?$crownLevelConfig[$baseInfo['CROWN_LV']]['LEVEL_NAME']:'',
  324. // 'MOBILE' => $baseInfo['MOBILE'],
  325. 'PERIOD_AT' => $baseInfo['PERIOD_AT'],
  326. ]);
  327. // 获取字节点数量
  328. $childNum = self::firstFloorChildNumFromPeriod($data['USER_ID'], $periodNum);
  329. if($childNum > 0 && $loopedDeep < $deep){
  330. $child = self::getChildrenWithDeepAndLayer($data['USER_ID'], $deep, $loopedDeep + 1, $periodNum);
  331. $leaf = false;
  332. $icon = 'el-icon-user-solid';
  333. }
  334. elseif($childNum > 0){
  335. $child = null;
  336. $leaf = false;
  337. $icon = 'el-icon-user-solid';
  338. }
  339. else {
  340. $child = null;
  341. $leaf = true;
  342. $icon = 'el-icon-user';
  343. }
  344. $allData[$key]['children'] = $child;
  345. $allData[$key]['leaf'] = $leaf;
  346. $allData[$key]['icon'] = $icon;
  347. $allData[$key]['isExpanded'] = false;
  348. $allData[$key]['displayNone'] = 'display-none';
  349. $allData[$key]['RELATIVE_LOCATION'] = $data['RELATIVE_LOCATION'] == 1 ? 'L' : 'R';
  350. }
  351. }
  352. return $allData;
  353. }
  354. /**
  355. * 通过期数获取应该查询哪个表和库
  356. * @param $periodNum
  357. * @return array
  358. */
  359. public static function getTableNameFromPeriod($periodNum = null){
  360. $db = self::getDb();
  361. $tableName = self::tableName();
  362. // if($periodNum !== null){
  363. // // 获取当前期数
  364. // $period = Period::instance();
  365. // $nowPeriodNum = $period->getNowPeriodNum();
  366. // if($nowPeriodNum != $periodNum){
  367. // // 从备份库里找到期数对应的网络
  368. // if(ActiveRecord::isExistsTable('{{%USER_NETWORK_'.$periodNum.'}}', 'dbNetPoint')){
  369. // $db = Yii::$app->dbNetPoint;
  370. // $tableName = '{{%USER_NETWORK_'.$periodNum.'}}';
  371. // }
  372. // }
  373. // }
  374. return [
  375. 'db' => $db,
  376. 'tableName' => $tableName,
  377. ];
  378. }
  379. /**
  380. * 获取指定深度的子会员从指定的期数中
  381. * @param $userId
  382. * @param $periodNum
  383. * @return array|\yii\db\DataReader
  384. * @throws \yii\db\Exception
  385. */
  386. public static function getChildrenFromPeriod($userId, $periodNum=null){
  387. $table = self::getTableNameFromPeriod($periodNum);
  388. $db = $table['db'];
  389. $tableName = $table['tableName'];
  390. return $db->createCommand("SELECT USER_ID,TOP_DEEP,RELATIVE_LOCATION FROM {$tableName} WHERE PARENT_UID=:PARENT_UID")->bindValues([':PARENT_UID'=>$userId])->queryAll();
  391. }
  392. /**
  393. * 获取指定层数的这一层的会员以分页的方式
  394. * @param $userId
  395. * @param $deep
  396. * @param null $periodNum
  397. * @param array $params
  398. * @return array
  399. */
  400. public static function getChildrenInDeepFromPeriodWithPage($userId, $deep, $periodNum=null, $params=[]){
  401. $pageSize = method_exists(\Yii::$app->request,'get')?\Yii::$app->request->get('pageSize', \Yii::$app->params['pageSize']):\Yii::$app->params['pageSize'];
  402. if(isset($params['pageSize'])) $pageSize=$params['pageSize'];
  403. $page = null;
  404. if( isset($params['page']) ) $page=$params['page'];
  405. $orderBy = 'TOP_DEEP ASC, ID ASC';
  406. if( isset($params['orderBy']) ) $orderBy = $params['orderBy'];
  407. $table = self::getTableNameFromPeriod($periodNum);
  408. $db = $table['db'];
  409. $tableName = $table['tableName'];
  410. $userNetInfo = $db->createCommand("SELECT LOCATION_TAG,TOP_DEEP FROM {$tableName} WHERE USER_ID=:USER_ID")->bindValues([':USER_ID'=>$userId])->queryOne();
  411. $userDeep = $userNetInfo['TOP_DEEP'];
  412. $userLocationTag = $userNetInfo['LOCATION_TAG'];
  413. $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}";
  414. $totalCount = $db->createCommand($totalCountSql)->bindValues([':LOCATION_TAG'=>$userLocationTag . '%', ':TOP_DEEP'=>$userDeep + $deep])->queryOne();
  415. $count = $totalCount['TOTAL_COUNT'];
  416. $pagination = new Pagination(['totalCount' => $count]);
  417. $pagination->setPageSize($pageSize);
  418. if( $page !== null ) {
  419. $pagination->setPage($page);
  420. }
  421. $offset = $pagination->offset;
  422. $limit = $pagination->limit;
  423. // $end = $offset + $limit;
  424. $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}";
  425. $lists = $db->createCommand($listSql)->bindValues([':LOCATION_TAG'=>$userLocationTag . '%', ':TOP_DEEP'=>$userDeep + $deep])->queryAll();
  426. return [
  427. 'list' => $lists ? $lists : [],
  428. 'pagination' => $pagination,
  429. 'currentPage'=>$pagination->page,
  430. 'totalPages'=>$pagination->pageCount,
  431. 'totalCount' => $pagination->totalCount,
  432. 'pageSize' => $pagination->pageSize,
  433. ];
  434. }
  435. /**
  436. * 获取所有的上级会员带着分页和期数
  437. * @param $userId
  438. * @param null $periodNum
  439. * @param null $deep
  440. * @return array
  441. */
  442. public static function getAllParentFromPeriodWithPage($userId, $periodNum=null, $deep=null){
  443. $pageSize = method_exists(\Yii::$app->request,'get')?\Yii::$app->request->get('pageSize', \Yii::$app->params['pageSize']):\Yii::$app->params['pageSize'];
  444. $table = self::getTableNameFromPeriod($periodNum);
  445. $db = $table['db'];
  446. $tableName = $table['tableName'];
  447. $sql = "SELECT * FROM {$tableName} WHERE USER_ID=:USER_ID";
  448. $oneData = $db->createCommand($sql)->bindValues([':USER_ID'=>$userId])->queryOne();
  449. $userDeep = $oneData['TOP_DEEP'];
  450. $parentUidsStr = $oneData['PARENT_UIDS'] ?? "";
  451. if( !$parentUidsStr ) return [];
  452. $parentUidsArr = explode(',', $parentUidsStr);
  453. $parentUidsFlip = array_reverse($parentUidsArr);
  454. if ($deep != null) {
  455. $parentUidsFlip = array_slice($parentUidsFlip, 0, $deep);
  456. }
  457. $count = count($parentUidsFlip);
  458. $pagination = new Pagination(['totalCount' => $count]);
  459. $pagination->setPageSize($pageSize);
  460. $offset = $pagination->offset;
  461. $limit = $pagination->limit;
  462. $parentUidList = array_slice($parentUidsFlip, $offset, $limit);
  463. $lists = [];
  464. foreach ($parentUidList as $parentUid) {
  465. $sql = "SELECT * FROM {$tableName} WHERE USER_ID=:USER_ID";
  466. $data = $db->createCommand($sql)->bindValues([':USER_ID' => $parentUid])->queryOne();
  467. if( !$data ) continue;
  468. $data['LOCATION'] = substr($oneData['LOCATION_TAG'], strlen($data['LOCATION_TAG']), 1);
  469. $lists[] = $data;
  470. }
  471. return [
  472. 'list' => $lists ? $lists : [],
  473. 'pagination' => $pagination,
  474. 'currentPage' => $pagination->page,
  475. 'totalPages' => $pagination->pageCount,
  476. 'totalCount' => $pagination->totalCount,
  477. 'pageSize' => $pagination->pageSize,
  478. 'listTopDeep' => $userDeep,
  479. ];
  480. }
  481. /**
  482. * 获取一层下级数量从期数
  483. * @param $userId
  484. * @param null $periodNum
  485. * @return int
  486. */
  487. public static function firstFloorChildNumFromPeriod($userId, $periodNum=null){
  488. $table = self::getTableNameFromPeriod($periodNum);
  489. $db = $table['db'];
  490. $tableName = $table['tableName'];
  491. $count = $db->createCommand("SELECT COUNT(ID) AS ID_COUNT FROM {$tableName} WHERE PARENT_UID=:PARENT_UID")->bindValues([':PARENT_UID'=>$userId])->queryOne();
  492. return intval($count['ID_COUNT']);
  493. }
  494. /**
  495. * 从缓存中获取会员的全部父级(主要用于结算时的处理,能够提高效率不去查库)
  496. * @param $userId
  497. * @return array|mixed
  498. */
  499. public static function getAllParentsFromRedis($userId){
  500. $key = Cache::USER_NETWORK_PARENTS;
  501. $data = Yii::$app->redis->hget($key, $userId);
  502. if(!$data){
  503. $data = [];
  504. self::getParentsWithOffsetFromDbCalc($userId, function($oneData) use(&$data){
  505. $data[] = $oneData;
  506. }, 1000);
  507. $data = Json::encode($data);
  508. Yii::$app->redis->hset($key, $userId, $data);
  509. }
  510. return $data ? Json::decode($data) : [];
  511. }
  512. /**
  513. * 判断在某个区位是否存在会员
  514. * @param $userId
  515. * @param $location
  516. * @return bool
  517. */
  518. public static function issetUserInLocation($userId, $location){
  519. $childrenNetList = UserNetwork::find()->select(['LOCATION_TAG'])->where('PARENT_UID=:PARENT_UID', [':PARENT_UID' => $userId])->asArray()->all();
  520. foreach ($childrenNetList as $everyData) {
  521. //取最后一位数
  522. $everyLocation = substr($everyData['LOCATION_TAG'], -1);
  523. if( $everyLocation == $location ) return true;
  524. }
  525. return false;
  526. }
  527. }