-- Cache the cross-system personnel mapping used by the local-first personnel list.
-- The table stores identity/mapping snapshots only. Realtime coordinates remain in FindS/Redis.
CREATE TABLE IF NOT EXISTS `personnel_match` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'primary key',
  `source_system` varchar(32) NOT NULL DEFAULT 'FINDS' COMMENT 'external source system',
  `source_config_code` varchar(64) NOT NULL DEFAULT 'FINDS' COMMENT 'FindS config scope',
  `finds_staff_no` varchar(64) NOT NULL COMMENT 'FindS business staff/track number',
  `finds_staff_no_type` varchar(32) NOT NULL DEFAULT 'TRACK_NO' COMMENT 'TRACK_NO/WORK_NO/STAFF_NO',
  `finds_track_id` bigint DEFAULT NULL COMMENT 'FindS track object id',
  `finds_id_card_no` varchar(64) DEFAULT NULL COMMENT 'FindS id card number',
  `finds_staff_name` varchar(128) DEFAULT NULL COMMENT 'FindS staff name snapshot',
  `finds_corpinfo_id` varchar(64) DEFAULT NULL COMMENT 'FindS company id snapshot',
  `finds_corpinfo_name` varchar(255) DEFAULT NULL COMMENT 'FindS company name snapshot',
  `finds_department_name` varchar(255) DEFAULT NULL COMMENT 'FindS department name snapshot',
  `finds_terminal_no` varchar(64) DEFAULT NULL COMMENT 'FindS terminal number snapshot',
  `local_user_id` bigint DEFAULT NULL COMMENT 'matched local platform user id',
  `local_mobile_no` varchar(64) DEFAULT NULL COMMENT 'local platform mobile snapshot used for matching',
  `local_id_card_no` varchar(64) DEFAULT NULL COMMENT 'matched local id card snapshot',
  `match_status` varchar(32) NOT NULL DEFAULT 'UNMATCHED' COMMENT 'MATCHED/UNMATCHED/AMBIGUOUS/DISABLED',
  `match_type` varchar(32) DEFAULT NULL COMMENT 'MOBILE/ID_CARD/MOBILE_AND_ID_CARD',
  `last_seen_time` datetime DEFAULT NULL COMMENT 'last time observed from FindS',
  `last_sync_time` datetime DEFAULT NULL COMMENT 'last mapping synchronization time',
  `sync_status` varchar(32) NOT NULL DEFAULT 'SUCCESS' COMMENT 'SUCCESS/FAIL',
  `sync_error` varchar(1000) DEFAULT NULL COMMENT 'last synchronization error',
  `delete_enum` varchar(32) NOT NULL DEFAULT 'FALSE' COMMENT 'soft delete flag',
  `remarks` varchar(500) DEFAULT NULL COMMENT 'remarks',
  `tenant_id` bigint DEFAULT NULL COMMENT 'tenant id',
  `org_id` bigint DEFAULT NULL COMMENT 'organization id',
  `env` varchar(32) DEFAULT NULL COMMENT 'environment',
  `version` int NOT NULL DEFAULT 0 COMMENT 'version',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'created time',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'updated time',
  `create_id` bigint DEFAULT NULL COMMENT 'creator id',
  `update_id` bigint DEFAULT NULL COMMENT 'updater id',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_personnel_match_source_staff_delete` (`source_config_code`, `finds_staff_no`, `delete_enum`),
  KEY `idx_personnel_match_local_user` (`local_user_id`, `match_status`),
  KEY `idx_personnel_match_local_mobile` (`local_mobile_no`),
  KEY `idx_personnel_match_find_id_card` (`finds_id_card_no`),
  KEY `idx_personnel_match_terminal_no` (`finds_terminal_no`),
  KEY `idx_personnel_match_local_id_card` (`local_id_card_no`),
  KEY `idx_personnel_match_scope_status` (`finds_corpinfo_id`, `match_status`),
  KEY `idx_personnel_match_last_seen` (`last_seen_time`),
  KEY `idx_personnel_match_tenant_org` (`tenant_id`, `org_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='FindS to local personnel mapping';
