在 Yii 2.0 中,控制台命令行,添加参数的实现

1、CmcConsoleUser/actionSync,现有实现,参考:https://www.shuijingwanwq.com/2020/03/02/3964/ ,现在需要添加参数:租户ID

2、参数将传递给请求的子命令对应的动作方法。设置参数 $groupId 的默认值为 null

public function actionSync($groupId = null)

3、判断 $groupId 是否为空,如果不为空,且在 $httpCmcApiGroupIds 中不存在,则成功退出

        /* 判断 $groupId 是否为空,如果不为空,且在 $httpCmcApiGroupIds 中不存在,则成功退出 */
        if (!empty($groupId) && !in_array($groupId, $httpCmcApiGroupIds)) {
            return ExitCode::OK;
        }

4、基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds,打印排序前后的数组

        // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds
        $sortCmcApiGroupIds = $cmcApiGroupIds;
        print_r($sortCmcApiGroupIds);
        ArrayHelper::multisort($sortCmcApiGroupIds, 'cmc_console_user_last_synced_at', SORT_ASC);
        print_r($sortCmcApiGroupIds);
        exit;
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

现在需要基于参数 $groupId 的值,来同步此租户下的用户,即需要让此租户排在第一位。才能够满足需求。先销毁 $sortCmcApiGroupIds 中的 $groupId,再插入至开头。打印排序前后的数组。符合预期

图1

        // 基于上次同步时间顺序排列,赋值给:$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;
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、最终的代码实现

        // 设置同步标识的缓存键
        $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, //上次同步时间
            ]);
        }

 

原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/181518.html

(0)
上一篇 2021年11月1日
下一篇 2021年11月1日

相关推荐

发表回复

登录后才能评论