safeLoad(); $dsn = sprintf( 'mysql:host=%s;port=%s;dbname=%s;charset=%s', $_ENV['DB_HOST'] ?? '127.0.0.1', $_ENV['DB_PORT'] ?? '3306', $_ENV['DB_DATABASE'] ?? '', $_ENV['DB_CHARSET'] ?? 'utf8mb4' ); $pdo = new PDO( $dsn, $_ENV['DB_USERNAME'] ?? '', $_ENV['DB_PASSWORD'] ?? '', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ] ); function ffHasColumn(PDO $pdo, string $table, string $column): bool { $stmt = $pdo->prepare('SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?'); $stmt->execute([$table, $column]); return (int)$stmt->fetchColumn() > 0; } function ffHasIndex(PDO $pdo, string $table, string $index): bool { $stmt = $pdo->prepare('SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND INDEX_NAME = ?'); $stmt->execute([$table, $index]); return (int)$stmt->fetchColumn() > 0; } $pdo->exec(<<<'SQL' CREATE TABLE IF NOT EXISTS internal_transfer_tag_batches ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, batch_no VARCHAR(64) NOT NULL, total_count INT NOT NULL DEFAULT 0, status VARCHAR(32) NOT NULL DEFAULT 'active', remark VARCHAR(500) NOT NULL DEFAULT '', created_by BIGINT UNSIGNED NULL DEFAULT NULL, created_by_name VARCHAR(64) NOT NULL DEFAULT '', created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uk_internal_transfer_tag_batches_no (batch_no), KEY idx_internal_transfer_tag_batches_status (status) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='内部流转挂牌批次' SQL); $pdo->exec(<<<'SQL' CREATE TABLE IF NOT EXISTS internal_transfer_tags ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, batch_id BIGINT UNSIGNED NULL DEFAULT NULL, tag_no VARCHAR(80) NOT NULL, status VARCHAR(32) NOT NULL DEFAULT 'active', bind_status VARCHAR(32) NOT NULL DEFAULT 'free', current_order_id BIGINT UNSIGNED NULL DEFAULT NULL, current_flow_id BIGINT UNSIGNED NULL DEFAULT NULL, current_stage VARCHAR(64) NOT NULL DEFAULT 'idle', current_location VARCHAR(64) NOT NULL DEFAULT 'warehouse', bound_by BIGINT UNSIGNED NULL DEFAULT NULL, bound_by_name VARCHAR(64) NOT NULL DEFAULT '', bound_at DATETIME NULL DEFAULT NULL, released_by BIGINT UNSIGNED NULL DEFAULT NULL, released_by_name VARCHAR(64) NOT NULL DEFAULT '', released_at DATETIME NULL DEFAULT NULL, created_by BIGINT UNSIGNED NULL DEFAULT NULL, created_by_name VARCHAR(64) NOT NULL DEFAULT '', created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uk_internal_transfer_tags_no (tag_no), KEY idx_internal_transfer_tags_batch_id (batch_id), KEY idx_internal_transfer_tags_bind_status (bind_status), KEY idx_internal_transfer_tags_current_order_id (current_order_id), KEY idx_internal_transfer_tags_current_stage (current_stage) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='内部流转挂牌' SQL); $pdo->exec(<<<'SQL' CREATE TABLE IF NOT EXISTS order_transfer_flows ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, order_id BIGINT UNSIGNED NOT NULL, internal_tag_id BIGINT UNSIGNED NOT NULL, internal_tag_no VARCHAR(80) NOT NULL, service_provider VARCHAR(32) NOT NULL DEFAULT 'anxinyan', flow_status VARCHAR(32) NOT NULL DEFAULT 'active', current_stage VARCHAR(64) NOT NULL DEFAULT 'warehouse_received', current_location VARCHAR(64) NOT NULL DEFAULT 'warehouse_pending_inspection', inbound_by BIGINT UNSIGNED NULL DEFAULT NULL, inbound_by_name VARCHAR(64) NOT NULL DEFAULT '', inbound_at DATETIME NULL DEFAULT NULL, zhongjian_outbound_by BIGINT UNSIGNED NULL DEFAULT NULL, zhongjian_outbound_by_name VARCHAR(64) NOT NULL DEFAULT '', zhongjian_outbound_at DATETIME NULL DEFAULT NULL, zhongjian_inbound_by BIGINT UNSIGNED NULL DEFAULT NULL, zhongjian_inbound_by_name VARCHAR(64) NOT NULL DEFAULT '', zhongjian_inbound_at DATETIME NULL DEFAULT NULL, appraisal_started_by BIGINT UNSIGNED NULL DEFAULT NULL, appraisal_started_by_name VARCHAR(64) NOT NULL DEFAULT '', appraisal_started_at DATETIME NULL DEFAULT NULL, report_published_by BIGINT UNSIGNED NULL DEFAULT NULL, report_published_by_name VARCHAR(64) NOT NULL DEFAULT '', report_published_at DATETIME NULL DEFAULT NULL, return_confirmed_by BIGINT UNSIGNED NULL DEFAULT NULL, return_confirmed_by_name VARCHAR(64) NOT NULL DEFAULT '', return_confirmed_at DATETIME NULL DEFAULT NULL, return_shipped_by BIGINT UNSIGNED NULL DEFAULT NULL, return_shipped_by_name VARCHAR(64) NOT NULL DEFAULT '', return_shipped_at DATETIME NULL DEFAULT NULL, ended_at DATETIME NULL DEFAULT NULL, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY idx_order_transfer_flows_order_id (order_id), KEY idx_order_transfer_flows_tag_id (internal_tag_id), KEY idx_order_transfer_flows_tag_no (internal_tag_no), KEY idx_order_transfer_flows_status_stage (flow_status, current_stage) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单内部流转' SQL); $pdo->exec(<<<'SQL' CREATE TABLE IF NOT EXISTS order_transfer_flow_logs ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, flow_id BIGINT UNSIGNED NOT NULL, order_id BIGINT UNSIGNED NOT NULL, internal_tag_id BIGINT UNSIGNED NOT NULL, internal_tag_no VARCHAR(80) NOT NULL, action_code VARCHAR(64) NOT NULL, action_text VARCHAR(128) NOT NULL DEFAULT '', before_stage VARCHAR(64) NOT NULL DEFAULT '', before_location VARCHAR(64) NOT NULL DEFAULT '', after_stage VARCHAR(64) NOT NULL DEFAULT '', after_location VARCHAR(64) NOT NULL DEFAULT '', operator_id BIGINT UNSIGNED NULL DEFAULT NULL, operator_name VARCHAR(64) NOT NULL DEFAULT '', remark VARCHAR(500) NOT NULL DEFAULT '', payload_json JSON NULL, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY idx_order_transfer_flow_logs_flow_id (flow_id), KEY idx_order_transfer_flow_logs_order_id (order_id), KEY idx_order_transfer_flow_logs_tag_no (internal_tag_no), KEY idx_order_transfer_flow_logs_action_code (action_code), KEY idx_order_transfer_flow_logs_created_at (created_at) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单内部流转日志' SQL); $reportColumns = [ 'zhongjian_report_no' => "ALTER TABLE reports ADD COLUMN zhongjian_report_no VARCHAR(128) NOT NULL DEFAULT '' AFTER publish_time", 'report_entry_admin_id' => "ALTER TABLE reports ADD COLUMN report_entry_admin_id BIGINT UNSIGNED NULL DEFAULT NULL AFTER zhongjian_report_no", 'report_entry_admin_name' => "ALTER TABLE reports ADD COLUMN report_entry_admin_name VARCHAR(64) NOT NULL DEFAULT '' AFTER report_entry_admin_id", 'report_entered_at' => "ALTER TABLE reports ADD COLUMN report_entered_at DATETIME NULL DEFAULT NULL AFTER report_entry_admin_name", ]; foreach ($reportColumns as $column => $sql) { if (!ffHasColumn($pdo, 'reports', $column)) { $pdo->exec($sql); echo "ADD_COLUMN reports.{$column}\n"; } } if (!ffHasIndex($pdo, 'reports', 'idx_reports_zhongjian_report_no')) { $pdo->exec('ALTER TABLE reports ADD KEY idx_reports_zhongjian_report_no (zhongjian_report_no)'); echo "ADD_INDEX reports.idx_reports_zhongjian_report_no\n"; } if (!ffHasColumn($pdo, 'report_contents', 'zhongjian_report_files_json')) { $pdo->exec('ALTER TABLE report_contents ADD COLUMN zhongjian_report_files_json JSON NULL AFTER evidence_attachments_json'); echo "ADD_COLUMN report_contents.zhongjian_report_files_json\n"; } echo "SCHEMA_UPGRADE_FULFILLMENT_FLOW_OK\n";