-- MES 设备小部位：建表 + 菜单 + 按钮权限 + 租户 admin 授权（可整文件一次执行）
-- 权限前缀与 Controller、前端 v-auth 一致：mes:mes_xsl_equipment_sub_part:*
-- 依赖：mes_xsl_equipment_category、mes_xsl_equipment_part；二级菜单父级为「MES XSL / XSLMES管理」（sys_permission.id 1900000000000000300 或按名称解析）
-- 修改租户改 SET @mes_tenant_id；已跑过 Flyway 的旧库可另执行 V3.9.2_60 仅改 parent_id
SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `mes_xsl_equipment_sub_part` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `equipment_category_id` varchar(32) NOT NULL COMMENT '所属设备类别主键 mes_xsl_equipment_category.id',
  `equipment_category_name` varchar(128) DEFAULT NULL COMMENT '设备类别名称冗余展示',
  `equipment_part_id` varchar(32) NOT NULL COMMENT '所属设备大部位主键 mes_xsl_equipment_part.id',
  `equipment_part_name` varchar(128) DEFAULT NULL COMMENT '设备大部位名称冗余（mes_xsl_equipment_part.part_name）',
  `sub_part_name` varchar(128) NOT NULL COMMENT '小部位名称',
  `sub_part_code` varchar(64) NOT NULL COMMENT '小部位代码（同租户未删除数据中唯一）',
  `sub_part_description` varchar(500) DEFAULT NULL COMMENT '部位描述',
  `enable_status` varchar(1) NOT NULL DEFAULT '0' COMMENT '是否启用（字典xslmes_unit_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_mesp_tenant_subcode` (`tenant_id`, `sub_part_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES设备小部位';

SET @mes_tenant_id = 1002;

SET @xslmes_root_pid = (
  SELECT MIN(`id`) FROM `sys_permission`
  WHERE `del_flag` = 0 AND `menu_type` = 0 AND `name` IN ('MES XSL', 'XSLMES管理', 'XSLMES')
);
SET @xslmes_root_pid = IFNULL(@xslmes_root_pid, '1900000000000000300');

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 ('1860000000000000091', @xslmes_root_pid, '设备小部位', '/xslmes/mesXslEquipmentSubPart', 'xslmes/mesXslEquipmentSubPart/MesXslEquipmentSubPartList', NULL, 1, NULL, '1', 14, 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`);

INSERT INTO `sys_permission`(`id`, `parent_id`, `name`, `menu_type`, `perms`, `perms_type`, `status`, `del_flag`, `create_by`, `create_time`) VALUES
('1860000000000000092', '1860000000000000091', '新增', 2, 'mes:mes_xsl_equipment_sub_part:add', '1', '1', 0, 'admin', NOW()),
('1860000000000000093', '1860000000000000091', '编辑', 2, 'mes:mes_xsl_equipment_sub_part:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000000094', '1860000000000000091', '删除', 2, 'mes:mes_xsl_equipment_sub_part:delete', '1', '1', 0, 'admin', NOW()),
('1860000000000000095', '1860000000000000091', '批量删除', 2, 'mes:mes_xsl_equipment_sub_part:deleteBatch', '1', '1', 0, 'admin', NOW()),
('1860000000000000096', '1860000000000000091', '导出', 2, 'mes:mes_xsl_equipment_sub_part:exportXls', '1', '1', 0, 'admin', NOW()),
('1860000000000000097', '1860000000000000091', '导入', 2, 'mes:mes_xsl_equipment_sub_part: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 (
    '1860000000000000091',
    '1860000000000000092', '1860000000000000093', '1860000000000000094', '1860000000000000095',
    '1860000000000000096', '1860000000000000097'
  )
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_permission` rp
    WHERE rp.`role_id` = r.`id` AND rp.`permission_id` = p.`id`
  );
