-- =============================================
-- 翰高 PostgreSQL 建表语句
-- =============================================

-- 如果之前创建了有问题的表，先删除
DROP TABLE IF EXISTS job_data_storage_detection;
DROP TABLE IF EXISTS job_data_storage_detail;
DROP TABLE IF EXISTS job_index;
DROP TABLE IF EXISTS website_management;
DROP TABLE IF EXISTS job_data_trend;

-- 1. 岗位数据入库检测
CREATE TABLE job_data_storage_detection (
    detection_id    VARCHAR(50) NOT NULL,
    storage_date    TIMESTAMP,
    storage_number  INTEGER,
    storage_result  VARCHAR(500),
    storage_detail  TEXT,
    failed_reason   VARCHAR(500),
    del_flag        CHAR(1) DEFAULT '0',
    create_by       VARCHAR(64),
    create_time     VARCHAR(50),
    update_by       VARCHAR(64),
    update_time     VARCHAR(50),
    remark          VARCHAR(500),
    CONSTRAINT pk_job_data_storage_detection PRIMARY KEY (detection_id)
);

COMMENT ON TABLE  job_data_storage_detection IS '岗位数据入库监测记录';
COMMENT ON COLUMN job_data_storage_detection.detection_id   IS '监测主键ID';
COMMENT ON COLUMN job_data_storage_detection.storage_date   IS '入库日期';
COMMENT ON COLUMN job_data_storage_detection.storage_number IS '入库数量';
COMMENT ON COLUMN job_data_storage_detection.storage_result IS '入库结果简述';
COMMENT ON COLUMN job_data_storage_detection.storage_detail IS '入库详情';
COMMENT ON COLUMN job_data_storage_detection.failed_reason  IS '失败原因';
COMMENT ON COLUMN job_data_storage_detection.del_flag       IS '删除标志（0代表存在 2代表删除）';
COMMENT ON COLUMN job_data_storage_detection.create_by      IS '创建者';
COMMENT ON COLUMN job_data_storage_detection.create_time    IS '创建时间';
COMMENT ON COLUMN job_data_storage_detection.update_by      IS '更新者';
COMMENT ON COLUMN job_data_storage_detection.update_time    IS '更新时间';
COMMENT ON COLUMN job_data_storage_detection.remark         IS '备注';

-- 2. 工作数据入库详情
CREATE TABLE job_data_storage_detail (
    detail_id       BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    detection_id    VARCHAR(50),
    website_id      BIGINT,
    website_name    VARCHAR(200),
    storage_time    TIMESTAMP,
    success_number  INTEGER,
    failed_number   INTEGER,
    storage_detail  TEXT,
    failed_reason   TEXT,
    remark          VARCHAR(500)
);

COMMENT ON TABLE  job_data_storage_detail IS '工作数据入库详情（分来源网站）';
COMMENT ON COLUMN job_data_storage_detail.detail_id      IS '详情主键ID';
COMMENT ON COLUMN job_data_storage_detail.detection_id   IS '监测ID';
COMMENT ON COLUMN job_data_storage_detail.website_id     IS '入库来源网站ID';
COMMENT ON COLUMN job_data_storage_detail.website_name   IS '入库来源网站名称';
COMMENT ON COLUMN job_data_storage_detail.storage_time   IS '入库具体时间';
COMMENT ON COLUMN job_data_storage_detail.success_number IS '入库成功数量';
COMMENT ON COLUMN job_data_storage_detail.failed_number  IS '入库失败数量';
COMMENT ON COLUMN job_data_storage_detail.storage_detail IS '入库数据详情';
COMMENT ON COLUMN job_data_storage_detail.failed_reason  IS '入库失败原因';
COMMENT ON COLUMN job_data_storage_detail.remark         IS '备注';

-- 3. 岗位指标
CREATE TABLE job_index (
    index_id    BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    index_name  VARCHAR(200),
    index_desc  VARCHAR(500),
    is_active   CHAR(1) DEFAULT '1',
    del_flag    CHAR(1) DEFAULT '0',
    create_by   VARCHAR(64),
    create_time VARCHAR(50),
    update_by   VARCHAR(64),
    update_time VARCHAR(50),
    remark      VARCHAR(500)
);

