
create database light default character set utf8mb4 collate utf8mb4_unicode_ci;
use light;






# "{"nickName":"小奎","gender":1,"language":"zh_CN","city":"Binzhou","province":"Shandong","country":"China",
    # "avatarUrl":"https://thirdwx.qlogo.cn/mmopen/vi_32/OG1Piaj5xGwhHu7FGHER6eU2Ff2ozcQibKP5ibtUTppiauGTPwpk0jFymoHXzInvLCAS5psFTHLtNSVNE8FOCe1glg/132"}"
create table light.t_wx_user(
id bigint primary key auto_increment,
openid varchar(128) not null comment '微信小程序唯一识别',
nick_name varchar(128) comment '微信昵称',
gender tinyint comment '微信性别',
language varchar(32) comment '微信语言',
city varchar(32) comment '微信城市',
province varchar(32) comment '微信省份',
country varchar(32) comment '微信国家',
avatar_url varchar(1024) comment '微信头像',
user_id varchar(128) comment '企业微信用户ID',
user_name varchar(128) comment '企业微信姓名/真实姓名'
)engine =InnoDB;
create table light.t_event_report(
id bigint primary key auto_increment,
category_id bigint not null default 0 comment '事件分类',
name varchar(128) not null comment '事件名称',
longitude varchar(64) comment '经度',
latitude varchar(64) comment '纬度'
)engine =InnoDB;
create table light.t_yellow_book(
    id bigint primary key auto_increment,
    no varchar(32)  comment '序号',
    crossing varchar(128) not null comment '具体地点',
    num int not null comment '数量（套）',
    remark text,
    longitude varchar(64) comment '经度',
    latitude varchar(64) comment '纬度'
)engine =InnoDB;
create table light.t_other_light(
    id bigint primary key auto_increment,
    name varchar(128) not null comment '名称',
    remark text
)engine =InnoDB;
drop  table t_mark_card;
create table  light.t_mark_card(
    id bigint primary key auto_increment,
    no varchar(32) not null comment '序号',
    crossing varchar(64) not null comment '道路名称',
    location varchar(128)  comment '起止点',
    mark_card json,
    remark text
)engine =InnoDB;
create table light.t_camera(
    id bigint primary key auto_increment,
    no varchar(64) not null comment '资产编号',
    name varchar(64) not null comment '监控点名称',
    code varchar(64) not null comment '监控点编号',
    crossing varchar(64) not null comment '安装位置',
    bar varchar(64) not null comment '杆位',
    bar_status varchar(64) not null comment '杆位状态',
    direction varchar(64) not null comment '监视方位',
    type varchar(64) not null comment '摄像机类型',
    ip varchar(64) not null comment 'IP地址',
    port int not null comment '端口',
    username varchar(64) not null comment '用户名',
    password varchar(64) not null comment '密码',
    linkman varchar(128) not null comment '联系人',
    link_mobile varchar(128) not null comment '联系电话',
    power int not null comment '权重',
    factory varchar(128) not null comment '设备厂商',
    model varchar(64) not null comment '设备型号',
    electric_location varchar(256) comment '取电位置',
    electric_distance varchar(128) comment '取电距离',
    remark text,
    point_status varchar(64) not null comment '经纬度规范',
    longitude varchar(64) comment '经度',
    latitude varchar(64) comment '纬度'
)engine =InnoDB;

create table light.t_event_category(
    id bigint primary key auto_increment,
    name varchar(64) not null comment '分类名称',
    deleted tinyint not null default 0 comment '是否删除'
)engine =InnoDB;
create table light.t_tour (
    id bigint primary key auto_increment,
    tourer varchar(128) not null comment '巡检人',
    time datetime not null comment '时间',
    type tinyint not null comment '类型：1 路口； 2 事件; 3 摄像头； 4 黄闪； 5 标识牌' default 1,
    crossing_id bigint not null comment '巡检路口/事件ID',
    crossing_status tinyint not null comment '路口状态 1:正常；2、故障；3：维修' default 1,
    crossing_case text comment '配时方案',
    level int comment '故障时，等级',
    video varchar(1024) comment '视频地址',id
    remark text
)engine =InnoDB;
create table light.t_image (
    id bigint primary key auto_increment,
    img varchar(64) not null comment 'OSS的对象名称',
    tour_id bigint not null comment '巡检ID',
    time datetime not null comment '创建时间'
)engine =InnoDB;

