-- =============================================
-- 补充 ILDM_FEE_CUSTOMER_INFO 关联组织字段
-- 用于相对方关联内部货代公司组织节点
-- 幂等执行：如已存在则跳过
-- =============================================

-- 关联组织ID（内部货代公司对应组织树节点ID）
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM ALL_TAB_COLUMNS
    WHERE OWNER = 'DEVELOPER' AND TABLE_NAME = 'ILDM_FEE_CUSTOMER_INFO' AND COLUMN_NAME = 'LINKED_ORG_ID';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'ALTER TABLE ILDM_FEE_CUSTOMER_INFO ADD LINKED_ORG_ID NUMBER(19)';
        EXECUTE IMMEDIATE 'COMMENT ON COLUMN ILDM_FEE_CUSTOMER_INFO.LINKED_ORG_ID IS ''关联内部组织ID（集团内货代公司对应组织树节点ID）''';
        DBMS_OUTPUT.PUT_LINE('LINKED_ORG_ID column added.');
    ELSE
        DBMS_OUTPUT.PUT_LINE('LINKED_ORG_ID already exists, skipped.');
    END IF;
END;
/

-- 关联用户账号/手机号（内部货代公司对应的业务端用户）
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM ALL_TAB_COLUMNS
    WHERE OWNER = 'DEVELOPER' AND TABLE_NAME = 'ILDM_FEE_CUSTOMER_INFO' AND COLUMN_NAME = 'LINKED_USER_ACCOUNT';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'ALTER TABLE ILDM_FEE_CUSTOMER_INFO ADD LINKED_USER_ACCOUNT VARCHAR2(64)';
        EXECUTE IMMEDIATE 'COMMENT ON COLUMN ILDM_FEE_CUSTOMER_INFO.LINKED_USER_ACCOUNT IS ''关联用户账号/手机号（内部货代公司对应的业务端登录账号）''';
        DBMS_OUTPUT.PUT_LINE('LINKED_USER_ACCOUNT column added.');
    ELSE
        DBMS_OUTPUT.PUT_LINE('LINKED_USER_ACCOUNT already exists, skipped.');
    END IF;
END;
/