COMMENT ON TABLE  job_index IS '岗位指标';
COMMENT ON COLUMN job_index.index_id    IS '指标ID';
COMMENT ON COLUMN job_index.index_name  IS '指标名称';
COMMENT ON COLUMN job_index.index_desc  IS '指标描述';
COMMENT ON COLUMN job_index.is_active   IS '是否启用（1：启用，0：未启用）';
COMMENT ON COLUMN job_index.del_flag    IS '删除标志（0代表存在 2代表删除）';
COMMENT ON COLUMN job_index.create_by   IS '创建者';
COMMENT ON COLUMN job_index.create_time IS '创建时间';
COMMENT ON COLUMN job_index.update_by   IS '更新者';
COMMENT ON COLUMN job_index.update_time IS '更新时间';
COMMENT ON COLUMN job_index.remark      IS '备注';

-- 4. 岗位信息来源
CREATE TABLE website_management (
    website_id            BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    website_name          VARCHAR(200),
    website_url           VARCHAR(500),
    website_owner_company VARCHAR(200),
    is_active             CHAR(1) DEFAULT '1',
    data_type             VARCHAR(10),
    del_flag              CHAR(1) DEFAULT '0',
    create_by             VARCHAR(64),
    create_time           VARCHAR(50),
    update_by             VARCHAR(64),
    update_time           VARCHAR(50),
    remark                VARCHAR(500)
);

COMMENT ON TABLE  website_management IS '岗位信息来源';
COMMENT ON COLUMN website_management.website_id            IS '网站ID';
COMMENT ON COLUMN website_management.website_name          IS '网站名称';
COMMENT ON COLUMN website_management.website_url           IS '网站地址';
COMMENT ON COLUMN website_management.website_owner_company IS '网站归属单位公司';
COMMENT ON COLUMN website_management.is_active             IS '是否启用（1：启用，0：未启用）';
COMMENT ON COLUMN website_management.data_type             IS '数据类型（1：系统数据，2：外部数据）';
COMMENT ON COLUMN website_management.del_flag              IS '删除标志（0代表存在 2代表删除）';
COMMENT ON COLUMN website_management.create_by             IS '创建者';
COMMENT ON COLUMN website_management.create_time           IS '创建时间';
COMMENT ON COLUMN website_management.update_by             IS '更新者';
COMMENT ON COLUMN website_management.update_time           IS '更新时间';
COMMENT ON COLUMN website_management.remark                IS '备注';

-- 5. 岗位数据采集趋势统计
CREATE TABLE job_data_trend (
    id           BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    delete_count BIGINT,
    insert_count BIGINT,
    fail_count   BIGINT,
    storage_time TIMESTAMP,
    website_name VARCHAR(64),
    del_flag     CHAR(1) DEFAULT '0',
    create_by    VARCHAR(64),
    create_time  VARCHAR(50),
    update_by    VARCHAR(64),
    update_time  VARCHAR(50),
    remark       VARCHAR(500)
);

COMMENT ON TABLE  job_data_trend IS '岗位数据采集趋势统计表';
COMMENT ON COLUMN job_data_trend.id           IS '主键ID';
COMMENT ON COLUMN job_data_trend.delete_count IS '逻辑删除数量';
COMMENT ON COLUMN job_data_trend.insert_count IS '新增入库数量';
COMMENT ON COLUMN job_data_trend.fail_count   IS '处理失败/重复数量';
COMMENT ON COLUMN job_data_trend.storage_time IS '统计归集时间';
COMMENT ON COLUMN job_data_trend.website_name IS '网站名称';
COMMENT ON COLUMN job_data_trend.del_flag     IS '删除标志（0代表存在 2代表删除）';
COMMENT ON COLUMN job_data_trend.create_by    IS '创建者';
COMMENT ON COLUMN job_data_trend.create_time  IS '创建时间';
COMMENT ON COLUMN job_data_trend.update_by    IS '更新者';
COMMENT ON COLUMN job_data_trend.update_time  IS '更新时间';
COMMENT ON COLUMN job_data_trend.remark       IS '备注';
