DROP TABLE IF EXISTS sys_user;
CREATE TABLE sys_user
(
    id          BIGINT                         NOT NULL COMMENT 'Id',
    biz_obj     VARCHAR(32)                    NOT NULL COMMENT '业务对象; 字典代码：biz_obj',
    nickname    VARCHAR(255) DEFAULT ''        NOT NULL COMMENT '昵称',
    avatar      VARCHAR(255) DEFAULT ''        NOT NULL COMMENT '头像',
    gender      VARCHAR(10)  DEFAULT 'Unknown' NOT NULL COMMENT '性别; 字典代码：gender',
    email       VARCHAR(255) DEFAULT ''        NOT NULL COMMENT '邮箱',
    phone       VARCHAR(20)  DEFAULT ''        NOT NULL COMMENT '手机号',
    tenant_id   BIGINT                         NOT NULL COMMENT '租户 Id; sys_tenant.id',
    creator_id  BIGINT                         NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id BIGINT                         NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time DATETIME                       NOT NULL COMMENT '创建时间',
    modify_time DATETIME                       NOT NULL COMMENT '修改时间',
    deleted     TINYINT(1)   DEFAULT 0         NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '用户信息表';

DROP TABLE IF EXISTS sys_role_resource;
CREATE TABLE sys_role_resource
(
    id         BIGINT       NOT NULL COMMENT 'Id',
    role_id    BIGINT       NOT NULL COMMENT '角色 Id; sys_role.id',
    res_id     BIGINT       NOT NULL COMMENT '资源 Id; sys_resource.id',
    res_sn     VARCHAR(255) NOT NULL COMMENT '资源编码; sys_resource.sn',
    table_name VARCHAR(255) NOT NULL COMMENT '表名称',
    data_id    BIGINT       NOT NULL COMMENT '数据行 Id',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '角色-资源关系表';

CREATE UNIQUE INDEX uk__role_id__menu_id ON sys_role_resource (role_id, res_id);

DROP TABLE IF EXISTS sys_tenant;
CREATE TABLE sys_tenant
(
    id            BIGINT               NOT NULL COMMENT 'Id',
    sn            VARCHAR(255)         NOT NULL COMMENT '编号',
    tenant_name   VARCHAR(255)         NOT NULL COMMENT '租户名称',
    tenant_ststus VARCHAR(255)         NOT NULL COMMENT '状态; 字典编码：tenant_ststus',
    creator_id    BIGINT               NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id   BIGINT               NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time   DATETIME             NOT NULL COMMENT '创建时间',
    modify_time   DATETIME             NOT NULL COMMENT '修改时间',
    deleted       TINYINT(1) DEFAULT 0 NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '租户表';

DROP TABLE IF EXISTS sys_user_account;
CREATE TABLE sys_user_account
(
    id             BIGINT                  NOT NULL COMMENT 'Id',
    user_id        BIGINT                  NOT NULL COMMENT '用户 Id; sys_user.id',
    username       VARCHAR(255) DEFAULT '' NOT NULL COMMENT '用户名',
    email          VARCHAR(255) DEFAULT '' NOT NULL COMMENT '邮箱',
    phone          VARCHAR(20)  DEFAULT '' NOT NULL COMMENT '手机号',
    secret         VARCHAR(255) DEFAULT '' NOT NULL COMMENT '密码',
    wechat_openid  VARCHAR(255) DEFAULT '' NOT NULL COMMENT '微信 openid',
    wechat_unionid VARCHAR(255) DEFAULT '' NOT NULL COMMENT '微信 unionid',
    regdate        DATETIME                NOT NULL COMMENT '注册时间',
    client_code    INT          DEFAULT 0  NOT NULL COMMENT '允许登录的客户端; 字典代码：client_code',
    disabled       TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否禁用; 0-->启用、1-->禁用',
    creator_id     BIGINT                  NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id    BIGINT                  NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time    DATETIME                NOT NULL COMMENT '创建时间',
    modify_time    DATETIME                NOT NULL COMMENT '修改时间',
    deleted        TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '用户账号信息表';

CREATE UNIQUE INDEX uk__user_id ON sys_user_account (user_id);
CREATE UNIQUE INDEX uk__username ON sys_user_account (username);
CREATE INDEX ix__phone ON sys_user_account (phone);
CREATE INDEX ix__wechat_openid__wechat_unionid ON sys_user_account (wechat_openid, wechat_unionid);

DROP TABLE IF EXISTS sys_user_role;
CREATE TABLE sys_user_role
(
    id      BIGINT NOT NULL COMMENT 'Id',
    user_id BIGINT NOT NULL COMMENT '用户 Id; sys_user.id',
    role_id BIGINT NOT NULL COMMENT '角色 Id; sys_role.id',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '用户-角色关系表';

CREATE UNIQUE INDEX uk__user_id__role_id ON sys_user_role (user_id, role_id);

DROP TABLE IF EXISTS sys_role;
CREATE TABLE sys_role
(
    id          BIGINT                  NOT NULL COMMENT 'Id',
    role_code   VARCHAR(64)             NOT NULL COMMENT '角色代码; 以 ROLE_ 开头',
    role_name   VARCHAR(64)             NOT NULL COMMENT '角色名称',
    memo        VARCHAR(512) DEFAULT '' NOT NULL COMMENT '备注',
    creator_id  BIGINT                  NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id BIGINT                  NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time DATETIME                NOT NULL COMMENT '创建时间',
    modify_time DATETIME                NOT NULL COMMENT '修改时间',
    deleted     TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '角色表';

CREATE UNIQUE INDEX uk__role_code ON sys_role (role_code);

DROP TABLE IF EXISTS sys_endpoint;
CREATE TABLE sys_endpoint
(
    id             BIGINT                         NOT NULL COMMENT 'Id',
    request_method VARCHAR(10)                    NOT NULL COMMENT '请求方式; 字典代码：request_method',
    routing_path   VARCHAR(255) DEFAULT ''        NOT NULL COMMENT '路由前缀; 以 / 开头 或 为空',
    endpoint_path  VARCHAR(255)                   NOT NULL COMMENT '端点地址; 以 / 开头, Ant 匹配模式',
    access_model   VARCHAR(64)  DEFAULT 'Logined' NOT NULL COMMENT '接口访问模式; 字典代码：endpoint_access_model',
    memo           VARCHAR(512) DEFAULT ''        NOT NULL COMMENT '备注',
    creator_id     BIGINT                         NOT NULL COMMENT '创建人 Id',
    modifier_id    BIGINT                         NOT NULL COMMENT '修改人 Id',
    create_time    DATETIME                       NOT NULL COMMENT '创建时间',
    modify_time    DATETIME                       NOT NULL COMMENT '修改时间',
    deleted        TINYINT(1)   DEFAULT 0         NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '端点信息表';

CREATE UNIQUE INDEX uk__request_method__routing_path__endpoint_path ON sys_endpoint (request_method, routing_path, endpoint_path);

DROP TABLE IF EXISTS sys_menu;
CREATE TABLE sys_menu
(
    id            BIGINT                    NOT NULL COMMENT 'Id',
    sn            VARCHAR(255)              NOT NULL COMMENT '编号',
    pid           BIGINT                    NOT NULL COMMENT '上级 Id; 层级为 1 的节点值为 0',
    title         VARCHAR(255)              NOT NULL COMMENT '菜单名称',
    icon          VARCHAR(255) DEFAULT ''   NOT NULL COMMENT '图标',
    tier          INT                       NOT NULL COMMENT '层级; >= 1',
    breadcrumb    VARCHAR(512) DEFAULT '[]' NOT NULL COMMENT '面包路径; 逗号分隔',
    menu_category VARCHAR(32)               NOT NULL COMMENT '类型; 字典代码：menu_category',
    freeze        TINYINT(1)   DEFAULT 0    NOT NULL COMMENT '标签是否冻结; 0-->否、1-->是',
    sort          INT                       NOT NULL COMMENT '排序',
    route_name    VARCHAR(128) DEFAULT ''   NOT NULL COMMENT '路由名称',
    creator_id    BIGINT                    NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id   BIGINT                    NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time   DATETIME                  NOT NULL COMMENT '创建时间',
    modify_time   DATETIME                  NOT NULL COMMENT '修改时间',
    deleted       TINYINT(1)   DEFAULT 0    NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '菜单信息表';

DROP TABLE IF EXISTS sys_file;
CREATE TABLE sys_file
(
    id            BIGINT               NOT NULL COMMENT 'Id',
    bucket_name   VARCHAR(255)         NOT NULL COMMENT '桶名称',
    object_name   VARCHAR(512)         NOT NULL COMMENT '对象名称',
    original_name VARCHAR(255)         NOT NULL COMMENT '原文件名称',
    size          BIGINT               NOT NULL COMMENT '文件大小; 单位: 字节',
    mime          VARCHAR(255)         NOT NULL COMMENT 'MIME 类型',
    link          VARCHAR(512)         NOT NULL COMMENT '访问链接',
    file_service  VARCHAR(10)          NOT NULL COMMENT '存储服务; 字典代码：file_service',
    creator_id    BIGINT               NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id   BIGINT               NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time   DATETIME             NOT NULL COMMENT '创建时间',
    modify_time   DATETIME             NOT NULL COMMENT '修改时间',
    deleted       TINYINT(1) DEFAULT 0 NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '文件记录表';

DROP TABLE IF EXISTS sys_file_ref;
CREATE TABLE sys_file_ref
(
    id          BIGINT       NOT NULL COMMENT 'Id',
    coord_table VARCHAR(255) NOT NULL COMMENT '表名称',
    coord_field VARCHAR(255) NOT NULL COMMENT '表字段',
    coord_id    BIGINT(255)  NOT NULL COMMENT '表记录 Id',
    file_id     BIGINT       NOT NULL COMMENT '文件 Id; sys_file.id',
    link        VARCHAR(512) NOT NULL COMMENT '访问链接; sys_file.link',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '文件引用表';

CREATE UNIQUE INDEX uk__coord_id__coord_field__coord_table__file_id ON sys_file_ref (coord_id, coord_field, coord_table, file_id);

DROP TABLE IF EXISTS sys_login_record;
CREATE TABLE sys_login_record
(
    id              BIGINT                  NOT NULL COMMENT 'Id',
    user_id         BIGINT                  NOT NULL COMMENT '用户 Id; sys_user.id',
    user_account_id BIGINT                  NOT NULL COMMENT '用户账号 Id; sys_user_account.id',
    login_time      DATETIME                NOT NULL COMMENT '登录时间',
    client_code     INT                     NOT NULL COMMENT '本次登录使用的客户端; 字典代码：client_code',
    auth_way        VARCHAR(50)             NOT NULL COMMENT '登录方式; 字典代码：auth_way',
    user_agent      VARCHAR(512) DEFAULT '' NOT NULL COMMENT '用户代理',
    ip              VARCHAR(128) DEFAULT '' NOT NULL COMMENT 'IP 地址',
    province_code   BIGINT                  NULL COMMENT 'IP 归属地, 省 (编码); sys_district.id',
    province_name   VARCHAR(255) DEFAULT '' NOT NULL COMMENT 'IP 归属地, 省 (名称); sys_district.name',
    city_code       BIGINT                  NULL COMMENT 'IP 归属地, 市 (编码); sys_district.id',
    city_name       VARCHAR(255) DEFAULT '' NOT NULL COMMENT 'IP 归属地, 市 (名称); sys_district.name',
    area_code       BIGINT                  NULL COMMENT 'IP 归属地, 区 (编码); sys_district.id',
    area_name       VARCHAR(255) DEFAULT '' NOT NULL COMMENT 'IP 归属地, 区 (名称); sys_district.name',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '用户登录记录表';

CREATE INDEX ix__user_id ON sys_login_record (user_id);
CREATE INDEX ix__user_account_id ON sys_login_record (user_account_id);

DROP TABLE IF EXISTS sys_dict;
CREATE TABLE sys_dict
(
    id        BIGINT                  NOT NULL COMMENT 'Id',
    dict_key  VARCHAR(255)            NOT NULL COMMENT '字典标识',
    dict_name VARCHAR(255)            NOT NULL COMMENT '字典名称',
    memo      VARCHAR(512) DEFAULT '' NOT NULL COMMENT '备注',
    deleted   TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '字典表';

CREATE UNIQUE INDEX uk__dict_key ON sys_dict (dict_key);

DROP TABLE IF EXISTS sys_district;
CREATE TABLE sys_district
(
    id            VARCHAR(16)            NOT NULL COMMENT 'Id; 地区代码',
    pid           VARCHAR(16)            NOT NULL COMMENT '上级地区代码',
    province      VARCHAR(16) DEFAULT '' NOT NULL COMMENT '省',
    city          VARCHAR(16) DEFAULT '' NOT NULL COMMENT '市',
    area          VARCHAR(16) DEFAULT '' NOT NULL COMMENT '区县',
    town          VARCHAR(16) DEFAULT '' NOT NULL COMMENT '乡镇街道',
    district_name VARCHAR(255)           NOT NULL COMMENT '地区名称',
    tier          INT                    NOT NULL COMMENT '层级; >= 1',
    sort          INT         DEFAULT 0  NOT NULL COMMENT '排序',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '省市区表';

DROP TABLE IF EXISTS sys_dict_item;
CREATE TABLE sys_dict_item
(
    id       BIGINT                  NOT NULL COMMENT 'Id',
    dict_id  BIGINT                  NOT NULL COMMENT '字典 Id; sys_dict.id',
    dict_key VARCHAR(255)            NOT NULL COMMENT '字典标识; sys_dict.dict_key',
    val      VARCHAR(100)            NOT NULL COMMENT '值; 分类值/字典项值',
    txt      VARCHAR(100)            NOT NULL COMMENT '显示文本; 分类显示文本/字典项显示文本',
    sort     INT                     NOT NULL COMMENT '排序',
    memo     VARCHAR(512) DEFAULT '' NOT NULL COMMENT '备注',
    deleted  TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '字典条目表';

CREATE INDEX ix__dict_id ON sys_dict_item (dict_id);
CREATE INDEX ix__dict_key ON sys_dict_item (dict_key);

DROP TABLE IF EXISTS sys_resource;
CREATE TABLE sys_resource
(
    id         BIGINT                  NOT NULL COMMENT 'Id',
    sn         VARCHAR(255)            NOT NULL COMMENT '编号',
    table_name VARCHAR(255)            NOT NULL COMMENT '表名称',
    data_id    BIGINT                  NOT NULL COMMENT '数据行 Id',
    memo       VARCHAR(512) DEFAULT '' NOT NULL COMMENT '备注',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '系统资源表';

DROP TABLE IF EXISTS sys_token;
CREATE TABLE sys_token
(
    id          BIGINT       NOT NULL COMMENT 'Id',
    user_id     BIGINT       NOT NULL COMMENT '用户 Id',
    tid         VARCHAR(128) NOT NULL COMMENT 'Token Id',
    tkey        VARCHAR(512) NOT NULL COMMENT 'Token 键',
    tval        TEXT         NOT NULL COMMENT 'Token 值',
    user_detail TEXT         NOT NULL COMMENT '用户信息',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '登录令牌表';

DROP TABLE IF EXISTS biz_company;
CREATE TABLE biz_company
(
    id                    BIGINT                    NOT NULL COMMENT 'Id',
    uscc                  VARCHAR(128)              NOT NULL COMMENT '统一社会信用代码',
    user_id               BIGINT                    NOT NULL COMMENT '用户 Id; sys_user.id',
    biz_obj               VARCHAR(32)               NOT NULL COMMENT '业务对象; 字典代码：biz_obj',
    company_name          VARCHAR(255)              NOT NULL COMMENT '企业名称',
    business_license      VARCHAR(255)              NOT NULL COMMENT '营业执照; 图片',
    certification         VARCHAR(255)              NOT NULL COMMENT '资质证明; 图片',
    business_license_date VARCHAR(64)  DEFAULT '[]' NOT NULL COMMENT '营业执照有效期; [开始日期，结束日期]',
    certification_date    VARCHAR(64)  DEFAULT '[]' NOT NULL COMMENT '资质证明有效期; [开始日期，结束日期]',
    legal_representative  VARCHAR(255)              NOT NULL COMMENT '法人名称',
    province              VARCHAR(255) DEFAULT ''   NOT NULL COMMENT '省; 名称',
    city                  VARCHAR(255) DEFAULT ''   NOT NULL COMMENT '市; 名称',
    county                VARCHAR(255) DEFAULT ''   NOT NULL COMMENT '区; 名称',
    street                VARCHAR(255) DEFAULT ''   NOT NULL COMMENT '街道; 名称',
    address               VARCHAR(255) DEFAULT ''   NOT NULL COMMENT '详细地址',
    contacts              VARCHAR(32)  DEFAULT ''   NOT NULL COMMENT '联系人',
    phone_num             VARCHAR(32)  DEFAULT ''   NOT NULL COMMENT '联系电话',
    audit_status          VARCHAR(64)               NOT NULL COMMENT '审核状态; 字典代码：audit_status',
    audit_memo            VARCHAR(512) DEFAULT ''   NOT NULL COMMENT '审核备注',
    creator_id            BIGINT                    NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id           BIGINT                    NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time           DATETIME                  NOT NULL COMMENT '创建时间',
    modify_time           DATETIME                  NOT NULL COMMENT '修改时间',
    deleted               TINYINT(1)   DEFAULT 0    NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '企业信息';

DROP TABLE IF EXISTS biz_truck;
CREATE TABLE biz_truck
(
    id                BIGINT                  NOT NULL COMMENT 'Id',
    user_id           BIGINT                  NOT NULL COMMENT '用户 Id; sys_user.id',
    company_id        BIGINT                  NOT NULL COMMENT '清运公司 Id; biz_company.id',
    license_plate     VARCHAR(16)             NOT NULL COMMENT '车牌',
    truck_license     VARCHAR(255)            NOT NULL COMMENT '行驶证; 图片',
    vn_code           VARCHAR(128)            NOT NULL COMMENT '车架号',
    qualification     VARCHAR(255)            NOT NULL COMMENT '车辆合格证; 图片',
    carrying_capacity VARCHAR(32)             NOT NULL COMMENT '最大载重; 单位：吨',
    gps               VARCHAR(128) DEFAULT '' NOT NULL COMMENT 'GPS',
    creator_id        BIGINT                  NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id       BIGINT                  NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time       DATETIME                NOT NULL COMMENT '创建时间',
    modify_time       DATETIME                NOT NULL COMMENT '修改时间',
    deleted           TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '车辆信息';

DROP TABLE IF EXISTS biz_scope;
CREATE TABLE biz_scope
(
    id          BIGINT                  NOT NULL COMMENT 'Id',
    user_id     BIGINT                  NOT NULL COMMENT '用户 Id; sys_user.id',
    biz_obj     VARCHAR(32)             NOT NULL COMMENT '业务对象; 字典代码：biz_obj',
    company_id  BIGINT                  NOT NULL COMMENT '清运公司/消纳场 Id; biz_company.id',
    province    VARCHAR(255) DEFAULT '' NOT NULL COMMENT '省; 名称',
    city        VARCHAR(255) DEFAULT '' NOT NULL COMMENT '市; 名称',
    county      VARCHAR(255) DEFAULT '' NOT NULL COMMENT '区; 名称',
    street      VARCHAR(255) DEFAULT '' NOT NULL COMMENT '街道; 名称',
    creator_id  BIGINT                  NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id BIGINT                  NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time DATETIME                NOT NULL COMMENT '创建时间',
    modify_time DATETIME                NOT NULL COMMENT '修改时间',
    deleted     TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '服务区域';

DROP TABLE IF EXISTS biz_contract;
CREATE TABLE biz_contract
(
    id            BIGINT                  NOT NULL COMMENT 'Id',
    user_id       BIGINT                  NOT NULL COMMENT '所属用户 Id; sys_user.id',
    biz_obj       VARCHAR(32)             NOT NULL COMMENT '业务对象; 字典代码：biz_obj',
    company_id    BIGINT                  NOT NULL COMMENT '清运公司/消纳场 Id; biz_company.id',
    contract      VARCHAR(255)            NOT NULL COMMENT '合同; 图片',
    contract_date VARCHAR(512)            NOT NULL COMMENT '合同有效期; json',
    memo          VARCHAR(512) DEFAULT '' NOT NULL COMMENT '备注',
    creator_id    BIGINT                  NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id   BIGINT                  NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time   DATETIME                NOT NULL COMMENT '创建时间',
    modify_time   DATETIME                NOT NULL COMMENT '修改时间',
    deleted       TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '合同报备';

DROP TABLE IF EXISTS biz_violate_rule_truck;
CREATE TABLE biz_violate_rule_truck
(
    id                       BIGINT                  NOT NULL COMMENT 'Id',
    project_id               BIGINT                  NOT NULL COMMENT '',
    project_name             VARCHAR(512)            NOT NULL COMMENT '',
    producer_id              BIGINT                  NOT NULL COMMENT '',
    producer_name            VARCHAR(128)            NOT NULL COMMENT '',
    contacts                 VARCHAR(32)  DEFAULT '' NOT NULL COMMENT '联系人',
    phone_num                VARCHAR(32)  DEFAULT '' NOT NULL COMMENT '联系电话',
    clearance_company_id     BIGINT                  NOT NULL COMMENT '清运公司 Id; biz_company.id',
    clearance_company_name   VARCHAR(128)            NOT NULL COMMENT '',
    consumption_company_id   BIGINT                  NOT NULL COMMENT '消纳场 Id; biz_company.id',
    consumption_company_name VARCHAR(128)            NOT NULL COMMENT '',
    report_time              DATETIME                NOT NULL COMMENT '',
    violate_rule_reason      VARCHAR(512)            NOT NULL COMMENT '',
    process_status           VARCHAR(64)             NOT NULL COMMENT '处理状态; 字典编号：process_status',
    process_result           VARCHAR(512) DEFAULT '' NOT NULL COMMENT '处理结果',
    creator_id               BIGINT                  NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id              BIGINT                  NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time              DATETIME                NOT NULL COMMENT '创建时间',
    modify_time              DATETIME                NOT NULL COMMENT '修改时间',
    deleted                  TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '违规车辆列表';

DROP TABLE IF EXISTS biz_violate_rule_strategy;
CREATE TABLE biz_violate_rule_strategy
(
    id                  BIGINT               NOT NULL COMMENT 'Id',
    unusual_stop_time   TINYINT(1) DEFAULT 0 NOT NULL COMMENT '异常停留时间',
    trajectory_mutation TINYINT(1) DEFAULT 0 NOT NULL COMMENT '轨迹突变',
    offline             TINYINT(1) DEFAULT 0 NOT NULL COMMENT '信号中断',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '违规策略';

DROP TABLE IF EXISTS biz_driver;
CREATE TABLE biz_driver
(
    id              BIGINT                 NOT NULL COMMENT 'Id',
    user_id         BIGINT                 NOT NULL COMMENT '用户 Id; sys_user.id',
    company_id      BIGINT                 NOT NULL COMMENT '清运公司 Id; biz_company.id',
    driver_name     VARCHAR(64)            NOT NULL COMMENT '司机名称',
    phone_num       VARCHAR(32) DEFAULT '' NOT NULL COMMENT '联系电话',
    driving_licence VARCHAR(255)           NOT NULL COMMENT '驾驶证; 图片',
    creator_id      BIGINT                 NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id     BIGINT                 NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time     DATETIME               NOT NULL COMMENT '创建时间',
    modify_time     DATETIME               NOT NULL COMMENT '修改时间',
    deleted         TINYINT(1)  DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '司机信息';

DROP TABLE IF EXISTS project_booking;
CREATE TABLE project_booking
(
    id                 BIGINT                           NOT NULL COMMENT 'Id',
    sn                 VARCHAR(255)                     NOT NULL COMMENT '编号',
    filing_id          BIGINT                           NULL COMMENT '备案 Id; project_filing.id',
    user_id            BIGINT                           NOT NULL COMMENT '用户 Id; sys_user.id',
    biz_obj            VARCHAR(32)                      NOT NULL COMMENT '业务对象; 字典代码：biz_obj',
    company_id         BIGINT                           NULL COMMENT '产废单位 Id; 个人预约置空，biz_company.id',
    company_name       VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '产废单位; 个人预约置空',
    project_name       VARCHAR(128) DEFAULT ''          NOT NULL COMMENT '项目名称',
    garbage_category   VARCHAR(32)                      NOT NULL COMMENT '垃圾类型; 字典编码：garbage_category',
    square             DOUBLE                           NULL COMMENT '装修面积',
    estimate_quantity  VARCHAR(32)                      NOT NULL COMMENT '预估垃圾量',
    contacts           VARCHAR(32)  DEFAULT ''          NOT NULL COMMENT '联系人',
    phone_num          VARCHAR(32)  DEFAULT ''          NOT NULL COMMENT '联系电话',
    site_photos        TEXT                             NOT NULL COMMENT '现场照片; json',
    province           VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '省; 名称，项目地',
    city               VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '市; 名称，项目地',
    county             VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '区; 名称，项目地',
    street             VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '街道; 名称，项目地',
    address            VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '详细地址; 项目地',
    booking_time       DATETIME                         NOT NULL COMMENT '预约时间',
    booking_status     VARCHAR(32)                      NOT NULL COMMENT '预约状态; 字典编码：booking_status',
    truck_quantity     INT                              NOT NULL COMMENT '派车数量',
    residue_quantity   INT                              NOT NULL COMMENT '剩余数量',
    dispatching_status VARCHAR(32)  DEFAULT 'DaiPaiChe' NOT NULL COMMENT '派车状态; 字典编码：dispatching_status',
    creator_id         BIGINT                           NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id        BIGINT                           NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time        DATETIME                         NOT NULL COMMENT '创建时间',
    modify_time        DATETIME                         NOT NULL COMMENT '修改时间',
    deleted            TINYINT(1)   DEFAULT 0           NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '预约订单';

DROP TABLE IF EXISTS project_filing;
CREATE TABLE project_filing
(
    id                BIGINT                  NOT NULL COMMENT 'Id',
    user_id           BIGINT                  NOT NULL COMMENT '用户 Id; sys_user.id',
    project_name      VARCHAR(128)            NOT NULL COMMENT '项目名称',
    garbage_category  VARCHAR(32)             NOT NULL COMMENT '垃圾类型; 字典编码：garbage_category',
    estimate_quantity VARCHAR(32)             NOT NULL COMMENT '预估垃圾量',
    province          VARCHAR(255) DEFAULT '' NOT NULL COMMENT '省; 名称，项目地',
    city              VARCHAR(255) DEFAULT '' NOT NULL COMMENT '市; 名称，项目地',
    county            VARCHAR(255) DEFAULT '' NOT NULL COMMENT '区; 名称，项目地',
    street            VARCHAR(255) DEFAULT '' NOT NULL COMMENT '街道; 名称，项目地',
    address           VARCHAR(255) DEFAULT '' NOT NULL COMMENT '详细地址; 项目地',
    contacts          VARCHAR(32)  DEFAULT '' NOT NULL COMMENT '联系人',
    phone_num         VARCHAR(32)  DEFAULT '' NOT NULL COMMENT '联系电话',
    contract          VARCHAR(255)            NOT NULL COMMENT '合同; 图片',
    audit_status      VARCHAR(32)             NOT NULL COMMENT '审核状态; 字典编码：audit_status',
    audit_memo        VARCHAR(512) DEFAULT '' NOT NULL COMMENT '审核备注',
    start_date        DATE                    NOT NULL COMMENT '开始时间',
    end_date          DATE                    NOT NULL COMMENT '结束时间',
    creator_id        BIGINT                  NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id       BIGINT                  NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time       DATETIME                NOT NULL COMMENT '创建时间',
    modify_time       DATETIME                NOT NULL COMMENT '修改时间',
    deleted           TINYINT(1)   DEFAULT 0  NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '项目备案信息';

DROP TABLE IF EXISTS project_info;
CREATE TABLE project_info
(
    id                 BIGINT                           NOT NULL COMMENT 'Id',
    sn                 VARCHAR(255)                     NOT NULL COMMENT '编号',
    project_source     VARCHAR(32)                      NOT NULL COMMENT '项目来源; 字典编码：project_source',
    project_source_id  BIGINT                           NOT NULL COMMENT '源单 Id; project_booking.id/project_filing.id',
    project_name       VARCHAR(128)                     NOT NULL COMMENT '项目名称',
    province           VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '省; 名称，项目地',
    city               VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '市; 名称，项目地',
    county             VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '区; 名称，项目地',
    street             VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '街道; 名称，项目地',
    address            VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '详细地址; 项目地',
    garbage_category   VARCHAR(32)                      NOT NULL COMMENT '垃圾类型; 字典编码：garbage_category',
    estimate_quantity  VARCHAR(32)                      NOT NULL COMMENT '预估垃圾量',
    contacts           VARCHAR(32)  DEFAULT ''          NOT NULL COMMENT '联系人',
    phone_num          VARCHAR(32)  DEFAULT ''          NOT NULL COMMENT '联系电话',
    contract           VARCHAR(255) DEFAULT ''          NOT NULL COMMENT '合同; 图片',
    site_photos        TEXT                             NOT NULL COMMENT '现场照片; json',
    start_date         DATE                             NULL COMMENT '开始时间',
    end_date           DATE                             NULL COMMENT '结束时间',
    progress_stage     VARCHAR(32)  DEFAULT 'WeiKaiShi' NOT NULL COMMENT '施工阶段; 字典编码：progress_stage',
    truck_quantity     INT                              NULL COMMENT '派车数量',
    dispatching_status VARCHAR(32)  DEFAULT 'DaiPaiChe' NOT NULL COMMENT '派车状态; 字典编码：dispatching_status',
    creator_id         BIGINT                           NOT NULL COMMENT '创建人 Id; sys_user.id',
    modifier_id        BIGINT                           NOT NULL COMMENT '修改人 Id; sys_user.id',
    create_time        DATETIME                         NOT NULL COMMENT '创建时间',
    modify_time        DATETIME                         NOT NULL COMMENT '修改时间',
    deleted            TINYINT(1)   DEFAULT 0           NOT NULL COMMENT '是否删除; 0-->未删除、1-->已删除',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '项目信息';

DROP TABLE IF EXISTS project_company;
CREATE TABLE project_company
(
    id                BIGINT      NOT NULL COMMENT 'Id',
    project_source    VARCHAR(32) NOT NULL COMMENT '项目来源; 字典编码：project_source',
    project_source_id BIGINT      NOT NULL COMMENT '源单 Id; project_booking.id/project_filing.id',
    biz_obj           VARCHAR(32) NOT NULL COMMENT '业务对象; 字典编码：biz_obj',
    company_id        BIGINT      NOT NULL COMMENT '清运公司/消纳场 Id; biz_company.id',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '项目清运公司/消纳场关系表';

DROP TABLE IF EXISTS project_clearance_detail;
CREATE TABLE project_clearance_detail
(
    id                     BIGINT       NOT NULL COMMENT 'Id',
    sn                     VARCHAR(255) NOT NULL COMMENT '编号',
    booking_id             BIGINT       NOT NULL COMMENT '预约单 Id; project_booking.id',
    biz_obj                VARCHAR(32)  NOT NULL COMMENT '业务对象; 字典代码：biz_obj',
    producing_id           BIGINT       NOT NULL COMMENT '产废单位; biz_company.id/sys_user.id',
    clearance_company_id   BIGINT       NOT NULL COMMENT '清运公司 Id; biz_company.id',
    consumption_company_id BIGINT       NOT NULL COMMENT '消纳场 Id; biz_company.id',
    garbage_category       VARCHAR(32)  NOT NULL COMMENT '垃圾类型; 字典编码：garbage_category',
    truck_id               BIGINT       NOT NULL COMMENT '车辆 Id; biz_truck.id',
    driver_id              BIGINT       NOT NULL COMMENT '司机 Id; biz_driver',
    clearance_status       VARCHAR(32)  NOT NULL COMMENT '状态; 字典编码：clearance_status',
    dispatching_time       DATETIME     NOT NULL COMMENT '派车时间',
    start_time             DATETIME     NOT NULL COMMENT '清运时间; 开始',
    end_time               DATETIME     NULL COMMENT '清运时间; 结束',
    bill_sn                VARCHAR(128) NULL COMMENT '业务系统单据编号',
    in_time                DATETIME     NULL COMMENT '进场时间',
    out_time               DATETIME     NULL COMMENT '出场时间',
    in_weight              VARCHAR(32)  NULL COMMENT '进场磅重',
    out_weight             VARCHAR(32)  NULL COMMENT '出场磅重',
    suttle_weight          VARCHAR(32)  NULL COMMENT '净重',
    photos                 TEXT         NOT NULL COMMENT '车辆照片; json',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '清运明细';

DROP TABLE IF EXISTS project_gps_data;
CREATE TABLE project_gps_data
(
    id           BIGINT   NOT NULL COMMENT 'Id',
    clearance_id BIGINT   NOT NULL COMMENT '清运明细 Id',
    lng          DOUBLE   NOT NULL COMMENT '经度',
    lat          DOUBLE   NOT NULL COMMENT '纬度',
    create_time  DATETIME NOT NULL COMMENT '创建时间',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '车辆 GPS 数据';

DROP TABLE IF EXISTS project_truck;
CREATE TABLE project_truck
(
    id               BIGINT   NOT NULL COMMENT 'Id',
    booking_id       BIGINT   NOT NULL COMMENT '项目 Id; project_info.id',
    company_id       BIGINT   NOT NULL COMMENT '清运公司 Id; biz_company.id',
    truck_id         BIGINT   NOT NULL COMMENT '车辆 Id; biz_truck.id',
    driver_id        BIGINT   NOT NULL COMMENT '司机 Id; biz_driver',
    dispatching_time DATETIME NOT NULL COMMENT '派车时间',
    PRIMARY KEY (id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_general_ci
    COMMENT = '项目派车记录';

