DROP TABLE IF EXISTS "public"."qrcode";
CREATE TABLE "public"."qrcode" (
  "id" serial,
  "url" varchar(255) NOT NULL,
  "name" varchar(128) NOT NULL,
  "type" varchar(32) NOT NULL,
  "key" uuid NOT NULL,
  "logo" varchar(255),
  PRIMARY KEY ("id")
);
COMMENT ON COLUMN "public"."qrcode"."url" IS '二维码图片存储链接';
COMMENT ON COLUMN "public"."qrcode"."name" IS '二维码名称';
COMMENT ON COLUMN "public"."qrcode"."type" IS '类型(图片|文件|链接|视频)';
COMMENT ON COLUMN "public"."qrcode"."key" IS '二维码唯一标识';
COMMENT ON COLUMN "public"."qrcode"."logo" IS '二维码中间的照片';


DROP TABLE IF EXISTS "public"."publicity_info";
CREATE TABLE "public"."publicity_info" (
  "id" serial,
  "name" varchar(128) NOT NULL,
  "type" varchar(32) NOT NULL,
  "time" timestamptz NOT NULL,
  "link" varchar(255),
  "qrcode_id" int4,
  PRIMARY KEY ("id"),
  CONSTRAINT "publicity_info_qrcode_id_fk" FOREIGN KEY ("qrcode_id") REFERENCES "public"."qrcode" ("id")
);
COMMENT ON COLUMN "public"."publicity_info"."name" IS '宣传标题';
COMMENT ON COLUMN "public"."publicity_info"."type" IS '类型(图片|文件|链接|视频)';
COMMENT ON COLUMN "public"."publicity_info"."time" IS '创建/更新时间';
COMMENT ON COLUMN "public"."publicity_info"."link" IS 'type为链接时的链接地址';
COMMENT ON COLUMN "public"."publicity_info"."qrcode_id" IS '关联二维码ID';


DROP TABLE IF EXISTS "public"."qrcode_files";
CREATE TABLE "public"."qrcode_files" (
  "id" serial,
  "file_name" varchar(128) NOT NULL,
  "file_size" int4 NOT NULL,
  "file_url" varchar(255) NOT NULL,
  "preview_img_url" varchar(255),
  "qrcode_id" int4,
  "publicity_info_id" int4,
  PRIMARY KEY ("id"),
  CONSTRAINT "qrcode_files_qrcode_id_fk" FOREIGN KEY ("qrcode_id") REFERENCES "public"."qrcode" ("id"),
  CONSTRAINT "qrcode_files_publicity_info_id_fk" FOREIGN KEY ("publicity_info_id") REFERENCES "public"."publicity_info" ("id")
);
COMMENT ON COLUMN "public"."qrcode_files"."file_name" IS '文件名';
COMMENT ON COLUMN "public"."qrcode_files"."file_size" IS '文件大小(byte)';
COMMENT ON COLUMN "public"."qrcode_files"."file_url" IS '文件存储链接';
COMMENT ON COLUMN "public"."qrcode_files"."preview_img_url" IS '文件预览图链接';
COMMENT ON COLUMN "public"."qrcode_files"."qrcode_id" IS '关联二维码ID';
COMMENT ON COLUMN "public"."qrcode_files"."publicity_info_id" IS '关联宣传信息ID';


DROP TABLE IF EXISTS "public"."qrcode_labels";
CREATE TABLE "public"."qrcode_labels" (
  "id" serial,
  "name" varchar(128) NOT NULL,
  PRIMARY KEY ("id")
);
COMMENT ON COLUMN "public"."qrcode_labels"."name" IS '标签名';


DROP TABLE IF EXISTS "public"."qrcode_labels_qrcode";
CREATE TABLE "public"."qrcode_labels_qrcode" (
  "id" serial,
  "label_id" int4 NOT NULL,
  "qrcode_id" int4 NOT NULL,
  "publicity_info_id" int4,
  PRIMARY KEY ("id"),
  CONSTRAINT "qrcode_labels_label_id_fk" FOREIGN KEY ("label_id") REFERENCES "public"."qrcode_labels" ("id"),
  CONSTRAINT "qrcode_labels_qrcode_id_fk" FOREIGN KEY ("qrcode_id") REFERENCES "public"."qrcode" ("id"),
  CONSTRAINT "qrcode_labels_publicity_info_id_fk" FOREIGN KEY ("publicity_info_id") REFERENCES "public"."publicity_info" ("id")
);