-- --------------------------------------------------------
-- 完整数据库备份与安装脚本 (V2 - 包含所有业务表)
-- 适用环境：宝塔面板 / PHP + MySQL
-- --------------------------------------------------------

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for admins
-- ----------------------------
DROP TABLE IF EXISTS `admins`;
CREATE TABLE `admins` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `role` varchar(20) DEFAULT 'admin',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `is_agent` tinyint(4) DEFAULT 0,
  `permissions` text DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `admins` VALUES (1, 'admin', '$2y$10$uJvcqHNb.naRWj.apBapi.C.fF2zaIbMhYEtVdGmmVUWZkQi9ESfe', 'admin', '2026-02-18 03:07:35', 0, NULL);

-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `password_hash` varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `uid` varchar(20) DEFAULT NULL,
  `credit_score` int(11) DEFAULT 80,
  `real_name_status` int(11) DEFAULT 0,
  `role` varchar(20) DEFAULT 'user',
  `vip_level` int(11) DEFAULT 0,
  `total_recharge` decimal(16,4) DEFAULT 0.0000,
  `transaction_password` varchar(255) DEFAULT NULL,
  `kyc_name` varchar(100) DEFAULT NULL,
  `kyc_id_number` varchar(50) DEFAULT NULL,
  `kyc_photo_front` varchar(255) DEFAULT NULL,
  `kyc_photo_back` varchar(255) DEFAULT NULL,
  `kyc_photo_handheld?` varchar(255) DEFAULT NULL,
  `kyc_photo_handheld` varchar(255) DEFAULT NULL,
  `kyc_status` int(11) DEFAULT 0 COMMENT '0: Unverified, 1: Pending, 2: Verified, 3: Rejected',
  `registration_ip` varchar(45) DEFAULT NULL,
  `status` enum('normal','frozen') DEFAULT 'normal',
  `win_loss_control` tinyint(4) DEFAULT 0 COMMENT '0: normal, 1: win, 2: loss',
  `remark` text DEFAULT NULL,
  `kyc_rejection_reason` text DEFAULT NULL,
  `agent_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `uid` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for user_balances
-- ----------------------------
DROP TABLE IF EXISTS `user_balances`;
CREATE TABLE `user_balances` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `symbol` varchar(10) NOT NULL,
  `available` decimal(20,8) DEFAULT 0.00000000,
  `frozen` decimal(20,8) DEFAULT 0.00000000,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`symbol`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for system_settings
-- ----------------------------
DROP TABLE IF EXISTS `system_settings`;
CREATE TABLE `system_settings` (
  `setting_key` varchar(50) NOT NULL,
  `setting_value` text DEFAULT NULL,
  PRIMARY KEY (`setting_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `system_settings` (setting_key, setting_value) VALUES 
('android_download_url', '/downloads/byro.apk'),
('apk_download_url', '/downloads/byro.apk'),
('email_verification_enabled', '0'),
('ios_download_url', '/downloads/byro.apk'),
('mail_from_email', ''),
('mail_from_name', 'Byro Exchange'),
('site_name', 'Byro'),
('smtp_port', '587'),
('smtp_secure', 'tls'),
('usdt_protocol', 'TRC20'),
('usdt_trc20_address', 'TYv9V5J1P1eEwz7y3WqJg9M2yv7f7xXv3x');

-- ----------------------------
-- Table structure for staking_records
-- ----------------------------
DROP TABLE IF EXISTS `staking_records`;
CREATE TABLE `staking_records` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `plan_name` varchar(100) NOT NULL,
  `amount` decimal(20,8) NOT NULL,
  `symbol` varchar(10) DEFAULT 'USDT',
  `daily_profit` decimal(5,2) NOT NULL,
  `period` int(11) NOT NULL COMMENT 'days',
  `status` enum('running','ended') DEFAULT 'running',
  `start_date` date NOT NULL,
  `end_date` date NOT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for transactions
-- ----------------------------
DROP TABLE IF EXISTS `transactions`;
CREATE TABLE `transactions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `type` varchar(20) NOT NULL,
  `amount` decimal(20,8) NOT NULL,
  `symbol` varchar(10) NOT NULL,
  `status` varchar(20) DEFAULT 'completed',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for binary_orders
-- ----------------------------
DROP TABLE IF EXISTS `binary_orders`;
CREATE TABLE `binary_orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `symbol` varchar(20) NOT NULL,
  `direction` varchar(10) DEFAULT NULL,
  `amount` decimal(20,8) NOT NULL,
  `duration` int(11) NOT NULL,
  `profit_rate` decimal(5,2) NOT NULL,
  `entry_price` decimal(20,8) NOT NULL,
  `close_price` decimal(20,8) DEFAULT NULL,
  `status` enum('pending','won','lost','cancelled') DEFAULT 'pending',
  `control_status` tinyint(4) DEFAULT 0 COMMENT '0: normal, 1: force win, 2: force loss',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `end_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for contract_orders
-- ----------------------------
DROP TABLE IF EXISTS `contract_orders`;
CREATE TABLE `contract_orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `symbol` varchar(20) NOT NULL,
  `type` enum('limit','market') DEFAULT 'market',
  `direction` varchar(10) DEFAULT NULL,
  `leverage` int(11) DEFAULT 1,
  `amount` decimal(20,8) NOT NULL,
  `entry_price` decimal(20,8) DEFAULT NULL,
  `close_price` decimal(20,8) DEFAULT NULL,
  `status` enum('open','closed','cancelled') DEFAULT 'open',
  `profit` decimal(20,8) DEFAULT 0.00000000,
  `control_status` tinyint(4) DEFAULT 0 COMMENT '0: normal, 1: force win, 2: force loss',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for exchange_records
-- ----------------------------
DROP TABLE IF EXISTS `exchange_records`;
CREATE TABLE `exchange_records` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `from_symbol` varchar(10) NOT NULL,
  `to_symbol` varchar(10) NOT NULL,
  `from_amount` decimal(20,8) NOT NULL,
  `to_amount` decimal(20,8) NOT NULL,
  `rate` decimal(20,8) NOT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for price_controls
-- ----------------------------
DROP TABLE IF EXISTS `price_controls`;
CREATE TABLE `price_controls` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `symbol` varchar(20) NOT NULL,
  `target_price` decimal(20,8) NOT NULL,
  `execution_time` timestamp NOT NULL,
  `duration` int(11) DEFAULT 60 COMMENT 'seconds',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for spot_orders
-- ----------------------------
DROP TABLE IF EXISTS `spot_orders`;
CREATE TABLE `spot_orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `symbol` varchar(20) NOT NULL,
  `side` enum('buy','sell') NOT NULL,
  `price` decimal(20,8) DEFAULT NULL,
  `amount` decimal(20,8) NOT NULL,
  `filled` decimal(20,8) DEFAULT 0.00000000,
  `status` enum('pending','filled','cancelled') DEFAULT 'pending',
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Table structure for messages
-- ----------------------------
DROP TABLE IF EXISTS `messages`;
CREATE TABLE `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `admin_id` int(11) DEFAULT NULL,
  `sender` enum('user','admin') DEFAULT NULL,
  `message` text DEFAULT NULL,
  `ip_address` varchar(45) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET FOREIGN_KEY_CHECKS = 1;
