

CREATE OR REPLACE FUNCTION kbdDebugInfo(info1 varchar(100), info2 varchar(100) )
RETURNS VOID
AS $$
DECLARE enableDebug int DEFAULT 1;
BEGIN
enableDebug = (SELECT PRINT_DEBUG_INFO_FLAG FROM kbd_debug_flag LIMIT 1);
IF enableDebug=1 THEN 
INSERT INTO kbd_debug(col1,col2,col3) VALUES (info1,info2,now());
END IF;
END;
$$LANGUAGE PLPGSQL;



CREATE OR REPLACE FUNCTION FUNC_TIA_ACCUML_LIMIT_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'accuml_limit_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('accuml_limit_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ACCUML_LIMIT_INFO ON accuml_limit_info;
CREATE TRIGGER TIA_ACCUML_LIMIT_INFO AFTER INSERT ON accuml_limit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ACCUML_LIMIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_ACCUML_LIMIT_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'accuml_limit_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('accuml_limit_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ACCUML_LIMIT_INFO ON accuml_limit_info;
CREATE TRIGGER TUA_ACCUML_LIMIT_INFO AFTER UPDATE ON accuml_limit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ACCUML_LIMIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_ACCUML_LIMIT_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'accuml_limit_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('accuml_limit_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ACCUML_LIMIT_INFO ON accuml_limit_info;
CREATE TRIGGER TDA_ACCUML_LIMIT_INFO AFTER DELETE ON accuml_limit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ACCUML_LIMIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TI_ACCUML_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    --首先禁止FES触发器动作
	SET SESSION "disable.FesAccumlTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.AccumlMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN 
                UPDATE fes_accuml SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
	SET SESSION "disable.FesAccumlTrigger" = 0;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'accuml_map_to_fes', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('accuml_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_ACCUML_MAP_TO_FES ON accuml_map_to_fes;
CREATE TRIGGER TI_ACCUML_MAP_TO_FES AFTER INSERT ON accuml_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_ACCUML_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TU_ACCUML_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    IF NEW.RTU_TAG = OLD.RTU_TAG AND 
        NEW.DOT_NO = OLD.DOT_NO AND 
        NEW.KEY_ID_TAG = OLD.KEY_ID_TAG
        THEN RETURN NEW;
    END IF;
    
    
    --首先禁止FES触发器动作
	SET SESSION "disable.FesAccumlTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.AccumlMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        --清除旧的
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN 
                UPDATE fes_accuml SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
        
        --添加新的
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN 
                UPDATE fes_accuml SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        ELSE NEW.DOT_NO = '-1';
        END IF;
    END;
    END IF;

	SET SESSION "disable.FesAccumlTrigger" = 0;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_ACCUML_MAP_TO_FES ON accuml_map_to_fes;
CREATE TRIGGER TU_ACCUML_MAP_TO_FES BEFORE UPDATE ON accuml_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_ACCUML_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TUA_ACCUML_MAP_TO_FES() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'accuml_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('accuml_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ACCUML_MAP_TO_FES ON accuml_map_to_fes;
CREATE TRIGGER TUA_ACCUML_MAP_TO_FES AFTER UPDATE ON accuml_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ACCUML_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TD_ACCUML_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    
    --首先禁止FES触发器动作
	SET SESSION "disable.FesAccumlTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.AccumlMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN 
                UPDATE fes_accuml SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
	SET SESSION "disable.FesAccumlTrigger" = 0;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_ACCUML_MAP_TO_FES ON accuml_map_to_fes;
CREATE TRIGGER TD_ACCUML_MAP_TO_FES BEFORE DELETE ON accuml_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_ACCUML_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TDA_ACCUML_MAP_TO_FES() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'accuml_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('accuml_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ACCUML_MAP_TO_FES ON accuml_map_to_fes;
CREATE TRIGGER TDA_ACCUML_MAP_TO_FES AFTER DELETE ON accuml_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ACCUML_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TI_ACCUML_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    /*
INSERT INTO accuml(
TAG_NAME,                                              DESCRIPTION,    DEVICE,   SEQ_NO,    LOCATION_ID, SUB_SYSTEM, RTU_TAG, GRAY, CONTRAST, QCOLOR, RAW_VALUE, VALUE, STATUS, LAST_UPDATE_TIME, LAST_CHANGE_TIME, UNIT_ID, SOUND_NAME1, SOUND_NAME2, SOUND_NAME3, REGION_ID, OPT_HANDOVER_GROUP, IS_LIMIT,     POINT_TYPE,    POINT_CLASS,    POINT_PROPERTY,    POINT_SORT,    DEV_TYPE, POINT_TP_NAME, IS_SAMPLE, IS_STATISTICS,       SAMPLE_PERIOD,    SAMPLE_DEADBAND,    CAMERA_TAG, PRESET_ID) 
SELECT   
concat(TAG_NAME, '.' , getAfterLastDot(NEW.TAG_NAME)), NEW.DESCRIPTION,TAG_NAME, NEW.SEQ_NO,LOCATION_ID, SUB_SYSTEM, '',      GRAY, CONTRAST, QCOLOR, '0',       VALUE, STATUS, 0,                0,                NEW.UNIT_ID,  '',      '',          '',         REGION_ID, OPT_HANDOVER_GROUP, NEW.IS_LIMIT, NEW.POINT_TYPE,NEW.POINT_CLASS,NEW.POINT_PROPERTY,NEW.POINT_SORT,DEV_TYPE, NEW.TAG_NAME, NEW.IS_SAMPLE,NEW.IS_STATISTICS, NEW.SAMPLE_PERIOD,NEW.SAMPLE_DEADBAND, '',       ''
FROM dev_info WHERE DEV_TP_NAME = NEW.DEV_TP_NAME;
*/
    INSERT INTO temp_plugin_point_map_acc
    (PLUGIN_NAME, TEMP_NAME, TEMP_POINT_NAME, FES_TEMP_NAME, FES_TEMP_POINT_NAME)
    SELECT 
    PLUGIN_NAME, TEMP_NAME, getAfterLastDot(NEW.TAG_NAME),'',''
    FROM temp_plugin_dev_map WHERE TEMP_NAME = NEW.DEV_TP_NAME;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'accuml_temp_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('accuml_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_ACCUML_TEMP_DEFINE ON accuml_temp_define;
CREATE TRIGGER TI_ACCUML_TEMP_DEFINE AFTER INSERT ON accuml_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_ACCUML_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TU_ACCUML_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    /*
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.DESCRIPTION <> OLD.DESCRIPTION OR NEW.UNIT_ID <> OLD.UNIT_ID OR NEW.IS_LIMIT <> OLD.IS_LIMIT OR NEW.POINT_CLASS <> OLD.POINT_CLASS OR
    NEW.IS_SAMPLE <> OLD.IS_SAMPLE OR NEW.SAMPLE_DEADBAND <> OLD.SAMPLE_DEADBAND OR NEW.POINT_TYPE <> OLD.POINT_TYPE OR NEW.SEQ_NO <> OLD.SEQ_NO OR 
    NEW.IS_STATISTICS <> OLD.IS_STATISTICS OR NEW.SAMPLE_PERIOD <> OLD.SAMPLE_PERIOD OR NEW.POINT_PROPERTY <> OLD.POINT_PROPERTY OR NEW.POINT_SORT <> OLD.POINT_SORT
    THEN 
        UPDATE accuml SET 
        accuml.TAG_NAME = concat(accuml.DEVICE, '.', getAfterLastDot(NEW.TAG_NAME)),
        accuml.DESCRIPTION = NEW.DESCRIPTION,
        accuml.UNIT_ID = NEW.UNIT_ID,
        accuml.IS_LIMIT = NEW.IS_LIMIT,
        accuml.POINT_CLASS = NEW.POINT_CLASS,
        accuml.POINT_TP_NAME = NEW.TAG_NAME,
        accuml.IS_SAMPLE = NEW.IS_SAMPLE,
        accuml.SAMPLE_DEADBAND = NEW.SAMPLE_DEADBAND,
        accuml.POINT_TYPE = NEW.POINT_TYPE,
        accuml.SEQ_NO = NEW.SEQ_NO,
        accuml.IS_STATISTICS = NEW.IS_STATISTICS,
        accuml.SAMPLE_PERIOD = NEW.SAMPLE_PERIOD,
        accuml.POINT_PROPERTY = NEW.POINT_PROPERTY,
        accuml.POINT_SORT = NEW.POINT_SORT
        WHERE accuml.POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    
    IF NEW.LIMIT_TYPE <> OLD.LIMIT_TYPE OR NEW.LIMIT_NUM <> OLD.LIMIT_NUM OR NEW.LIMIT_UP1 <> OLD.LIMIT_UP1 OR NEW.LIMIT_LOW1 <> OLD.LIMIT_LOW1 OR NEW.LIMIT_UP2 <> OLD.LIMIT_UP2 OR
    NEW.LIMIT_LOW2 <> OLD.LIMIT_LOW2 OR NEW.LIMIT_UP3 <> OLD.LIMIT_UP3 OR NEW.LIMIT_LOW3 <> OLD.LIMIT_LOW3 OR NEW.CROSS_PERCENT <> OLD.CROSS_PERCENT OR 
    NEW.RETURN_PERCENT <> OLD.RETURN_PERCENT OR NEW.ALARM_PRIORITY <> OLD.ALARM_PRIORITY OR NEW.ALARM_DELAY_TIME <> OLD.ALARM_DELAY_TIME OR NEW.IS_WATER_ALM <> OLD.IS_WATER_ALM OR
    NEW.IS_ACK_ON_RTN <> OLD.IS_ACK_ON_RTN OR NEW.DEL_ACT_ON_ACK <> OLD.DEL_ACT_ON_ACK OR NEW.IS_NEVER_ALM_ON_RTN <> OLD.IS_NEVER_ALM_ON_RTN OR NEW.TAG_NAME <> OLD.TAG_NAME 
    THEN
        UPDATE accuml_limit_info SET 
        LIMIT_TYPE = NEW.LIMIT_TYPE,
        LIMIT_NUM = NEW.LIMIT_NUM,
        LIMIT_UP1 = NEW.LIMIT_UP1,
        LIMIT_LOW1 = NEW.LIMIT_LOW1,
        LIMIT_UP2 = NEW.LIMIT_UP2,
        LIMIT_LOW2 = NEW.LIMIT_LOW2,
        LIMIT_UP3 = NEW.LIMIT_UP3,
        LIMIT_LOW3 = NEW.LIMIT_LOW3,
        CROSS_PERCENT = NEW.CROSS_PERCENT,
        RETURN_PERCENT = NEW.RETURN_PERCENT,
        ALARM_PRIORITY = NEW.ALARM_PRIORITY,
        ALARM_DELAY_TIME = NEW.ALARM_DELAY_TIME,
        IS_WATER_ALM = NEW.IS_WATER_ALM,
        IS_ACK_ON_RTN = NEW.IS_ACK_ON_RTN,
        DEL_ACT_ON_ACK = NEW.DEL_ACT_ON_ACK,
        IS_NEVER_ALM_ON_RTN = NEW.IS_NEVER_ALM_ON_RTN,
        POINT_TP_NAME = NEW.TAG_NAME
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
     END IF;
    IF NEW.RANGE_UP <> OLD.RANGE_UP OR NEW.RANGE_LOW <> OLD.RANGE_LOW OR NEW.IS_FILTER <> OLD.IS_FILTER OR NEW.PERCENT <> OLD.PERCENT OR
    NEW.BASE <> OLD.BASE OR NEW.COEFF <> OLD.COEFF 
    THEN
        UPDATE accuml_map_to_fes SET 
        RANGE_UP = NEW.RANGE_UP,
        RANGE_LOW = NEW.RANGE_LOW,
        IS_FILTER = NEW.IS_FILTER,
        PERCENT = NEW.PERCENT,
        BASE = NEW.BASE,
        COEFF = NEW.COEFF
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    */
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
        UPDATE temp_plugin_point_map_acc
        SET TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME)
        WHERE TEMP_NAME = OLD.DEV_TP_NAME AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    END IF;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'accuml_temp_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('accuml_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_ACCUML_TEMP_DEFINE ON accuml_temp_define;
CREATE TRIGGER TU_ACCUML_TEMP_DEFINE AFTER UPDATE ON accuml_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_ACCUML_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TD_ACCUML_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    
    DELETE FROM temp_plugin_point_map_acc WHERE TEMP_NAME = OLD.DEV_TP_NAME  AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
