-- ============================================================
-- 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 测试照片表';


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

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