-- Stock-System 优化后的数据库表结构

-- 设置字符集
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 1. 基础数据表

-- 1.1 行业表
DROP TABLE IF EXISTS `new_stock_industry`;
CREATE TABLE `new_stock_industry` (
  `industry_id` INT NOT NULL AUTO_INCREMENT COMMENT '行业ID',
  `industry_level` TINYINT NOT NULL COMMENT '行业级别（1-3）',
  `industry_code` VARCHAR(20) NOT NULL COMMENT '行业代码',
  `industry_name` VARCHAR(50) NOT NULL COMMENT '行业名称',
  `parent_code` VARCHAR(20) NULL COMMENT '父行业代码',
  PRIMARY KEY (`industry_id`) USING BTREE,
  UNIQUE INDEX `idx_industry_code` (`industry_code`) USING BTREE,
  INDEX `idx_industry_level` (`industry_level`) USING BTREE,
  INDEX `idx_parent_code` (`parent_code`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '行业分类表' ROW_FORMAT = Dynamic;

-- 1.2 股票基础信息表
DROP TABLE IF EXISTS `new_stock_basic`;
CREATE TABLE `new_stock_basic` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` VARCHAR(10) NOT NULL COMMENT '股票代码',
  `name` VARCHAR(20) NOT NULL COMMENT '股票名称',
  `industry_id` INT NULL COMMENT '所属行业ID（关联new_stock_industry）',
  `list_date` DATE NULL COMMENT '首发上市日期',
  `market_type` VARCHAR(10) NULL COMMENT '市场类型（SZ/SH）',
  `status` TINYINT DEFAULT 1 COMMENT '状态（1-正常，0-暂停上市）',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_code` (`code`) USING BTREE,
  INDEX `idx_industry_id` (`industry_id`) USING BTREE,
  INDEX `idx_list_date` (`list_date`) USING BTREE,
  INDEX `idx_market_type` (`market_type`) USING BTREE,
  CONSTRAINT `fk_stock_basic_industry` FOREIGN KEY (`industry_id`) REFERENCES `new_stock_industry` (`industry_id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '股票基础信息表' ROW_FORMAT = Dynamic;

-- 2. 财务数据表

-- 2.1 财务数据表
DROP TABLE IF EXISTS `new_stock_financial`;
CREATE TABLE `new_stock_financial` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` VARCHAR(10) NOT NULL COMMENT '股票代码（关联new_stock_basic）',
  `period` DATE NOT NULL COMMENT '报告期',
  `net_profit_yoy` DECIMAL(15,4) NULL COMMENT '净利润同比增长率',
  `net_profit_qoq` DECIMAL(15,4) NULL COMMENT '净利润环比增长率',
  `roe` DECIMAL(10,4) NULL COMMENT '净资产收益率ROE',
  `eps` DECIMAL(10,4) NULL COMMENT '每股收益EPS',
  `net_profit` DECIMAL(20,4) NULL COMMENT '净利润',
  `basic_eps` DECIMAL(10,4) NULL COMMENT '基本每股收益',
  `bps` DECIMAL(10,4) NULL COMMENT '每股净资产BPS',
  `total_assets` DECIMAL(20,4) NULL COMMENT '总资产',
  `total_liabilities` DECIMAL(20,4) NULL COMMENT '总负债',
  `operating_income` DECIMAL(20,4) NULL COMMENT '营业收入',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_code_period` (`code`, `period`) USING BTREE,
  INDEX `idx_period` (`period`) USING BTREE,
  INDEX `idx_roe` (`roe`) USING BTREE,
  INDEX `idx_eps` (`eps`) USING BTREE,
  CONSTRAINT `fk_stock_financial_basic` FOREIGN KEY (`code`) REFERENCES `new_stock_basic` (`code`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '股票财务数据表' ROW_FORMAT = Dynamic;

-- 3. 指数数据表

-- 3.1 指数数据表
DROP TABLE IF EXISTS `new_stock_index`;
CREATE TABLE `new_stock_index` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` VARCHAR(10) NOT NULL COMMENT '指数代码',
  `name` VARCHAR(20) NOT NULL COMMENT '指数名称',
  `trade_date` DATE NOT NULL COMMENT '交易日期',
  `open` DECIMAL(10,2) NULL COMMENT '开盘价',
  `close` DECIMAL(10,2) NULL COMMENT '收盘价',
  `high` DECIMAL(10,2) NULL COMMENT '最高价',
  `low` DECIMAL(10,2) NULL COMMENT '最低价',
  `change_rate` DECIMAL(8,4) NULL COMMENT '涨跌幅',
  `volume` BIGINT NULL COMMENT '成交量',
  `amount` DECIMAL(20,2) NULL COMMENT '成交额',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_code_trade_date` (`code`, `trade_date`) USING BTREE,
  INDEX `idx_trade_date` (`trade_date`) USING BTREE,
  INDEX `idx_change_rate` (`change_rate`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '股票指数数据表' ROW_FORMAT = Dynamic;

-- 4. 交易数据表

-- 4.1 交易数据表
DROP TABLE IF EXISTS `new_stocks`;
CREATE TABLE `new_stocks` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` VARCHAR(10) NOT NULL COMMENT '股票代码（关联new_stock_basic）',
  `trade_date` DATE NOT NULL COMMENT '交易日期',
  `open` DECIMAL(10,2) NULL COMMENT '开盘价',
  `close` DECIMAL(10,2) NULL COMMENT '收盘价',
  `change_rate` DECIMAL(8,4) NULL COMMENT '当日涨跌幅',
  `trade_days` INT NULL COMMENT '可交易日数',
  `volume` BIGINT NULL COMMENT '成交量',
  `amount` DECIMAL(20,2) NULL COMMENT '成交额',
  `change_rate_10` DECIMAL(8,4) NULL COMMENT '10日区间涨跌幅',
  `change_rate_20` DECIMAL(8,4) NULL COMMENT '20日区间涨跌幅',
  `change_rate_60` DECIMAL(8,4) NULL COMMENT '60日区间涨跌幅',
  `avg_volume_20` DECIMAL(20,2) NULL COMMENT '20日区间平均成交量',
  `free_float_market_value` DECIMAL(20,2) NULL COMMENT '自由流通市值',
  `total_market_value` DECIMAL(20,2) NULL COMMENT '总市值',
  `agencies_hold` DECIMAL(10,4) NULL COMMENT '机构持仓合计',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_code_trade_date` (`code`, `trade_date`) USING BTREE,
  INDEX `idx_trade_date` (`trade_date`) USING BTREE,
  INDEX `idx_change_rate` (`change_rate`) USING BTREE,
  INDEX `idx_total_market_value` (`total_market_value`) USING BTREE,
  CONSTRAINT `fk_stocks_basic` FOREIGN KEY (`code`) REFERENCES `new_stock_basic` (`code`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '股票每日交易数据表' ROW_FORMAT = Dynamic;

-- 5. 趋势数据表

-- 5.1 趋势数据表
DROP TABLE IF EXISTS `new_stock_trend`;
CREATE TABLE `new_stock_trend` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` VARCHAR(10) NOT NULL COMMENT '股票代码（关联new_stock_basic）',
  `trade_date` DATE NOT NULL COMMENT '交易日期',
  `ma5` DECIMAL(10,2) NULL COMMENT '5日均线',
  `ma10` DECIMAL(10,2) NULL COMMENT '10日均线',
  `ma20` DECIMAL(10,2) NULL COMMENT '20日均线',
  `ma60` DECIMAL(10,2) NULL COMMENT '60日均线',
  `macd` DECIMAL(8,4) NULL COMMENT 'MACD值',
  `kdj_k` DECIMAL(8,4) NULL COMMENT 'KDJ-K值',
  `kdj_d` DECIMAL(8,4) NULL COMMENT 'KDJ-D值',
  `kdj_j` DECIMAL(8,4) NULL COMMENT 'KDJ-J值',
  `rsi` DECIMAL(8,4) NULL COMMENT 'RSI值',
  `volume_ratio` DECIMAL(8,4) NULL COMMENT '量比',
  `turnover_rate` DECIMAL(8,4) NULL COMMENT '换手率',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_code_trade_date` (`code`, `trade_date`) USING BTREE,
  INDEX `idx_trade_date` (`trade_date`) USING BTREE,
  CONSTRAINT `fk_stock_trend_basic` FOREIGN KEY (`code`) REFERENCES `new_stock_basic` (`code`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '股票趋势数据表' ROW_FORMAT = Dynamic;

-- 5.2 涨跌停数据表
DROP TABLE IF EXISTS `new_stock_limit`;
CREATE TABLE `new_stock_limit` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` VARCHAR(10) NOT NULL COMMENT '股票代码（关联new_stock_basic）',
  `trade_date` DATE NOT NULL COMMENT '交易日期',
  `limit_type` TINYINT NOT NULL COMMENT '涨跌停类型（1-涨停，-1-跌停）',
  `limit_price` DECIMAL(10,2) NULL COMMENT '涨跌停价格',
  `open_price` DECIMAL(10,2) NULL COMMENT '开盘价',
  `close_price` DECIMAL(10,2) NULL COMMENT '收盘价',
  `volume` BIGINT NULL COMMENT '成交量',
  `amount` DECIMAL(20,2) NULL COMMENT '成交额',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_code_trade_date` (`code`, `trade_date`) USING BTREE,
  INDEX `idx_trade_date` (`trade_date`) USING BTREE,
  INDEX `idx_limit_type` (`limit_type`) USING BTREE,
  CONSTRAINT `fk_stock_limit_basic` FOREIGN KEY (`code`) REFERENCES `new_stock_basic` (`code`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '股票涨跌停数据表' ROW_FORMAT = Dynamic;

-- 5.3 创新高/新低数据表
DROP TABLE IF EXISTS `new_stock_new_record`;
CREATE TABLE `new_stock_new_record` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` VARCHAR(10) NOT NULL COMMENT '股票代码（关联new_stock_basic）',
  `trade_date` DATE NOT NULL COMMENT '交易日期',
  `record_type` TINYINT NOT NULL COMMENT '记录类型（1-创新高，-1-创新低）',
  `price` DECIMAL(10,2) NOT NULL COMMENT '价格',
  `days` INT NULL COMMENT '创新高/新低天数',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_code_trade_date` (`code`, `trade_date`) USING BTREE,
  INDEX `idx_trade_date` (`trade_date`) USING BTREE,
  INDEX `idx_record_type` (`record_type`) USING BTREE,
  CONSTRAINT `fk_stock_new_record_basic` FOREIGN KEY (`code`) REFERENCES `new_stock_basic` (`code`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '股票创新高/新低数据表' ROW_FORMAT = Dynamic;

-- 6. 交易日数据

-- 6.1 交易日历表
DROP TABLE IF EXISTS `new_trade_dates`;
CREATE TABLE `new_trade_dates` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `trade_date` DATE NOT NULL COMMENT '交易日日期',
  `is_trading` TINYINT DEFAULT 1 COMMENT '是否交易日（1-是，0-否）',
  `year` SMALLINT NOT NULL COMMENT '年份',
  `month` TINYINT NOT NULL COMMENT '月份',
  `day` TINYINT NOT NULL COMMENT '日期',
  `weekday` TINYINT NOT NULL COMMENT '星期几（1-7）',
  `is_weekend` TINYINT DEFAULT 0 COMMENT '是否周末（1-是，0-否）',
  `is_holiday` TINYINT DEFAULT 0 COMMENT '是否节假日（1-是，0-否）',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_trade_date` (`trade_date`) USING BTREE,
  INDEX `idx_year_month` (`year`, `month`) USING BTREE,
  INDEX `idx_is_trading` (`is_trading`) USING BTREE,
  INDEX `idx_weekday` (`weekday`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '交易日历表' ROW_FORMAT = Dynamic;

-- 7. 行业趋势分析数据

-- 7.1 行业趋势表
DROP TABLE IF EXISTS `new_industry_trend`;
CREATE TABLE `new_industry_trend` (
  `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `industry_id` INT NOT NULL COMMENT '行业ID（关联new_stock_industry）',
  `trade_date` DATE NOT NULL COMMENT '交易日期',
  `avg_change_rate` DECIMAL(8,4) NULL COMMENT '行业平均涨跌幅',
  `up_stocks_count` INT NULL COMMENT '上涨股票数',
  `down_stocks_count` INT NULL COMMENT '下跌股票数',
  `flat_stocks_count` INT NULL COMMENT '平盘股票数',
  `limit_up_count` INT NULL COMMENT '涨停股票数',
  `limit_down_count` INT NULL COMMENT '跌停股票数',
  `total_market_value` DECIMAL(25,2) NULL COMMENT '行业总市值',
  `avg_turnover_rate` DECIMAL(8,4) NULL COMMENT '行业平均换手率',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `idx_industry_trade_date` (`industry_id`, `trade_date`) USING BTREE,
  INDEX `idx_trade_date` (`trade_date`) USING BTREE,
  INDEX `idx_avg_change_rate` (`avg_change_rate`) USING BTREE,
  CONSTRAINT `fk_industry_trend_industry` FOREIGN KEY (`industry_id`) REFERENCES `new_stock_industry` (`industry_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '行业趋势分析表' ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;