-- =============================================================
-- 线上线下库对齐修复（修复版v2）
-- 适配 dict_code / dict_id 非自增的表结构
-- =============================================================
SET NAMES utf8mb4;

-- ==================== 1. 补字典类型 ====================
SET @max_dict_id = (SELECT COALESCE(MAX(dict_id), 0) FROM sys_dict_type);

INSERT IGNORE INTO sys_dict_type (dict_id, dict_name, dict_type, status, create_by, create_time)
VALUES (@max_dict_id + 1, '改判后质量状态', 'regrade_quality_type', '0', 'admin', NOW());

-- ==================== 2. 补字典数据 ====================
SET @max_code = (SELECT COALESCE(MAX(dict_code), 0) FROM sys_dict_data);
SET @code = @max_code + 1;

-- quality_review_status 缺失的3条
INSERT IGNORE INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time)
VALUES (@code, 2, '待审批', '2', 'quality_review_status', '', 'warning', 'Y', '0', 'admin', NOW());
SET @code = @code + 1;

INSERT IGNORE INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time)
VALUES (@code, 3, '已通过', '3', 'quality_review_status', '', 'success', 'Y', '0', 'admin', NOW());
SET @code = @code + 1;

INSERT IGNORE INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time)
VALUES (@code, 4, '已驳回', '4', 'quality_review_status', '', 'danger', 'Y', '0', 'admin', NOW());
SET @code = @code + 1;

-- regrade_quality_type 5条
INSERT IGNORE INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time)
VALUES (@code, 1, '协议销售', 'protocol_sale', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());
SET @code = @code + 1;

INSERT IGNORE INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time)
VALUES (@code, 2, '转分剪', 'to_slitting', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());
SET @code = @code + 1;

INSERT IGNORE INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time)
VALUES (@code, 3, '降级', 'downgrade', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());
SET @code = @code + 1;

INSERT IGNORE INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time)
VALUES (@code, 4, '返修', 'rework', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());
SET @code = @code + 1;

INSERT IGNORE INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time)
VALUES (@code, 5, '报废', 'scrap', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());

-- ==================== 3. 验证 ====================
SELECT 'Dict fix OK' AS result;
