/*
 Navicat Premium Data Transfer

 Source Server         : 智慧法务
 Source Server Type    : PostgreSQL
 Source Server Version : 170005
 Source Host           : nas.7bm.co:54302
 Source Catalog        : docauditai
 Source Schema         : public

 Target Server Type    : PostgreSQL
 Target Server Version : 170005
 File Encoding         : 65001

 Date: 09/07/2025 10:25:25
*/


-- ----------------------------
-- Table structure for documents
-- ----------------------------
DROP TABLE IF EXISTS "public"."documents";
CREATE TABLE "public"."documents" (
  "id" int4 NOT NULL DEFAULT nextval('documents_id_seq'::regclass),
  "user_id" int4,
  "type_id" int4 NOT NULL,
  "name" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "document_number" varchar(100) COLLATE "pg_catalog"."default",
  "path" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "storage_type" varchar(20) COLLATE "pg_catalog"."default" NOT NULL DEFAULT 'minio'::character varying,
  "file_size" int4,
  "upload_time" timestamp(6) DEFAULT CURRENT_TIMESTAMP,
  "is_test_document" bool DEFAULT false,
  "evaluation_level" varchar(255) COLLATE "pg_catalog"."default",
  "status" varchar(20) COLLATE "pg_catalog"."default" DEFAULT 'waiting'::character varying,
  "ocr_result" jsonb,
  "extracted_results" jsonb,
  "sumary" text COLLATE "pg_catalog"."default",
  "remark" text COLLATE "pg_catalog"."default",
  "created_at" timestamptz(6) DEFAULT now(),
  "updated_at" timestamptz(6) DEFAULT now(),
  "evaluations_status" int4,
  "audit_status" int4,
  "error_massage" varchar(500) COLLATE "pg_catalog"."default"
)
;
COMMENT ON COLUMN "public"."documents"."id" IS '文档ID，主键，自增';
COMMENT ON COLUMN "public"."documents"."user_id" IS '上传用户的ID，外键引用users表，可为空';
COMMENT ON COLUMN "public"."documents"."type_id" IS '文档类型ID，外键引用document_types表，非空';
COMMENT ON COLUMN "public"."documents"."name" IS '原始文件名，非空';
COMMENT ON COLUMN "public"."documents"."document_number" IS '合同编号、许可证号等，可为空';
COMMENT ON COLUMN "public"."documents"."path" IS '存储路径，非空';
COMMENT ON COLUMN "public"."documents"."storage_type" IS '文件存储方式，默认minio';
COMMENT ON COLUMN "public"."documents"."file_size" IS '文件大小，单位字节，可为空';
COMMENT ON COLUMN "public"."documents"."upload_time" IS '上传时间，默认当前时间';
COMMENT ON COLUMN "public"."documents"."is_test_document" IS '是否是测试文档，默认false';
COMMENT ON COLUMN "public"."documents"."status" IS '处理状态，默认waiting（waiting: 上传时默认状态, Cutting: 切分+OCR处理中, extractioning: 大模型抽取信息中, evaluationing: 评查中, processed: 文档处理完成等待审核）';
COMMENT ON COLUMN "public"."documents"."ocr_result" IS 'OCR处理结果，JSONB格式';
COMMENT ON COLUMN "public"."documents"."extracted_results" IS '内容抽取结果，JSONB格式';
COMMENT ON COLUMN "public"."documents"."sumary" IS '评查结果，可为空';
COMMENT ON COLUMN "public"."documents"."remark" IS '备注，可为空';
COMMENT ON COLUMN "public"."documents"."created_at" IS '创建时间，带时区，默认当前时间';
COMMENT ON COLUMN "public"."documents"."updated_at" IS '更新时间，带时区，默认当前时间';
COMMENT ON COLUMN "public"."documents"."evaluations_status" IS '评查状态：
* 通过 ：1
* 警告 ：-2
* 不通过 ：-1
* 待人工确认 ：0';
COMMENT ON COLUMN "public"."documents"."audit_status" IS '审核状态：
* 待审核：0
* 通过：1
* 审核中：2
* 不通过：-1
* 警告：-2';
COMMENT ON TABLE "public"."documents" IS '文档表';

-- ----------------------------
-- Triggers structure for table documents
-- ----------------------------
CREATE TRIGGER "trg_set_updated_at" BEFORE UPDATE ON "public"."documents"
FOR EACH ROW
EXECUTE PROCEDURE "public"."update_updated_at"();
CREATE TRIGGER "update_documents_updated_at" BEFORE UPDATE ON "public"."documents"
FOR EACH ROW
EXECUTE PROCEDURE "public"."update_updated_at_column"();

-- ----------------------------
-- Primary Key structure for table documents
-- ----------------------------
ALTER TABLE "public"."documents" ADD CONSTRAINT "documents_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Foreign Keys structure for table documents
-- ----------------------------
ALTER TABLE "public"."documents" ADD CONSTRAINT "documents_type_id_fkey" FOREIGN KEY ("type_id") REFERENCES "public"."document_types" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "public"."documents" ADD CONSTRAINT "documents_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
