UserNetwork.php 22 KB

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