<?php
require 'vendor/autoload.php'; // 引入 Composer 的自动加载文件

use inter\gen\DbStruct2ClassProcess;
use inter\gen\ProcessConfig;
use inter\mysqlStruct\Struct;
use inter\storage\mysql\Config;
use inter\storage\mysql\DB;
use Smarty\Smarty;

$argv = $_SERVER['argv'];

$database = 'new_energy';
//$database = 'bms_alarm';
//$database = 'business';

if (!empty($argv[1])) {
    $database = $argv[1];
}

$type = "query";
if (!empty($argv[2])) {
    $type = $argv[2];
}


$dbConfig = new Config('101.37.90.21', "", 'root', 'QAZwsx!@#456', $database);
$db = new DB($dbConfig);
$dbProcess = new Struct($db);
$dbProcess->SelectDatabase($database); //->AddUseTable("dh_admin_commission_account");
$dbStruct = $dbProcess->GetDatabase();

$config = new ProcessConfig();

$config->savePath = "gen";
$config->isHasGetFunc = true;
$config->isHasSetFunc = true;

if ($type == "record") {
    // record
    $tpl = "record.tpl";
    $config->fileNameSuffix = "Record";
    $config->SetNamespace('common/table/' . $database . '/record');
    $config->addUse("Yii");
    $config->addUse("common/table/NewEnergyDBRecord");
    $config->extends[] = "NewEnergyDBRecord";
} else if ($type == "query") {
    // query
    $tpl = "query.tpl";
    $config->SetNamespace('common/table/' . $database . '/query');
    $config->fileNameSuffix = "Query";
    $config->addUse("common/table/BaseQuery");
    $config->extends[] = "BaseQuery";
} else if ($type == "gset") {
    // get set
    $tpl = "setGet.tpl";
    $config->fileNameSuffix = "Trait";
    $config->SetNamespace('common/table/' . $database . '/getter_setter');
} else {
    echo "不支持的类型";
    return;
}

$opt = new DbStruct2ClassProcess($config, $dbStruct);
$files = $opt->GetFiles();


$smarty = new Smarty(); // 创建 Smarty 对象实例

foreach ($files as $file) {
    // 配置 Smarty
    $smarty->setTemplateDir('TPL') // 设置模板目录
        ->setCompileDir('gen/templates_c'); // 设置编译目录

    // 分配变量到模板
    $smarty->assign('file', $file);
    $smarty->assign("database", $database);

    // 渲染模板
    //$content = $smarty->fetch('template.tpl');
    $content = $smarty->fetch($tpl);

    // 保存内容到文件
    $filePath = $file->GetPath() . '/' . $file->GetFileName();
    file_put_contents($filePath, $content);
}
