CREATE TABLE IF NOT EXISTS `UserCfg` (
    `id` bigint(20) AUTO_INCREMENT,
    `tenant_id` int DEFAULT 0,
    `name` varchar(20) NOT NULL,
    `pwd` varchar(64) NOT NULL,
    `role` varchar(24) NOT NULL,
    `locked` boolean DEFAULT false,
    `nickname` varchar(20) DEFAULT NULL,
    `init_pwd` boolean DEFAULT true,
    `mobile` varchar(20) DEFAULT NULL,
    `avatar_url` varchar(255) DEFAULT NULL,
    `openid` varchar(64) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `TenantCfg` (
    `id` int NOT NULL,
    `name` varchar(128) NOT NULL,
    `create_time` timestamp(3) DEFAULT CURRENT_TIMESTAMP(3),
    `update_time` timestamp(3) DEFAULT CURRENT_TIMESTAMP(3),
    `contact` varchar(64) default NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


/***
  以下是北斗数据库表：
  1、设备配置表
  2、基本参数组、解算参数组、转发参数组
  3、设备状态表
  4、GNSS数据：原始数据、解算结果
  5、GNSS消息：消息概要、状态消息、收发统计消息
  6、告警：告警门限，告警消息
  以上配置除2外均和租户相关。SAAS提供商可看所有的数据；租户只能看到自己的设备的数据，以及基本参数组、解算参数组
 */
CREATE TABLE IF NOT EXISTS `gnssdevices` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `opmode` smallint DEFAULT 0,
    `createtime` datetime DEFAULT NULL,
    `createuser` varchar(50) DEFAULT NULL,
    `updatetime` datetime DEFAULT NULL,
    `updateuser` varchar(50) DEFAULT NULL,
    `deviceid` varchar(20) NOT NULL,
    `name` varchar(50) NOT NULL,
    `parentid` varchar(20) DEFAULT NULL,
    `devicetype` smallint DEFAULT 0,
    `tenantname` varchar(100) NOT NULL,
    `project_id` varchar(64) DEFAULT NULL COMMENT '项目id',
    `group_id` int DEFAULT 1,
    `calc_group_id` int DEFAULT 1,
    `fwd_group_id` varchar(64) DEFAULT NULL,
    `syn` bit(1) DEFAULT 0 COMMENT '是否已同步',
    `pictures` varchar(100) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `gnssgroup` (
    `id` int NOT NULL,
    `work_cycle` int DEFAULT 30,
    `active_time` int DEFAULT 6,
    `active_offset` int DEFAULT 0,
    `rs_adv` smallint DEFAULT 0,
    `power_mode` smallint DEFAULT 0,
    `device_num` int DEFAULT 0,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `gnssgroupcalc` (
    `id` int NOT NULL,
    `filter_hour` int DEFAULT NULL COMMENT '平滑窗口',
    `xy_threshold` float DEFAULT NULL COMMENT '坏点水平门限',
    `z_threshold` float DEFAULT NULL COMMENT '坏点垂直门限',
    `auto_filter` bit(1) DEFAULT 0,
    `filter_min_hour` int DEFAULT NULL COMMENT '最小平滑窗口',
    `auto_threshold` float DEFAULT NULL,
    `device_num` int DEFAULT 0,
    `auto_upload` bit(1) DEFAULT 0,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `gnssgroupfwd` (
    `id` int AUTO_INCREMENT,
    `name` varchar(64) DEFAULT NULL,
    `description` varchar(256) DEFAULT NULL,
    `updatetime` datetime DEFAULT NULL,
    `device_num` int DEFAULT 0,
    `fwd_device_num` int DEFAULT 0,
    `result` bit(1) DEFAULT 0 COMMENT '推送是否成功',
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `gnssstatus` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `updatetime` datetime DEFAULT NULL,
    `devicetime` time DEFAULT NULL,
    `deviceid` varchar(20) NOT NULL,
    `location` varchar(100) DEFAULT NULL,
    `roll` float DEFAULT 0,
    `pitch` float DEFAULT 0,
    `yaw` float DEFAULT 0,
    `rssi` smallint DEFAULT 0,
    `voltage` int DEFAULT 0,
    `temperature` float DEFAULT 0,
    `humidity` float DEFAULT 0,
    `satelliteinview` float DEFAULT 0,
    `satelliteinuse` float DEFAULT 0,
    `txbytes` int DEFAULT 0,
    `rxbytes` int DEFAULT 0,
    `b562bytes` int DEFAULT 0,
    `d3xxbytes` int DEFAULT 0,
    `state` smallint DEFAULT 0,
    `warningcode` int DEFAULT 0,
    `warning` smallint DEFAULT 0,
    `noreslutcount` int DEFAULT 0,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `gnssdevicelocationrecords` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `enabled` bit(1) DEFAULT NULL,
    `createtime` datetime DEFAULT NULL,
    `createuser` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
    `updatetime` datetime DEFAULT NULL,
    `updateuser` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
    `deviceid` varchar(20) CHARACTER SET utf8 NOT NULL,
    `b562e` double DEFAULT NULL COMMENT '原始北斗位置东E',
    `b562n` double DEFAULT NULL COMMENT '原始北斗位置北N',
    `b562d` double DEFAULT NULL COMMENT '原始北斗位置天D',
    `r9250e` double DEFAULT NULL,
    `r9250n` double DEFAULT NULL,
    `r9250d` double DEFAULT NULL,
    `resulte` double DEFAULT NULL,
    `resultn` double DEFAULT NULL,
    `resultd` double DEFAULT NULL,
    `rb562e` double DEFAULT NULL COMMENT '相对北斗位置东E',
    `rb562d` double DEFAULT NULL COMMENT '相对北斗位置北N',
    `rb562n` double DEFAULT NULL COMMENT '相对北斗位置天D',
    `pps` int DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_deviceid_time` (`deviceid`,`createtime`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE IF NOT EXISTS `gnssrawdata` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `createtime` datetime DEFAULT NULL,
    `deviceid` varchar(20) CHARACTER SET utf8 NOT NULL,
    `b562e` double DEFAULT NULL COMMENT '原始北斗位置东E',
    `b562n` double DEFAULT NULL COMMENT '原始北斗位置北N',
    `b562d` double DEFAULT NULL COMMENT '原始北斗位置天D',
    `roll` double DEFAULT NULL,
    `pitch` double DEFAULT NULL,
    `yaw` double DEFAULT NULL,
    `shock` double DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE IF NOT EXISTS `gnssmsg` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `createtime` datetime DEFAULT NULL,
    `deviceid` varchar(20) NOT NULL,
    `msgtype` int default 0,
    `msglen` int default 0,
    `tx` bit(1) DEFAULT 0,
    `content` varchar(128) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `gnssstatusmsg` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `createtime` datetime DEFAULT NULL,
    `devicetime` time DEFAULT NULL,
    `deviceid` varchar(20) NOT NULL,
    `roll` float DEFAULT NULL,
    `pitch` float DEFAULT NULL,
    `yaw` float DEFAULT NULL,
    `dtustate` smallint DEFAULT NULL,
    `rssi` smallint DEFAULT NULL,
    `voltage` int DEFAULT NULL,
    `temperature` float DEFAULT NULL,
    `humidity` float DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `gnsstrxmsg` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `createtime` datetime DEFAULT NULL,
    `devicetime` time DEFAULT NULL,
    `deviceid` varchar(20) NOT NULL,
    `uart1txbytes` int DEFAULT NULL,
    `uart1rxbytes` int DEFAULT NULL,
    `uart1unknown` int DEFAULT NULL,
    `uart2txbytes` int DEFAULT NULL,
    `uart2rxbytes` int DEFAULT NULL,
    `uart2unknown` int DEFAULT NULL,
    `b562bytes` int DEFAULT NULL,
    `d3xxbytes` int DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `warninginfo` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `createtime` datetime DEFAULT NULL,
    `deviceid` varchar(20) NOT NULL,
    `devicetype` smallint DEFAULT NULL,
    `level` smallint DEFAULT NULL,
    `code` int NOT NULL,
    `info` varchar(128) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

/***
  北斗告警类型：1，电压低；2，RSSI低；4，掉线；
    0x10、终端收到未知报文多；0x20,使用卫星数少；0x40,基站d331少于d341; 0x80、测站b562比例少；
 */
CREATE TABLE IF NOT EXISTS `warningcfg` (
    `id` bigint AUTO_INCREMENT,
    `devicetype` smallint NOT NULL,
    `type` int NOT NULL,
    `level` smallint DEFAULT 0,
    `typename` varchar(50) NOT NULL,
    `value` int NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

/***
  推送记录表：根据补传指令填写补传数据的设备id、数据时间段
  state状态值：1准备传送，2传送中，3传送结束待推送，0已推送
 */
CREATE TABLE IF NOT EXISTS `fwdrecords` (
    `id` bigint AUTO_INCREMENT,
    `tenantid` int NOT NULL,
    `project_id` varchar(64) DEFAULT NULL COMMENT '项目id',
    `devicenum` smallint NOT NULL,
    `deviceid` varchar(20) DEFAULT NULL,
    `starttime` datetime DEFAULT NULL,
    `endtime` datetime DEFAULT NULL,
    `state` smallint DEFAULT 0,
    `fwd_group_id` varchar(64) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
