
-- ----------------------------
-- Sequence structure for tbl_org_oid_seq
-- ----------------------------
DROP SEQUENCE IF EXISTS "public"."tbl_org_oid_seq" CASCADE;
CREATE SEQUENCE "public"."tbl_org_oid_seq"
INCREMENT 1
MINVALUE  1
MAXVALUE 9223372036854775807
START 1
CACHE 1;


-- ----------------------------
-- Sequence structure for tbl_sys_tag_id_seq
-- ----------------------------
DROP SEQUENCE IF EXISTS "public"."tbl_sys_tag_id_seq" CASCADE;
CREATE SEQUENCE "public"."tbl_sys_tag_id_seq"
INCREMENT 1
MINVALUE  1
MAXVALUE 9223372036854775807
START 1
CACHE 1;

-- ----------------------------
-- Function structure for auto_update_timestamp
-- ----------------------------
DROP FUNCTION IF EXISTS "public"."auto_update_timestamp"() CASCADE;
CREATE OR REPLACE FUNCTION "public"."auto_update_timestamp"()
  RETURNS "pg_catalog"."trigger" AS $BODY$
begin
    new.update_time = current_timestamp;
return new;
end
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;


-- ----------------------------
-- Function structure for get_oid_path
-- ----------------------------
DROP FUNCTION IF EXISTS "public"."get_oid_path"("coid" int8) CASCADE;
CREATE OR REPLACE FUNCTION "public"."get_oid_path"("coid" int8)
  RETURNS "pg_catalog"."text" AS $BODY$
DECLARE

BEGIN

    return '/' || array_to_string(array(SELECT oid
                                        from (WITH RECURSIVE T (oid, poid) AS (
                                            SELECT oid, poid
                                            FROM tbl_org
                                            WHERE oid = coid

                                            UNION ALL

                                            SELECT oo.oid, oo.poid
                                            FROM tbl_org oo
                                                     JOIN T ON oo.oid = T.poid
                                        )
                                              SELECT oid, poid
                                              FROM T
                                              ORDER BY oid ASC) X), '/') || '/';

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

-- ----------------------------
-- Function structure for get_org_name_path
-- ----------------------------
DROP FUNCTION IF EXISTS "public"."get_org_name_path"("coid" int8) CASCADE;
CREATE OR REPLACE FUNCTION "public"."get_org_name_path"("coid" int8)
  RETURNS "pg_catalog"."text" AS $BODY$
DECLARE

BEGIN

    return '/' || array_to_string(array(SELECT name
                                        from (WITH RECURSIVE T (name, oid, poid) AS (
                                            SELECT name, oid, poid
                                            FROM tbl_org
                                            WHERE oid = coid

                                            UNION ALL

                                            SELECT oo.name, oo.oid, oo.poid
                                            FROM tbl_org oo
                                                     JOIN T ON oo.oid = T.poid
                                        )
                                              SELECT name, oid, poid
                                              FROM T
                                              ORDER BY oid ASC) X), '/') || '/';

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

-- ----------------------------
-- Function structure for get_tag_id_path
-- ----------------------------
DROP FUNCTION IF EXISTS "public"."get_tag_id_path"("coid" int8) CASCADE;
CREATE OR REPLACE FUNCTION "public"."get_tag_id_path"("coid" int8)
  RETURNS "pg_catalog"."text" AS $BODY$
DECLARE

BEGIN

    return '/' || array_to_string(array(SELECT id
                                        from (WITH RECURSIVE T (id, parent_id) AS (
                                            SELECT id, parent_id
                                            FROM tbl_sys_tag
                                            WHERE id = coid

                                            UNION ALL

                                            SELECT oo.id, oo.parent_id
                                            FROM tbl_sys_tag oo
                                                     JOIN T ON oo.id = T.parent_id
                                        )
                                              SELECT id, parent_id
                                              FROM T
                                              ORDER BY id ASC) X), '/') || '/';

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

-- ----------------------------
-- Function structure for get_tag_name_path
-- ----------------------------
DROP FUNCTION IF EXISTS "public"."get_tag_name_path"("coid" int8) CASCADE;
CREATE OR REPLACE FUNCTION "public"."get_tag_name_path"("coid" int8)
  RETURNS "pg_catalog"."text" AS $BODY$
DECLARE

