-- =============================================
-- 巡检模块表结构
-- =============================================

-- ----------------------------
-- 巡检检查项模板表
-- ----------------------------
DROP TABLE IF EXISTS `ops_inspection_template`;
CREATE TABLE `ops_inspection_template` (
  `id` bigint NOT NULL COMMENT '模板ID',
  `function_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '功能类型（关联 ops_bus_area.function_type）',
  `item_title` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '检查项标题',
  `sort_order` int NOT NULL DEFAULT 0 COMMENT '排序序号',
  `is_active` bit(1) NOT NULL DEFAULT b'1' COMMENT '是否启用',
  `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  PRIMARY KEY (`id`),
  KEY `idx_function_type` (`function_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='巡检检查项模板';

-- ----------------------------
-- 巡检主记录表
-- ----------------------------
DROP TABLE IF EXISTS `ops_inspection_record`;
CREATE TABLE `ops_inspection_record` (
  `id` bigint NOT NULL COMMENT '巡检记录ID',
  `area_id` bigint NOT NULL COMMENT '区域ID',
  `inspector_id` bigint NOT NULL COMMENT '巡检员用户ID',
  `is_location_exception` tinyint NOT NULL DEFAULT 0 COMMENT '位置是否异常（0正常 1异常）',
  `result_status` tinyint COMMENT '巡检结果（0不合格 1合格）',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '备注',
  `last_order_id` bigint COMMENT '归属判定-上一个完成工单ID',
  `stay_duration` int COMMENT '归属判定-保洁员停留时长（秒）',
  `attribution_result` tinyint COMMENT '归属判定结果（1个人责任 2突发状况 3正常）',
  `generated_order_id` bigint COMMENT '整改工单ID',
  `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  PRIMARY KEY (`id`),
  KEY `idx_area_id` (`area_id`),
  KEY `idx_inspector_id` (`inspector_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='巡检主记录';

-- ----------------------------
-- 巡检明细表
-- ----------------------------
DROP TABLE IF EXISTS `ops_inspection_record_item`;
CREATE TABLE `ops_inspection_record_item` (
  `id` bigint NOT NULL COMMENT '明细ID',
  `record_id` bigint NOT NULL COMMENT '巡检记录ID',
  `template_id` bigint NOT NULL COMMENT '模板检查项ID',
  `is_passed` bit(1) NOT NULL DEFAULT b'1' COMMENT '是否合格',
  `remark` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '备注',
  `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  PRIMARY KEY (`id`),
  KEY `idx_record_id` (`record_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='巡检明细';