use light;
create or replace view v_crossing as
select c.*, IFNULL((select t.crossing_status from t_tour t where t.crossing_id=c.id and type=1 order by id desc limit 1), 1) as crossing_status,
       IFNULL((select t.level from t_tour t where t.crossing_id=c.id and type=1 order by id desc limit 1), -1) as level
from t_crossing c;
create or replace view v_event_report as
select e.*, IFNULL(ec.name, '其他') as category_name, IFNULL((select t.crossing_status from t_tour t where t.crossing_id=e.id and type=2 order by id desc limit 1), 2) as crossing_status
    ,IFNULL((select t.level from t_tour t where t.crossing_id=e.id and type=2 order by id desc limit 1), -1) as level
from t_event_report e left join t_event_category ec on e.category_id=ec.id;
create or replace view v_camera as
select c.*, IFNULL((select t.crossing_status from t_tour t where t.crossing_id=c.id and type=3 order by id desc limit 1), 1) as crossing_status,
       IFNULL((select t.level from t_tour t where t.crossing_id=c.id and type=3 order by id desc limit 1), -1) as level
from t_camera c;
create or replace view v_yellow_book as
select c.*, IFNULL((select t.crossing_status from t_tour t where t.crossing_id=c.id and type=4 order by id desc limit 1), 1) as crossing_status,
       IFNULL((select t.level from t_tour t where t.crossing_id=c.id and type=4 order by id desc limit 1), -1) as level
from t_yellow_book c;
create or replace view v_mark_card as
select c.*, IFNULL((select t.crossing_status from t_tour t where t.crossing_id=c.id and type=5 order by id desc limit 1), 1) as crossing_status,
       IFNULL((select t.level from t_tour t where t.crossing_id=c.id and type=5 order by id desc limit 1), -1) as level
from t_mark_card c;



create or replace view v_tour as
    select t.*, c.no as crossing_no, c.name as crossing_name  from t_tour t left join t_crossing c on t.crossing_id=c.id where t.type=1
    union
    select t.*, '' as crossing_no, e.name as crossing_name  from t_tour t left join t_event_report e on t.crossing_id=e.id where t.type=2
    union
    select t.*, '' as crossing_no, e.name as crossing_name  from t_tour t left join t_camera e on t.crossing_id=e.id where t.type=3
    union
    select t.*, '' as crossing_no, e.crossing as crossing_name  from t_tour t left join t_yellow_book e on t.crossing_id=e.id where t.type=4
    union
    select t.*, '' as crossing_no, e.crossing as crossing_name  from t_tour t left join t_mark_card e on t.crossing_id=e.id where t.type=5;
# 机动车灯型
# 灯头型号
    # 东		南		西		北
# 	全屏灯	箭头灯	全屏灯	箭头灯	全屏灯	箭头灯	全屏灯	箭头灯