BEGIN

    return '/' || array_to_string(array(SELECT tag_name
                                        from (WITH RECURSIVE T (tag_name, id, parent_id) AS (
                                            SELECT tag_name, id, parent_id
                                            FROM tbl_sys_tag
                                            WHERE id = coid

                                            UNION ALL

                                            SELECT oo.tag_name, oo.id, oo.parent_id
                                            FROM tbl_sys_tag oo
                                                     JOIN T ON oo.id = T.parent_id
                                        )
                                              SELECT tag_name, id, parent_id
                                              FROM T
                                              ORDER BY id ASC) X), '/') || '/';

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

-- ----------------------------
-- Function structure for tf_supply_org_info
-- ----------------------------
DROP FUNCTION IF EXISTS "public"."tf_supply_org_info"() CASCADE;
CREATE OR REPLACE FUNCTION "public"."tf_supply_org_info"()
  RETURNS "pg_catalog"."trigger" AS $BODY$
DECLARE
begin
    UPDATE tbl_org SET oid_path=get_oid_path(oid), path=get_org_name_path(oid) WHERE oid = NEW.oid;
    return NEW;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

-- ----------------------------
-- Function structure for tf_supply_tag_info
-- ----------------------------
DROP FUNCTION IF EXISTS "public"."tf_supply_tag_info"() CASCADE;
CREATE OR REPLACE FUNCTION "public"."tf_supply_tag_info"()
  RETURNS "pg_catalog"."trigger" AS $BODY$
DECLARE
begin
    UPDATE tbl_sys_tag SET id_path=get_tag_id_path(id), path=get_tag_name_path(id) WHERE id = NEW.id;
    return NEW;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;


-- ----------------------------
-- Table structure for tbl_org
-- ----------------------------
DROP TABLE IF EXISTS "public"."tbl_org" CASCADE;
CREATE TABLE "public"."tbl_org" (
  "oid" int4 NOT NULL DEFAULT nextval('tbl_org_oid_seq'::regclass),
  "name" varchar(32) COLLATE "pg_catalog"."default" NOT NULL,
  "fullname" varchar(255) COLLATE "pg_catalog"."default",
  "type" int4,
  "level" int2,
  "poid" int4,
  "geom" varchar COLLATE "pg_catalog"."default",
  "orderby" int4 DEFAULT 0,
  "path" varchar(255) COLLATE "pg_catalog"."default",
  "wkt" text COLLATE "pg_catalog"."default",
  "oid_path" varchar(255) COLLATE "pg_catalog"."default",
  "ref_oid" varchar COLLATE "pg_catalog"."default",
  "geo_json" text COLLATE "pg_catalog"."default",
  "remark" varchar(255) COLLATE "pg_catalog"."default",
  "ref_code" varchar(255) COLLATE "pg_catalog"."default",
  "district_oid" int4,
  "del_flag" int2 DEFAULT 0,
  "del_key" int4 DEFAULT 0,
  "zzd_org_code" varchar(64) COLLATE "pg_catalog"."default",
  "create_time" timestamp(6) DEFAULT now(),
  "update_time" timestamp(6) DEFAULT now(),
  "district_code" varchar(255) COLLATE "pg_catalog"."default",
  "org_property_id" int4,
  CONSTRAINT "tbl_org_pkey" PRIMARY KEY ("oid"),
  CONSTRAINT "uk_path_del_key" UNIQUE ("path", "del_key")
)
;

CREATE TRIGGER "tbl_org_auto_update_time" BEFORE UPDATE ON "public"."tbl_org"
FOR EACH ROW
EXECUTE PROCEDURE "public"."auto_update_timestamp"();

CREATE TRIGGER "tg_supply_org_info" AFTER INSERT OR UPDATE OF "oid", "name", "poid" ON "public"."tbl_org"
FOR EACH ROW
EXECUTE PROCEDURE "public"."tf_supply_org_info"();

