-- ============================================================
-- 租赁全链路建表 + 菜单权限
-- 执行顺序：先在 oneos_asset 执行建表，再在 oneos_system 执行菜单插入
-- ============================================================

-- ==================== Part 1: oneos_asset 建表 ====================

-- 1. 验车模板定义
CREATE TABLE IF NOT EXISTS `asset_inspection_template` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `code` varchar(50) NOT NULL COMMENT '模板编码',
  `name` varchar(100) NOT NULL COMMENT '模板名称',
  `biz_type` tinyint NOT NULL COMMENT '适用业务：1=备车 2=交车 3=还车',
  `vehicle_type` varchar(50) DEFAULT NULL COMMENT '适用车辆类型（nullable=通用）',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '0=禁用 1=启用',
  `remark` varchar(500) DEFAULT NULL,
  `creator` varchar(64) DEFAULT '',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` varchar(64) DEFAULT '',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` bit(1) NOT NULL DEFAULT b'0',
  `tenant_id` bigint NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='验车模板定义';

-- 2. 验车模板检查项
CREATE TABLE IF NOT EXISTS `asset_inspection_template_item` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `template_id` bigint NOT NULL COMMENT '关联模板',
  `category` varchar(50) NOT NULL COMMENT '分类',
  `item_name` varchar(100) NOT NULL COMMENT '检查项名称',
  `item_code` varchar(50) NOT NULL COMMENT '检查项编码',
  `input_type` varchar(20) NOT NULL DEFAULT 'checkbox' COMMENT '输入类型：checkbox/number/text',
  `sort` int NOT NULL DEFAULT 0,
  `required` tinyint NOT NULL DEFAULT 1 COMMENT '0=否 1=是',
  `creator` varchar(64) DEFAULT '',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` varchar(64) DEFAULT '',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` bit(1) NOT NULL DEFAULT b'0',
  `tenant_id` bigint NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_template_id` (`template_id`)
) ENGINE=InnoDB COMMENT='验车模板检查项';

-- 3. 验车记录
CREATE TABLE IF NOT EXISTS `asset_inspection_record` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `record_code` varchar(50) NOT NULL COMMENT '记录编码',
  `template_id` bigint NOT NULL COMMENT '使用的模板',
  `source_type` tinyint NOT NULL COMMENT '来源：1=备车 2=交车 3=还车',
  `source_id` bigint NOT NULL COMMENT '来源业务ID',
  `vehicle_id` bigint NOT NULL COMMENT '车辆ID',
  `inspector_name` varchar(50) DEFAULT NULL COMMENT '检查人',
  `inspection_time` datetime DEFAULT NULL COMMENT '检查时间',
  `status` tinyint NOT NULL DEFAULT 0 COMMENT '0=待检查 1=检查中 2=已完成',
  `overall_result` tinyint DEFAULT NULL COMMENT '1=合格 2=不合格',
  `remark` varchar(500) DEFAULT NULL,
  `cloned_from_id` bigint DEFAULT NULL COMMENT '克隆来源记录ID',
  `creator` varchar(64) DEFAULT '',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` varchar(64) DEFAULT '',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` bit(1) NOT NULL DEFAULT b'0',
  `tenant_id` bigint NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_vehicle_source` (`vehicle_id`, `source_type`)
) ENGINE=InnoDB COMMENT='验车记录';

