-- MES 停机记录：建表 + 菜单 + 按钮 + 租户 admin 授权（可整文件一次执行）
-- 权限前缀：mes:mes_xsl_downtime_record:*
-- 依赖：mes_xsl_equipment_ledger、mes_xsl_downtime_type；父菜单 设备管理
-- Flyway：V3.9.2_117__mes_xsl_downtime_record.sql
SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `mes_xsl_downtime_record` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `equipment_ledger_id` varchar(32) NOT NULL COMMENT '设备台账主键 mes_xsl_equipment_ledger.id',
  `equipment_code` varchar(500) DEFAULT NULL COMMENT '设备编号冗余',
  `equipment_name` varchar(500) DEFAULT NULL COMMENT '设备名称冗余',
  `downtime_type_id` varchar(32) NOT NULL COMMENT '停机类型主键 mes_xsl_downtime_type.id',
  `downtime_type_name` varchar(500) DEFAULT NULL COMMENT '停机类型名称冗余',
  `start_time` datetime NOT NULL COMMENT '开始时间',
  `end_time` datetime DEFAULT NULL COMMENT '结束时间',
  `equipment_part_id` varchar(32) DEFAULT NULL COMMENT '设备部位主键 mes_xsl_equipment_part.id',
  `equipment_part_name` varchar(500) DEFAULT NULL COMMENT '设备部位名称冗余',
  `inspect_maintain_item_id` varchar(32) DEFAULT NULL COMMENT '点检及保养项目主键 mes_xsl_inspect_maintain_item.id',
  `inspect_maintain_item_name` varchar(500) DEFAULT NULL COMMENT '点检项目名称冗余',
  `maintenance_result` varchar(500) DEFAULT NULL COMMENT '维修结果',
  `maintainer_user_id` varchar(32) DEFAULT NULL COMMENT '维修人用户ID',
  `maintainer_username` varchar(500) DEFAULT NULL COMMENT '维修人账号',
  `maintainer_realname` varchar(500) DEFAULT NULL COMMENT '维修人姓名',
  `maintenance_filled_flag` varchar(1) DEFAULT '0' COMMENT '是否已录入维修结果（字典yn：1是0否）',
  `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_mdr_equipment` (`equipment_ledger_id`),
  KEY `idx_mdr_downtime_type` (`downtime_type_id`),
  KEY `idx_mdr_start_time` (`start_time`)
) 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 ('1860000000000000201', @mes_equip_pid, '停机记录', '/xslmes/mesXslDowntimeRecord', 'xslmes/mesXslDowntimeRecord/MesXslDowntimeRecordList', 'MesXslDowntimeRecordList', 1, NULL, '1', 12, 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:history-outlined';

UPDATE `sys_permission` SET `icon` = 'ant-design:history-outlined' WHERE `id` = '1860000000000000201' 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
('1860000000000000202', '1860000000000000201', '新增', 2, 'mes:mes_xsl_downtime_record:add', '1', '1', 0, 'admin', NOW()),
('1860000000000000203', '1860000000000000201', '编辑', 2, 'mes:mes_xsl_downtime_record:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000000204', '1860000000000000201', '删除', 2, 'mes:mes_xsl_downtime_record:delete', '1', '1', 0, 'admin', NOW()),
('1860000000000000205', '1860000000000000201', '批量删除', 2, 'mes:mes_xsl_downtime_record:deleteBatch', '1', '1', 0, 'admin', NOW()),
('1860000000000000206', '1860000000000000201', '导出', 2, 'mes:mes_xsl_downtime_record:exportXls', '1', '1', 0, 'admin', NOW()),
('1860000000000000207', '1860000000000000201', '导入', 2, 'mes:mes_xsl_downtime_record: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 (
    '1860000000000000201',
    '1860000000000000202', '1860000000000000203', '1860000000000000204', '1860000000000000205',
    '1860000000000000206', '1860000000000000207'
  )
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_permission` rp
    WHERE rp.`role_id` = r.`id` AND rp.`permission_id` = p.`id`
  );
