-- 企查查企业主体信息缓存表。

CREATE TABLE IF NOT EXISTS qcc_company_info (
    id BIGSERIAL PRIMARY KEY,
    search_key VARCHAR(200) NOT NULL,
    credit_code VARCHAR(64),
    company_name VARCHAR(255),
    enterprise JSONB,
    dishonesty JSONB,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMPTZ
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_qcc_company_search_key
    ON qcc_company_info (search_key)
    WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_qcc_company_credit_code
    ON qcc_company_info (credit_code)
    WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_qcc_company_name
    ON qcc_company_info (company_name)
    WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_qcc_company_created_at
    ON qcc_company_info (created_at)
    WHERE deleted_at IS NULL;

COMMENT ON TABLE qcc_company_info IS '企查查企业主体信息缓存表';
COMMENT ON COLUMN qcc_company_info.search_key IS '原始查询关键词';
COMMENT ON COLUMN qcc_company_info.credit_code IS '统一社会信用代码';
COMMENT ON COLUMN qcc_company_info.company_name IS '企业名称';
COMMENT ON COLUMN qcc_company_info.enterprise IS '企查查工商信息原始响应 Result';
COMMENT ON COLUMN qcc_company_info.dishonesty IS '企查查失信核查原始响应 Result';
