-- =============================================================
-- 质量评审流程 - 安装脚本
-- 用法: mysql -h 140.143.206.120 -P 13306 -u klp -p 数据库名 --default-character-set=utf8mb4 < 本文件
-- =============================================================
SET NAMES utf8mb4;

-- ========== 1. 彻底清理所有质量评审相关数据 ==========
-- 删除角色-菜单关联
DELETE FROM sys_role_menu WHERE menu_id IN (SELECT menu_id FROM sys_menu WHERE perms LIKE '%qualityReview%');
DELETE FROM sys_role_menu WHERE menu_id IN (SELECT menu_id FROM sys_menu WHERE menu_name = '质量评审' OR menu_name = '质量评审待审批');
-- 删除菜单本身
DELETE FROM sys_menu WHERE perms LIKE '%qualityReview%';
DELETE FROM sys_menu WHERE menu_name = '质量评审';
DELETE FROM sys_menu WHERE menu_name = '质量评审待审批';
-- 删除字典
DELETE FROM sys_dict_data WHERE dict_type = 'quality_review_status';
DELETE FROM sys_dict_data WHERE dict_type = 'regrade_quality_type';
DELETE FROM sys_dict_type WHERE dict_type = 'quality_review_status';
DELETE FROM sys_dict_type WHERE dict_type = 'regrade_quality_type';
-- 删表
DROP TABLE IF EXISTS qc_quality_review_log;
DROP TABLE IF EXISTS qc_quality_review_coil;
DROP TABLE IF EXISTS qc_quality_review;

