-- MES 备品件信息：建表 + 菜单 + 按钮权限 + 租户 admin 授权（可整文件一次执行）
-- 权限前缀与 Controller、前端 v-auth 一致：mes:mes_xsl_spare_part:*
-- 依赖：mes_xsl_spare_parts_category、mes_xsl_unit；父菜单 MES基础资料 / MES资料；修改租户改 SET @mes_tenant_id
SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `mes_xsl_spare_part` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `spare_part_name` varchar(128) NOT NULL COMMENT '备品件名称（同租户未删除数据中唯一）',
  `spare_parts_category_id` varchar(32) NOT NULL COMMENT '所属备品件类别 mes_xsl_spare_parts_category.id',
  `spare_parts_category_name` varchar(128) DEFAULT NULL COMMENT '备品件类别名称冗余',
  `spec_model` varchar(200) DEFAULT NULL COMMENT '规格型号',
  `max_stock` decimal(18,4) NOT NULL DEFAULT '0.0000' COMMENT '最大库存',
  `min_stock` decimal(18,4) NOT NULL DEFAULT '0.0000' COMMENT '最小库存',
  `unit_id` varchar(36) NOT NULL COMMENT '单位 mes_xsl_unit.id',
  `unit_name` 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`),
  KEY `idx_msp_tenant_name` (`tenant_id`, `spare_part_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES备品件信息';

SET @mes_tenant_id = 1002;

SET @mes_base_pid = (
  SELECT MIN(`id`) FROM `sys_permission`
  WHERE `del_flag` = 0 AND `menu_type` = 0 AND `name` IN ('MES基础资料', 'MES资料')
);
SET @mes_base_pid = IFNULL(@mes_base_pid, '1860000000000000001');

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 ('1860000000000000105', @mes_base_pid, '备品件信息', '/xslmes/mesXslSparePart', 'xslmes/mesXslSparePart/MesXslSparePartList', NULL, 1, NULL, '1', 16, 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
('1860000000000000106', '1860000000000000105', '新增', 2, 'mes:mes_xsl_spare_part:add', '1', '1', 0, 'admin', NOW()),
('1860000000000000107', '1860000000000000105', '编辑', 2, 'mes:mes_xsl_spare_part:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000000108', '1860000000000000105', '删除', 2, 'mes:mes_xsl_spare_part:delete', '1', '1', 0, 'admin', NOW()),
('1860000000000000109', '1860000000000000105', '批量删除', 2, 'mes:mes_xsl_spare_part:deleteBatch', '1', '1', 0, 'admin', NOW()),
('1860000000000000110', '1860000000000000105', '导出', 2, 'mes:mes_xsl_spare_part:exportXls', '1', '1', 0, 'admin', NOW()),
('1860000000000000111', '1860000000000000105', '导入', 2, 'mes:mes_xsl_spare_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 (
    '1860000000000000105',
    '1860000000000000106', '1860000000000000107', '1860000000000000108', '1860000000000000109',
    '1860000000000000110', '1860000000000000111'
  )
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_permission` rp
    WHERE rp.`role_id` = r.`id` AND rp.`permission_id` = p.`id`
  );
