在 Yii 2.0 中,控制台命令行,添加参数的实现
1、CmcConsoleUser/actionSync,现有实现,参考:https://www.shuijingwanwq.com/2020/03/02/3964/ ,现在需要添加参数:租户ID
2、参数将传递给请求的子命令对应的动作方法。设置参数 $groupId 的默认值为 null
1 | public function actionSync( $groupId = null) |
3、判断 $groupId 是否为空,如果不为空,且在 $httpCmcApiGroupIds 中不存在,则成功退出
1 2 3 4 | /* 判断 $groupId 是否为空,如果不为空,且在 $httpCmcApiGroupIds 中不存在,则成功退出 */ if (! empty ( $groupId ) && !in_array( $groupId , $httpCmcApiGroupIds )) { return ExitCode::OK; } |
4、基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds,打印排序前后的数组
1 2 3 4 5 6 | // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds $sortCmcApiGroupIds = $cmcApiGroupIds ; print_r( $sortCmcApiGroupIds ); ArrayHelper::multisort( $sortCmcApiGroupIds , 'cmc_console_user_last_synced_at' , SORT_ASC); print_r( $sortCmcApiGroupIds ); exit ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | PS E:\wwwroot\pcs-api> ./yii cmc-console-user/sync Array ( [0] => Array ( [group_id] => c10e87f39873512a16727e17f57456a5 [cmc_console_user_last_synced_at] => 1590649259 ) [1] => Array ( [group_id] => 015ce30b116ce86058fa6ab4fea4ac63 [cmc_console_user_last_synced_at] => 0 ) [2] => Array ( [group_id] => 4fd58ceba1fbc537b5402302702131eb [cmc_console_user_last_synced_at] => 0 ) [3] => Array ( [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e [cmc_console_user_last_synced_at] => 0 ) [4] => Array ( [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94 [cmc_console_user_last_synced_at] => 0 ) [5] => Array ( [group_id] => b28312e46044da2682e540d1fb838c67 [cmc_console_user_last_synced_at] => 0 ) ) Array ( [0] => Array ( [group_id] => 015ce30b116ce86058fa6ab4fea4ac63 [cmc_console_user_last_synced_at] => 0 ) [1] => Array ( [group_id] => 4fd58ceba1fbc537b5402302702131eb [cmc_console_user_last_synced_at] => 0 ) [2] => Array ( [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e [cmc_console_user_last_synced_at] => 0 ) [3] => Array ( [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94 [cmc_console_user_last_synced_at] => 0 ) [4] => Array ( [group_id] => b28312e46044da2682e540d1fb838c67 [cmc_console_user_last_synced_at] => 0 ) [5] => Array ( [group_id] => c10e87f39873512a16727e17f57456a5 [cmc_console_user_last_synced_at] => 1590649259 ) ) |
5、现在需要基于参数 $groupId 的值,来同步此租户下的用户,即需要让此租户排在第一位。才能够满足需求。先销毁 $sortCmcApiGroupIds 中的 $groupId,再插入至开头。打印排序前后的数组。符合预期。如图1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds $sortCmcApiGroupIds = $cmcApiGroupIds ; print_r( $sortCmcApiGroupIds ); ArrayHelper::multisort( $sortCmcApiGroupIds , 'cmc_console_user_last_synced_at' , SORT_ASC); print_r( $sortCmcApiGroupIds ); /* 判断 $groupId 是否为空,如果不为空 */ if (! empty ( $groupId )) { // 销毁 $sortCmcApiGroupIds 中的 $groupId foreach ( $sortCmcApiGroupIds as $sortCmcApiGroupIdKey => $sortCmcApiGroupId ) { if ( $sortCmcApiGroupId [ 'group_id' ] == $groupId ) { unset( $sortCmcApiGroupIds [ $sortCmcApiGroupIdKey ]); break ; } } print_r( $sortCmcApiGroupIds ); // 将 $groupId 插入至 $sortCmcApiGroupIds 的开头 array_unshift ( $sortCmcApiGroupIds , [ 'group_id' => $groupId , 'cmc_console_user_last_synced_at' => 0, //上次同步时间 ]); } print_r( $sortCmcApiGroupIds ); exit ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | PS E:\wwwroot\pcs-api> ./yii cmc-console-user/sync b28312e46044da2682e540d1fb838c67 Array ( [0] => Array ( [group_id] => c10e87f39873512a16727e17f57456a5 [cmc_console_user_last_synced_at] => 1590649259 ) [1] => Array ( [group_id] => 015ce30b116ce86058fa6ab4fea4ac63 [cmc_console_user_last_synced_at] => 0 ) [2] => Array ( [group_id] => 4fd58ceba1fbc537b5402302702131eb [cmc_console_user_last_synced_at] => 0 ) [3] => Array ( [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e [cmc_console_user_last_synced_at] => 0 ) [4] => Array ( [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94 [cmc_console_user_last_synced_at] => 0 ) [5] => Array ( [group_id] => b28312e46044da2682e540d1fb838c67 [cmc_console_user_last_synced_at] => 0 ) ) Array ( [0] => Array ( [group_id] => 015ce30b116ce86058fa6ab4fea4ac63 [cmc_console_user_last_synced_at] => 0 ) [1] => Array ( [group_id] => 4fd58ceba1fbc537b5402302702131eb [cmc_console_user_last_synced_at] => 0 ) [2] => Array ( [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e [cmc_console_user_last_synced_at] => 0 ) [3] => Array ( [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94 [cmc_console_user_last_synced_at] => 0 ) [4] => Array ( [group_id] => b28312e46044da2682e540d1fb838c67 [cmc_console_user_last_synced_at] => 0 ) [5] => Array ( [group_id] => c10e87f39873512a16727e17f57456a5 [cmc_console_user_last_synced_at] => 1590649259 ) ) Array ( [0] => Array ( [group_id] => 015ce30b116ce86058fa6ab4fea4ac63 [cmc_console_user_last_synced_at] => 0 ) [1] => Array ( [group_id] => 4fd58ceba1fbc537b5402302702131eb [cmc_console_user_last_synced_at] => 0 ) [2] => Array ( [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e [cmc_console_user_last_synced_at] => 0 ) [3] => Array ( [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94 [cmc_console_user_last_synced_at] => 0 ) [5] => Array ( [group_id] => c10e87f39873512a16727e17f57456a5 [cmc_console_user_last_synced_at] => 1590649259 ) ) Array ( [0] => Array ( [group_id] => b28312e46044da2682e540d1fb838c67 [cmc_console_user_last_synced_at] => 0 ) [1] => Array ( [group_id] => 015ce30b116ce86058fa6ab4fea4ac63 [cmc_console_user_last_synced_at] => 0 ) [2] => Array ( [group_id] => 4fd58ceba1fbc537b5402302702131eb [cmc_console_user_last_synced_at] => 0 ) [3] => Array ( [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e [cmc_console_user_last_synced_at] => 0 ) [4] => Array ( [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94 [cmc_console_user_last_synced_at] => 0 ) [5] => Array ( [group_id] => c10e87f39873512a16727e17f57456a5 [cmc_console_user_last_synced_at] => 1590649259 ) ) |
6、最终的代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | // 设置同步标识的缓存键 $redisCache = Yii:: $app ->redisCache; // HTTP 请求,获取开通有效服务的租户ID列表 $httpCmcApiGroupIds = CmcApiGroupService::httpGetGroupIds(); /* 判断 $httpCmcApiGroupIds 是否为空,如果为空,则成功退出 */ if ( empty ( $httpCmcApiGroupIds )) { // 延缓执行 60 * 60 秒 // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyYesSleepTime']); return ExitCode::OK; } /* 判断 $groupId 是否为空,如果不为空,且在 $httpCmcApiGroupIds 中不存在,则成功退出 */ if (! empty ( $groupId ) && !in_array( $groupId , $httpCmcApiGroupIds )) { return ExitCode::OK; } // 设置租户ID列表的缓存键 $redisCacheGroupIdsKey = 'cmc_api_group_ids' ; // 从缓存中取回租户ID列表 $redisCacheGroupIdsData = $redisCache [ $redisCacheGroupIdsKey ]; // 是否设置租户ID列表的缓存,默认:否 $isSetRedisCacheGroupIds = false; if ( $redisCacheGroupIdsData === false) { $cmcApiGroupIds = []; foreach ( $httpCmcApiGroupIds as $httpCmcApiGroupId ) { $cmcApiGroupIds [] = [ 'group_id' => $httpCmcApiGroupId , 'cmc_console_user_last_synced_at' => 0, //上次同步时间 ]; } // 是否设置租户ID列表的缓存:是 $isSetRedisCacheGroupIds = true; } else { // 获取 group_id 值列表 $redisCacheGroupIds = ArrayHelper::getColumn( $redisCacheGroupIdsData , 'group_id' ); $cmcApiGroupIds = $redisCacheGroupIdsData ; /* 删除出现在 $cmcApiGroupIds 中但是未出现在 $httpCmcApiGroupIds 中的租户 */ foreach ( $cmcApiGroupIds as $cmcApiGroupIdKey => $cmcApiGroupId ) { if (!in_array( $cmcApiGroupId [ 'group_id' ], $httpCmcApiGroupIds )) { unset( $cmcApiGroupIds [ $cmcApiGroupIdKey ]); // 是否设置租户ID列表的缓存:是 $isSetRedisCacheGroupIds = true; } } foreach ( $httpCmcApiGroupIds as $httpCmcApiGroupId ) { if (!in_array( $httpCmcApiGroupId , $redisCacheGroupIds )) { $cmcApiGroupIds [] = [ 'group_id' => $httpCmcApiGroupId , 'cmc_console_user_last_synced_at' => 0, //上次同步时间 ]; // 是否设置租户ID列表的缓存:是 $isSetRedisCacheGroupIds = true; } } } // 判断是否设置租户ID列表的缓存 if ( $isSetRedisCacheGroupIds ) { // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留 $redisCache ->set( $redisCacheGroupIdsKey , $cmcApiGroupIds ); } // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds $sortCmcApiGroupIds = $cmcApiGroupIds ; ArrayHelper::multisort( $sortCmcApiGroupIds , 'cmc_console_user_last_synced_at' , SORT_ASC); /* 判断 $groupId 是否为空,如果不为空 */ if (! empty ( $groupId )) { // 销毁 $sortCmcApiGroupIds 中的 $groupId foreach ( $sortCmcApiGroupIds as $sortCmcApiGroupIdKey => $sortCmcApiGroupId ) { if ( $sortCmcApiGroupId [ 'group_id' ] == $groupId ) { unset( $sortCmcApiGroupIds [ $sortCmcApiGroupIdKey ]); break ; } } // 将 $groupId 插入至 $sortCmcApiGroupIds 的开头 array_unshift ( $sortCmcApiGroupIds , [ 'group_id' => $groupId , 'cmc_console_user_last_synced_at' => 0, //上次同步时间 ]); } |
近期评论