-- ============================================================
-- Corewing QC 后端数据库建表 SQL（MySQL）
-- ============================================================


CREATE TABLE `flight_controller` (
    `id`          BIGINT       NOT NULL AUTO_INCREMENT,
    `sn`          VARCHAR(64)  NOT NULL COMMENT '序列号',
    `model`       VARCHAR(64)  DEFAULT NULL COMMENT '型号',
    `batch_no`    VARCHAR(64)  DEFAULT NULL COMMENT '批次号',
    `status`      VARCHAR(32)  NOT NULL DEFAULT 'REGISTERED' COMMENT '状态: REGISTERED / QC_PASS / QC_FAIL / ACTIVATED',
    `create_time` DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `update_time` DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_sn` (`sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='飞控板设备注册表';


CREATE TABLE `qc_test_record` (
    `id`            BIGINT       NOT NULL AUTO_INCREMENT,
    `test_no`       VARCHAR(32)  NOT NULL COMMENT '测试单号，格式: WL-yyyyMMdd-NNN / FC-yyyyMMdd-NNN',
    `product_type`  VARCHAR(32)  NOT NULL COMMENT '产品类型: WIRELESS_BOARD / FLIGHT_CONTROLLER',
    `sn`            VARCHAR(64)  NOT NULL COMMENT '产品序列号（无线板为 BLE MAC）',
    `operator_id`   VARCHAR(32)  NOT NULL COMMENT '测试员 ID',
    `operator_name` VARCHAR(64)  NOT NULL COMMENT '测试员姓名',
    `phone_model`   VARCHAR(64)  DEFAULT NULL COMMENT '测试手机型号',
    `app_version`   VARCHAR(16)  DEFAULT NULL COMMENT 'APP 版本',
    `status`        VARCHAR(16)  NOT NULL COMMENT '测试结果: PASS / FAIL',
    `create_time`   DATETIME     NOT NULL COMMENT 'APP 端创建时间',
    `updated_at`    DATETIME     DEFAULT NULL COMMENT 'APP 端最后更新时间',
    `upload_time`   DATETIME     DEFAULT NULL COMMENT 'APP 端上传时间',
    `report_url`    VARCHAR(512) DEFAULT NULL COMMENT '测试报告 URL',
    `server_create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '服务端入库时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_test_no` (`test_no`),
    KEY `idx_sn` (`sn`),
    KEY `idx_product_type` (`product_type`),
    KEY `idx_status` (`status`),
    KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='QC 测试记录主表';


CREATE TABLE `qc_step_result` (
    `id`             BIGINT       NOT NULL AUTO_INCREMENT,
    `test_record_id` BIGINT       NOT NULL COMMENT '关联 qc_test_record.id',
    `step_index`     INT          NOT NULL COMMENT '步骤序号（从 1 开始）',
    `step_name`      VARCHAR(64)  NOT NULL COMMENT '步骤名称',
    `result`         VARCHAR(8)   DEFAULT NULL COMMENT '步骤结果: PASS / FAIL / NULL',
    `data_json`      TEXT         DEFAULT NULL COMMENT '步骤详细数据 JSON',
    `duration`       BIGINT       DEFAULT 0 COMMENT '步骤耗时（毫秒）',
    `completed_at`   DATETIME     DEFAULT NULL COMMENT '步骤完成时间',
    PRIMARY KEY (`id`),
    KEY `idx_test_record_id` (`test_record_id`),
    UNIQUE KEY `uk_record_step` (`test_record_id`, `step_index`),
    CONSTRAINT `fk_step_record` FOREIGN KEY (`test_record_id`)
        REFERENCES `qc_test_record` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='QC 步骤结果表';


CREATE TABLE `qc_photo` (
    `id`             BIGINT       NOT NULL AUTO_INCREMENT,
    `test_record_id` BIGINT       NOT NULL COMMENT '关联 qc_test_record.id',
    `test_no`        VARCHAR(32)  NOT NULL COMMENT '测试单号',
    `url`            VARCHAR(512) NOT NULL COMMENT '照片远程 URL',
    `create_time`    DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_test_no` (`test_no`),
    CONSTRAINT `fk_photo_record` FOREIGN KEY (`test_record_id`)
        REFERENCES `qc_test_record` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='QC 测试照片表';


-- ============================================================
-- 固件表
-- ============================================================

CREATE TABLE `app_aat_version` (
    `id`                   BIGINT       NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `firmware_type`        VARCHAR(50)  NOT NULL COMMENT '固件类型: lb_main / lb_signal_master / lbPro_main / lbPro_signal_master / lbPro_screen_ui',
    `firmware_name`        VARCHAR(100) NOT NULL COMMENT '固件文件名',
    `firmware_version`     VARCHAR(20)  NOT NULL COMMENT '固件版本号',
    `min_hardware_version` VARCHAR(20)  NOT NULL DEFAULT '0.0' COMMENT '最低硬件版本',
    `download_url`         VARCHAR(500) DEFAULT NULL COMMENT '固件下载地址',
    `firmware_size`        BIGINT       DEFAULT NULL COMMENT '固件大小（字节）',
    `firmware_description` TEXT         DEFAULT NULL COMMENT '固件描述',
    `create_time`          DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time`          DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_type_version` (`firmware_type`, `firmware_version`),
    KEY `idx_firmware_type` (`firmware_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AAT固件版本表';

-- 插入固件数据
INSERT INTO `app_aat_version` (`firmware_type`, `firmware_name`, `firmware_version`, `min_hardware_version`)
VALUES
-- lb_main
('lb_main', 'lb_main_2.1.bin', '2.1', '1.0'),
-- lb_signal_master
('lb_signal_master', 'lbsm_1.1.bin', '1.1', '0.0'),
-- lbPro_main
('lbPro_main', 'lbPro_main_2.3.bin', '2.3', '0.0'),
-- lbPro_signal_master
('lbPro_signal_master', 'lbPro_sm_1.4.bin', '1.4', '0.0'),
-- lbPro_screen_ui
('lbPro_screen_ui', 'lbPro_ui_2.7.tft', '2.7', '0.0');


-- ============================================================
-- 示例数据（可选）
-- ============================================================

-- 注册一批无线板
-- INSERT INTO `wireless_board` (`mac`) VALUES
--     ('AA:BB:CC:DD:EE:01'),
--     ('AA:BB:CC:DD:EE:02'),
--     ('AA:BB:CC:DD:EE:03');
