-- MES 点检及保养项目：字典 + 建表 + 菜单 + 按钮 + 租户 admin 授权（可整文件一次执行）
-- 权限前缀与 Controller、前端 v-auth 一致：mes:mes_xsl_inspect_maintain_item:*
-- 父菜单：设备管理；修改租户改 SET @mes_tenant_id
-- 新环境也可依赖 Flyway：V3.9.2_77__mes_xsl_inspect_maintain_item.sql
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_im_item_category', 'inspect点检/maintain保养', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_im_item_category' 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, '点检', 'inspect', 1, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_item_category' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'inspect');
INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.id, '保养', 'maintain', 2, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_item_category' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'maintain');

INSERT INTO `sys_dict` (`id`, `dict_name`, `dict_code`, `description`, `del_flag`, `create_by`, `create_time`, `type`, `tenant_id`)
SELECT REPLACE(UUID(), '-', ''), 'MES点检保养项目类型', 'xslmes_im_item_type', 'mechanical机械类/electrical电气类', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_im_item_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, '机械类', 'mechanical', 1, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_item_type' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'mechanical');
INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.id, '电气类', 'electrical', 2, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_item_type' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'electrical');

INSERT INTO `sys_dict` (`id`, `dict_name`, `dict_code`, `description`, `del_flag`, `create_by`, `create_time`, `type`, `tenant_id`)
SELECT REPLACE(UUID(), '-', ''), 'MES点检方式', 'xslmes_im_inspect_method', 'visual视觉/sight目测/hearing听觉', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_im_inspect_method' 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, '视觉', 'visual', 1, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_inspect_method' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'visual');
INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.id, '目测', 'sight', 2, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_inspect_method' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'sight');
INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.id, '听觉', 'hearing', 3, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_inspect_method' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'hearing');

CREATE TABLE IF NOT EXISTS `mes_xsl_inspect_maintain_item` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `item_name` varchar(500) NOT NULL COMMENT '项目名称（同租户未删除数据中唯一）',
  `item_code` varchar(500) NOT NULL COMMENT '项目编号（同租户未删除数据中唯一）',
  `equipment_category_id` varchar(32) NOT NULL COMMENT '设备类别主键 mes_xsl_equipment_category.id',
  `equipment_category_name` varchar(500) DEFAULT NULL COMMENT '设备类别名称冗余',
  `equipment_type_id` varchar(32) NOT NULL COMMENT '设备类型主键 mes_xsl_equipment_type.id',
  `equipment_type_name` varchar(500) DEFAULT NULL COMMENT '设备类型名称冗余',
  `equipment_part_id` varchar(32) NOT NULL COMMENT '设备部位主键 mes_xsl_equipment_part.id',
  `equipment_part_name` varchar(500) DEFAULT NULL COMMENT '设备部位名称冗余',
  `equipment_sub_part_id` varchar(32) NOT NULL COMMENT '设备小部位主键 mes_xsl_equipment_sub_part.id',
  `equipment_sub_part_name` varchar(500) DEFAULT NULL COMMENT '设备小部位名称冗余',
  `item_category` varchar(500) NOT NULL COMMENT '项目类别（字典xslmes_im_item_category：inspect点检/maintain保养）',
  `item_type` varchar(500) NOT NULL COMMENT '项目类型（字典xslmes_im_item_type：mechanical机械类/electrical电气类）',
  `inspect_method` varchar(500) NOT NULL COMMENT '点检方式（字典xslmes_im_inspect_method：visual视觉/sight目测/hearing听觉）',
  `judgment_criteria` varchar(500) NOT NULL COMMENT '判断基准',
  `maintain_cycle_days` int DEFAULT NULL COMMENT '保养周期（天）',
  `tenant_id` int DEFAULT NULL COMMENT '租户',
  `sys_org_code` varchar(500) DEFAULT NULL COMMENT '部门',
  `create_by` varchar(500) DEFAULT NULL COMMENT '创建人',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(500) DEFAULT NULL COMMENT '更新人',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `del_flag` int DEFAULT '0' COMMENT '删除标记（0正常1删除）',
  PRIMARY KEY (`id`),
  KEY `idx_mimi_tenant_name` (`tenant_id`, `item_name`),
  KEY `idx_mimi_tenant_code` (`tenant_id`, `item_code`),
  KEY `idx_mimi_category` (`equipment_category_id`),
  KEY `idx_mimi_type` (`equipment_type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES点检及保养项目';

SET @mes_tenant_id = 1002;

SET @mes_equip_pid = (
  SELECT `id` FROM `sys_permission`
  WHERE `del_flag` = 0 AND `menu_type` = 0 AND `name` = '设备管理'
  LIMIT 1
);
SET @mes_equip_pid = IFNULL(@mes_equip_pid, '1860000000000000133');

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 ('1860000000000000141', @mes_equip_pid, '点检及保养项目', '/xslmes/mesXslInspectMaintainItem', 'xslmes/mesXslInspectMaintainItem/MesXslInspectMaintainItemList', NULL, 1, NULL, '1', 11, 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` = VALUES(`is_leaf`), `hidden` = VALUES(`hidden`), `status` = VALUES(`status`), `del_flag` = VALUES(`del_flag`),
`keep_alive` = VALUES(`keep_alive`), `internal_or_external` = VALUES(`internal_or_external`), `icon` = 'ant-design:audit-outlined';

UPDATE `sys_permission` SET `icon` = 'ant-design:audit-outlined' WHERE `id` = '1860000000000000141' 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
('1860000000000000142', '1860000000000000141', '新增', 2, 'mes:mes_xsl_inspect_maintain_item:add', '1', '1', 0, 'admin', NOW()),
('1860000000000000143', '1860000000000000141', '编辑', 2, 'mes:mes_xsl_inspect_maintain_item:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000000144', '1860000000000000141', '删除', 2, 'mes:mes_xsl_inspect_maintain_item:delete', '1', '1', 0, 'admin', NOW()),
('1860000000000000145', '1860000000000000141', '批量删除', 2, 'mes:mes_xsl_inspect_maintain_item:deleteBatch', '1', '1', 0, 'admin', NOW()),
('1860000000000000146', '1860000000000000141', '导出', 2, 'mes:mes_xsl_inspect_maintain_item:exportXls', '1', '1', 0, 'admin', NOW()),
('1860000000000000147', '1860000000000000141', '导入', 2, 'mes:mes_xsl_inspect_maintain_item:importExcel', '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`),
`status` = VALUES(`status`), `del_flag` = VALUES(`del_flag`);

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 (
    '1860000000000000141',
    '1860000000000000142', '1860000000000000143', '1860000000000000144', '1860000000000000145',
    '1860000000000000146', '1860000000000000147'
  )
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_permission` rp
    WHERE rp.`role_id` = r.`id` AND rp.`permission_id` = p.`id`
  );
