BEGIN
-- 更新树表NEW行本级长编码长名称级次
-- 1. 更新树表NEW行本级长名称和级次
EXECUTE format(
'
UPDATE %1$I
SET
"f_fullName" = COALESCE((SELECT "f_fullName" FROM %1$I WHERE id = $1), '''') || ''/'' || $2,
"f_level" = COALESCE((SELECT "f_level" FROM %1$I WHERE id = $1), 0) + 1
WHERE id = $3
',
TG_TABLE_NAME
) USING NEW."parentId",
NEW."f_name",
NEW."id";
-- 2. 更新树表NEW行本级长编码, 基于f_number
IF (
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = TG_TABLE_NAME
AND column_name IN ('f_fullNumber', 'f_number')
) = 2 THEN EXECUTE format(
'
UPDATE %1$I
SET "f_fullNumber" = COALESCE((SELECT "f_fullNumber" FROM %1$I WHERE id = $1), '''') || ''/'' || $2
WHERE id = $3
',
TG_TABLE_NAME
) USING NEW."parentId",
NEW."f_number",
NEW."id";
END IF;
-- 3. 更新树表NEW行本级长编码, 基于f_autoNumber
IF (
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = TG_TABLE_NAME
AND column_name IN ('f_fullNumber', 'f_autoNumber')
) = 2 THEN EXECUTE format(
'
UPDATE %1$I
SET "f_fullNumber" = COALESCE((SELECT "f_fullNumber" FROM %1$I WHERE id = $1), '''') || ''/'' || $2
WHERE id = $3
',
TG_TABLE_NAME
) USING NEW."parentId",
NEW."f_autoNumber",
NEW."id";
END IF;
RETURN NEW;
END;
BEGIN -- 更新树表NEW行下游长编码长名称级次
-- 1. 更新树表NEW行下游长名称和级次
EXECUTE format(
'
UPDATE %1$I
SET
"f_fullName" = COALESCE((SELECT "f_fullName" FROM %1$I WHERE id = $1), '''') || ''/'' || "f_name",
"f_level" = COALESCE((SELECT "f_level" FROM %1$I WHERE id = $1), 0) + 1
WHERE "parentId" = $1;
',
TG_TABLE_NAME
) USING NEW."id";
-- 2. 更新树表NEW行下游长编码, 基于f_number
IF (
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = TG_TABLE_NAME
AND column_name IN ('f_fullNumber', 'f_number')
) = 2 THEN EXECUTE format(
'
UPDATE %1$I
SET "f_fullNumber" = COALESCE((SELECT "f_fullNumber" FROM %1$I WHERE id = $1), '''') || ''/'' || "f_number"
WHERE "parentId" = $1
',
TG_TABLE_NAME
) USING NEW."id";
END IF;
-- 3. 更新树表NEW行下游长编码, 基于f_autoNumber
IF (
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = TG_TABLE_NAME
AND column_name IN ('f_fullNumber', 'f_autoNumber')
) = 2 THEN EXECUTE format(
'
UPDATE %1$I
SET "f_fullNumber" = COALESCE((SELECT "f_fullNumber" FROM %1$I WHERE id = $1), '''') || ''/'' || "f_autoNumber"
WHERE "parentId" = $1
',
TG_TABLE_NAME
) USING NEW."id";
END IF;
RETURN NEW;
END;