COMMENT ON COLUMN "public"."tbl_org"."oid" IS '组织id';
COMMENT ON COLUMN "public"."tbl_org"."name" IS '名称';
COMMENT ON COLUMN "public"."tbl_org"."type" IS '类型';
COMMENT ON COLUMN "public"."tbl_org"."level" IS '等级';
COMMENT ON COLUMN "public"."tbl_org"."poid" IS '父组织编码';
COMMENT ON COLUMN "public"."tbl_org"."orderby" IS '排序';
COMMENT ON COLUMN "public"."tbl_org"."district_oid" IS '行政区域id';
COMMENT ON COLUMN "public"."tbl_org"."zzd_org_code" IS '专有钉钉部门code';
COMMENT ON COLUMN "public"."tbl_org"."district_code" IS '行政区划编码';
COMMENT ON COLUMN "public"."tbl_org"."org_property_id" IS '组织属性id';


-- ----------------------------
-- Table structure for tbl_sys_tag
-- ----------------------------
DROP TABLE IF EXISTS "public"."tbl_sys_tag" CASCADE;
CREATE TABLE "public"."tbl_sys_tag" (
  "id" int4 NOT NULL DEFAULT nextval('tbl_sys_tag_id_seq'::regclass),
  "tag_name" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "dict_item_value" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "sort" int4,
  "del_flag" int2 DEFAULT 0,
  "create_time" timestamp(6) NOT NULL DEFAULT now(),
  "update_time" timestamp(6) NOT NULL DEFAULT now(),
  "del_key" int4 DEFAULT 0,
  "tag_description" varchar(255) COLLATE "pg_catalog"."default",
  "parent_id" int4 NOT NULL DEFAULT 0,
  "level" int2 NOT NULL DEFAULT 1,
  "path" varchar(255) COLLATE "pg_catalog"."default",
  "id_path" varchar(255) COLLATE "pg_catalog"."default",
  "color" varchar(255) COLLATE "pg_catalog"."default",
  CONSTRAINT "tbl_sys_tag_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "uk_tag_name_del_key" UNIQUE ("tag_name", "del_key")
)
;

CREATE TRIGGER "auto_update_time" BEFORE UPDATE ON "public"."tbl_sys_tag"
FOR EACH ROW
EXECUTE PROCEDURE "public"."auto_update_timestamp"();

CREATE TRIGGER "tf_supply_tag_info" AFTER INSERT OR UPDATE OF "parent_id", "id", "tag_name" ON "public"."tbl_sys_tag"
FOR EACH ROW
EXECUTE PROCEDURE "public"."tf_supply_tag_info"();

COMMENT ON COLUMN "public"."tbl_sys_tag"."tag_name" IS '标签名称';
COMMENT ON COLUMN "public"."tbl_sys_tag"."dict_item_value" IS '标签所属模块（存字典项表中的item_value）';
COMMENT ON COLUMN "public"."tbl_sys_tag"."sort" IS '排序字段';
COMMENT ON COLUMN "public"."tbl_sys_tag"."del_flag" IS '删除标记,1:已删除,0:正常';
COMMENT ON COLUMN "public"."tbl_sys_tag"."create_time" IS '创建时间';
COMMENT ON COLUMN "public"."tbl_sys_tag"."update_time" IS '更新时间';
COMMENT ON COLUMN "public"."tbl_sys_tag"."tag_description" IS '标签描述';
COMMENT ON COLUMN "public"."tbl_sys_tag"."parent_id" IS '父id';
COMMENT ON COLUMN "public"."tbl_sys_tag"."level" IS '级别';
COMMENT ON COLUMN "public"."tbl_sys_tag"."path" IS '标签名称路径';
COMMENT ON COLUMN "public"."tbl_sys_tag"."id_path" IS '标签id路径';
COMMENT ON COLUMN "public"."tbl_sys_tag"."color" IS '标签颜色';


INSERT INTO "public"."tbl_org" ("oid", "name", "fullname", "type", "level", "poid", "geom", "orderby", "path", "wkt", "oid_path", "ref_oid", "geo_json", "remark", "ref_code", "district_oid", "del_flag", "del_key", "zzd_org_code", "create_time", "update_time", "district_code", "org_property_id") VALUES (1, '临平区', '临平区', 1, 1, NULL, NULL, NULL, '/临平区/', '', '/1/', 1, NULL, NULL, NULL, NULL, 0, 0, NULL, NOW(), NOW(), '330113', 3);

-- ----------------------------
-- Alter sequences owned by
-- ----------------------------
ALTER SEQUENCE "public"."tbl_org_oid_seq"
OWNED BY "public"."tbl_org"."oid";
SELECT setval('"public"."tbl_org_oid_seq"', 1, true);
