-- =============================================
-- 资质备案管理模块 - 增量脚本
-- 基准: init-v2.sql（105fb28 及之前已建表环境）
-- 说明: 支持资质申请管理后端（四态、变更明细5步、changeCount聚合等）
-- 日期: 2026-06-29
-- 执行: mysql -u<user> -p<db> < increment-qual-filing-management-20260629.sql
-- =============================================

SET NAMES utf8mb4;

-- ---------------------------------------------
-- 1. qual_filing 主表
-- ---------------------------------------------
ALTER TABLE `qual_filing`
    ADD COLUMN `origin_filing_id` bigint DEFAULT NULL COMMENT '变更草稿关联的原备案id' AFTER `attachment_url`,
    ADD COLUMN `reject_reason` varchar(500) DEFAULT NULL COMMENT '打回原因' AFTER `origin_filing_id`,
    ADD COLUMN `filing_review_id` bigint DEFAULT NULL COMMENT '审核id' AFTER `reject_reason`,
    ADD COLUMN `submit_time` datetime DEFAULT NULL COMMENT '最近提交时间' AFTER `filing_review_id`,
    ADD COLUMN `approve_time` datetime DEFAULT NULL COMMENT '最近审核通过时间' AFTER `submit_time`;

ALTER TABLE `qual_filing`
    MODIFY COLUMN `filing_status_code` int DEFAULT NULL COMMENT '备案状态编码(1申请中2审核中3已打回4变更审核中)',
    MODIFY COLUMN `apply_type_code` int DEFAULT NULL COMMENT '申请类型编码(1资质备案申请2变更备案草稿3已备案资质填报)',
    MODIFY COLUMN `apply_type_name` varchar(50) DEFAULT NULL COMMENT '申请类型名称';

ALTER TABLE `qual_filing`
    ADD KEY `idx_qual_filing_origin_id` (`origin_filing_id`),
    ADD KEY `idx_qual_filing_apply_type` (`apply_type_code`);

-- 申请类型名称与新版枚举对齐（code 不变，仅刷新展示名）
UPDATE `qual_filing`
SET `apply_type_name` = CASE `apply_type_code`
    WHEN 1 THEN '资质备案申请'
    WHEN 2 THEN '变更备案草稿'
    WHEN 3 THEN '已备案资质填报'
    ELSE `apply_type_name`
END
WHERE `apply_type_code` IN (1, 2, 3);

-- ---------------------------------------------
-- 2. qual_filing_change 变更头表
-- ---------------------------------------------
ALTER TABLE `qual_filing_change`
    ADD COLUMN `change_filing_id` bigint DEFAULT NULL COMMENT '关联变更草稿qual_filing.id' AFTER `filing_id`,
    ADD COLUMN `reject_reason` varchar(500) DEFAULT NULL COMMENT '打回原因' AFTER `change_filing_id`,
    ADD COLUMN `submit_time` datetime DEFAULT NULL COMMENT '提交时间' AFTER `reject_reason`;

ALTER TABLE `qual_filing_change`
    DROP COLUMN `change_count`;

-- ---------------------------------------------
-- 3. qual_filing_change_detail 变更明细（5步表单）
-- ---------------------------------------------
ALTER TABLE `qual_filing_change_detail`
    ADD COLUMN `form_step_code` int DEFAULT NULL COMMENT '表单步骤编码(1-5)' AFTER `change_id`,
    ADD COLUMN `form_step_name` varchar(50) DEFAULT NULL COMMENT '表单步骤名称' AFTER `form_step_code`,
    ADD COLUMN `change_item_code` varchar(32) DEFAULT NULL COMMENT '变更细项编码' AFTER `change_content`,
    ADD COLUMN `change_item_name` varchar(50) DEFAULT NULL COMMENT '变更细项名称' AFTER `change_item_code`,
    ADD COLUMN `before_value` text COMMENT '变更前' AFTER `change_item_name`,
    ADD COLUMN `after_value` text COMMENT '变更后' AFTER `before_value`;

-- 历史明细无步骤信息时，默认归入第1步
UPDATE `qual_filing_change_detail`
SET `form_step_code` = 1,
    `form_step_name` = '备案基本信息'
WHERE `form_step_code` IS NULL;

ALTER TABLE `qual_filing_change_detail`
    MODIFY COLUMN `form_step_code` int NOT NULL COMMENT '表单步骤编码(1-5)',
    MODIFY COLUMN `form_step_name` varchar(50) NOT NULL COMMENT '表单步骤名称',
    MODIFY COLUMN `change_field` varchar(100) DEFAULT NULL COMMENT '变更字段',
    MODIFY COLUMN `change_content` varchar(1000) DEFAULT NULL COMMENT '变更内容摘要';

ALTER TABLE `qual_filing_change_detail`
    ADD KEY `idx_qual_filing_change_detail_step` (`change_id`, `form_step_code`);

-- ---------------------------------------------
-- 4. qual_filing_material 材料表
-- ---------------------------------------------
ALTER TABLE `qual_filing_material`
    ADD COLUMN `sort_order` int DEFAULT '0' COMMENT '排序序号' AFTER `material_format`,
    ADD COLUMN `required_flag` tinyint DEFAULT '1' COMMENT '是否必需(1是2否)' AFTER `sort_order`,
    ADD COLUMN `upload_status_code` int DEFAULT NULL COMMENT '上传状态编码(1待上传2已上传)' AFTER `required_flag`,
    ADD COLUMN `upload_status_name` varchar(50) DEFAULT NULL COMMENT '上传状态名称' AFTER `upload_status_code`,
    ADD COLUMN `attachment_desc` varchar(200) DEFAULT NULL COMMENT '上传附件说明' AFTER `upload_status_name`;

-- ---------------------------------------------
-- 5. qual_filing_equipment 装备表
-- ---------------------------------------------
ALTER TABLE `qual_filing_equipment`
    ADD COLUMN `calibration_report_url` varchar(500) DEFAULT NULL COMMENT '计量检定报告地址' AFTER `dual_channel_flag`;

-- ---------------------------------------------
-- 6. qual_filing_personnel_cert 人员证书表
-- ---------------------------------------------
ALTER TABLE `qual_filing_personnel_cert`
    ADD COLUMN `source_cert_id` bigint DEFAULT NULL COMMENT '来源人员证书id' AFTER `cert_attachment_url`,
    ADD KEY `idx_qual_filing_personnel_cert_source_id` (`source_cert_id`);

-- =============================================
-- 完成
-- =============================================
