-- 胶料快检实验方法（主子表）：字典 + 建表 + 菜单（质量管理下）+ 按钮 + 租户 admin 授权

-- 权限前缀：mes:mes_xsl_rubber_quick_test_method:*

-- 菜单 ID 段 1860000000000000177（与数据点 170 段区分）

-- 可与 Flyway V3.9.2_102 重复执行（ON DUPLICATE / IF NOT EXISTS）

-- SET @mes_tenant_id：多租户 admin 授权目标租户

SET NAMES utf8mb4;



INSERT INTO `sys_dict` (`id`, `dict_name`, `dict_code`, `description`, `del_flag`, `create_by`, `create_time`, `type`, `tenant_id`)

SELECT REPLACE(UUID(), '-', ''), 'MES胶料快检转子类型', 'xslmes_rubber_quick_test_rotor_type', '1大转子2小转子', 0, 'admin', NOW(), 0, 0

WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_rotor_type' AND `del_flag` = 0);



INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)

SELECT REPLACE(UUID(), '-', ''), d.id, '大转子', '1', 1, 1, 'admin', NOW() FROM `sys_dict` d

WHERE d.`dict_code` = 'xslmes_rubber_quick_test_rotor_type' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = '1');

INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)

SELECT REPLACE(UUID(), '-', ''), d.id, '小转子', '2', 2, 1, 'admin', NOW() FROM `sys_dict` d

WHERE d.`dict_code` = 'xslmes_rubber_quick_test_rotor_type' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = '2');



INSERT INTO `sys_dict` (`id`, `dict_name`, `dict_code`, `description`, `del_flag`, `create_by`, `create_time`, `type`, `tenant_id`)

SELECT REPLACE(UUID(), '-', ''), 'MES胶料快检扭矩单位', 'xslmes_rubber_quick_test_torque_unit', 'Ib.in、dNm、kg.cm、Nm、mdm', 0, 'admin', NOW(), 0, 0

WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_torque_unit' AND `del_flag` = 0);



INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)

SELECT REPLACE(UUID(), '-', ''), d.id, v.txt, v.val, v.ord, 1, 'admin', NOW()

FROM `sys_dict` d

CROSS JOIN (

  SELECT 'Ib.in' AS txt, 'Ib.in' AS val, 1 AS ord UNION ALL

  SELECT 'dNm', 'dNm', 2 UNION ALL

  SELECT 'kg.cm', 'kg.cm', 3 UNION ALL

  SELECT 'Nm', 'Nm', 4 UNION ALL

  SELECT 'mdm', 'mdm', 5

) v

WHERE d.`dict_code` = 'xslmes_rubber_quick_test_torque_unit'

  AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = v.val);



INSERT INTO `sys_dict` (`id`, `dict_name`, `dict_code`, `description`, `del_flag`, `create_by`, `create_time`, `type`, `tenant_id`)

SELECT REPLACE(UUID(), '-', ''), 'MES胶料快检时间单位', 'xslmes_rubber_quick_test_time_unit', 'sec、min、m:s', 0, 'admin', NOW(), 0, 0

WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_time_unit' AND `del_flag` = 0);



INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)

SELECT REPLACE(UUID(), '-', ''), d.id, v.txt, v.val, v.ord, 1, 'admin', NOW()

FROM `sys_dict` d

CROSS JOIN (

  SELECT 'sec' AS txt, 'sec' AS val, 1 AS ord UNION ALL

  SELECT 'min', 'min', 2 UNION ALL

  SELECT 'm:s', 'm:s', 3

) v

WHERE d.`dict_code` = 'xslmes_rubber_quick_test_time_unit'

  AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = v.val);



INSERT INTO `sys_dict` (`id`, `dict_name`, `dict_code`, `description`, `del_flag`, `create_by`, `create_time`, `type`, `tenant_id`)

SELECT REPLACE(UUID(), '-', ''), 'MES胶料快检门尼单位', 'xslmes_rubber_quick_test_mooney_unit', 'MU', 0, 'admin', NOW(), 0, 0

WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_mooney_unit' AND `del_flag` = 0);



INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)

SELECT REPLACE(UUID(), '-', ''), d.id, 'MU', 'MU', 1, 1, 'admin', NOW() FROM `sys_dict` d

