-- 胶料快检实验标准（主子表）：字典 + 建表 + 菜单（质量管理下）+ 按钮 + 租户 admin 授权
-- 权限前缀：mes:mes_xsl_rubber_quick_test_std:*
-- 菜单 ID 段 1860000000000000184（实验方法占用 177-183）
-- 可与 Flyway V3.9.2_103 重复执行（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_mixer_type', '1普通密炼机2串密炼机', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_mixer_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_mixer_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_mixer_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_std_enable_status', '1使用中0已停用', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_std_enable_status' 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_std_enable_status' 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, '已停用', '0', 2, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_rubber_quick_test_std_enable_status' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = '0');

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_std_audit_status', '0草稿1已批准', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_std_audit_status' 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, '草稿', '0', 1, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_rubber_quick_test_std_audit_status' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = '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', 2, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_rubber_quick_test_std_audit_status' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = '1');

CREATE TABLE IF NOT EXISTS `mes_xsl_rubber_quick_test_std` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `std_name` varchar(128) NOT NULL COMMENT '实验标准名称（同租户未删除唯一）',
  `test_method_id` varchar(32) NOT NULL COMMENT '实验方法 mes_xsl_rubber_quick_test_method.id',
  `test_method_name` varchar(128) DEFAULT NULL COMMENT '实验方法名称冗余',
  `mixer_type` varchar(2) DEFAULT NULL COMMENT '密炼机类型（字典xslmes_rubber_quick_test_mixer_type：1普通密炼机2串密炼机）',
  `rubber_material_id` varchar(32) DEFAULT NULL COMMENT '胶料 mes_material.id',
  `rubber_material_name` varchar(128) DEFAULT NULL COMMENT '胶料名称冗余',
  `ps_compile_id` varchar(32) DEFAULT NULL COMMENT '密炼PS编制 mes_xsl_mixer_ps_compile.id',
  `issue_number` varchar(100) DEFAULT NULL COMMENT '发行编号（密炼PS编码冗余）',
  `issue_date` date DEFAULT NULL COMMENT '发行日期',
  `issue_dept_id` varchar(32) DEFAULT NULL COMMENT '发行部门ID',
  `issue_dept_name` varchar(200) DEFAULT NULL COMMENT '发行部门名称冗余',
  `enable_status` varchar(2) DEFAULT '1' COMMENT '启用状态（字典xslmes_rubber_quick_test_std_enable_status：1使用中0已停用）',
  `audit_status` varchar(2) DEFAULT '0' COMMENT '审核状态（字典xslmes_rubber_quick_test_std_audit_status：0草稿1已批准）',
  `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_mrqts_method` (`test_method_id`),
  KEY `idx_mrqts_material` (`rubber_material_id`),
  KEY `idx_mrqts_tenant` (`tenant_id`),
  UNIQUE KEY `uk_mrqts_tenant_name_del` (`tenant_id`, `std_name`, `del_flag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES胶料快检实验标准';

CREATE TABLE IF NOT EXISTS `mes_xsl_rubber_quick_test_std_line` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `std_id` varchar(32) NOT NULL COMMENT '主表 mes_xsl_rubber_quick_test_std.id',
  `data_point_id` varchar(32) NOT NULL COMMENT '数据点 mes_xsl_rubber_quick_test_data_point.id',
  `point_name` varchar(128) DEFAULT NULL COMMENT '数据点名称冗余（只读带出）',
  `lower_limit` decimal(18,6) DEFAULT NULL COMMENT '下限值',
  `lower_warn` decimal(18,6) DEFAULT NULL COMMENT '下警告值',
  `target_value` decimal(18,6) DEFAULT NULL COMMENT '目标值',
  `upper_warn` decimal(18,6) DEFAULT NULL COMMENT '上警告值',
  `upper_limit` decimal(18,6) DEFAULT NULL COMMENT '上限值',
  `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_mrqtsl_std` (`std_id`),
  UNIQUE KEY `uk_mrqtsl_std_point` (`std_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 ('1860000000000000184', @mes_quality_pid, '胶料快检实验标准', '/xslmes/mesXslRubberQuickTestStd', 'xslmes/mesXslRubberQuickTestStd/MesXslRubberQuickTestStdList', 'MesXslRubberQuickTestStdList', 1, NULL, '1', 4, 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`), `sort_no` = VALUES(`sort_no`), `is_leaf` = VALUES(`is_leaf`), `keep_alive` = VALUES(`keep_alive`);

UPDATE `sys_permission` SET `icon` = 'ant-design:file-protect-outlined' WHERE `id` = '1860000000000000184' AND `del_flag` = 0;

INSERT INTO `sys_permission`(`id`, `parent_id`, `name`, `menu_type`, `perms`, `perms_type`, `status`, `del_flag`, `create_by`, `create_time`) VALUES
('1860000000000000185', '1860000000000000184', '新增', 2, 'mes:mes_xsl_rubber_quick_test_std:add', '1', '1', 0, 'admin', NOW()),
('1860000000000000186', '1860000000000000184', '编辑', 2, 'mes:mes_xsl_rubber_quick_test_std:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000000187', '1860000000000000184', '删除', 2, 'mes:mes_xsl_rubber_quick_test_std:delete', '1', '1', 0, 'admin', NOW()),
('1860000000000000188', '1860000000000000184', '批量删除', 2, 'mes:mes_xsl_rubber_quick_test_std:deleteBatch', '1', '1', 0, 'admin', NOW()),
('1860000000000000189', '1860000000000000184', '导出', 2, 'mes:mes_xsl_rubber_quick_test_std:exportXls', '1', '1', 0, 'admin', NOW()),
('1860000000000000190', '1860000000000000184', '导入', 2, 'mes:mes_xsl_rubber_quick_test_std:importExcel', '1', '1', 0, 'admin', NOW()),
('1860000000000000191', '1860000000000000184', '启用停用', 2, 'mes:mes_xsl_rubber_quick_test_std:updateStatus', '1', '1', 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE `perms` = VALUES(`perms`), `name` = VALUES(`name`);

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 (
    '1860000000000000184',
    '1860000000000000185', '1860000000000000186', '1860000000000000187', '1860000000000000188',
    '1860000000000000189', '1860000000000000190', '1860000000000000191'
  )
  AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission rp
    WHERE rp.role_id = r.id AND rp.permission_id = p.id
  );
