feat: 发布智能体版

This commit is contained in:
augushong
2026-03-26 20:22:34 +08:00
parent 7ee9e102a5
commit 8cc08bcb8c
138 changed files with 7964 additions and 660 deletions

View File

@@ -2,8 +2,8 @@
namespace base\common\command\tools\db;
use base\common\service\ToolsDbServiceBase;
use think\console\Command;
use app\common\service\tools\DbService;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
@@ -13,6 +13,8 @@ class ToolsDbCountBase extends Command
{
protected function configure()
{
parent::configure();
$this->setName('tools:db:count')
->setDescription('统计表记录数')
->addArgument('table', null, '表名')
@@ -24,12 +26,12 @@ class ToolsDbCountBase extends Command
protected function execute($input, $output)
{
if ($input->getOption('help')) {
$service = new ToolsDbServiceBase();
$service = new DbService();
$service->showHelp('tools:db:count', $output);
return;
}
$service = new ToolsDbServiceBase();
$service = new DbService();
if (!$service->checkDebugMode($output)) {
return;
@@ -61,19 +63,19 @@ class ToolsDbCountBase extends Command
$endTime = microtime(true);
$executionTime = round(($endTime - $startTime) * 1000, 2);
$output->writeln('');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('<info>表名:' . $fullTableName . '</info>');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('');
$output->writeln('<info>记录数:</info>' . $count);
$output->writeln('<info>执行时间:</info>' . $executionTime . 'ms');
$output->writeln('');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('');
$output->newLine();
$output->comment(str_repeat('=', 60));
$output->info('表名:' . $fullTableName);
$output->comment(str_repeat('=', 60));
$output->newLine();
$output->info('记录数:' . $count);
$output->info('执行时间:' . $executionTime . 'ms');
$output->newLine();
$output->comment(str_repeat('=', 60));
$output->newLine();
} catch (\Exception $e) {
$output->error('统计失败:' . $e->getMessage());
return;
}
}
}
}

View File

