setName('teaching:sync') ->setDescription('教研管理人员权限同步工具') ->addArgument('action', Argument::OPTIONAL, 'sync|test|status', 'sync') ->addOption('table-types', 't', Option::VALUE_OPTIONAL, '指定要同步的table_type,多个用逗号分隔,如:30,31,32') ->addOption('dry-run', 'd', Option::VALUE_NONE, '仅测试不实际执行同步') ->setHelp(' 使用示例: php think teaching:sync # 同步所有自动分发模块 php think teaching:sync --table-types=30,31 # 仅同步指定table_type php think teaching:sync --dry-run # 测试模式,不实际执行 php think teaching:sync test # 执行测试 php think teaching:sync status # 查看配置状态 '); } protected function execute(Input $input, Output $output) { $action = $input->getArgument('action'); $tableTypesStr = $input->getOption('table-types'); $dryRun = $input->getOption('dry-run'); $output->writeln("教研管理人员同步工具"); $output->writeln("======================"); switch ($action) { case 'sync': $this->executeSync($input, $output, $tableTypesStr, $dryRun); break; case 'test': $this->executeTest($input, $output); break; case 'status': $this->showStatus($input, $output); break; default: $output->writeln("未知的操作: {$action}"); $output->writeln("支持的操作: sync, test, status"); } } /** * 执行同步操作 */ private function executeSync(Input $input, Output $output, ?string $tableTypesStr, bool $dryRun) { try { $tableTypes = null; if ($tableTypesStr) { $tableTypes = array_map('intval', explode(',', $tableTypesStr)); $output->writeln("指定table_type: " . implode(', ', $tableTypes) . ""); } if ($dryRun) { $output->writeln("【测试模式】仅模拟执行,不实际更新数据"); } $service = new LessonCourseTeachingService(); if ($dryRun) { // 测试模式:仅统计待同步的记录数量 $config = $service->getAllModuleConfigs(); $autoDistributeModules = []; foreach ($config as $key => $moduleConfig) { if ($moduleConfig['auto_distribute'] ?? false) { if ($tableTypes === null || in_array($moduleConfig['table_type'], $tableTypes)) { $autoDistributeModules[] = [ 'name' => $moduleConfig['name'], 'table_type' => $moduleConfig['table_type'] ]; } } } $output->writeln("需要同步的模块:"); foreach ($autoDistributeModules as $module) { $output->writeln(" - {$module['name']} (table_type: {$module['table_type']})"); } $output->writeln("测试完成,实际同步请移除 --dry-run 参数"); return; } // 实际执行同步 $output->writeln("开始执行同步..."); $result = $service->batchUpdatePersonnelPermissions($tableTypes); $output->writeln(""); $output->writeln("同步结果:"); $output->writeln(" 总记录数: {$result['total']}"); $output->writeln(" 成功同步: {$result['success']}"); $output->writeln(" 同步失败: " . ($result['failed'] > 0 ? "{$result['failed']}" : "{$result['failed']}")); if ($result['failed'] > 0 && !empty($result['errors'])) { $output->writeln(""); $output->writeln("失败详情:"); foreach ($result['errors'] as $error) { $output->writeln(" - {$error}"); } } if ($result['success'] > 0) { $output->writeln(""); $output->writeln("✓ 同步完成!"); } } catch (\Exception $e) { $output->writeln(""); $output->writeln("同步失败: {$e->getMessage()}"); $output->writeln("文件: {$e->getFile()}:{$e->getLine()}"); } } /** * 执行测试 */ private function executeTest(Input $input, Output $output) { try { $output->writeln("执行定时任务测试..."); $job = new TeachingPersonnelSync(); $result = $job->doJob(); $output->writeln(""); $output->writeln("测试结果:"); $output->writeln(" {$result}"); $output->writeln(""); $output->writeln("✓ 测试完成!"); } catch (\Exception $e) { $output->writeln(""); $output->writeln("测试失败: {$e->getMessage()}"); $output->writeln("文件: {$e->getFile()}:{$e->getLine()}"); } } /** * 显示配置状态 */ private function showStatus(Input $input, Output $output) { try { $config = config('teaching_management.teaching_management', []); $moduleConfigs = $config['module_configs'] ?? []; $cronConfig = $config['cron_config']['sync_personnel'] ?? []; $output->writeln("配置状态:"); $output->writeln(" 教练部门ID: " . ($config['coach_department_id'] ?? 'N/A') . ""); $output->writeln(" 定时任务启用: " . ($cronConfig['enabled'] ?? false ? '是' : '否') . ""); $output->writeln(" 定时任务调度: " . ($cronConfig['schedule'] ?? 'N/A') . ""); $output->writeln(""); $output->writeln("自动分发模块:"); $autoModules = []; $regularModules = []; foreach ($moduleConfigs as $key => $moduleConfig) { if ($moduleConfig['auto_distribute'] ?? false) { $autoModules[] = $moduleConfig; } else { $regularModules[] = $moduleConfig; } } if (!empty($autoModules)) { foreach ($autoModules as $module) { $output->writeln(" ✓ {$module['name']} (table_type: {$module['table_type']})"); } } else { $output->writeln(" 暂无自动分发模块"); } $output->writeln(""); $output->writeln("普通模块:"); if (!empty($regularModules)) { foreach ($regularModules as $module) { $output->writeln(" - {$module['name']} (table_type: {$module['table_type']})"); } } else { $output->writeln(" 暂无普通模块"); } } catch (\Exception $e) { $output->writeln(""); $output->writeln("获取状态失败: {$e->getMessage()}"); } } }