-- MES 点检/保养记录（主子表）：建表 + 字典 + 菜单 + 按钮 + 租户 admin 授权
-- 权限前缀：mes:mes_xsl_equip_inspect_record:*
-- 父菜单：设备管理；依赖设备点检配置、设备台账
-- Flyway：V3.9.2_79__mes_xsl_equip_inspect_record.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_inspect_result', '合格/不合格', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_im_inspect_result' AND `del_flag` = 0);

INSERT INTO `sys_dict_item`(`id`, `dict_id`, `item_text`, `item_value`, `description`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.`id`, '合格', 'pass', '', 1, 1, 'admin', NOW()
FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_inspect_result' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.`id` AND i.`item_value` = 'pass');

INSERT INTO `sys_dict_item`(`id`, `dict_id`, `item_text`, `item_value`, `description`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.`id`, '不合格', 'fail', '', 2, 1, 'admin', NOW()
FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_inspect_result' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.`id` AND i.`item_value` = 'fail');

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_record_status', '待点检/已点检', 0, 'admin', NOW(), 0, 0
WHERE NOT EXISTS (SELECT 1 FROM `sys_dict` WHERE `dict_code` = 'xslmes_im_record_status' AND `del_flag` = 0);

INSERT INTO `sys_dict_item`(`id`, `dict_id`, `item_text`, `item_value`, `description`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.`id`, '待点检', 'pending', '', 1, 1, 'admin', NOW()
FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_record_status' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.`id` AND i.`item_value` = 'pending');

INSERT INTO `sys_dict_item`(`id`, `dict_id`, `item_text`, `item_value`, `description`, `sort_order`, `status`, `create_by`, `create_time`)
SELECT REPLACE(UUID(), '-', ''), d.`id`, '已点检', 'done', '', 2, 1, 'admin', NOW()
FROM `sys_dict` d
WHERE d.`dict_code` = 'xslmes_im_record_status' AND NOT EXISTS (SELECT 1 FROM `sys_dict_item` i WHERE i.`dict_id` = d.`id` AND i.`item_value` = 'done');

CREATE TABLE IF NOT EXISTS `mes_xsl_equip_inspect_record` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `record_no` varchar(32) NOT NULL COMMENT '记录编号（EC+yyyyMMdd+4位流水，租户内按日递增）',
  `plan_no` varchar(500) DEFAULT NULL COMMENT '计划单号',
  `plan_id` varchar(32) DEFAULT 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 '设备名称冗余',
  `equip_inspect_config_id` varchar(32) DEFAULT NULL COMMENT '设备点检配置主键 mes_xsl_equip_inspect_config.id',
  `record_type` varchar(500) NOT NULL COMMENT '类型（字典xslmes_im_item_category：inspect点检/maintain保养）',
  `inspect_date` date DEFAULT NULL COMMENT '点检日期',
  `inspector_user_id` varchar(32) DEFAULT NULL COMMENT '点检人用户ID',
  `inspector_username` varchar(500) DEFAULT NULL COMMENT '点检人账号',
  `inspector_realname` varchar(500) DEFAULT NULL COMMENT '点检人姓名',
  `inspect_result` varchar(500) NOT NULL COMMENT '点检结果（字典xslmes_im_inspect_result：pass合格/fail不合格）',
  `record_status` varchar(500) NOT NULL COMMENT '状态（字典xslmes_im_record_status：pending待点检/done已点检）',
  `handled_flag` varchar(1) DEFAULT NULL COMMENT '是否已处理（字典yn：1是0否，仅不合格记录使用）',
  `handler_user_id` varchar(32) DEFAULT NULL COMMENT '处理人用户ID',
  `handler_username` varchar(500) DEFAULT NULL COMMENT '处理人账号',
  `handler_realname` varchar(500) DEFAULT NULL COMMENT '处理人姓名',
  `handle_time` datetime 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`),
  UNIQUE KEY `uk_meir_tenant_record_no` (`tenant_id`, `record_no`),
  KEY `idx_meir_equip_type` (`equipment_ledger_id`, `record_type`),
  KEY `idx_meir_inspect_date` (`inspect_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES点检保养记录';

CREATE TABLE IF NOT EXISTS `mes_xsl_equip_inspect_record_line` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `record_id` varchar(32) NOT NULL COMMENT '主表主键 mes_xsl_equip_inspect_record.id',
  `equip_inspect_config_line_id` varchar(32) NOT NULL COMMENT '设备点检配置明细主键 mes_xsl_equip_inspect_config_line.id',
  `inspect_maintain_item_id` varchar(32) DEFAULT NULL COMMENT '点检及保养项目主键冗余',
  `item_code` varchar(500) DEFAULT NULL COMMENT '点检项目编号冗余',
  `item_name` varchar(500) DEFAULT NULL COMMENT '项目名称冗余',
  `item_category` varchar(500) DEFAULT NULL COMMENT '项目类别冗余',
  `item_type` varchar(500) DEFAULT NULL COMMENT '项目类型冗余',
  `equipment_part_name` varchar(500) DEFAULT NULL COMMENT '设备部位冗余',
  `equipment_sub_part_name` varchar(500) DEFAULT NULL COMMENT '设备小部位冗余',
  `inspect_method` varchar(500) DEFAULT NULL COMMENT '点检方式冗余',
  `judgment_criteria` varchar(500) DEFAULT NULL COMMENT '判断基准冗余',
  `line_inspect_result` varchar(500) DEFAULT NULL COMMENT '明细点检结果（文本）',
  `picture_files` varchar(2000) DEFAULT NULL COMMENT '图片（上传路径，逗号分隔）',
  `sort_no` int DEFAULT '0' 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 '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_meirl_record` (`record_id`),
  KEY `idx_meirl_config_line` (`equip_inspect_config_line_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 ('1860000000000000155', @mes_equip_pid, '点检保养记录', '/xslmes/mesXslEquipInspectRecord', 'xslmes/mesXslEquipInspectRecord/MesXslEquipInspectRecordList', 'MesXslEquipInspectRecordList', 1, NULL, '1', 13, 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:file-done-outlined';

UPDATE `sys_permission` SET `icon` = 'ant-design:file-done-outlined' WHERE `id` = '1860000000000000155' 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
('1860000000000000156', '1860000000000000155', '新增', 2, 'mes:mes_xsl_equip_inspect_record:add', '1', '1', 0, 'admin', NOW()),
('1860000000000000157', '1860000000000000155', '编辑', 2, 'mes:mes_xsl_equip_inspect_record:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000000158', '1860000000000000155', '删除', 2, 'mes:mes_xsl_equip_inspect_record:delete', '1', '1', 0, 'admin', NOW()),
('1860000000000000159', '1860000000000000155', '批量删除', 2, 'mes:mes_xsl_equip_inspect_record:deleteBatch', '1', '1', 0, 'admin', NOW()),
('1860000000000000160', '1860000000000000155', '导出', 2, 'mes:mes_xsl_equip_inspect_record:exportXls', '1', '1', 0, 'admin', NOW()),
('1860000000000000161', '1860000000000000155', '导入', 2, 'mes:mes_xsl_equip_inspect_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 (
    '1860000000000000155',
    '1860000000000000156', '1860000000000000157', '1860000000000000158', '1860000000000000159',
    '1860000000000000160', '1860000000000000161'
  )
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_permission` rp
    WHERE rp.`role_id` = r.`id` AND rp.`permission_id` = p.`id`
  );