-- ========== 2. 建表 ==========
CREATE TABLE IF NOT EXISTS `qc_quality_review` (
  `review_id`       bigint NOT NULL AUTO_INCREMENT COMMENT '评审单主键',
  `review_no`       varchar(50) NOT NULL COMMENT '评审单编号',
  `product_name`    varchar(100)  DEFAULT NULL COMMENT '产品名称',
  `transmit_dept`   varchar(50)   DEFAULT NULL COMMENT '传递部门',
  `transmit_user`   varchar(50)   DEFAULT NULL COMMENT '传递人',
  `transmit_date`   date          DEFAULT NULL COMMENT '传递日期',
  `prod_date_range` varchar(50)   DEFAULT NULL COMMENT '生产日期范围',
  `flow_status`     tinyint(1)    DEFAULT '1' COMMENT '流程状态: 1=待提交 2=待审批 3=已通过 4=已驳回',
  `dept_opinion`    text          COMMENT '品质部评审意见',
  `dept_sign`       varchar(50)   DEFAULT NULL COMMENT '品质部签字人',
  `dept_sign_date`  date          DEFAULT NULL COMMENT '品质部签字日期',
  `leader_opinion`  text          COMMENT '领导审批意见',
  `leader_sign`     varchar(50)   DEFAULT NULL COMMENT '领导签字人',
  `leader_sign_date` date         DEFAULT NULL COMMENT '领导签字日期',
  `reject_reason`   varchar(500)  DEFAULT NULL COMMENT '驳回原因',
  `remark`          varchar(500)  DEFAULT NULL COMMENT '备注',
  `del_flag`        tinyint(1)    DEFAULT '0' COMMENT '删除标志',
  `create_by`       varchar(50)   DEFAULT NULL,
  `create_time`     datetime      DEFAULT CURRENT_TIMESTAMP,
  `update_by`       varchar(50)   DEFAULT NULL,
  `update_time`     datetime      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`review_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='质量评审单';

CREATE TABLE IF NOT EXISTS `qc_quality_review_coil` (
  `detail_id`       bigint NOT NULL AUTO_INCREMENT COMMENT '明细主键',
  `review_id`       bigint NOT NULL COMMENT '关联评审单ID',
  `group_seq`       int           DEFAULT NULL COMMENT '序号',
  `group_remark`    varchar(200)  DEFAULT NULL COMMENT '组备注',
  `coil_id`         bigint        DEFAULT NULL COMMENT '钢卷ID',
  `current_coil_no` varchar(50)   DEFAULT NULL COMMENT '产品卷号',
  `supplier_coil_no` varchar(50)  DEFAULT NULL COMMENT '原料卷号',
  `spec`            varchar(50)   DEFAULT NULL COMMENT '规格',
  `net_weight`      decimal(10,3) DEFAULT NULL COMMENT '卷重(t)',
  `defect_desc`     text          COMMENT '缺陷描述',
  `before_quality`  varchar(20)   DEFAULT NULL COMMENT '改判前质量等级',
  `regrade_quality` varchar(50)   DEFAULT NULL COMMENT '改判后质量状态',
  `execute_status`  tinyint(1)    DEFAULT '0' COMMENT '执行状态: 0=待执行 1=已执行',
  `execute_time`    datetime      DEFAULT NULL COMMENT '执行时间',
  `remark`          varchar(500)  DEFAULT NULL,
  `del_flag`        tinyint(1)    DEFAULT '0',
  `create_by`       varchar(50)   DEFAULT NULL,
  `create_time`     datetime      DEFAULT CURRENT_TIMESTAMP,
  `update_by`       varchar(50)   DEFAULT NULL,
  `update_time`     datetime      DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`detail_id`),
  KEY `idx_review_id` (`review_id`),
  KEY `idx_coil_id` (`coil_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='质量评审钢卷明细';

CREATE TABLE IF NOT EXISTS `qc_quality_review_log` (
  `log_id`       bigint NOT NULL AUTO_INCREMENT COMMENT '日志主键',
  `review_id`    bigint NOT NULL COMMENT '关联评审单ID',
  `action`       varchar(20) NOT NULL COMMENT '动作: submit/approve/reject',
  `operator`     varchar(50) DEFAULT NULL COMMENT '操作人',
  `opinion`      text        COMMENT '审批意见',
  `operate_time` datetime    DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
  `create_by`    varchar(50) DEFAULT NULL,
  `create_time`  datetime    DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`log_id`),
  KEY `idx_review_id` (`review_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='质量评审审批日志';

-- ========== 3. 字典 ==========
INSERT IGNORE INTO sys_dict_type (dict_name, dict_type, status, create_by, create_time, update_by, update_time, remark)
VALUES ('Quality Review Status', 'quality_review_status', '0', 'admin', NOW(), 'admin', NOW(), '');
INSERT IGNORE INTO sys_dict_type (dict_name, dict_type, status, create_by, create_time, update_by, update_time, remark)
VALUES ('Regrade Quality Type', 'regrade_quality_type', '0', 'admin', NOW(), 'admin', NOW(), '');

INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (1, '待提交', '1', 'quality_review_status', '', 'info', 'Y', '0', 'admin', NOW());
INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (2, '待审批', '2', 'quality_review_status', '', 'warning', 'Y', '0', 'admin', NOW());
INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (3, '已通过', '3', 'quality_review_status', '', 'success', 'Y', '0', 'admin', NOW());
INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (4, '已驳回', '4', 'quality_review_status', '', 'danger', 'Y', '0', 'admin', NOW());

INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (1, '协议销售', 'protocol_sale', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());
INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (2, '转分剪', 'to_slitting', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());
INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (3, '降级', 'downgrade', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());
INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (4, '返修', 'rework', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());
INSERT IGNORE INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time) VALUES (5, '报废', 'scrap', 'regrade_quality_type', '', '', 'Y', '0', 'admin', NOW());

-- ========== 4. 创建菜单 ==========
-- 父菜单: 质量模块 (path=quality, menu_id=2068954238598967297)
SET @parent_id = 2068954238598967297;
SET @max_order = (SELECT COALESCE(MAX(order_num), 0) FROM sys_menu WHERE parent_id = @parent_id);
SET @max_id = (SELECT COALESCE(MAX(menu_id), 0) FROM sys_menu);

SET @id1 = @max_id + 1;
SET @id2 = @max_id + 2;
SET @id3 = @max_id + 3;
SET @id4 = @max_id + 4;
SET @id5 = @max_id + 5;
SET @id6 = @max_id + 6;
SET @id7 = @max_id + 7;
SET @id8 = @max_id + 8;

-- 质量评审菜单 (type=C 菜单)
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES (@id1, '质量评审', @parent_id, @max_order + 1, 'qualityReview', 'mes/qc/qualityReview/index', 1, 0, 'C', '0', '0', 'qc:qualityReview:list', 'guide', 'admin', NOW());

-- 质量评审待审批 (type=C 隐藏)
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES (@id2, '质量评审待审批', @parent_id, @max_order + 2, 'qualityReviewTodo', 'mes/qc/qualityReview/todo', 1, 0, 'C', '1', '0', 'qc:qualityReview:approve', 'checkbox', 'admin', NOW());

-- 按钮权限 (type=F)
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id3, '质量评审新增', @id1, 1, 'F', '0', '0', 'qc:qualityReview:add', 'admin', NOW());
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id4, '质量评审修改', @id1, 2, 'F', '0', '0', 'qc:qualityReview:edit', 'admin', NOW());
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id5, '质量评审删除', @id1, 3, 'F', '0', '0', 'qc:qualityReview:delete', 'admin', NOW());
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id6, '质量评审提交送审', @id1, 4, 'F', '0', '0', 'qc:qualityReview:submit', 'admin', NOW());
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id7, '质量评审审批', @id1, 5, 'F', '0', '0', 'qc:qualityReview:approve', 'admin', NOW());
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id8, '质量评审执行改判', @id1, 6, 'F', '0', '0', 'qc:qualityReview:execute', 'admin', NOW());

-- ========== 5. 授权 admin 角色 ==========
INSERT IGNORE INTO sys_role_menu (role_id, menu_id) VALUES
(1, @parent_id), (1, @id1), (1, @id2),
(1, @id3), (1, @id4), (1, @id5),
(1, @id6), (1, @id7), (1, @id8);

-- ========== 6. 验证 ==========
SELECT CONCAT('Tables: ', COUNT(*)) AS result FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE 'qc_quality_review%';
SELECT CONCAT('Dict: ', dict_type) AS result FROM sys_dict_type WHERE dict_type = 'quality_review_status';
SELECT CONCAT('Menu: ', menu_name, ' -> parent_id=', parent_id) AS result FROM sys_menu WHERE perms = 'qc:qualityReview:list';