@@ -2,8 +2,8 @@
namespace base\common\command\tools\db;
use base\common\service\ToolsDbServiceBase;
use think\console\Command;
use app\common\service\tools\DbService;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
@@ -13,6 +13,8 @@ class ToolsDbDescBase extends Command
{
protected function configure()
{
parent::configure();
$this->setName('tools:db:desc')
->setDescription('显示表结构信息')
->addArgument('table', null, '表名')
@@ -24,12 +26,12 @@ class ToolsDbDescBase extends Command
protected function execute($input, $output)
{
if ($input->getOption('help')) {
$service = new ToolsDbServiceBase();
$service = new DbService();
$service->showHelp('tools:db:desc', $output);
return;
}
$service = new ToolsDbServiceBase();
$service = new DbService();
if (!$service->checkDebugMode($output)) {
return;
@@ -48,12 +50,6 @@ class ToolsDbDescBase extends Command
$showIndex = $input->getOption('show-index');
try {
$output->writeln('');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('<info>表结构:' . $fullTableName . '</info>');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('');
$tableComment = '';
try {
$escaped = addcslashes($fullTableName, "\\_%");
@@ -77,9 +73,6 @@ class ToolsDbDescBase extends Command
}
}
$output->writeln('<info>表注释:</info>' . $tableComment);
$output->writeln('');
$columns = Db::connect($connection)->query("SHOW FULL COLUMNS FROM `$fullTableName`");
if (empty($columns)) {
@@ -88,6 +81,45 @@ class ToolsDbDescBase extends Command
return;
}
$columnData = [];
foreach ($columns as $column) {
$columnData[] = [
'name' => $column['Field'],
'type' => $column['Type'],
'null' => $column['Null'] === 'YES',
'key' => $column['Key'],
'default' => $column['Default'] ?? null,
'extra' => $column['Extra'] ?? '',
'comment' => $column['Comment'] ?? '',
];
}
$indexData = [];
if ($showIndex) {
$indexes = Db::connect($connection)->query("SHOW INDEX FROM `$fullTableName`");
if (!empty($indexes)) {
foreach ($indexes as $index) {
$indexData[] = [
'name' => $index['Key_name'],
'column' => $index['Column_name'],
'unique' => $index['Non_unique'] == 0,
'type' => $index['Index_type'],
'sequence' => $index['Seq_in_index'],
];
}
}
}
$output->newLine();
$output->comment(str_repeat('=', 60));
$output->info('表结构:' . $fullTableName);
$output->comment(str_repeat('=', 60));
$output->newLine();
$output->info('表注释:' . $tableComment);
$output->newLine();
$tableData = [];
foreach ($columns as $column) {
$tableData[] = [
@@ -104,18 +136,16 @@ class ToolsDbDescBase extends Command
$service->formatTableOutput($tableData, $output);
if ($showIndex) {
$output->writeln('');
$output->writeln('<comment>' . str_repeat('-', 60) . '</comment>');
$output->writeln('<info>索引信息</info>');
$output->writeln('<comment>' . str_repeat('-', 60) . '</comment>');
$output->writeln('');
$indexes = Db::connect($connection)->query("SHOW INDEX FROM `$fullTableName`");
$output->newLine();
$output->comment(str_repeat('-', 60));
$output->info('索引信息');
$output->comment(str_repeat('-', 60));
$output->newLine();
if (!empty($indexes)) {
$indexData = [];
$indexDataDisplay = [];
foreach ($indexes as $index) {
$indexData[] = [
$indexDataDisplay[] = [
'索引名' => $index['Key_name'],
'列名' => $index['Column_name'],
'唯一' => $index['Non_unique'] == 0 ? '是' : '否',
@@ -123,18 +153,18 @@ class ToolsDbDescBase extends Command
'顺序' => $index['Seq_in_index'],
];
}
$service->formatTableOutput($indexData, $output);
$service->formatTableOutput($indexDataDisplay, $output);
} else {
$output->writeln('无索引');
}
}
$output->writeln('');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('');
$output->newLine();
$output->comment(str_repeat('=', 60));
$output->newLine();
} catch (\Exception $e) {
$output->error('获取表结构失败:' . $e->getMessage());
return;
}
}
}
}

View File

@@ -2,8 +2,8 @@
namespace base\common\command\tools\db;
use base\common\service\ToolsDbServiceBase;
use think\console\Command;
use app\common\service\tools\DbService;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
@@ -13,10 +13,11 @@ class ToolsDbExecuteBase extends Command
{
protected function configure()
{
parent::configure();
$this->setName('tools:db:execute')
->setDescription('执行 SQL 非查询语句INSERT/UPDATE/DELETE')
->addArgument('sql', null, 'SQL 执行语句')
->addOption('force', null, Option::VALUE_NONE, '跳过确认直接执行')
->addOption('transaction', null, Option::VALUE_NONE, '在事务中执行(失败自动回滚)')
->addOption('connection', null, Option::VALUE_OPTIONAL, '指定数据库连接配置')
->addOption('help', 'h', Option::VALUE_NONE, '显示帮助信息');
@@ -25,12 +26,12 @@ class ToolsDbExecuteBase extends Command
protected function execute($input, $output)
{
if ($input->getOption('help')) {
$service = new ToolsDbServiceBase();
$service = new DbService();
$service->showHelp('tools:db:execute', $output);
return;
}
$service = new ToolsDbServiceBase();
$service = new DbService();
if (!$service->checkDebugMode($output)) {
return;
@@ -45,7 +46,6 @@ class ToolsDbExecuteBase extends Command
return;
}
$force = $input->getOption('force');
$useTransaction = $input->getOption('transaction');
if (preg_match('/^\s*SELECT\s+/i', $sql)) {
@@ -53,17 +53,14 @@ class ToolsDbExecuteBase extends Command
return;
}
$output->writeln('');
$output->writeln('<comment>准备执行的 SQL</comment>');
$output->newLine();
$output->comment('准备执行的 SQL');
$output->writeln($sql);
$output->writeln('');
$output->newLine();
if (!$force) {
$confirm = $output->confirm($input, '<question>确定要执行此 SQL 语句吗?</question> ');
if (!$confirm) {
$output->writeln('<comment>操作已取消</comment>');
return;
}
if (!$output->confirm($input, '确定要执行此 SQL 语句吗?', true)) {
$output->comment('已取消。');
return;
}
try {
@@ -85,19 +82,19 @@ class ToolsDbExecuteBase extends Command
$endTime = microtime(true);
$executionTime = round(($endTime - $startTime) * 1000, 2);
$output->writeln('');
$output->writeln('<info>执行成功</info>');
$output->newLine();
$output->info('执行成功');
$output->writeln('影响行数:' . $affectedRows);
$output->writeln('执行时间:' . $executionTime . 'ms');
$output->writeln('');
$output->newLine();
} catch (\Exception $e) {
$output->writeln('');
$output->newLine();
$output->error('执行失败:' . $e->getMessage());
if ($useTransaction) {
$output->writeln('<comment>事务已回滚</comment>');
$output->comment('事务已回滚');
}
$output->writeln('');
$output->newLine();
return;
}
}
}
}

View File

@@ -2,8 +2,8 @@
namespace base\common\command\tools\db;
use base\common\service\ToolsDbServiceBase;
use think\console\Command;
use app\common\service\tools\DbService;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
@@ -14,6 +14,8 @@ class ToolsDbInfoBase extends Command
{
protected function configure()
{
parent::configure();
$this->setName('tools:db:info')
->setDescription('显示数据库连接信息和表列表')
->addOption('with-count', null, Option::VALUE_NONE, '显示每个表的记录数')
@@ -24,12 +26,12 @@ class ToolsDbInfoBase extends Command
protected function execute($input, $output)
{
if ($input->getOption('help')) {
$service = new ToolsDbServiceBase();
$service = new DbService();
$service->showHelp('tools:db:info', $output);
return;
}
$service = new ToolsDbServiceBase();
$service = new DbService();
if (!$service->checkDebugMode($output)) {
return;
@@ -47,11 +49,69 @@ class ToolsDbInfoBase extends Command
return;
}
$output->writeln('');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('<info>数据库连接信息</info>');
$output->writeln('<comment>' . str_repeat('=', 60) . '</comment>');
$output->writeln('');
$tables = Db::connect($connection)->getTables();
$tableList = [];
if (!empty($tables)) {
$prefix = $config['prefix'] ?? '';
foreach ($tables as $table) {
$shortName = $table;
if ($prefix && str_starts_with($table, $prefix)) {
$shortName = substr($table, strlen($prefix));
}
$tableItem = [
'name' => $shortName,
'full_name' => $table
];
if ($withCount) {
$count = Db::connect($connection)->table($table)->count();
$tableItem['count'] = $count;
}
$tableList[] = $tableItem;
}
}
$output->newLine();
$output->comment(str_repeat('=', 60));
$output->info('数据库连接信息');
$output->comment(str_repeat('=', 60));
$output->newLine();
$output->info('连接名称:' . $connection);
$output->info('数据库类型:' . ($config['type'] ?? 'unknown'));
$output->info('主机地址:' . ($config['hostname'] ?? 'unknown'));
$output->info('数据库名:' . ($config['database'] ?? 'unknown'));
$output->info('端口:' . ($config['hostport'] ?? 'unknown'));
$output->info('字符集:' . ($config['charset'] ?? 'unknown'));
$output->info('表前缀:' . ($config['prefix'] ?? ''));
$output->newLine();
$output->comment(str_repeat('-', 60));
$output->info('表列表');
$output->comment(str_repeat('-', 60));
$output->newLine();
if (empty($tables)) {
$output->writeln('无表');
$output->newLine();
return;
}
foreach ($tableList as $tableItem) {
if ($withCount) {
$output->info(str_pad($tableItem['name'], 40) . ($tableItem['count'] ?? 0) . ' 条记录');
} else {
$output->info($tableItem['name']);
}
}
$output->newLine();
$output->comment(str_repeat('=', 60));
$output->newLine();
$output->writeln('<info>连接名称:</info>' . $connection);
$output->writeln('<info>数据库类型:</info>' . ($config['type'] ?? 'unknown'));
$output->writeln('<info>主机地址:</info>' . ($config['hostname'] ?? 'unknown'));
@@ -66,27 +126,17 @@ class ToolsDbInfoBase extends Command
$output->writeln('<comment>' . str_repeat('-', 60) . '</comment>');
$output->writeln('');
$tables = Db::connect($connection)->getTables();
if (empty($tables)) {
$output->writeln('无表');
$output->writeln('');
return;
}
$prefix = $config['prefix'] ?? '';
foreach ($tables as $table) {
$shortName = $table;
if ($prefix && str_starts_with($table, $prefix)) {
$shortName = substr($table, strlen($prefix));
}
foreach ($tableList as $tableItem) {
if ($withCount) {
$count = Db::connect($connection)->table($table)->count();
$output->writeln('<info>' . str_pad($shortName, 40) . '</info>' . $count . ' 条记录');
$output->writeln('<info>' . str_pad($tableItem['name'], 40) . '</info>' . ($tableItem['count'] ?? 0) . ' 条记录');
} else {
$output->writeln('<info>' . $shortName . '</info>');
$output->writeln('<info>' . $tableItem['name'] . '</info>');
}
}
@@ -98,4 +148,4 @@ class ToolsDbInfoBase extends Command
return;
}
}
}
}

View File

@@ -2,8 +2,8 @@
namespace base\common\command\tools\db;
use base\common\service\ToolsDbServiceBase;
use think\console\Command;
use app\common\service\tools\DbService;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
@@ -13,10 +13,11 @@ class ToolsDbQueryBase extends Command
{
protected function configure()
{
parent::configure();
$this->setName('tools:db:query')
->setDescription('执行 SQL 查询语句SELECT并显示结果')
->addArgument('sql', null, 'SQL 查询语句')
->addOption('format', null, Option::VALUE_OPTIONAL, '输出格式可选值table默认、json', 'table')
->addOption('limit', null, Option::VALUE_OPTIONAL, '限制显示行数')
->addOption('connection', null, Option::VALUE_OPTIONAL, '指定数据库连接配置')
->addOption('help', 'h', Option::VALUE_NONE, '显示帮助信息');
@@ -25,12 +26,12 @@ class ToolsDbQueryBase extends Command
protected function execute($input, $output)
{
if ($input->getOption('help')) {
$service = new ToolsDbServiceBase();
$service = new DbService();
$service->showHelp('tools:db:query', $output);
return;
}
$service = new ToolsDbServiceBase();
$service = new DbService();
if (!$service->checkDebugMode($output)) {
return;
@@ -45,7 +46,6 @@ class ToolsDbQueryBase extends Command
return;
}
$format = $input->getOption('format') ?: 'table';
$limit = $input->getOption('limit');
if (!preg_match('/^\s*SELECT\s+/i', $sql)) {
@@ -69,22 +69,16 @@ class ToolsDbQueryBase extends Command
$query = array_slice($query, 0, (int)$limit);
}
$output->writeln('');
if ($format === 'json') {
$service->formatJsonOutput($query, $output);
} else {
$service->formatTableOutput($query, $output);
}
$output->writeln('');
$output->writeln('<info>查询成功</info>');
$output->newLine();
$service->formatTableOutput($query, $output);
$output->newLine();
$output->info('查询成功');
$output->writeln('影响行数:' . count($query));
$output->writeln('执行时间:' . $executionTime . 'ms');
$output->writeln('');
$output->newLine();
} catch (\Exception $e) {
$output->error('查询失败:' . $e->getMessage());
return;
}
}
}
}

View File

@@ -2,8 +2,8 @@
namespace base\common\command\tools\db;
use base\common\service\ToolsDbServiceBase;
use think\console\Command;
use app\common\service\tools\DbService;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
@@ -13,6 +13,8 @@ class ToolsDbTableBase extends Command
{
protected function configure()
{
parent::configure();
$this->setName('tools:db:table')
->setDescription('使用查询构建器操作表')
->addArgument('table', null, '表名')
@@ -28,12 +30,12 @@ class ToolsDbTableBase extends Command
protected function execute($input, $output)
{
if ($input->getOption('help')) {
$service = new ToolsDbServiceBase();
$service = new DbService();
$service->showHelp('tools:db:table', $output);
return;
}
$service = new ToolsDbServiceBase();
$service = new DbService();
if (!$service->checkDebugMode($output)) {
return;
@@ -75,10 +77,11 @@ class ToolsDbTableBase extends Command
if ($count) {
$result = $query->count();
$output->writeln('');
$output->writeln('<info>表名:' . $fullTableName . '</info>');
$output->newLine();
$output->info('表名:' . $fullTableName);
$output->writeln('记录数:' . $result);
$output->writeln('');
$output->newLine();
} else {
if ($limit && is_numeric($limit)) {
$query->limit((int)$limit);
@@ -89,17 +92,17 @@ class ToolsDbTableBase extends Command
$endTime = microtime(true);
$executionTime = round(($endTime - $startTime) * 1000, 2);
$output->writeln('');
$output->writeln('<info>表名:' . $fullTableName . '</info>');
$output->newLine();
$output->info('表名:' . $fullTableName);
$service->formatTableOutput($result, $output);
$output->writeln('');
$output->newLine();
$output->writeln('影响行数:' . count($result));
$output->writeln('执行时间:' . $executionTime . 'ms');
$output->writeln('');
$output->newLine();
}
} catch (\Exception $e) {
$output->error('查询失败:' . $e->getMessage());
return;
}
}
}
}