ChangeNetController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liang
  5. * Date: 2020/2/18
  6. * Time: 6:36 PM
  7. */
  8. namespace console\controllers;
  9. use common\models\UserInfo;
  10. use common\models\UserNetwork;
  11. use common\models\UserRelation;
  12. class ChangeNetController extends BaseController {
  13. //顶点ID列表
  14. const TOP_ID_LIST = [
  15. '670B84FD7C216D4EE055736AECE8644D',
  16. '80552117701578757',
  17. ];
  18. //LOCATION缓存键
  19. const CHANGE_NET_CACHE_LOCATION_TAG_KEY = 'changeNet:cache:locationTag:key';
  20. /**
  21. * 清空缓存
  22. */
  23. public function actionClearCache() {
  24. $this->_clearTagCache();
  25. }
  26. /**
  27. * 加入安置数据
  28. * @param int $startPage
  29. * @throws \yii\db\Exception
  30. */
  31. public function actionAddNetwork($startPage=1) {
  32. $allCount = UserInfo::find()->where('1=1')->orderBy(' NETWORK_DEEP ASC,USER_ID ASC')->count('ID');
  33. $limit = 1000;
  34. $pageCount = ceil($allCount/$limit);
  35. unset($allCount);
  36. for($page=$startPage;$page<=$pageCount;$page++) {
  37. $userList = UserInfo::find()->where('1=1')->orderBy('NETWORK_DEEP ASC,USER_ID ASC')->limit($limit)->offset(($page-1)*$limit)->asArray()->all();
  38. $batchInsertList = [];
  39. foreach ($userList as $everyUser) {
  40. $netParentList = \Yii::$app->db->createCommand("SELECT * FROM AR_USER_NETWORK WHERE USER_ID=:USER_ID ORDER BY PARENT_UID ASC")->bindValues([':USER_ID'=>$everyUser['USER_ID']])->queryAll();
  41. if( !$netParentList ) {
  42. echo '不存在parentList :' . $everyUser['USER_ID'] . PHP_EOL;
  43. continue;
  44. }
  45. $parentUidsStr = '';
  46. $topUid = '';
  47. $location = 0;
  48. foreach ($netParentList as $netParentData) {
  49. if( !$netParentData['PARENT_UID'] ) continue;
  50. if( !$topUid ) {
  51. $topUid = $netParentData['TOP_UID'];
  52. }
  53. if( $netParentData['PARENT_UID'] === $everyUser['CON_UID'] ) {
  54. $location = $netParentData['LOCATION'];
  55. }
  56. $parentUidsStr .= $netParentData['PARENT_UID'] . ',';
  57. unset($netParentData);
  58. }
  59. if( $location === 0 && !in_array($everyUser['USER_ID'], self::TOP_ID_LIST) ) {
  60. echo 'location Error :' . $everyUser['USER_ID'] . PHP_EOL;
  61. continue;
  62. }
  63. $parentUidsStr = rtrim($parentUidsStr, ',');
  64. unset($netParentList);
  65. $parentLocationTag = $this->_getLocationTagCache($everyUser['CON_UID']);
  66. if( $parentLocationTag ) {
  67. $locationTag = $parentLocationTag . $location;
  68. }else {
  69. $locationTag = '1';
  70. }
  71. if( $locationTag ==='1' && !in_array($everyUser['USER_ID'], self::TOP_ID_LIST) ) {
  72. echo 'locationTag错误 :' . $everyUser['USER_ID'] . PHP_EOL;
  73. continue;
  74. }
  75. $this->_addLocationTagCache($everyUser['USER_ID'], $locationTag);
  76. if( $topUid === '' && in_array($everyUser['USER_ID'], self::TOP_ID_LIST) ) {
  77. $topUid = $everyUser['USER_ID'];
  78. }
  79. $batchInsertList[] = [
  80. 'USER_ID' => $everyUser['USER_ID'],
  81. 'PARENT_UID' => $everyUser['CON_UID'],
  82. 'LOCATION_TAG' => $locationTag,
  83. 'RELATIVE_LOCATION' => $location,
  84. 'TOP_UID' => $topUid,
  85. 'TOP_DEEP' => $everyUser['NETWORK_DEEP'],
  86. 'PARENT_UIDS' => $parentUidsStr,
  87. 'CREATED_AT' => $everyUser['CREATED_AT'],
  88. // 'UPDATED_AT' => '',
  89. ];
  90. unset($everyUser, $location, $locationTag, $topUid, $parentUidsStr);
  91. }
  92. unset($userList);
  93. UserNetwork::batchInsert($batchInsertList);
  94. unset($batchInsertList);
  95. echo sprintf("导入新网体表数据,总页数【%s】,当前页数:【%s】写入成功".PHP_EOL, $pageCount, $page);
  96. }
  97. }
  98. /**
  99. * 加入推荐数据
  100. * @param int $startPage
  101. * @throws \yii\db\Exception
  102. */
  103. public function actionAddRelation($startPage=1) {
  104. $allCount = UserInfo::find()->where('1=1')->orderBy(' RELATION_DEEP ASC,USER_ID ASC')->count('ID');
  105. $limit = 1000;
  106. $pageCount = ceil($allCount/$limit);
  107. unset($allCount);
  108. for($page=$startPage;$page<=$pageCount;$page++) {
  109. $userList = UserInfo::find()->where('1=1')->orderBy('RELATION_DEEP ASC,USER_ID ASC')->limit($limit)->offset(($page-1)*$limit)->asArray()->all();
  110. $batchInsertList = [];
  111. foreach ($userList as $everyUser) {
  112. $relationParentList = \Yii::$app->db->createCommand("SELECT * FROM AR_USER_RELATION WHERE USER_ID=:USER_ID ORDER BY PARENT_UID ASC")->bindValues([':USER_ID'=>$everyUser['USER_ID']])->queryAll();
  113. if( !$relationParentList ) {
  114. echo '不存在parentList :' . $everyUser['USER_ID'] . PHP_EOL;
  115. continue;
  116. }
  117. $parentUidsStr = '';
  118. $topUid = '';
  119. foreach ($relationParentList as $relationParentData) {
  120. if( !$relationParentData['PARENT_UID'] ) continue;
  121. if( !$topUid ) {
  122. $topUid = $relationParentData['TOP_UID'];
  123. }
  124. $parentUidsStr .= $relationParentData['PARENT_UID'] . ',';
  125. unset($relationParentData);
  126. }
  127. $parentUidsStr = rtrim($parentUidsStr, ',');
  128. unset($relationParentList);
  129. if( $topUid === '' && in_array($everyUser['USER_ID'], self::TOP_ID_LIST) ) {
  130. $topUid = $everyUser['USER_ID'];
  131. }
  132. $batchInsertList[] = [
  133. 'USER_ID' => $everyUser['USER_ID'],
  134. 'PARENT_UID' => $everyUser['REC_UID'],
  135. 'TOP_UID' => $topUid,
  136. 'TOP_DEEP' => $everyUser['RELATION_DEEP'],
  137. 'PARENT_UIDS' => $parentUidsStr,
  138. 'CREATED_AT' => $everyUser['CREATED_AT'],
  139. // 'UPDATED_AT' => '',
  140. ];
  141. unset($everyUser, $topUid, $parentUidsStr);
  142. }
  143. unset($userList);
  144. UserRelation::batchInsert($batchInsertList);
  145. unset($batchInsertList);
  146. echo sprintf("导入新推荐表数据,总页数【%s】,当前页数:【%s】写入成功".PHP_EOL, $pageCount, $page);
  147. }
  148. }
  149. /**
  150. * 添加网体标记Cache数据
  151. * @param $userId
  152. * @param $locationTag
  153. * @return mixed
  154. */
  155. private function _addLocationTagCache($userId, $locationTag) {
  156. return \Yii::$app->redis->hSet(self::CHANGE_NET_CACHE_LOCATION_TAG_KEY, $userId, $locationTag);
  157. }
  158. /**
  159. * 获取网体标记Cache数据
  160. * @param $userId
  161. * @return mixed
  162. */
  163. private function _getLocationTagCache($userId) {
  164. return \Yii::$app->redis->hGet(self::CHANGE_NET_CACHE_LOCATION_TAG_KEY, $userId);
  165. }
  166. /**
  167. * 清空网体标记Cache数据
  168. * @return mixed
  169. */
  170. private function _clearTagCache() {
  171. return \Yii::$app->redis->del(self::CHANGE_NET_CACHE_LOCATION_TAG_KEY);
  172. }
  173. }