WHERE d.`dict_code` = 'xslmes_rubber_quick_test_mooney_unit'

  AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'MU');



CREATE TABLE IF NOT EXISTS `mes_xsl_rubber_quick_test_method` (

  `id` varchar(32) NOT NULL COMMENT '主键',

  `method_code` varchar(16) NOT NULL COMMENT '方法编号（租户内从001递增自动生成）',

  `method_name` varchar(128) NOT NULL COMMENT '实验方法名称（同租户未删除唯一）',

  `quick_test_type_id` varchar(32) NOT NULL COMMENT '实验类型 mes_xsl_rubber_quick_test_type.id',

  `quick_test_type_name` varchar(128) DEFAULT NULL COMMENT '实验类型名称冗余',

  `test_temp_c` decimal(12,2) DEFAULT NULL COMMENT '实验温度°C',

  `preheat_time_min` decimal(12,2) DEFAULT NULL COMMENT '预热时间min',

  `test_time_min` decimal(12,2) DEFAULT NULL COMMENT '实验时间min',

  `test_angle_deg` decimal(12,2) DEFAULT NULL COMMENT '实验角度Deg',

  `test_freq_hz` decimal(12,2) DEFAULT NULL COMMENT '实验频率Hz',

  `rotor_type` varchar(2) DEFAULT NULL COMMENT '转子类型（字典xslmes_rubber_quick_test_rotor_type：1大转子2小转子）',

  `rotor_speed_rpm` decimal(12,2) DEFAULT NULL COMMENT '转子速度rpm',

  `method_desc` varchar(500) DEFAULT NULL COMMENT '方法描述',

  `tenant_id` int DEFAULT NULL COMMENT '租户',

  `sys_org_code` varchar(64) DEFAULT NULL COMMENT '部门',

  `create_by` varchar(32) DEFAULT NULL COMMENT '创建人',

  `create_time` datetime DEFAULT NULL COMMENT '创建时间',

  `update_by` varchar(32) DEFAULT NULL COMMENT '更新人',

  `update_time` datetime DEFAULT NULL COMMENT '更新时间',

  `del_flag` int DEFAULT '0' COMMENT '删除标记（0正常1删除）',

  PRIMARY KEY (`id`),

  KEY `idx_mrqtm_tenant_code` (`tenant_id`, `method_code`),

  KEY `idx_mrqtm_type` (`quick_test_type_id`),

  UNIQUE KEY `uk_mrqtm_tenant_name_del` (`tenant_id`, `method_name`, `del_flag`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES胶料快检实验方法';



CREATE TABLE IF NOT EXISTS `mes_xsl_rubber_quick_test_method_line` (

  `id` varchar(32) NOT NULL COMMENT '主键',

  `method_id` varchar(32) NOT NULL COMMENT '主表 mes_xsl_rubber_quick_test_method.id',

  `data_point_id` varchar(32) NOT NULL COMMENT '数据点 mes_xsl_rubber_quick_test_data_point.id',

  `point_name` varchar(128) DEFAULT NULL COMMENT '数据点名称冗余（只读带出）',

  `unit_type` varchar(64) DEFAULT NULL COMMENT '单位类型冗余（只读带出）',

  `torque_unit_type` varchar(32) DEFAULT NULL COMMENT '扭矩单位类型（字典xslmes_rubber_quick_test_torque_unit）',

  `time_unit_type` varchar(32) DEFAULT NULL COMMENT '时间单位类型（字典xslmes_rubber_quick_test_time_unit）',

  `mooney_unit_type` varchar(32) DEFAULT NULL COMMENT '门尼单位类型（字典xslmes_rubber_quick_test_mooney_unit）',

  `sort_no` int DEFAULT NULL COMMENT '排序号',

  `create_by` varchar(32) DEFAULT NULL COMMENT '创建人',

  `create_time` datetime DEFAULT NULL COMMENT '创建时间',

  `update_by` varchar(32) DEFAULT NULL COMMENT '更新人',

  `update_time` datetime DEFAULT NULL COMMENT '更新时间',

  PRIMARY KEY (`id`),

  KEY `idx_mrqtml_method` (`method_id`),

  UNIQUE KEY `uk_mrqtml_method_point` (`method_id`, `data_point_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES胶料快检实验方法明细';



SET @mes_tenant_id = 1002;

SET @mes_quality_pid = IFNULL(

  (SELECT `id` FROM `sys_permission` WHERE `del_flag` = 0 AND `menu_type` = 0 AND `name` = '质量管理' LIMIT 1),

  '1860000000000000162'

);



INSERT INTO `sys_permission`(`id`, `parent_id`, `name`, `url`, `component`, `component_name`, `menu_type`, `perms`, `perms_type`, `sort_no`, `is_route`, `is_leaf`, `hidden`, `status`, `del_flag`, `keep_alive`, `internal_or_external`, `create_by`, `create_time`)

VALUES ('1860000000000000177', @mes_quality_pid, '胶料快检实验方法', '/xslmes/mesXslRubberQuickTestMethod', 'xslmes/mesXslRubberQuickTestMethod/MesXslRubberQuickTestMethodList', 'MesXslRubberQuickTestMethodList', 1, NULL, '1', 3, 1, 0, 0, '1', 0, 1, 0, 'admin', NOW())

ON DUPLICATE KEY UPDATE

`parent_id` = VALUES(`parent_id`), `name` = VALUES(`name`), `url` = VALUES(`url`), `component` = VALUES(`component`), `component_name` = VALUES(`component_name`),

`menu_type` = VALUES(`menu_type`), `perms` = VALUES(`perms`), `perms_type` = VALUES(`perms_type`), `sort_no` = VALUES(`sort_no`),

`is_route` = VALUES(`is_route`), `is_leaf` = 0, `hidden` = 0, `status` = '1', `del_flag` = 0,

`keep_alive` = VALUES(`keep_alive`), `internal_or_external` = VALUES(`internal_or_external`);



UPDATE `sys_permission` SET `icon` = 'ant-design:profile-outlined' WHERE `id` = '1860000000000000177' AND `del_flag` = 0;



INSERT INTO `sys_permission`(`id`, `parent_id`, `name`, `menu_type`, `perms`, `perms_type`, `is_leaf`, `status`, `del_flag`, `create_by`, `create_time`) VALUES

('1860000000000000178', '1860000000000000177', '新增', 2, 'mes:mes_xsl_rubber_quick_test_method:add', '1', 1, '1', 0, 'admin', NOW()),

('1860000000000000179', '1860000000000000177', '编辑', 2, 'mes:mes_xsl_rubber_quick_test_method:edit', '1', 1, '1', 0, 'admin', NOW()),

('1860000000000000180', '1860000000000000177', '删除', 2, 'mes:mes_xsl_rubber_quick_test_method:delete', '1', 1, '1', 0, 'admin', NOW()),

('1860000000000000181', '1860000000000000177', '批量删除', 2, 'mes:mes_xsl_rubber_quick_test_method:deleteBatch', '1', 1, '1', 0, 'admin', NOW()),

('1860000000000000182', '1860000000000000177', '导出', 2, 'mes:mes_xsl_rubber_quick_test_method:exportXls', '1', 1, '1', 0, 'admin', NOW()),

('1860000000000000183', '1860000000000000177', '导入', 2, 'mes:mes_xsl_rubber_quick_test_method:importExcel', '1', 1, '1', 0, 'admin', NOW())

ON DUPLICATE KEY UPDATE

`parent_id` = VALUES(`parent_id`), `name` = VALUES(`name`), `menu_type` = VALUES(`menu_type`), `perms` = VALUES(`perms`), `perms_type` = VALUES(`perms_type`),

`is_leaf` = 1, `status` = '1', `del_flag` = 0;



INSERT INTO `sys_role_permission`(`id`, `role_id`, `permission_id`, `operate_date`, `operate_ip`)

SELECT REPLACE(UUID(), '-', ''), r.`id`, p.`id`, NOW(), '127.0.0.1'

FROM `sys_role` r

CROSS JOIN `sys_permission` p

WHERE r.`tenant_id` = @mes_tenant_id

  AND r.`role_code` = 'admin'

  AND p.`id` IN (

    '1860000000000000177',

    '1860000000000000178', '1860000000000000179', '1860000000000000180', '1860000000000000181',

    '1860000000000000182', '1860000000000000183'

  )

  AND NOT EXISTS (

    SELECT 1 FROM `sys_role_permission` rp

    WHERE rp.`role_id` = r.`id` AND rp.`permission_id` = p.`id`

  );

