cqdevicemgt/sql/jtt808_tables.sql

124 lines
7.3 KiB
MySQL
Raw Normal View History

2025-09-16 09:25:54 +08:00
-- JTT808协议相关数据表
-- 创建日期: 2025-01-15
-- 说明: 用于存储车载终端的注册信息、位置数据等
SET NAMES utf8mb4;
-- ----------------------------
-- 1、终端设备表
-- ----------------------------
DROP TABLE IF EXISTS jtt808_terminal;
CREATE TABLE jtt808_terminal (
terminal_id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '终端ID',
phone_number varchar(20) NOT NULL COMMENT '终端手机号(BCD编码)',
province_id int(11) DEFAULT 0 COMMENT '省域ID',
city_id int(11) DEFAULT 0 COMMENT '市县域ID',
manufacturer_id varchar(20) DEFAULT '' COMMENT '制造商ID',
terminal_model varchar(30) DEFAULT '' COMMENT '终端型号',
terminal_device_id varchar(30) DEFAULT '' COMMENT '终端设备ID',
plate_color tinyint(4) DEFAULT 0 COMMENT '车牌颜色(0-其他,1-蓝色,2-黄色,3-黑色,4-白色,9-新能源)',
plate_number varchar(20) DEFAULT '' COMMENT '车牌号',
auth_code varchar(50) DEFAULT '' COMMENT '鉴权码',
register_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间',
last_auth_time datetime NULL COMMENT '最后鉴权时间',
online_status tinyint(4) DEFAULT 0 COMMENT '在线状态(0-离线,1-在线)',
last_heartbeat_time datetime NULL COMMENT '最后心跳时间',
client_ip varchar(50) DEFAULT '' COMMENT '客户端IP地址',
create_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (terminal_id),
UNIQUE KEY uk_phone_number (phone_number),
KEY idx_plate_number (plate_number),
KEY idx_online_status (online_status),
KEY idx_register_time (register_time)
) ENGINE=InnoDB AUTO_INCREMENT=1000 COMMENT = 'JTT808终端设备表';
-- ----------------------------
-- 2、位置信息表
-- ----------------------------
DROP TABLE IF EXISTS jtt808_location;
CREATE TABLE jtt808_location (
location_id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '位置记录ID',
phone_number varchar(20) NOT NULL COMMENT '终端手机号',
alarm_flag bigint(20) DEFAULT 0 COMMENT '报警标志',
status_flag bigint(20) DEFAULT 0 COMMENT '状态标志',
latitude decimal(10,6) NOT NULL COMMENT '纬度',
longitude decimal(10,6) NOT NULL COMMENT '经度',
altitude int(11) DEFAULT 0 COMMENT '海拔高度(米)',
speed decimal(5,1) DEFAULT 0.0 COMMENT '速度(km/h)',
direction int(11) DEFAULT 0 COMMENT '方向(0-359度)',
location_time datetime NOT NULL COMMENT '定位时间',
additional_info text NULL COMMENT '附加信息(JSON格式)',
client_ip varchar(50) DEFAULT '' COMMENT '客户端IP地址',
receive_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '接收时间',
PRIMARY KEY (location_id),
KEY idx_phone_number (phone_number),
KEY idx_location_time (location_time),
KEY idx_receive_time (receive_time),
KEY idx_lat_lng (latitude, longitude)
) ENGINE=InnoDB AUTO_INCREMENT=1000 COMMENT = 'JTT808位置信息表';
-- ----------------------------
-- 3、消息日志表
-- ----------------------------
DROP TABLE IF EXISTS jtt808_message_log;
CREATE TABLE jtt808_message_log (
log_id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '日志ID',
phone_number varchar(20) NOT NULL COMMENT '终端手机号',
message_id varchar(10) NOT NULL COMMENT '消息ID(十六进制)',
message_type varchar(20) NOT NULL COMMENT '消息类型',
serial_number int(11) NOT NULL COMMENT '流水号',
message_direction tinyint(4) NOT NULL COMMENT '消息方向(0-上行,1-下行)',
message_content text NULL COMMENT '消息内容(十六进制)',
process_result tinyint(4) DEFAULT 0 COMMENT '处理结果(0-成功,1-失败)',
error_message varchar(500) DEFAULT '' COMMENT '错误信息',
client_ip varchar(50) DEFAULT '' COMMENT '客户端IP地址',
process_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '处理时间',
PRIMARY KEY (log_id),
KEY idx_phone_number (phone_number),
KEY idx_message_id (message_id),
KEY idx_process_time (process_time),
KEY idx_message_direction (message_direction)
) ENGINE=InnoDB AUTO_INCREMENT=1000 COMMENT = 'JTT808消息处理日志表';
-- ----------------------------
-- 4、终端状态统计表
-- ----------------------------
DROP TABLE IF EXISTS jtt808_terminal_stats;
CREATE TABLE jtt808_terminal_stats (
stats_id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '统计ID',
phone_number varchar(20) NOT NULL COMMENT '终端手机号',
stats_date date NOT NULL COMMENT '统计日期',
total_messages int(11) DEFAULT 0 COMMENT '总消息数',
location_reports int(11) DEFAULT 0 COMMENT '位置上报数',
heartbeat_count int(11) DEFAULT 0 COMMENT '心跳次数',
online_duration int(11) DEFAULT 0 COMMENT '在线时长(分钟)',
max_speed decimal(5,1) DEFAULT 0.0 COMMENT '最高速度(km/h)',
total_mileage decimal(10,2) DEFAULT 0.00 COMMENT '总里程(km)',
create_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (stats_id),
UNIQUE KEY uk_phone_date (phone_number, stats_date),
KEY idx_stats_date (stats_date)
) ENGINE=InnoDB AUTO_INCREMENT=1000 COMMENT = 'JTT808终端统计表';
-- ----------------------------
-- 初始化数据
-- ----------------------------
-- 插入测试终端数据
INSERT INTO jtt808_terminal (
phone_number, province_id, city_id, manufacturer_id, terminal_model,
terminal_device_id, plate_color, plate_number, auth_code,
register_time, online_status, client_ip
) VALUES (
'020322495257', 0, 0, '', '', '', 0, '2495257',
CONCAT('AUTH_', UNIX_TIMESTAMP()), NOW(), 1, '39.144.163.108'
);
-- 创建索引优化查询性能
-- 位置信息表按时间分区索引
ALTER TABLE jtt808_location ADD INDEX idx_phone_time (phone_number, location_time DESC);
-- 消息日志表按时间分区索引
ALTER TABLE jtt808_message_log ADD INDEX idx_phone_time (phone_number, process_time DESC);