-- 4. 验车记录检查项结果
CREATE TABLE IF NOT EXISTS `asset_inspection_record_item` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `record_id` bigint NOT NULL COMMENT '关联记录',
  `item_code` varchar(50) NOT NULL COMMENT '检查项编码',
  `category` varchar(50) NOT NULL COMMENT '分类',
  `item_name` varchar(100) NOT NULL COMMENT '检查项名称',
  `input_type` varchar(20) NOT NULL DEFAULT 'checkbox' COMMENT '输入类型',
  `result` tinyint DEFAULT NULL COMMENT '1=合格 2=不合格 3=不适用',
  `value` varchar(200) DEFAULT NULL COMMENT '数值/文本输入值',
  `remark` varchar(500) DEFAULT NULL,
  `image_urls` varchar(2000) DEFAULT NULL COMMENT '图片URL，逗号分隔',
  `creator` varchar(64) DEFAULT '',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` varchar(64) DEFAULT '',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` bit(1) NOT NULL DEFAULT b'0',
  `tenant_id` bigint NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_record_id` (`record_id`)
) ENGINE=InnoDB COMMENT='验车记录检查项结果';

-- 5. 替换车申请
CREATE TABLE IF NOT EXISTS `asset_vehicle_replacement` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `replacement_code` varchar(50) NOT NULL COMMENT '替换单编码',
  `replacement_type` tinyint NOT NULL COMMENT '1=临时 2=永久',
  `contract_id` bigint NOT NULL,
  `contract_code` varchar(50) DEFAULT NULL,
  `project_name` varchar(200) DEFAULT NULL,
  `customer_id` bigint DEFAULT NULL,
  `customer_name` varchar(100) DEFAULT NULL,
  `delivery_order_id` bigint DEFAULT NULL COMMENT '来源交车单ID',
  `original_vehicle_id` bigint NOT NULL,
  `original_plate_no` varchar(20) DEFAULT NULL,
  `original_vin` varchar(50) DEFAULT NULL,
  `new_vehicle_id` bigint DEFAULT NULL,
  `new_plate_no` varchar(20) DEFAULT NULL,
  `new_vin` varchar(50) DEFAULT NULL,
  `replacement_reason` varchar(500) DEFAULT NULL,
  `expected_date` date DEFAULT NULL,
  `actual_date` date DEFAULT NULL,
  `return_date` date DEFAULT NULL COMMENT '临时替换预计归还日期',
  `actual_return_date` date DEFAULT NULL,
  `status` tinyint NOT NULL DEFAULT 0 COMMENT '0=草稿 1=审批中 2=审批通过 3=执行中 4=已完成 5=审批驳回 6=已撤回',
  `approval_status` tinyint NOT NULL DEFAULT 0,
  `bpm_instance_id` varchar(64) DEFAULT NULL,
  `remark` varchar(500) DEFAULT NULL,
  `creator` varchar(64) DEFAULT '',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` varchar(64) DEFAULT '',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted` bit(1) NOT NULL DEFAULT b'0',
  `tenant_id` bigint NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='替换车申请';


-- ==================== Part 2: oneos_system 菜单权限 ====================
-- parent_id=5055 为"资产管理"一级菜单，从 id=5161 开始

-- 验车模板管理 - 页面菜单 (type=2)
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES
(5161, '验车模板', '', 2, 15, 5055, 'inspection-template', '', 'asset/inspection-template/index', 'InspectionTemplate', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0');

-- 验车模板 - 权限按钮 (type=3)
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES
(5162, '验车模板查询', 'asset:inspection-template:query', 3, 1, 5161, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(5163, '验车模板创建', 'asset:inspection-template:create', 3, 2, 5161, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(5164, '验车模板更新', 'asset:inspection-template:update', 3, 3, 5161, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(5165, '验车模板删除', 'asset:inspection-template:delete', 3, 4, 5161, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0');

-- 验车记录 - 权限按钮 (挂在验车模板下，无独立页面)
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES
(5166, '验车记录查询', 'asset:inspection-record:query', 3, 5, 5161, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(5167, '验车记录更新', 'asset:inspection-record:update', 3, 6, 5161, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0');

-- 替换车管理 - 页面菜单 (type=2)
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES
(5170, '替换车管理', '', 2, 16, 5055, 'vehicle-replacement', '', 'asset/vehicle-replacement/index', 'VehicleReplacement', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0');

-- 替换车管理 - 权限按钮 (type=3)
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES
(5171, '替换车查询', 'asset:vehicle-replacement:query', 3, 1, 5170, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(5172, '替换车创建', 'asset:vehicle-replacement:create', 3, 2, 5170, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(5173, '替换车更新', 'asset:vehicle-replacement:update', 3, 3, 5170, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(5174, '替换车删除', 'asset:vehicle-replacement:delete', 3, 4, 5170, '', '', '', '', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0');