/*
DELETE FROM accuml WHERE POINT_TP_NAME = OLD.TAG_NAME;
*/
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_ACCUML_TEMP_DEFINE ON accuml_temp_define;
CREATE TRIGGER TD_ACCUML_TEMP_DEFINE BEFORE DELETE ON accuml_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_ACCUML_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ACCUML_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'accuml_temp_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('accuml_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ACCUML_TEMP_DEFINE ON accuml_temp_define;
CREATE TRIGGER TDA_ACCUML_TEMP_DEFINE AFTER DELETE ON accuml_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ACCUML_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_LINK_ENABLE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'alarm_link_enable_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('alarm_link_enable_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_LINK_ENABLE_INFO ON alarm_link_enable_info;
CREATE TRIGGER TIA_ALARM_LINK_ENABLE_INFO AFTER INSERT ON alarm_link_enable_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_LINK_ENABLE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_LINK_ENABLE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'alarm_link_enable_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('alarm_link_enable_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_LINK_ENABLE_INFO ON alarm_link_enable_info;
CREATE TRIGGER TUA_ALARM_LINK_ENABLE_INFO AFTER UPDATE ON alarm_link_enable_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_LINK_ENABLE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_LINK_ENABLE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'alarm_link_enable_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('alarm_link_enable_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_LINK_ENABLE_INFO ON alarm_link_enable_info;
CREATE TRIGGER TDA_ALARM_LINK_ENABLE_INFO AFTER DELETE ON alarm_link_enable_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_LINK_ENABLE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_SMS_CONFIG() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'alarm_sms_config', 'NO', '', NEW.NO, getTableSubSystem('alarm_sms_config'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_SMS_CONFIG ON alarm_sms_config;
CREATE TRIGGER TIA_ALARM_SMS_CONFIG AFTER INSERT ON alarm_sms_config FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_SMS_CONFIG();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_SMS_CONFIG() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'alarm_sms_config', 'NO', OLD.NO, NEW.NO, getTableSubSystem('alarm_sms_config'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_SMS_CONFIG ON alarm_sms_config;
CREATE TRIGGER TUA_ALARM_SMS_CONFIG AFTER UPDATE ON alarm_sms_config FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_SMS_CONFIG();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_SMS_CONFIG() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'alarm_sms_config', 'NO', OLD.NO, '', getTableSubSystem('alarm_sms_config'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_SMS_CONFIG ON alarm_sms_config;
CREATE TRIGGER TDA_ALARM_SMS_CONFIG AFTER DELETE ON alarm_sms_config FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_SMS_CONFIG();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_SMS_DEVICE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'alarm_sms_device', 'DEVICE_ID', '', NEW.DEVICE_ID, getTableSubSystem('alarm_sms_device'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_SMS_DEVICE ON alarm_sms_device;
CREATE TRIGGER TIA_ALARM_SMS_DEVICE AFTER INSERT ON alarm_sms_device FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_SMS_DEVICE();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_SMS_DEVICE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'alarm_sms_device', 'DEVICE_ID', OLD.DEVICE_ID, NEW.DEVICE_ID, getTableSubSystem('alarm_sms_device'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_SMS_DEVICE ON alarm_sms_device;
CREATE TRIGGER TUA_ALARM_SMS_DEVICE AFTER UPDATE ON alarm_sms_device FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_SMS_DEVICE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_SMS_DEVICE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'alarm_sms_device', 'DEVICE_ID', OLD.DEVICE_ID, '', getTableSubSystem('alarm_sms_device'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_SMS_DEVICE ON alarm_sms_device;
CREATE TRIGGER TDA_ALARM_SMS_DEVICE AFTER DELETE ON alarm_sms_device FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_SMS_DEVICE();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_SMS_RULE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'alarm_sms_rule', 'RULE_ID', '', NEW.RULE_ID, getTableSubSystem('alarm_sms_rule'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_SMS_RULE ON alarm_sms_rule;
CREATE TRIGGER TIA_ALARM_SMS_RULE AFTER INSERT ON alarm_sms_rule FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_SMS_RULE();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_SMS_RULE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'alarm_sms_rule', 'RULE_ID', OLD.RULE_ID, NEW.RULE_ID, getTableSubSystem('alarm_sms_rule'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_SMS_RULE ON alarm_sms_rule;
CREATE TRIGGER TUA_ALARM_SMS_RULE AFTER UPDATE ON alarm_sms_rule FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_SMS_RULE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_SMS_RULE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'alarm_sms_rule', 'RULE_ID', OLD.RULE_ID, '', getTableSubSystem('alarm_sms_rule'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_SMS_RULE ON alarm_sms_rule;
CREATE TRIGGER TDA_ALARM_SMS_RULE AFTER DELETE ON alarm_sms_rule FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_SMS_RULE();



CREATE OR REPLACE FUNCTION FUNC_TIA_ANALOG_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'analog_control', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('analog_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ANALOG_CONTROL ON analog_control;
CREATE TRIGGER TIA_ANALOG_CONTROL AFTER INSERT ON analog_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ANALOG_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TU_ANALOG_CONTROL() RETURNS TRIGGER AS $$
BEGIN
    
    --修改功能名称
    IF COALESCE(NEW.RTU_TAG,'') = '' THEN 
    BEGIN
        NEW.OFFSET_NO = '-1';
    END;
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_ANALOG_CONTROL ON analog_control;
CREATE TRIGGER TU_ANALOG_CONTROL BEFORE UPDATE ON analog_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_ANALOG_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TUA_ANALOG_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'analog_control', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('analog_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ANALOG_CONTROL ON analog_control;
CREATE TRIGGER TUA_ANALOG_CONTROL AFTER UPDATE ON analog_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ANALOG_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TDA_ANALOG_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'analog_control', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('analog_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ANALOG_CONTROL ON analog_control;
CREATE TRIGGER TDA_ANALOG_CONTROL AFTER DELETE ON analog_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ANALOG_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TIA_ANALOG_LIMIT_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'analog_limit_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('analog_limit_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ANALOG_LIMIT_INFO ON analog_limit_info;
CREATE TRIGGER TIA_ANALOG_LIMIT_INFO AFTER INSERT ON analog_limit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ANALOG_LIMIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_ANALOG_LIMIT_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'analog_limit_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('analog_limit_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ANALOG_LIMIT_INFO ON analog_limit_info;
CREATE TRIGGER TUA_ANALOG_LIMIT_INFO AFTER UPDATE ON analog_limit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ANALOG_LIMIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_ANALOG_LIMIT_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'analog_limit_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('analog_limit_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ANALOG_LIMIT_INFO ON analog_limit_info;
CREATE TRIGGER TDA_ANALOG_LIMIT_INFO AFTER DELETE ON analog_limit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ANALOG_LIMIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TI_ANALOG_MAP_TO_FES() RETURNS TRIGGER AS $$
	DECLARE flag int;
BEGIN
	SET SESSION "disable.FesAnalogTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.AnalogMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN 
                UPDATE fes_analog SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
	SET SESSION "disable.FesAnalogTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'analog_map_to_fes', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('analog_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_ANALOG_MAP_TO_FES ON analog_map_to_fes;
CREATE TRIGGER TI_ANALOG_MAP_TO_FES AFTER INSERT ON analog_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_ANALOG_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TU_ANALOG_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    IF NEW.RTU_TAG = OLD.RTU_TAG AND 
        NEW.DOT_NO = OLD.DOT_NO AND 
        NEW.KEY_ID_TAG = OLD.KEY_ID_TAG
        THEN RETURN NEW;
    END IF;
    
    CALL kbdDebugInfo('FES analog update','In TU_ANALOG_MAP_TO_FES');
    
	SET SESSION "disable.FesAnalogTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.AnalogMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN 
                UPDATE fes_analog SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
        
        
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN 
                UPDATE fes_analog SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO); 
            END IF;
            
        END;
        ELSE NEW.DOT_NO = '-1';
        END IF;
     END;
     END IF;
    
	SET SESSION "disable.FesAnalogTrigger" = 0;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_ANALOG_MAP_TO_FES ON analog_map_to_fes;
CREATE TRIGGER TU_ANALOG_MAP_TO_FES BEFORE UPDATE ON analog_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_ANALOG_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TUA_ANALOG_MAP_TO_FES() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'analog_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('analog_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ANALOG_MAP_TO_FES ON analog_map_to_fes;
CREATE TRIGGER TUA_ANALOG_MAP_TO_FES AFTER UPDATE ON analog_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ANALOG_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TD_ANALOG_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    CALL kbdDebugInfo('FES analog update','In TD_ANALOG_MAP_TO_FES');
    
	SET SESSION "disable.FesAnalogTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.AnalogMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN 
                UPDATE fes_analog SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
	SET SESSION "disable.FesAnalogTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_ANALOG_MAP_TO_FES ON analog_map_to_fes;
CREATE TRIGGER TD_ANALOG_MAP_TO_FES BEFORE DELETE ON analog_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_ANALOG_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TDA_ANALOG_MAP_TO_FES() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'analog_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('analog_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ANALOG_MAP_TO_FES ON analog_map_to_fes;
CREATE TRIGGER TDA_ANALOG_MAP_TO_FES AFTER DELETE ON analog_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ANALOG_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TI_ANALOG_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    
/*
INSERT INTO analog(
TAG_NAME,                                               DESCRIPTION,     RTU_TAG, DEV_TYPE, DEVICE,  SEQ_NO,    LOCATION_ID, SUB_SYSTEM, IS_CONTROL,    GRAY, CONTRAST, QCOLOR, VALUE, STATUS, LAST_UPDATE_TIME, LAST_CHANGE_TIME, UNIT_ID,     SOUND_NAME1, SOUND_NAME2, SOUND_NAME3, REGION_ID, OPT_HANDOVER_GROUP, IS_LIMIT,    POINT_TYPE,   POINT_CLASS,    POINT_PROPERTY,    POINT_SORT,   POINT_TP_NAME, IS_SAMPLE,    IS_STATISTICS,   SAMPLE_PERIOD,    SAMPLE_DEADBAND,   CAMERA_TAG, PRESET_ID) 
SELECT 
concat(TAG_NAME, '.' , getAfterLastDot(NEW.TAG_NAME)) , NEW.DESCRIPTION ,'',      DEV_TYPE, TAG_NAME,NEW.SEQ_NO,LOCATION_ID, SUB_SYSTEM, NEW.IS_CONTROL,GRAY, CONTRAST, QCOLOR, VALUE, STATUS,  0,                0,               NEW.UNIT_ID, '',          '',          '',          REGION_ID, OPT_HANDOVER_GROUP,NEW.IS_LIMIT,NEW.POINT_TYPE,NEW.POINT_CLASS,NEW.POINT_PROPERTY,NEW.POINT_SORT,NEW.TAG_NAME, NEW.IS_SAMPLE,NEW.IS_STATISTICS,NEW.SAMPLE_PERIOD,NEW.SAMPLE_DEADBAND, '',      ''
FROM dev_info WHERE DEV_TP_NAME = NEW.DEV_TP_NAME;
*/
    
    INSERT INTO temp_plugin_point_map_ana
    (PLUGIN_NAME, TEMP_NAME, TEMP_POINT_NAME,            FES_TEMP_NAME, FES_TEMP_POINT_NAME, FES_TEMP_OUT_NAME)
    SELECT 
    PLUGIN_NAME, TEMP_NAME, getAfterLastDot(NEW.TAG_NAME),'','',''
    FROM temp_plugin_dev_map WHERE TEMP_NAME = NEW.DEV_TP_NAME;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'analog_temp_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('analog_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_ANALOG_TEMP_DEFINE ON analog_temp_define;
CREATE TRIGGER TI_ANALOG_TEMP_DEFINE AFTER INSERT ON analog_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_ANALOG_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TU_ANALOG_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    /*
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.DESCRIPTION <> OLD.DESCRIPTION OR NEW.SEQ_NO <> OLD.SEQ_NO OR NEW.IS_CONTROL <> OLD.IS_CONTROL OR NEW.UNIT_ID <> OLD.UNIT_ID OR
    NEW.IS_LIMIT <> OLD.IS_LIMIT OR NEW.POINT_TYPE <> OLD.POINT_TYPE OR NEW.POINT_CLASS <> OLD.POINT_CLASS OR NEW.POINT_PROPERTY <> OLD.POINT_PROPERTY OR NEW.POINT_SORT <> OLD.POINT_SORT OR
    NEW.IS_SAMPLE <> OLD.IS_SAMPLE OR NEW.IS_STATISTICS <> OLD.IS_STATISTICS OR NEW.SAMPLE_PERIOD <> OLD.SAMPLE_PERIOD OR NEW.SAMPLE_DEADBAND <> OLD.SAMPLE_DEADBAND 
    THEN 
        UPDATE analog  SET 
        TAG_NAME = concat(DEVICE, '.', getAfterLastDot(NEW.TAG_NAME)),
        DESCRIPTION = NEW.DESCRIPTION,
        SEQ_NO = NEW.SEQ_NO,
        IS_CONTROL = NEW.IS_CONTROL,
        UNIT_ID = NEW.UNIT_ID,
        IS_LIMIT = NEW.IS_LIMIT,
        POINT_TYPE = NEW.POINT_TYPE,
        POINT_CLASS = NEW.POINT_CLASS,
        POINT_PROPERTY = NEW.POINT_PROPERTY,
        POINT_SORT = NEW.POINT_SORT,
        POINT_TP_NAME = NEW.TAG_NAME,
        IS_SAMPLE = NEW.IS_SAMPLE,
        IS_STATISTICS = NEW.IS_STATISTICS,
        SAMPLE_PERIOD = NEW.SAMPLE_PERIOD,
        SAMPLE_DEADBAND = NEW.SAMPLE_DEADBAND
        WHERE POINT_TP_NAME=OLD.TAG_NAME;
    END IF;
    
    IF NEW.IS_TAGT_STATE <> OLD.IS_TAGT_STATE OR NEW.CTRL_TIMEOUT <> OLD.CTRL_TIMEOUT OR NEW.RESV_TIMEOUT <> OLD.RESV_TIMEOUT OR NEW.BASE <> OLD.BASE OR NEW.COEFF <> OLD.COEFF OR
    NEW.RANGE_UP <> OLD.RANGE_UP OR NEW.RANGE_LOW <> OLD.RANGE_LOW OR NEW.CTRL_TOLERANCE <> OLD.CTRL_TOLERANCE OR NEW.TAG_NAME <> OLD.TAG_NAME 
    THEN
        UPDATE analog_control SET
        IS_TAGT_STATE = NEW.IS_TAGT_STATE,
        CTRL_TIMEOUT = NEW.CTRL_TIMEOUT,
        RESV_TIMEOUT = NEW.RESV_TIMEOUT,
        BASE = NEW.BASE,
        COEFF = NEW.COEFF,
        RANGE_UP = NEW.RANGE_UP,
        RANGE_LOW = NEW.RANGE_LOW,
        CTRL_TOLERANCE = NEW.CTRL_TOLERANCE,
        POINT_TP_NAME = NEW.TAG_NAME
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    
    IF NEW.LIMIT_TYPE <> OLD.LIMIT_TYPE OR NEW.LIMIT_NUM <> OLD.LIMIT_NUM OR NEW.LIMIT_UP1 <> OLD.LIMIT_UP1 OR NEW.LIMIT_LOW1 <> OLD.LIMIT_LOW1 OR NEW.LIMIT_UP2 <> OLD.LIMIT_UP2 OR
    NEW.LIMIT_LOW2 <> OLD.LIMIT_LOW2 OR NEW.LIMIT_UP3 <> OLD.LIMIT_UP3 OR NEW.LIMIT_LOW3 <> OLD.LIMIT_LOW3 OR NEW.CROSS_PERCENT <> OLD.CROSS_PERCENT OR NEW.RETURN_PERCENT <> OLD.RETURN_PERCENT OR
    NEW.ALARM_PRIORITY <> OLD.ALARM_PRIORITY OR NEW.ALARM_DELAY_TIME <> OLD.ALARM_DELAY_TIME OR NEW.IS_WATER_ALM <> OLD.IS_WATER_ALM OR NEW.IS_ACK_ON_RTN <> OLD.IS_ACK_ON_RTN OR
    NEW.DEL_ACT_ON_ACK <> OLD.DEL_ACT_ON_ACK OR NEW.IS_NEVER_ALM_ON_RTN <> OLD.IS_NEVER_ALM_ON_RTN OR NEW.TAG_NAME <> OLD.TAG_NAME 
    THEN 
        UPDATE analog_limit_info SET
        LIMIT_TYPE = NEW.LIMIT_TYPE ,
        LIMIT_NUM = NEW.LIMIT_NUM ,
        LIMIT_UP1 = NEW.LIMIT_UP1 ,
        LIMIT_LOW1 = NEW.LIMIT_LOW1 ,
        LIMIT_UP2 = NEW.LIMIT_UP2 ,
        LIMIT_LOW2 = NEW.LIMIT_LOW2 ,
        LIMIT_UP3 = NEW.LIMIT_UP3 ,
        LIMIT_LOW3 = NEW.LIMIT_LOW3 ,
        CROSS_PERCENT = NEW.CROSS_PERCENT ,
        RETURN_PERCENT = NEW.RETURN_PERCENT ,
        ALARM_PRIORITY = NEW.ALARM_PRIORITY ,
        ALARM_DELAY_TIME = NEW.ALARM_DELAY_TIME ,
        IS_WATER_ALM = NEW.IS_WATER_ALM ,
        IS_ACK_ON_RTN = NEW.IS_ACK_ON_RTN ,
        DEL_ACT_ON_ACK = NEW.DEL_ACT_ON_ACK ,
        IS_NEVER_ALM_ON_RTN = NEW.IS_NEVER_ALM_ON_RTN,
        POINT_TP_NAME = NEW.TAG_NAME
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.IS_FILTER <> OLD.IS_FILTER OR NEW.PERCENT <> OLD.PERCENT OR NEW.IS_VALID <> OLD.IS_VALID OR NEW.DEADBAND <> OLD.DEADBAND OR
    NEW.ZEROBAND <> OLD.ZEROBAND OR NEW.BASE <> OLD.BASE OR NEW.COEFF <> OLD.COEFF OR NEW.RANGE_UP <> OLD.RANGE_UP OR NEW.RANGE_LOW <> OLD.RANGE_LOW 
    THEN
        UPDATE analog_map_to_fes SET
        POINT_TP_NAME = NEW.TAG_NAME,
        IS_FILTER = NEW.IS_FILTER,
        PERCENT =  NEW.PERCENT,
        IS_VALID =  NEW.IS_VALID,
        DEADBAND =  NEW.DEADBAND,
        ZEROBAND =  NEW.ZEROBAND,
        BASE = NEW.BASE,
        COEFF = NEW.COEFF,
        RANGE_UP = NEW.RANGE_UP,
        RANGE_LOW = NEW.RANGE_LOW
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    */
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
        UPDATE temp_plugin_point_map_ana
        SET TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME)
        WHERE TEMP_NAME = OLD.DEV_TP_NAME AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    END IF;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'analog_temp_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('analog_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_ANALOG_TEMP_DEFINE ON analog_temp_define;
CREATE TRIGGER TU_ANALOG_TEMP_DEFINE AFTER UPDATE ON analog_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_ANALOG_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TD_ANALOG_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM temp_plugin_point_map_ana WHERE TEMP_NAME = OLD.DEV_TP_NAME  AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
/*
    DELETE FROM analog WHERE POINT_TP_NAME = OLD.TAG_NAME;
*/
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_ANALOG_TEMP_DEFINE ON analog_temp_define;
CREATE TRIGGER TD_ANALOG_TEMP_DEFINE BEFORE DELETE ON analog_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_ANALOG_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ANALOG_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'analog_temp_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('analog_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ANALOG_TEMP_DEFINE ON analog_temp_define;
CREATE TRIGGER TDA_ANALOG_TEMP_DEFINE AFTER DELETE ON analog_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ANALOG_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_APC_ANALOG() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_analog', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('apc_analog'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_APC_ANALOG ON APC_ANALOG;
CREATE TRIGGER TIA_APC_ANALOG AFTER INSERT ON APC_ANALOG FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_APC_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TUB_APC_ANALOG() RETURNS TRIGGER AS $$
BEGIN
IF NEW.VALUE_FROM_SELF IS NOT NULL THEN 
    NEW.VALUE = NEW.VALUE_FROM_SELF;
    NEW.VALUE_FROM_SELF = NULL;
ELSEIF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_analog', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('apc_analog'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUB_APC_ANALOG ON APC_ANALOG;
CREATE TRIGGER TUB_APC_ANALOG BEFORE UPDATE ON APC_ANALOG FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUB_APC_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TDA_APC_ANALOG() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_analog', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('apc_analog'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_APC_ANALOG ON APC_ANALOG;
CREATE TRIGGER TDA_APC_ANALOG AFTER DELETE ON APC_ANALOG FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_APC_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TIA_apc_curve() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('apc_curve'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_apc_curve ON apc_curve;
CREATE TRIGGER TIA_apc_curve AFTER INSERT ON apc_curve FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_apc_curve();



CREATE OR REPLACE FUNCTION FUNC_TUA_apc_curve() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('apc_curve'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_apc_curve ON apc_curve;
CREATE TRIGGER TUA_apc_curve AFTER UPDATE ON apc_curve FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_apc_curve();



CREATE OR REPLACE FUNCTION FUNC_TDA_apc_curve() RETURNS TRIGGER AS $$
BEGIN
delete from apc_curve_day_index where curve_key_id_tag = old.key_id_tag;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('apc_curve'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_apc_curve ON apc_curve;
CREATE TRIGGER TDA_apc_curve AFTER DELETE ON apc_curve FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_apc_curve();



CREATE OR REPLACE FUNCTION FUNC_TIA_APC_CURVE_DAY_INDEX() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                             KEY_OLD_VALUE,KEY_NEW_VALUE,                                                          SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_day_index', 'curve_key_id_tag,year_rule_id,week_rule_id', '',          concat(NEW.CURVE_KEY_ID_TAG,',',NEW.YEAR_RULE_ID,',',NEW.WEEK_RULE_ID), getTableSubSystem('apc_curve_day_index'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_APC_CURVE_DAY_INDEX ON apc_curve_day_index;
CREATE TRIGGER TIA_APC_CURVE_DAY_INDEX AFTER INSERT ON apc_curve_day_index FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_APC_CURVE_DAY_INDEX();



CREATE OR REPLACE FUNCTION FUNC_TUA_APC_CURVE_DAY_INDEX() RETURNS TRIGGER AS $$
BEGIN
IF((select count(*) from apc_curve_day_index where day_rule_id = old.day_rule_id)<1) THEN
delete from apc_curve_day_value where day_rule_id = old.day_rule_id;
END IF;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                              KEY_OLD_VALUE,                                                          KEY_NEW_VALUE,                                                          SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve_day_index', 'curve_key_id_tag,year_rule_id,week_rule_id', concat(OLD.CURVE_KEY_ID_TAG,',',OLD.YEAR_RULE_ID,',',OLD.WEEK_RULE_ID), concat(NEW.CURVE_KEY_ID_TAG,',',NEW.YEAR_RULE_ID,',',NEW.WEEK_RULE_ID), getTableSubSystem('apc_curve_day_index'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_APC_CURVE_DAY_INDEX ON apc_curve_day_index;
CREATE TRIGGER TUA_APC_CURVE_DAY_INDEX AFTER UPDATE ON apc_curve_day_index FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_APC_CURVE_DAY_INDEX();



CREATE OR REPLACE FUNCTION FUNC_TDA_APC_CURVE_DAY_INDEX() RETURNS TRIGGER AS $$
BEGIN
delete from apc_curve_day_value where day_rule_id = old.day_rule_id;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                             KEY_OLD_VALUE,                                                         KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_day_index', 'curve_key_id_tag,year_rule_id,week_rule_id',concat(OLD.CURVE_KEY_ID_TAG,',',OLD.YEAR_RULE_ID,',',OLD.WEEK_RULE_ID), '', getTableSubSystem('apc_curve_day_index'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_APC_CURVE_DAY_INDEX ON apc_curve_day_index;
CREATE TRIGGER TDA_APC_CURVE_DAY_INDEX AFTER DELETE ON apc_curve_day_index FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_APC_CURVE_DAY_INDEX();



CREATE OR REPLACE FUNCTION FUNC_TIA_APC_CURVE_DAY_VALUE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                 KEY_OLD_VALUE,KEY_NEW_VALUE,                       SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_day_value', 'day_rule_id,start_sec,end_sec',  '',          concat(NEW.DAY_RULE_ID,',',NEW.START_SEC,',',NEW.END_SEC), getTableSubSystem('apc_curve_day_value'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_APC_CURVE_DAY_VALUE ON apc_curve_day_value;
CREATE TRIGGER TIA_APC_CURVE_DAY_VALUE AFTER INSERT ON apc_curve_day_value FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_APC_CURVE_DAY_VALUE();



CREATE OR REPLACE FUNCTION FUNC_TUA_APC_CURVE_DAY_VALUE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                  KEY_OLD_VALUE,                                              KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve_day_value', 'day_rule_id,start_sec,end_sec', concat(OLD.DAY_RULE_ID,',',OLD.START_SEC,',',OLD.END_SEC), concat(NEW.DAY_RULE_ID,',',NEW.START_SEC,',',NEW.END_SEC), getTableSubSystem('apc_curve_day_value'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_APC_CURVE_DAY_VALUE ON apc_curve_day_value;
CREATE TRIGGER TUA_APC_CURVE_DAY_VALUE AFTER UPDATE ON apc_curve_day_value FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_APC_CURVE_DAY_VALUE();



CREATE OR REPLACE FUNCTION FUNC_TDA_APC_CURVE_DAY_VALUE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                      KEY_OLD_VALUE,                                            KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_day_value', 'day_rule_id,start_sec,end_sec', concat(OLD.DAY_RULE_ID,',',OLD.START_SEC,',',OLD.END_SEC), '',           getTableSubSystem('apc_curve_day_value'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_APC_CURVE_DAY_VALUE ON apc_curve_day_value;
CREATE TRIGGER TDA_APC_CURVE_DAY_VALUE AFTER DELETE ON apc_curve_day_value FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_APC_CURVE_DAY_VALUE();



CREATE OR REPLACE FUNCTION FUNC_TIA_APC_CURVE_WEEK() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,    KEY_COLUMN_NAME,       KEY_OLD_VALUE,KEY_NEW_VALUE,                       SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_week', 'type_id,week_day',  '',          concat(NEW.TYPE_ID,',',NEW.WEEK_DAY), getTableSubSystem('apc_curve_week'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_APC_CURVE_WEEK ON apc_curve_week;
CREATE TRIGGER TIA_APC_CURVE_WEEK AFTER INSERT ON apc_curve_week FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_APC_CURVE_WEEK();



CREATE OR REPLACE FUNCTION FUNC_TUA_APC_CURVE_WEEK() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,      KEY_OLD_VALUE,                        KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve_week', 'type_id,week_day', concat(OLD.TYPE_ID,',',OLD.WEEK_DAY), concat(NEW.TYPE_ID,',',NEW.WEEK_DAY), getTableSubSystem('apc_curve_week'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_APC_CURVE_WEEK ON apc_curve_week;
CREATE TRIGGER TUA_APC_CURVE_WEEK AFTER UPDATE ON apc_curve_week FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_APC_CURVE_WEEK();



CREATE OR REPLACE FUNCTION FUNC_TDA_APC_CURVE_WEEK() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,      KEY_OLD_VALUE,                    KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_week', 'type_id,week_day', concat(OLD.TYPE_ID,',',OLD.WEEK_DAY), '',           getTableSubSystem('apc_curve_week'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_APC_CURVE_WEEK ON apc_curve_week;
CREATE TRIGGER TDA_APC_CURVE_WEEK AFTER DELETE ON apc_curve_week FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_APC_CURVE_WEEK();



CREATE OR REPLACE FUNCTION FUNC_TIA_APC_CURVE_YEAR() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,       KEY_COLUMN_NAME,                                   KEY_OLD_VALUE,KEY_NEW_VALUE,                       SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_year', 'type_id,month_begin,day_begin,month_end,day_end',  '',          concat(NEW.TYPE_ID,',',NEW.MONTH_BEGIN,',',NEW.DAY_BEGIN,',',NEW.MONTH_END,',',NEW.DAY_END), getTableSubSystem('apc_curve_year'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_APC_CURVE_YEAR ON apc_curve_year;
CREATE TRIGGER TIA_APC_CURVE_YEAR AFTER INSERT ON apc_curve_year FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_APC_CURVE_YEAR();



CREATE OR REPLACE FUNCTION FUNC_TUA_APC_CURVE_YEAR() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,                                      KEY_OLD_VALUE,                                                                             KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve_year', 'type_id,month_begin,day_begin,month_end,day_end', concat(OLD.TYPE_ID,',',OLD.MONTH_BEGIN,',',OLD.DAY_BEGIN,',',OLD.MONTH_END,',',OLD.DAY_END), concat(NEW.TYPE_ID,',',NEW.MONTH_BEGIN,',',NEW.DAY_BEGIN,',',NEW.MONTH_END,',',NEW.DAY_END), getTableSubSystem('apc_curve_year'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_APC_CURVE_YEAR ON apc_curve_year;
CREATE TRIGGER TUA_APC_CURVE_YEAR AFTER UPDATE ON apc_curve_year FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_APC_CURVE_YEAR();



CREATE OR REPLACE FUNCTION FUNC_TDA_APC_CURVE_YEAR() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                                  KEY_OLD_VALUE,                                                                             KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_year', 'type_id,month_begin,day_begin,month_end,day_end', concat(OLD.TYPE_ID,',',OLD.MONTH_BEGIN,',',OLD.DAY_BEGIN,',',OLD.MONTH_END,',',OLD.DAY_END), '',           getTableSubSystem('apc_curve_year'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_APC_CURVE_YEAR ON apc_curve_year;
CREATE TRIGGER TDA_APC_CURVE_YEAR AFTER DELETE ON apc_curve_year FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_APC_CURVE_YEAR();



CREATE OR REPLACE FUNCTION FUNC_TIA_APC_DIGITAL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,    KEY_COLUMN_NAME,  KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_digital', 'key_id_tag',      '',           NEW.KEY_ID_TAG, getTableSubSystem('apc_digital'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_APC_DIGITAL ON apc_digital;
CREATE TRIGGER TIA_APC_DIGITAL AFTER INSERT ON apc_digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_APC_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TUB_APC_DIGITAL() RETURNS TRIGGER AS $$
BEGIN
IF NEW.VALUE_FROM_SELF IS NOT NULL THEN 
    NEW.VALUE = NEW.VALUE_FROM_SELF;
    NEW.VALUE_FROM_SELF = NULL;
ELSEIF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,      KEY_OLD_VALUE,  KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_digital', 'key_id_tag',          OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('apc_digital'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUB_APC_DIGITAL ON apc_digital;
CREATE TRIGGER TUB_APC_DIGITAL BEFORE UPDATE ON apc_digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUB_APC_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TDA_APC_DIGITAL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,   KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_digital', 'key_id_tag',       OLD.KEY_ID_TAG, '',           getTableSubSystem('apc_digital'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_APC_DIGITAL ON apc_digital;
CREATE TRIGGER TDA_APC_DIGITAL AFTER DELETE ON apc_digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_APC_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TIA_ATS_CASCO_PLANNED_TRAIN_DATA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'ats_casco_planned_train_data', 'zdate,location_id,platform_id,table_id,train_id', '', concat(NEW.ZDATE,',',NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.TABLE_ID,',',NEW.TRAIN_ID), getTableSubSystem('ats_casco_planned_train_data'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ATS_CASCO_PLANNED_TRAIN_DATA ON ats_casco_planned_train_data;
CREATE TRIGGER TIA_ATS_CASCO_PLANNED_TRAIN_DATA AFTER INSERT ON ats_casco_planned_train_data FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ATS_CASCO_PLANNED_TRAIN_DATA();



CREATE OR REPLACE FUNCTION FUNC_TUA_ATS_CASCO_PLANNED_TRAIN_DATA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'ats_casco_planned_train_data', 'zdate,location_id,platform_id,table_id,train_id', concat(OLD.ZDATE,',',OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.TABLE_ID,',',OLD.TRAIN_ID), concat(NEW.ZDATE,',',NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.TABLE_ID,',',NEW.TRAIN_ID), getTableSubSystem('ats_casco_planned_train_data'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ATS_CASCO_PLANNED_TRAIN_DATA ON ats_casco_planned_train_data;
CREATE TRIGGER TUA_ATS_CASCO_PLANNED_TRAIN_DATA AFTER UPDATE ON ats_casco_planned_train_data FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ATS_CASCO_PLANNED_TRAIN_DATA();



CREATE OR REPLACE FUNCTION FUNC_TDA_ATS_CASCO_PLANNED_TRAIN_DATA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'ats_casco_planned_train_data', 'zdate,location_id,platform_id,table_id,train_id', concat(OLD.ZDATE,',',OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.TABLE_ID,',',OLD.TRAIN_ID), '', getTableSubSystem('ats_casco_planned_train_data'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ATS_CASCO_PLANNED_TRAIN_DATA ON ats_casco_planned_train_data;
CREATE TRIGGER TDA_ATS_CASCO_PLANNED_TRAIN_DATA AFTER DELETE ON ats_casco_planned_train_data FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ATS_CASCO_PLANNED_TRAIN_DATA();



CREATE OR REPLACE FUNCTION FUNC_TIA_ATS_CASCO_PRACTICAL_TRAIN_DATA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'ats_casco_practical_train_data', 'zdate,location_id,platform_id,table_id,train_id', '', concat(NEW.ZDATE,',',NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.TABLE_ID,',',NEW.TRAIN_ID), getTableSubSystem('ats_casco_practical_train_data'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ATS_CASCO_PRACTICAL_TRAIN_DATA ON ats_casco_practical_train_data;
CREATE TRIGGER TIA_ATS_CASCO_PRACTICAL_TRAIN_DATA AFTER INSERT ON ats_casco_practical_train_data FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ATS_CASCO_PRACTICAL_TRAIN_DATA();



CREATE OR REPLACE FUNCTION FUNC_TUA_ATS_CASCO_PRACTICAL_TRAIN_DATA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'ats_casco_practical_train_data', 'zdate,location_id,platform_id,table_id,train_id', concat(OLD.ZDATE,',',OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.TABLE_ID,',',OLD.TRAIN_ID), concat(NEW.ZDATE,',',NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.TABLE_ID,',',NEW.TRAIN_ID), getTableSubSystem('ats_casco_practical_train_data'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ATS_CASCO_PRACTICAL_TRAIN_DATA ON ats_casco_practical_train_data;
CREATE TRIGGER TUA_ATS_CASCO_PRACTICAL_TRAIN_DATA AFTER UPDATE ON ats_casco_practical_train_data FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ATS_CASCO_PRACTICAL_TRAIN_DATA();



CREATE OR REPLACE FUNCTION FUNC_TDA_ATS_CASCO_PRACTICAL_TRAIN_DATA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'ats_casco_practical_train_data', 'zdate,location_id,platform_id,table_id,train_id', concat(OLD.ZDATE,',',OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.TABLE_ID,',',OLD.TRAIN_ID), '', getTableSubSystem('ats_casco_practical_train_data'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ATS_CASCO_PRACTICAL_TRAIN_DATA ON ats_casco_practical_train_data;
CREATE TRIGGER TDA_ATS_CASCO_PRACTICAL_TRAIN_DATA AFTER DELETE ON ats_casco_practical_train_data FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ATS_CASCO_PRACTICAL_TRAIN_DATA();



CREATE OR REPLACE FUNCTION FUNC_TIA_ATS_FES_LOCATION_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'ats_fes_location_info', 'ats_location_id', '', NEW.ATS_LOCATION_ID, getTableSubSystem('ats_fes_location_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ATS_FES_LOCATION_INFO ON ats_fes_location_info;
CREATE TRIGGER TIA_ATS_FES_LOCATION_INFO AFTER INSERT ON ats_fes_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ATS_FES_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_ATS_FES_LOCATION_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'ats_fes_location_info', 'ats_location_id', OLD.ATS_LOCATION_ID, NEW.ATS_LOCATION_ID, getTableSubSystem('ats_fes_location_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ATS_FES_LOCATION_INFO ON ats_fes_location_info;
CREATE TRIGGER TUA_ATS_FES_LOCATION_INFO AFTER UPDATE ON ats_fes_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ATS_FES_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_ATS_FES_LOCATION_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'ats_fes_location_info', 'ats_location_id', OLD.ATS_LOCATION_ID, '', getTableSubSystem('ats_fes_location_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ATS_FES_LOCATION_INFO ON ats_fes_location_info;
CREATE TRIGGER TDA_ATS_FES_LOCATION_INFO AFTER DELETE ON ats_fes_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ATS_FES_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_ATS_FES_PLATFORM_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'ats_fes_platform_info', 'location_id,platform_id,platform_des', '', concat(NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.PLATFORM_DES), getTableSubSystem('ats_fes_platform_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ATS_FES_PLATFORM_INFO ON ats_fes_platform_info;
CREATE TRIGGER TIA_ATS_FES_PLATFORM_INFO AFTER INSERT ON ats_fes_platform_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ATS_FES_PLATFORM_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_ATS_FES_PLATFORM_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'ats_fes_platform_info', 'location_id,platform_id,platform_des', concat(OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.PLATFORM_DES), concat(NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.PLATFORM_DES), getTableSubSystem('ats_fes_platform_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ATS_FES_PLATFORM_INFO ON ats_fes_platform_info;
CREATE TRIGGER TUA_ATS_FES_PLATFORM_INFO AFTER UPDATE ON ats_fes_platform_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ATS_FES_PLATFORM_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_ATS_FES_PLATFORM_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'ats_fes_platform_info', 'location_id,platform_id,platform_des', concat(OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.PLATFORM_DES), '', getTableSubSystem('ats_fes_platform_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ATS_FES_PLATFORM_INFO ON ats_fes_platform_info;
CREATE TRIGGER TDA_ATS_FES_PLATFORM_INFO AFTER DELETE ON ats_fes_platform_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ATS_FES_PLATFORM_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_BAS_TIMETABLE_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'bas_timetable_ctrl_param', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('bas_timetable_ctrl_param'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_BAS_TIMETABLE_CTRL_PARAM ON bas_timetable_ctrl_param;
CREATE TRIGGER TIA_BAS_TIMETABLE_CTRL_PARAM AFTER INSERT ON bas_timetable_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_BAS_TIMETABLE_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TUA_BAS_TIMETABLE_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'bas_timetable_ctrl_param', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('bas_timetable_ctrl_param'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_BAS_TIMETABLE_CTRL_PARAM ON bas_timetable_ctrl_param;
CREATE TRIGGER TUA_BAS_TIMETABLE_CTRL_PARAM AFTER UPDATE ON bas_timetable_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_BAS_TIMETABLE_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TDA_BAS_TIMETABLE_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'bas_timetable_ctrl_param', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('bas_timetable_ctrl_param'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_BAS_TIMETABLE_CTRL_PARAM ON bas_timetable_ctrl_param;
CREATE TRIGGER TDA_BAS_TIMETABLE_CTRL_PARAM AFTER DELETE ON bas_timetable_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_BAS_TIMETABLE_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TIA_BAS_TIMETABLE_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'bas_timetable_def', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('bas_timetable_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_BAS_TIMETABLE_DEF ON bas_timetable_def;
CREATE TRIGGER TIA_BAS_TIMETABLE_DEF AFTER INSERT ON bas_timetable_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_BAS_TIMETABLE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_BAS_TIMETABLE_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'bas_timetable_def', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('bas_timetable_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_BAS_TIMETABLE_DEF ON bas_timetable_def;
CREATE TRIGGER TUA_BAS_TIMETABLE_DEF AFTER UPDATE ON bas_timetable_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_BAS_TIMETABLE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_BAS_TIMETABLE_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'bas_timetable_def', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('bas_timetable_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_BAS_TIMETABLE_DEF ON bas_timetable_def;
CREATE TRIGGER TDA_BAS_TIMETABLE_DEF AFTER DELETE ON bas_timetable_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_BAS_TIMETABLE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_BAS_TIMETABLE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'bas_timetable_info', 'location_id,timetable_id,begin_time', '', concat(NEW.LOCATION_ID,',',NEW.TIMETABLE_ID,',',NEW.BEGIN_TIME), getTableSubSystem('bas_timetable_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_BAS_TIMETABLE_INFO ON bas_timetable_info;
CREATE TRIGGER TIA_BAS_TIMETABLE_INFO AFTER INSERT ON bas_timetable_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_BAS_TIMETABLE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_BAS_TIMETABLE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'bas_timetable_info', 'location_id,timetable_id,begin_time', concat(OLD.LOCATION_ID,',',OLD.TIMETABLE_ID,',',OLD.BEGIN_TIME), concat(NEW.LOCATION_ID,',',NEW.TIMETABLE_ID,',',NEW.BEGIN_TIME), getTableSubSystem('bas_timetable_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_BAS_TIMETABLE_INFO ON bas_timetable_info;
CREATE TRIGGER TUA_BAS_TIMETABLE_INFO AFTER UPDATE ON bas_timetable_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_BAS_TIMETABLE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_BAS_TIMETABLE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'bas_timetable_info', 'location_id,timetable_id,begin_time', concat(OLD.LOCATION_ID,',',OLD.TIMETABLE_ID,',',OLD.BEGIN_TIME), '', getTableSubSystem('bas_timetable_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_BAS_TIMETABLE_INFO ON bas_timetable_info;
CREATE TRIGGER TDA_BAS_TIMETABLE_INFO AFTER DELETE ON bas_timetable_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_BAS_TIMETABLE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_BAS_TIMETABLE_SCHEDULE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'bas_timetable_schedule', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('bas_timetable_schedule'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_BAS_TIMETABLE_SCHEDULE ON bas_timetable_schedule;
CREATE TRIGGER TIA_BAS_TIMETABLE_SCHEDULE AFTER INSERT ON bas_timetable_schedule FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_BAS_TIMETABLE_SCHEDULE();



CREATE OR REPLACE FUNCTION FUNC_TUA_BAS_TIMETABLE_SCHEDULE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'bas_timetable_schedule', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('bas_timetable_schedule'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_BAS_TIMETABLE_SCHEDULE ON bas_timetable_schedule;
CREATE TRIGGER TUA_BAS_TIMETABLE_SCHEDULE AFTER UPDATE ON bas_timetable_schedule FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_BAS_TIMETABLE_SCHEDULE();



CREATE OR REPLACE FUNCTION FUNC_TDA_BAS_TIMETABLE_SCHEDULE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'bas_timetable_schedule', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('bas_timetable_schedule'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_BAS_TIMETABLE_SCHEDULE ON bas_timetable_schedule;
CREATE TRIGGER TDA_BAS_TIMETABLE_SCHEDULE AFTER DELETE ON bas_timetable_schedule FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_BAS_TIMETABLE_SCHEDULE();



CREATE OR REPLACE FUNCTION FUNC_TI_CALC_FUNC_DEF() RETURNS TRIGGER AS $$
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_func_def', 'func_name', '', NEW.FUNC_NAME, getTableSubSystem('calc_func_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_CALC_FUNC_DEF ON calc_func_def;
CREATE TRIGGER TI_CALC_FUNC_DEF AFTER INSERT ON calc_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_CALC_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TU_CALC_FUNC_DEF() RETURNS TRIGGER AS $$
BEGIN
   
    UPDATE calc_out_para SET
    FUNC_NAME = NEW.FUNC_NAME,
    CALC_FLAG = '1'
    WHERE FUNC_NAME = OLD.FUNC_NAME;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,   KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_func_def', 'func_name',     OLD.FUNC_NAME,  NEW.FUNC_NAME, getTableSubSystem('calc_func_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_CALC_FUNC_DEF ON calc_func_def;
CREATE TRIGGER TU_CALC_FUNC_DEF AFTER UPDATE ON calc_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_CALC_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TD_CALC_FUNC_DEF() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM calc_out_para WHERE FUNC_NAME = OLD.FUNC_NAME;
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_func_def', 'func_name', OLD.FUNC_NAME, '', getTableSubSystem('calc_func_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_CALC_FUNC_DEF ON calc_func_def;
CREATE TRIGGER TD_CALC_FUNC_DEF AFTER DELETE ON calc_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_CALC_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TI_CALC_GROUP_DEF() RETURNS TRIGGER AS $$
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_group_def', 'group_id', '', NEW.GROUP_ID, getTableSubSystem('calc_group_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_CALC_GROUP_DEF ON calc_group_def;
CREATE TRIGGER TI_CALC_GROUP_DEF AFTER INSERT ON calc_group_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_CALC_GROUP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TU_CALC_GROUP_DEF() RETURNS TRIGGER AS $$
BEGIN
   
    IF NEW.GROUP_ID <> OLD.GROUP_ID THEN 
    UPDATE calc_func_def SET GROUP_ID = NEW.GROUP_ID WHERE GROUP_ID = OLD.GROUP_ID;
    END IF;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,   KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_group_def', 'group_id',     OLD.GROUP_ID,  NEW.GROUP_ID, getTableSubSystem('calc_group_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_CALC_GROUP_DEF ON calc_group_def;
CREATE TRIGGER TU_CALC_GROUP_DEF AFTER UPDATE ON calc_group_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_CALC_GROUP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TD_CALC_GROUP_DEF() RETURNS TRIGGER AS $$
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_group_def', 'group_id', OLD.GROUP_ID, '', getTableSubSystem('calc_group_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_CALC_GROUP_DEF ON calc_group_def;
CREATE TRIGGER TD_CALC_GROUP_DEF AFTER DELETE ON calc_group_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_CALC_GROUP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TI_CALC_IN_PARA() RETURNS TRIGGER AS $$
BEGIN
   
    UPDATE calc_out_para SET CALC_FLAG = '1' WHERE KEY_ID_TAG = NEW.KEY_ID_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_in_para', 'key_id_tag,para_seq_no', '', concat(NEW.KEY_ID_TAG,',',NEW.PARA_SEQ_NO), getTableSubSystem('calc_in_para'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_CALC_IN_PARA ON calc_in_para;
CREATE TRIGGER TI_CALC_IN_PARA AFTER INSERT ON calc_in_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_CALC_IN_PARA();



CREATE OR REPLACE FUNCTION FUNC_TU_CALC_IN_PARA() RETURNS TRIGGER AS $$
BEGIN
   
    UPDATE calc_out_para SET CALC_FLAG = '1' WHERE KEY_ID_TAG = NEW.KEY_ID_TAG;
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_in_para', 'key_id_tag,para_seq_no', concat(OLD.KEY_ID_TAG,',',OLD.PARA_SEQ_NO), concat(NEW.KEY_ID_TAG,',',NEW.PARA_SEQ_NO), getTableSubSystem('calc_in_para'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_CALC_IN_PARA ON calc_in_para;
CREATE TRIGGER TU_CALC_IN_PARA AFTER UPDATE ON calc_in_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_CALC_IN_PARA();



CREATE OR REPLACE FUNCTION FUNC_TD_CALC_IN_PARA() RETURNS TRIGGER AS $$
BEGIN
    
    UPDATE calc_out_para SET CALC_FLAG = '1' WHERE KEY_ID_TAG = OLD.KEY_ID_TAG;
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_in_para', 'key_id_tag,para_seq_no', concat(OLD.KEY_ID_TAG,',',OLD.PARA_SEQ_NO), '', getTableSubSystem('calc_in_para'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_CALC_IN_PARA ON calc_in_para;
CREATE TRIGGER TD_CALC_IN_PARA AFTER DELETE ON calc_in_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_CALC_IN_PARA();



CREATE OR REPLACE FUNCTION FUNC_TI_CALC_OUT_PARA() RETURNS TRIGGER AS $$
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_out_para', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('calc_out_para'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_CALC_OUT_PARA ON calc_out_para;
CREATE TRIGGER TI_CALC_OUT_PARA AFTER INSERT ON calc_out_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_CALC_OUT_PARA();



CREATE OR REPLACE FUNCTION FUNC_TU_CALC_OUT_PARA() RETURNS TRIGGER AS $$
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_out_para', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('calc_out_para'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_CALC_OUT_PARA ON calc_out_para;
CREATE TRIGGER TU_CALC_OUT_PARA AFTER UPDATE ON calc_out_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_CALC_OUT_PARA();



CREATE OR REPLACE FUNCTION FUNC_TD_CALC_OUT_PARA() RETURNS TRIGGER AS $$
BEGIN
   
    --这里不能触发删除本输出的输入,因为输入的删除触发器中调用了本表
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_out_para', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('calc_out_para'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_CALC_OUT_PARA ON calc_out_para;
CREATE TRIGGER TD_CALC_OUT_PARA AFTER DELETE ON calc_out_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_CALC_OUT_PARA();



CREATE OR REPLACE FUNCTION FUNC_TI_CALC_PARA_DEF() RETURNS TRIGGER AS $$
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_para_def', 'func_name,para_seq_no', '', concat(NEW.FUNC_NAME,',',NEW.PARA_SEQ_NO), getTableSubSystem('calc_para_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_CALC_PARA_DEF ON calc_para_def;
CREATE TRIGGER TI_CALC_PARA_DEF AFTER INSERT ON calc_para_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_CALC_PARA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TU_CALC_PARA_DEF() RETURNS TRIGGER AS $$
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_para_def', 'func_name,para_seq_no', concat(OLD.FUNC_NAME,',',OLD.PARA_SEQ_NO), concat(NEW.FUNC_NAME,',',NEW.PARA_SEQ_NO), getTableSubSystem('calc_para_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_CALC_PARA_DEF ON calc_para_def;
CREATE TRIGGER TU_CALC_PARA_DEF AFTER UPDATE ON calc_para_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_CALC_PARA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TD_CALC_PARA_DEF() RETURNS TRIGGER AS $$
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_para_def', 'func_name,para_seq_no', concat(OLD.FUNC_NAME,',',OLD.PARA_SEQ_NO), '', getTableSubSystem('calc_para_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_CALC_PARA_DEF ON calc_para_def;
CREATE TRIGGER TD_CALC_PARA_DEF AFTER DELETE ON calc_para_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_CALC_PARA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_CFG_DEVICE_INST() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'cfg_device_inst', 'id', '', NEW.id, getTableSubSystem('cfg_device_inst'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_CFG_DEVICE_INST ON cfg_device_inst;
CREATE TRIGGER TIA_CFG_DEVICE_INST AFTER INSERT ON cfg_device_inst FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_CFG_DEVICE_INST();



CREATE OR REPLACE FUNCTION FUNC_TUA_CFG_DEVICE_INST() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'cfg_device_inst', 'id', OLD.id, NEW.id, getTableSubSystem('cfg_device_inst'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_CFG_DEVICE_INST ON cfg_device_inst;
CREATE TRIGGER TUA_CFG_DEVICE_INST AFTER UPDATE ON cfg_device_inst FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_CFG_DEVICE_INST();



CREATE OR REPLACE FUNCTION FUNC_TDA_CFG_DEVICE_INST() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'cfg_device_inst', 'id', OLD.id, '', getTableSubSystem('cfg_device_inst'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_CFG_DEVICE_INST ON cfg_device_inst;
CREATE TRIGGER TDA_CFG_DEVICE_INST AFTER DELETE ON cfg_device_inst FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_CFG_DEVICE_INST();



CREATE OR REPLACE FUNCTION FUNC_TIA_CFG_SIGNAL_INST() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'cfg_signal_inst', 'id', '', NEW.id, getTableSubSystem('cfg_signal_inst'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_CFG_SIGNAL_INST ON cfg_signal_inst;
CREATE TRIGGER TIA_CFG_SIGNAL_INST AFTER INSERT ON cfg_signal_inst FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_CFG_SIGNAL_INST();



CREATE OR REPLACE FUNCTION FUNC_TUA_CFG_SIGNAL_INST() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'cfg_signal_inst', 'id', OLD.id, NEW.id, getTableSubSystem('cfg_signal_inst'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_CFG_SIGNAL_INST ON cfg_signal_inst;
CREATE TRIGGER TUA_CFG_SIGNAL_INST AFTER UPDATE ON cfg_signal_inst FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_CFG_SIGNAL_INST();



CREATE OR REPLACE FUNCTION FUNC_TDA_CFG_SIGNAL_INST() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'cfg_signal_inst', 'id', OLD.id, '', getTableSubSystem('cfg_signal_inst'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_CFG_SIGNAL_INST ON cfg_signal_inst;
CREATE TRIGGER TDA_CFG_SIGNAL_INST AFTER DELETE ON cfg_signal_inst FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_CFG_SIGNAL_INST();



CREATE OR REPLACE FUNCTION FUNC_TIA_DEV_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'dev_temp_def', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('dev_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DEV_TEMP_DEF ON dev_temp_def;
CREATE TRIGGER TIA_DEV_TEMP_DEF AFTER INSERT ON dev_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DEV_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TU_DEV_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
IF OLD.TAG_NAME != NEW.TAG_NAME THEN
BEGIN
    UPDATE accuml set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
    UPDATE analog set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
    UPDATE digital set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
    UPDATE mix set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
END;
END IF;
    
IF OLD.TAG_NAME != NEW.TAG_NAME OR OLD.DESCRIPTION != NEW.DESCRIPTION  THEN 
BEGIN 
    UPDATE accuml_temp_define SET
    DEV_TP_NAME=NEW.TAG_NAME ,
    TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)) 
    WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
    
    UPDATE analog_temp_define SET
    DEV_TP_NAME=NEW.TAG_NAME ,
    TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)) 
    WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
    
    
    UPDATE digital_temp_define SET
    DEV_TP_NAME=NEW.TAG_NAME ,
    TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)) 
    WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
    
    UPDATE mix_temp_define SET
    DEV_TP_NAME=NEW.TAG_NAME ,
    TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)) 
    WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
    --更新对应的套件
    IF OLD.TAG_NAME != NEW.TAG_NAME THEN 
        BEGIN
            UPDATE temp_plugin_dev_map SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_fes_dev_map SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_point_map_acc SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_point_map_ana SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_point_map_dig SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_point_map_mix SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
        END;
    END IF;
END;
END IF;
/*
IF OLD.TAG_NAME != NEW.TAG_NAME OR OLD.DEV_TYPE_ID != NEW.DEV_TYPE_ID OR OLD.SUB_SYSTEM != NEW.SUB_SYSTEM
THEN 
UPDATE dev_info SET 
DEV_TP_NAME=NEW.TAG_NAME, 
DEV_TYPE=NEW.DEV_TYPE_ID,
SUB_SYSTEM = NEW.SUB_SYSTEM
WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
END IF;
*/
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'dev_temp_def', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('dev_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_DEV_TEMP_DEF ON dev_temp_def;
CREATE TRIGGER TU_DEV_TEMP_DEF AFTER UPDATE ON dev_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_DEV_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TD_DEV_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
    
/*   
DELETE FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME;
*/
DELETE FROM accuml_temp_define WHERE DEV_TP_NAME = OLD.TAG_NAME;
DELETE FROM analog_temp_define WHERE DEV_TP_NAME = OLD.TAG_NAME;
DELETE FROM digital_temp_define WHERE DEV_TP_NAME = OLD.TAG_NAME;
DELETE FROM mix_temp_define WHERE DEV_TP_NAME = OLD.TAG_NAME;
--套件的处理
DELETE FROM temp_plugin_dev_map WHERE TEMP_NAME = OLD.TAG_NAME;
UPDATE accuml set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
UPDATE analog set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
UPDATE digital set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
UPDATE mix set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_DEV_TEMP_DEF ON dev_temp_def;
CREATE TRIGGER TD_DEV_TEMP_DEF BEFORE DELETE ON dev_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_DEV_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_DEV_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'dev_temp_def', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('dev_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DEV_TEMP_DEF ON dev_temp_def;
CREATE TRIGGER TDA_DEV_TEMP_DEF AFTER DELETE ON dev_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DEV_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TU_DEV_TEMP_GROUP() RETURNS TRIGGER AS $$
BEGIN
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN 
    UPDATE dev_temp_def SET DEV_TEMP_GRP = NEW.TAG_NAME WHERE DEV_TEMP_GRP = OLD.TAG_NAME;
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_DEV_TEMP_GROUP ON dev_temp_group;
CREATE TRIGGER TU_DEV_TEMP_GROUP AFTER UPDATE ON dev_temp_group FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_DEV_TEMP_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TD_DEV_TEMP_GROUP() RETURNS TRIGGER AS $$
BEGIN
    
    DELETE FROM dev_temp_def WHERE DEV_TEMP_GRP = OLD.TAG_NAME;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_DEV_TEMP_GROUP ON dev_temp_group;
CREATE TRIGGER TD_DEV_TEMP_GROUP AFTER DELETE ON dev_temp_group FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_DEV_TEMP_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TIA_DIGITAL_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'digital_control', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('digital_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DIGITAL_CONTROL ON digital_control;
CREATE TRIGGER TIA_DIGITAL_CONTROL AFTER INSERT ON digital_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DIGITAL_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TU_DIGITAL_CONTROL() RETURNS TRIGGER AS $$
BEGIN
    
    --修改功能名称
    IF COALESCE(NEW.RTU_TAG,'') = '' THEN 
    BEGIN
        NEW.OFFSET_NO1 = '-1';
        NEW.OFFSET_NO2 = '-1';
        NEW.OFFSET_NO3 = '-1';
        NEW.OFFSET_NO4 = '-1';
        NEW.OFFSET_NO5 = '-1';
    END;
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_DIGITAL_CONTROL ON digital_control;
CREATE TRIGGER TU_DIGITAL_CONTROL BEFORE UPDATE ON digital_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_DIGITAL_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TUA_DIGITAL_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'digital_control', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('digital_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DIGITAL_CONTROL ON digital_control;
CREATE TRIGGER TUA_DIGITAL_CONTROL AFTER UPDATE ON digital_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DIGITAL_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TDA_DIGITAL_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'digital_control', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('digital_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DIGITAL_CONTROL ON digital_control;
CREATE TRIGGER TDA_DIGITAL_CONTROL AFTER DELETE ON digital_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DIGITAL_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TI_DIGITAL_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('FES digtal update','In TI_DIGTAL_MAP_TO_FES');
    
    SET SESSION "disable.FesDigitalTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.DigitalMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(NEW.DOT_NO1,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value1' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO1);
            END IF;
            
            IF COALESCE(NEW.DOT_NO2,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value2' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO2);
            END IF;
            
            IF COALESCE(NEW.DOT_NO3,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value3' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO3);
            END IF;
            
            IF COALESCE(NEW.DOT_NO4,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value4' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO4);
            END IF;
            
            IF COALESCE(NEW.DOT_NO5,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value5' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO5);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    SET SESSION "disable.FesDigitalTrigger" = 0;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'digital_map_to_fes', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('digital_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_DIGITAL_MAP_TO_FES ON digital_map_to_fes;
CREATE TRIGGER TI_DIGITAL_MAP_TO_FES AFTER INSERT ON digital_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_DIGITAL_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TU_DIGITAL_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    IF NEW.RTU_TAG = OLD.RTU_TAG AND 
        NEW.DOT_NO1 = OLD.DOT_NO1 AND 
        NEW.DOT_NO2 = OLD.DOT_NO2 AND 
        NEW.DOT_NO3 = OLD.DOT_NO3 AND 
        NEW.DOT_NO4 = OLD.DOT_NO4 AND 
        NEW.DOT_NO5 = OLD.DOT_NO5 AND 
        NEW.KEY_ID_TAG = OLD.KEY_ID_TAG 
        THEN RETURN NEW;
    END IF;
    
    CALL kbdDebugInfo('FES digtal update','In TU_DIGTAL_MAP_TO_FES');
    
    SET SESSION "disable.FesDigitalTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.DigitalMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(OLD.DOT_NO1,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO1);
            END IF;
            
            IF COALESCE(OLD.DOT_NO2,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO2);
            END IF;
            
            IF COALESCE(OLD.DOT_NO3,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO3);
            END IF;
            
            IF COALESCE(OLD.DOT_NO4,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO4);
            END IF;
            
            IF COALESCE(OLD.DOT_NO5,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO5);
            END IF;
            
        END;
        END IF;
        
        
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(NEW.DOT_NO1,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value1' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO1);
            END IF;
            
            IF COALESCE(NEW.DOT_NO2,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value2' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO2);
            END IF;
            
            IF COALESCE(NEW.DOT_NO3,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value3' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO3);
            END IF;
            
            IF COALESCE(NEW.DOT_NO4,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value4' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO4);
            END IF;
            
            IF COALESCE(NEW.DOT_NO5,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value5' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO5);
            END IF;
            
        END;
        ELSE 
        BEGIN
            NEW.DOT_NO1 = '-1';
            NEW.DOT_NO2 = '-1';
            NEW.DOT_NO3 = '-1';
            NEW.DOT_NO4 = '-1';
            NEW.DOT_NO5 = '-1';
        END; 
        END IF;
    END;
    END IF;
    SET SESSION "disable.FesDigitalTrigger" = 0;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_DIGITAL_MAP_TO_FES ON digital_map_to_fes;
CREATE TRIGGER TU_DIGITAL_MAP_TO_FES BEFORE UPDATE ON digital_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_DIGITAL_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TUA_DIGITAL_MAP_TO_FES() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'digital_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('digital_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DIGITAL_MAP_TO_FES ON digital_map_to_fes;
CREATE TRIGGER TUA_DIGITAL_MAP_TO_FES AFTER UPDATE ON digital_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DIGITAL_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TD_DIGITAL_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('FES digtal update','In TD_DIGTAL_MAP_TO_FES');
    
    SET SESSION "disable.FesDigitalTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.DigitalMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(OLD.DOT_NO1,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO1);
            END IF;
            
            IF COALESCE(OLD.DOT_NO2,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO2);
            END IF;
            
            IF COALESCE(OLD.DOT_NO3,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO3);
            END IF;
            
            IF COALESCE(OLD.DOT_NO4,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO4);
            END IF;
            
            IF COALESCE(OLD.DOT_NO5,'') <> '' THEN 
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO5);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
    SET SESSION "disable.FesDigitalTrigger" = 0;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_DIGITAL_MAP_TO_FES ON digital_map_to_fes;
CREATE TRIGGER TD_DIGITAL_MAP_TO_FES BEFORE DELETE ON digital_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_DIGITAL_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TDA_DIGITAL_MAP_TO_FES() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'digital_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('digital_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DIGITAL_MAP_TO_FES ON digital_map_to_fes;
CREATE TRIGGER TDA_DIGITAL_MAP_TO_FES AFTER DELETE ON digital_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DIGITAL_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TI_DIGITAL_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    
/*
INSERT INTO digital(
TAG_NAME,                                             DESCRIPTION,    DEVICE, SEQ_NO,     LOCATION_ID, SUB_SYSTEM, RTU_TAG, IS_CONTROL,     STATE_TEXT_NAME,    VALUE_NUM,    GRAY, CONTRAST, QCOLOR, VALUE, STATUS, LAST_UPDATE_TIME, VALUE1, STATUS1, LAST_CHANGE_TIME1, VALUE2, STATUS2, LAST_CHANGE_TIME2, VALUE3, STATUS3, LAST_CHANGE_TIME3, VALUE4, STATUS4, LAST_CHANGE_TIME4, VALUE5, STATUS5, LAST_CHANGE_TIME5, ALARM_PRIORITY,     PIC_NAME, SOUND_NAME1, SOUND_NAME2, SOUND_NAME3, ALARM_DELAY_TIME,    INPUT_DELAY_TIME,    IS_WATER_ALM,    IS_ACK_ON_RTN,   DEL_ACT_ON_ACK,     IS_NEVER_ALM_ON_RTN,    REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TYPE, POINT_TP_NAME, IS_SAMPLE,     IS_STATISTICS,   SAMPLE_PERIOD,    IS_ALARM_FREQ,    ALARM_FREQ,    ALARM_FREQ_DESC,    IS_ALARM_TIME,     ALARM_TIME,   ALARM_TIME_DESC,   ALARM_FREQ_RANGE,    INFER_NAME, POINT_TYPE,   POINT_CLASS,   POINT_PROPERTY,     POINT_SORT, CAMERA_TAG, PRESET_ID ) 
SELECT   
concat(TAG_NAME, '.' , getAfterLastDot(NEW.TAG_NAME)),NEW.DESCRIPTION,TAG_NAME,NEW.SEQ_NO,LOCATION_ID, SUB_SYSTEM, "",      NEW.IS_CONTROL, NEW.STATE_TEXT_NAME,NEW.VALUE_NUM,GRAY, CONTRAST, QCOLOR, VALUE, STATUS, '0',              '0',    '0',     '0',              '0',     '0',     '0',               '0',    '0',     '0',               '0',    '0',     '0',               '0',    '0',     '0',               NEW.ALARM_PRIORITY, '',       '',          '',          '',          NEW.ALARM_DELAY_TIME,NEW.INPUT_DELAY_TIME,NEW.IS_WATER_ALM,NEW.IS_ACK_ON_RTN,NEW.DEL_ACT_ON_ACK,NEW.IS_NEVER_ALM_ON_RTN,REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TYPE, NEW.TAG_NAME,  NEW.IS_SAMPLE,NEW.IS_STATISTICS,NEW.SAMPLE_PERIOD,NEW.IS_ALARM_FREQ,NEW.ALARM_FREQ,NEW.ALARM_FREQ_DESC,NEW.IS_ALARM_TIME,NEW.ALARM_TIME,NEW.ALARM_TIME_DESC,NEW.ALARM_FREQ_RANGE,'',       NEW.POINT_TYPE,NEW.POINT_CLASS,NEW.POINT_PROPERTY,NEW.POINT_SORT, '',      ''  
FROM dev_info WHERE DEV_TP_NAME = NEW.DEV_TP_NAME;
*/
    INSERT INTO temp_plugin_point_map_dig
    (PLUGIN_NAME, TEMP_NAME, TEMP_POINT_NAME,              FES_TEMP_NAME, FES_TEMP_POINT_NAME1, FES_TEMP_POINT_NAME2, FES_TEMP_POINT_NAME3, FES_TEMP_POINT_NAME4, FES_TEMP_POINT_NAME5, FES_TEMP_OUT_NAME1, FES_TEMP_OUT_NAME2, FES_TEMP_OUT_NAME3, FES_TEMP_OUT_NAME4, FES_TEMP_OUT_NAME5)
    SELECT 
    PLUGIN_NAME,  TEMP_NAME, getAfterLastDot(NEW.TAG_NAME),'',             '',                    '',                 '',                     '',                 '',                     '',                '',                '',                   '',                ''
    FROM temp_plugin_dev_map WHERE TEMP_NAME = NEW.DEV_TP_NAME;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'digital_temp_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('digital_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_DIGITAL_TEMP_DEFINE ON digital_temp_define;
CREATE TRIGGER TI_DIGITAL_TEMP_DEFINE AFTER INSERT ON digital_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_DIGITAL_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TU_DIGITAL_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    /*
    UPDATE digital SET 
    TAG_NAME = concat(DEVICE, '.', getAfterLastDot(NEW.TAG_NAME)),
    DESCRIPTION = NEW.DESCRIPTION,
    SEQ_NO = NEW.SEQ_NO,
    IS_CONTROL = NEW.IS_CONTROL,
    STATE_TEXT_NAME = NEW.STATE_TEXT_NAME,
    VALUE_NUM = NEW.VALUE_NUM,
    ALARM_PRIORITY = NEW.ALARM_PRIORITY,
    ALARM_DELAY_TIME = NEW.ALARM_DELAY_TIME,
    INPUT_DELAY_TIME = NEW.INPUT_DELAY_TIME,
    IS_WATER_ALM = NEW.IS_WATER_ALM,
    IS_ACK_ON_RTN = NEW.IS_ACK_ON_RTN,
    DEL_ACT_ON_ACK = NEW.DEL_ACT_ON_ACK,
    IS_NEVER_ALM_ON_RTN = NEW.IS_NEVER_ALM_ON_RTN,
    IS_SUMMARY = NEW.IS_SUMMARY,
    POINT_TP_NAME = NEW.TAG_NAME,
    IS_SAMPLE = NEW.IS_SAMPLE,
    IS_STATISTICS = NEW.IS_STATISTICS,
    SAMPLE_PERIOD = NEW.SAMPLE_PERIOD,
    IS_ALARM_FREQ = NEW.IS_ALARM_FREQ,
    ALARM_FREQ = NEW.ALARM_FREQ,
    ALARM_FREQ_DESC = NEW.ALARM_FREQ_DESC,
    IS_ALARM_TIME = NEW.IS_ALARM_TIME,
    ALARM_TIME = NEW.ALARM_TIME,
    ALARM_TIME_DESC = NEW.ALARM_TIME_DESC,
    ALARM_FREQ_RANGE = NEW.ALARM_FREQ_RANGE,
    POINT_TYPE = NEW.POINT_TYPE,
    POINT_CLASS = NEW.POINT_CLASS,
    POINT_PROPERTY = NEW.POINT_PROPERTY,
    POINT_SORT = NEW.POINT_SORT
    WHERE POINT_TP_NAME=OLD.TAG_NAME  ;
    UPDATE digital_control SET
    CTRL_ACT_NAME = NEW.CTRL_ACT_NAME,
    IS_TAGT_STATE = NEW.IS_TAGT_STATE,
    CTRL_TIMEOUT = NEW.CTRL_TIMEOUT,
    RESV_TIMEOUT = NEW.RESV_TIMEOUT,
    PULSE_DURATION = NEW.PULSE_DURATION,
    OFFSET_NUM = NEW.OFFSET_NUM,
    IS_CHECK_PREVENTION = NEW.IS_CHECK_PREVENTION,
    CTRL_TYPE = NEW.CTRL_TYPE,
    POINT_TP_NAME = NEW.TAG_NAME
    WHERE POINT_TP_NAME = OLD.TAG_NAME;
    */
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
        UPDATE temp_plugin_point_map_dig
        SET TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME)
        WHERE TEMP_NAME = OLD.DEV_TP_NAME AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    END IF;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'digital_temp_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('digital_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_DIGITAL_TEMP_DEFINE ON digital_temp_define;
CREATE TRIGGER TU_DIGITAL_TEMP_DEFINE AFTER UPDATE ON digital_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_DIGITAL_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TD_DIGITAL_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM temp_plugin_point_map_dig WHERE TEMP_NAME = OLD.DEV_TP_NAME  AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
/*
DELETE FROM digital WHERE POINT_TP_NAME = OLD.TAG_NAME;
*/
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_DIGITAL_TEMP_DEFINE ON digital_temp_define;
CREATE TRIGGER TD_DIGITAL_TEMP_DEFINE BEFORE DELETE ON digital_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_DIGITAL_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_DIGITAL_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'digital_temp_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('digital_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DIGITAL_TEMP_DEFINE ON digital_temp_define;
CREATE TRIGGER TDA_DIGITAL_TEMP_DEFINE AFTER DELETE ON digital_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DIGITAL_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_FAULT_RECALL_COND_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fault_recall_cond_def', 'location_id,trigger_tag_name', '', concat(NEW.LOCATION_ID,',',NEW.TRIGGER_TAG_NAME), getTableSubSystem('fault_recall_cond_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FAULT_RECALL_COND_DEF ON fault_recall_cond_def;
CREATE TRIGGER TIA_FAULT_RECALL_COND_DEF AFTER INSERT ON fault_recall_cond_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FAULT_RECALL_COND_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_FAULT_RECALL_COND_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fault_recall_cond_def', 'location_id,trigger_tag_name', concat(OLD.LOCATION_ID,',',OLD.TRIGGER_TAG_NAME), concat(NEW.LOCATION_ID,',',NEW.TRIGGER_TAG_NAME), getTableSubSystem('fault_recall_cond_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FAULT_RECALL_COND_DEF ON fault_recall_cond_def;
CREATE TRIGGER TUA_FAULT_RECALL_COND_DEF AFTER UPDATE ON fault_recall_cond_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FAULT_RECALL_COND_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_FAULT_RECALL_COND_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fault_recall_cond_def', 'location_id,trigger_tag_name', concat(OLD.LOCATION_ID,',',OLD.TRIGGER_TAG_NAME), '', getTableSubSystem('fault_recall_cond_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FAULT_RECALL_COND_DEF ON fault_recall_cond_def;
CREATE TRIGGER TDA_FAULT_RECALL_COND_DEF AFTER DELETE ON fault_recall_cond_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FAULT_RECALL_COND_DEF();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_ACCUML() RETURNS TRIGGER AS $$
	DECLARE flag INT;
BEGIN
    CALL kbdDebugInfo('ACCUML_MAP_TO_FES update','In TI_FES_ACCUML');
    --首先禁止MAP_TO_FES触发器动作
	SET SESSION "disable.AccumlMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesAccumlTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE accuml_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        
    END;
    END IF;
    
	SET SESSION "disable.AccumlMapToFesTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_accuml', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_accuml'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_ACCUML ON fes_accuml;
CREATE TRIGGER TI_FES_ACCUML AFTER INSERT ON fes_accuml FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_ACCUML();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_ACCUML() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    IF OLD.APP_TABLE_NAME=NEW.APP_TABLE_NAME AND OLD.APP_TAG_NAME = NEW.APP_TAG_NAME AND OLD.APP_COLUMN_NAME = NEW.APP_COLUMN_NAME
    AND OLD.RTU_TAG = NEW.RTU_TAG AND OLD.DOT_NO = NEW.DOT_NO
    THEN RETURN NEW;
    END IF;
    
    CALL kbdDebugInfo('ACCUML_MAP_TO_FES update','In TU_FES_ACCUML');
    
	SET SESSION "disable.AccumlMapToFesTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.FesAccumlTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        --删除旧关联
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE accuml_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        --建立新关联
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE accuml_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
    END;
    END IF;
    
	SET SESSION "disable.AccumlMapToFesTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_ACCUML ON fes_accuml;
CREATE TRIGGER TU_FES_ACCUML BEFORE UPDATE ON fes_accuml FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_ACCUML();



CREATE OR REPLACE FUNCTION FUNC_TUA_FES_ACCUML() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_accuml', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_accuml'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FES_ACCUML ON fes_accuml;
CREATE TRIGGER TUA_FES_ACCUML AFTER UPDATE ON fes_accuml FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FES_ACCUML();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_ACCUML() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('ACCUML_MAP_TO_FES update','In TD_FES_ACCUML');
    
	SET SESSION "disable.AccumlMapToFesTrigger" = 1;
	
    SELECT CURRENT_SETTING('disable.FesAccumlTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE accuml_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
    END;
    END IF;
   
	SET SESSION "disable.AccumlMapToFesTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_accuml', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_accuml'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_ACCUML ON fes_accuml;
CREATE TRIGGER TD_FES_ACCUML AFTER DELETE ON fes_accuml FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_ACCUML();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_ANALOG() RETURNS TRIGGER AS $$
	DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('ANALOG_MAP_TO_FES update','In TI_FES_ANALOG');
    --首先禁止MAP_TO_FES触发器动作
	SET SESSION "disable.AnalogMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesAnalogTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
       
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE analog_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
          
    END;
    END IF;
    
	SET SESSION "disable.AnalogMapToFesTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_analog', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_analog'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_ANALOG ON fes_analog;
CREATE TRIGGER TI_FES_ANALOG AFTER INSERT ON fes_analog FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_ANALOG() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    IF OLD.APP_TABLE_NAME=NEW.APP_TABLE_NAME AND OLD.APP_TAG_NAME = NEW.APP_TAG_NAME AND OLD.APP_COLUMN_NAME = NEW.APP_COLUMN_NAME
    AND OLD.RTU_TAG = NEW.RTU_TAG AND OLD.DOT_NO = NEW.DOT_NO
    THEN RETURN NEW;
    END IF;
    
    CALL kbdDebugInfo('ANALOG_MAP_TO_FES update','In TU_FES_ANALOG');
    --首先禁止MAP_TO_FES触发器动作
    SET SESSION "disable.AnalogMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesAnalogTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        
        --不管是ana还是const都是放在analog_map_to_fes表
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE analog_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE analog_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET SESSION "disable.AnalogMapToFesTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_ANALOG ON fes_analog;
CREATE TRIGGER TU_FES_ANALOG BEFORE UPDATE ON fes_analog FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TUA_FES_ANALOG() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_analog', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_analog'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FES_ANALOG ON fes_analog;
CREATE TRIGGER TUA_FES_ANALOG AFTER UPDATE ON fes_analog FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FES_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_ANALOG() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('ANALOG_MAP_TO_FES update','In TD_FES_ANALOG');
    --首先禁止MAP_TO_FES触发器动作
    SET SESSION "disable.AnalogMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesAnalogTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        --不管是ana还是const都是放在analog_map_to_fes表
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE analog_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
    END;
    END IF;
    
    SET SESSION "disable.AnalogMapToFesTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_analog', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_analog'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_ANALOG ON fes_analog;
CREATE TRIGGER TD_FES_ANALOG AFTER DELETE ON fes_analog FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TIA_FES_ANALOG_CTRL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_analog_ctrl', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_analog_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FES_ANALOG_CTRL ON fes_analog_ctrl;
CREATE TRIGGER TIA_FES_ANALOG_CTRL AFTER INSERT ON fes_analog_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FES_ANALOG_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_ANALOG_CTRL() RETURNS TRIGGER AS $$
BEGIN
    --更新关联了此点的遥控点
    
    IF NEW.RTU_TAG <> OLD.RTU_TAG OR NEW.DOT_NO<> OLD.DOT_NO
    THEN 
    BEGIN
        UPDATE analog_control SET
        RTU_TAG = NEW.RTU_TAG,
        OFFSET_NO = NEW.DOT_NO
        WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO = cast(OLD.DOT_NO AS char);
    END; 
    END IF;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_analog_ctrl', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_analog_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_ANALOG_CTRL ON fes_analog_ctrl;
CREATE TRIGGER TU_FES_ANALOG_CTRL AFTER UPDATE ON fes_analog_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_ANALOG_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_ANALOG_CTRL() RETURNS TRIGGER AS $$
BEGIN
    UPDATE analog_control SET
    OFFSET_NO = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO = cast(OLD.DOT_NO AS char);
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_analog_ctrl', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_analog_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_ANALOG_CTRL ON fes_analog_ctrl;
CREATE TRIGGER TD_FES_ANALOG_CTRL AFTER DELETE ON fes_analog_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_ANALOG_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_CHANNEL_PARA() RETURNS TRIGGER AS $$
    DECLARE locationTag varchar(64) DEFAULT '';
    DECLARE subsystemTag varchar(64) DEFAULT '';
BEGIN
    SELECT TAG_NAME INTO locationTag FROM sys_model_location_info WHERE LOCATION_ID = NEW.LOCATION_ID;
    SELECT TAG_NAME INTO subsystemTag FROM sys_model_sub_system_info WHERE SUB_SYSTEM_ID = NEW.SUB_SYSTEM;
    NEW.TAG_NAME = concat(locationTag,'.',subsystemTag,'.',NEW.CHAN_NAME);
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_CHANNEL_PARA ON FES_CHANNEL_PARA;
CREATE TRIGGER TI_FES_CHANNEL_PARA BEFORE INSERT ON FES_CHANNEL_PARA FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_CHANNEL_PARA();



CREATE OR REPLACE FUNCTION FUNC_TIA_FES_CHANNEL_PARA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_channel_para', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_channel_para'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FES_CHANNEL_PARA ON fes_channel_para;
CREATE TRIGGER TIA_FES_CHANNEL_PARA AFTER INSERT ON fes_channel_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FES_CHANNEL_PARA();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_CHANNEL_PARA() RETURNS TRIGGER AS $$
    DECLARE locationTag varchar(64) DEFAULT '';
    DECLARE subsystemTag varchar(64) DEFAULT '';
BEGIN
    IF NEW.CHAN_NAME <> OLD.CHAN_NAME THEN 
    BEGIN 
        SELECT TAG_NAME INTO locationTag FROM sys_model_location_info WHERE LOCATION_ID = NEW.LOCATION_ID;
        SELECT TAG_NAME INTO subsystemTag FROM sys_model_sub_system_info WHERE SUB_SYSTEM_ID = NEW.SUB_SYSTEM;
        NEW.TAG_NAME = concat(locationTag,'.',subsystemTag,'.',NEW.CHAN_NAME);
    END;
    END IF; 
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_CHANNEL_PARA ON fes_channel_para;
CREATE TRIGGER TU_FES_CHANNEL_PARA BEFORE UPDATE ON fes_channel_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_CHANNEL_PARA();



CREATE OR REPLACE FUNCTION FUNC_TUA_FES_CHANNEL_PARA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_channel_para', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_channel_para'));
END IF;
IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.CHAN_NO <> OLD.CHAN_NO OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM THEN
BEGIN
    UPDATE fes_rtu_para SET
    TAG_NAME = concat(NEW.TAG_NAME,'.',RTU_NAME),
    CHAN_TAG = NEW.TAG_NAME,
    CHAN_NO = NEW.CHAN_NO,
    LOCATION_ID = NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM
    WHERE CHAN_TAG = OLD.TAG_NAME;
END;
END IF;
	
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FES_CHANNEL_PARA ON fes_channel_para;
CREATE TRIGGER TUA_FES_CHANNEL_PARA AFTER UPDATE ON fes_channel_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FES_CHANNEL_PARA();



CREATE OR REPLACE FUNCTION FUNC_TDA_FES_CHANNEL_PARA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_channel_para', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_channel_para'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FES_CHANNEL_PARA ON fes_channel_para;
CREATE TRIGGER TDA_FES_CHANNEL_PARA AFTER DELETE ON fes_channel_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FES_CHANNEL_PARA();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_CONST() RETURNS TRIGGER AS $$
BEGIN
      
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_const', 'tag_name', '',NEW.TAG_NAME,  getTableSubSystem('fes_const'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_CONST ON fes_const;
CREATE TRIGGER TI_FES_CONST AFTER INSERT ON fes_const FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_CONST();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_CONST() RETURNS TRIGGER AS $$
BEGIN
      
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_const', 'tag_name', OLD.TAG_NAME,NEW.TAG_NAME,  getTableSubSystem('fes_const'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_CONST ON fes_const;
CREATE TRIGGER TU_FES_CONST AFTER UPDATE ON fes_const FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_CONST();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_CONST() RETURNS TRIGGER AS $$
BEGIN
      
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_const', 'tag_name', OLD.TAG_NAME,'',  getTableSubSystem('fes_const'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_CONST ON fes_const;
CREATE TRIGGER TD_FES_CONST AFTER DELETE ON fes_const FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_CONST();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_CONST_TEMP() RETURNS TRIGGER AS $$
BEGIN
    /*
    INSERT INTO fes_const(
    TAG_NAME,                                                                                              DESCRIPTION,    GROUP_NO,    RTU_TAG,              RTU_NO,            DOT_NO,                                                           SEQ_NO,    SUB_SYSTEM,               LOCATION_ID,           DEV_TAG,              VALUE,    VALUE_TYPE,   UNIT_ID,    VALUE_TEXT,      VALUE_RATIO,   VALUE_ADDED,    VALUE_MAX,     VALUE_MIN,   VALUE_DEFAULT,     VALUE_VERIFY,    EX_INFO,   FES_POINT_TP_NAME,PATH61850,POSITION61850) SELECT   
    concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_CONST'))), NEW.DESCRIPTION,NEW.GROUP_NO,fes_dev_info.RTU_TAG,fes_rtu_para.RTU_NO,getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_CONST')),NEW.SEQ_NO,fes_rtu_para.SUB_SYSTEM,fes_rtu_para.LOCATION_ID,fes_dev_info.TAG_NAME,NEW.VALUE,NEW.VALUE_TYPE,NEW.UNIT_ID,NEW.VALUE_TEXT, NEW.VALUE_RATIO,NEW.VALUE_ADDED,NEW.VALUE_MAX,NEW.VALUE_MIN,NEW.VALUE_DEFAULT,NEW.VALUE_VERIFY,NEW.EX_INFO,NEW.TAG_NAME,    NEW.PATH61850,NEW.POSITION61850
    FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
    */
    IF getTriggerFlag() = TRUE THEN 
        INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
        VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_const_temp', 'tag_name',    '',           NEW.TAG_NAME, getTableSubSystem('fes_const_temp'));
    END IF;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_CONST_TEMP ON fes_const_temp;
CREATE TRIGGER TI_FES_CONST_TEMP AFTER INSERT ON fes_const_temp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_CONST_TEMP();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_CONST_TEMP() RETURNS TRIGGER AS $$
BEGIN
    /*
    UPDATE fes_const SET 
    DESCRIPTION = NEW.DESCRIPTION,
    GROUP_NO = NEW.GROUP_NO,
    SEQ_NO = NEW.SEQ_NO,
    VALUE = NEW.VALUE,
    VALUE_TYPE = NEW.VALUE_TYPE,
    UNIT_ID = NEW.UNIT_ID,
    VALUE_TEXT = NEW.VALUE_TEXT,
    VALUE_RATIO = NEW.VALUE_RATIO,
    VALUE_ADDED = NEW.VALUE_ADDED,
    VALUE_MAX = NEW.VALUE_MAX,
    VALUE_MIN = NEW.VALUE_MIN,
    VALUE_DEFAULT = NEW.VALUE_DEFAULT,
    VALUE_VERIFY = NEW.VALUE_VERIFY,
    EX_INFO = NEW.EX_INFO,
    FES_POINT_TP_NAME = NEW.TAG_NAME,
    PATH61850 = NEW.PATH61850,
    POSITION61850 = NEW.POSITION61850
    WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
    */
        
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_const_temp', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_const_temp'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_CONST_TEMP ON fes_const_temp;
CREATE TRIGGER TU_FES_CONST_TEMP AFTER UPDATE ON fes_const_temp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_CONST_TEMP();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_CONST_TEMP() RETURNS TRIGGER AS $$
BEGIN
    /*
    DELETE FROM fes_const WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
     */   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_const_temp', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_const_temp'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_CONST_TEMP ON fes_const_temp;
CREATE TRIGGER TD_FES_CONST_TEMP AFTER DELETE ON fes_const_temp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_CONST_TEMP();



CREATE OR REPLACE FUNCTION FUNC_TIA_FES_DATA_BLOCK() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,       KEY_COLUMN_NAME,                                                                  KEY_OLD_VALUE,   KEY_NEW_VALUE,                                                                                                                      SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_data_block', 'tag_name,block_id,rtu_no,func_code', '',             concat(NEW.TAG_NAME,',', NEW.BLOCK_ID,',',NEW.RTU_NO,',',NEW.FUNC_CODE), getTableSubSystem('fes_data_block'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FES_DATA_BLOCK ON fes_data_block;
CREATE TRIGGER TIA_FES_DATA_BLOCK AFTER INSERT ON fes_data_block FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FES_DATA_BLOCK();



CREATE OR REPLACE FUNCTION FUNC_TUA_FES_DATA_BLOCK() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                     KEY_OLD_VALUE,                                                            KEY_NEW_VALUE,                                                         SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_data_block', 'tag_name,block_id,rtu_no,func_code', concat(OLD.TAG_NAME,',', OLD.BLOCK_ID,',',OLD.RTU_NO,',',OLD.FUNC_CODE), concat(NEW.TAG_NAME,',',NEW.BLOCK_ID,',',NEW.RTU_NO,',',NEW.FUNC_CODE), getTableSubSystem('fes_data_block'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FES_DATA_BLOCK ON fes_data_block;
CREATE TRIGGER TUA_FES_DATA_BLOCK AFTER UPDATE ON fes_data_block FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FES_DATA_BLOCK();



CREATE OR REPLACE FUNCTION FUNC_TDA_FES_DATA_BLOCK() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                                                                  KEY_OLD_VALUE,         KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_data_block', 'tag_name,block_id,rtu_no,func_code', concat(OLD.TAG_NAME,OLD.BLOCK_ID,',',OLD.RTU_NO,',',OLD.FUNC_CODE), '', getTableSubSystem('fes_data_block'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FES_DATA_BLOCK ON fes_data_block;
CREATE TRIGGER TDA_FES_DATA_BLOCK AFTER DELETE ON fes_data_block FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FES_DATA_BLOCK();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_DATA_BLOCK_TEMP() RETURNS TRIGGER AS $$
BEGIN
    /*
    INSERT INTO fes_data_block(
    TAG_NAME,                                                        BLOCK_ID,     RTU_NO,            FUNC_CODE,    STAR_ADDR,    DATA_LEN,     DATA_FARME_TYPE,     REF_CYCLE,     IS_CREATE_SOE,     PARA_INT1,     PARA_INT2,     DESCRIPTION,    LOCATION_ID,            SUB_SYSTEM,              LD,     CALL_MODE,     DATA_SET,    DATA_SET_TYPE,    DATA_SET_CALL_TIME,    REPORT_CTRL,   FES_POINT_TP_NAME, DEV_TAG) SELECT   
    concat(fes_dev_info.TAG_NAME,'.',getAfterLastDot(NEW.TAG_NAME)), NEW.BLOCK_ID,fes_rtu_para.RTU_NO,NEW.FUNC_CODE,NEW.STAR_ADDR,NEW.DATA_LEN, NEW.DATA_FARME_TYPE, NEW.REF_CYCLE, NEW.IS_CREATE_SOE, NEW.PARA_INT1, NEW.PARA_INT2, NEW.DESCRIPTION,fes_rtu_para.LOCATION_ID,fes_rtu_para.SUB_SYSTEM,NEW.LD, NEW.CALL_MODE, NEW.DATA_SET,NEW.DATA_SET_TYPE,NEW.DATA_SET_CALL_TIME,NEW.REPORT_CTRL,NEW.TAG_NAME,     fes_dev_info.TAG_NAME
    FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
    */
    IF getTriggerFlag() = TRUE THEN 
        INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,           KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
        VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_data_block_temp', 'tag_name',    '',           NEW.TAG_NAME, getTableSubSystem('fes_data_block_temp'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_DATA_BLOCK_TEMP ON fes_data_block_temp;
CREATE TRIGGER TI_FES_DATA_BLOCK_TEMP AFTER INSERT ON fes_data_block_temp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_DATA_BLOCK_TEMP();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_DATA_BLOCK_TEMP() RETURNS TRIGGER AS $$
BEGIN
    /*       
    UPDATE fes_data_block SET 
    TAG_NAME = concat(getBeforeLastDot(fes_data_block.TAG_NAME),'.',getAfterLastDot(NEW.TAG_NAME)),
    BLOCK_ID = NEW.BLOCK_ID,
    FUNC_CODE = NEW.FUNC_CODE,
    STAR_ADDR = NEW.STAR_ADDR,
    DATA_LEN = NEW.DATA_LEN,
    DATA_FARME_TYPE = NEW.DATA_FARME_TYPE,
    REF_CYCLE = NEW.REF_CYCLE,
    IS_CREATE_SOE = NEW.IS_CREATE_SOE,
    PARA_INT1 = NEW.PARA_INT1,
    PARA_INT2 = NEW.PARA_INT2,
    DESCRIPTION = NEW.DESCRIPTION,
    LD = NEW.LD,
    CALL_MODE = NEW.CALL_MODE,
    DATA_SET = NEW.DATA_SET,
    DATA_SET_TYPE = NEW.DATA_SET_TYPE,
    DATA_SET_CALL_TIME = NEW.DATA_SET_CALL_TIME,
    REPORT_CTRL = NEW.REPORT_CTRL,
    FES_POINT_TP_NAME = NEW.TAG_NAME
    WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
    */
        
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_data_block_temp', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_data_block_temp'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_DATA_BLOCK_TEMP ON fes_data_block_temp;
CREATE TRIGGER TU_FES_DATA_BLOCK_TEMP AFTER UPDATE ON fes_data_block_temp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_DATA_BLOCK_TEMP();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_DATA_BLOCK_TEMP() RETURNS TRIGGER AS $$
BEGIN
    /*  
    DELETE FROM fes_data_block WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
      */  
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_data_block_temp', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_data_block_temp'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_DATA_BLOCK_TEMP ON fes_data_block_temp;
CREATE TRIGGER TD_FES_DATA_BLOCK_TEMP AFTER DELETE ON fes_data_block_temp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_DATA_BLOCK_TEMP();



CREATE OR REPLACE FUNCTION FUNC_TIB_FES_DEV_INFO() RETURNS TRIGGER AS $$
BEGIN
    
    NEW.TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DEV_NAME);
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIB_FES_DEV_INFO ON FES_DEV_INFO;
CREATE TRIGGER TIB_FES_DEV_INFO BEFORE INSERT ON FES_DEV_INFO FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIB_FES_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_DEV_INFO() RETURNS TRIGGER AS $$
DECLARE incVarFesDev1 int;
DECLARE incVarFesDev2 int;
BEGIN
  
    IF COALESCE(NEW.DEV_TP_NAME,'') <> '' THEN 
    BEGIN 
        
        --获取最大的点号时已经自动加1,这里减掉
        incVarFesDev1 = getMaxRtuDotNo(NEW.RTU_TAG,getFesPointType('FES_ACC')) - 1;
        incVarFesDev2 = incVarFesDev1;
        
        INSERT INTO fes_accuml(
        TAG_NAME,                                                         LOCATION_ID,              SUB_SYSTEM,              DOT_NO,                               RTU_TAG,    DEV_TAG,      SEQ_NO,                   APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, BASE,                      COEFF,                         RTU_NO,              RES_PARA_INT1,                     RES_PARA_INT2,                    RES_PARA_INT3,                    RES_PARA_INT4,                    DESCRIPTION,                       FES_POINT_TP_NAME,          PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8) SELECT   
        concat(NEW.RTU_TAG, '.' , row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev1),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM ,
		row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+ incVarFesDev2, NEW.RTU_TAG,NEW.TAG_NAME,fes_dev_pt_temp_def.SEQ_NO,'',             '',            '',            fes_dev_pt_temp_def.REVISE,  fes_dev_pt_temp_def.RATIO,    fes_rtu_para.RTU_NO , fes_dev_pt_temp_def.RES_PARA_INT1, fes_dev_pt_temp_def.RES_PARA_INT2,fes_dev_pt_temp_def.RES_PARA_INT3,fes_dev_pt_temp_def.RES_PARA_INT4,fes_dev_pt_temp_def.DESCRIPTION , fes_dev_pt_temp_def.TAG_NAME,PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8                             
        FROM fes_dev_pt_temp_def, fes_rtu_para WHERE fes_dev_pt_temp_def.DEV_TP_NAME=NEW.DEV_TP_NAME AND fes_dev_pt_temp_def.POINT_TYPE=getFesPointType('FES_ACC') AND fes_rtu_para.TAG_NAME=NEW.RTU_TAG; 
        
        incVarFesDev1 = getMaxRtuDotNo(NEW.RTU_TAG,getFesPointType('FES_ANA')) - 1;
        incVarFesDev2 = incVarFesDev1;
        
        INSERT INTO fes_analog(
        TAG_NAME,                                                          LOCATION_ID,              SUB_SYSTEM,               DEV_TAG,     SEQ_NO,                     RTU_TAG,     RTU_NO,                DOT_NO,                             APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME,   IS_FILTER,                            PERCENT,                        DEADBAND_TYPE,    DEADBAND,  ZEROBAND,  BASE,                         COEFF,                         MAX_RANGE, MIN_RANGE,   RES_PARA_INT1,                     RES_PARA_INT2,                    RES_PARA_INT3,                    RES_PARA_INT4,                     DESCRIPTION,                       FES_POINT_TP_NAME ,         PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8) SELECT   
        concat(NEW.RTU_TAG, '.' , row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev1),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , 
		NEW.TAG_NAME,fes_dev_pt_temp_def.SEQ_NO, NEW.RTU_TAG,  fes_rtu_para.RTU_NO ,  row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev2, '',           '',            '',               fes_dev_pt_temp_def.IS_FILTER,       fes_dev_pt_temp_def.PERCENT,     '0',                '0',       '0',     fes_dev_pt_temp_def.REVISE,  fes_dev_pt_temp_def.RATIO,     '99999999',    '-32767',    fes_dev_pt_temp_def.RES_PARA_INT1, fes_dev_pt_temp_def.RES_PARA_INT2,fes_dev_pt_temp_def.RES_PARA_INT3,fes_dev_pt_temp_def.RES_PARA_INT4, fes_dev_pt_temp_def.DESCRIPTION , fes_dev_pt_temp_def.TAG_NAME,PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8                             
        FROM fes_dev_pt_temp_def, fes_rtu_para WHERE fes_dev_pt_temp_def.DEV_TP_NAME=NEW.DEV_TP_NAME AND fes_dev_pt_temp_def.POINT_TYPE=getFesPointType('FES_ANA') 
		AND fes_rtu_para.TAG_NAME=NEW.RTU_TAG; 
        
        incVarFesDev1 = getMaxRtuDotNo(NEW.RTU_TAG,getFesPointType('FES_DIG')) - 1;
        incVarFesDev2 = incVarFesDev1;
        
        INSERT INTO fes_digital(
        TAG_NAME,                                                         LOCATION_ID,              SUB_SYSTEM,               DEV_TAG,      SEQ_NO,                    RTU_TAG,     DOT_NO,                               APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, FILTER_ERR,                            FILTER_DISTURB,                                DISTURB_TIME,                               REVERS,                         RTU_NO,         RES_PARA_INT1,                     RES_PARA_INT2,                    RES_PARA_INT3,                    RES_PARA_INT4,                      DESCRIPTION,                       FES_POINT_TP_NAME,          PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8,ATTR,ASS_DIG) SELECT   
        concat(NEW.RTU_TAG, '.' , row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev1),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , 
		NEW.TAG_NAME,fes_dev_pt_temp_def.SEQ_NO, NEW.RTU_TAG, row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev2 ,        '',         '',            '',          fes_dev_pt_temp_def.FILTER_ERR,        fes_dev_pt_temp_def.FILTER_DISTURB,            fes_dev_pt_temp_def.DISTURB_TIME,           fes_dev_pt_temp_def.REVERS,     fes_rtu_para.RTU_NO , fes_dev_pt_temp_def.RES_PARA_INT1, fes_dev_pt_temp_def.RES_PARA_INT2,fes_dev_pt_temp_def.RES_PARA_INT3,fes_dev_pt_temp_def.RES_PARA_INT4,  fes_dev_pt_temp_def.DESCRIPTION , fes_dev_pt_temp_def.TAG_NAME,PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8,ATTR,ASS_DIG                             
        FROM fes_dev_pt_temp_def, fes_rtu_para WHERE fes_dev_pt_temp_def.DEV_TP_NAME=NEW.DEV_TP_NAME AND fes_dev_pt_temp_def.POINT_TYPE=getFesPointType('FES_DIG') 
		AND fes_rtu_para.TAG_NAME=NEW.RTU_TAG ; 
        
        incVarFesDev1 = getMaxRtuDotNo(NEW.RTU_TAG,getFesPointType('FES_MIX')) - 1;
        incVarFesDev2 = incVarFesDev1;
        
        INSERT INTO fes_mix(
        TAG_NAME,                                                       DESCRIPTION,                    LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,     RTU_NO,              DOT_NO,                               DEV_TAG,     SEQ_NO,                     APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, BASE,                        COEFF,                       MAX_RANGE, MIN_RANGE,  RES_PARA_INT1,                     RES_PARA_INT2,                    RES_PARA_INT3,                    RES_PARA_INT4,                     FES_POINT_TP_NAME,          PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8 )SELECT
        concat(NEW.RTU_TAG, '.' , row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev1),fes_dev_pt_temp_def.DESCRIPTION,fes_rtu_para.LOCATION_ID ,
		fes_rtu_para.SUB_SYSTEM , NEW.RTU_TAG,fes_rtu_para.RTU_NO, row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev2 ,  NEW.TAG_NAME,fes_dev_pt_temp_def.SEQ_NO, '',            '',            '',             fes_dev_pt_temp_def.REVISE,   fes_dev_pt_temp_def.RATIO,  '99999999',   '-32768',          fes_dev_pt_temp_def.RES_PARA_INT1, fes_dev_pt_temp_def.RES_PARA_INT2,fes_dev_pt_temp_def.RES_PARA_INT3,fes_dev_pt_temp_def.RES_PARA_INT4, fes_dev_pt_temp_def.TAG_NAME,PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8
        FROM fes_dev_pt_temp_def, fes_rtu_para WHERE fes_dev_pt_temp_def.DEV_TP_NAME=NEW.DEV_TP_NAME 
		AND fes_dev_pt_temp_def.POINT_TYPE=getFesPointType('FES_MIX') AND fes_rtu_para.TAG_NAME=NEW.RTU_TAG ; 
        
        incVarFesDev1 = getMaxRtuDotNo(NEW.RTU_TAG,getFesPointType('FES_ANA_CTRL')) - 1;
        incVarFesDev2 = incVarFesDev1;
        
        INSERT INTO fes_analog_ctrl(
        TAG_NAME,                                                       LOCATION_ID,              SUB_SYSTEM,              BASE,                        COEFF,                       MAX_RANGE, MIN_RANGE, RTU_NO,             DEV_TAG,      DOT_NO,                               RES_PARA_INT1,                     RES_PARA_INT2,                    RES_PARA_INT3,                    RES_PARA_INT4,                     SEQ_NO,                    RTU_TAG,    DESCRIPTION,                      POINT_TP_NAME,             PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8)SELECT
        concat(NEW.RTU_TAG, '.' , row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev1),fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM ,
		fes_dev_pt_temp_def.REVISE,  fes_dev_pt_temp_def.RATIO,   '99999999',   '-32768',       fes_rtu_para.RTU_NO,NEW.TAG_NAME, 
		row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev2,fes_dev_pt_temp_def.RES_PARA_INT1, fes_dev_pt_temp_def.RES_PARA_INT2,fes_dev_pt_temp_def.RES_PARA_INT3,fes_dev_pt_temp_def.RES_PARA_INT4, fes_dev_pt_temp_def.SEQ_NO,NEW.RTU_TAG,fes_dev_pt_temp_def.DESCRIPTION, fes_dev_pt_temp_def.TAG_NAME,PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8
        FROM fes_dev_pt_temp_def, fes_rtu_para WHERE fes_dev_pt_temp_def.DEV_TP_NAME=NEW.DEV_TP_NAME AND fes_dev_pt_temp_def.POINT_TYPE=getFesPointType('FES_ANA_CTRL') 
		AND fes_rtu_para.TAG_NAME=NEW.RTU_TAG ; 
        
        incVarFesDev1 = getMaxRtuDotNo(NEW.RTU_TAG,getFesPointType('FES_DIG_CTRL')) - 1;
        incVarFesDev2 = incVarFesDev1;
       
        INSERT INTO fes_digital_ctrl(
        TAG_NAME,                                                       LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,    RTU_NO,              DOT_NO,                               DEV_TAG,     DESCRIPTION,                     REVERS,                        POINT_TP_NAME,               SEQ_NO,                    RES_PARA_INT1,                     RES_PARA_INT2,                    RES_PARA_INT3,                    RES_PARA_INT4,                   PATH61850,POSITION61850, YK_SELECT61850, YK_EXE61850, YK_CANCLE61850, YK_DOUBLE_SELECT61850, YK_DOUBLE_EXE61850, YK_DOUBLE_CANCLE61850, PULSE_TIME,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8,ATTR,CTRL_TYPE)SELECT
        concat(NEW.RTU_TAG, '.' , row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev1),fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , NEW.RTU_TAG,
		fes_rtu_para.RTU_NO, row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev2, NEW.TAG_NAME,fes_dev_pt_temp_def.DESCRIPTION, fes_dev_pt_temp_def.REVERS,    fes_dev_pt_temp_def.TAG_NAME,fes_dev_pt_temp_def.SEQ_NO, fes_dev_pt_temp_def.RES_PARA_INT1, fes_dev_pt_temp_def.RES_PARA_INT2,fes_dev_pt_temp_def.RES_PARA_INT3,fes_dev_pt_temp_def.RES_PARA_INT4,PATH61850,POSITION61850,YK_SELECT61850, YK_EXE61850, YK_CANCLE61850, YK_DOUBLE_SELECT61850, YK_DOUBLE_EXE61850, YK_DOUBLE_CANCLE61850, PULSE_TIME,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8,ATTR,CTRL_TYPE  
        FROM fes_dev_pt_temp_def, fes_rtu_para WHERE fes_dev_pt_temp_def.DEV_TP_NAME=NEW.DEV_TP_NAME AND fes_dev_pt_temp_def.POINT_TYPE=getFesPointType('FES_DIG_CTRL') 
		AND fes_rtu_para.TAG_NAME=NEW.RTU_TAG ; 
        
        incVarFesDev1 = getMaxRtuDotNo(NEW.RTU_TAG,getFesPointType('FES_MIX_CTRL')) - 1;
        incVarFesDev2 = incVarFesDev1;
        
        INSERT INTO fes_mix_ctrl(
        TAG_NAME,                                                       LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,    RTU_NO,             BASE, DESCRIPTION,                      DOT_NO,                             COEFF,                       MIN_RANGE, MAX_RANGE, RES_PARA_INT1,                     RES_PARA_INT2,                    RES_PARA_INT3,                    RES_PARA_INT4,                    SEQ_NO,                     POINT_TP_NAME,               DEV_TAG,     PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8  )SELECT
        concat(NEW.RTU_TAG, '.' , row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev1),fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , 
		NEW.RTU_TAG,fes_rtu_para.RTU_NO,fes_dev_pt_temp_def.REVISE,  fes_dev_pt_temp_def.DESCRIPTION, 
		row_number() over (ORDER BY fes_dev_pt_temp_def.SEQ_NO)+incVarFesDev2,fes_dev_pt_temp_def.RATIO,   '-32768',      '99999999',     fes_dev_pt_temp_def.RES_PARA_INT1, fes_dev_pt_temp_def.RES_PARA_INT2,fes_dev_pt_temp_def.RES_PARA_INT3,fes_dev_pt_temp_def.RES_PARA_INT4,fes_dev_pt_temp_def.SEQ_NO, fes_dev_pt_temp_def.TAG_NAME,NEW.TAG_NAME,PATH61850,POSITION61850,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8
        FROM fes_dev_pt_temp_def, fes_rtu_para WHERE fes_dev_pt_temp_def.DEV_TP_NAME=NEW.DEV_TP_NAME 
		AND fes_dev_pt_temp_def.POINT_TYPE=getFesPointType('FES_MIX_CTRL') AND fes_rtu_para.TAG_NAME=NEW.RTU_TAG ; 
        
        
        INSERT INTO fes_data_block(
        TAG_NAME,                                                               BLOCK_ID,                    RTU_TAG,              RTU_NO,             FUNC_CODE,                    STAR_ADDR,                    DATA_LEN,                     DATA_FARME_TYPE,                    REF_CYCLE,                    IS_CREATE_SOE,                    PARA_INT1,                      PARA_INT2,                   DESCRIPTION,                      LOCATION_ID,            SUB_SYSTEM,              LD,                    CALL_MODE,                    DATA_SET,                DATA_SET_TYPE,                    DATA_SET_CALL_TIME,                     REPORT_CTRL,                     FES_POINT_TP_NAME,            DEV_TAG,   PARA_INT3,PARA_INT4,IS_ENABLE) SELECT   
        concat(NEW.TAG_NAME,'.',getAfterLastDot(fes_data_block_temp.TAG_NAME)), fes_data_block_temp.BLOCK_ID,fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO,fes_data_block_temp.FUNC_CODE,fes_data_block_temp.STAR_ADDR,fes_data_block_temp.DATA_LEN, fes_data_block_temp.DATA_FARME_TYPE,fes_data_block_temp.REF_CYCLE,fes_data_block_temp.IS_CREATE_SOE,fes_data_block_temp.PARA_INT1, fes_data_block_temp.PARA_INT2, fes_data_block_temp.DESCRIPTION,fes_rtu_para.LOCATION_ID,fes_rtu_para.SUB_SYSTEM,fes_data_block_temp.LD, fes_data_block_temp.CALL_MODE, fes_data_block_temp.DATA_SET, fes_data_block_temp.DATA_SET_TYPE,fes_data_block_temp.DATA_SET_CALL_TIME, fes_data_block_temp.REPORT_CTRL,fes_data_block_temp.TAG_NAME, NEW.TAG_NAME,PARA_INT3,PARA_INT4,IS_ENABLE
        FROM fes_data_block_temp, fes_rtu_para WHERE fes_data_block_temp.DEV_TP_NAME=NEW.DEV_TP_NAME  AND fes_rtu_para.TAG_NAME=NEW.RTU_TAG ORDER BY fes_data_block_temp.BLOCK_ID; 
    
        incVarFesDev1 = getMaxRtuDotNo(NEW.RTU_TAG,getFesPointType('FES_CONST')) - 1;
        incVarFesDev2 = incVarFesDev1;
        
        INSERT INTO fes_const(
        TAG_NAME,                                                        DESCRIPTION,               GROUP_NO,                RTU_TAG,     RTU_NO,            DOT_NO,                               SEQ_NO,            SUB_SYSTEM,        LOCATION_ID,  DEV_TAG,     VALUE,              VALUE_TYPE,               UNIT_ID,                VALUE_TEXT,                VALUE_RATIO,             VALUE_ADDED,                 VALUE_MAX,               VALUE_MIN,              VALUE_DEFAULT,                VALUE_VERIFY,              EX_INFO,               FES_POINT_TP_NAME,      PATH61850, POSITION61850,RES_PARA_INT1,               RES_PARA_INT2,               RES_PARA_INT3,               RES_PARA_INT4,               RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8,DZ_SEQ) SELECT   
        concat(NEW.RTU_TAG, '.' , row_number() over (ORDER BY fes_const_temp.SEQ_NO)+incVarFesDev1), fes_const_temp.DESCRIPTION,fes_const_temp.GROUP_NO,NEW.RTU_TAG,
		fes_rtu_para.RTU_NO,row_number() over (ORDER BY fes_const_temp.SEQ_NO)+incVarFesDev2,fes_const_temp.SEQ_NO,NEW.SUB_SYSTEM,NEW.LOCATION_ID,NEW.TAG_NAME,fes_const_temp.VALUE,fes_const_temp.VALUE_TYPE,fes_const_temp.UNIT_ID,fes_const_temp.VALUE_TEXT, fes_const_temp.VALUE_RATIO,fes_const_temp.VALUE_ADDED,fes_const_temp.VALUE_MAX,fes_const_temp.VALUE_MIN,fes_const_temp.VALUE_DEFAULT,fes_const_temp.VALUE_VERIFY,fes_const_temp.EX_INFO,fes_const_temp.TAG_NAME,PATH61850, POSITION61850,fes_const_temp.RES_PARA_INT1,fes_const_temp.RES_PARA_INT2,fes_const_temp.RES_PARA_INT3,fes_const_temp.RES_PARA_INT4,RES_PARA_INT5,RES_PARA_INT6,RES_PARA_INT7,RES_PARA_INT8,DZ_SEQ
        FROM fes_const_temp, fes_rtu_para WHERE fes_const_temp.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=NEW.RTU_TAG ; 
        
    END; 
    END IF;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_dev_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_dev_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_DEV_INFO ON fes_dev_info;
CREATE TRIGGER TI_FES_DEV_INFO AFTER INSERT ON fes_dev_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUB_FES_DEV_INFO() RETURNS TRIGGER AS $$
BEGIN
    
    IF NEW.DEV_NAME <> OLD.DEV_NAME THEN 
        NEW.TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DEV_NAME);
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUB_FES_DEV_INFO ON FES_DEV_INFO;
CREATE TRIGGER TUB_FES_DEV_INFO BEFORE UPDATE ON FES_DEV_INFO FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUB_FES_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_DEV_INFO() RETURNS TRIGGER AS $$
BEGIN
        IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.RTU_TAG <> OLD.RTU_TAG OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM
        OR NEW.RTU_NO <> OLD.RTU_NO
        THEN 
        BEGIN 
        
    update fes_accuml
    SET fes_accuml.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_accuml.DOT_NO), fes_accuml.LOCATION_ID = fes_rtu_para.LOCATION_ID, fes_accuml.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM, fes_accuml.RTU_TAG = NEW.RTU_TAG, fes_accuml.DEV_TAG = NEW.TAG_NAME, fes_accuml.RTU_NO = fes_rtu_para.RTU_NO
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_accuml.DEV_TAG = OLD.TAG_NAME;

            
            
    update fes_analog
    SET fes_analog.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_analog.DOT_NO), fes_analog.LOCATION_ID = fes_rtu_para.LOCATION_ID, fes_analog.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM, fes_analog.RTU_TAG = NEW.RTU_TAG, fes_analog.DEV_TAG = NEW.TAG_NAME, fes_analog.RTU_NO = fes_rtu_para.RTU_NO
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_analog.DEV_TAG = OLD.TAG_NAME;

            
            
    update fes_digital
    SET fes_digital.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_digital.DOT_NO ), fes_digital.LOCATION_ID = fes_rtu_para.LOCATION_ID, fes_digital.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM, fes_digital.RTU_TAG = NEW.RTU_TAG, fes_digital.DEV_TAG = NEW.TAG_NAME, fes_digital.RTU_NO = fes_rtu_para.RTU_NO
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_digital.DEV_TAG = OLD.TAG_NAME;

            
            
    update fes_mix
    SET fes_mix.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_mix.DOT_NO), fes_mix.LOCATION_ID = fes_rtu_para.LOCATION_ID, fes_mix.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM, fes_mix.RTU_TAG = NEW.RTU_TAG, fes_mix.DEV_TAG = NEW.TAG_NAME, fes_mix.RTU_NO = fes_rtu_para.RTU_NO
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_mix.DEV_TAG = OLD.TAG_NAME;

            
            
    update fes_analog_ctrl
    SET fes_analog_ctrl.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_analog_ctrl.DOT_NO), fes_analog_ctrl.LOCATION_ID = fes_rtu_para.LOCATION_ID, fes_analog_ctrl.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM, fes_analog_ctrl.RTU_TAG = NEW.RTU_TAG, fes_analog_ctrl.DEV_TAG = NEW.TAG_NAME, fes_analog_ctrl.RTU_NO = fes_rtu_para.RTU_NO
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_analog_ctrl.DEV_TAG = OLD.TAG_NAME;

            
            
    update fes_digital_ctrl
    SET fes_digital_ctrl.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_digital_ctrl.DOT_NO), fes_digital_ctrl.LOCATION_ID = fes_rtu_para.LOCATION_ID, fes_digital_ctrl.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM, fes_digital_ctrl.RTU_TAG = NEW.RTU_TAG, fes_digital_ctrl.DEV_TAG = NEW.TAG_NAME, fes_digital_ctrl.RTU_NO = fes_rtu_para.RTU_NO
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_digital_ctrl.DEV_TAG = OLD.TAG_NAME;

            
            
    update fes_mix_ctrl
    SET fes_mix_ctrl.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_mix_ctrl.DOT_NO), fes_mix_ctrl.LOCATION_ID = fes_rtu_para.LOCATION_ID, fes_mix_ctrl.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM, fes_mix_ctrl.RTU_TAG = NEW.RTU_TAG, fes_mix_ctrl.DEV_TAG = NEW.TAG_NAME, fes_mix_ctrl.RTU_NO = fes_rtu_para.RTU_NO
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_mix_ctrl.DEV_TAG = OLD.TAG_NAME;

            
    update fes_data_block
    SET fes_data_block.LOCATION_ID = NEW.LOCATION_ID, fes_data_block.SUB_SYSTEM = NEW.SUB_SYSTEM, fes_data_block.DEV_TAG = NEW.TAG_NAME, fes_data_block.RTU_TAG = NEW.RTU_TAG, fes_data_block.TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(fes_data_block.TAG_NAME)), fes_data_block.RTU_NO = fes_rtu_para.RTU_NO
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_data_block.DEV_TAG = OLD.TAG_NAME;

            
    update fes_const
    SET fes_const.LOCATION_ID = NEW.LOCATION_ID, fes_const.SUB_SYSTEM = NEW.SUB_SYSTEM, fes_const.DEV_TAG = NEW.TAG_NAME, fes_const.RTU_TAG = NEW.RTU_TAG, fes_const.RTU_NO = fes_rtu_para.RTU_NO, fes_const.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_const.DOT_NO)
    from fes_rtu_para
    WHERE fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_const.DEV_TAG = OLD.TAG_NAME;

            
            
        END;
        END IF;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_dev_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_dev_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_DEV_INFO ON fes_dev_info;
CREATE TRIGGER TU_FES_DEV_INFO AFTER UPDATE ON fes_dev_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_DEV_INFO() RETURNS TRIGGER AS $$
BEGIN
DELETE FROM fes_accuml WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND  DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_analog WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND  DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_digital WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_mix WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_analog_ctrl WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_digital_ctrl WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_mix_ctrl WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_data_block WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_const WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_DEV_INFO ON fes_dev_info;
CREATE TRIGGER TD_FES_DEV_INFO BEFORE DELETE ON fes_dev_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_FES_DEV_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_dev_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_dev_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FES_DEV_INFO ON fes_dev_info;
CREATE TRIGGER TDA_FES_DEV_INFO AFTER DELETE ON fes_dev_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FES_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_DEV_PT_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
        
        /*
        IF NEW.POINT_TYPE = getFesPointType('FES_ACC') THEN 
            INSERT INTO fes_accuml(
            TAG_NAME,                                                                                              LOCATION_ID,             SUB_SYSTEM,               DOT_NO,                                                         RTU_TAG,              DEV_TAG,              SEQ_NO,    APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, BASE, COEFF,  RTU_NO,              RES_PARA_INT1, RES_PARA_INT2, RES_PARA_INT3, RES_PARA_INT4, DESCRIPTION,    FES_POINT_TP_NAME,PATH61850,POSITION61850) SELECT   
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ACC'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM ,getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ACC')),  fes_dev_info.RTU_TAG,fes_dev_info.TAG_NAME,NEW.SEQ_NO,'',              '',           '',             '0',   '1',   fes_rtu_para.RTU_NO ,NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4, NEW.DESCRIPTION ,NEW.TAG_NAME    ,NEW.PATH61850,NEW.POSITION61850                        
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        IF NEW.POINT_TYPE = getFesPointType('FES_ANA') THEN
            INSERT INTO fes_analog(
            TAG_NAME,                                                            LOCATION_ID,              SUB_SYSTEM,               DEV_TAG,              SEQ_NO,     RTU_TAG,             RTU_NO,                                                 DOT_NO,                                                         APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, IS_FILTER, PERCENT, DEADBAND_TYPE, DEADBAND, ZEROBAND, BASE, COEFF, MAX_RANGE, MIN_RANGE,  RES_PARA_INT1,     RES_PARA_INT2,    RES_PARA_INT3,      RES_PARA_INT4, DESCRIPTION,      FES_POINT_TP_NAME,    PATH61850,    POSITION61850) SELECT   
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ANA'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_dev_info.TAG_NAME,NEW.SEQ_NO, fes_dev_info.RTU_TAG,fes_rtu_para.RTU_NO ,  getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ANA')),'',           '',         '',                   0,         0,      '0',           '0',      '0',     '0',   '1',  65536,      0,          NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4, NEW.DESCRIPTION , NEW.TAG_NAME     ,NEW.PATH61850,NEW.POSITION61850                       
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF; 
        
        IF NEW.POINT_TYPE = getFesPointType('FES_DIG') THEN
            INSERT INTO fes_digital(
            TAG_NAME,                                                                                              LOCATION_ID,                    SUB_SYSTEM,                     DEV_TAG, SEQ_NO,     RTU_TAG,             DOT_NO,                                                           APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, FILTER_ERR, FILTER_DISTURB, DISTURB_TIME,  REVERS,  RTU_NO,               RES_PARA_INT1, RES_PARA_INT2, RES_PARA_INT3, RES_PARA_INT4, DESCRIPTION,    FES_POINT_TP_NAME,PATH61850,POSITION61850) SELECT   
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_DIG'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_dev_info.TAG_NAME,NEW.SEQ_NO, fes_dev_info.RTU_TAG,getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_DIG')),   '',            '',           '',              0,          0,              0,             0,       fes_rtu_para.RTU_NO , NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4, NEW.DESCRIPTION , NEW.TAG_NAME   ,NEW.PATH61850,NEW.POSITION61850                          
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_MIX') THEN
            INSERT INTO fes_mix(
            TAG_NAME,                                                                                             DESCRIPTION,      LOCATION_ID,             SUB_SYSTEM,               RTU_TAG,              RTU_NO,                                               DOT_NO,                         DEV_TAG,               SEQ_NO,     APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME,    BASE, COEFF, MAX_RANGE, MIN_RANGE,  RES_PARA_INT1, RES_PARA_INT2, RES_PARA_INT3, RES_PARA_INT4, FES_POINT_TP_NAME,PATH61850,POSITION61850) SELECT   
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_MIX'))),  NEW.DESCRIPTION ,fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO, getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_MIX')), fes_dev_info.TAG_NAME, NEW.SEQ_NO, '',             '',           '',                 '0',  '1',   65536,      0,         NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4,  NEW.TAG_NAME    ,NEW.PATH61850,NEW.POSITION61850                         
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_ANA_CTRL') THEN
            INSERT INTO fes_analog_ctrl(
            TAG_NAME,                                                                                                DESCRIPTION,    LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,              RTU_NO,              DOT_NO,                                                                DEV_TAG,              SEQ_NO,     BASE, COEFF, MAX_RANGE, MIN_RANGE, POINT_TP_NAME,RES_PARA_INT1,RES_PARA_INT2,RES_PARA_INT3,RES_PARA_INT4,PATH61850,POSITION61850)SELECT
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ANA_CTRL'))),NEW.DESCRIPTION,fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO, getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ANA_CTRL')),  fes_dev_info.TAG_NAME,NEW.SEQ_NO, '0',  '1',    65536,     0,        NEW.TAG_NAME ,NEW.RES_PARA_INT1,NEW.RES_PARA_INT2,NEW.RES_PARA_INT3,NEW.RES_PARA_INT4,NEW.PATH61850,NEW.POSITION61850
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_DIG_CTRL') THEN
            INSERT INTO fes_digital_ctrl(
            TAG_NAME,                                                                                                DESCRIPTION,                                                       LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,              RTU_NO,              DOT_NO,             DEV_TAG,              SEQ_NO,     REVERS, POINT_TP_NAME,RES_PARA_INT1,     RES_PARA_INT2,     RES_PARA_INT3,     RES_PARA_INT4,    PATH61850,    POSITION61850,    YK_SELECT61850,     YK_EXE61850,     YK_CANCLE61850,     YK_DOUBLE_SELECT61850,     YK_DOUBLE_EXE61850,     YK_DOUBLE_CANCLE61850,     PULSE_TIME)SELECT
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_DIG_CTRL'))),NEW.DESCRIPTION,fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO, getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_DIG_CTRL')), fes_dev_info.TAG_NAME,NEW.SEQ_NO,  0,      NEW.TAG_NAME, NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4,NEW.PATH61850,NEW.POSITION61850,NEW.YK_SELECT61850, NEW.YK_EXE61850, NEW.YK_CANCLE61850, NEW.YK_DOUBLE_SELECT61850, NEW.YK_DOUBLE_EXE61850, NEW.YK_DOUBLE_CANCLE61850, NEW.PULSE_TIME
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_MIX_CTRL') THEN
            INSERT INTO fes_mix_ctrl(
            TAG_NAME,                                                                                                DESCRIPTION,                                                       LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,              RTU_NO,              DOT_NO,             DEV_TAG,              SEQ_NO,    BASE, COEFF, MIN_RANGE, MAX_RANGE, POINT_TP_NAME,RES_PARA_INT1, RES_PARA_INT2, RES_PARA_INT3, RES_PARA_INT4,PATH61850,POSITION61850)SELECT
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_MIX_CTRL'))),NEW.DESCRIPTION,fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO, getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_MIX_CTRL')),  fes_dev_info.TAG_NAME,NEW.SEQ_NO,'0',  '1',    0,       65536,      NEW.TAG_NAME, NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4,NEW.PATH61850,NEW.POSITION61850
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        */
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_dev_pt_temp_def', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_dev_pt_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_DEV_PT_TEMP_DEF ON fes_dev_pt_temp_def;
CREATE TRIGGER TI_FES_DEV_PT_TEMP_DEF AFTER INSERT ON fes_dev_pt_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_DEV_PT_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_DEV_PT_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
        
        /*
        IF NEW.POINT_TYPE = getFesPointType('FES_ACC') THEN 
            UPDATE fes_accuml SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            FES_POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        IF NEW.POINT_TYPE = getFesPointType('FES_ANA') THEN
            UPDATE fes_analog SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            FES_POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
        END IF; 
        
        IF NEW.POINT_TYPE = getFesPointType('FES_DIG') THEN
            UPDATE fes_digital SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            FES_POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_MIX') THEN
            UPDATE fes_mix SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            FES_POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_ANA_CTRL') THEN
            UPDATE fes_analog_ctrl SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_DIG_CTRL') THEN
            UPDATE fes_digital_ctrl SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850,
            YK_SELECT61850 = NEW.YK_SELECT61850,
            YK_EXE61850 = NEW.YK_EXE61850,
            YK_CANCLE61850 = NEW.YK_CANCLE61850,
            YK_DOUBLE_SELECT61850 = NEW.YK_DOUBLE_SELECT61850,
            YK_DOUBLE_EXE61850 = NEW.YK_DOUBLE_EXE61850,
            YK_DOUBLE_CANCLE61850 = NEW.YK_DOUBLE_CANCLE61850,
            PULSE_TIME = NEW.PULSE_TIME
            WHERE POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_MIX_CTRL') THEN
            UPDATE fes_mix_ctrl SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        */
    --更新套件内容
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
    BEGIN
        IF NEW.POINT_TYPE = getFesPointType('FES_ACC') THEN
          UPDATE temp_plugin_point_map_acc SET FES_TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME) 
          WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF NEW.POINT_TYPE = getFesPointType('FES_ANA') THEN
           UPDATE temp_plugin_point_map_ana SET FES_TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME) 
           WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF  NEW.POINT_TYPE = getFesPointType('FES_MIX') THEN
           UPDATE temp_plugin_point_map_mix SET FES_TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME) 
           WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF  NEW.POINT_TYPE = getFesPointType('FES_DIG') THEN
           UPDATE temp_plugin_point_map_dig SET 
           FES_TEMP_POINT_NAME1 = IF(FES_TEMP_POINT_NAME1 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME1),
           FES_TEMP_POINT_NAME2 = IF(FES_TEMP_POINT_NAME2 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME2),
           FES_TEMP_POINT_NAME3 = IF(FES_TEMP_POINT_NAME3 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME3),
           FES_TEMP_POINT_NAME4 = IF(FES_TEMP_POINT_NAME4 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME4),
           FES_TEMP_POINT_NAME5 = IF(FES_TEMP_POINT_NAME5 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME5)
           WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME;
        ELSEIF NEW.POINT_TYPE = getFesPointType('FES_ANA_CTRL') THEN
           UPDATE temp_plugin_point_map_ana SET FES_TEMP_OUT_NAME = getAfterLastDot(NEW.TAG_NAME)
           WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_OUT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF NEW.POINT_TYPE = getFesPointType('FES_MIX_CTRL') THEN
            UPDATE temp_plugin_point_map_mix SET FES_TEMP_OUT_NAME = getAfterLastDot(NEW.TAG_NAME)
            WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_OUT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF NEW.POINT_TYPE = getFesPointType('FES_DIG_CTRL') THEN
            UPDATE temp_plugin_point_map_dig SET
            FES_TEMP_OUT_NAME1 = IF(FES_TEMP_OUT_NAME1 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME1),
            FES_TEMP_OUT_NAME2 = IF(FES_TEMP_OUT_NAME2 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME2),
            FES_TEMP_OUT_NAME3 = IF(FES_TEMP_OUT_NAME3 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME3),
            FES_TEMP_OUT_NAME4 = IF(FES_TEMP_OUT_NAME4 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME4),
            FES_TEMP_OUT_NAME5 = IF(FES_TEMP_OUT_NAME5 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME5)
            WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME;
         END IF;
    END; 
    END IF;         
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_dev_pt_temp_def', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_dev_pt_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_DEV_PT_TEMP_DEF ON fes_dev_pt_temp_def;
CREATE TRIGGER TU_FES_DEV_PT_TEMP_DEF AFTER UPDATE ON fes_dev_pt_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_DEV_PT_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_DEV_PT_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
        
        /*
        IF OLD.POINT_TYPE = getFesPointType('FES_ACC') THEN 
            DELETE FROM fes_accuml WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
        IF OLD.POINT_TYPE = getFesPointType('FES_ANA') THEN
            DELETE FROM fes_analog WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
        END IF; 
        
        IF OLD.POINT_TYPE = getFesPointType('FES_DIG') THEN
            DELETE FROM fes_digital WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
        
        IF OLD.POINT_TYPE = getFesPointType('FES_MIX') THEN
            DELETE FROM fes_mix WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
        
        IF OLD.POINT_TYPE = getFesPointType('FES_ANA_CTRL') THEN
            DELETE FROM fes_analog_ctrl WHERE POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
         
        IF OLD.POINT_TYPE = getFesPointType('FES_DIG_CTRL') THEN
            DELETE FROM fes_digital_ctrl WHERE POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
        
        IF OLD.POINT_TYPE = getFesPointType('FES_MIX_CTRL') THEN
            DELETE FROM fes_mix_ctrl WHERE POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        */
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_DEV_PT_TEMP_DEF ON fes_dev_pt_temp_def;
CREATE TRIGGER TD_FES_DEV_PT_TEMP_DEF BEFORE DELETE ON fes_dev_pt_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_DEV_PT_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_FES_DEV_PT_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_dev_pt_temp_def', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_dev_pt_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FES_DEV_PT_TEMP_DEF ON fes_dev_pt_temp_def;
CREATE TRIGGER TDA_FES_DEV_PT_TEMP_DEF AFTER DELETE ON fes_dev_pt_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FES_DEV_PT_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_FES_DEV_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_dev_temp_def', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_dev_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FES_DEV_TEMP_DEF ON fes_dev_temp_def;
CREATE TRIGGER TIA_FES_DEV_TEMP_DEF AFTER INSERT ON fes_dev_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FES_DEV_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_DEV_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
        
        UPDATE  fes_dev_pt_temp_def SET 
        TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(TAG_NAME)),
        DEV_TP_NAME = NEW.TAG_NAME
        WHERE DEV_TP_NAME = OLD.TAG_NAME;
        
        UPDATE fes_const_temp SET
        TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(TAG_NAME)),
        DEV_TP_NAME = NEW.TAG_NAME
        WHERE DEV_TP_NAME = OLD.TAG_NAME;
        
        UPDATE fes_data_block_temp SET
        TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(TAG_NAME)),
        DEV_TP_NAME = NEW.TAG_NAME
        WHERE DEV_TP_NAME = OLD.TAG_NAME;
        
        /*
        UPDATE fes_dev_info SET 
        DEV_TP_NAME = NEW.TAG_NAME,
        DEV_TYPE_ID = NEW.DEV_TYPE_ID
        WHERE DEV_TP_NAME = OLD.TAG_NAME;
        */
        
        --更新套件
        IF OLD.TAG_NAME != NEW.TAG_NAME THEN 
            BEGIN
                UPDATE temp_plugin_fes_dev_map SET FES_TEMP_NAME = NEW.TAG_NAME WHERE FES_TEMP_NAME = OLD.TAG_NAME;
            END;
        END IF;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_dev_temp_def', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_dev_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_DEV_TEMP_DEF ON fes_dev_temp_def;
CREATE TRIGGER TU_FES_DEV_TEMP_DEF AFTER UPDATE ON fes_dev_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_DEV_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_DEV_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
        
        DELETE FROM fes_dev_pt_temp_def WHERE  DEV_TP_NAME = OLD.TAG_NAME;
        DELETE FROM fes_const_temp WHERE  DEV_TP_NAME = OLD.TAG_NAME;
        DELETE FROM fes_data_block_temp WHERE  DEV_TP_NAME = OLD.TAG_NAME;
        --套件的处理
        DELETE FROM temp_plugin_fes_dev_map WHERE FES_TEMP_NAME = OLD.TAG_NAME;
        /*
        DELETE FROM fes_dev_info WHERE  DEV_TP_NAME = OLD.TAG_NAME;
         */
        
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_DEV_TEMP_DEF ON fes_dev_temp_def;
CREATE TRIGGER TD_FES_DEV_TEMP_DEF BEFORE DELETE ON fes_dev_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_DEV_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_FES_DEV_TEMP_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_dev_temp_def', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_dev_temp_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FES_DEV_TEMP_DEF ON fes_dev_temp_def;
CREATE TRIGGER TDA_FES_DEV_TEMP_DEF AFTER DELETE ON fes_dev_temp_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FES_DEV_TEMP_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_FES_DEV_TYPE_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_dev_type_def', 'dev_type_id', '', NEW.DEV_TYPE_ID, getTableSubSystem('fes_dev_type_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FES_DEV_TYPE_DEF ON fes_dev_type_def;
CREATE TRIGGER TIA_FES_DEV_TYPE_DEF AFTER INSERT ON fes_dev_type_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FES_DEV_TYPE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_DEV_TYPE_DEF() RETURNS TRIGGER AS $$
BEGIN
    IF NEW.DEV_TYPE_ID <> OLD.DEV_TYPE_ID THEN
    BEGIN
        UPDATE fes_dev_temp_def SET
        DEV_TYPE_ID = NEW.DEV_TYPE_ID
        WHERE DEV_TYPE_ID = OLD.DEV_TYPE_ID;
    END;
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_DEV_TYPE_DEF ON fes_dev_type_def;
CREATE TRIGGER TU_FES_DEV_TYPE_DEF BEFORE UPDATE ON fes_dev_type_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_DEV_TYPE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_FES_DEV_TYPE_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_dev_type_def', 'dev_type_id', OLD.DEV_TYPE_ID, NEW.DEV_TYPE_ID, getTableSubSystem('fes_dev_type_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FES_DEV_TYPE_DEF ON fes_dev_type_def;
CREATE TRIGGER TUA_FES_DEV_TYPE_DEF AFTER UPDATE ON fes_dev_type_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FES_DEV_TYPE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_FES_DEV_TYPE_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_dev_type_def', 'dev_type_id', OLD.DEV_TYPE_ID, '', getTableSubSystem('fes_dev_type_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FES_DEV_TYPE_DEF ON fes_dev_type_def;
CREATE TRIGGER TDA_FES_DEV_TYPE_DEF AFTER DELETE ON fes_dev_type_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FES_DEV_TYPE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_DIGITAL() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('DIGITAL_MAP_TO_FES update','In TI_FES_DIGITAL');
    --首先禁止MAP_TO_FES触发器动作
    SET SESSION "disable.DigitalMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesDigitalTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value1' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO1 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value2' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO2 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value3' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO3 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
         
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value4' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO4 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF; 
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value5' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO5 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET SESSION "disable.DigitalMapToFesTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_digital', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_digital'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_DIGITAL ON fes_digital;
CREATE TRIGGER TI_FES_DIGITAL AFTER INSERT ON fes_digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_DIGITAL() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    IF OLD.APP_TABLE_NAME=NEW.APP_TABLE_NAME AND OLD.APP_TAG_NAME = NEW.APP_TAG_NAME AND OLD.APP_COLUMN_NAME = NEW.APP_COLUMN_NAME
    AND OLD.RTU_TAG = NEW.RTU_TAG AND OLD.DOT_NO = NEW.DOT_NO
    THEN RETURN NEW;
    END IF;
    
    CALL kbdDebugInfo('DIGITAL_MAP_TO_FES update','In TU_FES_DIGITAL');
    --首先禁止MAP_TO_FES触发器动作
    SET SESSION "disable.DigitalMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesDigitalTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value1' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO1 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value2' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO2 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value3' THEN    
            UPDATE digital_map_to_fes SET
            DOT_NO3 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value4' THEN   
            UPDATE digital_map_to_fes SET
            DOT_NO4 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value5' THEN  
            UPDATE digital_map_to_fes SET
            DOT_NO5 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value1' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO1 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value2' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO2 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value3' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO3 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
         
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value4' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO4 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF; 
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value5' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO5 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET SESSION "disable.DigitalMapToFesTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_DIGITAL ON fes_digital;
CREATE TRIGGER TU_FES_DIGITAL BEFORE UPDATE ON fes_digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TUA_FES_DIGITAL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_digital', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_digital'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FES_DIGITAL ON fes_digital;
CREATE TRIGGER TUA_FES_DIGITAL AFTER UPDATE ON fes_digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FES_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_DIGITAL() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('DIGITAL_MAP_TO_FES update','In TD_FES_DIGITAL');
    --首先禁止MAP_TO_FES触发器动作
    SET SESSION "disable.DigitalMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesDigitalTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value1' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO1 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value2' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO2 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value3' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO3 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value4' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO4 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value5' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO5 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
    END;
    END IF;
    
    SET SESSION "disable.DigitalMapToFesTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_digital', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_digital'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_DIGITAL ON fes_digital;
CREATE TRIGGER TD_FES_DIGITAL AFTER DELETE ON fes_digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TIA_FES_DIGITAL_CTRL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_digital_ctrl', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_digital_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FES_DIGITAL_CTRL ON fes_digital_ctrl;
CREATE TRIGGER TIA_FES_DIGITAL_CTRL AFTER INSERT ON fes_digital_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FES_DIGITAL_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_DIGITAL_CTRL() RETURNS TRIGGER AS $$
BEGIN
    --更新关联了此点的遥控点
    
     IF NEW.RTU_TAG <> OLD.RTU_TAG OR NEW.DOT_NO<> OLD.DOT_NO
     THEN 
     BEGIN
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO1 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO1 = cast(OLD.DOT_NO AS char);
            
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO2 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO2 = cast(OLD.DOT_NO AS char);
            
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO3 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO3 = cast(OLD.DOT_NO AS char);
            
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO4 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO4 = cast(OLD.DOT_NO AS char);
            
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO5 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO5 = cast(OLD.DOT_NO AS char);
         
     END; 
     END IF;
    
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_digital_ctrl', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_digital_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_DIGITAL_CTRL ON fes_digital_ctrl;
CREATE TRIGGER TU_FES_DIGITAL_CTRL AFTER UPDATE ON fes_digital_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_DIGITAL_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_DIGITAL_CTRL() RETURNS TRIGGER AS $$
BEGIN
    UPDATE digital_control SET
    OFFSET_NO1 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO1 = cast(OLD.DOT_NO AS char);
   
    UPDATE digital_control SET
    OFFSET_NO2 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO2 = cast(OLD.DOT_NO AS char);
    
    UPDATE digital_control SET
    OFFSET_NO3 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO3 = cast(OLD.DOT_NO AS char);
    
    UPDATE digital_control SET
    OFFSET_NO4 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO4 = cast(OLD.DOT_NO AS char);
    
    UPDATE digital_control SET
    OFFSET_NO5 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO5 = cast(OLD.DOT_NO AS char);
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_digital_ctrl', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_digital_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_DIGITAL_CTRL ON fes_digital_ctrl;
CREATE TRIGGER TD_FES_DIGITAL_CTRL AFTER DELETE ON fes_digital_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_DIGITAL_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_MIX() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('MIX_MAP_TO_FES update','In TI_FES_MIX');
    --首先禁止MAP_TO_FES触发器动作
    SET SESSION "disable.MixMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesMixTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE mix_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET SESSION "disable.MixMapToFesTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_mix', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_mix'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_MIX ON fes_mix;
CREATE TRIGGER TI_FES_MIX AFTER INSERT ON fes_mix FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_MIX();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_MIX() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    IF OLD.APP_TABLE_NAME=NEW.APP_TABLE_NAME AND OLD.APP_TAG_NAME = NEW.APP_TAG_NAME AND OLD.APP_COLUMN_NAME = NEW.APP_COLUMN_NAME
    AND OLD.RTU_TAG = NEW.RTU_TAG AND OLD.DOT_NO = NEW.DOT_NO
    THEN RETURN NEW;
    END IF;
    
    CALL kbdDebugInfo('MIX_MAP_TO_FES update','In TU_FES_MIX');
    --首先禁止MAP_TO_FES触发器动作
    SET SESSION "disable.MixMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesMixTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE mix_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE mix_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET SESSION "disable.MixMapToFesTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_MIX ON fes_mix;
CREATE TRIGGER TU_FES_MIX BEFORE UPDATE ON fes_mix FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_MIX();



CREATE OR REPLACE FUNCTION FUNC_TUA_FES_MIX() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_mix', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_mix'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FES_MIX ON fes_mix;
CREATE TRIGGER TUA_FES_MIX AFTER UPDATE ON fes_mix FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FES_MIX();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_MIX() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    CALL kbdDebugInfo('MIX_MAP_TO_FES update','In TD_FES_MIX');
    --首先禁止MAP_TO_FES触发器动作
    SET SESSION "disable.MixMapToFesTrigger" = 1;
    --然后询问自己是否已经被禁止
	SELECT CURRENT_SETTING('disable.FesMixTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE mix_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
    END;
    END IF;
    
    SET SESSION "disable.MixMapToFesTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_mix', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_mix'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_MIX ON fes_mix;
CREATE TRIGGER TD_FES_MIX AFTER DELETE ON fes_mix FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_MIX();



CREATE OR REPLACE FUNCTION FUNC_TIA_FES_MIX_CTRL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_mix_ctrl', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_mix_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FES_MIX_CTRL ON fes_mix_ctrl;
CREATE TRIGGER TIA_FES_MIX_CTRL AFTER INSERT ON fes_mix_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FES_MIX_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_MIX_CTRL() RETURNS TRIGGER AS $$
BEGIN
    --更新关联了此点的遥控点
    
    IF NEW.RTU_TAG = OLD.RTU_TAG AND NEW.DOT_NO= OLD.DOT_NO
    THEN
    BEGIN
        UPDATE mix_control SET
        RTU_TAG = NEW.RTU_TAG,
        OFFSET_NO = NEW.DOT_NO
        WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO = cast(OLD.DOT_NO AS char);
    END; 
    END IF;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_mix_ctrl', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_mix_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_MIX_CTRL ON fes_mix_ctrl;
CREATE TRIGGER TU_FES_MIX_CTRL AFTER UPDATE ON fes_mix_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_MIX_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_MIX_CTRL() RETURNS TRIGGER AS $$
BEGIN
    UPDATE mix_control SET
    OFFSET_NO = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO = cast(OLD.DOT_NO AS char);
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_mix_ctrl', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_mix_ctrl'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_MIX_CTRL ON fes_mix_ctrl;
CREATE TRIGGER TD_FES_MIX_CTRL AFTER DELETE ON fes_mix_ctrl FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_MIX_CTRL();



CREATE OR REPLACE FUNCTION FUNC_TIA_FES_PROTOCOL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_protocol', 'protocol_id', '', NEW.PROTOCOL_ID, getTableSubSystem('fes_protocol'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FES_PROTOCOL ON fes_protocol;
CREATE TRIGGER TIA_FES_PROTOCOL AFTER INSERT ON fes_protocol FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FES_PROTOCOL();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_PROTOCOL() RETURNS TRIGGER AS $$
BEGIN
    IF NEW.PROTOCOL_ID <> OLD.PROTOCOL_ID THEN
    BEGIN
        UPDATE fes_channel_para SET
        PROTO_TYPE = NEW.PROTOCOL_ID
        WHERE PROTO_TYPE = OLD.PROTOCOL_ID;
    END;
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_PROTOCOL ON fes_protocol;
CREATE TRIGGER TU_FES_PROTOCOL BEFORE UPDATE ON fes_protocol FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_PROTOCOL();



CREATE OR REPLACE FUNCTION FUNC_TUA_FES_PROTOCOL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_protocol', 'protocol_id', OLD.PROTOCOL_ID, NEW.PROTOCOL_ID, getTableSubSystem('fes_protocol'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FES_PROTOCOL ON fes_protocol;
CREATE TRIGGER TUA_FES_PROTOCOL AFTER UPDATE ON fes_protocol FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FES_PROTOCOL();



CREATE OR REPLACE FUNCTION FUNC_TDA_FES_PROTOCOL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_protocol', 'protocol_id', OLD.PROTOCOL_ID, '', getTableSubSystem('fes_protocol'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FES_PROTOCOL ON fes_protocol;
CREATE TRIGGER TDA_FES_PROTOCOL AFTER DELETE ON fes_protocol FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FES_PROTOCOL();



CREATE OR REPLACE FUNCTION FUNC_TIB_FES_RTU_PARA() RETURNS TRIGGER AS $$
    DECLARE chNo decimal(10,0) DEFAULT 0;
BEGIN
    NEW.TAG_NAME = concat(NEW.CHAN_TAG,'.',NEW.RTU_NAME);
    
    SELECT CHAN_NO INTO chNo FROM fes_channel_para WHERE TAG_NAME = NEW.CHAN_TAG;
    
    NEW.CHAN_NO = chNo;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIB_FES_RTU_PARA ON fes_rtu_para;
CREATE TRIGGER TIB_FES_RTU_PARA BEFORE INSERT ON fes_rtu_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIB_FES_RTU_PARA();



CREATE OR REPLACE FUNCTION FUNC_TI_FES_RTU_PARA() RETURNS TRIGGER AS $$
BEGIN
  /*  
   * 不再产生默认设备
INSERT INTO fes_dev_info 
(TAG_NAME,                                 DEV_NAME,   LOCATION_ID,    SUB_SYSTEM,    DEV_ID,   DEV_TYPE_ID, RTU_TAG,    DEV_DESC,  RTU_NO,     DEV_TP_NAME) VALUES 
(concat(NEW.TAG_NAME, '.', 'defaultDev' ),'defaultDev',NEW.LOCATION_ID,NEW.SUB_SYSTEM,'-1',     '-1'       ,NEW.TAG_NAME,'默认设备',NEW.RTU_NO  ,'' );
*/
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_rtu_para', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_rtu_para'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_FES_RTU_PARA ON fes_rtu_para;
CREATE TRIGGER TI_FES_RTU_PARA AFTER INSERT ON fes_rtu_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_FES_RTU_PARA();



CREATE OR REPLACE FUNCTION FUNC_TUB_FES_RTU_PARA() RETURNS TRIGGER AS $$
    DECLARE chNo decimal(10,0) DEFAULT 0;
BEGIN
    IF NEW.RTU_NAME <> OLD.RTU_NAME OR NEW.CHAN_TAG <> OLD.CHAN_TAG THEN 
        NEW.TAG_NAME = concat(NEW.CHAN_TAG,'.',NEW.RTU_NAME);
    END IF;
	
	    IF NEW.CHAN_TAG <> OLD.CHAN_TAG THEN 
    BEGIN
        SELECT CHAN_NO INTO chNo FROM fes_channel_para WHERE TAG_NAME = NEW.CHAN_TAG;
        NEW.CHAN_NO = chNo;
    END; 
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUB_FES_RTU_PARA ON fes_rtu_para;
CREATE TRIGGER TUB_FES_RTU_PARA BEFORE UPDATE ON fes_rtu_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUB_FES_RTU_PARA();



CREATE OR REPLACE FUNCTION FUNC_TU_FES_RTU_PARA() RETURNS TRIGGER AS $$
BEGIN
    
IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.RTU_NO <> OLD.RTU_NO OR NEW.DESCRIPTION <> OLD.DESCRIPTION
THEN
BEGIN 
    
    
    UPDATE fes_dev_info SET 
    TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(TAG_NAME)) ,
    DEV_DESC = DEV_DESC,
    RTU_TAG = NEW.TAG_NAME,
    RTU_NO = NEW.RTU_NO,
    LOCATION_ID = NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM
    WHERE RTU_TAG = OLD.TAG_NAME;
    
    UPDATE fes_data_block SET 
    RTU_NO = NEW.RTU_NO,
    LOCATION_ID = NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM
    WHERE RTU_NO = OLD.RTU_NO;
    
    IF NEW.RTU_NO <> OLD.RTU_NO THEN
    BEGIN
        UPDATE dev_group SET
        RTU_NO = NEW.RTU_NO
        WHERE RTU_NO = OLD.RTU_NO;
    END;
    END IF;
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
    BEGIN
        --更新关联的后台测点
        UPDATE accuml SET
        RTU_TAG = NEW.TAG_NAME
        WHERE RTU_TAG = OLD.TAG_NAME;
        
        UPDATE analog SET
        RTU_TAG = NEW.TAG_NAME
        WHERE RTU_TAG = OLD.TAG_NAME;
        
        UPDATE digital SET
        RTU_TAG = NEW.TAG_NAME
        WHERE RTU_TAG = OLD.TAG_NAME;
        
        UPDATE mix SET
        RTU_TAG = NEW.TAG_NAME
        WHERE RTU_TAG = OLD.TAG_NAME;
        
        
    END;
    END IF;
    
END;
END IF;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_rtu_para', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_rtu_para'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_FES_RTU_PARA ON fes_rtu_para;
CREATE TRIGGER TU_FES_RTU_PARA AFTER UPDATE ON fes_rtu_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_FES_RTU_PARA();



CREATE OR REPLACE FUNCTION FUNC_TD_FES_RTU_PARA() RETURNS TRIGGER AS $$
BEGIN
DELETE FROM fes_dev_info WHERE RTU_TAG = OLD.TAG_NAME;
DELETE FROM fes_data_block WHERE RTU_NO = OLD.RTU_NO;
--更新关联的后台测点
UPDATE accuml SET
RTU_TAG = ''
WHERE RTU_TAG = OLD.TAG_NAME;
UPDATE analog SET
RTU_TAG = ''
WHERE RTU_TAG = OLD.TAG_NAME;
UPDATE digital SET
RTU_TAG = ''
WHERE RTU_TAG = OLD.TAG_NAME;
UPDATE mix SET
RTU_TAG = ''
WHERE RTU_TAG = OLD.TAG_NAME;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_FES_RTU_PARA ON fes_rtu_para;
CREATE TRIGGER TD_FES_RTU_PARA BEFORE DELETE ON fes_rtu_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_FES_RTU_PARA();



CREATE OR REPLACE FUNCTION FUNC_TDA_FES_RTU_PARA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_rtu_para', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_rtu_para'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FES_RTU_PARA ON fes_rtu_para;
CREATE TRIGGER TDA_FES_RTU_PARA AFTER DELETE ON fes_rtu_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FES_RTU_PARA();



CREATE OR REPLACE FUNCTION FUNC_TI_LINKAGE_ACTION() RETURNS TRIGGER AS $$
BEGIN
    --仅仅修改时标
    
    NEW.TIMEFLAG = floor(extract(epoch from now()));
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_LINKAGE_ACTION ON linkage_action;
CREATE TRIGGER TI_LINKAGE_ACTION BEFORE INSERT ON linkage_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_LINKAGE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TIA_LINKAGE_ACTION() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'linkage_action', 'name', '', NEW.NAME, getTableSubSystem('linkage_action'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_LINKAGE_ACTION ON linkage_action;
CREATE TRIGGER TIA_LINKAGE_ACTION AFTER INSERT ON linkage_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_LINKAGE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TU_LINKAGE_ACTION() RETURNS TRIGGER AS $$
BEGIN
    --修改对应功能
    NEW.TIMEFLAG = floor(extract(epoch from now()));
    UPDATE linkage_function SET 
    TIMEFLAG = NEW.TIMEFLAG,
    ACTION_NAME = NEW.NAME
    WHERE ACTION_NAME = OLD.NAME;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_LINKAGE_ACTION ON linkage_action;
CREATE TRIGGER TU_LINKAGE_ACTION BEFORE UPDATE ON linkage_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_LINKAGE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TUA_LINKAGE_ACTION() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'linkage_action', 'name', OLD.NAME, NEW.NAME, getTableSubSystem('linkage_action'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_LINKAGE_ACTION ON linkage_action;
CREATE TRIGGER TUA_LINKAGE_ACTION AFTER UPDATE ON linkage_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_LINKAGE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TD_LINKAGE_ACTION() RETURNS TRIGGER AS $$
BEGIN
    --删除关联了此动作的功能
    DELETE FROM linkage_function WHERE ACTION_NAME = OLD.NAME; 
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'linkage_action', 'name', OLD.NAME, '', getTableSubSystem('linkage_action'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_LINKAGE_ACTION ON linkage_action;
CREATE TRIGGER TD_LINKAGE_ACTION AFTER DELETE ON linkage_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_LINKAGE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TI_LINKAGE_DEFINE() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --仅仅修改时标
    --避免触发器循环
    SET SESSION "disable.LinkageSettingTrigger" = 1;
    
    CALL kbdDebugInfo('linkage_define Insert','linkage_define Insert');
    
	SELECT CURRENT_SETTING('disable.LinkageDefineTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        NEW.TIMEFLAG = floor(extract(epoch from now()));
        UPDATE linkage_setting SET 
        TIMEFLAG = NEW.TIMEFLAG
        WHERE NAME = NEW.LINKAGE_NAME;
    END;
    END IF;
    
    SET SESSION "disable.LinkageSettingTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_LINKAGE_DEFINE ON linkage_define;
CREATE TRIGGER TI_LINKAGE_DEFINE BEFORE INSERT ON linkage_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_LINKAGE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_LINKAGE_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'linkage_define', 'linkage_name,func_name', '', concat(NEW.LINKAGE_NAME,',',NEW.FUNC_NAME), getTableSubSystem('linkage_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_LINKAGE_DEFINE ON linkage_define;
CREATE TRIGGER TIA_LINKAGE_DEFINE AFTER INSERT ON linkage_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_LINKAGE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TU_LINKAGE_DEFINE() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --修改对应联动功能关系表
    --避免触发器循环
    SET SESSION "disable.LinkageSettingTrigger" = 1;
    
    CALL kbdDebugInfo('linkage_define update','linkage_define update');
    
	SELECT CURRENT_SETTING('disable.LinkageDefineTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        NEW.TIMEFLAG = floor(extract(epoch from now()));
        UPDATE linkage_setting SET 
        TIMEFLAG = NEW.TIMEFLAG
        WHERE NAME = OLD.LINKAGE_NAME;
    END;
    END IF;
    
    SET SESSION "disable.LinkageSettingTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_LINKAGE_DEFINE ON linkage_define;
CREATE TRIGGER TU_LINKAGE_DEFINE BEFORE UPDATE ON linkage_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_LINKAGE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_LINKAGE_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'linkage_define', 'linkage_name,func_name', concat(OLD.LINKAGE_NAME,',',OLD.FUNC_NAME), concat(NEW.LINKAGE_NAME,',',NEW.FUNC_NAME), getTableSubSystem('linkage_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_LINKAGE_DEFINE ON linkage_define;
CREATE TRIGGER TUA_LINKAGE_DEFINE AFTER UPDATE ON linkage_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_LINKAGE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TD_LINKAGE_DEFINE() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --仅仅修改时标
    --避免触发器循环
    SET SESSION "disable.LinkageSettingTrigger" = 1;
    
    CALL kbdDebugInfo('linkage_define delete','linkage_define delete');
    
	SELECT CURRENT_SETTING('disable.LinkageDefineTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        UPDATE linkage_setting SET 
        TIMEFLAG = floor(extract(epoch from now()))
        WHERE NAME = OLD.LINKAGE_NAME;
    END;
    END IF;
    
    SET SESSION "disable.LinkageSettingTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'linkage_define', 'linkage_name,func_name', concat(OLD.LINKAGE_NAME,',',OLD.FUNC_NAME), '', getTableSubSystem('linkage_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_LINKAGE_DEFINE ON linkage_define;
CREATE TRIGGER TD_LINKAGE_DEFINE AFTER DELETE ON linkage_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_LINKAGE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TI_LINKAGE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
    --仅仅修改时标
    NEW.TIMEFLAG = floor(extract(epoch from now()));
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_LINKAGE_FUNCTION ON linkage_function;
CREATE TRIGGER TI_LINKAGE_FUNCTION BEFORE INSERT ON linkage_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_LINKAGE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TIA_LINKAGE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'linkage_function', 'name,action_name', '', concat(NEW.NAME,',',NEW.ACTION_NAME), getTableSubSystem('linkage_function'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_LINKAGE_FUNCTION ON linkage_function;
CREATE TRIGGER TIA_LINKAGE_FUNCTION AFTER INSERT ON linkage_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_LINKAGE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TU_LINKAGE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
    --修改对应联动功能关系表
    NEW.TIMEFLAG = floor(extract(epoch from now()));
    UPDATE linkage_define SET 
    TIMEFLAG = NEW.TIMEFLAG,
    FUNC_NAME = NEW.NAME
    WHERE FUNC_NAME = OLD.NAME;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_LINKAGE_FUNCTION ON linkage_function;
CREATE TRIGGER TU_LINKAGE_FUNCTION BEFORE UPDATE ON linkage_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_LINKAGE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TUA_LINKAGE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'linkage_function', 'name,action_name', concat(OLD.NAME,',',OLD.ACTION_NAME), concat(NEW.NAME,',',NEW.ACTION_NAME), getTableSubSystem('linkage_function'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_LINKAGE_FUNCTION ON linkage_function;
CREATE TRIGGER TUA_LINKAGE_FUNCTION AFTER UPDATE ON linkage_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_LINKAGE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TD_LINKAGE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
    --删除关联了此功能的联动定义
    --检查功能是否被删除
    IF NOT EXISTS (SELECT * FROM linkage_function WHERE NAME = OLD.NAME) THEN 
    BEGIN
        DELETE FROM linkage_define WHERE FUNC_NAME = OLD.NAME; 
    END;
    ELSE 
    BEGIN
        --修改时标
        UPDATE linkage_define SET
        TIMEFLAG = floor(extract(epoch from now()))
        WHERE FUNC_NAME = OLD.NAME;
    END; 
    END IF;
    
    
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'linkage_function', 'name,action_name', concat(OLD.NAME,',',OLD.ACTION_NAME), '', getTableSubSystem('linkage_function'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_LINKAGE_FUNCTION ON linkage_function;
CREATE TRIGGER TD_LINKAGE_FUNCTION AFTER DELETE ON linkage_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_LINKAGE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TI_LINKAGE_SETTING() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --仅仅修改时标
    --避免触发器循环
    SET SESSION "disable.LinkageDefineTrigger" = 1;
    
    CALL kbdDebugInfo('linkage_setting insert','linkage_setting insert');
    
	SELECT CURRENT_SETTING('disable.LinkageSettingTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        NEW.TIMEFLAG = floor(extract(epoch from now()));
    END;
    END IF;
    
    SET SESSION "disable.LinkageDefineTrigger" = 0;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_LINKAGE_SETTING ON linkage_setting;
CREATE TRIGGER TI_LINKAGE_SETTING BEFORE INSERT ON linkage_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_LINKAGE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TIA_LINKAGE_SETTING() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'linkage_setting', 'name', '', NEW.NAME, getTableSubSystem('linkage_setting'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_LINKAGE_SETTING ON linkage_setting;
CREATE TRIGGER TIA_LINKAGE_SETTING AFTER INSERT ON linkage_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_LINKAGE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TU_LINKAGE_SETTING() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --修改对应联动功能关系表
    --避免触发器循环
    SET SESSION "disable.LinkageDefineTrigger" = 1;
    
    CALL kbdDebugInfo('linkage_setting update','linkage_setting update');
    
	SELECT CURRENT_SETTING('disable.LinkageSettingTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        NEW.TIMEFLAG = floor(extract(epoch from now()));
        IF NEW.NAME<>OLD.NAME THEN 
            UPDATE linkage_define SET 
            LINKAGE_NAME = NEW.NAME
            WHERE LINKAGE_NAME = OLD.NAME;
       END IF;
    END;
    END IF;
    
    SET SESSION "disable.LinkageDefineTrigger" = 0;
    
   
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_LINKAGE_SETTING ON linkage_setting;
CREATE TRIGGER TU_LINKAGE_SETTING BEFORE UPDATE ON linkage_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_LINKAGE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TUA_LINKAGE_SETTING() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'linkage_setting', 'name', OLD.NAME, NEW.NAME, getTableSubSystem('linkage_setting'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_LINKAGE_SETTING ON linkage_setting;
CREATE TRIGGER TUA_LINKAGE_SETTING AFTER UPDATE ON linkage_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_LINKAGE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TD_LINKAGE_SETTING() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --删除关联了此联动的功能关联
    --避免触发器循环
    SET SESSION "disable.LinkageDefineTrigger" = 1;
    
    CALL kbdDebugInfo('linkage_setting delete','linkage_setting delete');
    
	SELECT CURRENT_SETTING('disable.LinkageSettingTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        DELETE FROM linkage_define WHERE LINKAGE_NAME = OLD.NAME;
    END;
    END IF;
    
    SET SESSION "disable.LinkageDefineTrigger" = 0;
     
   
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'linkage_setting', 'name', OLD.NAME, '', getTableSubSystem('linkage_setting'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_LINKAGE_SETTING ON linkage_setting;
CREATE TRIGGER TD_LINKAGE_SETTING AFTER DELETE ON linkage_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_LINKAGE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TIA_MIX_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'mix_control', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('mix_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_MIX_CONTROL ON mix_control;
CREATE TRIGGER TIA_MIX_CONTROL AFTER INSERT ON mix_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_MIX_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TU_MIX_CONTROL() RETURNS TRIGGER AS $$
BEGIN
    
    --修改功能名称
    IF COALESCE(NEW.RTU_TAG,'') = '' THEN 
    BEGIN
        NEW.OFFSET_NO = '-1';
    END;
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_MIX_CONTROL ON mix_control;
CREATE TRIGGER TU_MIX_CONTROL BEFORE UPDATE ON mix_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_MIX_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TUA_MIX_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'mix_control', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('mix_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_MIX_CONTROL ON mix_control;
CREATE TRIGGER TUA_MIX_CONTROL AFTER UPDATE ON mix_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_MIX_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TDA_MIX_CONTROL() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'mix_control', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('mix_control'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_MIX_CONTROL ON mix_control;
CREATE TRIGGER TDA_MIX_CONTROL AFTER DELETE ON mix_control FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_MIX_CONTROL();



CREATE OR REPLACE FUNCTION FUNC_TI_MIX_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    CALL kbdDebugInfo('FES MIX update','In TI_MIX_MAP_TO_FES');
    
    SET SESSION "disable.FesMixTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.MixMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN 
                UPDATE fes_mix SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    SET SESSION "disable.FesMixTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'mix_map_to_fes', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('mix_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_MIX_MAP_TO_FES ON mix_map_to_fes;
CREATE TRIGGER TI_MIX_MAP_TO_FES AFTER INSERT ON mix_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_MIX_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TU_MIX_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    IF NEW.RTU_TAG = OLD.RTU_TAG AND 
        NEW.DOT_NO = OLD.DOT_NO AND 
        NEW.KEY_ID_TAG = OLD.KEY_ID_TAG
        THEN RETURN NEW;
    END IF;
    
    
    SET SESSION "disable.FesMixTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.MixMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN 
                UPDATE fes_mix SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
        
        
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN 
                UPDATE fes_mix SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        ELSE NEW.DOT_NO = '-1';
        END IF;
    END;
    END IF;
    
    SET SESSION "disable.FesMixTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_MIX_MAP_TO_FES ON mix_map_to_fes;
CREATE TRIGGER TU_MIX_MAP_TO_FES BEFORE UPDATE ON mix_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_MIX_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TUA_MIX_MAP_TO_FES() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'mix_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('mix_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_MIX_MAP_TO_FES ON mix_map_to_fes;
CREATE TRIGGER TUA_MIX_MAP_TO_FES AFTER UPDATE ON mix_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_MIX_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TD_MIX_MAP_TO_FES() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    CALL kbdDebugInfo('FES MIX update','In TD_MIX_MAP_TO_FES');
    
    SET SESSION "disable.FesMixTrigger" = 1;
    
	SELECT CURRENT_SETTING('disable.MixMapToFesTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN 
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN 
                UPDATE fes_mix SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
    SET SESSION "disable.FesMixTrigger" = 0;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_MIX_MAP_TO_FES ON mix_map_to_fes;
CREATE TRIGGER TD_MIX_MAP_TO_FES BEFORE DELETE ON mix_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_MIX_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TDA_MIX_MAP_TO_FES() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'mix_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('mix_map_to_fes'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_MIX_MAP_TO_FES ON mix_map_to_fes;
CREATE TRIGGER TDA_MIX_MAP_TO_FES AFTER DELETE ON mix_map_to_fes FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_MIX_MAP_TO_FES();



CREATE OR REPLACE FUNCTION FUNC_TI_MIX_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
   /*
    INSERT INTO mix(
    TAG_NAME,                                             DESCRIPTION,    DEVICE,  SEQ_NO,    LOCATION_ID, SUB_SYSTEM, RTU_TAG, IS_CONTROL,     STATE_TEXT_NAME,    GRAY, CONTRAST, QCOLOR, RAW_VALUE, VALUE, STATUS, LAST_UPDATE_TIME, LAST_CHANGE_TIME, METHOD, ALARM_PRIORITY,    PIC_NAME, SOUND_NAME1, SOUND_NAME2, SOUND_NAME3, ALARM_DELAY_TIME,    IS_WATER_ALM,    IS_ACK_ON_RTN,    DEL_ACT_ON_ACK,    IS_NEVER_ALM_ON_RTN,    REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TYPE, POINT_TP_NAME, IS_SAMPLE,    IS_STATISTICS,    SAMPLE_PERIOD,    POINT_TYPE,    POINT_CLASS,    POINT_PROPERTY,    POINT_SORT, CAMERA_TAG, PRESET_ID) SELECT   
    concat(TAG_NAME, '.' , getAfterLastDot(NEW.TAG_NAME)),NEW.DESCRIPTION,TAG_NAME,NEW.SEQ_NO,LOCATION_ID, SUB_SYSTEM, '',      NEW.IS_CONTROL, NEW.STATE_TEXT_NAME,GRAY, CONTRAST, QCOLOR, '0',       VALUE, STATUS, '0',              '0',             '',      NEW.ALARM_PRIORITY, '',       '',         '',          '',          NEW.ALARM_DELAY_TIME,NEW.IS_WATER_ALM,NEW.IS_ACK_ON_RTN,NEW.DEL_ACT_ON_ACK,NEW.IS_NEVER_ALM_ON_RTN,REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TYPE, NEW.TAG_NAME,  NEW.IS_SAMPLE,NEW.IS_STATISTICS,NEW.SAMPLE_PERIOD,NEW.POINT_TYPE,NEW.POINT_CLASS,NEW.POINT_PROPERTY,NEW.POINT_SORT,'',      ''
    FROM dev_info WHERE DEV_TP_NAME = NEW.DEV_TP_NAME;
*/
    
    INSERT INTO temp_plugin_point_map_mix
    (PLUGIN_NAME, TEMP_NAME, TEMP_POINT_NAME, FES_TEMP_NAME, FES_TEMP_POINT_NAME, FES_TEMP_OUT_NAME)
    SELECT 
    PLUGIN_NAME, TEMP_NAME, getAfterLastDot(NEW.TAG_NAME),'','',''
    FROM temp_plugin_dev_map WHERE TEMP_NAME = NEW.DEV_TP_NAME;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'mix_temp_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('mix_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_MIX_TEMP_DEFINE ON mix_temp_define;
CREATE TRIGGER TI_MIX_TEMP_DEFINE AFTER INSERT ON mix_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_MIX_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TU_MIX_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    /*
    UPDATE mix SET 
    TAG_NAME = concat(DEVICE, '.', getAfterLastDot(NEW.TAG_NAME)),
    DESCRIPTION = NEW.DESCRIPTION,
    SEQ_NO = NEW.SEQ_NO,
    IS_CONTROL = NEW.IS_CONTROL,
    STATE_TEXT_NAME = NEW.STATE_TEXT_NAME,
    ALARM_PRIORITY = NEW.ALARM_PRIORITY,
    ALARM_DELAY_TIME = NEW.ALARM_DELAY_TIME,
    IS_WATER_ALM = NEW.IS_WATER_ALM,
    IS_ACK_ON_RTN = NEW.IS_ACK_ON_RTN,
    DEL_ACT_ON_ACK = NEW.DEL_ACT_ON_ACK,
    IS_NEVER_ALM_ON_RTN = NEW.IS_NEVER_ALM_ON_RTN,
    POINT_TP_NAME = NEW.TAG_NAME,
    IS_SAMPLE = NEW.IS_SAMPLE,
    IS_STATISTICS = NEW.IS_STATISTICS,
    SAMPLE_PERIOD = NEW.SAMPLE_PERIOD,
    POINT_TYPE = NEW.POINT_TYPE,
    POINT_CLASS = NEW.POINT_CLASS,
    POINT_PROPERTY = NEW.POINT_PROPERTY,
    POINT_SORT = NEW.POINT_SORT
    WHERE POINT_TP_NAME=OLD.TAG_NAME;
    
    UPDATE mix_control SET
    CTRL_ACT_NAME = NEW.CTRL_ACT_NAME,
    CTRL_TYPE = NEW.CTRL_TYPE,
    IS_TAGT_STATE = NEW.IS_TAGT_STATE,
    CTRL_TIMEOUT = NEW.CTRL_TIMEOUT,
    RESV_TIMEOUT = NEW.RESV_TIMEOUT,
    BASE = NEW.BASE,
    COEFF = NEW.COEFF,
    CTRL_TOLERANCE = NEW.CTRL_TOLERANCE,
    RANGE_UP = NEW.RANGE_UP,
    RANGE_LOW = NEW.RANGE_LOW,
    POINT_TP_NAME = NEW.TAG_NAME
    WHERE POINT_TP_NAME = OLD.TAG_NAME;
    
    UPDATE mix_map_to_fes SET
    POINT_TP_NAME = NEW.TAG_NAME,
    BASE = NEW.BASE,
    COEFF = NEW.COEFF,
    RANGE_UP = NEW.RANGE_UP,
    RANGE_LOW = NEW.RANGE_LOW,
    IS_FILTER_ERROR = NEW.IS_FILTER_ERROR,
    IS_FILTER_DISTURB = NEW.IS_FILTER_DISTURB,
    DISTURB_TIME = NEW.DISTURB_TIME
    WHERE POINT_TP_NAME = OLD.TAG_NAME;
    */
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
        UPDATE temp_plugin_point_map_mix
        SET TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME)
        WHERE TEMP_NAME = OLD.DEV_TP_NAME AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    END IF;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'mix_temp_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('mix_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_MIX_TEMP_DEFINE ON mix_temp_define;
CREATE TRIGGER TU_MIX_TEMP_DEFINE AFTER UPDATE ON mix_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_MIX_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TD_MIX_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM temp_plugin_point_map_mix WHERE TEMP_NAME = OLD.DEV_TP_NAME  AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    /*
    DELETE FROM mix WHERE POINT_TP_NAME = OLD.TAG_NAME;
    */
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_MIX_TEMP_DEFINE ON mix_temp_define;
CREATE TRIGGER TD_MIX_TEMP_DEFINE BEFORE DELETE ON mix_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_MIX_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_MIX_TEMP_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'mix_temp_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('mix_temp_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_MIX_TEMP_DEFINE ON mix_temp_define;
CREATE TRIGGER TDA_MIX_TEMP_DEFINE AFTER DELETE ON mix_temp_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_MIX_TEMP_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPC_CONFIG_DEFINE() RETURNS TRIGGER AS $$
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'opc_config_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('opc_config_define'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPC_CONFIG_DEFINE ON opc_config_define;
CREATE TRIGGER TIA_OPC_CONFIG_DEFINE AFTER INSERT ON opc_config_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPC_CONFIG_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPC_CONFIG_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,   KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'opc_config_define', 'tag_name',     OLD.TAG_NAME,  NEW.TAG_NAME, getTableSubSystem('opc_config_define'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPC_CONFIG_DEFINE ON opc_config_define;
CREATE TRIGGER TUA_OPC_CONFIG_DEFINE AFTER UPDATE ON opc_config_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPC_CONFIG_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPC_CONFIG_DEFINE() RETURNS TRIGGER AS $$
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'opc_config_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('opc_config_define'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPC_CONFIG_DEFINE ON opc_config_define;
CREATE TRIGGER TDA_OPC_CONFIG_DEFINE AFTER DELETE ON opc_config_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPC_CONFIG_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPC_CONFIG_VERSION() RETURNS TRIGGER AS $$
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES						(getMaxTriggerId(),getOpType('insert'),'opc_config_version', 'domain_id,sub_system', '', concat(NEW.DOMAIN_ID,',',NEW.SUB_SYSTEM), getTableSubSystem('opc_config_version'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPC_CONFIG_VERSION ON opc_config_version;
CREATE TRIGGER TIA_OPC_CONFIG_VERSION AFTER INSERT ON opc_config_version FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPC_CONFIG_VERSION();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPC_CONFIG_VERSION() RETURNS TRIGGER AS $$
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,   KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'opc_config_version', 'domain_id,sub_system', concat(OLD.DOMAIN_ID,',',OLD.SUB_SYSTEM), concat(NEW.DOMAIN_ID,',',NEW.SUB_SYSTEM), getTableSubSystem('opc_config_version'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPC_CONFIG_VERSION ON opc_config_version;
CREATE TRIGGER TUA_OPC_CONFIG_VERSION AFTER UPDATE ON opc_config_version FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPC_CONFIG_VERSION();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPC_CONFIG_VERSION() RETURNS TRIGGER AS $$
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                                            (getMaxTriggerId(),getOpType('delete'),'opc_config_version', 'domain_id,sub_system', concat(OLD.DOMAIN_ID,',',OLD.SUB_SYSTEM), '', getTableSubSystem('opc_config_version'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPC_CONFIG_VERSION ON opc_config_version;
CREATE TRIGGER TDA_OPC_CONFIG_VERSION AFTER DELETE ON opc_config_version FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPC_CONFIG_VERSION();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_AREA_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_area_info', 'id', '', NEW.ID, getTableSubSystem('pa_area_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_AREA_INFO ON pa_area_info;
CREATE TRIGGER TIA_PA_AREA_INFO AFTER INSERT ON pa_area_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_AREA_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_AREA_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_area_info', 'id', OLD.ID, NEW.ID, getTableSubSystem('pa_area_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_AREA_INFO ON pa_area_info;
CREATE TRIGGER TUA_PA_AREA_INFO AFTER UPDATE ON pa_area_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_AREA_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_AREA_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_area_info', 'id', OLD.ID, '', getTableSubSystem('pa_area_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_AREA_INFO ON pa_area_info;
CREATE TRIGGER TDA_PA_AREA_INFO AFTER DELETE ON pa_area_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_AREA_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_AUDIO_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_audio_info', 'id', '', NEW.ID, getTableSubSystem('pa_audio_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_AUDIO_INFO ON pa_audio_info;
CREATE TRIGGER TIA_PA_AUDIO_INFO AFTER INSERT ON pa_audio_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_AUDIO_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_AUDIO_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_audio_info', 'id', OLD.ID, NEW.ID, getTableSubSystem('pa_audio_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_AUDIO_INFO ON pa_audio_info;
CREATE TRIGGER TUA_PA_AUDIO_INFO AFTER UPDATE ON pa_audio_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_AUDIO_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_AUDIO_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_audio_info', 'id', OLD.ID, '', getTableSubSystem('pa_audio_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_AUDIO_INFO ON pa_audio_info;
CREATE TRIGGER TDA_PA_AUDIO_INFO AFTER DELETE ON pa_audio_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_AUDIO_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_BROADCAST_GROUP() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_broadcast_group', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pa_broadcast_group'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_BROADCAST_GROUP ON pa_broadcast_group;
CREATE TRIGGER TIA_PA_BROADCAST_GROUP AFTER INSERT ON pa_broadcast_group FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_BROADCAST_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_BROADCAST_GROUP() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_broadcast_group', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pa_broadcast_group'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_BROADCAST_GROUP ON pa_broadcast_group;
CREATE TRIGGER TUA_PA_BROADCAST_GROUP AFTER UPDATE ON pa_broadcast_group FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_BROADCAST_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_BROADCAST_GROUP() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_broadcast_group', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('pa_broadcast_group'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_BROADCAST_GROUP ON pa_broadcast_group;
CREATE TRIGGER TDA_PA_BROADCAST_GROUP AFTER DELETE ON pa_broadcast_group FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_BROADCAST_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_ctrl_param', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('pa_ctrl_param'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_CTRL_PARAM ON pa_ctrl_param;
CREATE TRIGGER TIA_PA_CTRL_PARAM AFTER INSERT ON pa_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_ctrl_param', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('pa_ctrl_param'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_CTRL_PARAM ON pa_ctrl_param;
CREATE TRIGGER TUA_PA_CTRL_PARAM AFTER UPDATE ON pa_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_ctrl_param', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('pa_ctrl_param'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_CTRL_PARAM ON pa_ctrl_param;
CREATE TRIGGER TDA_PA_CTRL_PARAM AFTER DELETE ON pa_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_LOCATION_AREA_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_location_area_def', 'location_id,area_id', '', concat(NEW.LOCATION_ID,',',NEW.AREA_ID), getTableSubSystem('pa_location_area_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_LOCATION_AREA_DEF ON pa_location_area_def;
CREATE TRIGGER TIA_PA_LOCATION_AREA_DEF AFTER INSERT ON pa_location_area_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_LOCATION_AREA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_LOCATION_AREA_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_location_area_def', 'location_id,area_id', concat(OLD.LOCATION_ID,',',OLD.AREA_ID), concat(NEW.LOCATION_ID,',',NEW.AREA_ID), getTableSubSystem('pa_location_area_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_LOCATION_AREA_DEF ON pa_location_area_def;
CREATE TRIGGER TUA_PA_LOCATION_AREA_DEF AFTER UPDATE ON pa_location_area_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_LOCATION_AREA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_LOCATION_AREA_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_location_area_def', 'location_id,area_id', concat(OLD.LOCATION_ID,',',OLD.AREA_ID), '', getTableSubSystem('pa_location_area_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_LOCATION_AREA_DEF ON pa_location_area_def;
CREATE TRIGGER TDA_PA_LOCATION_AREA_DEF AFTER DELETE ON pa_location_area_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_LOCATION_AREA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_LOCATION_SEQ_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_location_seq_def', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('pa_location_seq_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_LOCATION_SEQ_DEF ON pa_location_seq_def;
CREATE TRIGGER TIA_PA_LOCATION_SEQ_DEF AFTER INSERT ON pa_location_seq_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_LOCATION_SEQ_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_LOCATION_SEQ_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_location_seq_def', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('pa_location_seq_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_LOCATION_SEQ_DEF ON pa_location_seq_def;
CREATE TRIGGER TUA_PA_LOCATION_SEQ_DEF AFTER UPDATE ON pa_location_seq_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_LOCATION_SEQ_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_LOCATION_SEQ_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_location_seq_def', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('pa_location_seq_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_LOCATION_SEQ_DEF ON pa_location_seq_def;
CREATE TRIGGER TDA_PA_LOCATION_SEQ_DEF AFTER DELETE ON pa_location_seq_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_LOCATION_SEQ_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_OPERATOR_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_operator_def', 'id', '', NEW.ID, getTableSubSystem('pa_operator_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_OPERATOR_DEF ON pa_operator_def;
CREATE TRIGGER TIA_PA_OPERATOR_DEF AFTER INSERT ON pa_operator_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_OPERATOR_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_OPERATOR_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_operator_def', 'id', OLD.ID, NEW.ID, getTableSubSystem('pa_operator_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_OPERATOR_DEF ON pa_operator_def;
CREATE TRIGGER TUA_PA_OPERATOR_DEF AFTER UPDATE ON pa_operator_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_OPERATOR_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_OPERATOR_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_operator_def', 'id', OLD.ID, '', getTableSubSystem('pa_operator_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_OPERATOR_DEF ON pa_operator_def;
CREATE TRIGGER TDA_PA_OPERATOR_DEF AFTER DELETE ON pa_operator_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_OPERATOR_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_OPERATOR_MAP() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_operator_map', 'user_grp_id', '', NEW.USER_GRP_ID, getTableSubSystem('pa_operator_map'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_OPERATOR_MAP ON pa_operator_map;
CREATE TRIGGER TIA_PA_OPERATOR_MAP AFTER INSERT ON pa_operator_map FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_OPERATOR_MAP();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_OPERATOR_MAP() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_operator_map', 'user_grp_id', OLD.USER_GRP_ID, NEW.USER_GRP_ID, getTableSubSystem('pa_operator_map'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_OPERATOR_MAP ON pa_operator_map;
CREATE TRIGGER TUA_PA_OPERATOR_MAP AFTER UPDATE ON pa_operator_map FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_OPERATOR_MAP();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_OPERATOR_MAP() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_operator_map', 'user_grp_id', OLD.USER_GRP_ID, '', getTableSubSystem('pa_operator_map'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_OPERATOR_MAP ON pa_operator_map;
CREATE TRIGGER TDA_PA_OPERATOR_MAP AFTER DELETE ON pa_operator_map FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_OPERATOR_MAP();



CREATE OR REPLACE FUNCTION FUNC_TIA_PA_TIMED_AUDIO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_timed_audio', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pa_timed_audio'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PA_TIMED_AUDIO ON pa_timed_audio;
CREATE TRIGGER TIA_PA_TIMED_AUDIO AFTER INSERT ON pa_timed_audio FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PA_TIMED_AUDIO();



CREATE OR REPLACE FUNCTION FUNC_TUA_PA_TIMED_AUDIO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_timed_audio', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pa_timed_audio'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PA_TIMED_AUDIO ON pa_timed_audio;
CREATE TRIGGER TUA_PA_TIMED_AUDIO AFTER UPDATE ON pa_timed_audio FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PA_TIMED_AUDIO();



CREATE OR REPLACE FUNCTION FUNC_TDA_PA_TIMED_AUDIO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_timed_audio', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('pa_timed_audio'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PA_TIMED_AUDIO ON pa_timed_audio;
CREATE TRIGGER TDA_PA_TIMED_AUDIO AFTER DELETE ON pa_timed_audio FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PA_TIMED_AUDIO();



CREATE OR REPLACE FUNCTION FUNC_TIA_PIS_AREA_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_area_info', 'id', '', NEW.ID, getTableSubSystem('pis_area_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PIS_AREA_INFO ON pis_area_info;
CREATE TRIGGER TIA_PIS_AREA_INFO AFTER INSERT ON pis_area_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PIS_AREA_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_PIS_AREA_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_area_info', 'id', OLD.ID, NEW.ID, getTableSubSystem('pis_area_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PIS_AREA_INFO ON pis_area_info;
CREATE TRIGGER TUA_PIS_AREA_INFO AFTER UPDATE ON pis_area_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PIS_AREA_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_PIS_AREA_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_area_info', 'id', OLD.ID, '', getTableSubSystem('pis_area_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PIS_AREA_INFO ON pis_area_info;
CREATE TRIGGER TDA_PIS_AREA_INFO AFTER DELETE ON pis_area_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PIS_AREA_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_PIS_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_ctrl_param', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('pis_ctrl_param'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PIS_CTRL_PARAM ON pis_ctrl_param;
CREATE TRIGGER TIA_PIS_CTRL_PARAM AFTER INSERT ON pis_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PIS_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TUA_PIS_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_ctrl_param', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('pis_ctrl_param'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PIS_CTRL_PARAM ON pis_ctrl_param;
CREATE TRIGGER TUA_PIS_CTRL_PARAM AFTER UPDATE ON pis_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PIS_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TDA_PIS_CTRL_PARAM() RETURNS TRIGGER AS $$
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_ctrl_param', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('pis_ctrl_param'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PIS_CTRL_PARAM ON pis_ctrl_param;
CREATE TRIGGER TDA_PIS_CTRL_PARAM AFTER DELETE ON pis_ctrl_param FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PIS_CTRL_PARAM();



CREATE OR REPLACE FUNCTION FUNC_TIA_PIS_DISPLAY_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_display_info', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pis_display_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PIS_DISPLAY_INFO ON pis_display_info;
CREATE TRIGGER TIA_PIS_DISPLAY_INFO AFTER INSERT ON pis_display_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PIS_DISPLAY_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_PIS_DISPLAY_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_display_info', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pis_display_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PIS_DISPLAY_INFO ON pis_display_info;
CREATE TRIGGER TUA_PIS_DISPLAY_INFO AFTER UPDATE ON pis_display_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PIS_DISPLAY_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_PIS_DISPLAY_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_display_info', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('pis_display_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PIS_DISPLAY_INFO ON pis_display_info;
CREATE TRIGGER TDA_PIS_DISPLAY_INFO AFTER DELETE ON pis_display_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PIS_DISPLAY_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_PIS_FES_LOCATION_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_fes_location_info', 'pis_station_id', '', NEW.PIS_STATION_ID, getTableSubSystem('pis_fes_location_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PIS_FES_LOCATION_INFO ON pis_fes_location_info;
CREATE TRIGGER TIA_PIS_FES_LOCATION_INFO AFTER INSERT ON pis_fes_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PIS_FES_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_PIS_FES_LOCATION_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_fes_location_info', 'pis_station_id', OLD.PIS_STATION_ID, NEW.PIS_STATION_ID, getTableSubSystem('pis_fes_location_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PIS_FES_LOCATION_INFO ON pis_fes_location_info;
CREATE TRIGGER TUA_PIS_FES_LOCATION_INFO AFTER UPDATE ON pis_fes_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PIS_FES_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_PIS_FES_LOCATION_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_fes_location_info', 'pis_station_id', OLD.PIS_STATION_ID, '', getTableSubSystem('pis_fes_location_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PIS_FES_LOCATION_INFO ON pis_fes_location_info;
CREATE TRIGGER TDA_PIS_FES_LOCATION_INFO AFTER DELETE ON pis_fes_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PIS_FES_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_PIS_LOCATION_AREA_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_location_area_def', 'location_id,area_id', '', concat(NEW.LOCATION_ID,',',NEW.AREA_ID), getTableSubSystem('pis_location_area_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PIS_LOCATION_AREA_DEF ON pis_location_area_def;
CREATE TRIGGER TIA_PIS_LOCATION_AREA_DEF AFTER INSERT ON pis_location_area_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PIS_LOCATION_AREA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_PIS_LOCATION_AREA_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_location_area_def', 'location_id,area_id', concat(OLD.LOCATION_ID,',',OLD.AREA_ID), concat(NEW.LOCATION_ID,',',NEW.AREA_ID), getTableSubSystem('pis_location_area_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PIS_LOCATION_AREA_DEF ON pis_location_area_def;
CREATE TRIGGER TUA_PIS_LOCATION_AREA_DEF AFTER UPDATE ON pis_location_area_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PIS_LOCATION_AREA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_PIS_LOCATION_AREA_DEF() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_location_area_def', 'location_id,area_id', concat(OLD.LOCATION_ID,',',OLD.AREA_ID), '', getTableSubSystem('pis_location_area_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PIS_LOCATION_AREA_DEF ON pis_location_area_def;
CREATE TRIGGER TDA_PIS_LOCATION_AREA_DEF AFTER DELETE ON pis_location_area_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PIS_LOCATION_AREA_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_PIS_MESSAGE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_message_info', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pis_message_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_PIS_MESSAGE_INFO ON pis_message_info;
CREATE TRIGGER TIA_PIS_MESSAGE_INFO AFTER INSERT ON pis_message_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_PIS_MESSAGE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_PIS_MESSAGE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_message_info', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pis_message_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_PIS_MESSAGE_INFO ON pis_message_info;
CREATE TRIGGER TUA_PIS_MESSAGE_INFO AFTER UPDATE ON pis_message_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_PIS_MESSAGE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_PIS_MESSAGE_INFO() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_message_info', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('pis_message_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_PIS_MESSAGE_INFO ON pis_message_info;
CREATE TRIGGER TDA_PIS_MESSAGE_INFO AFTER DELETE ON pis_message_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_PIS_MESSAGE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_SCL_DATASET() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_dataset', 'id', '', NEW.id, getTableSubSystem('scl_dataset'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SCL_DATASET ON scl_dataset;
CREATE TRIGGER TIA_SCL_DATASET AFTER INSERT ON scl_dataset FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SCL_DATASET();



CREATE OR REPLACE FUNCTION FUNC_TUA_SCL_DATASET() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_dataset', 'id', OLD.id, NEW.id, getTableSubSystem('scl_dataset'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SCL_DATASET ON scl_dataset;
CREATE TRIGGER TUA_SCL_DATASET AFTER UPDATE ON scl_dataset FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SCL_DATASET();



CREATE OR REPLACE FUNCTION FUNC_TDA_SCL_DATASET() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_dataset', 'id', OLD.id, '', getTableSubSystem('scl_dataset'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SCL_DATASET ON scl_dataset;
CREATE TRIGGER TDA_SCL_DATASET AFTER DELETE ON scl_dataset FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SCL_DATASET();



CREATE OR REPLACE FUNCTION FUNC_TIA_SCL_FCDA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_fcda', 'id', '', NEW.id, getTableSubSystem('scl_fcda'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SCL_FCDA ON scl_fcda;
CREATE TRIGGER TIA_SCL_FCDA AFTER INSERT ON scl_fcda FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SCL_FCDA();



CREATE OR REPLACE FUNCTION FUNC_TUA_SCL_FCDA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_fcda', 'id', OLD.id, NEW.id, getTableSubSystem('scl_fcda'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SCL_FCDA ON scl_fcda;
CREATE TRIGGER TUA_SCL_FCDA AFTER UPDATE ON scl_fcda FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SCL_FCDA();



CREATE OR REPLACE FUNCTION FUNC_TDA_SCL_FCDA() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_fcda', 'id', OLD.id, '', getTableSubSystem('scl_fcda'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SCL_FCDA ON scl_fcda;
CREATE TRIGGER TDA_SCL_FCDA AFTER DELETE ON scl_fcda FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SCL_FCDA();



CREATE OR REPLACE FUNCTION FUNC_TIA_SCL_LCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_lcb', 'id', '', NEW.id, getTableSubSystem('scl_lcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SCL_LCB ON scl_lcb;
CREATE TRIGGER TIA_SCL_LCB AFTER INSERT ON scl_lcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SCL_LCB();



CREATE OR REPLACE FUNCTION FUNC_TUA_SCL_LCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_lcb', 'id', OLD.id, NEW.id, getTableSubSystem('scl_lcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SCL_LCB ON scl_lcb;
CREATE TRIGGER TUA_SCL_LCB AFTER UPDATE ON scl_lcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SCL_LCB();



CREATE OR REPLACE FUNCTION FUNC_TDA_SCL_LCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_lcb', 'id', OLD.id, '', getTableSubSystem('scl_lcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SCL_LCB ON scl_lcb;
CREATE TRIGGER TDA_SCL_LCB AFTER DELETE ON scl_lcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SCL_LCB();



CREATE OR REPLACE FUNCTION FUNC_TIA_SCL_RCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_rcb', 'id', '', NEW.id, getTableSubSystem('scl_rcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SCL_RCB ON scl_rcb;
CREATE TRIGGER TIA_SCL_RCB AFTER INSERT ON scl_rcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SCL_RCB();



CREATE OR REPLACE FUNCTION FUNC_TUA_SCL_RCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_rcb', 'id', OLD.id, NEW.id, getTableSubSystem('scl_rcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SCL_RCB ON scl_rcb;
CREATE TRIGGER TUA_SCL_RCB AFTER UPDATE ON scl_rcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SCL_RCB();



CREATE OR REPLACE FUNCTION FUNC_TDA_SCL_RCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_rcb', 'id', OLD.id, '', getTableSubSystem('scl_rcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SCL_RCB ON scl_rcb;
CREATE TRIGGER TDA_SCL_RCB AFTER DELETE ON scl_rcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SCL_RCB();



CREATE OR REPLACE FUNCTION FUNC_TIA_SCL_RUNTIME() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_runtime', 'id', '', NEW.id, getTableSubSystem('scl_runtime'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SCL_RUNTIME ON scl_runtime;
CREATE TRIGGER TIA_SCL_RUNTIME AFTER INSERT ON scl_runtime FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SCL_RUNTIME();



CREATE OR REPLACE FUNCTION FUNC_TUA_SCL_RUNTIME() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_runtime', 'id', OLD.id, NEW.id, getTableSubSystem('scl_runtime'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SCL_RUNTIME ON scl_runtime;
CREATE TRIGGER TUA_SCL_RUNTIME AFTER UPDATE ON scl_runtime FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SCL_RUNTIME();



CREATE OR REPLACE FUNCTION FUNC_TDA_SCL_RUNTIME() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_runtime', 'id', OLD.id, '', getTableSubSystem('scl_runtime'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SCL_RUNTIME ON scl_runtime;
CREATE TRIGGER TDA_SCL_RUNTIME AFTER DELETE ON scl_runtime FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SCL_RUNTIME();



CREATE OR REPLACE FUNCTION FUNC_TIA_SCL_SGCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_sgcb', 'id', '', NEW.id, getTableSubSystem('scl_sgcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SCL_SGCB ON scl_sgcb;
CREATE TRIGGER TIA_SCL_SGCB AFTER INSERT ON scl_sgcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SCL_SGCB();



CREATE OR REPLACE FUNCTION FUNC_TUA_SCL_SGCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_sgcb', 'id', OLD.id, NEW.id, getTableSubSystem('scl_sgcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SCL_SGCB ON scl_sgcb;
CREATE TRIGGER TUA_SCL_SGCB AFTER UPDATE ON scl_sgcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SCL_SGCB();



CREATE OR REPLACE FUNCTION FUNC_TDA_SCL_SGCB() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_sgcb', 'id', OLD.id, '', getTableSubSystem('scl_sgcb'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SCL_SGCB ON scl_sgcb;
CREATE TRIGGER TDA_SCL_SGCB AFTER DELETE ON scl_sgcb FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SCL_SGCB();



CREATE OR REPLACE FUNCTION FUNC_TI_SEQUENCE_ACTION() RETURNS TRIGGER AS $$
BEGIN
    NEW.TIMEFLAG = floor(extract(epoch from now()));
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_SEQUENCE_ACTION ON sequence_action;
CREATE TRIGGER TI_SEQUENCE_ACTION BEFORE INSERT ON sequence_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_SEQUENCE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TIA_SEQUENCE_ACTION() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'sequence_action', 'name', '', NEW.NAME, getTableSubSystem('sequence_action'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SEQUENCE_ACTION ON sequence_action;
CREATE TRIGGER TIA_SEQUENCE_ACTION AFTER INSERT ON sequence_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SEQUENCE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TU_SEQUENCE_ACTION() RETURNS TRIGGER AS $$
BEGIN
    --修改对应功能
    NEW.TIMEFLAG = floor(extract(epoch from now()));
    UPDATE sequence_function SET 
    TIMEFLAG = NEW.TIMEFLAG,
    ACTION_NAME = NEW.NAME
    WHERE ACTION_NAME = OLD.NAME;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_SEQUENCE_ACTION ON sequence_action;
CREATE TRIGGER TU_SEQUENCE_ACTION BEFORE UPDATE ON sequence_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_SEQUENCE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TUA_SEQUENCE_ACTION() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'sequence_action', 'name', OLD.NAME, NEW.NAME, getTableSubSystem('sequence_action'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SEQUENCE_ACTION ON sequence_action;
CREATE TRIGGER TUA_SEQUENCE_ACTION AFTER UPDATE ON sequence_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SEQUENCE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TD_SEQUENCE_ACTION() RETURNS TRIGGER AS $$
BEGIN
    --删除关联了此动作的功能
    DELETE FROM sequence_function WHERE ACTION_NAME = OLD.NAME; 
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'sequence_action', 'name', OLD.NAME, '', getTableSubSystem('sequence_action'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_SEQUENCE_ACTION ON sequence_action;
CREATE TRIGGER TD_SEQUENCE_ACTION AFTER DELETE ON sequence_action FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_SEQUENCE_ACTION();



CREATE OR REPLACE FUNCTION FUNC_TI_SEQUENCE_DEFINE() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --仅仅修改时标
    --避免触发器循环
    SET SESSION "disable.SequenceSettingTrigger" = 1;
    
    CALL kbdDebugInfo('sequence_define Insert','sequence_define Insert');
    
	SELECT CURRENT_SETTING('disable.SequenceDefineTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        NEW.TIMEFLAG = floor(extract(epoch from now()));
        UPDATE sequence_setting SET 
        TIMEFLAG = NEW.TIMEFLAG
        WHERE NAME = NEW.SEQ_NAME;
    END;
    END IF;
    
    SET SESSION "disable.SequenceSettingTrigger" = 0;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_SEQUENCE_DEFINE ON sequence_define;
CREATE TRIGGER TI_SEQUENCE_DEFINE BEFORE INSERT ON sequence_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_SEQUENCE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_SEQUENCE_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'sequence_define', 'seq_name,func_name', '', concat(NEW.SEQ_NAME,',',NEW.FUNC_NAME), getTableSubSystem('sequence_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SEQUENCE_DEFINE ON sequence_define;
CREATE TRIGGER TIA_SEQUENCE_DEFINE AFTER INSERT ON sequence_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SEQUENCE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TU_SEQUENCE_DEFINE() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    
    --修改对应联动功能关系表
    --避免触发器循环
    SET SESSION "disable.SequenceSettingTrigger" = 1;
    
    CALL kbdDebugInfo('sequence_define update','sequence_define update');
    
	SELECT CURRENT_SETTING('disable.SequenceDefineTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        NEW.TIMEFLAG = floor(extract(epoch from now()));
        UPDATE sequence_setting SET 
        TIMEFLAG = NEW.TIMEFLAG
        WHERE NAME = OLD.SEQ_NAME;
    END;
    END IF;
    
    SET SESSION "disable.SequenceSettingTrigger" = 0;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_SEQUENCE_DEFINE ON sequence_define;
CREATE TRIGGER TU_SEQUENCE_DEFINE BEFORE UPDATE ON sequence_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_SEQUENCE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_SEQUENCE_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'sequence_define', 'seq_name,func_name', concat(OLD.SEQ_NAME,',',OLD.FUNC_NAME), concat(NEW.SEQ_NAME,',',NEW.FUNC_NAME), getTableSubSystem('sequence_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SEQUENCE_DEFINE ON sequence_define;
CREATE TRIGGER TUA_SEQUENCE_DEFINE AFTER UPDATE ON sequence_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SEQUENCE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TD_SEQUENCE_DEFINE() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --仅仅修改时标
    --避免触发器循环
    SET SESSION "disable.SequenceSettingTrigger" = 1;
    
    CALL kbdDebugInfo('sequence_define delete','sequence_define delete');
    
	SELECT CURRENT_SETTING('disable.SequenceDefineTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        UPDATE sequence_setting SET 
        TIMEFLAG = floor(extract(epoch from now()))
        WHERE NAME = OLD.SEQ_NAME;
    END;
    END IF;
    
    SET SESSION "disable.SequenceSettingTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'sequence_define', 'seq_name,func_name', concat(OLD.SEQ_NAME,',',OLD.FUNC_NAME), '', getTableSubSystem('sequence_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_SEQUENCE_DEFINE ON sequence_define;
CREATE TRIGGER TD_SEQUENCE_DEFINE AFTER DELETE ON sequence_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_SEQUENCE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TI_SEQUENCE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
    --仅仅修改时标
    NEW.TIMEFLAG = floor(extract(epoch from now()));
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_SEQUENCE_FUNCTION ON sequence_function;
CREATE TRIGGER TI_SEQUENCE_FUNCTION BEFORE INSERT ON sequence_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_SEQUENCE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TIA_SEQUENCE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
    UPDATE sequence_define SET 
    TIMEFLAG = NEW.TIMEFLAG
    WHERE FUNC_NAME = NEW.NAME;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'sequence_function', 'name,action_name', '', concat(NEW.NAME,',',NEW.ACTION_NAME), getTableSubSystem('sequence_function'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SEQUENCE_FUNCTION ON sequence_function;
CREATE TRIGGER TIA_SEQUENCE_FUNCTION AFTER INSERT ON sequence_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SEQUENCE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TU_SEQUENCE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
    --修改对应联动功能关系表
    NEW.TIMEFLAG = floor(extract(epoch from now()));
    UPDATE sequence_define SET 
    TIMEFLAG = NEW.TIMEFLAG,
    FUNC_NAME = NEW.NAME
    WHERE FUNC_NAME = OLD.NAME;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_SEQUENCE_FUNCTION ON sequence_function;
CREATE TRIGGER TU_SEQUENCE_FUNCTION BEFORE UPDATE ON sequence_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_SEQUENCE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TUA_SEQUENCE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'sequence_function', 'name,action_name', concat(OLD.NAME,',',OLD.ACTION_NAME), concat(NEW.NAME,',',NEW.ACTION_NAME), getTableSubSystem('sequence_function'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SEQUENCE_FUNCTION ON sequence_function;
CREATE TRIGGER TUA_SEQUENCE_FUNCTION AFTER UPDATE ON sequence_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SEQUENCE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TD_SEQUENCE_FUNCTION() RETURNS TRIGGER AS $$
BEGIN
    --删除关联了此功能的联动定义
    --检查功能是否被删除
    IF NOT EXISTS (SELECT * FROM sequence_function WHERE NAME = OLD.NAME) THEN 
    BEGIN
        DELETE FROM sequence_define WHERE FUNC_NAME = OLD.NAME; 
    END;
    ELSE 
    BEGIN
        --修改时标
        UPDATE sequence_define SET
        TIMEFLAG = floor(extract(epoch from now()))
        WHERE FUNC_NAME = OLD.NAME;
    END; 
    END IF;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'sequence_function', 'name,action_name', concat(OLD.NAME,',',OLD.ACTION_NAME), '', getTableSubSystem('sequence_function'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_SEQUENCE_FUNCTION ON sequence_function;
CREATE TRIGGER TD_SEQUENCE_FUNCTION AFTER DELETE ON sequence_function FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_SEQUENCE_FUNCTION();



CREATE OR REPLACE FUNCTION FUNC_TI_SEQUENCE_SETTING() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --仅仅修改时标
    --避免触发器循环
    SET SESSION "disable.SequenceDefineTrigger" = 1;
    
    CALL kbdDebugInfo('sequence_setting insert','sequence_setting insert');
    
	SELECT CURRENT_SETTING('disable.SequenceSettingTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        NEW.TIMEFLAG = floor(extract(epoch from now()));
    END;
    END IF;
    
    SET SESSION "disable.SequenceDefineTrigger" = 0;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_SEQUENCE_SETTING ON sequence_setting;
CREATE TRIGGER TI_SEQUENCE_SETTING BEFORE INSERT ON sequence_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_SEQUENCE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TIA_SEQUENCE_SETTING() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'sequence_setting', 'name', '', NEW.NAME, getTableSubSystem('sequence_setting'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SEQUENCE_SETTING ON sequence_setting;
CREATE TRIGGER TIA_SEQUENCE_SETTING AFTER INSERT ON sequence_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SEQUENCE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TU_SEQUENCE_SETTING() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --修改对应联动功能关系表
    --避免触发器循环
    SET SESSION "disable.SequenceDefineTrigger" = 1;
    
    CALL kbdDebugInfo('sequence_setting update','sequence_setting update');
    
	SELECT CURRENT_SETTING('disable.SequenceSettingTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        NEW.TIMEFLAG = floor(extract(epoch from now()));
        IF NEW.NAME<>OLD.NAME THEN 
            UPDATE sequence_define SET 
            SEQ_NAME = NEW.NAME
            WHERE SEQ_NAME = OLD.NAME;
       END IF;
    END;
    END IF;
    
    SET SESSION "disable.SequenceDefineTrigger" = 0;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_SEQUENCE_SETTING ON sequence_setting;
CREATE TRIGGER TU_SEQUENCE_SETTING BEFORE UPDATE ON sequence_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_SEQUENCE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TUA_SEQUENCE_SETTING() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'sequence_setting', 'name', OLD.NAME, NEW.NAME, getTableSubSystem('sequence_setting'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SEQUENCE_SETTING ON sequence_setting;
CREATE TRIGGER TUA_SEQUENCE_SETTING AFTER UPDATE ON sequence_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SEQUENCE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TD_SEQUENCE_SETTING() RETURNS TRIGGER AS $$
DECLARE flag int;
BEGIN
    --删除关联了此联动的功能关联
    --避免触发器循环
    SET SESSION "disable.SequenceDefineTrigger" = 1;
    
    CALL kbdDebugInfo('sequence_setting delete','sequence_setting delete');
    
	SELECT CURRENT_SETTING('disable.SequenceSettingTrigger') INTO flag;
    IF flag = 0 THEN
    BEGIN
        DELETE FROM sequence_define WHERE SEQ_NAME = OLD.NAME;
    END;
    END IF;
    
    SET SESSION "disable.SequenceDefineTrigger" = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'sequence_setting', 'name', OLD.NAME, '', getTableSubSystem('sequence_setting'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_SEQUENCE_SETTING ON sequence_setting;
CREATE TRIGGER TD_SEQUENCE_SETTING AFTER DELETE ON sequence_setting FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_SEQUENCE_SETTING();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_INST_DEF() RETURNS TRIGGER AS $$
BEGIN
    
    --插入测点分组表
    --插入测点表
    --插入时间测点变
    --插入返回分组表
    --插入返回参数表
    INSERT INTO stat_inst_para_grp 
    (PARA_GRP_TAG,                                        DESCRIPTION,  INST_TAG,      SORT,LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG)) ,  DESCRIPTION,  NEW.INST_TAG,  SORT,NEW.LOCATION_ID,NEW.SUB_SYSTEM 
    FROM stat_template_para WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 1;
    INSERT INTO stat_inst_para_point 
    (PARA_TAG,                                         DESCRIPTION, RANK, POINT_TAG, SORT, PARA_GRP_TAG, INST_TAG,    LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG)),DESCRIPTION,  1,     '',        SORT, '',           NEW.INST_TAG,NEW.LOCATION_ID,NEW.SUB_SYSTEM
    FROM stat_template_para WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 0 AND COALESCE(TYPE,0) = 1;
    
    INSERT INTO stat_inst_para_time 
    (PARA_TAG,                                         DESCRIPTION, RANK, TIMESTAMP,           SORT, PARA_GRP_TAG, INST_TAG,    TIME_MODE, YEAR_R, MONTH_R, DAY_R, WEEKDAY_R, HOUR_R, MINUTE_R, SECOND_R,LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG)),DESCRIPTION, 1,       floor(extract(epoch from now())),SORT, '',           NEW.INST_TAG, 0,        0,      0,       0,      0,         0,      0,        0,      NEW.LOCATION_ID,NEW.SUB_SYSTEM
    FROM stat_template_para WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 0 AND COALESCE(TYPE,0) = 2;
    
    INSERT INTO stat_inst_ret_grp 
    (RET_GRP_TAG,                                     DESCRIPTION, INST_TAG,    SORT,LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(RET_TAG)),DESCRIPTION, NEW.INST_TAG,SORT,NEW.LOCATION_ID,NEW.SUB_SYSTEM
    FROM stat_template_ret WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 1;
    
    INSERT INTO stat_inst_ret_para 
    (RET_TAG,                                         DESCRIPTION, RANK, POINT_TAG, SORT, PARA_GRP_TAG, VALUE, IS_BIND, INST_TAG,   LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(RET_TAG)),DESCRIPTION,  1,      '',       SORT,  '',          0,      0,      NEW.INST_TAG,NEW.LOCATION_ID,NEW.SUB_SYSTEM          
    FROM stat_template_ret WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 0;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_def', 'inst_tag', '', NEW.INST_TAG, getTableSubSystem('stat_inst_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_INST_DEF ON stat_inst_def;
CREATE TRIGGER TIA_STAT_INST_DEF AFTER INSERT ON stat_inst_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_INST_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_INST_DEF() RETURNS TRIGGER AS $$
BEGIN
    IF NEW.INST_TAG <> OLD.INST_TAG THEN 
    BEGIN
        UPDATE stat_inst_para_grp
        SET INST_TAG = NEW.INST_TAG,
        PARA_GRP_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_GRP_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
        
        UPDATE stat_inst_para_point
        SET INST_TAG = NEW.INST_TAG,
        PARA_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
        
        UPDATE stat_inst_para_time
        SET INST_TAG = NEW.INST_TAG,
        PARA_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
    
        UPDATE stat_inst_ret_grp
        SET INST_TAG = NEW.INST_TAG,
        RET_GRP_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(RET_GRP_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
        
        UPDATE stat_inst_ret_para
        SET INST_TAG = NEW.INST_TAG,
        RET_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(RET_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
    
    END;
    END IF;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_def', 'inst_tag', OLD.INST_TAG, NEW.INST_TAG, getTableSubSystem('stat_inst_def'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_INST_DEF ON stat_inst_def;
CREATE TRIGGER TUA_STAT_INST_DEF AFTER UPDATE ON stat_inst_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_INST_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_INST_DEF() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM stat_inst_para_grp WHERE INST_TAG = OLD.INST_TAG;
    DELETE FROM stat_inst_para_point WHERE INST_TAG = OLD.INST_TAG;
    DELETE FROM stat_inst_para_time WHERE INST_TAG = OLD.INST_TAG;
    DELETE FROM stat_inst_ret_grp WHERE INST_TAG = OLD.INST_TAG;
    DELETE FROM stat_inst_ret_para WHERE INST_TAG = OLD.INST_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_def', 'inst_tag', OLD.INST_TAG, '', getTableSubSystem('stat_inst_def'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_INST_DEF ON stat_inst_def;
CREATE TRIGGER TDA_STAT_INST_DEF AFTER DELETE ON stat_inst_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_INST_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_INST_PARA_GRP() RETURNS TRIGGER AS $$
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,         KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_para_grp', 'para_grp_tag', '',        NEW.PARA_GRP_TAG, getTableSubSystem('stat_inst_para_grp'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_INST_PARA_GRP ON stat_inst_para_grp;
CREATE TRIGGER TIA_STAT_INST_PARA_GRP AFTER INSERT ON stat_inst_para_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_INST_PARA_GRP();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_INST_PARA_GRP() RETURNS TRIGGER AS $$
BEGIN
    IF NEW.PARA_GRP_TAG = OLD.PARA_GRP_TAG
    THEN RETURN NEW;
    END IF;
    
    UPDATE stat_inst_para_point
    SET PARA_GRP_TAG = NEW.PARA_GRP_TAG
    WHERE PARA_GRP_TAG = OLD.PARA_GRP_TAG;
    
    UPDATE stat_inst_para_time
    SET PARA_GRP_TAG = NEW.PARA_GRP_TAG
    WHERE PARA_GRP_TAG = OLD.PARA_GRP_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,     KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_para_grp', 'para_grp_tag', OLD.PARA_GRP_TAG, NEW.PARA_GRP_TAG, getTableSubSystem('stat_inst_para_grp'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_INST_PARA_GRP ON stat_inst_para_grp;
CREATE TRIGGER TUA_STAT_INST_PARA_GRP AFTER UPDATE ON stat_inst_para_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_INST_PARA_GRP();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_INST_PARA_GRP() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM stat_inst_para_point WHERE PARA_GRP_TAG = OLD.PARA_GRP_TAG;
    DELETE FROM stat_inst_para_time WHERE PARA_GRP_TAG = OLD.PARA_GRP_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,         KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_para_grp', 'para_grp_tag', OLD.PARA_GRP_TAG, '', getTableSubSystem('stat_inst_para_grp'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_INST_PARA_GRP ON stat_inst_para_grp;
CREATE TRIGGER TDA_STAT_INST_PARA_GRP AFTER DELETE ON stat_inst_para_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_INST_PARA_GRP();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_INST_PARA_POINT() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_para_point', 'para_tag', '',        NEW.PARA_TAG, getTableSubSystem('stat_inst_para_point'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_INST_PARA_POINT ON stat_inst_para_point;
CREATE TRIGGER TIA_STAT_INST_PARA_POINT AFTER INSERT ON stat_inst_para_point FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_INST_PARA_POINT();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_INST_PARA_POINT() RETURNS TRIGGER AS $$
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_para_point', 'para_tag', OLD.PARA_TAG,   NEW.PARA_TAG, getTableSubSystem('stat_inst_para_point'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_INST_PARA_POINT ON stat_inst_para_point;
CREATE TRIGGER TUA_STAT_INST_PARA_POINT AFTER UPDATE ON stat_inst_para_point FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_INST_PARA_POINT();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_INST_PARA_POINT() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_para_point', 'para_tag', OLD.PARA_TAG, '', getTableSubSystem('stat_inst_para_point'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_INST_PARA_POINT ON stat_inst_para_point;
CREATE TRIGGER TDA_STAT_INST_PARA_POINT AFTER DELETE ON stat_inst_para_point FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_INST_PARA_POINT();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_INST_PARA_TIME() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_para_time', 'para_tag', '',        NEW.PARA_TAG, getTableSubSystem('stat_inst_para_time'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_INST_PARA_TIME ON stat_inst_para_time;
CREATE TRIGGER TIA_STAT_INST_PARA_TIME AFTER INSERT ON stat_inst_para_time FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_INST_PARA_TIME();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_INST_PARA_TIME() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_para_time', 'para_tag', OLD.PARA_TAG,   NEW.PARA_TAG, getTableSubSystem('stat_inst_para_time'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_INST_PARA_TIME ON stat_inst_para_time;
CREATE TRIGGER TUA_STAT_INST_PARA_TIME AFTER UPDATE ON stat_inst_para_time FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_INST_PARA_TIME();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_INST_PARA_TIME() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_para_time', 'para_tag', OLD.PARA_TAG, '', getTableSubSystem('stat_inst_para_time'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_INST_PARA_TIME ON stat_inst_para_time;
CREATE TRIGGER TDA_STAT_INST_PARA_TIME AFTER DELETE ON stat_inst_para_time FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_INST_PARA_TIME();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_INST_RET_GRP() RETURNS TRIGGER AS $$
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_ret_grp', 'ret_grp_tag', '',        NEW.RET_GRP_TAG, getTableSubSystem('stat_inst_ret_grp'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_INST_RET_GRP ON stat_inst_ret_grp;
CREATE TRIGGER TIA_STAT_INST_RET_GRP AFTER INSERT ON stat_inst_ret_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_INST_RET_GRP();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_INST_RET_GRP() RETURNS TRIGGER AS $$
BEGIN
    
    IF NEW.RET_GRP_TAG = OLD.RET_GRP_TAG
    THEN RETURN NEW;
    END IF;
    
    UPDATE stat_inst_ret_para 
    SET PARA_GRP_TAG = NEW.RET_GRP_TAG
    WHERE PARA_GRP_TAG = OLD.RET_GRP_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_ret_grp', 'ret_grp_tag', OLD.RET_GRP_TAG, NEW.RET_GRP_TAG, getTableSubSystem('stat_inst_ret_grp'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_INST_RET_GRP ON stat_inst_ret_grp;
CREATE TRIGGER TUA_STAT_INST_RET_GRP AFTER UPDATE ON stat_inst_ret_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_INST_RET_GRP();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_INST_RET_GRP() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM stat_inst_ret_para WHERE PARA_GRP_TAG = OLD.RET_GRP_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_ret_grp', 'ret_grp_tag', OLD.RET_GRP_TAG, '', getTableSubSystem('stat_inst_ret_grp'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_INST_RET_GRP ON stat_inst_ret_grp;
CREATE TRIGGER TDA_STAT_INST_RET_GRP AFTER DELETE ON stat_inst_ret_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_INST_RET_GRP();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_INST_RET_PARA() RETURNS TRIGGER AS $$
BEGIN
   
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_ret_para', 'ret_tag', '',        NEW.RET_TAG, getTableSubSystem('stat_inst_ret_para'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_INST_RET_PARA ON stat_inst_ret_para;
CREATE TRIGGER TIA_STAT_INST_RET_PARA AFTER INSERT ON stat_inst_ret_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_INST_RET_PARA();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_INST_RET_PARA() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_ret_para', 'ret_tag', OLD.RET_TAG, NEW.RET_TAG, getTableSubSystem('stat_inst_ret_para'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_INST_RET_PARA ON stat_inst_ret_para;
CREATE TRIGGER TUA_STAT_INST_RET_PARA AFTER UPDATE ON stat_inst_ret_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_INST_RET_PARA();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_INST_RET_PARA() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_ret_para', 'ret_tag', OLD.RET_TAG, '', getTableSubSystem('stat_inst_ret_para'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_INST_RET_PARA ON stat_inst_ret_para;
CREATE TRIGGER TDA_STAT_INST_RET_PARA AFTER DELETE ON stat_inst_ret_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_INST_RET_PARA();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_LINK_DEF() RETURNS TRIGGER AS $$
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_link_def', 'tag_name', '',        NEW.TAG_NAME, getTableSubSystem('STAT_LINK_DEF'));
    END IF; 
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_LINK_DEF ON STAT_LINK_DEF;
CREATE TRIGGER TIA_STAT_LINK_DEF AFTER INSERT ON STAT_LINK_DEF FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_LINK_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_LINK_DEF() RETURNS TRIGGER AS $$
BEGIN
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN 
    BEGIN
         UPDATE stat_link_para
         SET LINK_TAG = NEW.TAG_NAME,
         TAG_NAME = concat(NEW.TAG_NAME,'.',getAfterLastDot(TAG_NAME))
         WHERE LINK_TAG = OLD.TAG_NAME;
        UPDATE stat_link_ret
        SET LINK_TAG = NEW.TAG_NAME,
        TAG_NAME = concat(NEW.TAG_NAME,'.',getAfterLastDot(TAG_NAME))
        WHERE LINK_TAG = OLD.TAG_NAME;
    
    END;
    END IF;
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_link_def', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('stat_link_def'));
    END IF;
    UPDATE stat_link_para
    SET STAT_PARA_TAG = concat(NEW.STAT_TEMP_TAG,'.',getAfterLastDot(STAT_PARA_TAG))
    WHERE LINK_TAG = OLD.TAG_NAME;
    UPDATE stat_link_ret
    SET STAT_RET_TAG = concat(NEW.STAT_TEMP_TAG,'.',getAfterLastDot(STAT_RET_TAG))
    WHERE LINK_TAG = OLD.TAG_NAME;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_LINK_DEF ON stat_link_def;
CREATE TRIGGER TUA_STAT_LINK_DEF AFTER UPDATE ON stat_link_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_LINK_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_LINK_DEF() RETURNS TRIGGER AS $$
BEGIN
    --删除对应的模板点
    DELETE FROM stat_link_para WHERE LINK_TAG= OLD.TAG_NAME;
    DELETE FROM stat_link_ret WHERE LINK_TAG = OLD.TAG_NAME;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_link_def', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('stat_link_def'));
    END IF;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_LINK_DEF ON stat_link_def;
CREATE TRIGGER TDA_STAT_LINK_DEF AFTER DELETE ON stat_link_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_LINK_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_LINK_PARA() RETURNS TRIGGER AS $$
BEGIN
   
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_link_para', 'tag_name', '',        NEW.TAG_NAME, getTableSubSystem('stat_link_para'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_LINK_PARA ON stat_link_para;
CREATE TRIGGER TIA_STAT_LINK_PARA AFTER INSERT ON stat_link_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_LINK_PARA();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_LINK_PARA() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_link_para', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('stat_link_para'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_LINK_PARA ON stat_link_para;
CREATE TRIGGER TUA_STAT_LINK_PARA AFTER UPDATE ON stat_link_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_LINK_PARA();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_LINK_PARA() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_link_para', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('stat_link_para'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_LINK_PARA ON stat_link_para;
CREATE TRIGGER TDA_STAT_LINK_PARA AFTER DELETE ON stat_link_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_LINK_PARA();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_LINK_RET() RETURNS TRIGGER AS $$
BEGIN
   
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_link_ret', 'tag_name', '',        NEW.TAG_NAME, getTableSubSystem('stat_link_ret'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_LINK_RET ON stat_link_ret;
CREATE TRIGGER TIA_STAT_LINK_RET AFTER INSERT ON stat_link_ret FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_LINK_RET();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_LINK_RET() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_link_ret', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('stat_link_ret'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_LINK_RET ON stat_link_ret;
CREATE TRIGGER TUA_STAT_LINK_RET AFTER UPDATE ON stat_link_ret FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_LINK_RET();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_LINK_RET() RETURNS TRIGGER AS $$
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_link_ret', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('stat_link_ret'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_LINK_RET ON stat_link_ret;
CREATE TRIGGER TDA_STAT_LINK_RET AFTER DELETE ON stat_link_ret FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_LINK_RET();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_TEMPLATE_DEF() RETURNS TRIGGER AS $$
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_template_def', 'template_tag', '',        NEW.TEMPLATE_TAG, getTableSubSystem('stat_template_def'));
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_TEMPLATE_DEF ON stat_template_def;
CREATE TRIGGER TIA_STAT_TEMPLATE_DEF AFTER INSERT ON stat_template_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_TEMPLATE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_TEMPLATE_DEF() RETURNS TRIGGER AS $$
BEGIN
    UPDATE stat_template_para
    SET TEMPLATE_TAG = NEW.TEMPLATE_TAG,
    PARA_TAG = concat(NEW.TEMPLATE_TAG,'.',getAfterLastDot(PARA_TAG))
    WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    
    UPDATE stat_template_ret
    SET TEMPLATE_TAG = NEW.TEMPLATE_TAG,
    RET_TAG = concat(NEW.TEMPLATE_TAG,'.',getAfterLastDot(RET_TAG))
    WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    
    UPDATE stat_inst_def
    SET TEMPLATE_TAG = NEW.TEMPLATE_TAG
    WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    UPDATE stat_link_def
    SET STAT_TEMP_TAG = NEW.TEMPLATE_TAG
    WHERE STAT_TEMP_TAG = OLD.TEMPLATE_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_template_def', 'template_tag', OLD.TEMPLATE_TAG, NEW.TEMPLATE_TAG, getTableSubSystem('stat_template_def'));
    END IF;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_TEMPLATE_DEF ON stat_template_def;
CREATE TRIGGER TUA_STAT_TEMPLATE_DEF AFTER UPDATE ON stat_template_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_TEMPLATE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_TEMPLATE_DEF() RETURNS TRIGGER AS $$
BEGIN
    --删除对应关联
    DELETE FROM stat_link_def WHERE STAT_TEMP_TAG = OLD.TEMPLATE_TAG;
    
    --删除对应实例
    DELETE FROM stat_inst_def WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    
    --删除对应的模板点
    DELETE FROM stat_template_para WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    DELETE FROM stat_template_ret WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_template_def', 'template_tag', OLD.TEMPLATE_TAG, '', getTableSubSystem('stat_template_def'));
    END IF;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_TEMPLATE_DEF ON stat_template_def;
CREATE TRIGGER TDA_STAT_TEMPLATE_DEF AFTER DELETE ON stat_template_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_TEMPLATE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_TEMPLATE_PARA() RETURNS TRIGGER AS $$
BEGIN
    --插入对应的实例
    INSERT INTO stat_inst_para_grp 
    (PARA_GRP_TAG,                                        DESCRIPTION,  INST_TAG,      SORT,     LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)) ,  NEW.DESCRIPTION, INST_TAG,    NEW.SORT,LOCATION_ID,SUB_SYSTEM 
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 1;
    
    INSERT INTO stat_inst_para_point 
    (PARA_TAG,                                         DESCRIPTION,   RANK,  POINT_TAG,  SORT,    PARA_GRP_TAG, INST_TAG,LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION,  1,     '',        NEW.SORT, '',          INST_TAG,LOCATION_ID,SUB_SYSTEM
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.TYPE,0) = 1;
    
    INSERT INTO stat_inst_para_time 
    (PARA_TAG,                                         DESCRIPTION,    RANK, TIMESTAMP,            SORT,    PARA_GRP_TAG, INST_TAG,    TIME_MODE, YEAR_R, MONTH_R, DAY_R, WEEKDAY_R, HOUR_R, MINUTE_R, SECOND_R,LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION, 1,       floor(extract(epoch from now())),NEW.SORT, '',           INST_TAG,    0,        0,      0,       0,      0,         0,      0,        0,      LOCATION_ID,SUB_SYSTEM
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.TYPE,0) = 2;
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_template_para', 'para_tag', '',        NEW.PARA_TAG, getTableSubSystem('stat_template_para'));
    END IF;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_TEMPLATE_PARA ON stat_template_para;
CREATE TRIGGER TIA_STAT_TEMPLATE_PARA AFTER INSERT ON stat_template_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_TEMPLATE_PARA();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_TEMPLATE_PARA() RETURNS TRIGGER AS $$
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_template_para', 'para_tag', OLD.PARA_TAG, NEW.PARA_TAG, getTableSubSystem('stat_template_para'));
    END IF;
    
    UPDATE stat_link_para
    SET STAT_PARA_TAG = concat(OLD.TEMPLATE_TAG,'.',getAfterLastDot(NEW.PARA_TAG))
    WHERE STAT_PARA_TAG = concat(OLD.TEMPLATE_TAG,'.',getAfterLastDot(OLD.PARA_TAG));
    
    IF OLD.PARA_TAG = NEW.PARA_TAG AND OLD.DESCRIPTION = NEW.DESCRIPTION AND OLD.ISGRP = NEW.ISGRP AND OLD.TYPE = NEW.TYPE
    AND OLD.SORT = NEW.SORT
    THEN RETURN NEW;
    END IF;
    
    
    --更新对应的实例点
    --考虑从分组到不分组,从测点类型到时间类型,需删除旧的数据
    IF OLD.ISGRP = 1 AND NEW.ISGRP = 0 THEN
    BEGIN   
        DELETE FROM stat_inst_para_grp USING stat_inst_def,stat_inst_para_grp 
        WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
        AND stat_inst_para_grp.INST_TAG = stat_inst_def.INST_TAG
        AND getAfterLastDot(stat_inst_para_grp.PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG);
        
        IF NEW.TYPE = 1 THEN
            INSERT INTO stat_inst_para_point 
            (PARA_TAG,                                         DESCRIPTION,   RANK,  POINT_TAG,  SORT,    PARA_GRP_TAG, INST_TAG,LOCATION_ID,SUB_SYSTEM)
            SELECT 
            concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION,  1,     '',        NEW.SORT, '',          INST_TAG,LOCATION_ID,SUB_SYSTEM
            FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.TYPE,0) = 1;
        ELSEIF NEW.TYPE = 2 THEN
            INSERT INTO stat_inst_para_time 
            (PARA_TAG,                                         DESCRIPTION,    RANK, TIMESTAMP,            SORT,    PARA_GRP_TAG, INST_TAG,    TIME_MODE, YEAR_R, MONTH_R, DAY_R, WEEKDAY_R, HOUR_R, MINUTE_R, SECOND_R,LOCATION_ID,SUB_SYSTEM)
            SELECT 
            concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION, 1,       floor(extract(epoch from now())),NEW.SORT, '',           INST_TAG,    0,        0,      0,       0,      0,         0,      0,        0,      LOCATION_ID,SUB_SYSTEM
            FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.TYPE,0) = 2;
        END IF; 
    END;
    ELSEIF OLD.ISGRP = 0 AND NEW.ISGRP = 1 THEN
    BEGIN
        IF OLD.TYPE = 1 THEN 
            DELETE FROM stat_inst_para_point USING stat_inst_def,stat_inst_para_point 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_para_point.INST_TAG = stat_inst_def.INST_TAG
            AND getAfterLastDot(stat_inst_para_point.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
        ELSEIF OLD.TYPE = 2 THEN
            DELETE FROM stat_inst_para_time USING stat_inst_def,stat_inst_para_time 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_para_time.INST_TAG = stat_inst_def.INST_TAG
            AND getAfterLastDot(stat_inst_para_time.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
        END IF;
        
        INSERT INTO stat_inst_para_grp 
        (PARA_GRP_TAG,                                        DESCRIPTION,    INST_TAG,      SORT,   LOCATION_ID,SUB_SYSTEM)
        SELECT 
        concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)) ,  NEW.DESCRIPTION, INST_TAG,    NEW.SORT,LOCATION_ID,SUB_SYSTEM 
        FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 1;
            
    END;
    ELSEIF OLD.ISGRP = NEW.ISGRP AND OLD.ISGRP = 0 THEN
    BEGIN
        IF OLD.TYPE = 1 AND NEW.TYPE = 2 THEN 
        BEGIN
            DELETE FROM stat_inst_para_point USING stat_inst_def,stat_inst_para_point 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_para_point.INST_TAG = stat_inst_def.INST_TAG
            AND getAfterLastDot(stat_inst_para_point.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
            
            INSERT INTO stat_inst_para_time 
            (PARA_TAG,                                         DESCRIPTION,    RANK, TIMESTAMP,            SORT,    PARA_GRP_TAG, INST_TAG,    TIME_MODE, YEAR_R, MONTH_R, DAY_R, WEEKDAY_R, HOUR_R, MINUTE_R, SECOND_R,LOCATION_ID,SUB_SYSTEM)
            SELECT 
            concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION, 1,       floor(extract(epoch from now())),NEW.SORT, '',           INST_TAG,    0,        0,      0,       0,      0,         0,      0,        0,      LOCATION_ID,SUB_SYSTEM
            FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.TYPE,0) = 2;
        END;
        ELSEIF OLD.TYPE = 2 AND NEW.TYPE = 1 THEN 
        BEGIN
            DELETE FROM stat_inst_para_time USING stat_inst_def,stat_inst_para_time 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_para_time.INST_TAG = stat_inst_def.INST_TAG
            AND getAfterLastDot(stat_inst_para_time.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
            
            INSERT INTO stat_inst_para_point 
            (PARA_TAG,                                         DESCRIPTION,   RANK,  POINT_TAG,  SORT,    PARA_GRP_TAG, INST_TAG,LOCATION_ID,SUB_SYSTEM)
            SELECT 
            concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION,  1,     '',        NEW.SORT, '',          INST_TAG,LOCATION_ID,SUB_SYSTEM
            FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.TYPE,0) = 1;
        END;
        ELSEIF OLD.TYPE = NEW.TYPE AND OLD.TYPE = 1 THEN 
    update stat_inst_def
    SET stat_inst_para_point.PARA_TAG = concat(stat_inst_para_point.INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)), stat_inst_para_point.DESCRIPTION = NEW.DESCRIPTION, stat_inst_para_point.SORT = NEW.SORT
    from stat_inst_para_point
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_para_point.INST_TAG AND RANK = 1 AND getAfterLastDot(PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);

       ELSEIF OLD.TYPE = NEW.TYPE AND OLD.TYPE = 2 THEN 
    update stat_inst_def
    SET stat_inst_para_time.PARA_TAG = concat(stat_inst_para_time.INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)), stat_inst_para_time.DESCRIPTION = NEW.DESCRIPTION, stat_inst_para_time.SORT = NEW.SORT
    from stat_inst_para_time
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_para_time.INST_TAG AND RANK = 1 AND getAfterLastDot(PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);

       END IF;
    END;
    ELSEIF OLD.ISGRP = NEW.ISGRP AND OLD.ISGRP = 1 THEN 
    BEGIN
        IF OLD.TYPE = 1 AND NEW.TYPE = 2 THEN 
        BEGIN
            DELETE FROM stat_inst_para_point USING stat_inst_def,stat_inst_para_grp,stat_inst_para_point 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_def.INST_TAG = stat_inst_para_grp.INST_TAG
            AND getAfterLastDot(stat_inst_para_grp.PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG)
            AND stat_inst_para_point.PARA_GRP_TAG = stat_inst_para_grp.PARA_GRP_TAG;
        END;
        ELSEIF OLD.TYPE = 2 AND NEW.TYPE = 1 THEN
        BEGIN
            DELETE FROM stat_inst_para_time USING stat_inst_def,stat_inst_para_grp,stat_inst_para_time 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_def.INST_TAG = stat_inst_para_grp.INST_TAG
            AND getAfterLastDot(stat_inst_para_grp.PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG)
            AND stat_inst_para_time.PARA_GRP_TAG = stat_inst_para_grp.PARA_GRP_TAG;
        END;
        END IF;
       
        
    update stat_inst_def
    SET stat_inst_para_grp.PARA_GRP_TAG = concat(stat_inst_para_grp.INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)), stat_inst_para_grp.DESCRIPTION = NEW.DESCRIPTION, stat_inst_para_grp.SORT = NEW.SORT
    from stat_inst_para_grp
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_para_grp.INST_TAG AND getAfterLastDot(PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG);

    END; 
    END IF;
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_TEMPLATE_PARA ON stat_template_para;
CREATE TRIGGER TUA_STAT_TEMPLATE_PARA AFTER UPDATE ON stat_template_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_TEMPLATE_PARA();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_TEMPLATE_PARA() RETURNS TRIGGER AS $$
BEGIN
    --删除相应的实例点
    DELETE FROM stat_inst_para_grp USING stat_inst_def,stat_inst_para_grp 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_para_grp.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_para_grp.PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG);
    
    DELETE FROM stat_inst_para_point USING stat_inst_def,stat_inst_para_point 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_para_point.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_para_point.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
    
    DELETE FROM stat_inst_para_time USING stat_inst_def,stat_inst_para_time 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_para_time.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_para_time.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
    DELETE FROM stat_link_para 
    WHERE STAT_PARA_TAG = OLD.PARA_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_template_para', 'para_tag', OLD.PARA_TAG, '', getTableSubSystem('stat_template_para'));
    END IF;
    
 
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_TEMPLATE_PARA ON stat_template_para;
CREATE TRIGGER TDA_STAT_TEMPLATE_PARA AFTER DELETE ON stat_template_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_TEMPLATE_PARA();



CREATE OR REPLACE FUNCTION FUNC_TIA_STAT_TEMPLATE_RET() RETURNS TRIGGER AS $$
BEGIN
    --插入相应实例点
    INSERT INTO stat_inst_ret_grp 
    (RET_GRP_TAG,                                        DESCRIPTION,     INST_TAG,  SORT,      LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)) ,  NEW.DESCRIPTION, INST_TAG,    NEW.SORT,LOCATION_ID,SUB_SYSTEM 
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 1;
    
    INSERT INTO stat_inst_ret_para 
    (RET_TAG,                                          DESCRIPTION,    RANK, POINT_TAG, SORT,    PARA_GRP_TAG, VALUE, IS_BIND, INST_TAG,LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)) ,NEW.DESCRIPTION, 1,      '',       NEW.SORT,'',            0,     0,      INST_TAG,LOCATION_ID,SUB_SYSTEM   
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_template_ret', 'ret_tag', '',        NEW.RET_TAG, getTableSubSystem('stat_template_ret'));
    END IF;
    
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_STAT_TEMPLATE_RET ON stat_template_ret;
CREATE TRIGGER TIA_STAT_TEMPLATE_RET AFTER INSERT ON stat_template_ret FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_STAT_TEMPLATE_RET();



CREATE OR REPLACE FUNCTION FUNC_TUA_STAT_TEMPLATE_RET() RETURNS TRIGGER AS $$
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_template_ret', 'ret_tag', OLD.RET_TAG, NEW.RET_TAG, getTableSubSystem('stat_template_ret'));
    END IF;
    
    UPDATE stat_link_ret
    SET STAT_RET_TAG = concat(OLD.TEMPLATE_TAG,'.',getAfterLastDot(NEW.RET_TAG))
    WHERE STAT_RET_TAG = concat(OLD.TEMPLATE_TAG,'.',getAfterLastDot(OLD.RET_TAG));    
    
    IF OLD.RET_TAG = NEW.RET_TAG AND OLD.DESCRIPTION = NEW.DESCRIPTION AND OLD.ISGRP = NEW.ISGRP AND OLD.SORT = NEW.SORT
    THEN RETURN NEW;
    END IF;
    
    --更新相应的实例点
    IF OLD.ISGRP = 1 AND NEW.ISGRP = 0 THEN
    BEGIN
        DELETE FROM stat_inst_ret_grp USING stat_inst_def,stat_inst_ret_grp 
        WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
        AND stat_inst_ret_grp.INST_TAG = stat_inst_def.INST_TAG
        AND getAfterLastDot(stat_inst_ret_grp.RET_GRP_TAG) = getAfterLastDot(OLD.RET_TAG);
        
        INSERT INTO stat_inst_ret_para 
        (RET_TAG,                                          DESCRIPTION,    RANK, POINT_TAG, SORT,    PARA_GRP_TAG, VALUE, IS_BIND, INST_TAG,LOCATION_ID,SUB_SYSTEM)
        SELECT 
        concat(INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)) ,NEW.DESCRIPTION, 1,      '',       NEW.SORT,'',            0,     0,      INST_TAG,LOCATION_ID,SUB_SYSTEM   
        FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0;
    END;
    ELSEIF OLD.ISGRP = 0 AND NEW.ISGRP = 1 THEN
    BEGIN
        DELETE FROM stat_inst_ret_para USING stat_inst_def,stat_inst_ret_para 
        WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
        AND stat_inst_ret_para.INST_TAG = stat_inst_def.INST_TAG
        AND getAfterLastDot(stat_inst_ret_para.RET_TAG) = getAfterLastDot(OLD.RET_TAG);
        
        INSERT INTO stat_inst_ret_grp 
        (RET_GRP_TAG,                                        DESCRIPTION,     INST_TAG,  SORT,      LOCATION_ID,SUB_SYSTEM)
        SELECT 
        concat(INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)) ,  NEW.DESCRIPTION, INST_TAG,    NEW.SORT,LOCATION_ID,SUB_SYSTEM 
        FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 1;
    END;
    ELSEIF OLD.ISGRP = NEW.ISGRP AND OLD.ISGRP = 0 THEN
    BEGIN
    update stat_inst_def
    SET stat_inst_ret_para.RET_TAG = concat(stat_inst_ret_para.INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)), stat_inst_ret_para.DESCRIPTION = NEW.DESCRIPTION, stat_inst_ret_para.SORT = NEW.SORT
    from stat_inst_ret_para
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_ret_para.INST_TAG AND `RANK` = 1 AND getAfterLastDot(RET_TAG) = getAfterLastDot(OLD.RET_TAG);

    END;
    ELSEIF OLD.ISGRP = NEW.ISGRP AND OLD.ISGRP = 1 THEN
    BEGIN
    update stat_inst_def
    SET stat_inst_ret_grp.RET_GRP_TAG = concat(stat_inst_ret_grp.INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)), stat_inst_ret_grp.DESCRIPTION = NEW.DESCRIPTION, stat_inst_ret_grp.SORT = NEW.SORT
    from stat_inst_ret_grp
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_ret_grp.INST_TAG AND getAfterLastDot(RET_GRP_TAG) = getAfterLastDot(OLD.RET_TAG);

    END; 
    END IF;
    
    
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_STAT_TEMPLATE_RET ON stat_template_ret;
CREATE TRIGGER TUA_STAT_TEMPLATE_RET AFTER UPDATE ON stat_template_ret FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_STAT_TEMPLATE_RET();



CREATE OR REPLACE FUNCTION FUNC_TDA_STAT_TEMPLATE_RET() RETURNS TRIGGER AS $$
BEGIN
    --删除相应的实例点
    DELETE FROM stat_inst_ret_grp USING stat_inst_def,stat_inst_ret_grp 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_ret_grp.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_ret_grp.RET_GRP_TAG) = getAfterLastDot(OLD.RET_TAG);
    
    DELETE FROM stat_inst_ret_para USING stat_inst_def,stat_inst_ret_para 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_ret_para.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_ret_para.RET_TAG) = getAfterLastDot(OLD.RET_TAG);
    DELETE FROM stat_link_ret
    WHERE STAT_RET_TAG = OLD.RET_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_template_ret', 'ret_tag', OLD.RET_TAG, '', getTableSubSystem('stat_template_ret'));
    END IF;
    
  
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_STAT_TEMPLATE_RET ON stat_template_ret;
CREATE TRIGGER TDA_STAT_TEMPLATE_RET AFTER DELETE ON stat_template_ret FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_STAT_TEMPLATE_RET();



CREATE OR REPLACE FUNCTION FUNC_TIA_TRIGGER_CONDITION_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'trigger_condition_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('trigger_condition_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_TRIGGER_CONDITION_DEFINE ON trigger_condition_define;
CREATE TRIGGER TIA_TRIGGER_CONDITION_DEFINE AFTER INSERT ON trigger_condition_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_TRIGGER_CONDITION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_TRIGGER_CONDITION_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'trigger_condition_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('trigger_condition_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_TRIGGER_CONDITION_DEFINE ON trigger_condition_define;
CREATE TRIGGER TUA_TRIGGER_CONDITION_DEFINE AFTER UPDATE ON trigger_condition_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_TRIGGER_CONDITION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_TRIGGER_CONDITION_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'trigger_condition_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('trigger_condition_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_TRIGGER_CONDITION_DEFINE ON trigger_condition_define;
CREATE TRIGGER TDA_TRIGGER_CONDITION_DEFINE AFTER DELETE ON trigger_condition_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_TRIGGER_CONDITION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_TRIGGER_FUNCTION_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'trigger_function_define', 'func_name', '', NEW.FUNC_NAME, getTableSubSystem('trigger_function_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_TRIGGER_FUNCTION_DEFINE ON trigger_function_define;
CREATE TRIGGER TIA_TRIGGER_FUNCTION_DEFINE AFTER INSERT ON trigger_function_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_TRIGGER_FUNCTION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_TRIGGER_FUNCTION_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'trigger_function_define', 'func_name', OLD.FUNC_NAME, NEW.FUNC_NAME, getTableSubSystem('trigger_function_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_TRIGGER_FUNCTION_DEFINE ON trigger_function_define;
CREATE TRIGGER TUA_TRIGGER_FUNCTION_DEFINE AFTER UPDATE ON trigger_function_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_TRIGGER_FUNCTION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_TRIGGER_FUNCTION_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'trigger_function_define', 'func_name', OLD.FUNC_NAME, '', getTableSubSystem('trigger_function_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_TRIGGER_FUNCTION_DEFINE ON trigger_function_define;
CREATE TRIGGER TDA_TRIGGER_FUNCTION_DEFINE AFTER DELETE ON trigger_function_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_TRIGGER_FUNCTION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_TRIGGER_PERIOD_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'trigger_period_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('trigger_period_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_TRIGGER_PERIOD_DEFINE ON trigger_period_define;
CREATE TRIGGER TIA_TRIGGER_PERIOD_DEFINE AFTER INSERT ON trigger_period_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_TRIGGER_PERIOD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_TRIGGER_PERIOD_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'trigger_period_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('trigger_period_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_TRIGGER_PERIOD_DEFINE ON trigger_period_define;
CREATE TRIGGER TUA_TRIGGER_PERIOD_DEFINE AFTER UPDATE ON trigger_period_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_TRIGGER_PERIOD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_TRIGGER_PERIOD_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'trigger_period_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('trigger_period_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_TRIGGER_PERIOD_DEFINE ON trigger_period_define;
CREATE TRIGGER TDA_TRIGGER_PERIOD_DEFINE AFTER DELETE ON trigger_period_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_TRIGGER_PERIOD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_TRIGGER_TIMER_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'trigger_timer_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('trigger_timer_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_TRIGGER_TIMER_DEFINE ON trigger_timer_define;
CREATE TRIGGER TIA_TRIGGER_TIMER_DEFINE AFTER INSERT ON trigger_timer_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_TRIGGER_TIMER_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_TRIGGER_TIMER_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'trigger_timer_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('trigger_timer_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_TRIGGER_TIMER_DEFINE ON trigger_timer_define;
CREATE TRIGGER TUA_TRIGGER_TIMER_DEFINE AFTER UPDATE ON trigger_timer_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_TRIGGER_TIMER_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_TRIGGER_TIMER_DEFINE() RETURNS TRIGGER AS $$
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'trigger_timer_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('trigger_timer_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_TRIGGER_TIMER_DEFINE ON trigger_timer_define;
CREATE TRIGGER TDA_TRIGGER_TIMER_DEFINE AFTER DELETE ON trigger_timer_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_TRIGGER_TIMER_DEFINE();




CREATE OR REPLACE FUNCTION splitList( strList varchar(100), split_char varchar(1) , strCount int) RETURNS varchar(64)
AS $$
    DECLARE splitCount int;
    DECLARE splitIndex int DEFAULT 1;
    DECLARE str1 varchar(100) DEFAULT '';
    DECLARE str2 varchar(100) DEFAULT '';
BEGIN 
    --分割字符串,返回第strCount个子串
    
    splitCount = char_length(strList)-char_length(replace(strList,split_char,'')) + 1;
    IF strCount > splitCount OR strCount <=0 THEN RETURN '';
    END IF;
    
    IF strCount = 1 THEN RETURN substring_index(strList,split_char,strCount);
    ELSE
    BEGIN
        str1 = substring_index(strList,split_char,strCount-1);
        str2 = substring_index(strList,split_char,strCount);
        RETURN right(str2,LENGTH(str2) - LENGTH(str1) - 1);
    END;
    END IF;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION clearTempRelation(devTempName varchar(64))
RETURNS VOID
AS $$
BEGIN
    /*
     * 清除指定模板的所有测点与前置模板的关系
     */
    
    UPDATE accuml_temp_define 
    SET SEC_DEVTP_NAME = '',
        SEC_SEQ_NO = -1
    WHERE DEV_TP_NAME = devTempName;
    
    UPDATE analog_temp_define 
    SET SEC_DEVTP_NAME = '',
        SEC_SEQ_NO = -1,
        OUT_SEC_SEQ_NO = -1
    WHERE DEV_TP_NAME = devTempName;
    
    UPDATE digital_temp_define 
    SET SEC_DEVTP_NAME = '',
        SEC_SEQ_NO1 = -1,
        SEC_SEQ_NO2 = -1,
        SEC_SEQ_NO3 = -1,
        SEC_SEQ_NO4 = -1,
        SEC_SEQ_NO5 = -1,
        OUT_SEC_SEQ_NO1 = -1,
        OUT_SEC_SEQ_NO2 = -1,
        OUT_SEC_SEQ_NO3 = -1,
        OUT_SEC_SEQ_NO4 = -1,
        OUT_SEC_SEQ_NO5 = -1
    WHERE DEV_TP_NAME = devTempName;
    
    UPDATE mix_temp_define 
    SET SEC_DEVTP_NAME = '',
        SEC_SEQ_NO = -1,
        OUT_SEC_SEQ_NO = -1
    WHERE DEV_TP_NAME = devTempName;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION updateFesResPara(tableName varchar(64),startNum decimal(10,0))
RETURNS VOID
AS $$
BEGIN
    /*
     * 批量修改设备的规约参数1,要批量修改的设备在export_seq中
     * tableName 表名
     * startNum 修改的起始数字
     */
    
    --创建临时表
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpUpdateFesResParaTbl(
        TAG_NAME varchar(64) NOT NULL,
        RES_PARA decimal(10,0) NOT NULL
    );
    DELETE FROM tmpUpdateFesResParaTbl;
    
    IF lower(tableName) = 'fes_accuml' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_accuml,export_seq WHERE fes_accuml.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.NO,fes_accuml.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (row_number() over (order by NULL))+startNum;
        
    update fes_accuml
    SET fes_accuml.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA
    from tmpupdatefesresparatbl
    where fes_accuml.tag_name = tmpupdatefesresparatbl.tag_name;

    END;
    ELSEIF lower(tableName) = 'fes_analog' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_analog,export_seq WHERE fes_analog.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.NO,fes_analog.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (row_number() over (order by NULL))+startNum;
        
    update fes_analog
    SET fes_analog.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA
    from tmpupdatefesresparatbl
    where fes_analog.tag_name = tmpupdatefesresparatbl.tag_name;

    END;
    ELSEIF lower(tableName) = 'fes_analog_ctrl' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_analog_ctrl,export_seq WHERE fes_analog_ctrl.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.NO,fes_analog_ctrl.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (row_number() over (order by NULL))+startNum;
        
    update fes_analog_ctrl
    SET fes_analog_ctrl.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA
    from tmpupdatefesresparatbl
    where fes_analog_ctrl.tag_name = tmpupdatefesresparatbl.tag_name;

    END;
    ELSEIF lower(tableName) = 'fes_digital' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_digital,export_seq WHERE fes_digital.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.NO,fes_digital.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (row_number() over (order by NULL))+startNum;
        
    update fes_digital
    SET fes_digital.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA
    from tmpupdatefesresparatbl
    where fes_digital.tag_name = tmpupdatefesresparatbl.tag_name;

    END;
    ELSEIF lower(tableName) = 'fes_digital_ctrl' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_digital_ctrl,export_seq WHERE fes_digital_ctrl.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.NO,fes_digital_ctrl.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (row_number() over (order by NULL))+startNum;
        
    update fes_digital_ctrl
    SET fes_digital_ctrl.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA
    from tmpupdatefesresparatbl
    where fes_digital_ctrl.tag_name = tmpupdatefesresparatbl.tag_name;

    END;
    ELSEIF lower(tableName) = 'fes_mix' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_mix,export_seq WHERE fes_mix.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.NO,fes_mix.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (row_number() over (order by NULL))+startNum;
        
    update fes_mix
    SET fes_mix.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA
    from tmpupdatefesresparatbl
    where fes_mix.tag_name = tmpupdatefesresparatbl.tag_name;

    END;
    ELSEIF lower(tableName) = 'fes_mix_ctrl' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_mix_ctrl,export_seq WHERE fes_mix_ctrl.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.NO,fes_mix_ctrl.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (row_number() over (order by NULL))+startNum;
        
    update fes_mix_ctrl
    SET fes_mix_ctrl.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA
    from tmpupdatefesresparatbl
    where fes_mix_ctrl.tag_name = tmpupdatefesresparatbl.tag_name;

    END;
    END IF; 
    
    
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION bindStatDev(statTag varchar(64),devTag varchar(64))
RETURNS VOID
AS $$
BEGIN
    /*
     * 绑定统计分析中的实例,使用对应的后台设备
     * statTag 统计分析实例  
     * devTag 后台设备  
     */
    
    --清空
    UPDATE stat_inst_para_point SET POINT_TAG = '' WHERE INST_TAG = statTag;
    
    UPDATE stat_inst_ret_para SET POINT_TAG = '' WHERE INST_TAG = statTag;
    
    --填写
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpBinStatDevTbl(
        PARA_TAG  varchar(64) NOT NULL,
        POINT_TAG varchar(64) NOT NULL
    );
    DELETE FROM tmpBinStatDevTbl;
    
    --查找对应的测点
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT PARA_TAG,concat('analog.',analog.TAG_NAME) AS A 
    FROM stat_inst_para_point,analog 
    WHERE INST_TAG = statTag AND analog.DEVICE = devTag 
    AND getAfterLastDot(PARA_TAG) = getAfterLastDot(analog.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT PARA_TAG,concat('accuml.',accuml.TAG_NAME) AS A 
    FROM stat_inst_para_point,accuml 
    WHERE INST_TAG = statTag AND accuml.DEVICE = devTag 
    AND getAfterLastDot(PARA_TAG) = getAfterLastDot(accuml.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT PARA_TAG,concat('digital.',digital.TAG_NAME) AS A 
    FROM stat_inst_para_point,digital 
    WHERE INST_TAG = statTag AND digital.DEVICE = devTag 
    AND getAfterLastDot(PARA_TAG) = getAfterLastDot(digital.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT PARA_TAG,concat('mix.',mix.TAG_NAME) AS A 
    FROM stat_inst_para_point,mix 
    WHERE INST_TAG = statTag AND mix.DEVICE = devTag 
    AND getAfterLastDot(PARA_TAG) = getAfterLastDot(mix.TAG_NAME);
    
    update stat_inst_para_point
    SET stat_inst_para_point.POINT_TAG = tmpBinStatDevTbl.POINT_TAG
    from tmpbinstatdevtbl
    where stat_inst_para_point.para_tag = tmpbinstatdevtbl.para_tag;

    
    DELETE FROM tmpBinStatDevTbl;
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT RET_TAG,concat('analog.',analog.TAG_NAME,'.value') AS A 
    FROM stat_inst_ret_para,analog 
    WHERE INST_TAG = statTag AND analog.DEVICE = devTag 
    AND getAfterLastDot(RET_TAG) = getAfterLastDot(analog.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT RET_TAG,concat('digital.',digital.TAG_NAME,'.value') AS A 
    FROM stat_inst_ret_para,digital 
    WHERE INST_TAG = statTag AND digital.DEVICE = devTag 
    AND getAfterLastDot(RET_TAG) = getAfterLastDot(digital.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT RET_TAG,concat('accuml.',accuml.TAG_NAME,'.value') AS A 
    FROM stat_inst_ret_para,accuml 
    WHERE INST_TAG = statTag AND accuml.DEVICE = devTag 
    AND getAfterLastDot(RET_TAG) = getAfterLastDot(accuml.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT RET_TAG,concat('mix.',mix.TAG_NAME,'.value') AS A 
    FROM stat_inst_ret_para,mix 
    WHERE INST_TAG = statTag AND mix.DEVICE = devTag 
    AND getAfterLastDot(RET_TAG) = getAfterLastDot(mix.TAG_NAME);
    
    update stat_inst_ret_para
    SET stat_inst_ret_para.POINT_TAG = tmpBinStatDevTbl.POINT_TAG, stat_inst_ret_para.IS_BIND = '1' 
    from tmpbinstatdevtbl
    where stat_inst_ret_para.ret_tag = tmpbinstatdevtbl.para_tag;

    
END;
$$LANGUAGE PLPGSQL;



CREATE OR REPLACE FUNCTION relationByTempPointName(devTempName varchar(64),fesDevTempName varchar(64))
RETURNS VOID
AS $$
BEGIN
    /*
     * 按照模板下的测点名称进行,后台模板与前置模板的关联
     */
    --获取设备测点名称
    
    CALL clearTempRelation(devTempName);
    --创建临时表,
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpRelationNameTbl(
        TAG_NAME varchar(64) NOT NULL,
        FES_SEQ_NO decimal(10,0) NOT NULL
    );
    
    --accuml
    DELETE FROM tmpRelationNameTbl;
    
    INSERT INTO tmpRelationNameTbl(TAG_NAME,FES_SEQ_NO)
    SELECT A.TAG_NAME,B.SEQ_NO
    FROM (SELECT TAG_NAME FROM accuml_temp_define WHERE DEV_TP_NAME = devTempName) A 
    INNER JOIN (SELECT TAG_NAME,SEQ_NO FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = fesDevTempName 
    AND POINT_TYPE = getFesPointType('FES_ACC')) B 
    ON getAfterLastDot(A.TAG_NAME) = getAfterLastDot(B.TAG_NAME);
    
    --进行关联
    update accuml_temp_define
    SET SEC_DEVTP_NAME = fesDevTempName, SEC_SEQ_NO = tmpRelationNameTbl.FES_SEQ_NO
    from tmprelationnametbl
    where accuml_temp_define.TAG_NAME = tmpRelationNameTbl.TAG_NAME;

    
    --analog
    DELETE FROM tmpRelationNameTbl;
    
    INSERT INTO tmpRelationNameTbl(TAG_NAME,FES_SEQ_NO)
    SELECT A.TAG_NAME,B.SEQ_NO
    FROM (SELECT TAG_NAME FROM analog_temp_define WHERE DEV_TP_NAME = devTempName) A 
    INNER JOIN (SELECT TAG_NAME,SEQ_NO FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = fesDevTempName 
    AND POINT_TYPE = getFesPointType('FES_ANA')) B 
    ON getAfterLastDot(A.TAG_NAME) = getAfterLastDot(B.TAG_NAME);
    
    --进行关联
    update analog_temp_define
    SET SEC_DEVTP_NAME = fesDevTempName, SEC_SEQ_NO = tmpRelationNameTbl.FES_SEQ_NO
    from tmprelationnametbl
    where analog_temp_define.TAG_NAME = tmpRelationNameTbl.TAG_NAME;

    
    --digital只判断第一个分量
    DELETE FROM tmpRelationNameTbl;
    
    INSERT INTO tmpRelationNameTbl(TAG_NAME,FES_SEQ_NO)
    SELECT A.TAG_NAME,B.SEQ_NO
    FROM (SELECT TAG_NAME FROM digital_temp_define WHERE DEV_TP_NAME = devTempName) A 
    INNER JOIN (SELECT TAG_NAME,SEQ_NO FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = fesDevTempName 
    AND POINT_TYPE = getFesPointType('FES_DIG')) B 
    ON getAfterLastDot(A.TAG_NAME) = getAfterLastDot(B.TAG_NAME);
    
    --进行关联
    update digital_temp_define
    SET SEC_DEVTP_NAME = fesDevTempName, SEC_SEQ_NO1 = tmpRelationNameTbl.FES_SEQ_NO
    from tmprelationnametbl
    where digital_temp_define.TAG_NAME = tmpRelationNameTbl.TAG_NAME;

        
    --mix
    DELETE FROM tmpRelationNameTbl;
    
    INSERT INTO tmpRelationNameTbl(TAG_NAME,FES_SEQ_NO)
    SELECT A.TAG_NAME,B.SEQ_NO
    FROM (SELECT TAG_NAME FROM mix_temp_define WHERE DEV_TP_NAME = devTempName) A 
    INNER JOIN (SELECT TAG_NAME,SEQ_NO FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = fesDevTempName 
    AND POINT_TYPE = getFesPointType('FES_MIX')) B 
    ON getAfterLastDot(A.TAG_NAME) = getAfterLastDot(B.TAG_NAME);
    
    --进行关联
    update mix_temp_define
    SET SEC_DEVTP_NAME = fesDevTempName, SEC_SEQ_NO = tmpRelationNameTbl.FES_SEQ_NO
    from tmprelationnametbl
    where mix_temp_define.TAG_NAME = tmpRelationNameTbl.TAG_NAME;

    
END;
$$LANGUAGE PLPGSQL;