create table light.t_crossing (
    id bigint  primary key auto_increment,
    no varchar(16) not null comment '序号',
    sixth_ring varchar(16) comment '六环内 1； 六环外 2',
    create_time varchar(64) not null comment '创建时间',
    name varchar(64) not null comment '路口名称',
    type varchar(32) comment '路口类型：1主-主2主-次3次-次4次-支5支-支',
    shape varchar(32) comment '路口形状',
    crossing_east varchar(64) comment '交叉道路东',
    crossing_south varchar(64) comment '交叉道路南',
    crossing_west varchar(64) comment '交叉道路西',
    crossing_north varchar(64) comment '交叉道路北',
    belong_branch varchar(64) comment '所属支队',
    belong_brigade varchar(64) comment '所属大队',
    right_belong varchar(64) comment '权属情况',
    property_right varchar(64) comment '产权单位',
    build_company varchar(64) comment '建设单位',
    safeguard_company varchar(64) comment '维护单位',
    warranty varchar(64) comment '质保情况：1新建免费质保期内2有偿运维服务期内',
    light_builder varchar(64) comment '信号灯建设时间',
    build_year varchar(64) comment '建成年度',
    warranty_date datetime comment '质保时间',
    repair_remark datetime comment '改造说明',
    signal_brand varchar(64) comment '信号机品牌',
    signal_model varchar(64) comment '信号机类型',
    electricity_provider varchar(64) comment '供电情况',
    electricity_way varchar(64) comment '供电分类:1正式报装电源、2正式借电协议、3临时借电协议' ,
    electricity_source varchar(64) comment '接电来源',
    description varchar(128) comment '路口描述',

    vehicle_light_diameter varchar(64) comment '机动车灯头型号',
    vehicle_circle_light_east varchar(64) comment '路东 机动车 全屏灯 数量',
    vehicle_arrow_light_east varchar(64) comment '路东 机动车 箭头灯 数量',
    vehicle_circle_light_west varchar(64) comment '路西 机动车 全屏灯 数量',
    vehicle_arrow_light_west varchar(64) comment '路西 机动车 箭头灯 数量',
    vehicle_circle_light_south varchar(64) comment '路南 机动车 全屏灯 数量',
    vehicle_arrow_light_south varchar(64) comment '路南 机动车 箭头灯 数量',
    vehicle_circle_light_north varchar(64) comment '路北 机动车 全屏灯 数量',
    vehicle_arrow_light_north varchar(64) comment '路北 机动车 箭头灯 数量',

#     机动车杆型
#         东			南			西			北
#         长伸臂m	97弯臂m	立柱式m	长伸臂m	97弯臂m	立柱式m	长伸臂m	97弯臂m	立柱式m	长伸臂m	97弯臂m	立柱式m

    vehicle_straight_rod_east varchar(64) comment '路东 机动车杆型  长伸臂m',
    vehicle_arm97_rod_east varchar(64) comment '路东 机动车杆型  97弯臂m',
    vehicle_self_rod_east varchar(64) comment '路东 机动车杆型  立柱式m',
    vehicle_straight_rod_west varchar(64) comment '路西 机动车杆型  长伸臂m',
    vehicle_arm97_rod_west varchar(64) comment '路西 机动车杆型  97弯臂m',
    vehicle_self_rod_west varchar(64) comment '路西 机动车杆型  立柱式m',
    vehicle_straight_rod_south varchar(64) comment '路南 机动车杆型  长伸臂m',
    vehicle_arm97_rod_south varchar(64) comment '路南 机动车杆型  97弯臂m',
    vehicle_self_rod_south varchar(64) comment '路南 机动车杆型  立柱式m',
    vehicle_straight_rod_north varchar(64) comment '路北 机动车杆型  长伸臂m',
    vehicle_arm97_rod_north varchar(64) comment '路北 机动车杆型  97弯臂m',
    vehicle_self_rod_north varchar(64) comment '路北 机动车杆型  立柱式m',

    non_vehicle_light_diameter varchar(64) comment '非机动车灯头型号',
    non_vehicle_light_east varchar(64) comment '路东 非机动车道  自行车灯',
    non_vehicle_light_west varchar(64) comment '路西 非机动车道  自行车灯',
    non_vehicle_light_south varchar(64) comment '路南 非机动车道  自行车灯',
    non_vehicle_light_north varchar(64) comment '路北 非机动车道  自行车灯',

    human_light_model varchar(64) comment '人行步道灯   灯型',
    human_light_east varchar(64) comment '路东 人行步道灯',
    human_light_west varchar(64) comment '路西 人行步道灯',
    human_light_south varchar(64) comment '路南 人行步道灯',
    human_light_north varchar(64) comment '路北 人行步道灯',

    human_rod_south_east varchar(64) comment '人行步道灯  东南',
    human_rod_south_west varchar(64) comment '人行步道灯  西南',
    human_rod_north_east varchar(64) comment '人行步道灯  东北',
    human_rod_north_west varchar(64) comment '人行步道灯  西北',

    adjust_light text comment '调灯方案',

    vehicle_light varchar(64) comment '机动车灯数量' ,
    non_vehicle_light varchar(64) comment '非机动车灯数量' ,
    human_light varchar(64) comment '人行灯数量',
    arm_rod varchar(64) comment '弯臂灯杆',
    straight_rod varchar(64) comment '直杆',
    self_rod varchar(64) comment '自助灯杆',
    signal_cover varchar(64) comment '信号灯井盖',

    longitude varchar(64) comment '经度',
    latitude varchar(64) comment '纬度'
)engine =InnoDB;