-- MES 胶料小料锁定日志 + 锁定原因字段 reason_desc：可整文件一次执行
-- 若已执行 V3.9.2_119 Flyway 可只跑本脚本补菜单/字段（幂等）
-- 权限前缀：mes:mes_xsl_rubber_small_lock_log:*
SET NAMES utf8mb4;

SET @reason_desc_exists := (
  SELECT COUNT(1)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'mes_xsl_rubber_small_lock_reason'
    AND COLUMN_NAME = 'reason_desc'
);
SET @ddl_reason_desc := IF(
  @reason_desc_exists = 0,
  'ALTER TABLE `mes_xsl_rubber_small_lock_reason` ADD COLUMN `reason_desc` varchar(500) NOT NULL DEFAULT '''' COMMENT ''原因（手动输入，必填）'' AFTER `barcode_type`',
  'SELECT 1'
);
PREPARE stmt_reason_desc FROM @ddl_reason_desc;
EXECUTE stmt_reason_desc;
DEALLOCATE PREPARE stmt_reason_desc;

UPDATE `mes_xsl_rubber_small_lock_reason` SET `reason_desc` = CONCAT('原因', `reason_code`) WHERE `reason_desc` = '' OR `reason_desc` IS NULL;

CREATE TABLE IF NOT EXISTS `mes_xsl_rubber_small_lock_log` (
  `id` varchar(32) NOT NULL COMMENT '主键',
  `lock_reason_id` varchar(32) NOT NULL COMMENT '锁定原因ID',
  `barcode_type` varchar(16) NOT NULL COMMENT '条码类型',
  `barcode` varchar(128) NOT NULL COMMENT '条码',
  `lock_type` varchar(16) NOT NULL COMMENT '状态',
  `reason_desc` varchar(500) NOT NULL COMMENT '原因',
  `log_date` date NOT 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_mrsl_log_tenant_date` (`tenant_id`, `log_date`),
  KEY `idx_mrsl_log_barcode` (`tenant_id`, `barcode_type`, `barcode`),
  KEY `idx_mrsl_log_reason` (`lock_reason_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='MES胶料小料锁定日志';

SET @mes_tenant_id = 1002;

SET @mes_quality_pid = IFNULL(
  (SELECT `id` FROM `sys_permission` WHERE `del_flag` = 0 AND `menu_type` = 0 AND `name` = '质量管理' LIMIT 1),
  '1860000000000000162'
);

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 ('1860000000000000215', @mes_quality_pid, '胶料小料锁定日志', '/xslmes/mesXslRubberSmallLockLog', 'xslmes/mesXslRubberSmallLockLog/MesXslRubberSmallLockLogList', 'MesXslRubberSmallLockLogList', 1, NULL, '1', 7, 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`),
`sort_no` = VALUES(`sort_no`), `is_leaf` = 0, `status` = '1', `del_flag` = 0, `keep_alive` = VALUES(`keep_alive`);

UPDATE `sys_permission` SET `icon` = 'ant-design:file-text-outlined' WHERE `id` = '1860000000000000215' AND `del_flag` = 0;

INSERT INTO `sys_permission`(`id`, `parent_id`, `name`, `menu_type`, `perms`, `perms_type`, `is_leaf`, `status`, `del_flag`, `create_by`, `create_time`) VALUES
('1860000000000000216', '1860000000000000215', '新增', 2, 'mes:mes_xsl_rubber_small_lock_log:add', '1', 1, '1', 0, 'admin', NOW()),
('1860000000000000217', '1860000000000000215', '编辑', 2, 'mes:mes_xsl_rubber_small_lock_log:edit', '1', 1, '1', 0, 'admin', NOW()),
('1860000000000000218', '1860000000000000215', '删除', 2, 'mes:mes_xsl_rubber_small_lock_log:delete', '1', 1, '1', 0, 'admin', NOW()),
('1860000000000000219', '1860000000000000215', '批量删除', 2, 'mes:mes_xsl_rubber_small_lock_log:deleteBatch', '1', 1, '1', 0, 'admin', NOW()),
('1860000000000000220', '1860000000000000215', '导出', 2, 'mes:mes_xsl_rubber_small_lock_log:exportXls', '1', 1, '1', 0, 'admin', NOW()),
('1860000000000000221', '1860000000000000215', '导入', 2, 'mes:mes_xsl_rubber_small_lock_log:importExcel', '1', 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`),
`is_leaf` = 1, `status` = '1', `del_flag` = 0;

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 (
    '1860000000000000215',
    '1860000000000000216', '1860000000000000217', '1860000000000000218', '1860000000000000219',
    '1860000000000000220', '1860000000000000221'
  )
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_permission` rp
    WHERE rp.`role_id` = r.`id` AND rp.`permission_id` = p.`id`
  );
