-- 胶料快检记录（主子表）：字典 + 建表 + 菜单（质量管理下）+ 按钮 + 胶料信息「检验」按钮 + 租户 admin 授权
-- 权限前缀：mes:mes_xsl_rubber_quick_test_record:*
-- 菜单 ID 段 1860000000000000192
-- 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_record_result', '1合格0不合格', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_record_result' 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_record_result' 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_record_result' 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_work_shift', '班次', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_work_shift' 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 '早班' AS txt, '1' AS val, 1 AS ord UNION ALL
  SELECT '中班', '2', 2 UNION ALL
  SELECT '晚班', '3', 3
) v
WHERE d.`dict_code` = 'xslmes_rubber_quick_test_work_shift'
  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_work_team', '班组', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_rubber_quick_test_work_team' 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 '甲班' AS txt, '1' AS val, 1 AS ord UNION ALL
  SELECT '乙班', '2', 2 UNION ALL
  SELECT '丙班', '3', 3
) v
WHERE d.`dict_code` = 'xslmes_rubber_quick_test_work_team'
  AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = v.val);

CREATE TABLE IF NOT EXISTS `mes_xsl_rubber_quick_test_record` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `record_no` varchar(32) DEFAULT NULL COMMENT '单号（JL+日期+4位流水，如JL202605280001）',
  `rubber_material_id` varchar(32) DEFAULT NULL COMMENT '胶料 mes_material.id',
  `rubber_material_name` varchar(128) DEFAULT NULL COMMENT '胶料名称冗余',
  `std_id` varchar(32) DEFAULT NULL COMMENT '引用的实验标准 mes_xsl_rubber_quick_test_std.id',
  `prod_equipment_ledger_id` varchar(32) DEFAULT NULL COMMENT '生产机台 mes_xsl_equipment_ledger.id',
  `prod_equipment_name` varchar(128) DEFAULT NULL COMMENT '生产机台名称冗余',
  `production_date` date DEFAULT NULL COMMENT '生产日期',
  `train_no` varchar(64) DEFAULT NULL COMMENT '车次编号',
  `work_shift` varchar(8) DEFAULT NULL COMMENT '班次（字典xslmes_rubber_quick_test_work_shift）',
  `work_team` varchar(8) DEFAULT NULL COMMENT '班组（字典xslmes_rubber_quick_test_work_team）',
  `inspect_times` int DEFAULT NULL COMMENT '检验次数',
  `inspect_time` datetime DEFAULT NULL COMMENT '检验时间',
  `inspector_user_id` varchar(32) DEFAULT NULL COMMENT '检验人用户ID',
  `inspector_username` varchar(64) DEFAULT NULL COMMENT '检验人账号冗余',
  `inspector_realname` varchar(64) DEFAULT NULL COMMENT '检验人姓名冗余',
  `quick_test_type_id` varchar(32) DEFAULT NULL COMMENT '检验类型 mes_xsl_rubber_quick_test_type.id',
  `quick_test_type_name` varchar(128) DEFAULT NULL COMMENT '检验类型名称冗余',
  `inspect_result` varchar(2) DEFAULT NULL COMMENT '检验结果（字典xslmes_rubber_quick_test_record_result：1合格0不合格）',
  `production_plan_no` varchar(100) DEFAULT NULL COMMENT '生产计划号',
  `inspect_equipment_ledger_id` varchar(32) DEFAULT NULL COMMENT '检验机台 mes_xsl_equipment_ledger.id',
  `inspect_equipment_name` varchar(128) DEFAULT NULL COMMENT '检验机台名称冗余',
  `rubber_card_no` varchar(100) DEFAULT NULL COMMENT '胶料卡片号',
  `rubber_batch_no` varchar(100) 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`),
  UNIQUE KEY `uk_mrqtr_record_no` (`record_no`),
  KEY `idx_mrqtr_material` (`rubber_material_id`),
  KEY `idx_mrqtr_std` (`std_id`),
  KEY `idx_mrqtr_tenant` (`tenant_id`),
  KEY `idx_mrqtr_inspect_time` (`inspect_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES胶料快检记录';

CREATE TABLE IF NOT EXISTS `mes_xsl_rubber_quick_test_record_line` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `record_id` varchar(32) NOT NULL COMMENT '主表 mes_xsl_rubber_quick_test_record.id',
  `data_point_id` varchar(32) DEFAULT NULL COMMENT '数据点 mes_xsl_rubber_quick_test_data_point.id',
  `inspect_item` varchar(128) DEFAULT NULL COMMENT '检验项目（数据点名称，只读带出）',
  `lower_limit` decimal(18,6) DEFAULT NULL COMMENT '检验下限（只读带出）',
  `inspect_value` 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_mrqtrl_record` (`record_id`),
  UNIQUE KEY `uk_mrqtrl_record_point` (`record_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 ('1860000000000000192', @mes_quality_pid, '胶料快检记录', '/xslmes/mesXslRubberQuickTestRecord', 'xslmes/mesXslRubberQuickTestRecord/MesXslRubberQuickTestRecordList', 'MesXslRubberQuickTestRecordList', 1, NULL, '1', 5, 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-search-outlined' WHERE `id` = '1860000000000000192' 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
('1860000000000000193', '1860000000000000192', '新增', 2, 'mes:mes_xsl_rubber_quick_test_record:add', '1', '1', 0, 'admin', NOW()),
('1860000000000000194', '1860000000000000192', '编辑', 2, 'mes:mes_xsl_rubber_quick_test_record:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000000195', '1860000000000000192', '删除', 2, 'mes:mes_xsl_rubber_quick_test_record:delete', '1', '1', 0, 'admin', NOW()),
('1860000000000000196', '1860000000000000192', '批量删除', 2, 'mes:mes_xsl_rubber_quick_test_record:deleteBatch', '1', '1', 0, 'admin', NOW()),
('1860000000000000197', '1860000000000000192', '导出', 2, 'mes:mes_xsl_rubber_quick_test_record:exportXls', '1', '1', 0, 'admin', NOW()),
('1860000000000000198', '1860000000000000192', '导入', 2, 'mes:mes_xsl_rubber_quick_test_record:importExcel', '1', '1', 0, 'admin', NOW()),
('1860000000000000199', '1860000000000000192', '从胶料生成', 2, 'mes:mes_xsl_rubber_quick_test_record:batchFromMaterial', '1', '1', 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE `perms` = VALUES(`perms`), `name` = VALUES(`name`);

INSERT INTO `sys_permission`(`id`, `parent_id`, `name`, `menu_type`, `perms`, `perms_type`, `status`, `del_flag`, `create_by`, `create_time`) VALUES
('1860000000000000200', '1860000000000000011', '胶料快检', 2, 'mes:mes_material:rubberQuickTestInspect', '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 (
    '1860000000000000192',
    '1860000000000000193', '1860000000000000194', '1860000000000000195', '1860000000000000196',
    '1860000000000000197', '1860000000000000198', '1860000000000000199',
    '1860000000000000200'
  )
  AND NOT EXISTS (
    SELECT 1 FROM sys_role_permission rp
    WHERE rp.role_id = r.id AND rp.permission_id = p.id
  );
