-- MES 厂家信息：字典 + 建表 + 菜单 + 按钮 + 租户 admin 授权（可整文件一次执行）
-- 权限前缀与 Controller、前端 v-auth 一致：mes:mes_xsl_manufacturer:*
-- 父菜单：设备管理（@mes_equip_pid）；修改租户改 SET @mes_tenant_id
-- 新环境也可依赖 Flyway：V3.9.2_63__mes_xsl_manufacturer.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_manufacturer_category', '模具厂家、胶囊厂家、设备厂家', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_manufacturer_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, '模具厂家', 'mold', 1, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_manufacturer_category' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'mold');
INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.id, '胶囊厂家', 'capsule', 2, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_manufacturer_category' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'capsule');
INSERT INTO `sys_dict_item` (`id`, `dict_id`, `item_text`, `item_value`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.id, '设备厂家', 'equipment', 3, 1, 'admin', NOW() FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_manufacturer_category' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.id AND i.`item_value` = 'equipment');

INSERT INTO `sys_dict` (`id`, `dict_name`, `dict_code`, `description`, `del_flag`, `create_by`, `create_time`, `type`, `tenant_id`)
SELECT REPLACE(UUID(), '-', ''), 'MES厂家是否有效', 'xslmes_manufacturer_valid', '0有效1无效', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_manufacturer_valid' 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_manufacturer_valid' 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_manufacturer_valid' 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_manufacturer` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `manufacturer_category` varchar(500) NOT NULL COMMENT '厂家类别（字典xslmes_manufacturer_category：mold模具 capsule胶囊 equipment设备）',
  `manufacturer_name` varchar(500) NOT NULL COMMENT '厂家名称（同租户未删除数据中唯一）',
  `valid_status` varchar(1) NOT NULL DEFAULT '0' COMMENT '是否有效（字典xslmes_manufacturer_valid：0有效1无效）',
  `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_mxm_tenant_name` (`tenant_id`, `manufacturer_name`)
) 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 ('1860000000000000112', @mes_equip_pid, '厂家信息', '/xslmes/mesXslManufacturer', 'xslmes/mesXslManufacturer/MesXslManufacturerList', NULL, 1, NULL, '1', 8, 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:shop-outlined';

UPDATE `sys_permission` SET `icon` = 'ant-design:shop-outlined' WHERE `id` = '1860000000000000112' 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
('1860000000000000113', '1860000000000000112', '新增', 2, 'mes:mes_xsl_manufacturer:add', '1', '1', 0, 'admin', NOW()),
('1860000000000000114', '1860000000000000112', '编辑', 2, 'mes:mes_xsl_manufacturer:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000000115', '1860000000000000112', '删除', 2, 'mes:mes_xsl_manufacturer:delete', '1', '1', 0, 'admin', NOW()),
('1860000000000000116', '1860000000000000112', '批量删除', 2, 'mes:mes_xsl_manufacturer:deleteBatch', '1', '1', 0, 'admin', NOW()),
('1860000000000000117', '1860000000000000112', '导出', 2, 'mes:mes_xsl_manufacturer:exportXls', '1', '1', 0, 'admin', NOW()),
('1860000000000000118', '1860000000000000112', '导入', 2, 'mes:mes_xsl_manufacturer: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 (
    '1860000000000000112',
    '1860000000000000113', '1860000000000000114', '1860000000000000115', '1860000000000000116',
    '1860000000000000117', '1860000000000000118'
  )
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_permission` rp
    WHERE rp.`role_id` = r.`id` AND rp.`permission_id` = p.`id`
  );
