CREATE TABLE IF NOT EXISTS `asr_bas_crnp` (
  `crn_no` int NOT NULL COMMENT '堆垛机编号',
  `status` int DEFAULT 1 COMMENT '状态 1:正常 0:禁用',
  `create_by` bigint DEFAULT NULL COMMENT '创建人',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` bigint DEFAULT NULL COMMENT '修改人',
  `update_time` datetime DEFAULT NULL COMMENT '修改时间',
  `memo` varchar(255) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`crn_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='堆垛机配置';

INSERT INTO `sys_config` (`name`, `code`, `value`, `type`, `status`, `select_type`)
SELECT 'WCS堆垛机状态接口', 'wcsDeviceStatusUrl', '', 1, 1, NULL
WHERE NOT EXISTS (
    SELECT 1 FROM `sys_config` WHERE `code` = 'wcsDeviceStatusUrl'
);

INSERT INTO `sys_resource` (`code`, `name`, `resource_id`, `level`, `sort`, `status`)
SELECT 'views/basCrnp/basCrnp.html', '堆垛机状态监控', `resource_id`, `level`, `sort` + 1, `status`
FROM `sys_resource`
WHERE `code` = 'views/basStation/basStation.html'
  AND NOT EXISTS (
      SELECT 1 FROM `sys_resource` WHERE `code` = 'views/basCrnp/basCrnp.html'
  )
LIMIT 1;

INSERT INTO `sys_role_resource` (`role_id`, `resource_id`)
SELECT rr.`role_id`, new_res.`id`
FROM `sys_role_resource` rr
JOIN `sys_resource` old_res ON old_res.`id` = rr.`resource_id`
JOIN `sys_resource` new_res ON new_res.`code` = 'views/basCrnp/basCrnp.html'
WHERE old_res.`code` = 'views/basStation/basStation.html'
  AND NOT EXISTS (
      SELECT 1
      FROM `sys_role_resource` srr
      WHERE srr.`role_id` = rr.`role_id`
        AND srr.`resource_id` = new_res.`id`
  );
