<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateVersionTables extends Migrator
{
    public function change()
    {
        // 添加系统配置
        $this->addSystemConfig();
        
        // 软件版本表
        if (!$this->hasTable('version')) {
            $this->table('version', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci'])
                ->addColumn('version_no', 'string', ['limit' => 50, 'null' => false, 'comment' => '版本号'])
                ->addColumn('version_name', 'string', ['limit' => 100, 'null' => false, 'comment' => '版本名称'])
                ->addColumn('download_url', 'string', ['limit' => 500, 'null' => false, 'comment' => '下载地址'])
                ->addColumn('is_force', 'boolean', ['null' => false, 'default' => 0, 'comment' => '是否强制更新'])
                ->addColumn('min_version', 'string', ['limit' => 50, 'null' => true, 'comment' => '最低要求版本'])
                ->addColumn('platform', 'string', ['limit' => 20, 'null' => false, 'default' => 'all', 'comment' => '平台(windows,mac,linux)'])
                ->addColumn('status', 'boolean', ['null' => false, 'default' => 1, 'comment' => '状态(0禁用,1启用)'])
                ->addColumn('description', 'text', ['null' => true, 'comment' => '版本描述'])
                ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
                ->addIndex('version_no', ['name' => 'idx_version_no'])
                ->create();
        }

        // 版本更新日志表
        if (!$this->hasTable('version_log')) {
            $this->table('version_log', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci'])
                ->addColumn('version_id', 'integer', ['limit' => 20, 'null' => false, 'comment' => '版本ID'])
                ->addColumn('type', 'string', ['limit' => 20, 'null' => false, 'default' => 'feature', 'comment' => '更新类型(feature,fix,optimize)'])
                ->addColumn('content', 'text', ['null' => false, 'comment' => '更新内容'])
                ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
                ->addIndex('version_id', ['name' => 'idx_version_id'])
                ->create();
        }

        // 版本控制表
        if (!$this->hasTable('version_control')) {
            $this->table('version_control', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci'])
                ->addColumn('version_id', 'integer', ['limit' => 20, 'null' => false, 'comment' => '版本ID'])
                ->addColumn('control_type', 'string', ['limit' => 20, 'null' => false, 'comment' => '控制类型(white_list,black_list)'])
                ->addColumn('target_id', 'integer', ['limit' => 20, 'null' => false, 'comment' => '目标ID'])
                ->addColumn('target_type', 'string', ['limit' => 20, 'null' => false, 'comment' => '目标类型(user,agent,device)'])
                ->addColumn('expire_time', 'datetime', ['null' => true, 'comment' => '过期时间'])
                ->addColumn('create_time', 'datetime', ['null' => true, 'comment' => '创建时间'])
                ->addColumn('update_time', 'datetime', ['null' => true, 'comment' => '更新时间'])
                ->addIndex(['version_id', 'target_id', 'target_type'], ['name' => 'idx_version_target'])
                ->create();
        }
    }
    
    /**
     * 添加系统配置
     */
    protected function addSystemConfig()
    {
        // 检查是否存在配置
        $exists = \think\facade\Db::name('system_config')
            ->where('type', 'base')
            ->where('name', 'site_domain')
            ->find();
            
        if (!$exists) {
            // 添加站点域名配置
            \think\facade\Db::name('system_config')->insert([
                'type' => 'base',
                'name' => 'site_domain',
                'value' => '',
                'title' => '站点域名',
                'description' => '请设置站点访问域名，例如: https://www.example.com',
                'create_at' => date('Y-m-d H:i:s'),
            ]);
        }
    }
} 