FixNetController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 FixNetController extends BaseController {
  13. const LIMIT = 1000;
  14. //顶点ID列表
  15. const TOP_ID_LIST = [
  16. '670B84FD7C216D4EE055736AECE8644D',
  17. '80552117701578757',
  18. ];
  19. //PARENT_UIDS缓存键
  20. const FIX_NET_CACHE_NETWORK_PARENT_UIDS_KEY = 'fixNet:cache:network:parentUids:key';
  21. const FIX_NET_CACHE_RELATION_PARENT_UIDS_KEY = 'fixNet:cache:relation:parentUids:key';
  22. const FIX_RELATION_DEEP_DEEP_USER_ID_KEY = 'fixRelationDeepDeepUserIdKey_%d';
  23. /**
  24. * 清空缓存
  25. */
  26. public function actionClearCache() {
  27. $this->_clearNetworkParentUidsCache();
  28. $this->_clearRelationParentUidsCache();
  29. echo '缓存清理成功' . PHP_EOL;
  30. }
  31. /**
  32. * 安置数据
  33. * @param int $startPage
  34. * @throws \yii\db\Exception
  35. */
  36. public function actionNetwork($startPage=1) {
  37. $allCount = UserNetwork::find()->where('1=1')->orderBy('TOP_DEEP ASC,ID ASC')->count('ID');
  38. $limit = self::LIMIT;
  39. $pageCount = ceil($allCount/$limit);
  40. unset($allCount);
  41. for($page=$startPage;$page<=$pageCount;$page++) {
  42. $userList = UserNetwork::find()->where('1=1')->orderBy('TOP_DEEP ASC,ID ASC')->limit($limit)->offset(($page-1)*$limit)->asArray()->all();
  43. foreach ($userList as $everyUser) {
  44. //顶点不处理
  45. if( $everyUser['PARENT_UID'] == 0 ) continue;
  46. $parentUidsCacheStr = $this->_getNetworkParentUidsCache($everyUser['PARENT_UID']);
  47. if( $parentUidsCacheStr ) {
  48. $parentUidsStr = $parentUidsCacheStr . ',' . $everyUser['PARENT_UID'];
  49. }else {
  50. if( $everyUser['TOP_DEEP'] == 1 ) {
  51. $parentUidsStr = $everyUser['PARENT_UID'];
  52. }else {
  53. echo sprintf('parentUids错误 userId:%s,parentUid:%s'.PHP_EOL, $everyUser['USER_ID'], $everyUser['PARENT_UID']);
  54. continue;
  55. }
  56. }
  57. $this->_addNetworkParentUidsCache($everyUser['USER_ID'], $parentUidsStr);
  58. if( $parentUidsStr != $everyUser['PARENT_UIDS'] ) {
  59. UserNetwork::updateAll(['PARENT_UIDS'=>$parentUidsStr], 'ID=:ID', ['ID'=>$everyUser['ID']]);
  60. echo sprintf('parentUids成功 userId:%s,parentUid:%s'.PHP_EOL, $everyUser['USER_ID'], $everyUser['PARENT_UID']);
  61. }
  62. unset($everyUser, $parentUidsCacheStr, $parentUidsStr);
  63. }
  64. unset($userList);
  65. echo sprintf("更新网体表数据,总页数【%s】,当前页数:【%s】更新成功".PHP_EOL, $pageCount, $page);
  66. }
  67. echo '更新网体表数据全部完成' . PHP_EOL;
  68. }
  69. /**
  70. * 跑关系表中所有的深度
  71. * @param int $deep
  72. */
  73. public function actionRelationTopDeep($deep=1) {
  74. if( $deep === 1 ) $this->_cacheOneDeepInit();
  75. //取deep的列表
  76. $status = $this->_calcOneDeepList($deep);
  77. if( $status ) $this->actionRelationTopDeep($deep+1);
  78. }
  79. /**
  80. * 初始化0层的缓存
  81. */
  82. private function _cacheOneDeepInit() {
  83. foreach (self::TOP_ID_LIST as $userId) {
  84. self::_addRelationDeepUserIdCache(0, $userId);
  85. }
  86. }
  87. /**
  88. * 计算某一层深度的列表
  89. * @param $deep
  90. * @param int $offset
  91. * @return bool
  92. */
  93. private function _calcOneDeepList($deep, $offset=0) {
  94. echo sprintf("时间:[%s]推荐深度,当前deep为:【%d】,当前offset为:【%d】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $deep, $offset);
  95. $allData = self::_getRelationDeepList($deep-1, $offset, self::LIMIT);
  96. if( $allData ) {
  97. foreach ($allData as $userId) {
  98. $childrenList = UserRelation::find()->where('PARENT_UID=:PARENT_UID', ['PARENT_UID'=>$userId])->asArray()->all();
  99. foreach ($childrenList as $childData) {
  100. if( $childData['TOP_DEEP'] != $deep ) {
  101. UserRelation::updateAll([
  102. 'TOP_DEEP' => $deep,
  103. ], 'USER_ID=:USER_ID', ['USER_ID'=>$childData['USER_ID']]);
  104. UserInfo::updateAll(['RELATION_DEEP'=>$deep], 'USER_ID=:USER_ID', ['USER_ID'=>$childData['USER_ID']]);
  105. }
  106. if( !in_array($childData['TOP_UID'], self::TOP_ID_LIST) ) {
  107. if( in_array($userId, self::TOP_ID_LIST) ) {
  108. UserRelation::updateAll([
  109. 'TOP_UID' => $userId,
  110. ], 'USER_ID=:USER_ID', ['USER_ID'=>$childData['USER_ID']]);
  111. }else {
  112. $parentData = UserRelation::find()->where('USER_ID=:USER_ID', ['USER_ID'=>$userId])->asArray()->one();
  113. UserRelation::updateAll([
  114. 'TOP_UID' => $parentData['TOP_UID'],
  115. ], 'USER_ID=:USER_ID', ['USER_ID'=>$childData['USER_ID']]);
  116. unset($parentData);
  117. }
  118. }
  119. self::_addRelationDeepUserIdCache($deep, $childData['USER_ID']);
  120. }
  121. }
  122. return $this->_calcOneDeepList($deep, $offset + self::LIMIT);
  123. }
  124. unset($allData);
  125. $this->_clearRelationDeepUserIdCache($deep-1);
  126. if( $offset > 0 ) {
  127. return true;
  128. }else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * 推荐数据
  134. * @param int $startPage
  135. * @throws \yii\db\Exception
  136. */
  137. public function actionRelation($startPage=1) {
  138. $allCount = UserRelation::find()->where('1=1')->orderBy('TOP_DEEP ASC,ID ASC')->count('ID');
  139. $limit = self::LIMIT;
  140. $pageCount = ceil($allCount/$limit);
  141. unset($allCount);
  142. for($page=$startPage;$page<=$pageCount;$page++) {
  143. $userList = UserRelation::find()->where('1=1')->orderBy('TOP_DEEP ASC,ID ASC')->limit($limit)->offset(($page-1)*$limit)->asArray()->all();
  144. foreach ($userList as $everyUser) {
  145. //顶点不处理
  146. if( $everyUser['PARENT_UID'] == 0 ) continue;
  147. $parentUidsCacheStr = $this->_getRelationParentUidsCache($everyUser['PARENT_UID']);
  148. if( $parentUidsCacheStr ) {
  149. $parentUidsStr = $parentUidsCacheStr . ',' . $everyUser['PARENT_UID'];
  150. }else {
  151. if( $everyUser['TOP_DEEP'] == 1 ) {
  152. $parentUidsStr = $everyUser['PARENT_UID'];
  153. }else {
  154. echo sprintf('parentUids错误 userId:%s,parentUid:%s'.PHP_EOL, $everyUser['USER_ID'], $everyUser['PARENT_UID']);
  155. continue;
  156. }
  157. }
  158. $this->_addRelationParentUidsCache($everyUser['USER_ID'], $parentUidsStr);
  159. if( $parentUidsStr != $everyUser['PARENT_UIDS'] ) {
  160. UserRelation::updateAll(['PARENT_UIDS'=>$parentUidsStr], 'ID=:ID', ['ID'=>$everyUser['ID']]);
  161. echo sprintf('parentUids成功 userId:%s,parentUid:%s'.PHP_EOL, $everyUser['USER_ID'], $everyUser['PARENT_UID']);
  162. }
  163. unset($everyUser, $parentUidsCacheStr, $parentUidsStr);
  164. }
  165. unset($userList);
  166. echo sprintf("更新推荐表数据,总页数【%s】,当前页数:【%s】更新成功".PHP_EOL, $pageCount, $page);
  167. }
  168. echo '更新推荐表数据全部完成' . PHP_EOL;
  169. }
  170. /**
  171. * 添加parent_uids Cache数据
  172. * @param $userId
  173. * @param $parentUidsStr
  174. * @return mixed
  175. */
  176. private function _addNetworkParentUidsCache($userId, $parentUidsStr) {
  177. return \Yii::$app->redis->hSet(self::FIX_NET_CACHE_NETWORK_PARENT_UIDS_KEY, $userId, $parentUidsStr);
  178. }
  179. /**
  180. * 获取parent_uids Cache数据
  181. * @param $userId
  182. * @return mixed
  183. */
  184. private function _getNetworkParentUidsCache($userId) {
  185. return \Yii::$app->redis->hGet(self::FIX_NET_CACHE_NETWORK_PARENT_UIDS_KEY, $userId);
  186. }
  187. /**
  188. * 清空parent_uids Cache数据
  189. * @return mixed
  190. */
  191. private function _clearNetworkParentUidsCache() {
  192. return \Yii::$app->redis->del(self::FIX_NET_CACHE_NETWORK_PARENT_UIDS_KEY);
  193. }
  194. /**
  195. * 添加parent_uids Cache数据
  196. * @param $userId
  197. * @param $parentUidsStr
  198. * @return mixed
  199. */
  200. private function _addRelationParentUidsCache($userId, $parentUidsStr) {
  201. return \Yii::$app->redis->hSet(self::FIX_NET_CACHE_RELATION_PARENT_UIDS_KEY, $userId, $parentUidsStr);
  202. }
  203. /**
  204. * 获取parent_uids Cache数据
  205. * @param $userId
  206. * @return mixed
  207. */
  208. private function _getRelationParentUidsCache($userId) {
  209. return \Yii::$app->redis->hGet(self::FIX_NET_CACHE_RELATION_PARENT_UIDS_KEY, $userId);
  210. }
  211. /**
  212. * 清空parent_uids Cache数据
  213. * @return mixed
  214. */
  215. private function _clearRelationParentUidsCache() {
  216. return \Yii::$app->redis->del(self::FIX_NET_CACHE_RELATION_PARENT_UIDS_KEY);
  217. }
  218. private function _clearRelationDeepUserIdCache($deep) {
  219. $key = sprintf(self::FIX_RELATION_DEEP_DEEP_USER_ID_KEY, $deep);
  220. return \Yii::$app->redis->del($key);
  221. }
  222. private function _addRelationDeepUserIdCache($deep, $userId) {
  223. $key = sprintf(self::FIX_RELATION_DEEP_DEEP_USER_ID_KEY, $deep);
  224. return \Yii::$app->redis->rpush($key, $userId);
  225. }
  226. private function _getRelationDeepList($deep, $offset = 0, $limit = 1000) {
  227. $key = sprintf(self::FIX_RELATION_DEEP_DEEP_USER_ID_KEY, $deep);
  228. return \Yii::$app->redis->lrange($key, $offset, ($offset + $limit - 1));
  229. }
  230. }