智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

214 lines
8.6 KiB

<?php
// +----------------------------------------------------------------------
// | 教研管理人员同步命令行工具
// +----------------------------------------------------------------------
// | 提供命令行方式执行教研人员权限同步
// +----------------------------------------------------------------------
namespace app\command;
use app\job\schedule\TeachingPersonnelSync;
use app\service\admin\lesson_course_teaching\LessonCourseTeachingService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use think\console\input\Option;
/**
* 教研管理人员同步命令
* Class TeachingSyncCommand
* @package app\command
*/
class TeachingSyncCommand extends Command
{
protected function configure()
{
$this->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("<info>教研管理人员同步工具</info>");
$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("<error>未知的操作: {$action}</error>");
$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("<info>指定table_type: " . implode(', ', $tableTypes) . "</info>");
}
if ($dryRun) {
$output->writeln("<comment>【测试模式】仅模拟执行,不实际更新数据</comment>");
}
$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("<info>需要同步的模块:</info>");
foreach ($autoDistributeModules as $module) {
$output->writeln(" - {$module['name']} (table_type: {$module['table_type']})");
}
$output->writeln("<comment>测试完成,实际同步请移除 --dry-run 参数</comment>");
return;
}
// 实际执行同步
$output->writeln("<info>开始执行同步...</info>");
$result = $service->batchUpdatePersonnelPermissions($tableTypes);
$output->writeln("");
$output->writeln("<info>同步结果:</info>");
$output->writeln(" 总记录数: <comment>{$result['total']}</comment>");
$output->writeln(" 成功同步: <info>{$result['success']}</info>");
$output->writeln(" 同步失败: " . ($result['failed'] > 0 ? "<error>{$result['failed']}</error>" : "<info>{$result['failed']}</info>"));
if ($result['failed'] > 0 && !empty($result['errors'])) {
$output->writeln("");
$output->writeln("<error>失败详情:</error>");
foreach ($result['errors'] as $error) {
$output->writeln(" - {$error}");
}
}
if ($result['success'] > 0) {
$output->writeln("");
$output->writeln("<info>✓ 同步完成!</info>");
}
} catch (\Exception $e) {
$output->writeln("");
$output->writeln("<error>同步失败: {$e->getMessage()}</error>");
$output->writeln("<error>文件: {$e->getFile()}:{$e->getLine()}</error>");
}
}
/**
* 执行测试
*/
private function executeTest(Input $input, Output $output)
{
try {
$output->writeln("<info>执行定时任务测试...</info>");
$job = new TeachingPersonnelSync();
$result = $job->doJob();
$output->writeln("");
$output->writeln("<info>测试结果:</info>");
$output->writeln(" {$result}");
$output->writeln("");
$output->writeln("<info>✓ 测试完成!</info>");
} catch (\Exception $e) {
$output->writeln("");
$output->writeln("<error>测试失败: {$e->getMessage()}</error>");
$output->writeln("<error>文件: {$e->getFile()}:{$e->getLine()}</error>");
}
}
/**
* 显示配置状态
*/
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("<info>配置状态:</info>");
$output->writeln(" 教练部门ID: <comment>" . ($config['coach_department_id'] ?? 'N/A') . "</comment>");
$output->writeln(" 定时任务启用: <comment>" . ($cronConfig['enabled'] ?? false ? '是' : '否') . "</comment>");
$output->writeln(" 定时任务调度: <comment>" . ($cronConfig['schedule'] ?? 'N/A') . "</comment>");
$output->writeln("");
$output->writeln("<info>自动分发模块:</info>");
$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(" <comment>暂无自动分发模块</comment>");
}
$output->writeln("");
$output->writeln("<info>普通模块:</info>");
if (!empty($regularModules)) {
foreach ($regularModules as $module) {
$output->writeln(" - {$module['name']} (table_type: {$module['table_type']})");
}
} else {
$output->writeln(" <comment>暂无普通模块</comment>");
}
} catch (\Exception $e) {
$output->writeln("");
$output->writeln("<error>获取状态失败: {$e->getMessage()}</error>");
}
}
}