﻿
-- 兼容MySQL的函数
create or replace function elt(decimal(10,0), variadic decimal[])
returns decimal language sql immutable as $$
    select $2[$1]
$$;

-- 兼容MySQL的函数
create or replace function if(bln boolean,inValue1 anyelement,inValue2 anyelement)
returns anyelement as
$$
begin
    return (case when bln = true then inValue1 else inValue2 end);
end;
$$
language plpgsql;

-- 兼容MySQL的函数,上面的anyelement参数的函数，不能适应输入为(boolean,int,decimal)的情况
create or replace function if(bln boolean,inValue1 decimal,inValue2 decimal)
returns decimal as
$$
begin
    return (case when bln = true then inValue1 else inValue2 end);
end;
$$
language plpgsql;

-- 兼容MySQL的 substring_index 函数
DROP FUNCTION IF EXISTS substring_index;
CREATE OR REPLACE FUNCTION substring_index (str text, delim text, num integer = 1, out ret text)
RETURNS text
AS $$
BEGIN
    IF num > 0 THEN
        ret = array_to_string((string_to_array(str, delim))[:num], delim);
    ELSE
        DECLARE _array TEXT[];
        BEGIN
            _array = string_to_array(str, delim);
            ret = array_to_string(_array[array_length(_array, 1) + num + 1:], delim);
        END;
    END IF;
END;
$$LANGUAGE PLPGSQL;


CREATE OR REPLACE FUNCTION FUNC_TI_ACCUML() RETURNS TRIGGER AS $$
BEGIN
    /*
     * 触发器命名规则
     * 命名规则,数据库表格:数据库名.表名,小写
     * 局部变量驼峰写法,
     * 列名:大写加下划线
     */
    /*
     * 生成Acc后,
     * 1,自动生成Acc_limit_info的记录,
     * 2,ANALOG_MAP_TO_FES中添加一条记录
     */
    
    IF COALESCE(NEW.IS_LIMIT,0) <> 0  THEN    
        
        IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
            INSERT INTO accuml_limit_info (
            TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,      LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, CROSS_TIME, ALARM_PRIORITY, PIC_NAME, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, POINT_TP_NAME) 
            VALUES ( 
            NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,  '1',        '1',       0,          0,          0,        0,          0,         0,          5,             5,               0,         1,              '',       5,                0,            1,             0,               0,                  NEW.POINT_TP_NAME 
            );
        ELSE
            INSERT INTO accuml_limit_info (
            TAG_NAME,          LOCATION_ID,         SUB_SYSTEM,           LIMIT_TYPE,  LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, CROSS_TIME, ALARM_PRIORITY, PIC_NAME, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, POINT_TP_NAME) SELECT 
            NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,  LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, 0,                         ALARM_PRIORITY, '',       ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, NEW.POINT_TP_NAME
            FROM accuml_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME ;
        END IF;
        
    END IF;
    IF COALESCE(NEW.IS_SAMPLE,0) <> 0 THEN 
        
       INSERT INTO sample_define (
       TAG_NAME,                                           DESCRIPTION,      LOCATION_ID,      SUB_SYSTEM,     DEVICE_TAG, POINT_TYPE,     SAMPLE_PERIOD,    SAMPLE_DEADBAND,    VALUE, STATUS, UPDATE_TIME) VALUES 
       ( NEW.TAG_NAME, NEW.DESCRIPTION,  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEVICE, NEW.POINT_TYPE, NEW.SAMPLE_PERIOD,NEW.SAMPLE_DEADBAND, 0,  0,  0       );
       
    END IF;
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
        INSERT INTO accuml_map_to_fes (
        KEY_ID_TAG,                            LOCATION_ID,     SUB_SYSTEM,     RTU_TAG,     DOT_NO, BASE, COEFF, RANGE_UP, RANGE_LOW, IS_FILTER, PERCENT, DEV_TYPE,     POINT_TP_NAME, DESCRIPTION) 
        VALUES ( 
        concat('accuml' , '.' , NEW.TAG_NAME), NEW.LOCATION_ID, NEW.SUB_SYSTEM, NEW.RTU_TAG ,  '-1',   '0',   '1',99999999999999.9, 0, 1,          20,    NEW.DEV_TYPE,NEW.POINT_TP_NAME,NEW.DESCRIPTION
        );
    ELSE
        INSERT INTO accuml_map_to_fes (
        KEY_ID_TAG,                            LOCATION_ID,     SUB_SYSTEM,     RTU_TAG,     DOT_NO, BASE, COEFF, RANGE_UP, RANGE_LOW, IS_FILTER, PERCENT, DEV_TYPE,     POINT_TP_NAME, DESCRIPTION) 
        SELECT 
        concat('accuml' , '.' , NEW.TAG_NAME), NEW.LOCATION_ID, NEW.SUB_SYSTEM, NEW.RTU_TAG ,  '-1',   '0',   '1',   RANGE_UP,RANGE_LOW, IS_FILTER, PERCENT, NEW.DEV_TYPE,NEW.POINT_TP_NAME,NEW.DESCRIPTION
        FROM accuml_temp_define WHERE TAG_NAME=NEW.POINT_TP_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('insert'),'accuml', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('accuml'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_ACCUML ON accuml;
CREATE TRIGGER TI_ACCUML AFTER INSERT ON accuml FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_ACCUML();



CREATE OR REPLACE FUNCTION FUNC_TU_ACCUML() RETURNS TRIGGER AS $$
BEGIN 
--由limit变不limit
IF  COALESCE(OLD.IS_LIMIT,0) != 0 AND COALESCE(NEW.IS_LIMIT,0) = 0 
THEN
BEGIN 
    DELETE FROM accuml_limit_info WHERE TAG_NAME=OLD.TAG_NAME;
END;
--由不limit变limit
ELSEIF COALESCE(OLD.IS_LIMIT,0) = 0 AND COALESCE(NEW.IS_LIMIT,0) != 0
THEN 
BEGIN
    
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
        INSERT INTO accuml_limit_info (
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,      LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, CROSS_TIME, ALARM_PRIORITY, PIC_NAME, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, POINT_TP_NAME) 
        VALUES ( 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,  '1',        '1',       0,          0,          0,        0,          0,         0,          5,             5,               0,         1,              '',       5,                0,            1,             0,               0,                  NEW.POINT_TP_NAME 
         );
    ELSE
        INSERT INTO accuml_limit_info (
        TAG_NAME,          LOCATION_ID,         SUB_SYSTEM,           LIMIT_TYPE,  LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, CROSS_TIME, ALARM_PRIORITY, PIC_NAME, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, POINT_TP_NAME) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,  LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, 0,                         ALARM_PRIORITY, '',       ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, NEW.POINT_TP_NAME
        FROM accuml_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME ;
    END IF;
    
END;
--更新limit表
ELSE
BEGIN
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM<>OLD.SUB_SYSTEM OR NEW.POINT_TP_NAME <> OLD.POINT_TP_NAME
    THEN 
    BEGIN   
        UPDATE accuml_limit_info  
        SET 
        TAG_NAME = NEW.TAG_NAME,
        LOCATION_ID = NEW.LOCATION_ID,
        SUB_SYSTEM = NEW.SUB_SYSTEM,
        POINT_TP_NAME = NEW.POINT_TP_NAME
        WHERE accuml_limit_info.TAG_NAME = OLD.TAG_NAME;
    END;
    END IF;
END; 
END IF;
--sample变成不采样
IF  COALESCE(OLD.IS_SAMPLE,0) != 0 AND COALESCE(NEW.IS_SAMPLE,0) = 0 
THEN
BEGIN 
    DELETE FROM sample_define WHERE TAG_NAME= OLD.TAG_NAME ;
END;
--由不采样变成采样
ELSEIF COALESCE(OLD.IS_SAMPLE,0) = 0 AND COALESCE(NEW.IS_SAMPLE,0) != 0
THEN 
BEGIN
    INSERT INTO sample_define (
    TAG_NAME,                                           DESCRIPTION,      LOCATION_ID,      SUB_SYSTEM,     DEVICE_TAG, POINT_TYPE,     SAMPLE_PERIOD,    SAMPLE_DEADBAND,    VALUE, STATUS, UPDATE_TIME) VALUES 
    (NEW.TAG_NAME, NEW.DESCRIPTION,  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEVICE, NEW.POINT_TYPE, NEW.SAMPLE_PERIOD,NEW.SAMPLE_DEADBAND, 0,  0,  0       );
END;
--更新采样表
ELSE
BEGIN
    IF NEW.TAG_NAME<>OLD.TAG_NAME OR NEW.DESCRIPTION<>OLD.DESCRIPTION OR NEW.LOCATION_ID<>OLD.LOCATION_ID OR NEW.SUB_SYSTEM<>OLD.SUB_SYSTEM
    OR NEW.DEVICE<>OLD.DEVICE OR NEW.POINT_TYPE<>OLD.POINT_TYPE OR NEW.SAMPLE_PERIOD<>OLD.SAMPLE_PERIOD OR NEW.SAMPLE_DEADBAND<>OLD.SAMPLE_DEADBAND
    THEN 
    BEGIN   
        
        UPDATE sample_define 
        SET 
        TAG_NAME =   NEW.TAG_NAME ,
        DESCRIPTION = NEW.DESCRIPTION ,
        LOCATION_ID = NEW.LOCATION_ID ,
        SUB_SYSTEM = NEW.SUB_SYSTEM ,
        DEVICE_TAG = NEW.DEVICE,
        POINT_TYPE = NEW.POINT_TYPE ,
        SAMPLE_PERIOD = NEW.SAMPLE_PERIOD ,
        SAMPLE_DEADBAND = NEW.SAMPLE_DEADBAND
        WHERE TAG_NAME=  OLD.TAG_NAME;
    END;
    END IF;
END; 
END IF;
IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM OR NEW.RTU_TAG <> OLD.RTU_TAG
OR NEW.DEV_TYPE <> OLD.DEV_TYPE OR NEW.POINT_TP_NAME <> OLD.POINT_TP_NAME OR NEW.DESCRIPTION <> OLD.DESCRIPTION
THEN 
    BEGIN 
        
        UPDATE accuml_map_to_fes SET 
        KEY_ID_TAG = concat('accuml' , '.' , NEW.TAG_NAME),
        LOCATION_ID =NEW.LOCATION_ID,
        SUB_SYSTEM = NEW.SUB_SYSTEM,
        RTU_TAG = NEW.RTU_TAG,
        DEV_TYPE = NEW.DEV_TYPE,
        POINT_TP_NAME = NEW.POINT_TP_NAME,
        DESCRIPTION = NEW.DESCRIPTION
        WHERE KEY_ID_TAG=concat('accuml' , '.' , 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'),'accuml', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('accuml'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_ACCUML ON accuml;
CREATE TRIGGER TU_ACCUML AFTER UPDATE ON accuml FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_ACCUML();



CREATE OR REPLACE FUNCTION FUNC_TD_ACCUML() RETURNS TRIGGER AS $$
BEGIN
    
    
    
IF COALESCE(OLD.IS_LIMIT,0) <> 0  
THEN
DELETE  FROM  accuml_limit_info WHERE TAG_NAME=OLD.TAG_NAME ;
END IF;
IF COALESCE(OLD.IS_SAMPLE,0) <> 0  
THEN
DELETE  FROM  sample_define WHERE TAG_NAME=   OLD.TAG_NAME ;
END IF;
DELETE FROM accuml_map_to_fes WHERE KEY_ID_TAG=concat('accuml' , '.' , OLD.TAG_NAME);
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_ACCUML ON accuml;
CREATE TRIGGER TD_ACCUML BEFORE DELETE ON accuml FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_ACCUML();



CREATE OR REPLACE FUNCTION FUNC_TDA_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('delete'),'accuml', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('accuml'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ACCUML ON accuml;
CREATE TRIGGER TDA_ACCUML AFTER DELETE ON accuml FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ACCUML();



CREATE OR REPLACE FUNCTION FUNC_TIA_ACS_DEVICE_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'),'acs_device_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('acs_device_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ACS_DEVICE_INFO ON acs_device_info;
CREATE TRIGGER TIA_ACS_DEVICE_INFO AFTER INSERT ON acs_device_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ACS_DEVICE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_ACS_DEVICE_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'),'acs_device_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('acs_device_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ACS_DEVICE_INFO ON acs_device_info;
CREATE TRIGGER TUA_ACS_DEVICE_INFO AFTER UPDATE ON acs_device_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ACS_DEVICE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_ACS_DEVICE_INFO() RETURNS TRIGGER AS $$
BEGIN
delete from acs_device_node_map where ACS_DEVICE_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'),'acs_device_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('acs_device_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ACS_DEVICE_INFO ON acs_device_info;
CREATE TRIGGER TDA_ACS_DEVICE_INFO AFTER DELETE ON acs_device_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ACS_DEVICE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_ACS_DEVICE_NODE_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'),'acs_device_node_map', 'NODE_NAME', '', NEW.NODE_NAME, getTableSubSystem('acs_device_node_map'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ACS_DEVICE_NODE_MAP ON acs_device_node_map;
CREATE TRIGGER TIA_ACS_DEVICE_NODE_MAP AFTER INSERT ON acs_device_node_map FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ACS_DEVICE_NODE_MAP();



CREATE OR REPLACE FUNCTION FUNC_TUA_ACS_DEVICE_NODE_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'),'acs_device_node_map', 'NODE_NAME', OLD.NODE_NAME, NEW.NODE_NAME, getTableSubSystem('acs_device_node_map'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ACS_DEVICE_NODE_MAP ON acs_device_node_map;
CREATE TRIGGER TUA_ACS_DEVICE_NODE_MAP AFTER UPDATE ON acs_device_node_map FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ACS_DEVICE_NODE_MAP();



CREATE OR REPLACE FUNCTION FUNC_TDA_ACS_DEVICE_NODE_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'),'acs_device_node_map', 'NODE_NAME', OLD.NODE_NAME, '', getTableSubSystem('acs_device_node_map'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ACS_DEVICE_NODE_MAP ON acs_device_node_map;
CREATE TRIGGER TDA_ACS_DEVICE_NODE_MAP AFTER DELETE ON acs_device_node_map FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ACS_DEVICE_NODE_MAP();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_ACTION_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'),'alarm_action_define', 'action_id', '', NEW.ACTION_ID, getTableSubSystem('alarm_action_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_ACTION_DEFINE ON alarm_action_define;
CREATE TRIGGER TIA_ALARM_ACTION_DEFINE AFTER INSERT ON alarm_action_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_ACTION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_ACTION_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'),'alarm_action_define', 'action_id', OLD.ACTION_ID, NEW.ACTION_ID, getTableSubSystem('alarm_action_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_ACTION_DEFINE ON alarm_action_define;
CREATE TRIGGER TUA_ALARM_ACTION_DEFINE AFTER UPDATE ON alarm_action_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_ACTION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_ACTION_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'),'alarm_action_define', 'action_id', OLD.ACTION_ID, '', getTableSubSystem('alarm_action_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_ACTION_DEFINE ON alarm_action_define;
CREATE TRIGGER TDA_ALARM_ACTION_DEFINE AFTER DELETE ON alarm_action_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_ACTION_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_KEYWORD_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'),'alarm_keyword_define', 'keyword_id', '', NEW.KEYWORD_ID, getTableSubSystem('alarm_keyword_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_KEYWORD_DEFINE ON alarm_keyword_define;
CREATE TRIGGER TIA_ALARM_KEYWORD_DEFINE AFTER INSERT ON alarm_keyword_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_KEYWORD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_KEYWORD_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'),'alarm_keyword_define', 'keyword_id', OLD.KEYWORD_ID, NEW.KEYWORD_ID, getTableSubSystem('alarm_keyword_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_KEYWORD_DEFINE ON alarm_keyword_define;
CREATE TRIGGER TUA_ALARM_KEYWORD_DEFINE AFTER UPDATE ON alarm_keyword_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_KEYWORD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_KEYWORD_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'),'alarm_keyword_define', 'keyword_id', OLD.KEYWORD_ID, '', getTableSubSystem('alarm_keyword_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_KEYWORD_DEFINE ON alarm_keyword_define;
CREATE TRIGGER TDA_ALARM_KEYWORD_DEFINE AFTER DELETE ON alarm_keyword_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_KEYWORD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_LEVEL_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'),'alarm_level_define', 'priority_id', '', NEW.PRIORITY_ID, getTableSubSystem('alarm_level_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_LEVEL_DEFINE ON alarm_level_define;
CREATE TRIGGER TIA_ALARM_LEVEL_DEFINE AFTER INSERT ON alarm_level_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_LEVEL_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_LEVEL_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'),'alarm_level_define', 'priority_id', OLD.PRIORITY_ID, NEW.PRIORITY_ID, getTableSubSystem('alarm_level_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_LEVEL_DEFINE ON alarm_level_define;
CREATE TRIGGER TUA_ALARM_LEVEL_DEFINE AFTER UPDATE ON alarm_level_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_LEVEL_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_LEVEL_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'),'alarm_level_define', 'priority_id', OLD.PRIORITY_ID, '', getTableSubSystem('alarm_level_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_LEVEL_DEFINE ON alarm_level_define;
CREATE TRIGGER TDA_ALARM_LEVEL_DEFINE AFTER DELETE ON alarm_level_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_LEVEL_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_LEVEL_SOUND_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'),'alarm_level_sound_def', 'priority_id,region_id', '', concat(NEW.PRIORITY_ID,',',NEW.REGION_ID), getTableSubSystem('alarm_level_sound_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_LEVEL_SOUND_DEF ON alarm_level_sound_def;
CREATE TRIGGER TIA_ALARM_LEVEL_SOUND_DEF AFTER INSERT ON alarm_level_sound_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_LEVEL_SOUND_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_LEVEL_SOUND_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'),'alarm_level_sound_def', 'priority_id,region_id', concat(OLD.PRIORITY_ID,',',OLD.REGION_ID), concat(NEW.PRIORITY_ID,',',NEW.REGION_ID), getTableSubSystem('alarm_level_sound_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_LEVEL_SOUND_DEF ON alarm_level_sound_def;
CREATE TRIGGER TUA_ALARM_LEVEL_SOUND_DEF AFTER UPDATE ON alarm_level_sound_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_LEVEL_SOUND_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_LEVEL_SOUND_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'),'alarm_level_sound_def', 'priority_id,region_id', concat(OLD.PRIORITY_ID,',',OLD.REGION_ID), '', getTableSubSystem('alarm_level_sound_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_LEVEL_SOUND_DEF ON alarm_level_sound_def;
CREATE TRIGGER TDA_ALARM_LEVEL_SOUND_DEF AFTER DELETE ON alarm_level_sound_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_LEVEL_SOUND_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_MANNER_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'),'alarm_manner_define', 'manner_id', '', NEW.MANNER_ID, getTableSubSystem('alarm_manner_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_MANNER_DEFINE ON alarm_manner_define;
CREATE TRIGGER TIA_ALARM_MANNER_DEFINE AFTER INSERT ON alarm_manner_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_MANNER_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_MANNER_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'),'alarm_manner_define', 'manner_id', OLD.MANNER_ID, NEW.MANNER_ID, getTableSubSystem('alarm_manner_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_MANNER_DEFINE ON alarm_manner_define;
CREATE TRIGGER TUA_ALARM_MANNER_DEFINE AFTER UPDATE ON alarm_manner_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_MANNER_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_MANNER_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'),'alarm_manner_define', 'manner_id', OLD.MANNER_ID, '', getTableSubSystem('alarm_manner_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_MANNER_DEFINE ON alarm_manner_define;
CREATE TRIGGER TDA_ALARM_MANNER_DEFINE AFTER DELETE ON alarm_manner_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_MANNER_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_STATUS_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'),'alarm_status_define', 'alarm_type,status_value', '', concat(NEW.ALARM_TYPE,',',NEW.STATUS_VALUE), getTableSubSystem('alarm_status_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_STATUS_DEFINE ON alarm_status_define;
CREATE TRIGGER TIA_ALARM_STATUS_DEFINE AFTER INSERT ON alarm_status_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_STATUS_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_STATUS_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'),'alarm_status_define', 'alarm_type,status_value', concat(OLD.ALARM_TYPE,',',OLD.STATUS_VALUE), concat(NEW.ALARM_TYPE,',',NEW.STATUS_VALUE), getTableSubSystem('alarm_status_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_STATUS_DEFINE ON alarm_status_define;
CREATE TRIGGER TUA_ALARM_STATUS_DEFINE AFTER UPDATE ON alarm_status_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_STATUS_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_STATUS_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'),'alarm_status_define', 'alarm_type,status_value', concat(OLD.ALARM_TYPE,',',OLD.STATUS_VALUE), '', getTableSubSystem('alarm_status_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_STATUS_DEFINE ON alarm_status_define;
CREATE TRIGGER TDA_ALARM_STATUS_DEFINE AFTER DELETE ON alarm_status_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_STATUS_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_ALARM_TYPE_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'),'alarm_type_define', 'type_id', '', NEW.TYPE_ID, getTableSubSystem('alarm_type_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_ALARM_TYPE_DEFINE ON alarm_type_define;
CREATE TRIGGER TIA_ALARM_TYPE_DEFINE AFTER INSERT ON alarm_type_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_ALARM_TYPE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_ALARM_TYPE_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'),'alarm_type_define', 'type_id', OLD.TYPE_ID, NEW.TYPE_ID, getTableSubSystem('alarm_type_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_ALARM_TYPE_DEFINE ON alarm_type_define;
CREATE TRIGGER TUA_ALARM_TYPE_DEFINE AFTER UPDATE ON alarm_type_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_ALARM_TYPE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_ALARM_TYPE_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'),'alarm_type_define', 'type_id', OLD.TYPE_ID, '', getTableSubSystem('alarm_type_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ALARM_TYPE_DEFINE ON alarm_type_define;
CREATE TRIGGER TDA_ALARM_TYPE_DEFINE AFTER DELETE ON alarm_type_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ALARM_TYPE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TI_ANALOG() RETURNS TRIGGER AS $$
BEGIN
    
    
IF COALESCE(NEW.IS_CONTROL,0) <> 0 
THEN
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
        INSERT INTO analog_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,    CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, RTU_TAG, OFFSET_NO, CTRL_ALIAS, POINT_TP_NAME) 
        values (
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,0,         1,             30,            30,          0,     1,    9999999.9, 0,         0.01,          NEW.RTU_TAG,       '-1',       '',       NEW.POINT_TP_NAME
        );
    ELSE
        INSERT INTO analog_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,    CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, RTU_TAG, OFFSET_NO, CTRL_ALIAS, POINT_TP_NAME) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, NEW.RTU_TAG,       '-1',       '',       NEW.POINT_TP_NAME
        FROM analog_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME;
    END IF;
END IF;
IF COALESCE(NEW.IS_LIMIT,0) <> 0  
THEN
    
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
        INSERT INTO analog_limit_info (
        TAG_NAME,     LOCATION_ID,      SUB_SYSTEM,     LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, CROSS_TIME, ALARM_PRIORITY, PIC_NAME, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, POINT_TP_NAME) 
        VALUES ( 
        NEW.TAG_NAME, NEW.LOCATION_ID,  NEW.SUB_SYSTEM, 1,           0,         0,         0,          0,        0,          0,         0,           5,             5,              0,          1,              '',      5,                 0,            1,             0,              0,                  NEW.POINT_TP_NAME
        );
    ELSE
        INSERT INTO analog_limit_info (
        TAG_NAME,     LOCATION_ID,      SUB_SYSTEM,     LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, CROSS_TIME, ALARM_PRIORITY, PIC_NAME, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, POINT_TP_NAME) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID,  NEW.SUB_SYSTEM, LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, 0,          ALARM_PRIORITY,  '',      ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, NEW.POINT_TP_NAME
        FROM analog_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME ;
    END IF;
END IF;
IF COALESCE(NEW.IS_SAMPLE,0) <> 0
THEN 
INSERT INTO sample_define (
TAG_NAME,                                           DESCRIPTION,      LOCATION_ID,      SUB_SYSTEM,     DEVICE_TAG, POINT_TYPE,     SAMPLE_PERIOD,    SAMPLE_DEADBAND,    VALUE, STATUS, UPDATE_TIME) VALUES 
( NEW.TAG_NAME, NEW.DESCRIPTION,  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEVICE, NEW.POINT_TYPE, NEW.SAMPLE_PERIOD,NEW.SAMPLE_DEADBAND, 0,  0,  0       );
END IF;
IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
    INSERT INTO analog_map_to_fes (
    KEY_ID_TAG,                             LOCATION_ID,      SUB_SYSTEM,     DEV_TYPE,     POINT_TP_NAME,      RTU_TAG,    DOT_NO, IS_FILTER, PERCENT, IS_VALID, DEADBAND, ZEROBAND, BASE, COEFF, RANGE_UP, RANGE_LOW, DESCRIPTION) 
    VALUES ( 
    concat('analog' , '.' , NEW.TAG_NAME),  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEV_TYPE, NEW.POINT_TP_NAME,  NEW.RTU_TAG, '-1',    1,        20,      1,        0.01,     0.0001,  0,     1,    9999999.9, 0, NEW.DESCRIPTION
    );
ELSE
    INSERT INTO analog_map_to_fes (
    KEY_ID_TAG,                             LOCATION_ID,      SUB_SYSTEM,     DEV_TYPE,     POINT_TP_NAME,      RTU_TAG,    DOT_NO, IS_FILTER, PERCENT, IS_VALID, DEADBAND, ZEROBAND, BASE, COEFF, RANGE_UP, RANGE_LOW, DESCRIPTION) 
    SELECT 
    concat('analog' , '.' , NEW.TAG_NAME),  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEV_TYPE, NEW.POINT_TP_NAME,  NEW.RTU_TAG, '-1',    IS_FILTER, PERCENT, IS_VALID, DEADBAND, ZEROBAND, BASE, COEFF, RANGE_UP, RANGE_LOW, NEW.DESCRIPTION
    FROM analog_temp_define WHERE TAG_NAME=NEW.POINT_TP_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('insert'),'analog', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('analog'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_ANALOG ON analog;
CREATE TRIGGER TI_ANALOG AFTER INSERT ON analog FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TU_ANALOG() RETURNS TRIGGER AS $$
BEGIN
IF COALESCE(OLD.IS_CONTROL,0) != 0 AND COALESCE(NEW.IS_CONTROL,0) = 0
THEN
BEGIN 
    DELETE FROM analog_control WHERE TAG_NAME=OLD.TAG_NAME;
	DELETE FROM opt_interlock_para WHERE TAG_NAME=OLD.TAG_NAME;
END;
ELSEIF COALESCE(OLD.IS_CONTROL,0) = 0 AND COALESCE(NEW.IS_CONTROL,0) != 0
THEN 
BEGIN
    
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
        INSERT INTO analog_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,    CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, RTU_TAG,     OFFSET_NO, CTRL_ALIAS, POINT_TP_NAME) 
        values (
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,0,         1,             30,            30,          0,     1,    9999999.9, 0,         0.01,           NEW.RTU_TAG,   '-1',       '',       NEW.POINT_TP_NAME
        );
    ELSE
        INSERT INTO analog_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,    CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, RTU_TAG,     OFFSET_NO, CTRL_ALIAS, POINT_TP_NAME) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, NEW.RTU_TAG,       '-1',       '',       NEW.POINT_TP_NAME
        FROM analog_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME;
    END IF;
    
END;
ELSE
BEGIN
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM OR NEW.POINT_TP_NAME <> OLD.POINT_TP_NAME 
    OR NEW.RTU_TAG <> OLD.RTU_TAG 
    THEN 
        UPDATE analog_control  
        SET 
        TAG_NAME = NEW.TAG_NAME,
        LOCATION_ID = NEW.LOCATION_ID,
        SUB_SYSTEM = NEW.SUB_SYSTEM,
        POINT_TP_NAME = NEW.POINT_TP_NAME,
        RTU_TAG = NEW.RTU_TAG
        WHERE analog_control.TAG_NAME = OLD.TAG_NAME;
    END IF;
END; 
END IF;
IF COALESCE(OLD.IS_LIMIT,0) != 0 AND COALESCE(NEW.IS_LIMIT,0) = 0
THEN
BEGIN 
    DELETE FROM analog_limit_info WHERE TAG_NAME=OLD.TAG_NAME;
END;
ELSEIF COALESCE(OLD.IS_LIMIT,0) = 0 AND COALESCE(NEW.IS_LIMIT,0) != 0
THEN 
BEGIN
    
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
        INSERT INTO analog_limit_info (
        TAG_NAME,     LOCATION_ID,      SUB_SYSTEM,     LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, CROSS_TIME, ALARM_PRIORITY, PIC_NAME, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, POINT_TP_NAME) 
        VALUES ( 
        NEW.TAG_NAME, NEW.LOCATION_ID,  NEW.SUB_SYSTEM, 1,           0,         0,         0,          0,        0,          0,         0,           5,             5,              0,          1,              '',      5,                 0,            1,             0,              0,                  NEW.POINT_TP_NAME
        );
    ELSE
        INSERT INTO analog_limit_info (
        TAG_NAME,     LOCATION_ID,      SUB_SYSTEM,     LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, CROSS_TIME, ALARM_PRIORITY, PIC_NAME, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, POINT_TP_NAME) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID,  NEW.SUB_SYSTEM, LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, 0,          ALARM_PRIORITY,  '',      ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, NEW.POINT_TP_NAME
        FROM analog_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME ;
    END IF;
    
END;
ELSE
BEGIN
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM OR NEW.POINT_TP_NAME <> OLD.POINT_TP_NAME
    THEN 
        UPDATE analog_limit_info  SET 
        TAG_NAME = NEW.TAG_NAME ,
        LOCATION_ID = NEW.LOCATION_ID ,
        SUB_SYSTEM = NEW.SUB_SYSTEM ,
        POINT_TP_NAME = NEW.POINT_TP_NAME
        WHERE analog_limit_info.TAG_NAME = OLD.TAG_NAME;
    END IF;
END; 
END IF;
IF  COALESCE(OLD.IS_SAMPLE,0) != 0 AND COALESCE(NEW.IS_SAMPLE,0) = 0 
THEN
BEGIN 
    DELETE FROM sample_define WHERE TAG_NAME=  OLD.TAG_NAME ;
END;
ELSEIF COALESCE(OLD.IS_SAMPLE,0) = 0 AND COALESCE(NEW.IS_SAMPLE,0) != 0
THEN 
BEGIN
    INSERT INTO sample_define (
    TAG_NAME,                                           DESCRIPTION,      LOCATION_ID,      SUB_SYSTEM,     DEVICE_TAG, POINT_TYPE,     SAMPLE_PERIOD,    SAMPLE_DEADBAND,    VALUE, STATUS, UPDATE_TIME) VALUES 
    (  NEW.TAG_NAME, NEW.DESCRIPTION,  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEVICE, NEW.POINT_TYPE, NEW.SAMPLE_PERIOD,NEW.SAMPLE_DEADBAND, 0,  0,  0       );
END;
ELSE
BEGIN
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.DESCRIPTION <> OLD.DESCRIPTION OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM
    OR NEW.DEVICE <> OLD.DEVICE OR NEW.POINT_TYPE <> OLD.POINT_TYPE OR NEW.SAMPLE_PERIOD <> OLD.SAMPLE_PERIOD OR NEW.SAMPLE_DEADBAND <> OLD.SAMPLE_DEADBAND 
    THEN
        UPDATE sample_define 
        SET 
        TAG_NAME =  NEW.TAG_NAME ,
        DESCRIPTION = NEW.DESCRIPTION ,
        LOCATION_ID = NEW.LOCATION_ID ,
        SUB_SYSTEM = NEW.SUB_SYSTEM ,
        DEVICE_TAG = NEW.DEVICE,
        POINT_TYPE = NEW.POINT_TYPE ,
        SAMPLE_PERIOD = NEW.SAMPLE_PERIOD ,
        SAMPLE_DEADBAND = NEW.SAMPLE_DEADBAND
        WHERE TAG_NAME=   OLD.TAG_NAME;
    END IF;
END; 
END IF;
IF  NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM OR NEW.DEV_TYPE <> OLD.DEV_TYPE
OR NEW.RTU_TAG <> OLD.RTU_TAG OR NEW.POINT_TYPE <> OLD.POINT_TYPE OR NEW.POINT_TP_NAME <> OLD.POINT_TP_NAME OR NEW.DESCRIPTION <> OLD.DESCRIPTION
THEN 
    UPDATE analog_map_to_fes SET 
    KEY_ID_TAG = concat('analog' , '.' , NEW.TAG_NAME),
    LOCATION_ID =NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM,
    DEV_TYPE = NEW.DEV_TYPE,
    POINT_TP_NAME = NEW.POINT_TP_NAME,
    RTU_TAG = NEW.RTU_TAG,
    DESCRIPTION = NEW.DESCRIPTION
    WHERE KEY_ID_TAG=concat('analog' , '.' , 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', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('analog'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_ANALOG ON analog;
CREATE TRIGGER TU_ANALOG AFTER UPDATE ON analog FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TD_ANALOG() RETURNS TRIGGER AS $$
BEGIN
    
    
IF COALESCE(OLD.IS_CONTROL,0) <> 0 
THEN
DELETE FROM analog_control WHERE TAG_NAME=OLD.TAG_NAME ;
END IF;
IF COALESCE(OLD.IS_LIMIT,0) <> 0 
THEN
DELETE FROM analog_limit_info WHERE TAG_NAME=OLD.TAG_NAME ;
END IF;
IF COALESCE(OLD.IS_SAMPLE,0) <> 0  
THEN
DELETE  FROM  sample_define WHERE TAG_NAME=  OLD.TAG_NAME ;
END IF;
DELETE FROM analog_map_to_fes WHERE KEY_ID_TAG=concat('analog' , '.' , OLD.TAG_NAME) ;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_ANALOG ON analog;
CREATE TRIGGER TD_ANALOG BEFORE DELETE ON analog FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TDA_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'),'analog', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('analog'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_ANALOG ON analog;
CREATE TRIGGER TDA_ANALOG AFTER DELETE ON analog FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_ANALOG();



CREATE OR REPLACE FUNCTION FUNC_TIA_BAS_CRAFT_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_craft_def', 'id', '', NEW.ID, getTableSubSystem('bas_craft_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_BAS_CRAFT_DEF ON bas_craft_def;
CREATE TRIGGER TIA_BAS_CRAFT_DEF AFTER INSERT ON bas_craft_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_BAS_CRAFT_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_BAS_CRAFT_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_craft_def', 'id', OLD.ID, NEW.ID, getTableSubSystem('bas_craft_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_BAS_CRAFT_DEF ON bas_craft_def;
CREATE TRIGGER TUA_BAS_CRAFT_DEF AFTER UPDATE ON bas_craft_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_BAS_CRAFT_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_BAS_CRAFT_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_craft_def', 'id', OLD.ID, '', getTableSubSystem('bas_craft_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_BAS_CRAFT_DEF ON bas_craft_def;
CREATE TRIGGER TDA_BAS_CRAFT_DEF AFTER DELETE ON bas_craft_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_BAS_CRAFT_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_BAS_MODE_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_mode_def', 'id,location_id,craft_id,mode_no', '', concat(NEW.ID,',',NEW.LOCATION_ID,',',NEW.CRAFT_ID,',',NEW.MODE_NO), getTableSubSystem('bas_mode_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_BAS_MODE_DEF ON bas_mode_def;
CREATE TRIGGER TIA_BAS_MODE_DEF AFTER INSERT ON bas_mode_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_BAS_MODE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_BAS_MODE_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_mode_def', 'id,location_id,craft_id,mode_no', concat(OLD.ID,',',OLD.LOCATION_ID,',',OLD.CRAFT_ID,',',OLD.MODE_NO), concat(NEW.ID,',',NEW.LOCATION_ID,',',NEW.CRAFT_ID,',',NEW.MODE_NO), getTableSubSystem('bas_mode_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_BAS_MODE_DEF ON bas_mode_def;
CREATE TRIGGER TUA_BAS_MODE_DEF AFTER UPDATE ON bas_mode_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_BAS_MODE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_BAS_MODE_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_mode_def', 'id,location_id,craft_id,mode_no', concat(OLD.ID,',',OLD.LOCATION_ID,',',OLD.CRAFT_ID,',',OLD.MODE_NO), '', getTableSubSystem('bas_mode_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_BAS_MODE_DEF ON bas_mode_def;
CREATE TRIGGER TDA_BAS_MODE_DEF AFTER DELETE ON bas_mode_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_BAS_MODE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_BAS_MODE_TYPE() 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_mode_type', 'id', '', NEW.ID, getTableSubSystem('bas_mode_type'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_BAS_MODE_TYPE ON bas_mode_type;
CREATE TRIGGER TIA_BAS_MODE_TYPE AFTER INSERT ON bas_mode_type FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_BAS_MODE_TYPE();



CREATE OR REPLACE FUNCTION FUNC_TUA_BAS_MODE_TYPE() 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_mode_type', 'id', OLD.ID, NEW.ID, getTableSubSystem('bas_mode_type'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_BAS_MODE_TYPE ON bas_mode_type;
CREATE TRIGGER TUA_BAS_MODE_TYPE AFTER UPDATE ON bas_mode_type FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_BAS_MODE_TYPE();



CREATE OR REPLACE FUNCTION FUNC_TDA_BAS_MODE_TYPE() 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_mode_type', 'id', OLD.ID, '', getTableSubSystem('bas_mode_type'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_BAS_MODE_TYPE ON bas_mode_type;
CREATE TRIGGER TDA_BAS_MODE_TYPE AFTER DELETE ON bas_mode_type FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_BAS_MODE_TYPE();



CREATE OR REPLACE FUNCTION FUNC_TIA_DEV_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'),'dev_group', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('dev_group'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DEV_GROUP ON dev_group;
CREATE TRIGGER TIA_DEV_GROUP AFTER INSERT ON dev_group FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DEV_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TUA_DEV_GROUP() RETURNS TRIGGER AS $$
        DECLARE rtuTag varchar(64) DEFAULT '';
BEGIN
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
    BEGIN 
       UPDATE dev_info SET GROUP_TAG_NAME = NEW.TAG_NAME WHERE GROUP_TAG_NAME = OLD.TAG_NAME;
    END; 
    END IF;
	
	-- IF NEW.RTU_NO <> OLD.RTU_NO THEN
       -- SELECT TAG_NAME INTO rtuTag FROM fes_rtu_para WHERE fes_rtu_para.RTU_NO = NEW.RTU_NO;
       -- UPDATE accuml SET RTU_TAG = rtuTag from dev_info WHERE dev_info.GROUP_TAG_NAME = NEW.TAG_NAME AND accuml.DEVICE = dev_info.TAG_NAME;
       -- UPDATE analog SET RTU_TAG = rtuTag from dev_info WHERE dev_info.GROUP_TAG_NAME = NEW.TAG_NAME AND analog.DEVICE = dev_info.TAG_NAME;
       -- UPDATE digital SET RTU_TAG = rtuTag from dev_info WHERE dev_info.GROUP_TAG_NAME = NEW.TAG_NAME AND digital.DEVICE = dev_info.TAG_NAME;
       -- UPDATE mix SET RTU_TAG = rtuTag from dev_info WHERE dev_info.GROUP_TAG_NAME = NEW.TAG_NAME AND mix.DEVICE = dev_info.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_group', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('dev_group'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DEV_GROUP ON dev_group;
CREATE TRIGGER TUA_DEV_GROUP AFTER UPDATE ON dev_group FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DEV_GROUP();


CREATE OR REPLACE FUNCTION FUNC_TDA_DEV_GROUP() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM dev_info WHERE GROUP_TAG_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'),'dev_group', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('dev_group'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DEV_GROUP ON dev_group;
CREATE TRIGGER TDA_DEV_GROUP AFTER DELETE ON dev_group FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DEV_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TI_DEV_INFO() 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(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)),DESCRIPTION,  NEW.TAG_NAME,SEQ_NO, NEW.LOCATION_ID, NEW.SUB_SYSTEM, '',    NEW.GRAY, NEW.CONTRAST, NEW.QCOLOR,'0',       NEW.VALUE, NEW.STATUS,  0,                0,               UNIT_ID,  '',         '',          '',        NEW.REGION_ID, NEW.OPT_HANDOVER_GROUP, IS_LIMIT, POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, NEW.DEV_TYPE, TAG_NAME,      IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, SAMPLE_DEADBAND, '',        ''
FROM accuml_temp_define WHERE DEV_TP_NAME=NEW.DEV_TP_NAME; 
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(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)),  DESCRIPTION ,    '',      NEW.DEV_TYPE,NEW.TAG_NAME,SEQ_NO, NEW.LOCATION_ID,NEW.SUB_SYSTEM,IS_CONTROL,NEW.GRAY,NEW.CONTRAST,NEW.QCOLOR,NEW.VALUE,NEW.STATUS,'0',              '0',              UNIT_ID, '',          '',          '',          NEW.REGION_ID,NEW.OPT_HANDOVER_GROUP,IS_LIMIT, POINT_TYPE,   POINT_CLASS,    POINT_PROPERTY, POINT_SORT, TAG_NAME,      IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, SAMPLE_DEADBAND, '',         ''
FROM analog_temp_define WHERE DEV_TP_NAME=NEW.DEV_TP_NAME; 
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(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)),DESCRIPTION, NEW.TAG_NAME,SEQ_NO, NEW.LOCATION_ID,NEW.SUB_SYSTEM,'',      IS_CONTROL, STATE_TEXT_NAME, VALUE_NUM, NEW.GRAY,NEW.CONTRAST,NEW.QCOLOR,NEW.VALUE,NEW.STATUS,'0',              '0',    '0',     '0',               '0',    '0',     '0',               '0',    '0',     '0',               '0',    '0',     '0',               '0',    '0',     '0',               ALARM_PRIORITY, '',       '',          '',          '',          ALARM_DELAY_TIME,INPUT_DELAY_TIME, IS_WATER_ALM,IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, NEW.REGION_ID,NEW.OPT_HANDOVER_GROUP,IS_SUMMARY, NEW.SUMMARY_VALUE,NEW.SUMMARY_STATUS,NEW.DEV_TYPE,TAG_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, '',         POINT_TYPE,POINT_CLASS, POINT_PROPERTY, POINT_SORT, '',         ''
FROM digital_temp_define WHERE DEV_TP_NAME=NEW.DEV_TP_NAME; 
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(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)),DESCRIPTION, NEW.TAG_NAME,SEQ_NO, NEW.LOCATION_ID,NEW.SUB_SYSTEM,'',      IS_CONTROL, STATE_TEXT_NAME, NEW.GRAY,NEW.CONTRAST,NEW.QCOLOR,'0',    NEW.VALUE,NEW.STATUS, '0',               '0' ,            '',     ALARM_PRIORITY, '',        '',         '',          '',          ALARM_DELAY_TIME, IS_WATER_ALM,IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, NEW.REGION_ID,NEW.OPT_HANDOVER_GROUP,NEW.IS_SUMMARY,NEW.SUMMARY_VALUE,NEW.SUMMARY_STATUS,NEW.DEV_TYPE,TAG_NAME,      IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, POINT_TYPE,POINT_CLASS, POINT_PROPERTY, POINT_SORT, '',          ''
FROM mix_temp_define WHERE DEV_TP_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'),'dev_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('dev_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_DEV_INFO ON dev_info;
CREATE TRIGGER TI_DEV_INFO AFTER INSERT ON dev_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TU_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('update'),'dev_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('dev_info'));
END IF;
    
IF 
OLD.CONTRAST = NEW.CONTRAST AND
OLD.DESCRIPTION = NEW.DESCRIPTION AND
OLD.DEV_TYPE = NEW.DEV_TYPE AND
OLD.GRAY = NEW.GRAY AND
OLD.LOCATION_ID = NEW.LOCATION_ID AND
OLD.OPT_HANDOVER_GROUP = NEW.OPT_HANDOVER_GROUP AND
OLD.QCOLOR = NEW.QCOLOR AND
OLD.REGION_ID = NEW.REGION_ID AND
OLD.STATUS = NEW.STATUS AND
OLD.SUB_SYSTEM = NEW.SUB_SYSTEM AND
OLD.TAG_NAME = NEW.TAG_NAME AND
OLD.VALUE = NEW.VALUE
THEN RETURN NEW;
END IF;
UPDATE accuml SET 
DEVICE=NEW.TAG_NAME, 
LOCATION_ID=NEW.LOCATION_ID, 
SUB_SYSTEM=NEW.SUB_SYSTEM, 
GRAY=NEW.GRAY, 
CONTRAST=NEW.CONTRAST, 
QCOLOR=NEW.QCOLOR, 
VALUE=NEW.VALUE, 
STATUS=NEW.STATUS, 
REGION_ID=NEW.REGION_ID,
OPT_HANDOVER_GROUP=NEW.OPT_HANDOVER_GROUP, 
DEV_TYPE= NEW.DEV_TYPE  ,
TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)), 
DESCRIPTION = DESCRIPTION
WHERE DEVICE=OLD.TAG_NAME  ;

UPDATE analog SET 
DEVICE=NEW.TAG_NAME, 
LOCATION_ID=NEW.LOCATION_ID, 
SUB_SYSTEM=NEW.SUB_SYSTEM, 
GRAY=NEW.GRAY,
CONTRAST=NEW.CONTRAST,
QCOLOR=NEW.QCOLOR,
VALUE=NEW.VALUE, 
STATUS=NEW.STATUS, 
REGION_ID=NEW.REGION_ID,
OPT_HANDOVER_GROUP=NEW.OPT_HANDOVER_GROUP, 
DEV_TYPE= NEW.DEV_TYPE  ,
TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)),
DESCRIPTION = DESCRIPTION
WHERE DEVICE=OLD.TAG_NAME  ;

UPDATE digital SET 
DEVICE=NEW.TAG_NAME, 
LOCATION_ID=NEW.LOCATION_ID,
SUB_SYSTEM=NEW.SUB_SYSTEM, 
GRAY=NEW.GRAY, 
CONTRAST=NEW.CONTRAST,
QCOLOR=NEW.QCOLOR,
VALUE=NEW.VALUE, 
STATUS=NEW.STATUS, 
REGION_ID=NEW.REGION_ID,
OPT_HANDOVER_GROUP=NEW.OPT_HANDOVER_GROUP,
DEV_TYPE= NEW.DEV_TYPE  ,
TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)), 
DESCRIPTION = DESCRIPTION
WHERE DEVICE=OLD.TAG_NAME  ;

UPDATE mix SET 
DEVICE=NEW.TAG_NAME, 
LOCATION_ID=NEW.LOCATION_ID,
SUB_SYSTEM=NEW.SUB_SYSTEM, 
GRAY=NEW.GRAY,
CONTRAST=NEW.CONTRAST, 
QCOLOR=NEW.QCOLOR, 
VALUE=NEW.VALUE,
STATUS=NEW.STATUS, 
REGION_ID=NEW.REGION_ID,
OPT_HANDOVER_GROUP=NEW.OPT_HANDOVER_GROUP, 
DEV_TYPE= NEW.DEV_TYPE  ,
TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)), 
DESCRIPTION = DESCRIPTION
WHERE DEVICE=OLD.TAG_NAME  ;

RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_DEV_INFO ON dev_info;
CREATE TRIGGER TU_DEV_INFO AFTER UPDATE ON dev_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TD_DEV_INFO() RETURNS TRIGGER AS $$
BEGIN
    
DELETE FROM accuml WHERE DEVICE = OLD.TAG_NAME;
DELETE FROM analog WHERE DEVICE = OLD.TAG_NAME;
DELETE FROM digital WHERE DEVICE = OLD.TAG_NAME;
DELETE FROM mix WHERE DEVICE = OLD.TAG_NAME;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_DEV_INFO ON dev_info;
CREATE TRIGGER TD_DEV_INFO BEFORE DELETE ON dev_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_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'),'dev_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('dev_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DEV_INFO ON dev_info;
CREATE TRIGGER TDA_DEV_INFO AFTER DELETE ON dev_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DEV_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_DEV_TOPO_VER() 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_topo_ver', 'LOCATION_ID',        '',NEW.LOCATION_ID, getTableSubSystem('dev_topo_ver'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DEV_TOPO_VER ON dev_topo_ver;
CREATE TRIGGER TIA_DEV_TOPO_VER AFTER INSERT ON dev_topo_ver FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DEV_TOPO_VER();



CREATE OR REPLACE FUNCTION FUNC_TUA_DEV_TOPO_VER() 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'),'dev_topo_ver', 'LOCATION_ID', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('dev_topo_ver'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DEV_TOPO_VER ON dev_topo_ver;
CREATE TRIGGER TUA_DEV_TOPO_VER AFTER UPDATE ON dev_topo_ver FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DEV_TOPO_VER();



CREATE OR REPLACE FUNCTION FUNC_TDA_DEV_TOPO_VER() 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_topo_ver', 'LOCATION_ID', OLD.LOCATION_ID, '', getTableSubSystem('dev_topo_ver'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DEV_TOPO_VER ON dev_topo_ver;
CREATE TRIGGER TDA_DEV_TOPO_VER AFTER DELETE ON dev_topo_ver FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DEV_TOPO_VER();



CREATE OR REPLACE FUNCTION FUNC_TIA_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'),'dev_type_def', 'dev_type_id', '', NEW.DEV_TYPE_ID, getTableSubSystem('dev_type_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DEV_TYPE_DEF ON dev_type_def;
CREATE TRIGGER TIA_DEV_TYPE_DEF AFTER INSERT ON dev_type_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DEV_TYPE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_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'),'dev_type_def', 'dev_type_id', OLD.DEV_TYPE_ID, NEW.DEV_TYPE_ID, getTableSubSystem('dev_type_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DEV_TYPE_DEF ON dev_type_def;
CREATE TRIGGER TUA_DEV_TYPE_DEF AFTER UPDATE ON dev_type_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DEV_TYPE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_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'),'dev_type_def', 'dev_type_id', OLD.DEV_TYPE_ID, '', getTableSubSystem('dev_type_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DEV_TYPE_DEF ON dev_type_def;
CREATE TRIGGER TDA_DEV_TYPE_DEF AFTER DELETE ON dev_type_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DEV_TYPE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_DICT_MENU_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'),'dict_menu_info', 'menu_name,actual_value', '', concat(NEW.MENU_NAME,',',NEW.ACTUAL_VALUE), getTableSubSystem('dict_menu_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DICT_MENU_INFO ON dict_menu_info;
CREATE TRIGGER TIA_DICT_MENU_INFO AFTER INSERT ON dict_menu_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DICT_MENU_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_DICT_MENU_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'),'dict_menu_info', 'menu_name,actual_value', concat(OLD.MENU_NAME,',',OLD.ACTUAL_VALUE), concat(NEW.MENU_NAME,',',NEW.ACTUAL_VALUE), getTableSubSystem('dict_menu_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DICT_MENU_INFO ON dict_menu_info;
CREATE TRIGGER TUA_DICT_MENU_INFO AFTER UPDATE ON dict_menu_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DICT_MENU_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_DICT_MENU_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'),'dict_menu_info', 'menu_name,actual_value', concat(OLD.MENU_NAME,',',OLD.ACTUAL_VALUE), '', getTableSubSystem('dict_menu_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DICT_MENU_INFO ON dict_menu_info;
CREATE TRIGGER TDA_DICT_MENU_INFO AFTER DELETE ON dict_menu_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DICT_MENU_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_DICT_STATE_TEXT_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'),'dict_state_text_info', 'state_text_name,actual_value', '', concat(NEW.STATE_TEXT_NAME,',',NEW.ACTUAL_VALUE), getTableSubSystem('dict_state_text_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DICT_STATE_TEXT_INFO ON dict_state_text_info;
CREATE TRIGGER TIA_DICT_STATE_TEXT_INFO AFTER INSERT ON dict_state_text_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DICT_STATE_TEXT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_DICT_STATE_TEXT_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'),'dict_state_text_info', 'state_text_name,actual_value', concat(OLD.STATE_TEXT_NAME,',',OLD.ACTUAL_VALUE), concat(NEW.STATE_TEXT_NAME,',',NEW.ACTUAL_VALUE), getTableSubSystem('dict_state_text_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DICT_STATE_TEXT_INFO ON dict_state_text_info;
CREATE TRIGGER TUA_DICT_STATE_TEXT_INFO AFTER UPDATE ON dict_state_text_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DICT_STATE_TEXT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_DICT_STATE_TEXT_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'),'dict_state_text_info', 'state_text_name,actual_value', concat(OLD.STATE_TEXT_NAME,',',OLD.ACTUAL_VALUE), '', getTableSubSystem('dict_state_text_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DICT_STATE_TEXT_INFO ON dict_state_text_info;
CREATE TRIGGER TDA_DICT_STATE_TEXT_INFO AFTER DELETE ON dict_state_text_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DICT_STATE_TEXT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_DICT_TEXT_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'),'dict_text_define', 'class_name,actual_value', '', concat(NEW.CLASS_NAME,',',NEW.ACTUAL_VALUE), getTableSubSystem('dict_text_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DICT_TEXT_DEFINE ON dict_text_define;
CREATE TRIGGER TIA_DICT_TEXT_DEFINE AFTER INSERT ON dict_text_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DICT_TEXT_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_DICT_TEXT_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'),'dict_text_define', 'class_name,actual_value', concat(OLD.CLASS_NAME,',',OLD.ACTUAL_VALUE), concat(NEW.CLASS_NAME,',',NEW.ACTUAL_VALUE), getTableSubSystem('dict_text_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DICT_TEXT_DEFINE ON dict_text_define;
CREATE TRIGGER TUA_DICT_TEXT_DEFINE AFTER UPDATE ON dict_text_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DICT_TEXT_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_DICT_TEXT_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'),'dict_text_define', 'class_name,actual_value', concat(OLD.CLASS_NAME,',',OLD.ACTUAL_VALUE), '', getTableSubSystem('dict_text_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DICT_TEXT_DEFINE ON dict_text_define;
CREATE TRIGGER TDA_DICT_TEXT_DEFINE AFTER DELETE ON dict_text_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DICT_TEXT_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_DICT_UNIT_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'),'dict_unit_info', 'unit_id', '', NEW.UNIT_ID, getTableSubSystem('dict_unit_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DICT_UNIT_INFO ON dict_unit_info;
CREATE TRIGGER TIA_DICT_UNIT_INFO AFTER INSERT ON dict_unit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DICT_UNIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_DICT_UNIT_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'),'dict_unit_info', 'unit_id', OLD.UNIT_ID, NEW.UNIT_ID, getTableSubSystem('dict_unit_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DICT_UNIT_INFO ON dict_unit_info;
CREATE TRIGGER TUA_DICT_UNIT_INFO AFTER UPDATE ON dict_unit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DICT_UNIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_DICT_UNIT_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'),'dict_unit_info', 'unit_id', OLD.UNIT_ID, '', getTableSubSystem('dict_unit_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DICT_UNIT_INFO ON dict_unit_info;
CREATE TRIGGER TDA_DICT_UNIT_INFO AFTER DELETE ON dict_unit_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DICT_UNIT_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_DICT_VOL_LEVEL() 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'),'dict_vol_level', 'vl_id', '', NEW.VL_ID, getTableSubSystem('dict_vol_level'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_DICT_VOL_LEVEL ON dict_vol_level;
CREATE TRIGGER TIA_DICT_VOL_LEVEL AFTER INSERT ON dict_vol_level FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_DICT_VOL_LEVEL();



CREATE OR REPLACE FUNCTION FUNC_TUA_DICT_VOL_LEVEL() 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'),'dict_vol_level', 'vl_id', OLD.VL_ID, NEW.VL_ID, getTableSubSystem('dict_vol_level'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_DICT_VOL_LEVEL ON dict_vol_level;
CREATE TRIGGER TUA_DICT_VOL_LEVEL AFTER UPDATE ON dict_vol_level FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_DICT_VOL_LEVEL();



CREATE OR REPLACE FUNCTION FUNC_TDA_DICT_VOL_LEVEL() 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'),'dict_vol_level', 'vl_id', OLD.VL_ID, '', getTableSubSystem('dict_vol_level'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DICT_VOL_LEVEL ON dict_vol_level;
CREATE TRIGGER TDA_DICT_VOL_LEVEL AFTER DELETE ON dict_vol_level FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DICT_VOL_LEVEL();



CREATE OR REPLACE FUNCTION FUNC_TI_DIGITAL() RETURNS TRIGGER AS $$
BEGIN
    
IF COALESCE(NEW.IS_CONTROL,0) <> 0  
THEN
BEGIN
    
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
        INSERT INTO digital_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,      CTRL_ACT_NAME, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, PULSE_DURATION, RTU_TAG,  OFFSET_NUM, OFFSET_NO1, OFFSET_NO2, OFFSET_NO3, OFFSET_NO4, OFFSET_NO5, CTRL_ALIAS, IS_CHECK_PREVENTION, POINT_TP_NAME, CTRL_TYPE) 
        VALUES ( 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,  '',            1,              30,           30,           30,            NEW.RTU_TAG, 1,        '-1',         '-1',         '-1',         '-1',      '-1',        '',         0 ,                 NEW.POINT_TP_NAME,0 
        );
    ELSE
        INSERT INTO digital_control(
        TAG_NAME,          LOCATION_ID,          SUB_SYSTEM,          CTRL_ACT_NAME,  IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, PULSE_DURATION, RTU_TAG,  OFFSET_NUM, OFFSET_NO1, OFFSET_NO2, OFFSET_NO3, OFFSET_NO4, OFFSET_NO5, CTRL_ALIAS, IS_CHECK_PREVENTION, POINT_TP_NAME, CTRL_TYPE) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,  CTRL_ACT_NAME,  IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, PULSE_DURATION, NEW.RTU_TAG,            OFFSET_NUM, '-1',         '-1',         '-1',         '-1',       '-1',        '',       IS_CHECK_PREVENTION , NEW.POINT_TP_NAME,CTRL_TYPE 
        FROM digital_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME;
    END IF;
    
END;
END IF;
IF COALESCE(NEW.IS_SAMPLE,0) <> 0
THEN 
INSERT INTO sample_define (
TAG_NAME,                                           DESCRIPTION,      LOCATION_ID,      SUB_SYSTEM,     DEVICE_TAG, POINT_TYPE,     SAMPLE_PERIOD,      SAMPLE_DEADBAND, VALUE, STATUS, UPDATE_TIME) VALUES 
(NEW.TAG_NAME,NEW.DESCRIPTION,  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEVICE, NEW.POINT_TYPE, NEW.SAMPLE_PERIOD,  0,               0,      0,     0       );
END IF;
INSERT INTO digital_map_to_fes (
KEY_ID_TAG,                             LOCATION_ID,     SUB_SYSTEM,       DEV_TYPE,     POINT_TP_NAME,      VALUE_NUM,        RTU_TAG,     DOT_NO1, DOT_NO2, DOT_NO3, DOT_NO4, DOT_NO5, IS_FILTER_ERR, IS_FILTER_DISTURB, DISTURB_TIME, POLARITY, DESCRIPTION) VALUES ( 
concat('digital' , '.' , NEW.TAG_NAME), NEW.LOCATION_ID, NEW.SUB_SYSTEM,   NEW.DEV_TYPE, NEW.POINT_TP_NAME,  NEW.VALUE_NUM  ,  NEW.RTU_TAG, '-1',    '-1' ,   '-1' ,   '-1',    '-1',            '0'              ,  '0'                           ,  '0'   ,              '0',             NEW.DESCRIPTION );
IF getTriggerFlag() = TRUE THEN 
INSERT 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', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('digital'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_DIGITAL ON digital;
CREATE TRIGGER TI_DIGITAL AFTER INSERT ON digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TU_DIGITAL() RETURNS TRIGGER AS $$
BEGIN
IF COALESCE(OLD.IS_CONTROL,0) != 0 AND COALESCE(NEW.IS_CONTROL,0) = 0 
THEN
BEGIN 
    DELETE FROM digital_control WHERE TAG_NAME=OLD.TAG_NAME;
	DELETE FROM opt_interlock_para WHERE TAG_NAME=OLD.TAG_NAME;
END;
ELSEIF COALESCE(OLD.IS_CONTROL,0) = 0 AND COALESCE(NEW.IS_CONTROL,0) != 0 
THEN 
BEGIN
    
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN 
        INSERT INTO digital_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,      CTRL_ACT_NAME, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, PULSE_DURATION, RTU_TAG,  OFFSET_NUM, OFFSET_NO1, OFFSET_NO2, OFFSET_NO3, OFFSET_NO4, OFFSET_NO5, CTRL_ALIAS, IS_CHECK_PREVENTION, POINT_TP_NAME, CTRL_TYPE) 
        VALUES ( 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,  '',            1,              30,           30,           30,            NEW.RTU_TAG, 1,        '-1',         '-1',         '-1',         '-1',       '-1',        '',         0 ,            NEW.POINT_TP_NAME,0 
        );
    ELSE
        INSERT INTO digital_control(
        TAG_NAME,          LOCATION_ID,          SUB_SYSTEM,          CTRL_ACT_NAME,  IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, PULSE_DURATION, RTU_TAG,  OFFSET_NUM, OFFSET_NO1, OFFSET_NO2, OFFSET_NO3, OFFSET_NO4, OFFSET_NO5, CTRL_ALIAS, IS_CHECK_PREVENTION, POINT_TP_NAME, CTRL_TYPE) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM,  CTRL_ACT_NAME,  IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, PULSE_DURATION, NEW.RTU_TAG,            OFFSET_NUM, '-1',         '-1',         '-1',         '-1',         '-1',        '', IS_CHECK_PREVENTION , NEW.POINT_TP_NAME,CTRL_TYPE 
        FROM digital_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME;
    END IF;
    
END;
ELSE
BEGIN
    IF NEW.TAG_NAME<>OLD.TAG_NAME OR NEW.LOCATION_ID<>OLD.LOCATION_ID OR NEW.SUB_SYSTEM<>OLD.SUB_SYSTEM OR NEW.RTU_TAG<>OLD.RTU_TAG 
    OR NEW.POINT_TP_NAME<>OLD.POINT_TP_NAME
    THEN 
        UPDATE digital_control
        SET 
        TAG_NAME = NEW.TAG_NAME,
        LOCATION_ID = NEW.LOCATION_ID,
        SUB_SYSTEM = NEW.SUB_SYSTEM,
        RTU_TAG = NEW.RTU_TAG ,
        POINT_TP_NAME = NEW.POINT_TP_NAME
        WHERE digital_control.TAG_NAME = OLD.TAG_NAME;
    END IF;
END; 
END IF;
IF  COALESCE(OLD.IS_SAMPLE,0) != 0 AND COALESCE(NEW.IS_SAMPLE,0) = 0 
THEN
BEGIN 
    DELETE FROM sample_define WHERE TAG_NAME=  OLD.TAG_NAME ;
END;
ELSEIF COALESCE(OLD.IS_SAMPLE,0) = 0 AND COALESCE(NEW.IS_SAMPLE,0) != 0
THEN 
BEGIN
    INSERT INTO sample_define (
    TAG_NAME,                                           DESCRIPTION,      LOCATION_ID,      SUB_SYSTEM,     DEVICE_TAG, POINT_TYPE,     SAMPLE_PERIOD,   SAMPLE_DEADBAND, VALUE, STATUS, UPDATE_TIME) VALUES 
    (NEW.TAG_NAME,NEW.DESCRIPTION,  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEVICE, NEW.POINT_TYPE, NEW.SAMPLE_PERIOD,0,              0,     0,  0       );
END;
ELSE
BEGIN
    IF NEW.TAG_NAME<>OLD.TAG_NAME OR NEW.DESCRIPTION<>OLD.DESCRIPTION OR NEW.LOCATION_ID<>OLD.LOCATION_ID OR NEW.SUB_SYSTEM<>OLD.SUB_SYSTEM 
    OR NEW.DEVICE<>OLD.DEVICE OR NEW.POINT_TYPE<>OLD.POINT_TYPE OR NEW.SAMPLE_PERIOD<>OLD.SAMPLE_PERIOD 
    THEN 
        UPDATE sample_define 
        SET 
        TAG_NAME =  NEW.TAG_NAME,
        DESCRIPTION = NEW.DESCRIPTION ,
        LOCATION_ID = NEW.LOCATION_ID ,
        SUB_SYSTEM = NEW.SUB_SYSTEM ,
        DEVICE_TAG = NEW.DEVICE,
        POINT_TYPE = NEW.POINT_TYPE,
        SAMPLE_PERIOD = NEW.SAMPLE_PERIOD
        WHERE TAG_NAME=  OLD.TAG_NAME;
    END IF;
END; 
END IF;
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.DEV_TYPE<>OLD.DEV_TYPE OR NEW.POINT_TP_NAME<>OLD.POINT_TP_NAME OR NEW.DESCRIPTION<>OLD.DESCRIPTION 
THEN 
    UPDATE digital_map_to_fes SET 
    KEY_ID_TAG = concat('digital' , '.' , NEW.TAG_NAME),
    RTU_TAG = NEW.RTU_TAG,
    LOCATION_ID =NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM,
    DEV_TYPE = NEW.DEV_TYPE,
    POINT_TP_NAME = NEW.POINT_TP_NAME,
    DESCRIPTION = NEW.DESCRIPTION
    WHERE KEY_ID_TAG=concat('digital' , '.' , OLD.TAG_NAME) ;
END IF;
IF NEW.VALUE_NUM<>OLD.VALUE_NUM THEN
BEGIN
    IF NEW.VALUE_NUM = 1 THEN
    BEGIN
        UPDATE digital_map_to_fes SET
        VALUE_NUM = NEW.VALUE_NUM,
        DOT_NO2 = '-1',
        DOT_NO3 = '-1',
        DOT_NO4 = '-1',
        DOT_NO5 = '-1'
        WHERE KEY_ID_TAG=concat('digital' , '.' , OLD.TAG_NAME);
    END;
    ELSEIF NEW.VALUE_NUM = 2 THEN
    BEGIN
        UPDATE digital_map_to_fes SET
        VALUE_NUM = NEW.VALUE_NUM,
        DOT_NO3 = '-1',
        DOT_NO4 = '-1',
        DOT_NO5 = '-1'
        WHERE KEY_ID_TAG=concat('digital' , '.' , OLD.TAG_NAME);
    END;
    ELSEIF NEW.VALUE_NUM = 3 THEN
    BEGIN
        UPDATE digital_map_to_fes SET
        VALUE_NUM = NEW.VALUE_NUM,
        DOT_NO4 = '-1',
        DOT_NO5 = '-1'
        WHERE KEY_ID_TAG=concat('digital' , '.' , OLD.TAG_NAME);
    END;
    ELSEIF NEW.VALUE_NUM = 4 THEN
    BEGIN
        UPDATE digital_map_to_fes SET 
        VALUE_NUM = NEW.VALUE_NUM,
        DOT_NO5 = '-1'
        WHERE KEY_ID_TAG=concat('digital' , '.' , OLD.TAG_NAME);
    END;
	ELSE
    BEGIN
        UPDATE digital_map_to_fes SET 
        VALUE_NUM = NEW.VALUE_NUM
        WHERE KEY_ID_TAG=concat('digital' , '.' , 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'),'digital', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('digital'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_DIGITAL ON digital;
CREATE TRIGGER TU_DIGITAL AFTER UPDATE ON digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TD_DIGITAL() RETURNS TRIGGER AS $$
BEGIN
    
IF  COALESCE(OLD.IS_CONTROL,0) <> 0 
THEN
DELETE  FROM  digital_control WHERE TAG_NAME=OLD.TAG_NAME ;
END IF;
IF  COALESCE(OLD.IS_SAMPLE,0) <> 0 
THEN
DELETE  FROM  sample_define WHERE  TAG_NAME=  OLD.TAG_NAME ;
END IF;
DELETE FROM digital_map_to_fes WHERE KEY_ID_TAG=concat('digital' , '.' , OLD.TAG_NAME);
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_DIGITAL ON digital;
CREATE TRIGGER TD_DIGITAL BEFORE DELETE ON digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TDA_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'),'digital', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('digital'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_DIGITAL ON digital;
CREATE TRIGGER TDA_DIGITAL AFTER DELETE ON digital FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_DIGITAL();



CREATE OR REPLACE FUNCTION FUNC_TIA_FBD_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'),'fbd_group', 'id', '', NEW.ID, getTableSubSystem('fbd_group'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_FBD_GROUP ON FBD_GROUP;
CREATE TRIGGER TIA_FBD_GROUP AFTER INSERT ON FBD_GROUP FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_FBD_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TUA_FBD_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'),'fbd_group', 'id', OLD.ID, NEW.ID, getTableSubSystem('fbd_group'));
    END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_FBD_GROUP ON FBD_GROUP;
CREATE TRIGGER TUA_FBD_GROUP AFTER UPDATE ON FBD_GROUP FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_FBD_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TDA_FBD_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'),'fbd_group', 'id', OLD.ID, '', getTableSubSystem('fbd_group'));
    END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_FBD_GROUP ON FBD_GROUP;
CREATE TRIGGER TDA_FBD_GROUP AFTER DELETE ON FBD_GROUP FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_FBD_GROUP();



CREATE OR REPLACE FUNCTION FUNC_TIA_HMI_TYPE_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'),'hmi_type_info', 'hmi_id', '', NEW.HMI_ID, getTableSubSystem('hmi_type_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_HMI_TYPE_INFO ON hmi_type_info;
CREATE TRIGGER TIA_HMI_TYPE_INFO AFTER INSERT ON hmi_type_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_HMI_TYPE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_HMI_TYPE_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'),'hmi_type_info', 'hmi_id', OLD.HMI_ID, NEW.HMI_ID, getTableSubSystem('hmi_type_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_HMI_TYPE_INFO ON hmi_type_info;
CREATE TRIGGER TUA_HMI_TYPE_INFO AFTER UPDATE ON hmi_type_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_HMI_TYPE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_HMI_TYPE_INFO() RETURNS TRIGGER AS $$
    --1是报表,0是HMI画面
    DECLARE num int;
BEGIN
    num =  DIV((OLD.HMI_ID-1),32);
   
    --角色表的修改
    IF num = 0 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO1 = resetBit(PERM_INFO1,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO1,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 1 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO2 = resetBit(PERM_INFO2,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO2,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 2 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO3 = resetBit(PERM_INFO3,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO3,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 3 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO4 = resetBit(PERM_INFO4,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO4,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 4 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO5 = resetBit(PERM_INFO5,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO5,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 5 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO6 = resetBit(PERM_INFO6,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO6,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 6 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO7 = resetBit(PERM_INFO7,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO7,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 7 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO8 = resetBit(PERM_INFO8,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO8,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 8 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO9 = resetBit(PERM_INFO9,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO9,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 9 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO10 = resetBit(PERM_INFO10,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO10,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 10 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO11 = resetBit(PERM_INFO11,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO11,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 11 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO12 = resetBit(PERM_INFO12,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO12,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 12 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO13 = resetBit(PERM_INFO13,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO13,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 13 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO14 = resetBit(PERM_INFO14,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO14,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 14 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO15 = resetBit(PERM_INFO15,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO15,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 15 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO16 = resetBit(PERM_INFO16,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO16,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    END IF;
    
    --用户组表的修改
    IF num = 0 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO1 = resetBit(PERM_INFO1,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO1,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 1 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO2 = resetBit(PERM_INFO2,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO2,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 2 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO3 = resetBit(PERM_INFO3,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO3,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 3 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO4 = resetBit(PERM_INFO4,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO4,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 4 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO5 = resetBit(PERM_INFO5,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO5,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 5 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO6 = resetBit(PERM_INFO6,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO6,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 6 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO7 = resetBit(PERM_INFO7,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO7,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 7 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO8 = resetBit(PERM_INFO8,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO8,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 8 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO9 = resetBit(PERM_INFO9,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO9,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 9 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO10 = resetBit(PERM_INFO10,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO10,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 10 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO11 = resetBit(PERM_INFO11,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO11,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 11 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO12 = resetBit(PERM_INFO12,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO12,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 12 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO13 = resetBit(PERM_INFO13,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO13,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 13 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO14 = resetBit(PERM_INFO14,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO14,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 14 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO15 = resetBit(PERM_INFO15,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO15,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    ELSEIF num = 15 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO16 = resetBit(PERM_INFO16,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) WHERE testNumber(PERM_INFO16,IF(OLD.HMI_ID%32=0,32,OLD.HMI_ID%32)) AND PERM_TYPE = '0';
    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'),'hmi_type_info', 'hmi_id', OLD.HMI_ID, '', getTableSubSystem('hmi_type_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_HMI_TYPE_INFO ON hmi_type_info;
CREATE TRIGGER TDA_HMI_TYPE_INFO AFTER DELETE ON hmi_type_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_HMI_TYPE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_IA_DISPOSAL_PLAN() 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'),'ia_disposal_plan', 'KEY_ID_TAG',        '',NEW.KEY_ID_TAG, getTableSubSystem('ia_disposal_plan'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_IA_DISPOSAL_PLAN ON ia_disposal_plan;
CREATE TRIGGER TIA_IA_DISPOSAL_PLAN AFTER INSERT ON ia_disposal_plan FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_IA_DISPOSAL_PLAN();



CREATE OR REPLACE FUNCTION FUNC_TUA_IA_DISPOSAL_PLAN() 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'),'ia_disposal_plan', 'KEY_ID_TAG', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('ia_disposal_plan'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_IA_DISPOSAL_PLAN ON ia_disposal_plan;
CREATE TRIGGER TUA_IA_DISPOSAL_PLAN AFTER UPDATE ON ia_disposal_plan FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_IA_DISPOSAL_PLAN();



CREATE OR REPLACE FUNCTION FUNC_TDA_IA_DISPOSAL_PLAN() 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'),'ia_disposal_plan', 'KEY_ID_TAG', OLD.KEY_ID_TAG, '', getTableSubSystem('ia_disposal_plan'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_IA_DISPOSAL_PLAN ON ia_disposal_plan;
CREATE TRIGGER TDA_IA_DISPOSAL_PLAN AFTER DELETE ON ia_disposal_plan FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_IA_DISPOSAL_PLAN();



CREATE OR REPLACE FUNCTION FUNC_TI_MIX() RETURNS TRIGGER AS $$
BEGIN
   
IF COALESCE(NEW.IS_CONTROL,0) <> 0 
THEN
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN
        INSERT INTO mix_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,     CTRL_ACT_NAME, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, CTRL_TOLERANCE, RANGE_UP, RANGE_LOW, RTU_TAG,    OFFSET_NO, CTRL_ALIAS, POINT_TP_NAME) 
        VALUES (
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM, '',            0,         1,             30,            30,           0,    1,    0.01,           65536,     0,         NEW.RTU_TAG, '-1',        '',      NEW.POINT_TP_NAME 
        );
    ELSE
        INSERT INTO mix_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,     CTRL_ACT_NAME, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, CTRL_TOLERANCE, RANGE_UP, RANGE_LOW, RTU_TAG,    OFFSET_NO, CTRL_ALIAS, POINT_TP_NAME) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM, CTRL_ACT_NAME, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, CTRL_TOLERANCE, RANGE_UP, RANGE_LOW, NEW.RTU_TAG, '-1',        '',      NEW.POINT_TP_NAME 
        FROM mix_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME;
    END IF;
END IF;
IF COALESCE(NEW.IS_SAMPLE,0) <> 0
THEN 
INSERT INTO sample_define (
TAG_NAME,                                        DESCRIPTION,      LOCATION_ID,      SUB_SYSTEM,     DEVICE_TAG, POINT_TYPE,     SAMPLE_PERIOD,     SAMPLE_DEADBAND, VALUE, STATUS, UPDATE_TIME) VALUES 
(NEW.TAG_NAME, NEW.DESCRIPTION,  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEVICE, NEW.POINT_TYPE, NEW.SAMPLE_PERIOD, 0,                0,  0,  0       );
END IF;
IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN
    INSERT INTO mix_map_to_fes (
    KEY_ID_TAG,                          LOCATION_ID,      SUB_SYSTEM,     DEV_TYPE,      POINT_TP_NAME,      RTU_TAG,     DOT_NO, BASE, COEFF, RANGE_UP, RANGE_LOW, IS_FILTER_ERROR, IS_FILTER_DISTURB, DISTURB_TIME, DESCRIPTION) 
    VALUES (
    concat('mix' , '.' , NEW.TAG_NAME),  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEV_TYPE,  NEW.POINT_TP_NAME,  NEW.RTU_TAG, '-1',   0,     1,     65535,    0,         1,               1,                5,            NEW.DESCRIPTION
    );
ELSE
    INSERT INTO mix_map_to_fes (
    KEY_ID_TAG,                          LOCATION_ID,      SUB_SYSTEM,     DEV_TYPE,      POINT_TP_NAME,      RTU_TAG,     DOT_NO, BASE, COEFF, RANGE_UP, RANGE_LOW, IS_FILTER_ERROR, IS_FILTER_DISTURB, DISTURB_TIME, DESCRIPTION) SELECT
    concat('mix' , '.' , NEW.TAG_NAME),  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEV_TYPE,  NEW.POINT_TP_NAME,  NEW.RTU_TAG, '-1',   BASE, COEFF, RANGE_UP, RANGE_LOW, IS_FILTER_ERROR, IS_FILTER_DISTURB, DISTURB_TIME, NEW.DESCRIPTION
    FROM mix_temp_define WHERE TAG_NAME = NEW.POINT_TP_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('insert'),'mix', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('mix'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_MIX ON mix;
CREATE TRIGGER TI_MIX AFTER INSERT ON mix FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_MIX();



CREATE OR REPLACE FUNCTION FUNC_TU_MIX() RETURNS TRIGGER AS $$
BEGIN
IF COALESCE(OLD.IS_CONTROL,0) != 0 AND COALESCE(NEW.IS_CONTROL,0) = 0
THEN
BEGIN 
    DELETE FROM mix_control WHERE TAG_NAME=OLD.TAG_NAME;
	DELETE FROM opt_interlock_para WHERE TAG_NAME=OLD.TAG_NAME;
END;
ELSEIF COALESCE(OLD.IS_CONTROL,0) = 0 AND COALESCE(NEW.IS_CONTROL,0) != 0
THEN 
BEGIN
    
    IF COALESCE(NEW.POINT_TP_NAME,'') = '' THEN
        INSERT INTO mix_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,     CTRL_ACT_NAME, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, CTRL_TOLERANCE, RANGE_UP, RANGE_LOW, RTU_TAG,    OFFSET_NO, CTRL_ALIAS, POINT_TP_NAME) 
        VALUES (
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM, '',            0,         1,             30,            30,           0,    1,    0.01,           65536,     0,         NEW.RTU_TAG, '-1',        '',      NEW.POINT_TP_NAME 
        );
    ELSE
        INSERT INTO mix_control(
        TAG_NAME,     LOCATION_ID,     SUB_SYSTEM,     CTRL_ACT_NAME, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, CTRL_TOLERANCE, RANGE_UP, RANGE_LOW, RTU_TAG,    OFFSET_NO, CTRL_ALIAS, POINT_TP_NAME) SELECT 
        NEW.TAG_NAME, NEW.LOCATION_ID, NEW.SUB_SYSTEM, CTRL_ACT_NAME, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, CTRL_TOLERANCE, RANGE_UP, RANGE_LOW, NEW.RTU_TAG, '-1',        '',      NEW.POINT_TP_NAME 
        FROM mix_temp_define WHERE TAG_NAME=NEW.POINT_TP_NAME;
    END IF;
    
END;
ELSE
BEGIN
    IF NEW.TAG_NAME<>OLD.TAG_NAME OR NEW.LOCATION_ID<>OLD.LOCATION_ID OR NEW.SUB_SYSTEM<>OLD.SUB_SYSTEM OR NEW.RTU_TAG<>OLD.RTU_TAG 
    THEN 
        UPDATE mix_control 
        SET 
        TAG_NAME = NEW.TAG_NAME,
        LOCATION_ID = NEW.LOCATION_ID,
        SUB_SYSTEM = NEW.SUB_SYSTEM,
        RTU_TAG = NEW.RTU_TAG,
        POINT_TP_NAME = NEW.POINT_TP_NAME
        WHERE TAG_NAME = OLD.TAG_NAME;
    END IF;
END; 
END IF;
IF  COALESCE(OLD.IS_SAMPLE,0) != 0 AND COALESCE(NEW.IS_SAMPLE,0) = 0 
THEN
BEGIN 
    DELETE FROM sample_define WHERE TAG_NAME=   OLD.TAG_NAME ;
END;
ELSEIF COALESCE(OLD.IS_SAMPLE,0) = 0 AND COALESCE(NEW.IS_SAMPLE,0) != 0
THEN 
BEGIN
    INSERT INTO sample_define (
    TAG_NAME,                                        DESCRIPTION,      LOCATION_ID,      SUB_SYSTEM,     DEVICE_TAG, POINT_TYPE,     SAMPLE_PERIOD,   SAMPLE_DEADBAND, VALUE, STATUS, UPDATE_TIME) VALUES 
    ( NEW.TAG_NAME, NEW.DESCRIPTION,  NEW.LOCATION_ID,  NEW.SUB_SYSTEM, NEW.DEVICE, NEW.POINT_TYPE, NEW.SAMPLE_PERIOD, 0,            0,  0,  0       );
END;
ELSE
BEGIN
    IF NEW.TAG_NAME<>OLD.TAG_NAME OR NEW.DESCRIPTION<>OLD.DESCRIPTION OR NEW.LOCATION_ID<>OLD.LOCATION_ID OR NEW.SUB_SYSTEM<>OLD.SUB_SYSTEM
    OR NEW.DEVICE<>OLD.DEVICE OR NEW.POINT_TYPE<>OLD.POINT_TYPE OR NEW.SAMPLE_PERIOD<>OLD.SAMPLE_PERIOD
    THEN 
        UPDATE sample_define 
        SET 
        TAG_NAME = NEW.TAG_NAME ,
        DESCRIPTION = NEW.DESCRIPTION ,
        LOCATION_ID = NEW.LOCATION_ID ,
        SUB_SYSTEM = NEW.SUB_SYSTEM ,
        DEVICE_TAG = NEW.DEVICE,
        POINT_TYPE = NEW.POINT_TYPE,
        SAMPLE_PERIOD = NEW.SAMPLE_PERIOD
        WHERE TAG_NAME=   OLD.TAG_NAME;
    END IF;
END; 
END IF;
IF NEW.TAG_NAME<>OLD.TAG_NAME OR NEW.LOCATION_ID<>OLD.LOCATION_ID OR NEW.SUB_SYSTEM<>OLD.SUB_SYSTEM OR NEW.DEV_TYPE<>OLD.DEV_TYPE
OR NEW.POINT_TP_NAME<>OLD.POINT_TP_NAME OR NEW.RTU_TAG<>OLD.RTU_TAG OR NEW.POINT_TYPE<>OLD.POINT_TYPE OR NEW.DESCRIPTION<>OLD.DESCRIPTION
THEN 
    UPDATE mix_map_to_fes SET 
    KEY_ID_TAG = concat('mix' , '.' , NEW.TAG_NAME),
    LOCATION_ID =NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM,
    DEV_TYPE = NEW.DEV_TYPE,
    POINT_TP_NAME = NEW.POINT_TP_NAME,
    RTU_TAG = NEW.RTU_TAG,
    DESCRIPTION = NEW.DESCRIPTION
    WHERE KEY_ID_TAG=concat('mix' , '.' , 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', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('mix'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_MIX ON mix;
CREATE TRIGGER TU_MIX AFTER UPDATE ON mix FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_MIX();



CREATE OR REPLACE FUNCTION FUNC_TD_MIX() RETURNS TRIGGER AS $$
BEGIN
    
IF  COALESCE(OLD.IS_CONTROL,0) <> 0 
THEN
    DELETE FROM mix_control WHERE TAG_NAME=OLD.TAG_NAME;
END IF;
IF  COALESCE(OLD.IS_SAMPLE,0) <> 0 
THEN
    DELETE FROM sample_define WHERE TAG_NAME=  OLD.TAG_NAME ;
END IF;
DELETE FROM mix_map_to_fes WHERE KEY_ID_TAG=concat('mix' , '.' , OLD.TAG_NAME);
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_MIX ON mix;
CREATE TRIGGER TD_MIX BEFORE DELETE ON mix FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_MIX();



CREATE OR REPLACE FUNCTION FUNC_TDA_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('delete'),'mix', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('mix'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_MIX ON mix;
CREATE TRIGGER TDA_MIX AFTER DELETE ON mix FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_MIX();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_CTRL_ACT_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'),'opt_ctrl_act_define', 'ctrl_grp_name,ctrl_grp_no', '', concat(NEW.CTRL_GRP_NAME,',',NEW.CTRL_GRP_NO), getTableSubSystem('opt_ctrl_act_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_CTRL_ACT_DEFINE ON opt_ctrl_act_define;
CREATE TRIGGER TIA_OPT_CTRL_ACT_DEFINE AFTER INSERT ON opt_ctrl_act_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_CTRL_ACT_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_CTRL_ACT_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'),'opt_ctrl_act_define', 'ctrl_grp_name,ctrl_grp_no', concat(OLD.CTRL_GRP_NAME,',',OLD.CTRL_GRP_NO), concat(NEW.CTRL_GRP_NAME,',',NEW.CTRL_GRP_NO), getTableSubSystem('opt_ctrl_act_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_CTRL_ACT_DEFINE ON opt_ctrl_act_define;
CREATE TRIGGER TUA_OPT_CTRL_ACT_DEFINE AFTER UPDATE ON opt_ctrl_act_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_CTRL_ACT_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_CTRL_ACT_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'),'opt_ctrl_act_define', 'ctrl_grp_name,ctrl_grp_no', concat(OLD.CTRL_GRP_NAME,',',OLD.CTRL_GRP_NO), '', getTableSubSystem('opt_ctrl_act_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_CTRL_ACT_DEFINE ON opt_ctrl_act_define;
CREATE TRIGGER TDA_OPT_CTRL_ACT_DEFINE AFTER DELETE ON opt_ctrl_act_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_CTRL_ACT_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_HANDOVER_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'),'opt_handover_grp', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('opt_handover_grp'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_HANDOVER_GRP ON opt_handover_grp;
CREATE TRIGGER TIA_OPT_HANDOVER_GRP AFTER INSERT ON opt_handover_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_HANDOVER_GRP();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_HANDOVER_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('update'),'opt_handover_grp', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('opt_handover_grp'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_HANDOVER_GRP ON opt_handover_grp;
CREATE TRIGGER TUA_OPT_HANDOVER_GRP AFTER UPDATE ON opt_handover_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_HANDOVER_GRP();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_HANDOVER_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('delete'),'opt_handover_grp', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('opt_handover_grp'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_HANDOVER_GRP ON opt_handover_grp;
CREATE TRIGGER TDA_OPT_HANDOVER_GRP AFTER DELETE ON opt_handover_grp FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_HANDOVER_GRP();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_INTERLOCK_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'),'opt_interlock_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('opt_interlock_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_INTERLOCK_DEFINE ON opt_interlock_define;
CREATE TRIGGER TIA_OPT_INTERLOCK_DEFINE AFTER INSERT ON opt_interlock_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_INTERLOCK_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_INTERLOCK_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'),'opt_interlock_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('opt_interlock_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_INTERLOCK_DEFINE ON opt_interlock_define;
CREATE TRIGGER TUA_OPT_INTERLOCK_DEFINE AFTER UPDATE ON opt_interlock_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_INTERLOCK_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_INTERLOCK_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'),'opt_interlock_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('opt_interlock_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_INTERLOCK_DEFINE ON opt_interlock_define;
CREATE TRIGGER TDA_OPT_INTERLOCK_DEFINE AFTER DELETE ON opt_interlock_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_INTERLOCK_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_INTERLOCK_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'),'opt_interlock_para', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('opt_interlock_para'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_INTERLOCK_PARA ON opt_interlock_para;
CREATE TRIGGER TIA_OPT_INTERLOCK_PARA AFTER INSERT ON opt_interlock_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_INTERLOCK_PARA();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_INTERLOCK_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'),'opt_interlock_para', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('opt_interlock_para'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_INTERLOCK_PARA ON opt_interlock_para;
CREATE TRIGGER TUA_OPT_INTERLOCK_PARA AFTER UPDATE ON opt_interlock_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_INTERLOCK_PARA();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_INTERLOCK_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'),'opt_interlock_para', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('opt_interlock_para'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_INTERLOCK_PARA ON opt_interlock_para;
CREATE TRIGGER TDA_OPT_INTERLOCK_PARA AFTER DELETE ON opt_interlock_para FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_INTERLOCK_PARA();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_SHIELD_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'),'opt_shield_define', 'shield_id', '', NEW.SHIELD_ID, getTableSubSystem('opt_shield_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_SHIELD_DEFINE ON opt_shield_define;
CREATE TRIGGER TIA_OPT_SHIELD_DEFINE AFTER INSERT ON opt_shield_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_SHIELD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_SHIELD_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'),'opt_shield_define', 'shield_id', OLD.SHIELD_ID, NEW.SHIELD_ID, getTableSubSystem('opt_shield_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_SHIELD_DEFINE ON opt_shield_define;
CREATE TRIGGER TUA_OPT_SHIELD_DEFINE AFTER UPDATE ON opt_shield_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_SHIELD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_SHIELD_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'),'opt_shield_define', 'shield_id', OLD.SHIELD_ID, '', getTableSubSystem('opt_shield_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_SHIELD_DEFINE ON opt_shield_define;
CREATE TRIGGER TDA_OPT_SHIELD_DEFINE AFTER DELETE ON opt_shield_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_SHIELD_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_SHIELD_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'),'opt_shield_info', 'shield_id', '', NEW.SHIELD_ID, getTableSubSystem('opt_shield_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_SHIELD_INFO ON opt_shield_info;
CREATE TRIGGER TIA_OPT_SHIELD_INFO AFTER INSERT ON opt_shield_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_SHIELD_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_SHIELD_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'),'opt_shield_info', 'shield_id', OLD.SHIELD_ID, NEW.SHIELD_ID, getTableSubSystem('opt_shield_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_SHIELD_INFO ON opt_shield_info;
CREATE TRIGGER TUA_OPT_SHIELD_INFO AFTER UPDATE ON opt_shield_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_SHIELD_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_SHIELD_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'),'opt_shield_info', 'shield_id', OLD.SHIELD_ID, '', getTableSubSystem('opt_shield_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_SHIELD_INFO ON opt_shield_info;
CREATE TRIGGER TDA_OPT_SHIELD_INFO AFTER DELETE ON opt_shield_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_SHIELD_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_SHIELD_OBJ() 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'),'opt_shield_obj', 'shield_id,key_id_tag', '', concat(NEW.SHIELD_ID,',',NEW.KEY_ID_TAG), getTableSubSystem('opt_shield_obj'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_SHIELD_OBJ ON opt_shield_obj;
CREATE TRIGGER TIA_OPT_SHIELD_OBJ AFTER INSERT ON opt_shield_obj FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_SHIELD_OBJ();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_SHIELD_OBJ() 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'),'opt_shield_obj', 'shield_id,key_id_tag', concat(OLD.SHIELD_ID,',',OLD.KEY_ID_TAG), concat(NEW.SHIELD_ID,',',NEW.KEY_ID_TAG), getTableSubSystem('opt_shield_obj'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_SHIELD_OBJ ON opt_shield_obj;
CREATE TRIGGER TUA_OPT_SHIELD_OBJ AFTER UPDATE ON opt_shield_obj FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_SHIELD_OBJ();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_SHIELD_OBJ() 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'),'opt_shield_obj', 'shield_id,key_id_tag', concat(OLD.SHIELD_ID,',',OLD.KEY_ID_TAG), '', getTableSubSystem('opt_shield_obj'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_SHIELD_OBJ ON opt_shield_obj;
CREATE TRIGGER TDA_OPT_SHIELD_OBJ AFTER DELETE ON opt_shield_obj FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_SHIELD_OBJ();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_SHIELD_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'),'opt_shield_time', 'time_id', '', NEW.TIME_ID, getTableSubSystem('opt_shield_time'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_SHIELD_TIME ON opt_shield_time;
CREATE TRIGGER TIA_OPT_SHIELD_TIME AFTER INSERT ON opt_shield_time FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_SHIELD_TIME();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_SHIELD_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'),'opt_shield_time', 'time_id', OLD.TIME_ID, NEW.TIME_ID, getTableSubSystem('opt_shield_time'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_SHIELD_TIME ON opt_shield_time;
CREATE TRIGGER TUA_OPT_SHIELD_TIME AFTER UPDATE ON opt_shield_time FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_SHIELD_TIME();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_SHIELD_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'),'opt_shield_time', 'time_id', OLD.TIME_ID, '', getTableSubSystem('opt_shield_time'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_SHIELD_TIME ON opt_shield_time;
CREATE TRIGGER TDA_OPT_SHIELD_TIME AFTER DELETE ON opt_shield_time FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_SHIELD_TIME();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_TAG_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'),'opt_tag_info', 'key_id_tag,tag_type', '', concat(NEW.KEY_ID_TAG,',',NEW.TAG_TYPE), getTableSubSystem('opt_tag_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_TAG_INFO ON opt_tag_info;
CREATE TRIGGER TIA_OPT_TAG_INFO AFTER INSERT ON opt_tag_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_TAG_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_TAG_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'),'opt_tag_info', 'key_id_tag,tag_type', concat(OLD.KEY_ID_TAG,',',OLD.TAG_TYPE), concat(NEW.KEY_ID_TAG,',',NEW.TAG_TYPE), getTableSubSystem('opt_tag_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_TAG_INFO ON opt_tag_info;
CREATE TRIGGER TUA_OPT_TAG_INFO AFTER UPDATE ON opt_tag_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_TAG_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_TAG_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'),'opt_tag_info', 'key_id_tag,tag_type', concat(OLD.KEY_ID_TAG,',',OLD.TAG_TYPE), '', getTableSubSystem('opt_tag_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_TAG_INFO ON opt_tag_info;
CREATE TRIGGER TDA_OPT_TAG_INFO AFTER DELETE ON opt_tag_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_TAG_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_TOKEN_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'),'opt_token_define', 'token_id', '', NEW.TOKEN_ID, getTableSubSystem('opt_token_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_TOKEN_DEFINE ON opt_token_define;
CREATE TRIGGER TIA_OPT_TOKEN_DEFINE AFTER INSERT ON opt_token_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_TOKEN_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_TOKEN_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'),'opt_token_define', 'token_id', OLD.TOKEN_ID, NEW.TOKEN_ID, getTableSubSystem('opt_token_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_TOKEN_DEFINE ON opt_token_define;
CREATE TRIGGER TUA_OPT_TOKEN_DEFINE AFTER UPDATE ON opt_token_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_TOKEN_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_TOKEN_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'),'opt_token_define', 'token_id', OLD.TOKEN_ID, '', getTableSubSystem('opt_token_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_TOKEN_DEFINE ON opt_token_define;
CREATE TRIGGER TDA_OPT_TOKEN_DEFINE AFTER DELETE ON opt_token_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_TOKEN_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_OPT_TOKEN_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'),'opt_token_info', 'token_id,key_id_tag', '', concat(NEW.TOKEN_ID,',',NEW.KEY_ID_TAG), getTableSubSystem('opt_token_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_OPT_TOKEN_INFO ON opt_token_info;
CREATE TRIGGER TIA_OPT_TOKEN_INFO AFTER INSERT ON opt_token_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_OPT_TOKEN_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_OPT_TOKEN_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'),'opt_token_info', 'token_id,key_id_tag', concat(OLD.TOKEN_ID,',',OLD.KEY_ID_TAG), concat(NEW.TOKEN_ID,',',NEW.KEY_ID_TAG), getTableSubSystem('opt_token_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_OPT_TOKEN_INFO ON opt_token_info;
CREATE TRIGGER TUA_OPT_TOKEN_INFO AFTER UPDATE ON opt_token_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_OPT_TOKEN_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_OPT_TOKEN_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'),'opt_token_info', 'token_id,key_id_tag', concat(OLD.TOKEN_ID,',',OLD.KEY_ID_TAG), '', getTableSubSystem('opt_token_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_OPT_TOKEN_INFO ON opt_token_info;
CREATE TRIGGER TDA_OPT_TOKEN_INFO AFTER DELETE ON opt_token_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_OPT_TOKEN_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_REGION_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'),'region_info', 'region_id', '', NEW.REGION_ID, getTableSubSystem('region_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_REGION_INFO ON region_info;
CREATE TRIGGER TIA_REGION_INFO AFTER INSERT ON region_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_REGION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_REGION_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'),'region_info', 'region_id', OLD.REGION_ID, NEW.REGION_ID, getTableSubSystem('region_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_REGION_INFO ON region_info;
CREATE TRIGGER TUA_REGION_INFO AFTER UPDATE ON region_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_REGION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_REGION_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'),'region_info', 'region_id', OLD.REGION_ID, '', getTableSubSystem('region_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_REGION_INFO ON region_info;
CREATE TRIGGER TDA_REGION_INFO AFTER DELETE ON region_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_REGION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_REPORT_TYPE_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'),'report_type_info', 'report_id', '', NEW.REPORT_ID, getTableSubSystem('report_type_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_REPORT_TYPE_INFO ON report_type_info;
CREATE TRIGGER TIA_REPORT_TYPE_INFO AFTER INSERT ON report_type_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_REPORT_TYPE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_REPORT_TYPE_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'),'report_type_info', 'report_id', OLD.REPORT_ID, NEW.REPORT_ID, getTableSubSystem('report_type_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_REPORT_TYPE_INFO ON report_type_info;
CREATE TRIGGER TUA_REPORT_TYPE_INFO AFTER UPDATE ON report_type_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_REPORT_TYPE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_REPORT_TYPE_INFO() RETURNS TRIGGER AS $$
    --PERM_TYPE = 1是报表,0是HMI画面
    DECLARE num int;
BEGIN
    num =  DIV((OLD.REPORT_ID-1),32);
   
    --角色表的修改
    IF num = 0 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO1 = resetBit(PERM_INFO1,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO1,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 1 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO2 = resetBit(PERM_INFO2,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO2,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 2 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO3 = resetBit(PERM_INFO3,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO3,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 3 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO4 = resetBit(PERM_INFO4,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO4,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 4 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO5 = resetBit(PERM_INFO5,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO5,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 5 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO6 = resetBit(PERM_INFO6,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO6,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 6 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO7 = resetBit(PERM_INFO7,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO7,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 7 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO8 = resetBit(PERM_INFO8,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO8,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 8 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO9 = resetBit(PERM_INFO9,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO9,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 9 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO10 = resetBit(PERM_INFO10,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO10,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 10 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO11 = resetBit(PERM_INFO11,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO11,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 11 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO12 = resetBit(PERM_INFO12,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO12,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 12 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO13 = resetBit(PERM_INFO13,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO13,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 13 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO14 = resetBit(PERM_INFO14,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO14,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 14 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO15 = resetBit(PERM_INFO15,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO15,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 15 THEN
        UPDATE rm_role_hmi_info SET PERM_INFO16 = resetBit(PERM_INFO16,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO16,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    END IF;
    
    --用户组表的修改
    IF num = 0 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO1 = resetBit(PERM_INFO1,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO1,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 1 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO2 = resetBit(PERM_INFO2,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO2,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 2 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO3 = resetBit(PERM_INFO3,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO3,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 3 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO4 = resetBit(PERM_INFO4,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO4,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 4 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO5 = resetBit(PERM_INFO5,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO5,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 5 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO6 = resetBit(PERM_INFO6,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO6,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 6 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO7 = resetBit(PERM_INFO7,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO7,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 7 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO8 = resetBit(PERM_INFO8,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO8,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 8 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO9 = resetBit(PERM_INFO9,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO9,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 9 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO10 = resetBit(PERM_INFO10,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO10,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 10 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO11 = resetBit(PERM_INFO11,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO11,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 11 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO12 = resetBit(PERM_INFO12,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO12,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 12 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO13 = resetBit(PERM_INFO13,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO13,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 13 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO14 = resetBit(PERM_INFO14,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO14,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 14 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO15 = resetBit(PERM_INFO15,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO15,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    ELSEIF num = 15 THEN
        UPDATE rm_userg_hmi_info SET PERM_INFO16 = resetBit(PERM_INFO16,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) WHERE testNumber(PERM_INFO16,IF(OLD.REPORT_ID%32=0,32,OLD.REPORT_ID%32)) AND PERM_TYPE = '1';
    END IF;
    
    DELETE FROM t_sys_table_file_tree WHERE TREECODE = OLD.REPORT_DEFINE;
    
IF getTriggerFlag() = TRUE THEN 
INSERT 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'),'report_type_info', 'report_id', OLD.REPORT_ID, '', getTableSubSystem('report_type_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_REPORT_TYPE_INFO ON report_type_info;
CREATE TRIGGER TDA_REPORT_TYPE_INFO AFTER DELETE ON report_type_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_REPORT_TYPE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_DEPT_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'),'rm_dept_def', 'dept_id', '', NEW.DEPT_ID, getTableSubSystem('rm_dept_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_DEPT_DEF ON rm_dept_def;
CREATE TRIGGER TIA_RM_DEPT_DEF AFTER INSERT ON rm_dept_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_DEPT_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_DEPT_DEF() RETURNS TRIGGER AS $$
    DECLARE oldNum int;
    DECLARE newNum int;
BEGIN
    oldNum =  DIV((OLD.DEPT_ID-1),32);
    newNum =  DIV((NEW.DEPT_ID-1),32);
    
    IF NEW.DEPT_ID <> OLD.DEPT_ID THEN
    BEGIN
        --应该先赋值新的,再删除删除旧的,避免先删除旧的,无法再定位该为哪个用户赋新值
        
        IF oldNum = 0 THEN
            UPDATE rm_user_def SET 
            PERM_DEPT1 = IF(newNum=0, setBit(PERM_DEPT1,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT1),
            PERM_DEPT2 = IF(newNum=1, setBit(PERM_DEPT2,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT2),
            PERM_DEPT3 = IF(newNum=2, setBit(PERM_DEPT3,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT3),
            PERM_DEPT4 = IF(newNum=3, setBit(PERM_DEPT4,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT4)
            WHERE testNumber(PERM_DEPT1,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
        ELSEIF oldNum = 1 THEN
            UPDATE rm_user_def SET 
            PERM_DEPT1 = IF(newNum=0, setBit(PERM_DEPT1,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT1),
            PERM_DEPT2 = IF(newNum=1, setBit(PERM_DEPT2,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT2),
            PERM_DEPT3 = IF(newNum=2, setBit(PERM_DEPT3,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT3),
            PERM_DEPT4 = IF(newNum=3, setBit(PERM_DEPT4,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT4)
            WHERE testNumber(PERM_DEPT2,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
        ELSEIF oldNum = 2 THEN
            UPDATE rm_user_def SET 
            PERM_DEPT1 = IF(newNum=0, setBit(PERM_DEPT1,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT1),
            PERM_DEPT2 = IF(newNum=1, setBit(PERM_DEPT2,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT2),
            PERM_DEPT3 = IF(newNum=2, setBit(PERM_DEPT3,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT3),
            PERM_DEPT4 = IF(newNum=3, setBit(PERM_DEPT4,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT4)
            WHERE testNumber(PERM_DEPT3,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
        ELSEIF oldNum = 3 THEN
            UPDATE rm_user_def SET 
            PERM_DEPT1 = IF(newNum=0, setBit(PERM_DEPT1,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT1),
            PERM_DEPT2 = IF(newNum=1, setBit(PERM_DEPT2,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT2),
            PERM_DEPT3 = IF(newNum=2, setBit(PERM_DEPT3,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT3),
            PERM_DEPT4 = IF(newNum=3, setBit(PERM_DEPT4,IF(NEW.DEPT_ID%32=0,32,NEW.DEPT_ID%32)), PERM_DEPT4)
            WHERE testNumber(PERM_DEPT4,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
        END IF;
        
        --赋值新的之后,删除旧的
        
        IF oldNum = 0 THEN
            UPDATE rm_user_def SET PERM_DEPT1 = resetBit(PERM_DEPT1,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32)) WHERE testNumber(PERM_DEPT1,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
        ELSEIF oldNum = 1 THEN
            UPDATE rm_user_def SET PERM_DEPT2 = resetBit(PERM_DEPT2,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32)) WHERE testNumber(PERM_DEPT2,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
        ELSEIF oldNum = 2 THEN
            UPDATE rm_user_def SET PERM_DEPT3 = resetBit(PERM_DEPT3,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32)) WHERE testNumber(PERM_DEPT3,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
        ELSEIF oldNum = 3 THEN
            UPDATE rm_user_def SET PERM_DEPT4 = resetBit(PERM_DEPT4,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32)) WHERE testNumber(PERM_DEPT4,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
        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'),'rm_dept_def', 'dept_id', OLD.DEPT_ID, NEW.DEPT_ID, getTableSubSystem('rm_dept_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_DEPT_DEF ON rm_dept_def;
CREATE TRIGGER TUA_RM_DEPT_DEF AFTER UPDATE ON rm_dept_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_DEPT_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_DEPT_DEF() RETURNS TRIGGER AS $$
    DECLARE num int;
BEGIN
--删除引用了该部门的用户
    num =  DIV((OLD.DEPT_ID-1),32);
   
    IF num = 0 THEN
        UPDATE rm_user_def SET PERM_DEPT1 = resetBit(PERM_DEPT1,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32)) WHERE testNumber(PERM_DEPT1,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
    ELSEIF num = 1 THEN
        UPDATE rm_user_def SET PERM_DEPT2 = resetBit(PERM_DEPT2,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32)) WHERE testNumber(PERM_DEPT2,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
    ELSEIF num = 2 THEN
        UPDATE rm_user_def SET PERM_DEPT3 = resetBit(PERM_DEPT3,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32)) WHERE testNumber(PERM_DEPT3,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
    ELSEIF num = 3 THEN
        UPDATE rm_user_def SET PERM_DEPT4 = resetBit(PERM_DEPT4,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32)) WHERE testNumber(PERM_DEPT4,IF(OLD.DEPT_ID%32=0,32,OLD.DEPT_ID%32));
    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'),'rm_dept_def', 'dept_id', OLD.DEPT_ID, '', getTableSubSystem('rm_dept_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_DEPT_DEF ON rm_dept_def;
CREATE TRIGGER TDA_RM_DEPT_DEF AFTER DELETE ON rm_dept_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_DEPT_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_NORM_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'),'rm_norm_func_def', 'func_id', '', NEW.FUNC_ID, getTableSubSystem('rm_norm_func_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_NORM_FUNC_DEF ON rm_norm_func_def;
CREATE TRIGGER TIA_RM_NORM_FUNC_DEF AFTER INSERT ON rm_norm_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_NORM_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_NORM_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('update'),'rm_norm_func_def', 'func_id', OLD.FUNC_ID, NEW.FUNC_ID, getTableSubSystem('rm_norm_func_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_NORM_FUNC_DEF ON rm_norm_func_def;
CREATE TRIGGER TUA_RM_NORM_FUNC_DEF AFTER UPDATE ON rm_norm_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_NORM_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_NORM_FUNC_DEF() RETURNS TRIGGER AS $$
    DECLARE num int;
BEGIN
--删除引用了该功能的角色
    num =  DIV((OLD.FUNC_ID-1),32);
   
    IF num = 0 THEN
        UPDATE rm_role_def SET PERM_NOM_FUNC1 = resetBit(PERM_NOM_FUNC1,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32)) WHERE testNumber(PERM_NOM_FUNC1,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32));
    ELSEIF num = 1 THEN
        UPDATE rm_role_def SET PERM_NOM_FUNC2 = resetBit(PERM_NOM_FUNC2,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32)) WHERE testNumber(PERM_NOM_FUNC2,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32));
    ELSEIF num = 2 THEN
        UPDATE rm_role_def SET PERM_NOM_FUNC3 = resetBit(PERM_NOM_FUNC3,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32)) WHERE testNumber(PERM_NOM_FUNC3,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32));
    ELSEIF num = 3 THEN
        UPDATE rm_role_def SET PERM_NOM_FUNC4 = resetBit(PERM_NOM_FUNC4,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32)) WHERE testNumber(PERM_NOM_FUNC4,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32));
    ELSEIF num = 4 THEN
        UPDATE rm_role_def SET PERM_NOM_FUNC5 = resetBit(PERM_NOM_FUNC5,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32)) WHERE testNumber(PERM_NOM_FUNC5,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32));
    ELSEIF num = 5 THEN
        UPDATE rm_role_def SET PERM_NOM_FUNC6 = resetBit(PERM_NOM_FUNC6,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32)) WHERE testNumber(PERM_NOM_FUNC6,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32));
    ELSEIF num = 6 THEN
        UPDATE rm_role_def SET PERM_NOM_FUNC7 = resetBit(PERM_NOM_FUNC7,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32)) WHERE testNumber(PERM_NOM_FUNC7,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32));
    ELSEIF num = 7 THEN
        UPDATE rm_role_def SET PERM_NOM_FUNC8 = resetBit(PERM_NOM_FUNC8,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32)) WHERE testNumber(PERM_NOM_FUNC8,IF(OLD.FUNC_ID%32=0,32,OLD.FUNC_ID%32));
    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'),'rm_norm_func_def', 'func_id', OLD.FUNC_ID, '', getTableSubSystem('rm_norm_func_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_NORM_FUNC_DEF ON rm_norm_func_def;
CREATE TRIGGER TDA_RM_NORM_FUNC_DEF AFTER DELETE ON rm_norm_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_NORM_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_ROLE_DB_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'),'rm_role_db_info', 'perm_role,perm_table_name', '', concat(NEW.PERM_ROLE,',',NEW.PERM_TABLE_NAME), getTableSubSystem('rm_role_db_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_ROLE_DB_INFO ON rm_role_db_info;
CREATE TRIGGER TIA_RM_ROLE_DB_INFO AFTER INSERT ON rm_role_db_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_ROLE_DB_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_ROLE_DB_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'),'rm_role_db_info', 'perm_role,perm_table_name', concat(OLD.PERM_ROLE,',',OLD.PERM_TABLE_NAME), concat(NEW.PERM_ROLE,',',NEW.PERM_TABLE_NAME), getTableSubSystem('rm_role_db_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_ROLE_DB_INFO ON rm_role_db_info;
CREATE TRIGGER TUA_RM_ROLE_DB_INFO AFTER UPDATE ON rm_role_db_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_ROLE_DB_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_ROLE_DB_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'),'rm_role_db_info', 'perm_role,perm_table_name', concat(OLD.PERM_ROLE,',',OLD.PERM_TABLE_NAME), '', getTableSubSystem('rm_role_db_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_ROLE_DB_INFO ON rm_role_db_info;
CREATE TRIGGER TDA_RM_ROLE_DB_INFO AFTER DELETE ON rm_role_db_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_ROLE_DB_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_ROLE_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'),'rm_role_def', 'perm_id', '', NEW.PERM_ID, getTableSubSystem('rm_role_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_ROLE_DEF ON rm_role_def;
CREATE TRIGGER TIA_RM_ROLE_DEF AFTER INSERT ON rm_role_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_ROLE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_ROLE_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'),'rm_role_def', 'perm_id', OLD.PERM_ID, NEW.PERM_ID, getTableSubSystem('rm_role_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_ROLE_DEF ON rm_role_def;
CREATE TRIGGER TUA_RM_ROLE_DEF AFTER UPDATE ON rm_role_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_ROLE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_ROLE_DEF() RETURNS TRIGGER AS $$
    DECLARE num int;
BEGIN
--删除引用了该角色的数据
--用户组
    num =  DIV((OLD.PERM_ID-1),32);
   
    IF num = 0 THEN
        UPDATE rm_userg_def SET PERM_ROLE1 = resetBit(PERM_ROLE1,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_ROLE1,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 1 THEN
        UPDATE rm_userg_def SET PERM_ROLE2 = resetBit(PERM_ROLE2,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_ROLE2,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 2 THEN
        UPDATE rm_userg_def SET PERM_ROLE3 = resetBit(PERM_ROLE3,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_ROLE3,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 3 THEN
        UPDATE rm_userg_def SET PERM_ROLE4 = resetBit(PERM_ROLE4,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_ROLE4,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 4 THEN
        UPDATE rm_userg_def SET PERM_ROLE5 = resetBit(PERM_ROLE5,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_ROLE5,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 5 THEN
        UPDATE rm_userg_def SET PERM_ROLE6 = resetBit(PERM_ROLE6,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_ROLE6,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 6 THEN
        UPDATE rm_userg_def SET PERM_ROLE7 = resetBit(PERM_ROLE7,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_ROLE7,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 7 THEN
        UPDATE rm_userg_def SET PERM_ROLE8 = resetBit(PERM_ROLE8,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_ROLE8,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    END IF;
    
--rm_role_db_info
    DELETE FROM rm_role_db_info WHERE PERM_ROLE = OLD.PERM_ID;
--rm_role_hmi_info
    DELETE FROM rm_role_hmi_info WHERE PERM_ROLE = OLD.PERM_ID;
--rm_role_spef_reg
    DELETE FROM rm_role_spef_reg WHERE PERM_ROLE = OLD.PERM_ID;
--rm_userg_rsp_info
    DELETE FROM rm_userg_rsp_info WHERE PERM_ROLE = OLD.PERM_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'),'rm_role_def', 'perm_id', OLD.PERM_ID, '', getTableSubSystem('rm_role_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_ROLE_DEF ON rm_role_def;
CREATE TRIGGER TDA_RM_ROLE_DEF AFTER DELETE ON rm_role_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_ROLE_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_ROLE_HMI_INFO() RETURNS TRIGGER AS $$
    DECLARE num int;
    DECLARE pos int;
BEGIN
    --PERM_TYPE = 1是报表,0是HMI画面
    --先清除,后插入rm_userg_report_info
    num =  DIV((NEW.PERM_ROLE-1),32);
    pos = IF(NEW.PERM_ROLE%32=0,32,NEW.PERM_ROLE%32);
    
    IF NEW.PERM_TYPE = '1' THEN 
    BEGIN 
        --先删除关联了此角色的用户组的在rm_userg_report_info中的报表浏览权限
        DELETE FROM rm_userg_report_info WHERE EXISTS (
        SELECT rm_userg_def.PERM_ID FROM rm_userg_def 
        WHERE rm_userg_report_info.PERM_USER_GROUP = rm_userg_def.PERM_ID 
        AND testNumber(elt(num+1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),pos)
        );
        
        --重新把对应的报表浏览权限写入
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT rm_userg_def.PERM_ID,report_type_info.REPORT_DEFINE 
        FROM rm_userg_def,rm_userg_hmi_info ,report_type_info
        WHERE testNumber(elt(num+1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),pos)
        AND rm_userg_def.PERM_ID = rm_userg_hmi_info.PERM_USER_GROUP AND rm_userg_hmi_info.PERM_TYPE='1' 
        AND 
        testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
        PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32));
        
        --假如该用户组,还包含其他角色,其他角色的权限也要插入   
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT rm_userg_def.PERM_ID,report_type_info.REPORT_DEFINE
        FROM rm_role_hmi_info, rm_userg_def,report_type_info
        WHERE testNumber(elt( (DIV((PERM_ROLE-1),32)) +1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),
              IF(PERM_ROLE%32=0,32,PERM_ROLE%32))
        AND 
        testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
        PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32))
        ;
    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'),'rm_role_hmi_info', 'perm_role,perm_type', '', concat(NEW.PERM_ROLE,',',NEW.PERM_TYPE), getTableSubSystem('rm_role_hmi_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_ROLE_HMI_INFO ON rm_role_hmi_info;
CREATE TRIGGER TIA_RM_ROLE_HMI_INFO AFTER INSERT ON rm_role_hmi_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_ROLE_HMI_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_ROLE_HMI_INFO() RETURNS TRIGGER AS $$
    DECLARE oldNum int;
    DECLARE oldPos int;
    DECLARE newNum int;
    DECLARE newPos int;
BEGIN
    --PERM_TYPE = 1是报表,0是HMI画面
    --先清除,后插入rm_userg_report_info
    oldNum =  DIV((OLD.PERM_ROLE-1),32);
    oldPos = IF(OLD.PERM_ROLE%32=0,32,OLD.PERM_ROLE%32);
    newNum =  DIV((NEW.PERM_ROLE-1),32);
    newPos = IF(NEW.PERM_ROLE%32=0,32,NEW.PERM_ROLE%32);
    
    
    IF NEW.PERM_TYPE = '1' AND OLD.PERM_TYPE='1' THEN 
    BEGIN 
        --先删除关联了此角色的用户组的在rm_userg_report_info中的报表浏览权限
        DELETE FROM rm_userg_report_info WHERE EXISTS (
        SELECT rm_userg_def.PERM_ID FROM rm_userg_def 
        WHERE rm_userg_report_info.PERM_USER_GROUP = rm_userg_def.PERM_ID 
        AND testNumber(elt(oldNum+1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),oldPos)
        );
        
        --重新把对应的报表浏览权限写入
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT rm_userg_def.PERM_ID,report_type_info.REPORT_DEFINE 
        FROM rm_userg_def,rm_userg_hmi_info ,report_type_info
        WHERE testNumber(elt(newNum+1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),newPos)
        AND rm_userg_def.PERM_ID = rm_userg_hmi_info.PERM_USER_GROUP AND rm_userg_hmi_info.PERM_TYPE='1' 
        AND 
        testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
        PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32));
        
        --假如该用户组,还包含其他角色,其他角色的权限也要插入   
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT rm_userg_def.PERM_ID,report_type_info.REPORT_DEFINE
        FROM rm_role_hmi_info, rm_userg_def,report_type_info
        WHERE testNumber(elt( (DIV((PERM_ROLE-1),32)) +1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),
              IF(PERM_ROLE%32=0,32,PERM_ROLE%32))
        AND 
        testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
        PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32))
        ;
        
    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'),'rm_role_hmi_info', 'perm_role,perm_type', concat(OLD.PERM_ROLE,',',OLD.PERM_TYPE), concat(NEW.PERM_ROLE,',',NEW.PERM_TYPE), getTableSubSystem('rm_role_hmi_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_ROLE_HMI_INFO ON rm_role_hmi_info;
CREATE TRIGGER TUA_RM_ROLE_HMI_INFO AFTER UPDATE ON rm_role_hmi_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_ROLE_HMI_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_ROLE_HMI_INFO() RETURNS TRIGGER AS $$
    DECLARE num int;
    DECLARE pos int;
BEGIN
    --PERM_TYPE = 1是报表,0是HMI画面
    --先清除,后插入rm_userg_report_info
    num =  DIV((OLD.PERM_ROLE-1),32);
    pos = IF(OLD.PERM_ROLE%32=0,32,OLD.PERM_ROLE%32);
    
    IF OLD.PERM_TYPE = '1' THEN 
    BEGIN 
        --先删除关联了此角色的用户组的在rm_userg_report_info中的报表浏览权限
        DELETE FROM rm_userg_report_info WHERE EXISTS (
        SELECT rm_userg_def.PERM_ID FROM rm_userg_def 
        WHERE rm_userg_report_info.PERM_USER_GROUP = rm_userg_def.PERM_ID 
        AND testNumber(elt(num+1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),pos)
        );
        
        --重新把对应的报表浏览权限写入
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT rm_userg_def.PERM_ID,report_type_info.REPORT_DEFINE 
        FROM rm_userg_def,rm_userg_hmi_info ,report_type_info
        WHERE testNumber(elt(num+1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),pos)
        AND rm_userg_def.PERM_ID = rm_userg_hmi_info.PERM_USER_GROUP AND rm_userg_hmi_info.PERM_TYPE='1' 
        AND 
        testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
        PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32));
        
        --假如该用户组,还包含其他角色,其他角色的权限也要插入   
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT rm_userg_def.PERM_ID,report_type_info.REPORT_DEFINE
        FROM rm_role_hmi_info, rm_userg_def,report_type_info
        WHERE testNumber(elt( (DIV((PERM_ROLE-1),32)) +1,PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),
              IF(PERM_ROLE%32=0,32,PERM_ROLE%32))
        AND 
        testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
        PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32))
        ;
        
        
    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'),'rm_role_hmi_info', 'perm_role,perm_type', concat(OLD.PERM_ROLE,',',OLD.PERM_TYPE), '', getTableSubSystem('rm_role_hmi_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_ROLE_HMI_INFO ON rm_role_hmi_info;
CREATE TRIGGER TDA_RM_ROLE_HMI_INFO AFTER DELETE ON rm_role_hmi_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_ROLE_HMI_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_ROLE_SPEF_REG() 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'),'rm_role_spef_reg', 'perm_role,perm_spefunc', '', concat(NEW.PERM_ROLE,',',NEW.PERM_SPEFUNC), getTableSubSystem('rm_role_spef_reg'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_ROLE_SPEF_REG ON rm_role_spef_reg;
CREATE TRIGGER TIA_RM_ROLE_SPEF_REG AFTER INSERT ON rm_role_spef_reg FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_ROLE_SPEF_REG();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_ROLE_SPEF_REG() 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'),'rm_role_spef_reg', 'perm_role,perm_spefunc', concat(OLD.PERM_ROLE,',',OLD.PERM_SPEFUNC), concat(NEW.PERM_ROLE,',',NEW.PERM_SPEFUNC), getTableSubSystem('rm_role_spef_reg'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_ROLE_SPEF_REG ON rm_role_spef_reg;
CREATE TRIGGER TUA_RM_ROLE_SPEF_REG AFTER UPDATE ON rm_role_spef_reg FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_ROLE_SPEF_REG();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_ROLE_SPEF_REG() 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'),'rm_role_spef_reg', 'perm_role,perm_spefunc', concat(OLD.PERM_ROLE,',',OLD.PERM_SPEFUNC), '', getTableSubSystem('rm_role_spef_reg'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_ROLE_SPEF_REG ON rm_role_spef_reg;
CREATE TRIGGER TDA_RM_ROLE_SPEF_REG AFTER DELETE ON rm_role_spef_reg FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_ROLE_SPEF_REG();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_SECURITY_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'),'rm_security_def', 'perm_id', '', NEW.PERM_ID, getTableSubSystem('rm_security_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_SECURITY_DEF ON rm_security_def;
CREATE TRIGGER TIA_RM_SECURITY_DEF AFTER INSERT ON rm_security_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_SECURITY_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_SECURITY_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'),'rm_security_def', 'perm_id', OLD.PERM_ID, NEW.PERM_ID, getTableSubSystem('rm_security_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_SECURITY_DEF ON rm_security_def;
CREATE TRIGGER TUA_RM_SECURITY_DEF AFTER UPDATE ON rm_security_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_SECURITY_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_SECURITY_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'),'rm_security_def', 'perm_id', OLD.PERM_ID, '', getTableSubSystem('rm_security_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_SECURITY_DEF ON rm_security_def;
CREATE TRIGGER TDA_RM_SECURITY_DEF AFTER DELETE ON rm_security_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_SECURITY_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_SPCL_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'),'rm_spcl_func_def', 'perm_id', '', NEW.PERM_ID, getTableSubSystem('rm_spcl_func_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_SPCL_FUNC_DEF ON rm_spcl_func_def;
CREATE TRIGGER TIA_RM_SPCL_FUNC_DEF AFTER INSERT ON rm_spcl_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_SPCL_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_SPCL_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('update'),'rm_spcl_func_def', 'perm_id', OLD.PERM_ID, NEW.PERM_ID, getTableSubSystem('rm_spcl_func_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_SPCL_FUNC_DEF ON rm_spcl_func_def;
CREATE TRIGGER TUA_RM_SPCL_FUNC_DEF AFTER UPDATE ON rm_spcl_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_SPCL_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_SPCL_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('delete'),'rm_spcl_func_def', 'perm_id', OLD.PERM_ID, '', getTableSubSystem('rm_spcl_func_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_SPCL_FUNC_DEF ON rm_spcl_func_def;
CREATE TRIGGER TDA_RM_SPCL_FUNC_DEF AFTER DELETE ON rm_spcl_func_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_SPCL_FUNC_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_USER_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'),'rm_user_def', 'perm_id', '', NEW.PERM_ID, getTableSubSystem('rm_user_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_USER_DEF ON rm_user_def;
CREATE TRIGGER TIA_RM_USER_DEF AFTER INSERT ON rm_user_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_USER_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_USER_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'),'rm_user_def', 'perm_id', OLD.PERM_ID, NEW.PERM_ID, getTableSubSystem('rm_user_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_USER_DEF ON rm_user_def;
CREATE TRIGGER TUA_RM_USER_DEF AFTER UPDATE ON rm_user_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_USER_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_USER_DEF() RETURNS TRIGGER AS $$
BEGIN
delete from rm_user_finger_print_info where  USER_ID = old.PERM_ID;
delete from rm_user_face_info where USER_ID = old.PERM_ID;
delete from rm_user_signature_info where USER_ID = old.PERM_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'),'rm_user_def', 'perm_id', OLD.PERM_ID, '', getTableSubSystem('rm_user_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_USER_DEF ON rm_user_def;
CREATE TRIGGER TDA_RM_USER_DEF AFTER DELETE ON rm_user_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_USER_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_USERG_DB_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'),'rm_userg_db_info', 'perm_user_group,perm_table_name', '', concat(NEW.PERM_USER_GROUP,',',NEW.PERM_TABLE_NAME), getTableSubSystem('rm_userg_db_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_USERG_DB_INFO ON rm_userg_db_info;
CREATE TRIGGER TIA_RM_USERG_DB_INFO AFTER INSERT ON rm_userg_db_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_USERG_DB_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_USERG_DB_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'),'rm_userg_db_info', 'perm_user_group,perm_table_name', concat(OLD.PERM_USER_GROUP,',',OLD.PERM_TABLE_NAME), concat(NEW.PERM_USER_GROUP,',',NEW.PERM_TABLE_NAME), getTableSubSystem('rm_userg_db_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_USERG_DB_INFO ON rm_userg_db_info;
CREATE TRIGGER TUA_RM_USERG_DB_INFO AFTER UPDATE ON rm_userg_db_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_USERG_DB_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_USERG_DB_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'),'rm_userg_db_info', 'perm_user_group,perm_table_name', concat(OLD.PERM_USER_GROUP,',',OLD.PERM_TABLE_NAME), '', getTableSubSystem('rm_userg_db_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_USERG_DB_INFO ON rm_userg_db_info;
CREATE TRIGGER TDA_RM_USERG_DB_INFO AFTER DELETE ON rm_userg_db_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_USERG_DB_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_USERG_DEF() RETURNS TRIGGER AS $$
BEGIN
    --PERM_TYPE = 1是报表,0是HMI画面
    --先清除,后插入rm_userg_report_info
    --先删除关联了此用户组的在rm_userg_report_info中的报表浏览权限
    DELETE FROM rm_userg_report_info WHERE PERM_USER_GROUP = NEW.PERM_ID;
    
    
    --重新把对应的报表浏览权限写入
    INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
    SELECT  NEW.PERM_ID,report_type_info.REPORT_DEFINE 
    FROM rm_userg_hmi_info, report_type_info
    WHERE  rm_userg_hmi_info.PERM_USER_GROUP = NEW.PERM_ID AND rm_userg_hmi_info.PERM_TYPE = '1'
    AND 
    testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
    PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
    PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
    ,IF(REPORT_ID%32=0,32,REPORT_ID%32));
    
    INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
    SELECT NEW.PERM_ID,report_type_info.REPORT_DEFINE 
    FROM rm_role_hmi_info,report_type_info
    WHERE 
        testNumber(elt( (DIV((PERM_ROLE-1),32)) +1, NEW.PERM_ROLE1,NEW.PERM_ROLE2,NEW.PERM_ROLE3,NEW.PERM_ROLE4,NEW.PERM_ROLE5,NEW.PERM_ROLE6,NEW.PERM_ROLE7,NEW.PERM_ROLE8),
        IF(PERM_ROLE%32=0,32,PERM_ROLE%32))
    AND rm_role_hmi_info.PERM_TYPE='1' 
    AND
        testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
        PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32));
    
    INSERT INTO rm_userg_hmi_info(PERM_USER_GROUP,PERM_TYPE,PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16,PERM_ATTRIBUTE)
    VALUES(NEW.PERM_ID,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1)
    ;
   
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT 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'),'rm_userg_def', 'perm_id', '', NEW.PERM_ID, getTableSubSystem('rm_userg_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_USERG_DEF ON rm_userg_def;
CREATE TRIGGER TIA_RM_USERG_DEF AFTER INSERT ON rm_userg_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_USERG_DEF();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_USERG_DEF() RETURNS TRIGGER AS $$
BEGIN
    IF NEW.PERM_ID <> OLD.PERM_ID OR NEW.PERM_ROLE1 <> OLD.PERM_ROLE1 OR NEW.PERM_ROLE2 <> OLD.PERM_ROLE2 OR NEW.PERM_ROLE3 <> OLD.PERM_ROLE3 OR
    NEW.PERM_ROLE4 <> OLD.PERM_ROLE4 OR NEW.PERM_ROLE5 <> OLD.PERM_ROLE5 OR NEW.PERM_ROLE6 <> OLD.PERM_ROLE6 OR NEW.PERM_ROLE7 <> OLD.PERM_ROLE7 OR
    NEW.PERM_ROLE8 <> OLD.PERM_ROLE8 THEN
    BEGIN
        --PERM_TYPE = 1是报表,0是HMI画面
        --先清除,后插入rm_userg_report_info
        --先删除关联了此用户组的在rm_userg_report_info中的报表浏览权限
        DELETE FROM rm_userg_report_info WHERE PERM_USER_GROUP = OLD.PERM_ID;
        
        --重新把对应的报表浏览权限写入
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT  NEW.PERM_ID,report_type_info.REPORT_DEFINE 
        FROM rm_userg_hmi_info, report_type_info
        WHERE  rm_userg_hmi_info.PERM_USER_GROUP = NEW.PERM_ID AND rm_userg_hmi_info.PERM_TYPE = '1'
        AND 
        testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
        PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32));
        
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT NEW.PERM_ID,report_type_info.REPORT_DEFINE 
        FROM rm_role_hmi_info,report_type_info
        WHERE 
            testNumber(elt( (DIV((PERM_ROLE-1),32)) +1, NEW.PERM_ROLE1,NEW.PERM_ROLE2,NEW.PERM_ROLE3,NEW.PERM_ROLE4,NEW.PERM_ROLE5,NEW.PERM_ROLE6,NEW.PERM_ROLE7,NEW.PERM_ROLE8),
            IF(PERM_ROLE%32=0,32,PERM_ROLE%32))
        AND rm_role_hmi_info.PERM_TYPE='1' 
        AND
            testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
            PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
            PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
            ,IF(REPORT_ID%32=0,32,REPORT_ID%32))
        ;
    
    END;
    END IF;
	
	IF NEW.PERM_LOCATION1 <> OLD.PERM_LOCATION1 OR NEW.PERM_LOCATION2 <> OLD.PERM_LOCATION2 OR NEW.PERM_LOCATION3 <> OLD.PERM_LOCATION3 OR
    NEW.PERM_LOCATION4 <> OLD.PERM_LOCATION4 OR NEW.PERM_LOCATION5 <> OLD.PERM_LOCATION5 OR NEW.PERM_LOCATION6 <> OLD.PERM_LOCATION6 OR
    NEW.PERM_LOCATION7 <> OLD.PERM_LOCATION7 OR NEW.PERM_LOCATION8 <> OLD.PERM_LOCATION8 THEN
    BEGIN
    
    UPDATE rm_userg_rsp_info SET PERM_LOCATION1 = NEW.PERM_LOCATION1,PERM_LOCATION2 = NEW.PERM_LOCATION2,PERM_LOCATION3 = NEW.PERM_LOCATION3,PERM_LOCATION4 = NEW.PERM_LOCATION4,
    PERM_LOCATION5 = NEW.PERM_LOCATION5,PERM_LOCATION6 = NEW.PERM_LOCATION6,PERM_LOCATION7 = NEW.PERM_LOCATION7,PERM_LOCATION8 = NEW.PERM_LOCATION8 WHERE PERM_USER_GROUP = OLD.PERM_ID;
    
    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'),'rm_userg_def', 'perm_id', OLD.PERM_ID, NEW.PERM_ID, getTableSubSystem('rm_userg_def'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_USERG_DEF ON rm_userg_def;
CREATE TRIGGER TUA_RM_USERG_DEF AFTER UPDATE ON rm_userg_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_USERG_DEF();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_USERG_DEF() RETURNS TRIGGER AS $$
    DECLARE num int;
BEGIN
--删除用户中的用户组
    num =  DIV((OLD.PERM_ID-1),32);
   
    IF num = 0 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP1 = resetBit(PERM_USER_GROUP1,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP1,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 1 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP2 = resetBit(PERM_USER_GROUP2,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP2,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 2 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP3 = resetBit(PERM_USER_GROUP3,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP3,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 3 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP4 = resetBit(PERM_USER_GROUP4,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP4,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 4 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP5 = resetBit(PERM_USER_GROUP5,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP5,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 5 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP6 = resetBit(PERM_USER_GROUP6,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP6,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 6 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP7 = resetBit(PERM_USER_GROUP7,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP7,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 7 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP8 = resetBit(PERM_USER_GROUP8,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP8,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 8 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP9 = resetBit(PERM_USER_GROUP9,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP9,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 9 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP10 = resetBit(PERM_USER_GROUP10,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP10,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 10 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP11 = resetBit(PERM_USER_GROUP11,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP11,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 11 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP12 = resetBit(PERM_USER_GROUP12,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP12,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 12 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP13 = resetBit(PERM_USER_GROUP13,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP13,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 13 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP14 = resetBit(PERM_USER_GROUP14,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP14,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 14 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP15 = resetBit(PERM_USER_GROUP15,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP15,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    ELSEIF num = 15 THEN
        UPDATE rm_user_def SET PERM_USER_GROUP16 = resetBit(PERM_USER_GROUP16,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32)) WHERE testNumber(PERM_USER_GROUP16,IF(OLD.PERM_ID%32=0,32,OLD.PERM_ID%32));
    END IF;
    
--rm_userg_rsp_info
    DELETE FROM rm_userg_rsp_info WHERE PERM_USER_GROUP = OLD.PERM_ID;
--rm_userg_db_info
    DELETE FROM rm_userg_db_info WHERE PERM_USER_GROUP = OLD.PERM_ID;
--rm_userg_hmi_info
    DELETE FROM rm_userg_hmi_info WHERE PERM_USER_GROUP = OLD.PERM_ID;
--rm_userg_report_info
    DELETE FROM rm_userg_report_info WHERE PERM_USER_GROUP = OLD.PERM_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'),'rm_userg_def', 'perm_id', OLD.PERM_ID, '', getTableSubSystem('rm_userg_def'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_USERG_DEF ON rm_userg_def;
CREATE TRIGGER TDA_RM_USERG_DEF AFTER DELETE ON rm_userg_def FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_USERG_DEF();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_USERG_HMI_INFO() RETURNS TRIGGER AS $$
BEGIN
    --PERM_TYPE = 1是报表,0是HMI画面
    --先清除,后插入rm_userg_report_info
    
    IF NEW.PERM_TYPE = '1' THEN 
    BEGIN 
        --先删除关联了此用户组的在rm_userg_report_info中的报表浏览权限
        DELETE FROM rm_userg_report_info WHERE PERM_USER_GROUP = NEW.PERM_USER_GROUP;
        
        
        --重新把对应的报表浏览权限写入
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT  NEW.PERM_USER_GROUP,report_type_info.REPORT_DEFINE 
        FROM report_type_info
        WHERE testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        NEW.PERM_INFO1,NEW.PERM_INFO2,NEW.PERM_INFO3,NEW.PERM_INFO4,NEW.PERM_INFO5,NEW.PERM_INFO6,NEW.PERM_INFO7,NEW.PERM_INFO8,
        NEW.PERM_INFO9,NEW.PERM_INFO10,NEW.PERM_INFO11,NEW.PERM_INFO12,NEW.PERM_INFO13,NEW.PERM_INFO14,NEW.PERM_INFO15,NEW.PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32));
        
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT NEW.PERM_USER_GROUP,report_type_info.REPORT_DEFINE 
        FROM rm_userg_def,rm_role_hmi_info,report_type_info
        WHERE rm_userg_def.PERM_ID = NEW.PERM_USER_GROUP 
        AND 
            testNumber(elt( (DIV((PERM_ROLE-1),32)) +1, PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),
            IF(PERM_ROLE%32=0,32,PERM_ROLE%32))
        AND rm_role_hmi_info.PERM_TYPE='1' 
        AND
            testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
            PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
            PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
            ,IF(REPORT_ID%32=0,32,REPORT_ID%32))
        ;
        
    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'),'rm_userg_hmi_info', 'perm_user_group,perm_type,perm_attribute', '', concat(NEW.PERM_USER_GROUP,',',NEW.PERM_TYPE,',',NEW.PERM_ATTRIBUTE), getTableSubSystem('rm_userg_hmi_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_USERG_HMI_INFO ON rm_userg_hmi_info;
CREATE TRIGGER TIA_RM_USERG_HMI_INFO AFTER INSERT ON rm_userg_hmi_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_USERG_HMI_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_USERG_HMI_INFO() RETURNS TRIGGER AS $$
BEGIN
    --PERM_TYPE = 1是报表,0是HMI画面
    --先清除,后插入rm_userg_report_info
    
    IF NEW.PERM_TYPE = '1' AND OLD.PERM_TYPE = '1' THEN 
    BEGIN 
        --先删除关联了此用户组的在rm_userg_report_info中的报表浏览权限
        DELETE FROM rm_userg_report_info WHERE PERM_USER_GROUP = OLD.PERM_USER_GROUP;
        
        
        --重新把对应的报表浏览权限写入
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT  NEW.PERM_USER_GROUP,report_type_info.REPORT_DEFINE 
        FROM report_type_info
        WHERE testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
        NEW.PERM_INFO1,NEW.PERM_INFO2,NEW.PERM_INFO3,NEW.PERM_INFO4,NEW.PERM_INFO5,NEW.PERM_INFO6,NEW.PERM_INFO7,NEW.PERM_INFO8,
        NEW.PERM_INFO9,NEW.PERM_INFO10,NEW.PERM_INFO11,NEW.PERM_INFO12,NEW.PERM_INFO13,NEW.PERM_INFO14,NEW.PERM_INFO15,NEW.PERM_INFO16)
        ,IF(REPORT_ID%32=0,32,REPORT_ID%32));
        
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT NEW.PERM_USER_GROUP,report_type_info.REPORT_DEFINE 
        FROM rm_userg_def,rm_role_hmi_info,report_type_info
        WHERE rm_userg_def.PERM_ID = NEW.PERM_USER_GROUP 
        AND 
            testNumber(elt( (DIV((PERM_ROLE-1),32)) +1, PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),
            IF(PERM_ROLE%32=0,32,PERM_ROLE%32))
        AND rm_role_hmi_info.PERM_TYPE='1' 
        AND
			testNumber(elt((DIV((REPORT_ID-1),32)) +1,
            PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
            PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
            ,IF(REPORT_ID%32=0,32,REPORT_ID%32))
        ;
        
    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'),'rm_userg_hmi_info', 'perm_user_group,perm_type,perm_attribute', concat(OLD.PERM_USER_GROUP,',',OLD.PERM_TYPE,',',OLD.PERM_ATTRIBUTE), concat(NEW.PERM_USER_GROUP,',',NEW.PERM_TYPE,',',NEW.PERM_ATTRIBUTE), getTableSubSystem('rm_userg_hmi_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_USERG_HMI_INFO ON rm_userg_hmi_info;
CREATE TRIGGER TUA_RM_USERG_HMI_INFO AFTER UPDATE ON rm_userg_hmi_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_USERG_HMI_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_USERG_HMI_INFO() RETURNS TRIGGER AS $$
BEGIN
    --PERM_TYPE = 1是报表,0是HMI画面
    --先清除,后插入rm_userg_report_info
    
    IF  OLD.PERM_TYPE = '1' THEN 
    BEGIN 
        --先删除关联了此用户组的在rm_userg_report_info中的报表浏览权限
        DELETE FROM rm_userg_report_info WHERE PERM_USER_GROUP = OLD.PERM_USER_GROUP;
        
        
        --重新把对应的报表浏览权限写入
        INSERT INTO rm_userg_report_info(PERM_USER_GROUP,REPORT_CODE)
        SELECT OLD.PERM_USER_GROUP,report_type_info.REPORT_DEFINE 
        FROM rm_userg_def,rm_role_hmi_info,report_type_info
        WHERE rm_userg_def.PERM_ID = OLD.PERM_USER_GROUP 
        AND 
            testNumber(elt( (DIV((PERM_ROLE-1),32)) +1, PERM_ROLE1,PERM_ROLE2,PERM_ROLE3,PERM_ROLE4,PERM_ROLE5,PERM_ROLE6,PERM_ROLE7,PERM_ROLE8),
            IF(PERM_ROLE%32=0,32,PERM_ROLE%32))
        AND rm_role_hmi_info.PERM_TYPE='1' 
        AND
            testNumber(elt( (DIV((REPORT_ID-1),32)) +1,
            PERM_INFO1,PERM_INFO2,PERM_INFO3,PERM_INFO4,PERM_INFO5,PERM_INFO6,PERM_INFO7,PERM_INFO8,
            PERM_INFO9,PERM_INFO10,PERM_INFO11,PERM_INFO12,PERM_INFO13,PERM_INFO14,PERM_INFO15,PERM_INFO16)
            ,IF(REPORT_ID%32=0,32,REPORT_ID%32))
        ;
        
    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'),'rm_userg_hmi_info', 'perm_user_group,perm_type,perm_attribute', concat(OLD.PERM_USER_GROUP,',',OLD.PERM_TYPE,',',OLD.PERM_ATTRIBUTE), '', getTableSubSystem('rm_userg_hmi_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_USERG_HMI_INFO ON rm_userg_hmi_info;
CREATE TRIGGER TDA_RM_USERG_HMI_INFO AFTER DELETE ON rm_userg_hmi_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_USERG_HMI_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_RM_USERG_RSP_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'),'rm_userg_rsp_info', 'perm_user_group,perm_role,perm_spefunc', '', concat(NEW.PERM_USER_GROUP,',',NEW.PERM_ROLE,',',NEW.PERM_SPEFUNC), getTableSubSystem('rm_userg_rsp_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_RM_USERG_RSP_INFO ON rm_userg_rsp_info;
CREATE TRIGGER TIA_RM_USERG_RSP_INFO AFTER INSERT ON rm_userg_rsp_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_RM_USERG_RSP_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_RM_USERG_RSP_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'),'rm_userg_rsp_info', 'perm_user_group,perm_role,perm_spefunc', concat(OLD.PERM_USER_GROUP,',',OLD.PERM_ROLE,',',OLD.PERM_SPEFUNC), concat(NEW.PERM_USER_GROUP,',',NEW.PERM_ROLE,',',NEW.PERM_SPEFUNC), getTableSubSystem('rm_userg_rsp_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_RM_USERG_RSP_INFO ON rm_userg_rsp_info;
CREATE TRIGGER TUA_RM_USERG_RSP_INFO AFTER UPDATE ON rm_userg_rsp_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_RM_USERG_RSP_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_RM_USERG_RSP_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'),'rm_userg_rsp_info', 'perm_user_group,perm_role,perm_spefunc', concat(OLD.PERM_USER_GROUP,',',OLD.PERM_ROLE,',',OLD.PERM_SPEFUNC), '', getTableSubSystem('rm_userg_rsp_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_RM_USERG_RSP_INFO ON rm_userg_rsp_info;
CREATE TRIGGER TDA_RM_USERG_RSP_INFO AFTER DELETE ON rm_userg_rsp_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_RM_USERG_RSP_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_SAFETY_DAY() 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'),'safety_day', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('safety_day'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SAFETY_DAY ON safety_day;
CREATE TRIGGER TIA_SAFETY_DAY AFTER INSERT ON safety_day FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SAFETY_DAY();



CREATE OR REPLACE FUNCTION FUNC_TUA_SAFETY_DAY() 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'),'safety_day', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('safety_day'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SAFETY_DAY ON safety_day;
CREATE TRIGGER TUA_SAFETY_DAY AFTER UPDATE ON safety_day FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SAFETY_DAY();



CREATE OR REPLACE FUNCTION FUNC_TDA_SAFETY_DAY() 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'),'safety_day', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('safety_day'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SAFETY_DAY ON safety_day;
CREATE TRIGGER TDA_SAFETY_DAY AFTER DELETE ON safety_day FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SAFETY_DAY();



CREATE OR REPLACE FUNCTION FUNC_TIA_SAMPLE_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'),'sample_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('sample_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SAMPLE_DEFINE ON sample_define;
CREATE TRIGGER TIA_SAMPLE_DEFINE AFTER INSERT ON sample_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SAMPLE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TUA_SAMPLE_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'),'sample_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('sample_define'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SAMPLE_DEFINE ON sample_define;
CREATE TRIGGER TUA_SAMPLE_DEFINE AFTER UPDATE ON sample_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SAMPLE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TDA_SAMPLE_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'),'sample_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('sample_define'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SAMPLE_DEFINE ON sample_define;
CREATE TRIGGER TDA_SAMPLE_DEFINE AFTER DELETE ON sample_define FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SAMPLE_DEFINE();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_APP_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'),'sys_model_app_info', 'app_id', '', NEW.APP_ID, getTableSubSystem('sys_model_app_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_APP_INFO ON sys_model_app_info;
CREATE TRIGGER TIA_SYS_MODEL_APP_INFO AFTER INSERT ON sys_model_app_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_APP_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_APP_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'),'sys_model_app_info', 'app_id', OLD.APP_ID, NEW.APP_ID, getTableSubSystem('sys_model_app_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_APP_INFO ON sys_model_app_info;
CREATE TRIGGER TUA_SYS_MODEL_APP_INFO AFTER UPDATE ON sys_model_app_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_APP_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_APP_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'),'sys_model_app_info', 'app_id', OLD.APP_ID, '', getTableSubSystem('sys_model_app_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_APP_INFO ON sys_model_app_info;
CREATE TRIGGER TDA_SYS_MODEL_APP_INFO AFTER DELETE ON sys_model_app_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_APP_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_DBCONFIG_DOMAIN() 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'),'sys_model_dbconfig_domain', 'domain_id', '', NEW.DOMAIN_ID, getTableSubSystem('sys_model_dbconfig_domain'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_DBCONFIG_DOMAIN ON sys_model_dbconfig_domain;
CREATE TRIGGER TIA_SYS_MODEL_DBCONFIG_DOMAIN AFTER INSERT ON sys_model_dbconfig_domain FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_DBCONFIG_DOMAIN();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_DBCONFIG_DOMAIN() 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'),'sys_model_dbconfig_domain', 'domain_id', OLD.DOMAIN_ID, NEW.DOMAIN_ID, getTableSubSystem('sys_model_dbconfig_domain'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_DBCONFIG_DOMAIN ON sys_model_dbconfig_domain;
CREATE TRIGGER TUA_SYS_MODEL_DBCONFIG_DOMAIN AFTER UPDATE ON sys_model_dbconfig_domain FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_DBCONFIG_DOMAIN();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_DBCONFIG_DOMAIN() 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'),'sys_model_dbconfig_domain', 'domain_id', OLD.DOMAIN_ID, '', getTableSubSystem('sys_model_dbconfig_domain'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_DBCONFIG_DOMAIN ON sys_model_dbconfig_domain;
CREATE TRIGGER TDA_SYS_MODEL_DBCONFIG_DOMAIN AFTER DELETE ON sys_model_dbconfig_domain FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_DBCONFIG_DOMAIN();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_DBCONFIG_NODE() 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'),'sys_model_dbconfig_node', 'node_name', '', NEW.NODE_NAME, getTableSubSystem('sys_model_dbconfig_node'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_DBCONFIG_NODE ON sys_model_dbconfig_node;
CREATE TRIGGER TIA_SYS_MODEL_DBCONFIG_NODE AFTER INSERT ON sys_model_dbconfig_node FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_DBCONFIG_NODE();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_DBCONFIG_NODE() 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'),'sys_model_dbconfig_node', 'node_name', OLD.NODE_NAME, NEW.NODE_NAME, getTableSubSystem('sys_model_dbconfig_node'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_DBCONFIG_NODE ON sys_model_dbconfig_node;
CREATE TRIGGER TUA_SYS_MODEL_DBCONFIG_NODE AFTER UPDATE ON sys_model_dbconfig_node FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_DBCONFIG_NODE();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_DBCONFIG_NODE() 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'),'sys_model_dbconfig_node', 'node_name', OLD.NODE_NAME, '', getTableSubSystem('sys_model_dbconfig_node'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_DBCONFIG_NODE ON sys_model_dbconfig_node;
CREATE TRIGGER TDA_SYS_MODEL_DBCONFIG_NODE AFTER DELETE ON sys_model_dbconfig_node FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_DBCONFIG_NODE();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_DEPLOY_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'),'sys_model_deploy_info', 'domain_id,app_id', '', concat(NEW.DOMAIN_ID,',',NEW.APP_ID), getTableSubSystem('sys_model_deploy_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_DEPLOY_INFO ON sys_model_deploy_info;
CREATE TRIGGER TIA_SYS_MODEL_DEPLOY_INFO AFTER INSERT ON sys_model_deploy_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_DEPLOY_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_DEPLOY_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'),'sys_model_deploy_info', 'domain_id,app_id', concat(OLD.DOMAIN_ID,',',OLD.APP_ID), concat(NEW.DOMAIN_ID,',',NEW.APP_ID), getTableSubSystem('sys_model_deploy_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_DEPLOY_INFO ON sys_model_deploy_info;
CREATE TRIGGER TUA_SYS_MODEL_DEPLOY_INFO AFTER UPDATE ON sys_model_deploy_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_DEPLOY_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_DEPLOY_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'),'sys_model_deploy_info', 'domain_id,app_id', concat(OLD.DOMAIN_ID,',',OLD.APP_ID), '', getTableSubSystem('sys_model_deploy_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_DEPLOY_INFO ON sys_model_deploy_info;
CREATE TRIGGER TDA_SYS_MODEL_DEPLOY_INFO AFTER DELETE ON sys_model_deploy_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_DEPLOY_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_DOMAIN_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'),'sys_model_domain_info', 'domain_id', '', NEW.DOMAIN_ID, getTableSubSystem('sys_model_domain_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_DOMAIN_INFO ON sys_model_domain_info;
CREATE TRIGGER TIA_SYS_MODEL_DOMAIN_INFO AFTER INSERT ON sys_model_domain_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_DOMAIN_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_DOMAIN_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'),'sys_model_domain_info', 'domain_id', OLD.DOMAIN_ID, NEW.DOMAIN_ID, getTableSubSystem('sys_model_domain_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_DOMAIN_INFO ON sys_model_domain_info;
CREATE TRIGGER TUA_SYS_MODEL_DOMAIN_INFO AFTER UPDATE ON sys_model_domain_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_DOMAIN_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_DOMAIN_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'),'sys_model_domain_info', 'domain_id', OLD.DOMAIN_ID, '', getTableSubSystem('sys_model_domain_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_DOMAIN_INFO ON sys_model_domain_info;
CREATE TRIGGER TDA_SYS_MODEL_DOMAIN_INFO AFTER DELETE ON sys_model_domain_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_DOMAIN_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_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'),'sys_model_location_info', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('sys_model_location_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_LOCATION_INFO ON sys_model_location_info;
CREATE TRIGGER TIA_SYS_MODEL_LOCATION_INFO AFTER INSERT ON sys_model_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_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'),'sys_model_location_info', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('sys_model_location_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_LOCATION_INFO ON sys_model_location_info;
CREATE TRIGGER TUA_SYS_MODEL_LOCATION_INFO AFTER UPDATE ON sys_model_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_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'),'sys_model_location_info', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('sys_model_location_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_LOCATION_INFO ON sys_model_location_info;
CREATE TRIGGER TDA_SYS_MODEL_LOCATION_INFO AFTER DELETE ON sys_model_location_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_LOCATION_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_MSG_RELAY() 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'),'sys_model_msg_relay', 'src_domain_id,dst_domain_id,sub_app_id', '', concat(NEW.SRC_DOMAIN_ID,',',NEW.DST_DOMAIN_ID,',',NEW.SUB_APP_ID), getTableSubSystem('sys_model_msg_relay'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_MSG_RELAY ON sys_model_msg_relay;
CREATE TRIGGER TIA_SYS_MODEL_MSG_RELAY AFTER INSERT ON sys_model_msg_relay FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_MSG_RELAY();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_MSG_RELAY() 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'),'sys_model_msg_relay', 'src_domain_id,dst_domain_id,sub_app_id', concat(OLD.SRC_DOMAIN_ID,',',OLD.DST_DOMAIN_ID,',',OLD.SUB_APP_ID), concat(NEW.SRC_DOMAIN_ID,',',NEW.DST_DOMAIN_ID,',',NEW.SUB_APP_ID), getTableSubSystem('sys_model_msg_relay'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_MSG_RELAY ON sys_model_msg_relay;
CREATE TRIGGER TUA_SYS_MODEL_MSG_RELAY AFTER UPDATE ON sys_model_msg_relay FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_MSG_RELAY();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_MSG_RELAY() 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'),'sys_model_msg_relay', 'src_domain_id,dst_domain_id,sub_app_id', concat(OLD.SRC_DOMAIN_ID,',',OLD.DST_DOMAIN_ID,',',OLD.SUB_APP_ID), '', getTableSubSystem('sys_model_msg_relay'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_MSG_RELAY ON sys_model_msg_relay;
CREATE TRIGGER TDA_SYS_MODEL_MSG_RELAY AFTER DELETE ON sys_model_msg_relay FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_MSG_RELAY();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_NODE_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'),'sys_model_node_info', 'node_name', '', NEW.NODE_NAME, getTableSubSystem('sys_model_node_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_NODE_INFO ON sys_model_node_info;
CREATE TRIGGER TIA_SYS_MODEL_NODE_INFO AFTER INSERT ON sys_model_node_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_NODE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_NODE_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'),'sys_model_node_info', 'node_name', OLD.NODE_NAME, NEW.NODE_NAME, getTableSubSystem('sys_model_node_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_NODE_INFO ON sys_model_node_info;
CREATE TRIGGER TUA_SYS_MODEL_NODE_INFO AFTER UPDATE ON sys_model_node_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_NODE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_NODE_INFO() RETURNS TRIGGER AS $$
BEGIN
delete from acs_device_node_map where NODE_NAME = old.NODE_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'),'sys_model_node_info', 'node_name', OLD.NODE_NAME, '', getTableSubSystem('sys_model_node_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_NODE_INFO ON sys_model_node_info;
CREATE TRIGGER TDA_SYS_MODEL_NODE_INFO AFTER DELETE ON sys_model_node_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_NODE_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_PROCESS_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'),'sys_model_process_info', 'proc_alias', '', NEW.PROC_ALIAS, getTableSubSystem('sys_model_process_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_PROCESS_INFO ON sys_model_process_info;
CREATE TRIGGER TIA_SYS_MODEL_PROCESS_INFO AFTER INSERT ON sys_model_process_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_PROCESS_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_PROCESS_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'),'sys_model_process_info', 'proc_alias', OLD.PROC_ALIAS, NEW.PROC_ALIAS, getTableSubSystem('sys_model_process_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_PROCESS_INFO ON sys_model_process_info;
CREATE TRIGGER TUA_SYS_MODEL_PROCESS_INFO AFTER UPDATE ON sys_model_process_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_PROCESS_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_PROCESS_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'),'sys_model_process_info', 'proc_alias', OLD.PROC_ALIAS, '', getTableSubSystem('sys_model_process_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_PROCESS_INFO ON sys_model_process_info;
CREATE TRIGGER TDA_SYS_MODEL_PROCESS_INFO AFTER DELETE ON sys_model_process_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_PROCESS_INFO();



CREATE OR REPLACE FUNCTION FUNC_TIA_SYS_MODEL_SUB_SYSTEM_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'),'sys_model_sub_system_info', 'sub_system_id', '', NEW.SUB_SYSTEM_ID, getTableSubSystem('sys_model_sub_system_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TIA_SYS_MODEL_SUB_SYSTEM_INFO ON sys_model_sub_system_info;
CREATE TRIGGER TIA_SYS_MODEL_SUB_SYSTEM_INFO AFTER INSERT ON sys_model_sub_system_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TIA_SYS_MODEL_SUB_SYSTEM_INFO();



CREATE OR REPLACE FUNCTION FUNC_TUA_SYS_MODEL_SUB_SYSTEM_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'),'sys_model_sub_system_info', 'sub_system_id', OLD.SUB_SYSTEM_ID, NEW.SUB_SYSTEM_ID, getTableSubSystem('sys_model_sub_system_info'));
END IF;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TUA_SYS_MODEL_SUB_SYSTEM_INFO ON sys_model_sub_system_info;
CREATE TRIGGER TUA_SYS_MODEL_SUB_SYSTEM_INFO AFTER UPDATE ON sys_model_sub_system_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TUA_SYS_MODEL_SUB_SYSTEM_INFO();



CREATE OR REPLACE FUNCTION FUNC_TDA_SYS_MODEL_SUB_SYSTEM_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'),'sys_model_sub_system_info', 'sub_system_id', OLD.SUB_SYSTEM_ID, '', getTableSubSystem('sys_model_sub_system_info'));
END IF;
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TDA_SYS_MODEL_SUB_SYSTEM_INFO ON sys_model_sub_system_info;
CREATE TRIGGER TDA_SYS_MODEL_SUB_SYSTEM_INFO AFTER DELETE ON sys_model_sub_system_info FOR EACH ROW
EXECUTE PROCEDURE FUNC_TDA_SYS_MODEL_SUB_SYSTEM_INFO();



CREATE OR REPLACE FUNCTION FUNC_TU_TEMP_PLUGIN() RETURNS TRIGGER AS $$
BEGIN
    UPDATE temp_plugin_dev_map 
    SET PLUGIN_NAME  = NEW.TAG_NAME
    WHERE PLUGIN_NAME = OLD.TAG_NAME;
    
    UPDATE temp_plugin_fes_dev_map 
    SET PLUGIN_NAME  = NEW.TAG_NAME
    WHERE PLUGIN_NAME = OLD.TAG_NAME;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_TEMP_PLUGIN ON TEMP_PLUGIN;
CREATE TRIGGER TU_TEMP_PLUGIN AFTER UPDATE ON TEMP_PLUGIN FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_TEMP_PLUGIN();



CREATE OR REPLACE FUNCTION FUNC_TD_TEMP_PLUGIN() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM temp_plugin_dev_map WHERE PLUGIN_NAME = OLD.TAG_NAME;
    
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_TEMP_PLUGIN ON TEMP_PLUGIN;
CREATE TRIGGER TD_TEMP_PLUGIN AFTER DELETE ON TEMP_PLUGIN FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_TEMP_PLUGIN();



CREATE OR REPLACE FUNCTION FUNC_TI_TEMP_PLUGIN_DEV_MAP() RETURNS TRIGGER AS $$
BEGIN
     IF myTriggerIsDisable() = 1 THEN RETURN NEW;
     END IF;
    
    --生成对应的测点
      INSERT INTO temp_plugin_point_map_acc
      (PLUGIN_NAME,   TEMP_NAME,    TEMP_POINT_NAME,          FES_TEMP_NAME, FES_TEMP_POINT_NAME)
      SELECT
      NEW.PLUGIN_NAME,NEW.TEMP_NAME,getAfterLastDot(TAG_NAME),'',            ''
      FROM accuml_temp_define WHERE DEV_TP_NAME = NEW.TEMP_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
      NEW.PLUGIN_NAME,NEW.TEMP_NAME,getAfterLastDot(TAG_NAME),'',            '',                  ''
      FROM analog_temp_define WHERE DEV_TP_NAME = NEW.TEMP_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
      NEW.PLUGIN_NAME,NEW.TEMP_NAME,getAfterLastDot(TAG_NAME),'',            '',                  '',                   '',                   '',                   '',                   '',                 '',                 '',                  '',                ''
      FROM digital_temp_define WHERE DEV_TP_NAME = NEW.TEMP_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
      NEW.PLUGIN_NAME,NEW.TEMP_NAME,getAfterLastDot(TAG_NAME),'',            '',                  ''
      FROM mix_temp_define WHERE DEV_TP_NAME = NEW.TEMP_NAME;
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TI_TEMP_PLUGIN_DEV_MAP ON temp_plugin_dev_map;
CREATE TRIGGER TI_TEMP_PLUGIN_DEV_MAP AFTER INSERT ON temp_plugin_dev_map FOR EACH ROW
EXECUTE PROCEDURE FUNC_TI_TEMP_PLUGIN_DEV_MAP();



CREATE OR REPLACE FUNCTION FUNC_TD_TEMP_PLUGIN_DEV_MAP() RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM temp_plugin_fes_dev_map WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
    DELETE FROM temp_plugin_point_map_acc WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
    DELETE FROM temp_plugin_point_map_ana WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
    DELETE FROM temp_plugin_point_map_dig WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
    DELETE FROM temp_plugin_point_map_mix WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
    
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_TEMP_PLUGIN_DEV_MAP ON temp_plugin_dev_map;
CREATE TRIGGER TD_TEMP_PLUGIN_DEV_MAP AFTER DELETE ON temp_plugin_dev_map FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_TEMP_PLUGIN_DEV_MAP();



CREATE OR REPLACE FUNCTION FUNC_TU_TEMP_PLUGIN_FES_DEV_MAP() RETURNS TRIGGER AS $$
BEGIN
    --分两种一种是更新plugin_name和temp_name,另外的是更新fes_temp_name
    --已temp_plugin_point_map_acc为例,在此表中存在已关联的和未关联的两种情况,即FES_TEMP_NAME可能为空
    IF OLD.PLUGIN_NAME != NEW.PLUGIN_NAME OR OLD.TEMP_NAME != NEW.TEMP_NAME THEN 
    BEGIN
        UPDATE temp_plugin_point_map_acc 
        SET 
        PLUGIN_NAME = NEW.PLUGIN_NAME,
        TEMP_NAME = NEW.TEMP_NAME
        WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
        
        UPDATE temp_plugin_point_map_ana
        SET
        PLUGIN_NAME = NEW.PLUGIN_NAME,
        TEMP_NAME = NEW.TEMP_NAME
        WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
        
        UPDATE temp_plugin_point_map_dig
        SET
        PLUGIN_NAME = NEW.PLUGIN_NAME,
        TEMP_NAME = NEW.TEMP_NAME
        WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
        
        UPDATE temp_plugin_point_map_mix
        SET
        PLUGIN_NAME = NEW.PLUGIN_NAME,
        TEMP_NAME = NEW.TEMP_NAME
        WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME;
    END;
    END IF;
    
    --上面的更新完成后,就可以统一使用新的plugin_name和temp_name
    IF OLD.FES_TEMP_NAME != NEW.FES_TEMP_NAME THEN 
    BEGIN
        UPDATE temp_plugin_point_map_acc 
        SET 
        FES_TEMP_NAME = NEW.FES_TEMP_NAME
        WHERE PLUGIN_NAME = NEW.PLUGIN_NAME AND TEMP_NAME = NEW.TEMP_NAME AND FES_TEMP_NAME = OLD.FES_TEMP_NAME;
        
        UPDATE temp_plugin_point_map_ana 
        SET 
        FES_TEMP_NAME = NEW.FES_TEMP_NAME
        WHERE PLUGIN_NAME = NEW.PLUGIN_NAME AND TEMP_NAME = NEW.TEMP_NAME AND FES_TEMP_NAME = OLD.FES_TEMP_NAME;
        
        UPDATE temp_plugin_point_map_dig 
        SET 
        FES_TEMP_NAME = NEW.FES_TEMP_NAME
        WHERE PLUGIN_NAME = NEW.PLUGIN_NAME AND TEMP_NAME = NEW.TEMP_NAME AND FES_TEMP_NAME = OLD.FES_TEMP_NAME;
        
        UPDATE temp_plugin_point_map_mix 
        SET 
        FES_TEMP_NAME = NEW.FES_TEMP_NAME
        WHERE PLUGIN_NAME = NEW.PLUGIN_NAME AND TEMP_NAME = NEW.TEMP_NAME AND FES_TEMP_NAME = OLD.FES_TEMP_NAME;
    END;
    END IF;
    
RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TU_TEMP_PLUGIN_FES_DEV_MAP ON TEMP_PLUGIN_FES_DEV_MAP;
CREATE TRIGGER TU_TEMP_PLUGIN_FES_DEV_MAP AFTER UPDATE ON TEMP_PLUGIN_FES_DEV_MAP FOR EACH ROW
EXECUTE PROCEDURE FUNC_TU_TEMP_PLUGIN_FES_DEV_MAP();



CREATE OR REPLACE FUNCTION FUNC_TD_TEMP_PLUGIN_FES_DEV_MAP() RETURNS TRIGGER AS $$
BEGIN
    --直接把相关关联去除
    UPDATE temp_plugin_point_map_acc 
    SET 
    FES_TEMP_NAME = '',
    FES_TEMP_POINT_NAME = '' 
    WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME AND FES_TEMP_NAME = OLD.FES_TEMP_NAME;
    
    UPDATE temp_plugin_point_map_ana 
    SET 
    FES_TEMP_NAME = '',
    FES_TEMP_POINT_NAME = '',
    FES_TEMP_OUT_NAME = ''
    WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME AND FES_TEMP_NAME = OLD.FES_TEMP_NAME;
    
    UPDATE temp_plugin_point_map_dig 
    SET 
    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 = ''
    WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME AND FES_TEMP_NAME = OLD.FES_TEMP_NAME;
    
    UPDATE temp_plugin_point_map_mix 
    SET 
    FES_TEMP_NAME = '',
    FES_TEMP_POINT_NAME = '',
    FES_TEMP_OUT_NAME = ''
    WHERE PLUGIN_NAME = OLD.PLUGIN_NAME AND TEMP_NAME = OLD.TEMP_NAME AND FES_TEMP_NAME = OLD.FES_TEMP_NAME;
    
RETURN OLD;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TD_TEMP_PLUGIN_FES_DEV_MAP ON TEMP_PLUGIN_FES_DEV_MAP;
CREATE TRIGGER TD_TEMP_PLUGIN_FES_DEV_MAP AFTER DELETE ON TEMP_PLUGIN_FES_DEV_MAP FOR EACH ROW
EXECUTE PROCEDURE FUNC_TD_TEMP_PLUGIN_FES_DEV_MAP();



--dbop_list表实现序列的触发器
CREATE OR REPLACE FUNCTION FUNC_DBOP_LIST_SEQUENCE() RETURNS TRIGGER AS $$
	DECLARE max_id DECIMAL(10,0) DEFAULT 0;
BEGIN
	IF NEW.id IS NULL THEN 
		SELECT MAX(id) INTO max_id FROM dbop_list;
		max_id = COALESCE(max_id,0);
		NEW.id = max_id + 1;
	END IF;
	RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS DBOP_LIST_SEQUENCE ON dbop_list;
CREATE TRIGGER DBOP_LIST_SEQUENCE BEFORE INSERT ON dbop_list FOR EACH ROW
EXECUTE PROCEDURE FUNC_DBOP_LIST_SEQUENCE();



--push_his表实现序列的触发器
CREATE OR REPLACE FUNCTION FUNC_PUSH_HIS_SEQUENCE() RETURNS TRIGGER AS $$
	DECLARE max_id DECIMAL(10,0) DEFAULT 0;
BEGIN
	IF NEW.id IS NULL THEN 
		SELECT MAX(id) INTO max_id FROM push_his;
		max_id = COALESCE(max_id,0);
		NEW.id = max_id + 1;
	END IF;
	RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS PUSH_HIS_SEQUENCE ON push_his;
CREATE TRIGGER PUSH_HIS_SEQUENCE BEFORE INSERT ON push_his FOR EACH ROW
EXECUTE PROCEDURE FUNC_PUSH_HIS_SEQUENCE();


--trend_collection表实现序列的触发器
CREATE OR REPLACE FUNCTION FUNC_TREND_COLLECTION_SEQUENCE() RETURNS TRIGGER AS $$
	DECLARE max_id DECIMAL(10,0) DEFAULT 0;
BEGIN
	IF NEW.id IS NULL THEN 
		SELECT MAX(id) INTO max_id FROM trend_collection;
		max_id = COALESCE(max_id,0);
		NEW.id = max_id + 1;
	END IF;
	RETURN NEW;
END;
$$LANGUAGE PLPGSQL;
DROP TRIGGER IF EXISTS TREND_COLLECTION_SEQUENCE ON trend_collection;
CREATE TRIGGER TREND_COLLECTION_SEQUENCE BEFORE INSERT ON trend_collection FOR EACH ROW
EXECUTE PROCEDURE FUNC_TREND_COLLECTION_SEQUENCE();



--------------------------------------------------------------------------------------------------------------------
--以下是函数--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION findPsc3000Tag(pointDesc varchar(128),pointRemoteNo decimal(10,0), pointDevId decimal(10,0),tableName varchar(64)) RETURNS varchar(64) 
AS $$
    DECLARE tempVar varchar(64) DEFAULT '';
BEGIN 
    --在对应的PSC3000的测点表中查找对应测点的TAG
    
    
    IF tableName='psc_acc' THEN  
     tempVar =(SELECT TAG_NAME FROM psc3000_acc WHERE DESCRIPTION = pointDesc AND pointRemoteNo = REMOTE_NO AND pointDevId = DEV_ID);
    ELSEIF tableName='psc_ana' THEN
     tempVar =(SELECT TAG_NAME FROM psc3000_ana WHERE DESCRIPTION = pointDesc AND pointRemoteNo = REMOTE_NO AND pointDevId = DEV_ID);
    ELSEIF tableName='psc_dig' THEN
     tempVar =(SELECT TAG_NAME FROM psc3000_dig WHERE DESCRIPTION = pointDesc AND pointRemoteNo = REMOTE_NO AND pointDevId = DEV_ID);
    END IF;
    
    IF tempVar IS NULL THEN tempVar = '';
    END IF;
    --自增
    RETURN tempVar;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getAfterFirstDot( ds varchar(128)) RETURNS varchar(128) 
AS $$
    DECLARE tempVar varchar(128);
    DECLARE countStr int;
    DECLARE countFirstStr int;
BEGIN
    /*
     * 返回第一个.号之后的字符串
     */
    tempVar = ds;
    
    IF strpos(ds,'.') = 0 THEN RETURN tempVar;
    ELSE
    BEGIN
        tempVar = substring_index( ds, '.' , 1);
        countStr = char_length(ds);
        countFirstStr = char_length(tempVar);
        countFirstStr = countFirstStr + 1;
        tempVar = right(ds,countStr - countFirstStr);
        RETURN tempVar;
    END;   
    END IF;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getAfterLastDot( ds varchar(128)) RETURNS varchar(128) 
AS $$
    DECLARE tempVar varchar(128);
BEGIN
    
 /*
 * 返回最后一个.号之后的数据
 */
    tempVar = '';
    
    IF strpos(ds,'.') = 0 THEN RETURN tempVar;
    ELSE RETURN substring_index( ds, '.' , -1);
    END IF;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getBeforeFirstDot( ds varchar(128)) RETURNS varchar(128) 
AS $$
BEGIN
    /*
     * 返回第一个.号之前的字符串
     */
    
    IF strpos(ds,'.') = 0 THEN RETURN '';
    ELSE
    BEGIN
        RETURN substring_index( ds, '.' , 1);
    END;   
    END IF;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getBeforeLastDot( ds varchar(128)) RETURNS varchar(128) 
AS $$
    DECLARE tempVar varchar(128);
    DECLARE countStr int;
    DECLARE countLastStr int;
BEGIN
    /*
     * 返回最后一个.之前的字符串
     */
    tempVar = ds;
    
    IF strpos(ds,'.') = 0 THEN RETURN tempVar;
    ELSE
    BEGIN
        tempVar = substring_index( ds, '.' , -1);
        countStr = char_length(ds);
        countLastStr = char_length(tempVar);
        countLastStr = countLastStr + 1;
        tempVar = left(ds,countStr - countLastStr);
        RETURN tempVar;
    END;   
    END IF;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getFesPointType( flag varchar(64)) RETURNS decimal(10,0)
AS $$
    DECLARE tempVar decimal(10,0) DEFAULT 0;
BEGIN
    /*
     * 返回flag指定的点类型
     * 
     * FES_ACC:0
     * FES_ANA:1
     * FES_DIG:2
     * FES_MIX:3
     * FES_ANA_CTRL:4
     * FES_DIG_CTRL:5
     * FES_MIX_CTRL:6
     */
    
    IF flag='FES_ACC' THEN  tempVar =0;
    END IF;
    
    IF flag='FES_ANA' THEN  tempVar =1;
    END IF;
    
    IF flag='FES_DIG' THEN  tempVar =2;
    END IF;
    
    IF flag='FES_MIX' THEN  tempVar =3;
    END IF;
    
    IF flag='FES_ANA_CTRL' THEN  tempVar =4;
    END IF;
    
    IF flag='FES_DIG_CTRL' THEN  tempVar =5;
    END IF;
    
    IF flag='FES_MIX_CTRL' THEN  tempVar =6;
    END IF;
    
    IF flag='FES_CONST' THEN  tempVar =7;
    END IF;
   
    
    RETURN tempVar;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getMaxDefaultDevSeqNo( defaultDevTagName varchar(64), dotType decimal(10,0)) RETURNS decimal(10,0)
AS $$
    DECLARE tempVar decimal(10,0) DEFAULT 0;
BEGIN
    /*
     * 返回指定defaultDevTagName的最大SeqNo
     */
    
    IF dotType=getFesPointType('FES_ACC') THEN  tempVar =(SELECT max(SEQ_NO) FROM fes_accuml WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_ANA') THEN  tempVar =(SELECT max(SEQ_NO) FROM fes_analog WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_DIG') THEN  tempVar =(SELECT max(SEQ_NO) FROM fes_digital WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_MIX') THEN  tempVar =(SELECT max(SEQ_NO) FROM fes_mix WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_ANA_CTRL') THEN  tempVar =(SELECT max(SEQ_NO) FROM fes_analog_ctrl WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_DIG_CTRL') THEN  tempVar =(SELECT max(SEQ_NO) FROM fes_digital_ctrl WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_MIX_CTRL') THEN  tempVar =(SELECT max(SEQ_NO) FROM fes_mix_ctrl WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    
    IF tempVar IS NULL THEN tempVar = 0;
    END IF;
    
    RETURN tempVar + 1;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getMaxRtuDotNo( rtuTagName varchar(64), dotType decimal(10,0)) RETURNS decimal(10,0)
AS $$
    DECLARE tempVar decimal(10,0) DEFAULT 0;
BEGIN
    /*
     * 返回指定rtuTagName的最大点号
     * dotType可取三个值,0,1,2
     * 0表示FES_ACC
     * 1表示FES_ANA
     * 2表示FES_DIG
     * 3表示FES_MIX
     */
    
    IF dotType=getFesPointType('FES_ACC') THEN  tempVar =(SELECT max(DOT_NO) FROM fes_accuml WHERE RTU_TAG=rtuTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_ANA') THEN  tempVar =(SELECT max(DOT_NO) FROM fes_analog WHERE RTU_TAG=rtuTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_DIG') THEN  tempVar =(SELECT max(DOT_NO) FROM fes_digital WHERE RTU_TAG=rtuTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_MIX') THEN  tempVar =(SELECT max(DOT_NO) FROM fes_mix WHERE RTU_TAG=rtuTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_ANA_CTRL') THEN  tempVar =(SELECT max(DOT_NO) FROM fes_analog_ctrl WHERE RTU_TAG=rtuTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_DIG_CTRL') THEN  tempVar =(SELECT max(DOT_NO) FROM fes_digital_ctrl WHERE RTU_TAG=rtuTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_MIX_CTRL') THEN  tempVar =(SELECT max(DOT_NO) FROM fes_mix_ctrl WHERE RTU_TAG=rtuTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_CONST') THEN  tempVar =(SELECT max(DOT_NO) FROM fes_const WHERE RTU_TAG=rtuTagName);
    END IF;
    
    
    IF tempVar IS NULL THEN tempVar = -1;
    END IF;
    --自增
    RETURN tempVar + 1;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getMaxSeq(devTag varchar(64), tableName varchar(64)) RETURNS decimal(10,0)
AS $$
    DECLARE temp decimal(10) DEFAULT 0;
BEGIN 
    --获取指定设备的最大索引号
    
    IF tableName = 'accuml' THEN
        SELECT max(SEQ_NO) INTO temp FROM accuml WHERE DEVICE = devTag;
    ELSEIF tableName = 'analog' THEN
        SELECT max(SEQ_NO) INTO temp FROM analog WHERE DEVICE = devTag;
    ELSEIF tableName = 'digital' THEN
        SELECT max(SEQ_NO) INTO temp FROM digital WHERE DEVICE = devTag;
    ELSEIF tableName = 'mix' THEN
        SELECT max(SEQ_NO) INTO temp FROM mix WHERE DEVICE = devTag;
    END IF;
    
    RETURN  COALESCE(temp,0);
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getMaxTriggerId() RETURNS decimal(10,0)
AS $$
    DECLARE maxId decimal(10,0) DEFAULT 0;
BEGIN 
    --分割字符串,返回第strCount个子串
    
    SELECT (max(ACT_ID)) INTO maxId FROM sys_trigger_info;
    
    IF COALESCE(maxId,0) = 0 THEN
     maxId = 0;
    END IF;
    
     maxId = maxId + 1;
    RETURN maxId;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getOpType(opName varchar(32)) RETURNS decimal(10,0)
AS $$
    DECLARE flag decimal(10,0) DEFAULT -1;
BEGIN 
    --获取操作类型
    
    IF opName = 'insert' THEN
     flag = 0;
    ELSEIF opName = 'delete' THEN
     flag = 1;
    ELSEIF opName = 'update' THEN
     flag = 2;
    ELSE
     flag = -1;
    END IF;
    
    RETURN flag;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION getTableSubSystem( tableName varchar(100)) RETURNS decimal(10,0)
AS $$
    DECLARE subSystemFlag decimal(10,0) DEFAULT 0; 
BEGIN 
    
    
    RETURN subSystemFlag;
END;
$$LANGUAGE PLPGSQL;


CREATE OR REPLACE FUNCTION getTriggerFlag() RETURNS boolean
AS $$
    DECLARE flag boolean DEFAULT FALSE;
BEGIN 
    --分割字符串,返回第strCount个子串
    
    SELECT FLAG_ID INTO flag FROM sys_trigger_flag WHERE FLAG_ID = 1;
    
    IF COALESCE(flag,FALSE) = FALSE THEN
     flag = FALSE;
    END IF;
    
    RETURN flag;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION myTriggerIsDisable() RETURNS int
AS $$
	DECLARE flag INT;
BEGIN 
    --触发器开关状态,返回1时
	SELECT cast(coalesce(CURRENT_SETTING('disable.MyTrrigger','true'),'0')as int) INTO flag;
     IF flag = 0 THEN
        RETURN 0;
     ELSE
        RETURN 1;
     END IF;
END;
$$LANGUAGE PLPGSQL;




DROP FUNCTION IF EXISTS resetBit;
CREATE OR REPLACE FUNCTION resetBit(num decimal(10,0), pos decimal(10,0)) RETURNS int
AS $$
   	DECLARE tmpNum int;
   	DECLARE tmpPos int;
    DECLARE tmpVal int default 1;
BEGIN 
    --设置num的pos位为0,pos从1开始数
    --要求输入和输出都是Int范围的数
	tmpNum = num;
	tmpPos = pos;
    tmpVal = tmpVal << (tmpPos-1);
   	tmpVal = ~ tmpVal;
   	tmpNum = tmpNum & tmpVal;
    RETURN tmpNum;
END;
$$LANGUAGE PLPGSQL;


DROP FUNCTION IF EXISTS setBit;
CREATE OR REPLACE FUNCTION setBit( num decimal(10,0) ,pos decimal(10,0)) RETURNS int
AS $$
	DECLARE tmpNum int;
   	DECLARE tmpPos int;
    DECLARE tmpVal int default 1;
BEGIN 
    --设置num的pos位为1,pos从1开始数
    --要求输入和输出都是Int范围的数
	tmpNum = num;
	tmpPos = pos;
    tmpVal = tmpVal << (tmpPos-1);
   	tmpNum = tmpNum | tmpVal;
    RETURN tmpNum;
END;
$$LANGUAGE PLPGSQL;


DROP FUNCTION IF EXISTS testNumber;
CREATE OR REPLACE FUNCTION testNumber(num decimal(10,0), pos decimal(10,0)) RETURNS boolean
AS $$
	DECLARE tmpNum int;
   	DECLARE tmpPos int;
    DECLARE tmpVal int default 1;
BEGIN 
    --返回num的第pos位是否为1,pos从1开始数
	tmpNum = num;
	tmpPos = pos;
    tmpVal = tmpVal << (tmpPos-1);
   	IF (tmpNum & tmpVal) != 0 
   	THEN
   		RETURN TRUE;
   	ELSE 
   		RETURN FALSE;
    END IF;
END;
$$LANGUAGE PLPGSQL;


CREATE OR REPLACE FUNCTION autoRelationByPlugin(pluginName varchar(64))
RETURNS VOID
AS $$
BEGIN
    --自动形成套件中后台对前置的关联
    
    --清除本套件中的关联关系
    UPDATE temp_plugin_point_map_acc SET FES_TEMP_NAME = '',FES_TEMP_POINT_NAME = ''
    WHERE PLUGIN_NAME = pluginName;
    
    UPDATE temp_plugin_point_map_ana SET FES_TEMP_NAME = '',FES_TEMP_POINT_NAME = '',FES_TEMP_OUT_NAME = ''
    WHERE PLUGIN_NAME = pluginName;
    
    UPDATE temp_plugin_point_map_dig SET 
    FES_TEMP_NAME = '',
    FES_TEMP_OUT_NAME1 = '',
    FES_TEMP_OUT_NAME2 = '',
    FES_TEMP_OUT_NAME3 = '',
    FES_TEMP_OUT_NAME4 = '',
    FES_TEMP_OUT_NAME5 = '',
    FES_TEMP_POINT_NAME1 = '',
    FES_TEMP_POINT_NAME2 = '',
    FES_TEMP_POINT_NAME3 = '',
    FES_TEMP_POINT_NAME4 = '',
    FES_TEMP_POINT_NAME5 = ''
    WHERE PLUGIN_NAME = pluginName;
    
    UPDATE temp_plugin_point_map_mix SET FES_TEMP_NAME = '',FES_TEMP_POINT_NAME = '',FES_TEMP_OUT_NAME = ''
    WHERE PLUGIN_NAME = pluginName;
    
    --关联
    --accuml
    UPDATE temp_plugin_point_map_acc
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_POINT_NAME = temp_plugin_point_map_acc.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_acc.PLUGIN_NAME = pluginName 
	AND temp_plugin_point_map_acc.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_acc.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_ACC')
    AND temp_plugin_point_map_acc.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
    
    --analog
    UPDATE temp_plugin_point_map_ana
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_POINT_NAME = temp_plugin_point_map_ana.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_ana.PLUGIN_NAME = pluginName 
	AND temp_plugin_point_map_ana.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_ana.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_ANA')
    AND temp_plugin_point_map_ana.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
    
    UPDATE temp_plugin_point_map_ana
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_OUT_NAME = temp_plugin_point_map_ana.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_ana.PLUGIN_NAME = pluginName 
	AND temp_plugin_point_map_ana.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_ana.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_ANA_CTRL')
    AND temp_plugin_point_map_ana.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);

    --digital,只关联第一个分量
    UPDATE temp_plugin_point_map_dig
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_POINT_NAME1 = temp_plugin_point_map_dig.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName 
	AND temp_plugin_point_map_dig.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_dig.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_DIG')
    AND temp_plugin_point_map_dig.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
    
    UPDATE temp_plugin_point_map_dig
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_OUT_NAME1 = temp_plugin_point_map_dig.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName 
	AND temp_plugin_point_map_dig.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_dig.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_DIG_CTRL')
    AND temp_plugin_point_map_dig.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);

    UPDATE temp_plugin_point_map_mix
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_POINT_NAME = temp_plugin_point_map_mix.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_mix.PLUGIN_NAME = pluginName 
	AND temp_plugin_point_map_mix.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_mix.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_MIX')
    AND temp_plugin_point_map_mix.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
    
    UPDATE temp_plugin_point_map_mix
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_OUT_NAME = temp_plugin_point_map_mix.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_mix.PLUGIN_NAME = pluginName 
	AND temp_plugin_point_map_mix.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_mix.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_MIX_CTRL')
    AND temp_plugin_point_map_mix.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION autoRelationByPluginDev(pluginName varchar(64),devTag varchar(64))
RETURNS VOID
AS $$
BEGIN
    /*
     * 仅自动关联套件中的devTag指定的设备
     */
    
    
    --清除本套件中的关联关系
    UPDATE temp_plugin_point_map_acc SET FES_TEMP_NAME = '',FES_TEMP_POINT_NAME = ''
    WHERE PLUGIN_NAME = pluginName AND TEMP_NAME = devTag;
    
    UPDATE temp_plugin_point_map_ana SET FES_TEMP_NAME = '',FES_TEMP_POINT_NAME = '',FES_TEMP_OUT_NAME = ''
    WHERE PLUGIN_NAME = pluginName AND TEMP_NAME = devTag;
    
    UPDATE temp_plugin_point_map_dig SET 
    FES_TEMP_NAME = '',
    FES_TEMP_OUT_NAME1 = '',
    FES_TEMP_OUT_NAME2 = '',
    FES_TEMP_OUT_NAME3 = '',
    FES_TEMP_OUT_NAME4 = '',
    FES_TEMP_OUT_NAME5 = '',
    FES_TEMP_POINT_NAME1 = '',
    FES_TEMP_POINT_NAME2 = '',
    FES_TEMP_POINT_NAME3 = '',
    FES_TEMP_POINT_NAME4 = '',
    FES_TEMP_POINT_NAME5 = ''
    WHERE PLUGIN_NAME = pluginName AND TEMP_NAME = devTag;
    
    UPDATE temp_plugin_point_map_mix SET FES_TEMP_NAME = '',FES_TEMP_POINT_NAME = '',FES_TEMP_OUT_NAME = ''
    WHERE PLUGIN_NAME = pluginName AND TEMP_NAME = devTag;
    
    --关联
    --accuml
    UPDATE temp_plugin_point_map_acc
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_POINT_NAME = temp_plugin_point_map_acc.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_acc.PLUGIN_NAME = pluginName 
    AND temp_plugin_point_map_acc.TEMP_NAME = devTag 
    AND temp_plugin_point_map_acc.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME 
    AND temp_plugin_point_map_acc.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_ACC')
    AND temp_plugin_point_map_acc.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
    
    --analog
    UPDATE temp_plugin_point_map_ana
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_POINT_NAME = temp_plugin_point_map_ana.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_ana.PLUGIN_NAME = pluginName 
    AND temp_plugin_point_map_ana.TEMP_NAME = devTag 
    AND temp_plugin_point_map_ana.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_ana.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_ANA')
    AND temp_plugin_point_map_ana.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
    
    UPDATE temp_plugin_point_map_ana
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_OUT_NAME = temp_plugin_point_map_ana.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_ana.PLUGIN_NAME = pluginName 
    AND temp_plugin_point_map_ana.TEMP_NAME = devTag 
    AND temp_plugin_point_map_ana.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_ana.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_ANA_CTRL')
    AND temp_plugin_point_map_ana.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);

    --digital,只关联第一个分量
    UPDATE temp_plugin_point_map_dig
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_POINT_NAME1 = temp_plugin_point_map_dig.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName 
    AND temp_plugin_point_map_dig.TEMP_NAME = devTag
    AND temp_plugin_point_map_dig.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_dig.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_DIG')
    AND temp_plugin_point_map_dig.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);

    UPDATE temp_plugin_point_map_dig
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_OUT_NAME1 = temp_plugin_point_map_dig.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName 
    AND temp_plugin_point_map_dig.TEMP_NAME = devTag
    AND temp_plugin_point_map_dig.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_dig.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_DIG_CTRL')
    AND temp_plugin_point_map_dig.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
        
    UPDATE temp_plugin_point_map_mix
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_POINT_NAME = temp_plugin_point_map_mix.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_mix.PLUGIN_NAME = pluginName 
    AND temp_plugin_point_map_mix.TEMP_NAME = devTag
    AND temp_plugin_point_map_mix.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_mix.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_MIX')
    AND temp_plugin_point_map_mix.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);

    UPDATE temp_plugin_point_map_mix
    SET FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        FES_TEMP_OUT_NAME = temp_plugin_point_map_mix.TEMP_POINT_NAME
	FROM temp_plugin_fes_dev_map,fes_dev_pt_temp_def
	WHERE temp_plugin_point_map_mix.PLUGIN_NAME = pluginName 
    AND temp_plugin_point_map_mix.TEMP_NAME = devTag
    AND temp_plugin_point_map_mix.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME
    AND temp_plugin_point_map_mix.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME
    AND DEV_TP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME 
	AND POINT_TYPE = getFesPointType('FES_MIX_CTRL')
    AND temp_plugin_point_map_mix.TEMP_POINT_NAME = getAfterLastDot(TAG_NAME);
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION bingRtu(devTagName varchar(64), rtuTagName varchar(64))
RETURNS VOID
AS $$
BEGIN
    --先把RTU置空,促使触发器对对应点置空
    UPDATE accuml SET RTU_TAG = '' WHERE DEVICE = devTagName;
    UPDATE analog SET RTU_TAG = '' WHERE DEVICE = devTagName;
    
    UPDATE digital SET RTU_TAG = '' WHERE DEVICE = devTagName;
    UPDATE mix SET RTU_TAG = '' WHERE DEVICE = devTagName;
    --再关联
    UPDATE accuml SET RTU_TAG = rtuTagName WHERE DEVICE = devTagName;
    UPDATE analog SET RTU_TAG = rtuTagName WHERE DEVICE = devTagName;
    
    UPDATE digital SET RTU_TAG = rtuTagName WHERE DEVICE = devTagName;
    UPDATE mix SET RTU_TAG = rtuTagName WHERE DEVICE = devTagName;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION closeMyTrigger(closeFlag decimal(10,0))
RETURNS VOID
AS $$
BEGIN
    /*
     * 设置某些触发器的开关,获取开关状态使用myTriggerIsDisable
     * 目前支持本触发器开关的触发器有TI_TEMP_PLUGIN_DEV_MAP
     * 一般本存储过程需要成对使用时
     */
    
    IF closeFlag = 1 THEN 
       SET SESSION "disable.MyTrrigger" = 1;
    ELSE
       SET SESSION "disable.MyTrrigger" = 0;
    END IF;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION copyDevGroup(srcGroupName varchar(64),dstGroupName varchar(64),dstGroupDesc varchar(64))
RETURNS VOID
AS $$
    DECLARE totalInst int DEFAULT 1;
BEGIN
    /*
     * 复制整个设备组
     */
    
    IF COALESCE(srcGroupName,'') = '' OR COALESCE(dstGroupName,'')='' OR COALESCE(dstGroupDesc,'') = ''
    THEN RETURN;
    END IF;
    
    IF EXISTS (SELECT * FROM dev_group WHERE TAG_NAME = dstGroupName)
    THEN RETURN;
    END IF;
    
    --先插入目标设备组
    INSERT INTO dev_group (
    TAG_NAME,      DESCRIPTION, LOCATION_ID, SUB_SYSTEM, REGION_ID, PGROUP_TAG_NAME, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, GROUP_TAG)
    SELECT
    dstGroupName, dstGroupDesc, LOCATION_ID, SUB_SYSTEM, REGION_ID, PGROUP_TAG_NAME, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, GROUP_TAG
    FROM dev_group WHERE TAG_NAME = srcGroupName;
    
    SELECT count(*) INTO totalInst FROM dev_info WHERE TAG_NAME LIKE '%Gcp%';
    totalInst = totalInst + 1;
    --复制设备
    INSERT INTO dev_info(
    TAG_NAME,                           DESCRIPTION,                 LOCATION_ID, SUB_SYSTEM, GROUP_TAG_NAME, VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME)  SELECT      
    concat(TAG_NAME,'Gcp',totalInst), concat(DESCRIPTION,'Gcp'), LOCATION_ID, SUB_SYSTEM, dstGroupName,   VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME
    FROM dev_info  
    WHERE 
    dev_info.GROUP_TAG_NAME = srcGroupName ;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION copyDevInst( srcDevTagName varchar(64), dstDevTagName varchar(64), dstDesc varchar(64),dstLocation decimal(10,0), dstGroupTagName varchar(64), dstLevelId decimal(10,0),dstRegionId decimal(10,0), dstOptHandoverGroup varchar(64),  dstIfSummary decimal(10,0))
RETURNS VOID
AS $$
BEGIN
    IF EXISTS (SELECT * FROM dev_info WHERE TAG_NAME=dstDevTagName) THEN RETURN;
    END IF;
--往设备中插入一条记录,同时触发DEV表的触发器去插入点
    
INSERT INTO dev_info(
TAG_NAME,        DESCRIPTION,   LOCATION_ID, SUB_SYSTEM, GROUP_TAG_NAME, VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID,     OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME)  SELECT      
dstDevTagName, dstDesc,           dstLocation,     SUB_SYSTEM, dstGroupTagName,   dstLevelId,        DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, dstRegionId,   dstOptHandoverGroup,      dstIfSummary, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME
FROM dev_info WHERE TAG_NAME= srcDevTagName;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION copyDevTemp( srcTagName varchar(64),  dstTagName varchar(64), dstDesc varchar(64))
RETURNS VOID
AS $$
BEGIN
    
    
    IF EXISTS (SELECT * FROM dev_temp_def WHERE TAG_NAME=dstTagName) THEN RETURN;
    END IF;
    /*
     * 复制设备模板
     *  
     */ 
    --往设备模板中插入一条记录
    INSERT INTO dev_temp_def(
    TAG_NAME,  DEV_TYPE_ID, DEV_MODEL, DESCRIPTION, SUB_SYSTEM, PARA_STR1, PARA_STR2,DEV_TEMP_GRP )  SELECT      
    dstTagName, DEV_TYPE_ID,DEV_MODEL,dstDesc       ,SUB_SYSTEM, PARA_STR1,PARA_STR2,DEV_TEMP_GRP  
    FROM dev_temp_def WHERE TAG_NAME= srcTagName;
    
    --复制该模板的点,该点的TagName,DEV_TP_NAME需改变
    INSERT INTO accuml_temp_define( 
    TAG_NAME,                                            DESCRIPTION, DEV_TP_NAME, SEQ_NO, POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, UNIT_ID, IS_SAMPLE, SAMPLE_DEADBAND, SAMPLE_PERIOD, IS_STATISTICS, IS_LIMIT, LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, ALARM_PRIORITY, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, IS_FILTER, PERCENT, BASE, COEFF, RANGE_UP, RANGE_LOW, SEC_DEV_IN_GRP, SEC_DEVTP_NAME, SEC_SEQ_NO)  SELECT  
    concat(dstTagName, '.',  getAfterLastDot(TAG_NAME)), DESCRIPTION, dstTagName,  SEQ_NO, POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, UNIT_ID, IS_SAMPLE, SAMPLE_DEADBAND, SAMPLE_PERIOD, IS_STATISTICS, IS_LIMIT, LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, ALARM_PRIORITY, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, IS_FILTER, PERCENT, BASE, COEFF, RANGE_UP, RANGE_LOW, SEC_DEV_IN_GRP, SEC_DEVTP_NAME, SEC_SEQ_NO
    FROM accuml_temp_define WHERE DEV_TP_NAME= srcTagName;
    
    INSERT INTO analog_temp_define( 
    TAG_NAME,                                            DESCRIPTION, SEQ_NO, DEV_TP_NAME,  POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, UNIT_ID, IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, SAMPLE_DEADBAND, IS_LIMIT, LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, ALARM_PRIORITY, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, IS_FILTER, PERCENT, IS_VALID, DEADBAND, ZEROBAND, IS_CONTROL, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, SEC_DEV_IN_GRP, SEC_DEVTP_NAME, SEC_SEQ_NO, OUT_SEC_SEQ_NO)  SELECT  
    concat(dstTagName, '.',  getAfterLastDot(TAG_NAME)), DESCRIPTION, SEQ_NO, dstTagName,   POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, UNIT_ID, IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, SAMPLE_DEADBAND, IS_LIMIT, LIMIT_TYPE, LIMIT_NUM, LIMIT_UP1, LIMIT_LOW1, LIMIT_UP2, LIMIT_LOW2, LIMIT_UP3, LIMIT_LOW3, CROSS_PERCENT, RETURN_PERCENT, ALARM_PRIORITY, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, IS_FILTER, PERCENT, IS_VALID, DEADBAND, ZEROBAND, IS_CONTROL, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, SEC_DEV_IN_GRP, SEC_DEVTP_NAME, SEC_SEQ_NO, OUT_SEC_SEQ_NO
    FROM analog_temp_define WHERE DEV_TP_NAME= srcTagName;
    
    
        
    INSERT INTO digital_temp_define( 
    TAG_NAME,                                            DESCRIPTION, DEV_TP_NAME, SEQ_NO, POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, STATE_TEXT_NAME, VALUE_NUM, ALARM_PRIORITY, ALARM_DELAY_TIME, INPUT_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, IS_SUMMARY, IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, FILTER_ERR, FILTER_DISTURB, DISTURB_TIME, POLARITY, IS_CONTROL, CTRL_ACT_NAME, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, PULSE_DURATION, OFFSET_NUM, IS_ALARM_FREQ, ALARM_FREQ, ALARM_FREQ_DESC, IS_ALARM_TIME, ALARM_TIME, ALARM_TIME_DESC, IS_CHECK_PREVENTION, ALARM_FREQ_RANGE, SEC_DEV_IN_GRP, SEC_DEVTP_NAME, SEC_SEQ_NO1, SEC_SEQ_NO2, SEC_SEQ_NO3, SEC_SEQ_NO4, SEC_SEQ_NO5, OUT_SEC_SEQ_NO1, OUT_SEC_SEQ_NO2, OUT_SEC_SEQ_NO3, OUT_SEC_SEQ_NO4, OUT_SEC_SEQ_NO5)  SELECT  
    concat(dstTagName, '.',  getAfterLastDot(TAG_NAME)), DESCRIPTION, dstTagName,  SEQ_NO, POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, STATE_TEXT_NAME, VALUE_NUM, ALARM_PRIORITY, ALARM_DELAY_TIME, INPUT_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, IS_SUMMARY, IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, FILTER_ERR, FILTER_DISTURB, DISTURB_TIME, POLARITY, IS_CONTROL, CTRL_ACT_NAME, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, PULSE_DURATION, OFFSET_NUM, IS_ALARM_FREQ, ALARM_FREQ, ALARM_FREQ_DESC, IS_ALARM_TIME, ALARM_TIME, ALARM_TIME_DESC, IS_CHECK_PREVENTION, ALARM_FREQ_RANGE, SEC_DEV_IN_GRP, SEC_DEVTP_NAME, SEC_SEQ_NO1, SEC_SEQ_NO2, SEC_SEQ_NO3, SEC_SEQ_NO4, SEC_SEQ_NO5, OUT_SEC_SEQ_NO1, OUT_SEC_SEQ_NO2, OUT_SEC_SEQ_NO3, OUT_SEC_SEQ_NO4, OUT_SEC_SEQ_NO5
    FROM digital_temp_define WHERE DEV_TP_NAME= srcTagName;
    
    INSERT INTO mix_temp_define( 
    TAG_NAME,                                            DESCRIPTION, DEV_TP_NAME, SEQ_NO, POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, STATE_TEXT_NAME, ALARM_PRIORITY, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, IS_FILTER_ERROR, IS_FILTER_DISTURB, DISTURB_TIME, IS_CONTROL, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, CTRL_ACT_NAME, SEC_DEV_IN_GRP, SEC_DEVTP_NAME, SEC_SEQ_NO, OUT_SEC_SEQ_NO)  SELECT  
    concat(dstTagName, '.',  getAfterLastDot(TAG_NAME)), DESCRIPTION, dstTagName,  SEQ_NO, POINT_TYPE, POINT_CLASS, POINT_PROPERTY, POINT_SORT, IS_SAMPLE, IS_STATISTICS, SAMPLE_PERIOD, STATE_TEXT_NAME, ALARM_PRIORITY, ALARM_DELAY_TIME, IS_WATER_ALM, IS_ACK_ON_RTN, DEL_ACT_ON_ACK, IS_NEVER_ALM_ON_RTN, IS_FILTER_ERROR, IS_FILTER_DISTURB, DISTURB_TIME, IS_CONTROL, CTRL_TYPE, IS_TAGT_STATE, CTRL_TIMEOUT, RESV_TIMEOUT, BASE, COEFF, RANGE_UP, RANGE_LOW, CTRL_TOLERANCE, CTRL_ACT_NAME, SEC_DEV_IN_GRP, SEC_DEVTP_NAME, SEC_SEQ_NO, OUT_SEC_SEQ_NO
    FROM mix_temp_define WHERE DEV_TP_NAME= srcTagName;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION copyFesTemp(srcFesTempTag varchar(64),dstFesTempTag varchar(64),dstDesc varchar(128))
RETURNS VOID
AS $$
BEGIN
    /*
     * 复制前置模板
     */
    IF EXISTS (SELECT * FROM fes_dev_temp_def WHERE TAG_NAME=dstFesTempTag) THEN RETURN;
    END IF;
    
    --往设备模板中插入一条记录
    INSERT INTO fes_dev_temp_def(
    TAG_NAME,      DEV_TYPE_ID, DEV_MODEL, DESCRIPTION, SUB_SYSTEM, PARA_STR1, PARA_STR2 )  SELECT      
    dstFesTempTag, DEV_TYPE_ID, DEV_MODEL, dstDesc     ,SUB_SYSTEM, PARA_STR1, PARA_STR2  
    FROM fes_dev_temp_def WHERE TAG_NAME= srcFesTempTag;
    
    --复制该模板的点,该点的TagName,DEV_TP_NAME需改变
    INSERT INTO fes_dev_pt_temp_def(
    TAG_NAME,                                               POINT_TYPE, DEV_TP_NAME,  SEQ_NO, FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION, 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(dstFesTempTag, '.',  getAfterLastDot(TAG_NAME)), POINT_TYPE, dstFesTempTag,SEQ_NO, FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION, 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
    FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = srcFesTempTag;
    
    INSERT INTO fes_data_block_temp(
    TAG_NAME,                                               BLOCK_ID, FUNC_CODE, STAR_ADDR, DATA_LEN, DATA_FARME_TYPE, REF_CYCLE, IS_CREATE_SOE, PARA_INT1, PARA_INT2, DESCRIPTION, LD, CALL_MODE, DATA_SET, DATA_SET_TYPE, DATA_SET_CALL_TIME, REPORT_CTRL, DEV_TP_NAME
    ) SELECT 
    concat(dstFesTempTag, '.',  getAfterLastDot(TAG_NAME)), BLOCK_ID, FUNC_CODE, STAR_ADDR, DATA_LEN, DATA_FARME_TYPE, REF_CYCLE, IS_CREATE_SOE, PARA_INT1, PARA_INT2, DESCRIPTION, LD, CALL_MODE, DATA_SET, DATA_SET_TYPE, DATA_SET_CALL_TIME, REPORT_CTRL, dstFesTempTag
    FROM fes_data_block_temp WHERE DEV_TP_NAME = srcFesTempTag;
    
    INSERT INTO fes_const_temp(
    TAG_NAME,                                               DESCRIPTION, GROUP_NO, SEQ_NO, DEV_TP_NAME,  VALUE, VALUE_TYPE, UNIT_ID, VALUE_TEXT, VALUE_RATIO, VALUE_ADDED, VALUE_MAX, VALUE_MIN, VALUE_DEFAULT, VALUE_VERIFY, EX_INFO, PATH61850, POSITION61850
    ) SELECT 
    concat(dstFesTempTag, '.',  getAfterLastDot(TAG_NAME)), DESCRIPTION, GROUP_NO, SEQ_NO, dstFesTempTag, VALUE, VALUE_TYPE, UNIT_ID, VALUE_TEXT, VALUE_RATIO, VALUE_ADDED, VALUE_MAX, VALUE_MIN, VALUE_DEFAULT, VALUE_VERIFY, EX_INFO, PATH61850, POSITION61850
    FROM fes_const_temp WHERE DEV_TP_NAME = srcFesTempTag;
    
    
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION copyPlugin( srcPluginName varchar(64),dstPluginName varchar(64),dstPluginDesc varchar(128))
RETURNS VOID
AS $$
BEGIN
    /*
     * 复制套件
     */
    INSERT INTO temp_plugin (TAG_NAME,DESCRIPTION)
    VALUES (dstPluginName,dstPluginDesc);
    
    --关闭对应的触发器
    CALL closeMyTrigger(1);
    
    INSERT INTO temp_plugin_dev_map (PLUGIN_NAME,TEMP_NAME)
    SELECT dstPluginName, TEMP_NAME
    FROM temp_plugin_dev_map WHERE PLUGIN_NAME = srcPluginName;
    
    INSERT INTO temp_plugin_fes_dev_map(PLUGIN_NAME,TEMP_NAME,FES_TEMP_NAME)
    SELECT dstPluginName,TEMP_NAME,FES_TEMP_NAME
    FROM temp_plugin_fes_dev_map WHERE PLUGIN_NAME  = srcPluginName;
    
    INSERT INTO temp_plugin_point_map_acc (PLUGIN_NAME,TEMP_NAME,TEMP_POINT_NAME,FES_TEMP_NAME,FES_TEMP_POINT_NAME)
    SELECT dstPluginName,TEMP_NAME,TEMP_POINT_NAME,FES_TEMP_NAME,FES_TEMP_POINT_NAME
    FROM temp_plugin_point_map_acc WHERE PLUGIN_NAME = srcPluginName;
    
    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 dstPluginName,TEMP_NAME,TEMP_POINT_NAME,FES_TEMP_NAME,FES_TEMP_POINT_NAME,FES_TEMP_OUT_NAME
    FROM temp_plugin_point_map_ana WHERE PLUGIN_NAME = srcPluginName;
    
    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 dstPluginName,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
    FROM temp_plugin_point_map_dig WHERE PLUGIN_NAME = srcPluginName;
    
    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 dstPluginName,TEMP_NAME,TEMP_POINT_NAME,FES_TEMP_NAME,FES_TEMP_POINT_NAME,FES_TEMP_OUT_NAME
    FROM temp_plugin_point_map_mix WHERE PLUGIN_NAME = srcPluginName;
    
    CALL closeMyTrigger(0);
    
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION createOptLock(tableName varchar(64),pointTempTag varchar(64),funcTag varchar(64),argNum decimal(10,0))
RETURNS VOID
AS $$
	DECLARE hasDone int DEFAULT 0;
    DECLARE argTag varchar(64);
    DECLARE argNo int;
    DECLARE pointTempTagName varchar(64);
    DECLARE cur_args CURSOR FOR 
	SELECT DEV_TAG,NO FROM export_seq;
BEGIN
    /*
     * 依据指定的模板点,用于创建闭锁出口,除了参数列表中的参数,还有部分参数在export_seq表中
     * tableName:表名,指出pointTempTag是属于那张表的,目前取值范围有analog,digital,mix
     * pointTempTag:测点模板的标签名
     * funcTag:闭锁出口选用的函数
     * argNum:闭锁函数的参数个数
     * export_seq表中保存了参数1-20
     */
    
    
    IF COALESCE(tableName,'') = '' OR COALESCE(pointTempTag,'')=''
    THEN RETURN;
    END IF;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpCreateOptLockTbl(
        POINT_TAG_NAME varchar(64) NOT NULL,
        POINT_DESC varchar(128),
        GROUP_TAG_NAME varchar(64) NOT NULL,
        LOCATION_ID decimal(10,0),
        SUBSYSTEM decimal(10,0),
        ARGS_TAG_NAME varchar(64) DEFAULT ''
    );
    DELETE FROM tmpCreateOptLockTbl;
    
    IF lower(tableName) = 'analog'
    THEN
        INSERT INTO tmpCreateOptLockTbl(POINT_TAG_NAME,POINT_DESC,GROUP_TAG_NAME,LOCATION_ID,SUBSYSTEM)
        SELECT analog.TAG_NAME,analog.DESCRIPTION, dev_info.GROUP_TAG_NAME,analog.LOCATION_ID,analog.SUB_SYSTEM
        FROM analog,dev_info
        WHERE analog.POINT_TP_NAME = pointTempTag AND analog.DEVICE = dev_info.TAG_NAME;
    ELSEIF lower(tableName) = 'digital'
    THEN
        INSERT INTO tmpCreateOptLockTbl(POINT_TAG_NAME,POINT_DESC,GROUP_TAG_NAME,LOCATION_ID,SUBSYSTEM)
        SELECT digital.TAG_NAME,digital.DESCRIPTION, dev_info.GROUP_TAG_NAME,digital.LOCATION_ID,digital.SUB_SYSTEM
        FROM digital,dev_info
        WHERE digital.POINT_TP_NAME = pointTempTag AND digital.DEVICE = dev_info.TAG_NAME;
    ELSEIF lower(tableName) = 'mix'
    THEN
        INSERT INTO tmpCreateOptLockTbl(POINT_TAG_NAME,POINT_DESC,GROUP_TAG_NAME,LOCATION_ID,SUBSYSTEM)
        SELECT mix.TAG_NAME,mix.DESCRIPTION, dev_info.GROUP_TAG_NAME,mix.LOCATION_ID,mix.SUB_SYSTEM
        FROM mix,dev_info
        WHERE mix.POINT_TP_NAME = pointTempTag AND mix.DEVICE = dev_info.TAG_NAME;
    END IF;
    
    INSERT INTO opt_interlock_para(TAG_NAME, DESCRIPTION, LOCATION_ID, SUB_SYSTEM, INTERLOCK_TAG, INTERLOCK_NUM, 
    INTERLOCK_IN_TAG1, INTERLOCK_IN_TAG2, INTERLOCK_IN_TAG3, INTERLOCK_IN_TAG4, INTERLOCK_IN_TAG5, INTERLOCK_IN_TAG6, INTERLOCK_IN_TAG7, 
    INTERLOCK_IN_TAG8, INTERLOCK_IN_TAG9, INTERLOCK_IN_TAG10, INTERLOCK_IN_TAG11, INTERLOCK_IN_TAG12, INTERLOCK_IN_TAG13, 
    INTERLOCK_IN_TAG14, INTERLOCK_IN_TAG15, INTERLOCK_IN_TAG16, INTERLOCK_IN_TAG17, INTERLOCK_IN_TAG18, INTERLOCK_IN_TAG19, 
    INTERLOCK_IN_TAG20)
    SELECT POINT_TAG_NAME,POINT_DESC,LOCATION_ID,SUBSYSTEM,funcTag,argNum,
    '','','','','','','','','','','','','','','','','','','',''
    FROM tmpCreateOptLockTbl;
    
    --根据export_seq表中的数据更新输入参数
    --通过游标遍历export_Seq中的数据,把每个输入参数写入opt_interlock_para中
    OPEN cur_args;
    
    LOOP
        FETCH cur_args INTO argTag,argNo;
		     EXIT WHEN NOT FOUND; 
        --argTag中的格式是表名.测点模板名
        tableName = getBeforeFirstDot(argTag);
        pointTempTagName = getAfterFirstDot(argTag);
        
        IF tableName = 'analog' THEN 
           UPDATE tmpCreateOptLockTbl
           SET ARGS_TAG_NAME = analog.TAG_NAME
		   FROM analog,dev_info
		   WHERE analog.POINT_TP_NAME = pointTempTagName 
		   AND analog.DEVICE = dev_info.TAG_NAME
           AND tmpCreateOptLockTbl.GROUP_TAG_NAME = dev_info.GROUP_TAG_NAME;
        ELSEIF tableName = 'digital' THEN
           UPDATE tmpCreateOptLockTbl
           SET ARGS_TAG_NAME = digital.TAG_NAME
		   FROM digital,dev_info
		   WHERE digital.POINT_TP_NAME = pointTempTagName 
		   AND digital.DEVICE = dev_info.TAG_NAME
           AND tmpCreateOptLockTbl.GROUP_TAG_NAME = dev_info.GROUP_TAG_NAME;
        ELSEIF tableName = 'mix' THEN
           UPDATE tmpCreateOptLockTbl
           SET ARGS_TAG_NAME = mix.TAG_NAME
		   FROM mix,dev_info
		   WHERE mix.POINT_TP_NAME = pointTempTagName 
		   AND mix.DEVICE = dev_info.TAG_NAME
           AND tmpCreateOptLockTbl.GROUP_TAG_NAME = dev_info.GROUP_TAG_NAME;
        END IF;
    
        --更新闭锁输入参数
        UPDATE opt_interlock_para 
        SET 
        INTERLOCK_IN_TAG1 = if(argNo = 1,ARGS_TAG_NAME,INTERLOCK_IN_TAG1),
        INTERLOCK_IN_TAG2 = if(argNo = 2,ARGS_TAG_NAME,INTERLOCK_IN_TAG2),
        INTERLOCK_IN_TAG3 = if(argNo = 3,ARGS_TAG_NAME,INTERLOCK_IN_TAG3),
        INTERLOCK_IN_TAG4 = if(argNo = 4,ARGS_TAG_NAME,INTERLOCK_IN_TAG4),
        INTERLOCK_IN_TAG5 = if(argNo = 5,ARGS_TAG_NAME,INTERLOCK_IN_TAG5),
        INTERLOCK_IN_TAG6 = if(argNo = 6,ARGS_TAG_NAME,INTERLOCK_IN_TAG6),
        INTERLOCK_IN_TAG7 = if(argNo = 7,ARGS_TAG_NAME,INTERLOCK_IN_TAG7),
        INTERLOCK_IN_TAG8 = if(argNo = 8,ARGS_TAG_NAME,INTERLOCK_IN_TAG8),
        INTERLOCK_IN_TAG9 = if(argNo = 9,ARGS_TAG_NAME,INTERLOCK_IN_TAG9),
        INTERLOCK_IN_TAG10 = if(argNo = 10,ARGS_TAG_NAME,INTERLOCK_IN_TAG10),
        INTERLOCK_IN_TAG11 = if(argNo = 11,ARGS_TAG_NAME,INTERLOCK_IN_TAG11),
        INTERLOCK_IN_TAG12 = if(argNo = 12,ARGS_TAG_NAME,INTERLOCK_IN_TAG12),
        INTERLOCK_IN_TAG13 = if(argNo = 13,ARGS_TAG_NAME,INTERLOCK_IN_TAG13),
        INTERLOCK_IN_TAG14 = if(argNo = 14,ARGS_TAG_NAME,INTERLOCK_IN_TAG14),
        INTERLOCK_IN_TAG15 = if(argNo = 15,ARGS_TAG_NAME,INTERLOCK_IN_TAG15),
        INTERLOCK_IN_TAG16 = if(argNo = 16,ARGS_TAG_NAME,INTERLOCK_IN_TAG16),
        INTERLOCK_IN_TAG17 = if(argNo = 17,ARGS_TAG_NAME,INTERLOCK_IN_TAG17),
        INTERLOCK_IN_TAG18 = if(argNo = 18,ARGS_TAG_NAME,INTERLOCK_IN_TAG18),
        INTERLOCK_IN_TAG19 = if(argNo = 19,ARGS_TAG_NAME,INTERLOCK_IN_TAG19),
        INTERLOCK_IN_TAG20 = if(argNo = 20,ARGS_TAG_NAME,INTERLOCK_IN_TAG20)
		FROM tmpCreateOptLockTbl
        WHERE opt_interlock_para.TAG_NAME = tmpCreateOptLockTbl.POINT_TAG_NAME
        ;
         
    END LOOP;
    
    CLOSE cur_args;
    
END;
$$LANGUAGE PLPGSQL;





CREATE OR REPLACE FUNCTION db_sync(in src_hostname varchar(255),in src_last_id bigint,in src_last_statement TEXT,in new_id bigint,in new_statement TEXT)
RETURNS int
AS $$
    declare local_last_id bigint default 0;
    declare local_last_statement TEXT;
    declare local_hostname TEXT;
BEGIN
    
    -- 数据库同步服务 db_sync_server 调用

    local_hostname = pg_read_file('hostname');
    if lower(substring(local_hostname from 0 for char_length(local_hostname))) = lower(src_hostname) then
        -- 如果源主机就是本主机，则无需同步
        RETURN 10;
    end if;
    -- 存储过程中不支持锁表，程序在调用本存储过程前锁表
    -- lock tables dbop_list write;
    select max(id) into local_last_id from dbop_list;
    select statement into local_last_statement from dbop_list where ID = local_last_id;
    if local_last_id is null or local_last_statement is null then
        -- 表为空
        RETURN 20;
    end if;
    if src_last_id < local_last_id then
        -- 数据不一致
        RETURN 21;
    end if;
    if src_last_id > local_last_id then
        -- 数据不一致
        RETURN 22;
    end if;
    if src_last_statement <> local_last_statement then
        -- 数据不一致
        RETURN 23;
    end if;
    begin
        -- 执行需要同步的语句
        execute new_statement;
        -- 插入同步语句到本地表
        insert into dbop_list(ID, STATEMENT) values (new_id,new_statement);
        -- 执行成功
        RETURN 0;
    exception
    	when QUERY_CANCELED then
    		RETURN 100;
    	when OTHERS then
    		RETURN 100;
    end;
END;
$$LANGUAGE PLPGSQL;





CREATE OR REPLACE FUNCTION exportBgXFes( )
RETURNS VOID
AS $$
BEGIN
    --export_seq里保存了导出的设备的顺序
    --导出结果保存在background_x_fes_的表中  
    --先清除
    DELETE FROM background_x_fes_accuml;
    DELETE FROM background_x_fes_ana_ctrl;
    DELETE FROM background_x_fes_analog;
    DELETE FROM background_x_fes_dig_ctrl;
    DELETE FROM background_x_fes_digtal;
    DELETE FROM background_x_fes_mix;
    DELETE FROM background_x_fes_mix_ctrl;
    
    --插入所有ACC后台点
    INSERT INTO background_x_fes_accuml (
    BG_DEV_TAG,         BG_DEV_DESC,          BG_POINT_TAG,                  BG_POINT_OFFSET,BG_POINT_DESC,                  RTU_TAG,                  RTU_DOT_NO,              RTU_DOT_DESC,  RTU_DEV_TAG,          RTU_RES_PARA_INT1,   RTU_RES_PARA_INT2,   RTU_RES_PARA_INT3,    RTU_RES_PARA_INT4)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, accuml_map_to_fes.KEY_ID_TAG,  '1',            accuml_map_to_fes.DESCRIPTION,  accuml_map_to_fes.RTU_TAG,accuml_map_to_fes.DOT_NO,'',             '',                  0,                   0,                   0,                        0
    FROM  export_seq , dev_info ,accuml, accuml_map_to_fes 
    WHERE  export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = accuml.DEVICE AND accuml_map_to_fes.KEY_ID_TAG = concat('accuml','.', accuml.TAG_NAME)
    ORDER BY export_seq.NO ,accuml.SEQ_NO;
    
    --检索后台点,已有关联的,需要把关联导出
    UPDATE background_x_fes_accuml
    SET RTU_DOT_DESC = fes_accuml.DESCRIPTION,
        RTU_DEV_TAG = fes_accuml.DEV_TAG,
        RTU_RES_PARA_INT1 = fes_accuml.RES_PARA_INT1,
        RTU_RES_PARA_INT2 = fes_accuml.RES_PARA_INT2,
        RTU_RES_PARA_INT3 = fes_accuml.RES_PARA_INT3,
        RTU_RES_PARA_INT4 = fes_accuml.RES_PARA_INT4
	FROM fes_accuml
	WHERE fes_accuml.TAG_NAME = concat(background_x_fes_accuml.RTU_TAG,'.',background_x_fes_accuml.RTU_DOT_NO);
    
    --插入所有ANA后台点
    INSERT INTO background_x_fes_analog (
    BG_DEV_TAG,         BG_DEV_DESC,                           BG_POINT_TAG,  BG_POINT_OFFSET,BG_POINT_DESC,                 RTU_TAG,                  RTU_DOT_NO,              RTU_DOT_DESC,  RTU_DEV_TAG, RTU_RES_PARA_INT1,  RTU_RES_PARA_INT2,  RTU_RES_PARA_INT3,RTU_RES_PARA_INT4)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, analog_map_to_fes.KEY_ID_TAG,  '1',            analog_map_to_fes.DESCRIPTION,  analog_map_to_fes.RTU_TAG,analog_map_to_fes.DOT_NO,'',            '',          0,                  0,                  0,                0
    FROM  export_seq , dev_info ,analog, analog_map_to_fes
    WHERE  export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = analog.DEVICE AND analog_map_to_fes.KEY_ID_TAG = concat('analog','.', analog.TAG_NAME)
    ORDER BY export_seq.NO,analog.SEQ_NO;
  
    --检索后台点,已有关联的,需要把关联导出
    UPDATE background_x_fes_analog 
    SET RTU_DOT_DESC = fes_analog.DESCRIPTION,
        RTU_DEV_TAG = fes_analog.DEV_TAG,
        RTU_RES_PARA_INT1 = fes_analog.RES_PARA_INT1,
        RTU_RES_PARA_INT2 = fes_analog.RES_PARA_INT2,
        RTU_RES_PARA_INT3 = fes_analog.RES_PARA_INT3,
        RTU_RES_PARA_INT4 = fes_analog.RES_PARA_INT4
	FROM fes_analog
    WHERE fes_analog.TAG_NAME = concat(background_x_fes_analog.RTU_TAG,'.',background_x_fes_analog.RTU_DOT_NO);
    
    --插入analog_Ctrl
    INSERT INTO background_x_fes_ana_ctrl(
    BG_DEV_TAG,         BG_DEV_DESC,           BG_POINT_TAG,     BG_POINT_OFFSET,BG_POINT_DESC,      RTU_TAG,               RTU_DOT_NO,              RTU_DOT_DESC,  RTU_DEV_TAG, RTU_RES_PARA_INT1, RTU_RES_PARA_INT2,  RTU_RES_PARA_INT3,RTU_RES_PARA_INT4)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, analog.TAG_NAME,  '1',            analog.DESCRIPTION,  analog_control.RTU_TAG,analog_control.OFFSET_NO,'',            '',           0,                0,                  0,                0
    FROM export_seq , dev_info , analog ,analog_control 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = analog.DEVICE AND analog.TAG_NAME = analog_control.TAG_NAME
    ORDER BY export_seq.NO,analog.SEQ_NO;
    
    --检索后台点,已有关联的,需要把关联导出
    UPDATE background_x_fes_ana_ctrl
    SET RTU_DOT_DESC = fes_analog_ctrl.DESCRIPTION,
        RTU_DEV_TAG = fes_analog_ctrl.DEV_TAG,
        RTU_RES_PARA_INT1 = fes_analog_ctrl.RES_PARA_INT1,
        RTU_RES_PARA_INT2 = fes_analog_ctrl.RES_PARA_INT2,
        RTU_RES_PARA_INT3 = fes_analog_ctrl.RES_PARA_INT3,
        RTU_RES_PARA_INT4 = fes_analog_ctrl.RES_PARA_INT4
	FROM fes_analog_ctrl 
    WHERE fes_analog_ctrl.TAG_NAME = concat(background_x_fes_ana_ctrl.RTU_TAG,'.',background_x_fes_ana_ctrl.RTU_DOT_NO);
    
    --对于digital,注意其有5个分量
    INSERT INTO background_x_fes_digtal (
    BG_DEV_TAG,        BG_DEV_DESC,          BG_POINT_TAG,                 BG_POINT_OFFSET,BG_POINT_DESC,                  RTU_TAG,                  RTU_DOT_NO,                RTU_DOT_DESC, RTU_DEV_TAG, RTU_RES_PARA_INT1, RTU_RES_PARA_INT2, RTU_RES_PARA_INT3, RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO) SELECT
    export_seq.DEV_TAG,dev_info.DESCRIPTION, digital_map_to_fes.KEY_ID_TAG, 1,             digital_map_to_fes.DESCRIPTION,digital_map_to_fes.RTU_TAG,digital_map_to_fes.DOT_NO1,'',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq,dev_info, digital,digital_map_to_fes 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = digital.DEVICE AND digital_map_to_fes.KEY_ID_TAG = concat('digital.',digital.TAG_NAME)
    AND digital_map_to_fes.VALUE_NUM >= 1;
    
    INSERT INTO background_x_fes_digtal (
    BG_DEV_TAG,        BG_DEV_DESC,          BG_POINT_TAG,                 BG_POINT_OFFSET,BG_POINT_DESC,                  RTU_TAG,                  RTU_DOT_NO,                RTU_DOT_DESC, RTU_DEV_TAG, RTU_RES_PARA_INT1, RTU_RES_PARA_INT2, RTU_RES_PARA_INT3, RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO) SELECT
    export_seq.DEV_TAG,dev_info.DESCRIPTION, digital_map_to_fes.KEY_ID_TAG, 2,             digital_map_to_fes.DESCRIPTION,digital_map_to_fes.RTU_TAG,digital_map_to_fes.DOT_NO2,'',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq,dev_info, digital,digital_map_to_fes 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = digital.DEVICE AND digital_map_to_fes.KEY_ID_TAG = concat('digital.',digital.TAG_NAME)
    AND digital_map_to_fes.VALUE_NUM >= 2;
    
    INSERT INTO background_x_fes_digtal (
    BG_DEV_TAG,        BG_DEV_DESC,          BG_POINT_TAG,                 BG_POINT_OFFSET,BG_POINT_DESC,                  RTU_TAG,                  RTU_DOT_NO,                RTU_DOT_DESC, RTU_DEV_TAG, RTU_RES_PARA_INT1, RTU_RES_PARA_INT2, RTU_RES_PARA_INT3, RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO) SELECT
    export_seq.DEV_TAG,dev_info.DESCRIPTION, digital_map_to_fes.KEY_ID_TAG, 3,             digital_map_to_fes.DESCRIPTION,digital_map_to_fes.RTU_TAG,digital_map_to_fes.DOT_NO3,'',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq,dev_info, digital,digital_map_to_fes 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = digital.DEVICE AND digital_map_to_fes.KEY_ID_TAG = concat('digital.',digital.TAG_NAME)
    AND digital_map_to_fes.VALUE_NUM >= 3;
    
    INSERT INTO background_x_fes_digtal (
    BG_DEV_TAG,        BG_DEV_DESC,          BG_POINT_TAG,                 BG_POINT_OFFSET,BG_POINT_DESC,                  RTU_TAG,                  RTU_DOT_NO,                RTU_DOT_DESC, RTU_DEV_TAG, RTU_RES_PARA_INT1, RTU_RES_PARA_INT2, RTU_RES_PARA_INT3, RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO) SELECT
    export_seq.DEV_TAG,dev_info.DESCRIPTION, digital_map_to_fes.KEY_ID_TAG, 4,             digital_map_to_fes.DESCRIPTION,digital_map_to_fes.RTU_TAG,digital_map_to_fes.DOT_NO4,'',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq,dev_info, digital,digital_map_to_fes 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = digital.DEVICE AND digital_map_to_fes.KEY_ID_TAG = concat('digital.',digital.TAG_NAME)
    AND digital_map_to_fes.VALUE_NUM >= 4;
    
    INSERT INTO background_x_fes_digtal (
    BG_DEV_TAG,        BG_DEV_DESC,          BG_POINT_TAG,                 BG_POINT_OFFSET,BG_POINT_DESC,                  RTU_TAG,                  RTU_DOT_NO,                RTU_DOT_DESC, RTU_DEV_TAG, RTU_RES_PARA_INT1, RTU_RES_PARA_INT2, RTU_RES_PARA_INT3, RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO) SELECT
    export_seq.DEV_TAG,dev_info.DESCRIPTION, digital_map_to_fes.KEY_ID_TAG, 5,             digital_map_to_fes.DESCRIPTION,digital_map_to_fes.RTU_TAG,digital_map_to_fes.DOT_NO5,'',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq,dev_info, digital,digital_map_to_fes 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = digital.DEVICE AND digital_map_to_fes.KEY_ID_TAG = concat('digital.',digital.TAG_NAME)
    AND digital_map_to_fes.VALUE_NUM >= 5;
    
    --检索后台点,已有关联的,需要把关联导出
    UPDATE background_x_fes_digtal
    SET RTU_DOT_DESC = fes_digital.DESCRIPTION,
        RTU_DEV_TAG = fes_digital.DEV_TAG,
        RTU_RES_PARA_INT1 = fes_digital.RES_PARA_INT1,
        RTU_RES_PARA_INT2 = fes_digital.RES_PARA_INT2,
        RTU_RES_PARA_INT3 = fes_digital.RES_PARA_INT3,
        RTU_RES_PARA_INT4 = fes_digital.RES_PARA_INT4
	FROM fes_digital  
    WHERE fes_digital.TAG_NAME = concat(background_x_fes_digtal.RTU_TAG,'.',background_x_fes_digtal.RTU_DOT_NO) ;
            
    --对于digital_ctrl
    INSERT INTO background_x_fes_dig_ctrl (
    BG_DEV_TAG,         BG_DEV_DESC,           BG_POINT_TAG,            BG_POINT_OFFSET,  BG_POINT_DESC,        RTU_TAG,               RTU_DOT_NO,                 RTU_DOT_DESC,  RTU_DEV_TAG,  RTU_RES_PARA_INT1, RTU_RES_PARA_INT2,RTU_RES_PARA_INT3,  RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, digital_control.TAG_NAME,  1,               digital.DESCRIPTION, digital_control.RTU_TAG,digital_control.OFFSET_NO1, '',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq , dev_info ,digital , digital_control 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG=digital.DEVICE AND digital.TAG_NAME = digital_control.TAG_NAME
    AND digital_control.OFFSET_NUM >= 1;
    
    INSERT INTO background_x_fes_dig_ctrl (
    BG_DEV_TAG,         BG_DEV_DESC,           BG_POINT_TAG,            BG_POINT_OFFSET,  BG_POINT_DESC,        RTU_TAG,               RTU_DOT_NO,                 RTU_DOT_DESC,  RTU_DEV_TAG,  RTU_RES_PARA_INT1, RTU_RES_PARA_INT2,RTU_RES_PARA_INT3,  RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, digital_control.TAG_NAME,  2,               digital.DESCRIPTION, digital_control.RTU_TAG,digital_control.OFFSET_NO2, '',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq , dev_info ,digital , digital_control 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG=digital.DEVICE AND digital.TAG_NAME = digital_control.TAG_NAME
    AND digital_control.OFFSET_NUM >= 2;
    
    INSERT INTO background_x_fes_dig_ctrl (
    BG_DEV_TAG,         BG_DEV_DESC,           BG_POINT_TAG,            BG_POINT_OFFSET,  BG_POINT_DESC,        RTU_TAG,               RTU_DOT_NO,                 RTU_DOT_DESC,  RTU_DEV_TAG,  RTU_RES_PARA_INT1, RTU_RES_PARA_INT2,RTU_RES_PARA_INT3,  RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, digital_control.TAG_NAME,  3,               digital.DESCRIPTION, digital_control.RTU_TAG,digital_control.OFFSET_NO3, '',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq , dev_info ,digital , digital_control 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG=digital.DEVICE AND digital.TAG_NAME = digital_control.TAG_NAME
    AND digital_control.OFFSET_NUM >= 3;
    
    INSERT INTO background_x_fes_dig_ctrl (
    BG_DEV_TAG,         BG_DEV_DESC,           BG_POINT_TAG,            BG_POINT_OFFSET,  BG_POINT_DESC,        RTU_TAG,               RTU_DOT_NO,                 RTU_DOT_DESC,  RTU_DEV_TAG,  RTU_RES_PARA_INT1, RTU_RES_PARA_INT2,RTU_RES_PARA_INT3,  RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, digital_control.TAG_NAME,  4,               digital.DESCRIPTION, digital_control.RTU_TAG,digital_control.OFFSET_NO4, '',           '',          0,                 0,                0,                  0,            export_seq.NO,digital.SEQ_NO
    FROM export_seq , dev_info ,digital , digital_control 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG=digital.DEVICE AND digital.TAG_NAME = digital_control.TAG_NAME
    AND digital_control.OFFSET_NUM >= 4;
    
    INSERT INTO background_x_fes_dig_ctrl (
    BG_DEV_TAG,         BG_DEV_DESC,           BG_POINT_TAG,            BG_POINT_OFFSET,  BG_POINT_DESC,        RTU_TAG,               RTU_DOT_NO,                 RTU_DOT_DESC,  RTU_DEV_TAG,  RTU_RES_PARA_INT1, RTU_RES_PARA_INT2,RTU_RES_PARA_INT3,  RTU_RES_PARA_INT4,DEV_NO,         SEQ_NO)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, digital_control.TAG_NAME,  5,               digital.DESCRIPTION, digital_control.RTU_TAG,digital_control.OFFSET_NO5, '',           '',          0,                 0,                0,                  0,             export_seq.NO,digital.SEQ_NO
    FROM export_seq , dev_info ,digital , digital_control 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG=digital.DEVICE AND digital.TAG_NAME = digital_control.TAG_NAME
    AND digital_control.OFFSET_NUM >= 5;
    --检索后台点,已有关联的,需要把关联导出
    UPDATE background_x_fes_dig_ctrl
    SET RTU_DOT_DESC = fes_digital_ctrl.DESCRIPTION,
        RTU_DEV_TAG = fes_digital_ctrl.DEV_TAG,
        RTU_RES_PARA_INT1 = fes_digital_ctrl.RES_PARA_INT1,
        RTU_RES_PARA_INT2 = fes_digital_ctrl.RES_PARA_INT2,
        RTU_RES_PARA_INT3 = fes_digital_ctrl.RES_PARA_INT3,
        RTU_RES_PARA_INT4 = fes_digital_ctrl.RES_PARA_INT4
	FROM fes_digital_ctrl 
    WHERE fes_digital_ctrl.TAG_NAME = concat(background_x_fes_dig_ctrl.RTU_TAG,'.',background_x_fes_dig_ctrl.RTU_DOT_NO) ;
    
    --mix
    INSERT INTO background_x_fes_mix (
    BG_DEV_TAG,         BG_DEV_DESC,          BG_POINT_TAG,               BG_POINT_OFFSET,BG_POINT_DESC,               RTU_TAG,               RTU_DOT_NO,           RTU_DOT_DESC,  RTU_DEV_TAG, RTU_RES_PARA_INT1,  RTU_RES_PARA_INT2,  RTU_RES_PARA_INT3,RTU_RES_PARA_INT4)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, mix_map_to_fes.KEY_ID_TAG,  '1',            mix_map_to_fes.DESCRIPTION,  mix_map_to_fes.RTU_TAG,mix_map_to_fes.DOT_NO,'',           '',          0,                 0,                0,                  0
    FROM  export_seq , dev_info ,mix, mix_map_to_fes
    WHERE  export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = mix.DEVICE AND mix_map_to_fes.KEY_ID_TAG = concat('mix','.',mix.TAG_NAME)
    ORDER BY export_seq.NO,mix.SEQ_NO;
    --检索后台点,已有关联的,需要把关联导出
    UPDATE background_x_fes_mix
    SET RTU_DOT_DESC = fes_mix.DESCRIPTION,
        RTU_DEV_TAG = fes_mix.DEV_TAG,
        RTU_RES_PARA_INT1 = fes_mix.RES_PARA_INT1,
        RTU_RES_PARA_INT2 = fes_mix.RES_PARA_INT2,
        RTU_RES_PARA_INT3 = fes_mix.RES_PARA_INT3,
        RTU_RES_PARA_INT4 = fes_mix.RES_PARA_INT4
	FROM fes_mix
    WHERE fes_mix.TAG_NAME = concat(background_x_fes_mix.RTU_TAG,'.',background_x_fes_mix.RTU_DOT_NO);
    
    --插入mix_Ctrl
    INSERT INTO background_x_fes_mix_ctrl(
    BG_DEV_TAG,         BG_DEV_DESC,          BG_POINT_TAG,         BG_POINT_OFFSET,BG_POINT_DESC,    RTU_TAG,            RTU_DOT_NO,          RTU_DOT_DESC,  RTU_DEV_TAG, RTU_RES_PARA_INT1,  RTU_RES_PARA_INT2,  RTU_RES_PARA_INT3,RTU_RES_PARA_INT4)SELECT 
    export_seq.DEV_TAG, dev_info.DESCRIPTION, mix_control.TAG_NAME, '1',            mix.DESCRIPTION,  mix_control.RTU_TAG,mix_control.OFFSET_NO,'',           '',          0,                  0,                 0,                  0
    FROM export_seq , dev_info , mix ,mix_control 
    WHERE export_seq.DEV_TAG = dev_info.TAG_NAME AND export_seq.DEV_TAG = mix.DEVICE AND mix.TAG_NAME = mix_control.TAG_NAME 
    ORDER BY export_seq.NO,mix.SEQ_NO;
    --检索后台点,已有关联的,需要把关联导出
    UPDATE background_x_fes_mix_ctrl
    SET RTU_DOT_DESC = fes_mix_ctrl.DESCRIPTION,
        RTU_DEV_TAG = fes_mix_ctrl.DEV_TAG,
        RTU_RES_PARA_INT1 = fes_mix_ctrl.RES_PARA_INT1,
        RTU_RES_PARA_INT2 = fes_mix_ctrl.RES_PARA_INT2,
        RTU_RES_PARA_INT3 = fes_mix_ctrl.RES_PARA_INT3,
        RTU_RES_PARA_INT4 = fes_mix_ctrl.RES_PARA_INT4
	FROM fes_mix_ctrl 
    WHERE fes_mix_ctrl.TAG_NAME=concat(background_x_fes_mix_ctrl.RTU_TAG,'.',background_x_fes_mix_ctrl.RTU_DOT_NO);
      
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION generateBatchDev(devTagName varchar(64), batchNum decimal(10,0),batchStart decimal(10,0), formatTagName varchar(64),formatDesc varchar(128),fesRtuTagName varchar(64))
RETURNS VOID
AS $$
    DECLARE stepNum int DEFAULT 0;
    DECLARE fesDevTempTagName varchar(64) DEFAULT '';
    DECLARE tempStr varchar(64) DEFAULT '';
    DECLARE locationTagName varchar(64) DEFAULT '';
    DECLARE tmpDevTag varchar(64) DEFAULT '';
    DECLARE tmpFesDevTag varchar(64) DEFAULT '';
    DECLARE accSize decimal(10,0) DEFAULT 0;
    DECLARE anaSize decimal(10,0) DEFAULT 0;
    DECLARE digSize decimal(10,0) DEFAULT 0;
    DECLARE mixSize decimal(10,0) DEFAULT 0;
BEGIN
    --批量生成后台设备,当fesRtuTagName不为空时,同时批量生成对应的前置设备
    --devTagName 后台设备名
    --batchNum 批量生成的数量
    --batchStart 批量的起始数字
    --formatTagName TagName字段的批量格式
    --formatDesc 描述字段的批量格式
    --fesRtuTagName 前置RTU的标签名,为空表示不生成对应的前置设备
    
    --BAT_TAG_NAME中存储着短名
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpGenerateBatchDevTbl(
        BAT_TAG_NAME varchar(64) NOT NULL ,
        BAT_DESC varchar(64) NOT NULL
    );
    DELETE FROM tmpGenerateBatchDevTbl;
    
    locationTagName = getBeforeLastDot(devTagName);
    --根据批量规则生成一系列标签名和描述,
    stepNum = batchStart;
    WHILE stepNum < (batchStart + batchNum) LOOP
        INSERT INTO tmpGenerateBatchDevTbl(BAT_TAG_NAME,BAT_DESC)
        VALUES (replace(formatTagName,'%1',stepNum), replace(formatDesc,'%1',stepNum));
        stepNum = stepNum + 1;
    END LOOP;
    
    --第一个设备在调用本存储过程之前已经生成,这里不必再重新生成
    INSERT INTO dev_info(
    TAG_NAME,                                 DESCRIPTION, LOCATION_ID, SUB_SYSTEM, GROUP_TAG_NAME, VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME)SELECT
    concat(locationTagName,'.',BAT_TAG_NAME) ,BAT_DESC,    LOCATION_ID, SUB_SYSTEM, GROUP_TAG_NAME, VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME 
    FROM dev_info,tmpGenerateBatchDevTbl WHERE TAG_NAME=devTagName AND concat(locationTagName,'.',BAT_TAG_NAME) <> devTagName;
   
    --判断是否生成对应的前置设备
    IF length(trim(fesRtuTagName)) THEN
    BEGIN
        --根据前置模板生成前置设备,后台设备模板的表结构修改,这里已经不能够同时批量生成前置设备
        SELECT '' INTO fesDevTempTagName FROM dev_info,dev_temp_def WHERE dev_info.TAG_NAME = devTagName AND dev_info.DEV_TP_NAME = dev_temp_def.TAG_NAME;
        IF length(trim(fesDevTempTagName)) = 0 THEN RETURN;
        END IF;
        
        --生成前置设备
        INSERT INTO fes_dev_info(
        TAG_NAME,                                      DEV_NAME,                   LOCATION_ID,             SUB_SYSTEM,             DEV_ID, DEV_TYPE_ID,                 RTU_TAG,      RTU_NO,             DEV_DESC, DEV_TP_NAME)SELECT
        concat(fesRtuTagName,'.', BAT_TAG_NAME,'_fes'),concat(BAT_TAG_NAME,'_fes'),fes_rtu_para.LOCATION_ID,fes_rtu_para.SUB_SYSTEM,-1,     fes_dev_temp_def.DEV_TYPE_ID,fesRtuTagName,fes_rtu_para.RTU_NO,BAT_DESC,fesDevTempTagName
        FROM fes_dev_temp_def,fes_rtu_para,tmpGenerateBatchDevTbl 
        WHERE fes_dev_temp_def.TAG_NAME = fesDevTempTagName 
        AND fes_rtu_para.TAG_NAME = fesRtuTagName;
        
        --关联前置,创建临时表
        CREATE TEMPORARY TABLE IF NOT EXISTS tmpFesTblAcc(
        KEY_ID_TAG varchar(128) NOT NULL,
        DOT_NO varchar(48) NOT NULL,
        OFFSET_NO decimal(10)
        );
        DELETE FROM tmpFesTblAcc;
        CREATE TEMPORARY TABLE IF NOT EXISTS tmpFesTblAna(
        KEY_ID_TAG varchar(128) NOT NULL,
        DOT_NO varchar(48) NOT NULL,
        OFFSET_NO decimal(10)
        );
        DELETE FROM tmpFesTblAna;
        CREATE TEMPORARY TABLE IF NOT EXISTS tmpFesTblDig(
        KEY_ID_TAG varchar(128) NOT NULL,
        DOT_NO varchar(48) NOT NULL,
        OFFSET_NO decimal(10)
        );
        DELETE FROM tmpFesTblDig;
        CREATE TEMPORARY TABLE IF NOT EXISTS tmpFesTblMix(
        KEY_ID_TAG varchar(128) NOT NULL,
        DOT_NO varchar(48) NOT NULL,
        OFFSET_NO decimal(10)
        );
        DELETE FROM tmpFesTblMix;
        --关联前置,这里使用while循环重新生成tagName
        stepNum = batchStart;
        WHILE stepNum < (batchStart + batchNum) LOOP
            tempStr = replace(formatTagName,'%1',stepNum);
            tmpDevTag = concat(locationTagName,'.',tempStr);
            tmpFesDevTag = concat(fesRtuTagName,'.', tempStr,'_fes');
            
			--第一次的时候,初始化临时表,批量下,每个设备一样,所以临时表只需要初始化一次就行
            IF stepNum = batchStart THEN 
            BEGIN 
                --acc
                INSERT INTO tmpFesTblAcc(KEY_ID_TAG , DOT_NO) 
                SELECT accuml_map_to_fes.KEY_ID_TAG, fes_accuml.DOT_NO FROM accuml_map_to_fes,accuml_temp_define,fes_dev_pt_temp_def,fes_accuml WHERE 
                getBeforeLastDot(getAfterFirstDot(accuml_map_to_fes.KEY_ID_TAG)) = tmpDevTag AND 
                accuml_map_to_fes.POINT_TP_NAME=accuml_temp_define.TAG_NAME AND 
                accuml_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.DEV_TP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_ACC') AND 
                fes_dev_pt_temp_def.SEQ_NO = accuml_temp_define.SEC_SEQ_NO AND 
                fes_accuml.DEV_TAG = tmpFesDevTag AND
                fes_accuml.FES_POINT_TP_NAME = fes_dev_pt_temp_def.TAG_NAME;
                --ana
                INSERT INTO tmpFesTblAna (KEY_ID_TAG , DOT_NO) 
                SELECT analog_map_to_fes.KEY_ID_TAG, fes_analog.DOT_NO FROM analog_map_to_fes,analog_temp_define,fes_dev_pt_temp_def,fes_analog WHERE 
                getBeforeLastDot(getAfterFirstDot(analog_map_to_fes.KEY_ID_TAG)) = tmpDevTag AND 
                analog_map_to_fes.POINT_TP_NAME=analog_temp_define.TAG_NAME AND 
                analog_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.DEV_TP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_ANA') AND 
                fes_dev_pt_temp_def.SEQ_NO = analog_temp_define.SEC_SEQ_NO AND 
                fes_analog.DEV_TAG = tmpFesDevTag AND
                fes_analog.FES_POINT_TP_NAME = fes_dev_pt_temp_def.TAG_NAME;
                --digital 五个分量
                INSERT INTO tmpFesTblDig (KEY_ID_TAG , DOT_NO, OFFSET_NO) 
                SELECT digital_map_to_fes.KEY_ID_TAG, fes_digital.DOT_NO, 1 FROM digital_map_to_fes,digital_temp_define,fes_dev_pt_temp_def,fes_digital WHERE 
                getBeforeLastDot(getAfterFirstDot(digital_map_to_fes.KEY_ID_TAG)) = tmpDevTag AND 
                digital_map_to_fes.POINT_TP_NAME=digital_temp_define.TAG_NAME AND 
                digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.DEV_TP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') AND 
                fes_dev_pt_temp_def.SEQ_NO = digital_temp_define.SEC_SEQ_NO1 AND 
                fes_digital.DEV_TAG = tmpFesDevTag AND
                fes_digital.FES_POINT_TP_NAME = fes_dev_pt_temp_def.TAG_NAME;
                
                INSERT INTO tmpFesTblDig (KEY_ID_TAG , DOT_NO, OFFSET_NO) 
                SELECT digital_map_to_fes.KEY_ID_TAG, fes_digital.DOT_NO, 2 FROM digital_map_to_fes,digital_temp_define,fes_dev_pt_temp_def,fes_digital WHERE 
                getBeforeLastDot(getAfterFirstDot(digital_map_to_fes.KEY_ID_TAG)) = tmpDevTag AND 
                digital_map_to_fes.POINT_TP_NAME=digital_temp_define.TAG_NAME AND 
                digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.DEV_TP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') AND 
                fes_dev_pt_temp_def.SEQ_NO = digital_temp_define.SEC_SEQ_NO2 AND 
                fes_digital.DEV_TAG = tmpFesDevTag AND
                fes_digital.FES_POINT_TP_NAME = fes_dev_pt_temp_def.TAG_NAME;
                
                INSERT INTO tmpFesTblDig (KEY_ID_TAG , DOT_NO, OFFSET_NO) 
                SELECT digital_map_to_fes.KEY_ID_TAG, fes_digital.DOT_NO, 3 FROM digital_map_to_fes,digital_temp_define,fes_dev_pt_temp_def,fes_digital WHERE 
                getBeforeLastDot(getAfterFirstDot(digital_map_to_fes.KEY_ID_TAG)) = tmpDevTag AND 
                digital_map_to_fes.POINT_TP_NAME=digital_temp_define.TAG_NAME AND 
                digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.DEV_TP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') AND 
                fes_dev_pt_temp_def.SEQ_NO = digital_temp_define.SEC_SEQ_NO3 AND 
                fes_digital.DEV_TAG = tmpFesDevTag AND
                fes_digital.FES_POINT_TP_NAME = fes_dev_pt_temp_def.TAG_NAME;
                
                INSERT INTO tmpFesTblDig (KEY_ID_TAG , DOT_NO, OFFSET_NO) 
                SELECT digital_map_to_fes.KEY_ID_TAG, fes_digital.DOT_NO, 4 FROM digital_map_to_fes,digital_temp_define,fes_dev_pt_temp_def,fes_digital WHERE 
                getBeforeLastDot(getAfterFirstDot(digital_map_to_fes.KEY_ID_TAG)) = tmpDevTag AND 
                digital_map_to_fes.POINT_TP_NAME=digital_temp_define.TAG_NAME AND 
                digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.DEV_TP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') AND 
                fes_dev_pt_temp_def.SEQ_NO = digital_temp_define.SEC_SEQ_NO4 AND 
                fes_digital.DEV_TAG = tmpFesDevTag AND
                fes_digital.FES_POINT_TP_NAME = fes_dev_pt_temp_def.TAG_NAME;
                
                INSERT INTO tmpFesTblDig (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
                SELECT digital_map_to_fes.KEY_ID_TAG, fes_digital.DOT_NO, 5 FROM digital_map_to_fes,digital_temp_define,fes_dev_pt_temp_def,fes_digital WHERE 
                getBeforeLastDot(getAfterFirstDot(digital_map_to_fes.KEY_ID_TAG)) = tmpDevTag AND 
                digital_map_to_fes.POINT_TP_NAME=digital_temp_define.TAG_NAME AND 
                digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.DEV_TP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') AND 
                fes_dev_pt_temp_def.SEQ_NO = digital_temp_define.SEC_SEQ_NO5 AND 
                fes_digital.DEV_TAG = tmpFesDevTag AND
                fes_digital.FES_POINT_TP_NAME = fes_dev_pt_temp_def.TAG_NAME;
                --mix
                INSERT INTO tmpFesTblMix (KEY_ID_TAG , DOT_NO) 
                SELECT mix_map_to_fes.KEY_ID_TAG, fes_mix.DOT_NO FROM mix_map_to_fes,mix_temp_define,fes_dev_pt_temp_def,fes_mix WHERE 
                getBeforeLastDot(getAfterFirstDot(mix_map_to_fes.KEY_ID_TAG)) = tmpDevTag AND 
                mix_map_to_fes.POINT_TP_NAME=mix_temp_define.TAG_NAME AND 
                mix_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.DEV_TP_NAME = fesDevTempTagName AND 
                fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_MIX') AND 
                fes_dev_pt_temp_def.SEQ_NO = mix_temp_define.SEC_SEQ_NO AND 
                fes_mix.DEV_TAG = tmpFesDevTag AND
                fes_mix.FES_POINT_TP_NAME = fes_dev_pt_temp_def.TAG_NAME;
                
                --获取各个临时表的尺寸
                SELECT count(*) INTO accSize FROM fes_accuml WHERE DEV_TAG = tmpFesDevTag;
                SELECT count(*) INTO anaSize FROM fes_analog WHERE DEV_TAG = tmpFesDevTag;
                SELECT count(*) INTO digSize FROM fes_digital WHERE DEV_TAG = tmpFesDevTag;
                SELECT count(*) INTO mixSize FROM fes_mix WHERE DEV_TAG = tmpFesDevTag;
                IF COALESCE(accSize,0) = 0 THEN 
                    accSize = 0;
                END IF;
                IF COALESCE(anaSize,0) = 0 THEN 
                    anaSize = 0;
                END IF;
                IF COALESCE(digSize,0) = 0 THEN 
                    digSize = 0;
                END IF;
                IF COALESCE(mixSize,0) = 0 THEN 
                    mixSize = 0;
                END IF;
            END; 
            END IF;
            
            --对临时表中的KEY_ID_TAG,DOT_NO直接替换字段
            IF stepNum <> batchStart THEN
            BEGIN
                UPDATE tmpFesTblAcc SET 
                KEY_ID_TAG = concat(getBeforeFirstDot(KEY_ID_TAG),'.',tmpDevTag,'.',getAfterLastDot(KEY_ID_TAG)),
                DOT_NO = ( DOT_NO + accSize)
                WHERE 1;
                
                UPDATE tmpFesTblAna SET KEY_ID_TAG = concat(getBeforeFirstDot(KEY_ID_TAG),'.',tmpDevTag,'.',getAfterLastDot(KEY_ID_TAG)),
                DOT_NO = ( DOT_NO + anaSize)
                WHERE 1;
                UPDATE tmpFesTblDig SET KEY_ID_TAG = concat(getBeforeFirstDot(KEY_ID_TAG),'.',tmpDevTag,'.',getAfterLastDot(KEY_ID_TAG)),
                DOT_NO = ( DOT_NO + digSize)
                WHERE 1;
                UPDATE tmpFesTblMix SET KEY_ID_TAG = concat(getBeforeFirstDot(KEY_ID_TAG),'.',tmpDevTag,'.',getAfterLastDot(KEY_ID_TAG)),
                DOT_NO = ( DOT_NO + mixSize)
                WHERE 1;
            END;
            END IF;
            --acc
            UPDATE accuml SET RTU_TAG = fesRtuTagName WHERE DEVICE = tmpDevTag;
            UPDATE accuml_map_to_fes 
            SET DOT_NO = tmpFesTblAcc.DOT_NO
			FROM tmpFesTblAcc 
            WHERE accuml_map_to_fes.KEY_ID_TAG = tmpFesTblAcc.KEY_ID_TAG;
            --ana
            UPDATE analog SET RTU_TAG = fesRtuTagName WHERE DEVICE = tmpDevTag;
            UPDATE analog_map_to_fes
            SET DOT_NO = tmpFesTblAna.DOT_NO
			FROM tmpFesTblAna 
            WHERE analog_map_to_fes.KEY_ID_TAG = tmpFesTblAna.KEY_ID_TAG;
            --digital
            UPDATE digital SET RTU_TAG = fesRtuTagName WHERE DEVICE = tmpDevTag;
    
            UPDATE digital_map_to_fes 
            SET DOT_NO1 = tmpFesTblDig.DOT_NO
			FROM tmpFesTblDig 
            WHERE digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG 
			AND tmpFesTblDig.OFFSET_NO = 1; 
            
            UPDATE digital_map_to_fes 
            SET DOT_NO2 = tmpFesTblDig.DOT_NO
			FROM tmpFesTblDig 
            WHERE digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG 
			AND tmpFesTblDig.OFFSET_NO = 2; 
           
            UPDATE digital_map_to_fes 
            SET DOT_NO3 = tmpFesTblDig.DOT_NO
			FROM tmpFesTblDig 
            WHERE digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG 
			AND tmpFesTblDig.OFFSET_NO = 3; 
           
            UPDATE digital_map_to_fes 
            SET DOT_NO4 = tmpFesTblDig.DOT_NO
			FROM tmpFesTblDig 
            WHERE digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG 
			AND tmpFesTblDig.OFFSET_NO = 4; 
            
            UPDATE digital_map_to_fes 
            SET DOT_NO5 = tmpFesTblDig.DOT_NO
			FROM tmpFesTblDig 
            WHERE digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG 
			AND tmpFesTblDig.OFFSET_NO = 5; 
            --mix
            UPDATE mix 
			SET RTU_TAG = fesRtuTagName 
			WHERE DEVICE = tmpDevTag;
			
            UPDATE mix_map_to_fes 
            SET DOT_NO = tmpFesTblMix.DOT_NO
			FROM tmpFesTblMix 
            WHERE mix_map_to_fes.KEY_ID_TAG = tmpFesTblMix.KEY_ID_TAG; 
            
            --每次循环结束加1
            stepNum = stepNum + 1;
            
        END LOOP;
        
        
    END; 
    END IF;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION generateFesDevTemp(devTagName varchar(64), fesDevTagName varchar(64))
RETURNS VOID
AS $$
BEGIN
    --生成与后台设备模板一样的前置模板的对话框
    --devTagName 后台设备模板名
    --fesDevTagName 前置设备模板名,该前置设备模板应已存在,且该设备下无任何点
    IF EXISTS (SELECT TAG_NAME FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME= fesDevTagName ) 
    THEN RETURN;
    END IF;
    
    --插入FES前置模板点
    INSERT INTO fes_dev_pt_temp_def(
    TAG_NAME,                                           POINT_TYPE,                  DEV_TP_NAME, SEQ_NO, FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION) SELECT   
    concat(fesDevTagName,'.',getAfterLastDot(TAG_NAME)),getFesPointType('FES_ACC'),  fesDevTagName,SEQ_NO,'0',      '0',     1,     0,      0,      0,       0,          0,   '',              0,           '',         DESCRIPTION                      
    FROM accuml_temp_define WHERE DEV_TP_NAME = devTagName; 
    
    INSERT INTO fes_dev_pt_temp_def(
    TAG_NAME,                                           POINT_TYPE,                  DEV_TP_NAME, SEQ_NO, FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION) SELECT   
    concat(fesDevTagName,'.',getAfterLastDot(TAG_NAME)),getFesPointType('FES_ANA'),  fesDevTagName,SEQ_NO,'0',      '0',     1,     0,      0,      0,       0,          0,   '',              0,           '',         DESCRIPTION                      
    FROM analog_temp_define WHERE DEV_TP_NAME = devTagName; 
    
    --对于dig要特殊处理,dig要分辨分量数
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpGenerateFesDevTempTbl(
        FES_SEQ_NO serial,
        DIG_TAG_NAME varchar(64) NOT NULL,
        HouTai_SEQ_NO decimal(10,0) NOT NULL,
        HouTai_OFF_SET decimal(10,0) NOT NULL,
        PRIMARY KEY(FES_SEQ_NO)
    );
    DELETE FROM tmpGenerateFesDevTempTbl;
   
    INSERT INTO tmpGenerateFesDevTempTbl(DIG_TAG_NAME,HouTai_SEQ_NO,HouTai_OFF_SET)
    SELECT TAG_NAME,SEQ_NO,1
    FROM digital_temp_define WHERE DEV_TP_NAME = devTagName AND 1 <= VALUE_NUM ORDER BY SEQ_NO;
    
    INSERT INTO tmpGenerateFesDevTempTbl(DIG_TAG_NAME,HouTai_SEQ_NO,HouTai_OFF_SET)
    SELECT TAG_NAME,SEQ_NO,2
    FROM digital_temp_define WHERE DEV_TP_NAME = devTagName AND 2 <= VALUE_NUM ORDER BY SEQ_NO;
    
    INSERT INTO tmpGenerateFesDevTempTbl(DIG_TAG_NAME,HouTai_SEQ_NO,HouTai_OFF_SET)
    SELECT TAG_NAME,SEQ_NO,3
    FROM digital_temp_define WHERE DEV_TP_NAME = devTagName AND 3 <= VALUE_NUM ORDER BY SEQ_NO;
    
    INSERT INTO tmpGenerateFesDevTempTbl(DIG_TAG_NAME,HouTai_SEQ_NO,HouTai_OFF_SET)
    SELECT TAG_NAME,SEQ_NO,4
    FROM digital_temp_define WHERE DEV_TP_NAME = devTagName AND 4 <= VALUE_NUM ORDER BY SEQ_NO;
    
    INSERT INTO tmpGenerateFesDevTempTbl(DIG_TAG_NAME,HouTai_SEQ_NO,HouTai_OFF_SET)
    SELECT TAG_NAME,SEQ_NO,5
    FROM digital_temp_define WHERE DEV_TP_NAME = devTagName AND 5 <= VALUE_NUM ORDER BY SEQ_NO;
    
    INSERT INTO fes_dev_pt_temp_def(
    TAG_NAME,                                                          POINT_TYPE,                  DEV_TP_NAME,  SEQ_NO,     FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION) SELECT   
    concat(fesDevTagName,'.',getAfterLastDot(TAG_NAME),HouTai_OFF_SET),getFesPointType('FES_DIG'),  fesDevTagName,FES_SEQ_NO, '0',      '0',     1,     0,      0,      0,       0,          0,   '',              0,           '',         concat(DESCRIPTION,HouTai_OFF_SET)                      
    FROM digital_temp_define,tmpGenerateFesDevTempTbl WHERE DEV_TP_NAME = devTagName AND TAG_NAME = DIG_TAG_NAME; 
    
    INSERT INTO fes_dev_pt_temp_def(
    TAG_NAME,                                           POINT_TYPE,                  DEV_TP_NAME, SEQ_NO, FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION) SELECT   
    concat(fesDevTagName,'.',getAfterLastDot(TAG_NAME)),getFesPointType('FES_MIX'),  fesDevTagName,SEQ_NO,'0',      '0',     1,     0,      0,      0,       0,          0,   '',              0,           '',         DESCRIPTION                      
    FROM mix_temp_define WHERE DEV_TP_NAME = devTagName; 
    
    INSERT INTO fes_dev_pt_temp_def(
    TAG_NAME,                                                   POINT_TYPE,                      DEV_TP_NAME,  SEQ_NO, FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION) SELECT   
    concat(fesDevTagName,'.',getAfterLastDot(TAG_NAME),'_ctrl'),getFesPointType('FES_ANA_CTRL'), fesDevTagName,SEQ_NO,  '0',      '0',     1,     0,      0,      0,       0,          0,   '',              0,           '',         DESCRIPTION                      
    FROM analog_temp_define WHERE DEV_TP_NAME = devTagName AND COALESCE(IS_CONTROL,0)<>0; 
    
    INSERT INTO fes_dev_pt_temp_def(
    TAG_NAME,                                                   POINT_TYPE,                      DEV_TP_NAME,  SEQ_NO, FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION) SELECT   
    concat(fesDevTagName,'.',getAfterLastDot(TAG_NAME),'_ctrl'),getFesPointType('FES_DIG_CTRL'), fesDevTagName,SEQ_NO,  '0',      '0',     1,     0,      0,      0,       0,          0,   '',              0,           '',         DESCRIPTION                      
    FROM digital_temp_define WHERE DEV_TP_NAME = devTagName AND COALESCE(IS_CONTROL,0)<>0; 
    
    INSERT INTO fes_dev_pt_temp_def(
    TAG_NAME,                                                   POINT_TYPE,                      DEV_TP_NAME,  SEQ_NO, FUN_CODE, INFO_NO, RATIO, REVISE, APPEND, ASS_DIG, CTRL_TYPE, ATTR, CONST_CFG_FILE, COEFFICIENT, SHORT_NAME, DESCRIPTION) SELECT   
    concat(fesDevTagName,'.',getAfterLastDot(TAG_NAME),'_ctrl'),getFesPointType('FES_MIX_CTRL'), fesDevTagName,SEQ_NO,  '0',      '0',     1,     0,      0,      0,       0,          0,   '',              0,           '',         DESCRIPTION                      
    FROM mix_temp_define WHERE DEV_TP_NAME = devTagName AND COALESCE(IS_CONTROL,0)<>0; 
    
    --关联对应前置模板点,只能关联acc,ana,dig,mix,控制类的没法关联
    UPDATE dev_temp_def SET
    SEC_DEVTP_NAME = fesDevTagName
    WHERE TAG_NAME = devTagName;
    
    --acc,ana,mix点序号后台和前置里一一对应
    UPDATE accuml_temp_define SET
    SEC_SEQ_NO = SEQ_NO
    WHERE DEV_TP_NAME = devTagName;
    
    UPDATE analog_temp_define SET
    SEC_SEQ_NO = SEQ_NO
    WHERE DEV_TP_NAME = devTagName;
    
    UPDATE mix_temp_define SET
    SEC_SEQ_NO = SEQ_NO
    WHERE DEV_TP_NAME = devTagName;
    
    --dig点序号和分量数有关
    --先全部清空
    UPDATE digital_temp_define SET
    SEC_SEQ_NO1 = -1,
    SEC_SEQ_NO2 = -1,
    SEC_SEQ_NO3 = -1,
    SEC_SEQ_NO4 = -1,
    SEC_SEQ_NO5 = -1
    WHERE DEV_TP_NAME = devTagName;
    
    UPDATE digital_temp_define 
    SET SEC_SEQ_NO1 = FES_SEQ_NO
	FROM tmpGenerateFesDevTempTbl 
    WHERE TAG_NAME = DIG_TAG_NAME 
	AND DEV_TP_NAME = devTagName 
	AND HouTai_OFF_SET = 1;
    
    UPDATE digital_temp_define 
    SET SEC_SEQ_NO2 = FES_SEQ_NO
	FROM tmpGenerateFesDevTempTbl 
    WHERE TAG_NAME = DIG_TAG_NAME 
	AND DEV_TP_NAME = devTagName 
	AND HouTai_OFF_SET = 2;
    
    UPDATE digital_temp_define 
    SET SEC_SEQ_NO3 = FES_SEQ_NO
	FROM tmpGenerateFesDevTempTbl 
    WHERE TAG_NAME = DIG_TAG_NAME 
	AND DEV_TP_NAME = devTagName 
	AND HouTai_OFF_SET = 3;
    
    UPDATE digital_temp_define 
    SET SEC_SEQ_NO4 = FES_SEQ_NO
	FROM tmpGenerateFesDevTempTbl 
    WHERE TAG_NAME = DIG_TAG_NAME 
	AND DEV_TP_NAME = devTagName 
	AND HouTai_OFF_SET = 4;
    
    UPDATE digital_temp_define 
    SET SEC_SEQ_NO5 = FES_SEQ_NO
	FROM tmpGenerateFesDevTempTbl 
    WHERE TAG_NAME = DIG_TAG_NAME 
	AND DEV_TP_NAME = devTagName 
	AND HouTai_OFF_SET = 5;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION importbgxfes(openTriggerInfo decimal(10,0), importFesPointDesc decimal(10,0), fesDescToBG decimal(10,0) )
RETURNS VOID
AS $$
BEGIN
    --从background_x_fes表导入数据
    --openTriggerInfo = 1 开启同步到实时库的功能,此操作十分耗时,建议关闭
    --importFesPointDesc = 1导入FES点描述,此值只对非默认设备起作用, 对于默认设备点,总是自动导入描述
    --fesDescToBG = 1将把FES的描述导入后台
    
    --同步实时库的功能关闭
    IF openTriggerInfo <> 1 THEN 
    UPDATE sys_trigger_flag SET FLAG_ID = 0 WHERE FLAG_ID = 1;
    END IF;
    --预处理阶段
    --首先把background_x_fes表中提到的后台点与前置的映射关系取消掉,这里会触发触发器,会比较耗时,去优化触发器
    update accuml
    SET RTU_TAG = ''
    from background_x_fes_accuml
    WHERE concat('accuml','.',accuml.TAG_NAME) = background_x_fes_accuml.BG_POINT_TAG;

    
    update analog
    SET RTU_TAG = ''
    from background_x_fes_analog
    WHERE concat('analog','.',analog.TAG_NAME) = background_x_fes_analog.BG_POINT_TAG;

    
    update digital
    SET RTU_TAG = ''
    from background_x_fes_digtal
    WHERE concat('digital','.',digital.TAG_NAME) = background_x_fes_digtal.BG_POINT_TAG;

    
    update mix
    SET RTU_TAG = ''
    from background_x_fes_mix
    WHERE concat('mix','.',mix.TAG_NAME) = background_x_fes_mix.BG_POINT_TAG;

    
    --update完毕,会驱动触发器,更新对应map_to_fes和Control表中的RTU  
    
    --预处理, acc,ana,dig,mix 中后台与前置的关联有两种方法确定前置点,分别是1:RTU_TAG和RTU_DOT_NO可以确定一个点
    --2:通过RTU_DEV_TAG去前置模板和后台模板寻找实例的对应点,再写入对应的RTU_tag和RTU_DOT_NO
    --最终都通过RTU_TAG和RTU_DOT_NO确定前置点
    --不符合以上两点的记录删除 
    --ana_ctrl,dig_ctrl,mix_ctrl只能用一种方法确定,即rtu_tag, RTU_DOT_NO,RTU_DEV_TAG都需要填写,  rtu_tag, RTU_DOT_NO,RTU_DEV_TAG都对得上的使用实例的设备,否则使用默认设备
    --首先,对于acc,ana,dig,mix,把RTU_TAG或者RTU_DOT_NO为空, RTU_DEV_TAG不为空的记录,去模板找对应关系,填入RTU_TAG和RTU_DOT_NO信息
	
    --acc
    UPDATE background_x_fes_accuml
    SET RTU_TAG = fes_accuml.RTU_TAG,
    RTU_DOT_NO = fes_accuml.DOT_NO
	FROM accuml_map_to_fes,accuml_temp_define,fes_accuml
    WHERE COALESCE(background_x_fes_accuml.RTU_DEV_TAG,'')<>'' 
	AND (COALESCE(background_x_fes_accuml.RTU_TAG,'')='' OR COALESCE(background_x_fes_accuml.RTU_DOT_NO,'')='')
    AND background_x_fes_accuml.BG_POINT_TAG = accuml_map_to_fes.KEY_ID_TAG 
    AND accuml_map_to_fes.POINT_TP_NAME = accuml_temp_define.TAG_NAME
    AND fes_accuml.DEV_TAG = background_x_fes_accuml.RTU_DEV_TAG 
	AND fes_accuml.SEQ_NO = accuml_temp_define.SEC_SEQ_NO;
	
    --ana
    UPDATE background_x_fes_analog
    SET RTU_TAG = fes_analog.RTU_TAG,
    RTU_DOT_NO = fes_analog.DOT_NO
    FROM analog_map_to_fes, analog_temp_define,fes_analog
    WHERE COALESCE(background_x_fes_analog.RTU_DEV_TAG,'')<>'' 
	AND (COALESCE(background_x_fes_analog.RTU_TAG,'')='' OR COALESCE(background_x_fes_analog.RTU_DOT_NO,'')='')
    AND background_x_fes_analog.BG_POINT_TAG = analog_map_to_fes.KEY_ID_TAG 
    AND analog_map_to_fes.POINT_TP_NAME = analog_temp_define.TAG_NAME
    AND fes_analog.DEV_TAG = background_x_fes_analog.RTU_DEV_TAG 
	AND fes_analog.SEQ_NO = analog_temp_define.SEC_SEQ_NO;
	
    --dig的5个分量,需要判断是哪个分量,即判断background_x_fes_digtal.OFFSET字段,可枚举该字段的值,只有5个,1,2,3,4,5
    UPDATE background_x_fes_digtal
    SET RTU_TAG = fes_digital.RTU_TAG,
    RTU_DOT_NO = fes_digital.DOT_NO
    FROM digital_map_to_fes,digital_temp_define,fes_digital 
    WHERE COALESCE(background_x_fes_digtal.RTU_DEV_TAG,'')<>'' 
	AND (COALESCE(background_x_fes_digtal.RTU_TAG,'')='' OR COALESCE(background_x_fes_digtal.RTU_DOT_NO,'')='')
    AND background_x_fes_digtal.BG_POINT_OFFSET = 1 
	AND background_x_fes_digtal.BG_POINT_TAG = digital_map_to_fes.KEY_ID_TAG 
    AND digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    AND fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG 
	AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO1;
    
    UPDATE background_x_fes_digtal
    SET RTU_TAG = fes_digital.RTU_TAG,
    RTU_DOT_NO = fes_digital.DOT_NO
    FROM digital_map_to_fes,digital_temp_define,fes_digital
    WHERE COALESCE(background_x_fes_digtal.RTU_DEV_TAG,'')<>'' 
	AND (COALESCE(background_x_fes_digtal.RTU_TAG,'')='' OR COALESCE(background_x_fes_digtal.RTU_DOT_NO,'')='')
    AND background_x_fes_digtal.BG_POINT_OFFSET = 2 
	AND background_x_fes_digtal.BG_POINT_TAG = digital_map_to_fes.KEY_ID_TAG 
    AND digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    AND fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG 
	AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO2;
    
    UPDATE background_x_fes_digtal
    SET RTU_TAG = fes_digital.RTU_TAG,
    RTU_DOT_NO = fes_digital.DOT_NO
	FROM digital_map_to_fes,digital_temp_define,fes_digital
    WHERE COALESCE(background_x_fes_digtal.RTU_DEV_TAG,'')<>'' 
	AND (COALESCE(background_x_fes_digtal.RTU_TAG,'')='' OR COALESCE(background_x_fes_digtal.RTU_DOT_NO,'')='')
    AND background_x_fes_digtal.BG_POINT_OFFSET = 3 
	AND background_x_fes_digtal.BG_POINT_TAG = digital_map_to_fes.KEY_ID_TAG 
    AND digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    AND fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG 
	AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO3;
    
    UPDATE background_x_fes_digtal
    SET RTU_TAG = fes_digital.RTU_TAG,
    RTU_DOT_NO = fes_digital.DOT_NO
	FROM digital_map_to_fes,background_x_fes_digtal,fes_digital
    WHERE COALESCE(background_x_fes_digtal.RTU_DEV_TAG,'')<>'' 
	AND (COALESCE(background_x_fes_digtal.RTU_TAG,'')='' OR COALESCE(background_x_fes_digtal.RTU_DOT_NO,'')='')
    AND background_x_fes_digtal.BG_POINT_OFFSET = 4 
	AND background_x_fes_digtal.BG_POINT_TAG = digital_map_to_fes.KEY_ID_TAG 
    AND digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    AND fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG 
	AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO4;
    
    UPDATE background_x_fes_digtal
    SET RTU_TAG = fes_digital.RTU_TAG,
    RTU_DOT_NO = fes_digital.DOT_NO
	FROM digital_map_to_fes,digital_temp_define,fes_digital
    WHERE COALESCE(background_x_fes_digtal.RTU_DEV_TAG,'')<>'' 
	AND (COALESCE(background_x_fes_digtal.RTU_TAG,'')='' OR COALESCE(background_x_fes_digtal.RTU_DOT_NO,'')='')
    AND background_x_fes_digtal.BG_POINT_OFFSET = 5 
	AND background_x_fes_digtal.BG_POINT_TAG = digital_map_to_fes.KEY_ID_TAG 
    AND digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    AND fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG 
	AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO5;
	
    --mix
    UPDATE background_x_fes_mix
    SET RTU_TAG = fes_mix.RTU_TAG,
    RTU_DOT_NO = fes_mix.DOT_NO
	FROM mix_map_to_fes,mix_temp_define,fes_mix
    WHERE COALESCE(background_x_fes_mix.RTU_DEV_TAG,'')<>'' 
	AND (COALESCE(background_x_fes_mix.RTU_TAG,'')='' OR COALESCE(background_x_fes_mix.RTU_DOT_NO,'')='')
    AND background_x_fes_mix.BG_POINT_TAG = mix_map_to_fes.KEY_ID_TAG 
    AND mix_map_to_fes.POINT_TP_NAME = mix_temp_define.TAG_NAME
    AND fes_mix.DEV_TAG = background_x_fes_mix.RTU_DEV_TAG 
	AND fes_mix.SEQ_NO = mix_temp_define.SEC_SEQ_NO;
        
    --删除RTU_TAG和DOT_NO仍为空的
    DELETE FROM background_x_fes_accuml WHERE COALESCE(RTU_TAG,'') = '' OR COALESCE(RTU_DOT_NO,'') =''  ;
    DELETE FROM background_x_fes_analog WHERE COALESCE(RTU_TAG,'') = '' OR COALESCE(RTU_DOT_NO,'') =''  ;
    DELETE FROM background_x_fes_digtal WHERE COALESCE(RTU_TAG,'') = '' OR COALESCE(RTU_DOT_NO,'') =''  ;
    DELETE FROM background_x_fes_mix WHERE COALESCE(RTU_TAG,'') = '' OR COALESCE(RTU_DOT_NO,'') ='' ;
    
    DELETE FROM background_x_fes_ana_ctrl WHERE COALESCE(RTU_TAG,'') = '' OR COALESCE(RTU_DOT_NO,'') ='' ;
    DELETE FROM background_x_fes_dig_ctrl WHERE COALESCE(RTU_TAG,'') = '' OR COALESCE(RTU_DOT_NO,'') ='' ;
    DELETE FROM background_x_fes_mix_ctrl WHERE COALESCE(RTU_TAG,'') = '' OR COALESCE(RTU_DOT_NO,'') ='' ;
        
    --再次更新,对于RTU_TAG,DOT_NO和RTU_DEV_TAG对不上的点归类到默认设备
    UPDATE background_x_fes_accuml 
    SET RTU_DEV_TAG = concat(background_x_fes_accuml.RTU_TAG, '.', 'defaultDev')
    WHERE NOT EXISTS 
    (SELECT TAG_NAME FROM fes_accuml 
    WHERE fes_accuml.TAG_NAME =concat(background_x_fes_accuml.RTU_TAG,'.',background_x_fes_accuml.RTU_DOT_NO)
    AND fes_accuml.DEV_TAG = background_x_fes_accuml.RTU_DEV_TAG
    );
    
    UPDATE background_x_fes_analog 
    SET RTU_DEV_TAG = concat(background_x_fes_analog.RTU_TAG, '.', 'defaultDev')
    WHERE NOT EXISTS 
    (SELECT TAG_NAME FROM fes_analog 
    WHERE fes_analog.TAG_NAME =concat(background_x_fes_analog.RTU_TAG,'.',background_x_fes_analog.RTU_DOT_NO)
    AND fes_analog.DEV_TAG = background_x_fes_analog.RTU_DEV_TAG
    );
    
    UPDATE background_x_fes_digtal 
    SET RTU_DEV_TAG = concat(background_x_fes_digtal.RTU_TAG, '.', 'defaultDev')
    WHERE NOT EXISTS 
    (SELECT TAG_NAME FROM fes_digital 
    WHERE fes_digital.TAG_NAME =concat(background_x_fes_digtal.RTU_TAG,'.',background_x_fes_digtal.RTU_DOT_NO)
    AND fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG
    );
    
    UPDATE background_x_fes_mix 
    SET RTU_DEV_TAG = concat(background_x_fes_mix.RTU_TAG, '.', 'defaultDev')
    WHERE NOT EXISTS 
    (SELECT TAG_NAME FROM fes_mix 
    WHERE fes_mix.TAG_NAME =concat(background_x_fes_mix.RTU_TAG,'.',background_x_fes_mix.RTU_DOT_NO)
    AND fes_mix.DEV_TAG = background_x_fes_mix.RTU_DEV_TAG
    );
    
    UPDATE background_x_fes_ana_ctrl
    SET RTU_DEV_TAG = concat(background_x_fes_ana_ctrl.RTU_TAG, '.', 'defaultDev')
    WHERE NOT EXISTS 
    (SELECT TAG_NAME FROM fes_analog_ctrl 
    WHERE fes_analog_ctrl.TAG_NAME =concat(background_x_fes_ana_ctrl.RTU_TAG,'.',background_x_fes_ana_ctrl.RTU_DOT_NO)
    AND fes_analog_ctrl.DEV_TAG = background_x_fes_ana_ctrl.RTU_DEV_TAG
    );
    
    UPDATE background_x_fes_dig_ctrl
    SET RTU_DEV_TAG = concat(background_x_fes_dig_ctrl.RTU_TAG, '.', 'defaultDev')
    WHERE NOT EXISTS 
    (SELECT TAG_NAME FROM fes_digital_ctrl 
    WHERE fes_digital_ctrl.TAG_NAME =concat(background_x_fes_dig_ctrl.RTU_TAG,'.',background_x_fes_dig_ctrl.RTU_DOT_NO)
    AND fes_digital_ctrl.DEV_TAG = background_x_fes_dig_ctrl.RTU_DEV_TAG
    );
    
    UPDATE background_x_fes_mix_ctrl
    SET RTU_DEV_TAG = concat(background_x_fes_mix_ctrl.RTU_TAG, '.', 'defaultDev')
    WHERE NOT EXISTS 
    (SELECT TAG_NAME FROM fes_mix_ctrl 
    WHERE fes_mix_ctrl.TAG_NAME =concat(background_x_fes_mix_ctrl.RTU_TAG,'.',background_x_fes_mix_ctrl.RTU_DOT_NO)
    AND fes_mix_ctrl.DEV_TAG = background_x_fes_mix_ctrl.RTU_DEV_TAG
    );
    --预处理完毕,此时background_x_fes表中的字段情况有以下几种情况.
    --1:RTU_TAG,DOT_NO,RTU_DEV_TAG都对得上的(即对应表上有的),这些事前置实例设备中的点
    --2:RTU_TAG,DOT_NO与RTU_DEV_TAG可能对不上,这类点归类到默认设备中
    
    --先删除默认设备点
    DELETE FROM fes_accuml 
    USING fes_dev_info,background_x_fes_accuml 
    WHERE fes_accuml.DEV_TAG = fes_dev_info.TAG_NAME 
	AND fes_dev_info.TAG_NAME = concat(fes_accuml.RTU_TAG,'.', 'defaultDev')
    AND fes_accuml.RTU_TAG = background_x_fes_accuml.RTU_TAG;
    
    DELETE FROM fes_analog 
    USING fes_dev_info,background_x_fes_analog 
    WHERE fes_analog.DEV_TAG = fes_dev_info.TAG_NAME 
	AND fes_dev_info.TAG_NAME = concat(fes_analog.RTU_TAG,'.', 'defaultDev')
    AND fes_analog.RTU_TAG = background_x_fes_analog.RTU_TAG;
    
    DELETE FROM fes_digital 
    USING fes_dev_info,background_x_fes_digtal
    WHERE fes_digital.DEV_TAG = fes_dev_info.TAG_NAME 
	AND fes_dev_info.TAG_NAME = concat(fes_digital.RTU_TAG,'.', 'defaultDev')
    AND fes_digital.RTU_TAG = background_x_fes_digtal.RTU_TAG;
	
    DELETE FROM fes_mix 
    USING fes_dev_info,background_x_fes_mix 
    WHERE fes_mix.DEV_TAG = fes_dev_info.TAG_NAME 
	AND fes_dev_info.TAG_NAME = concat(fes_mix.RTU_TAG,'.', 'defaultDev')
    AND fes_mix.RTU_TAG = background_x_fes_mix.RTU_TAG;
    
    DELETE FROM fes_analog_ctrl 
    USING fes_dev_info,background_x_fes_ana_ctrl 
    WHERE fes_analog_ctrl.DEV_TAG = fes_dev_info.TAG_NAME 
	AND fes_dev_info.TAG_NAME = concat(fes_analog_ctrl.RTU_TAG,'.', 'defaultDev')
    AND fes_analog_ctrl.RTU_TAG = background_x_fes_ana_ctrl.RTU_TAG;
    
    DELETE FROM fes_digital_ctrl 
    USING fes_dev_info,background_x_fes_dig_ctrl 
    WHERE fes_digital_ctrl.DEV_TAG = fes_dev_info.TAG_NAME 
	AND fes_dev_info.TAG_NAME = concat(fes_digital_ctrl.RTU_TAG,'.', 'defaultDev')
    AND fes_digital_ctrl.RTU_TAG = background_x_fes_dig_ctrl.RTU_TAG;
    
    DELETE FROM fes_mix_ctrl 
    USING fes_dev_info,background_x_fes_mix_ctrl 
    WHERE fes_mix_ctrl.DEV_TAG = fes_dev_info.TAG_NAME 
	AND fes_dev_info.TAG_NAME = concat(fes_mix_ctrl.RTU_TAG,'.', 'defaultDev')
    AND fes_mix_ctrl.RTU_TAG = background_x_fes_mix_ctrl.RTU_TAG;
        
    --对于非默认设备将所有FES与后台的联系清除
    update fes_accuml
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='', RES_PARA_INT1=RTU_RES_PARA_INT1,RES_PARA_INT2=RTU_RES_PARA_INT2,RES_PARA_INT3=RTU_RES_PARA_INT3,RES_PARA_INT4=RTU_RES_PARA_INT4
    from background_x_fes_accuml
    where fes_accuml.TAG_NAME = concat(background_x_fes_accuml.RTU_TAG,'.',background_x_fes_accuml.RTU_DOT_NO);

    
    update fes_analog
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='', RES_PARA_INT1=RTU_RES_PARA_INT1,RES_PARA_INT2=RTU_RES_PARA_INT2,RES_PARA_INT3=RTU_RES_PARA_INT3,RES_PARA_INT4=RTU_RES_PARA_INT4
    from background_x_fes_analog
    where fes_analog.TAG_NAME = concat(background_x_fes_analog.RTU_TAG,'.',background_x_fes_analog.RTU_DOT_NO);

    
    update fes_digital
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='', RES_PARA_INT1=RTU_RES_PARA_INT1,RES_PARA_INT2=RTU_RES_PARA_INT2,RES_PARA_INT3=RTU_RES_PARA_INT3,RES_PARA_INT4=RTU_RES_PARA_INT4
    from background_x_fes_digtal
    where fes_digital.TAG_NAME = concat(background_x_fes_digtal.RTU_TAG,'.',background_x_fes_digtal.RTU_DOT_NO);

    
    update fes_mix
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='', RES_PARA_INT1=RTU_RES_PARA_INT1,RES_PARA_INT2=RTU_RES_PARA_INT2,RES_PARA_INT3=RTU_RES_PARA_INT3,RES_PARA_INT4=RTU_RES_PARA_INT4
    from background_x_fes_mix
    where fes_mix.TAG_NAME = concat(background_x_fes_mix.RTU_TAG,'.',background_x_fes_mix.RTU_DOT_NO);

    
    update fes_analog_ctrl
    SET RES_PARA_INT1=RTU_RES_PARA_INT1,RES_PARA_INT2=RTU_RES_PARA_INT2,RES_PARA_INT3=RTU_RES_PARA_INT3,RES_PARA_INT4=RTU_RES_PARA_INT4
    from background_x_fes_ana_ctrl
    where fes_analog_ctrl.TAG_NAME = concat(background_x_fes_ana_ctrl.RTU_TAG,'.',background_x_fes_ana_ctrl.RTU_DOT_NO);

    
    update fes_digital_ctrl
    SET RES_PARA_INT1=RTU_RES_PARA_INT1,RES_PARA_INT2=RTU_RES_PARA_INT2,RES_PARA_INT3=RTU_RES_PARA_INT3,RES_PARA_INT4=RTU_RES_PARA_INT4
    from background_x_fes_dig_ctrl
    where fes_digital_ctrl.TAG_NAME = concat(background_x_fes_dig_ctrl.RTU_TAG,'.',background_x_fes_dig_ctrl.RTU_DOT_NO);

    
    update fes_mix_ctrl
    SET RES_PARA_INT1=RTU_RES_PARA_INT1,RES_PARA_INT2=RTU_RES_PARA_INT2,RES_PARA_INT3=RTU_RES_PARA_INT3,RES_PARA_INT4=RTU_RES_PARA_INT4
    from background_x_fes_mix_ctrl
    where fes_mix_ctrl.TAG_NAME = concat(background_x_fes_mix_ctrl.RTU_TAG,'.',background_x_fes_mix_ctrl.RTU_DOT_NO);

    --ana_ctrl,dig_ctrl,mix_ctrl不用清除
    
    --重新生成默认设备
    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) SELECT 
    concat(background_x_fes_accuml.RTU_TAG,'.',background_x_fes_accuml.RTU_DOT_NO), fes_dev_info.LOCATION_ID, fes_dev_info.SUB_SYSTEM, background_x_fes_accuml.RTU_DOT_NO, background_x_fes_accuml.RTU_TAG, fes_dev_info.TAG_NAME,background_x_fes_accuml.RTU_DOT_NO, '',             '',           '',            '0',  '1',    fes_rtu_para.RTU_NO, background_x_fes_accuml.RTU_RES_PARA_INT1, background_x_fes_accuml.RTU_RES_PARA_INT2, background_x_fes_accuml.RTU_RES_PARA_INT3,  background_x_fes_accuml.RTU_RES_PARA_INT4,  background_x_fes_accuml.RTU_DOT_DESC,  ''
    FROM background_x_fes_accuml , fes_rtu_para ,fes_dev_info  WHERE  
    background_x_fes_accuml.RTU_DEV_TAG =concat(background_x_fes_accuml.RTU_TAG, '.', 'defaultDev' ) 
    AND fes_rtu_para.TAG_NAME=background_x_fes_accuml.RTU_TAG 
    AND fes_dev_info.TAG_NAME = background_x_fes_accuml.RTU_DEV_TAG  
    AND fes_dev_info.RTU_TAG = background_x_fes_accuml.RTU_TAG;
    
    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) SELECT 
    concat(background_x_fes_analog.RTU_TAG,'.',background_x_fes_analog.RTU_DOT_NO), fes_dev_info.LOCATION_ID, fes_dev_info.SUB_SYSTEM, fes_dev_info.TAG_NAME, background_x_fes_analog.RTU_DOT_NO,background_x_fes_analog.RTU_TAG,fes_rtu_para.RTU_NO, background_x_fes_analog.RTU_DOT_NO, '',           '',         '',            '0',       '0',     '0',           '0',       '0',     '0',  '1',   '65536',   '0',        background_x_fes_analog.RTU_RES_PARA_INT1, background_x_fes_analog.RTU_RES_PARA_INT2, background_x_fes_analog.RTU_RES_PARA_INT3,  background_x_fes_analog.RTU_RES_PARA_INT4,  background_x_fes_analog.RTU_DOT_DESC,  ''
    FROM background_x_fes_analog , fes_rtu_para ,fes_dev_info  WHERE  
    background_x_fes_analog.RTU_DEV_TAG =concat(background_x_fes_analog.RTU_TAG, '.', 'defaultDev' ) 
    AND fes_rtu_para.TAG_NAME=background_x_fes_analog.RTU_TAG 
    AND fes_dev_info.TAG_NAME = background_x_fes_analog.RTU_DEV_TAG  
    AND fes_dev_info.RTU_TAG = background_x_fes_analog.RTU_TAG;
    
    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) SELECT 
    concat(background_x_fes_digtal.RTU_TAG,'.',background_x_fes_digtal.RTU_DOT_NO), fes_dev_info.LOCATION_ID, fes_dev_info.SUB_SYSTEM, fes_dev_info.TAG_NAME, background_x_fes_digtal.RTU_DOT_NO,background_x_fes_digtal.RTU_TAG,background_x_fes_digtal.RTU_DOT_NO, '',           '',         '',            '1',       '1',             '0',           '0',    fes_rtu_para.RTU_NO,  background_x_fes_digtal.RTU_RES_PARA_INT1, background_x_fes_digtal.RTU_RES_PARA_INT2, background_x_fes_digtal.RTU_RES_PARA_INT3,  background_x_fes_digtal.RTU_RES_PARA_INT4,  background_x_fes_digtal.RTU_DOT_DESC,  ''
    FROM background_x_fes_digtal , fes_rtu_para ,fes_dev_info  WHERE  
    background_x_fes_digtal.RTU_DEV_TAG =concat(background_x_fes_digtal.RTU_TAG, '.', 'defaultDev' ) 
    AND fes_rtu_para.TAG_NAME=background_x_fes_digtal.RTU_TAG 
    AND fes_dev_info.TAG_NAME = background_x_fes_digtal.RTU_DEV_TAG  
    AND fes_dev_info.RTU_TAG = background_x_fes_digtal.RTU_TAG;
    
    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) SELECT 
    concat(background_x_fes_mix.RTU_TAG,'.',background_x_fes_mix.RTU_DOT_NO), background_x_fes_mix.RTU_DOT_DESC,  fes_dev_info.LOCATION_ID, fes_dev_info.SUB_SYSTEM,  background_x_fes_mix.RTU_TAG,fes_dev_info.RTU_NO,background_x_fes_mix.RTU_DOT_NO, fes_dev_info.TAG_NAME, background_x_fes_mix.RTU_DOT_NO,'',             '',           '',            '0',  '1',   '65536',   '0',        background_x_fes_mix.RTU_RES_PARA_INT1, background_x_fes_mix.RTU_RES_PARA_INT2, background_x_fes_mix.RTU_RES_PARA_INT3, background_x_fes_mix.RTU_RES_PARA_INT4, ''
    FROM background_x_fes_mix , fes_rtu_para ,fes_dev_info  WHERE  
    background_x_fes_mix.RTU_DEV_TAG =concat(background_x_fes_mix.RTU_TAG, '.', 'defaultDev' ) 
    AND fes_rtu_para.TAG_NAME=background_x_fes_mix.RTU_TAG 
    AND fes_dev_info.TAG_NAME = background_x_fes_mix.RTU_DEV_TAG  
    AND fes_dev_info.RTU_TAG = background_x_fes_mix.RTU_TAG;
    
    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) SELECT 
    concat(background_x_fes_ana_ctrl.RTU_TAG,'.',background_x_fes_ana_ctrl.RTU_DOT_NO), fes_dev_info.LOCATION_ID, fes_dev_info.SUB_SYSTEM, '0',  '1',   '65536',   '0',       fes_rtu_para.RTU_NO,fes_dev_info.TAG_NAME,background_x_fes_ana_ctrl.RTU_DOT_NO, background_x_fes_ana_ctrl.RTU_RES_PARA_INT1,background_x_fes_ana_ctrl.RTU_RES_PARA_INT2,background_x_fes_ana_ctrl.RTU_RES_PARA_INT3,background_x_fes_ana_ctrl.RTU_RES_PARA_INT4,background_x_fes_ana_ctrl.RTU_DOT_NO,background_x_fes_ana_ctrl.RTU_TAG,background_x_fes_ana_ctrl.RTU_DOT_DESC,''
    FROM background_x_fes_ana_ctrl , fes_rtu_para ,fes_dev_info  WHERE  
    background_x_fes_ana_ctrl.RTU_DEV_TAG =concat(background_x_fes_ana_ctrl.RTU_TAG, '.', 'defaultDev' ) 
    AND fes_rtu_para.TAG_NAME=background_x_fes_ana_ctrl.RTU_TAG 
    AND fes_dev_info.TAG_NAME = background_x_fes_ana_ctrl.RTU_DEV_TAG  
    AND fes_dev_info.RTU_TAG = background_x_fes_ana_ctrl.RTU_TAG;
    
    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) SELECT 
    concat(background_x_fes_dig_ctrl.RTU_TAG,'.',background_x_fes_dig_ctrl.RTU_DOT_NO), fes_dev_info.LOCATION_ID, fes_dev_info.SUB_SYSTEM, background_x_fes_dig_ctrl.RTU_TAG,fes_rtu_para.RTU_NO, background_x_fes_dig_ctrl.RTU_DOT_NO,fes_dev_info.TAG_NAME,background_x_fes_dig_ctrl.RTU_DOT_DESC,'0',    '',           background_x_fes_dig_ctrl.RTU_DOT_NO,background_x_fes_dig_ctrl.RTU_RES_PARA_INT1,background_x_fes_dig_ctrl.RTU_RES_PARA_INT2,background_x_fes_dig_ctrl.RTU_RES_PARA_INT3,background_x_fes_dig_ctrl.RTU_RES_PARA_INT4
    FROM background_x_fes_dig_ctrl , fes_rtu_para ,fes_dev_info  WHERE  
    background_x_fes_dig_ctrl.RTU_DEV_TAG =concat(background_x_fes_dig_ctrl.RTU_TAG, '.', 'defaultDev' ) 
    AND fes_rtu_para.TAG_NAME=background_x_fes_dig_ctrl.RTU_TAG 
    AND fes_dev_info.TAG_NAME = background_x_fes_dig_ctrl.RTU_DEV_TAG  
    AND fes_dev_info.RTU_TAG = background_x_fes_dig_ctrl.RTU_TAG;
    
    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) SELECT 
    concat(background_x_fes_mix_ctrl.RTU_TAG,'.',background_x_fes_mix_ctrl.RTU_DOT_NO), fes_dev_info.LOCATION_ID, fes_dev_info.SUB_SYSTEM, background_x_fes_mix_ctrl.RTU_TAG,fes_rtu_para.RTU_NO,'0',  background_x_fes_mix_ctrl.RTU_DOT_DESC, background_x_fes_mix_ctrl.RTU_DOT_NO,'1',   '0',       '65536',   background_x_fes_mix_ctrl.RTU_RES_PARA_INT1,background_x_fes_mix_ctrl.RTU_RES_PARA_INT2,background_x_fes_mix_ctrl.RTU_RES_PARA_INT3,background_x_fes_mix_ctrl.RTU_RES_PARA_INT4, background_x_fes_mix_ctrl.RTU_DOT_NO,'',          fes_dev_info.TAG_NAME
    FROM background_x_fes_mix_ctrl , fes_rtu_para ,fes_dev_info  WHERE  
    background_x_fes_mix_ctrl.RTU_DEV_TAG =concat(background_x_fes_mix_ctrl.RTU_TAG, '.', 'defaultDev' ) 
    AND fes_rtu_para.TAG_NAME=background_x_fes_mix_ctrl.RTU_TAG 
    AND fes_dev_info.TAG_NAME = background_x_fes_mix_ctrl.RTU_DEV_TAG  
    AND fes_dev_info.RTU_TAG = background_x_fes_mix_ctrl.RTU_TAG;
    --NOTE:默认设备处理完毕
    
    --重新写入FES与后台的关系 直接写入基础属性表和map_to_fes表,通过该表的触发器写入联系
    --重新写入,先写acc,ana,dig,再写map_to_fes,ctrl表
    update accuml
    SET RTU_TAG = background_x_fes_accuml.RTU_TAG
    from background_x_fes_accuml
    WHERE concat('accuml','.',accuml.TAG_NAME) = background_x_fes_accuml.BG_POINT_TAG;

   
    update analog
    SET RTU_TAG = background_x_fes_analog.RTU_TAG
    from background_x_fes_analog
    WHERE concat('analog','.',analog.TAG_NAME) = background_x_fes_analog.BG_POINT_TAG;

    
    update digital
    SET RTU_TAG = background_x_fes_digtal.RTU_TAG
    from background_x_fes_digtal
    WHERE concat('digital','.',digital.TAG_NAME) = background_x_fes_digtal.BG_POINT_TAG;

    
    update mix
    SET RTU_TAG = background_x_fes_mix.RTU_TAG
    from background_x_fes_mix
    WHERE concat('mix','.',mix.TAG_NAME) = background_x_fes_mix.BG_POINT_TAG;

    
    update accuml_map_to_fes
    SET DOT_NO = background_x_fes_accuml.RTU_DOT_NO
    from background_x_fes_accuml
    WHERE accuml_map_to_fes.KEY_ID_TAG = background_x_fes_accuml.BG_POINT_TAG AND accuml_map_to_fes.RTU_TAG = background_x_fes_accuml.RTU_TAG;

    
    update analog_map_to_fes
    SET DOT_NO = background_x_fes_analog.RTU_DOT_NO
    from background_x_fes_analog
    WHERE analog_map_to_fes.KEY_ID_TAG = background_x_fes_analog.BG_POINT_TAG AND analog_map_to_fes.RTU_TAG = background_x_fes_analog.RTU_TAG;

    
    update digital_map_to_fes
    SET DOT_NO1 = background_x_fes_digtal.RTU_DOT_NO
    from background_x_fes_digtal
    WHERE background_x_fes_digtal.BG_POINT_OFFSET = 1 AND digital_map_to_fes.KEY_ID_TAG = background_x_fes_digtal.BG_POINT_TAG AND digital_map_to_fes.RTU_TAG = background_x_fes_digtal.RTU_TAG;

    
    update digital_map_to_fes
    SET DOT_NO2 = background_x_fes_digtal.RTU_DOT_NO
    from background_x_fes_digtal
    WHERE background_x_fes_digtal.BG_POINT_OFFSET = 2 AND digital_map_to_fes.KEY_ID_TAG = background_x_fes_digtal.BG_POINT_TAG AND digital_map_to_fes.RTU_TAG = background_x_fes_digtal.RTU_TAG;

    
    update digital_map_to_fes
    SET DOT_NO3 = background_x_fes_digtal.RTU_DOT_NO
    from background_x_fes_digtal
    WHERE background_x_fes_digtal.BG_POINT_OFFSET = 3 AND digital_map_to_fes.KEY_ID_TAG = background_x_fes_digtal.BG_POINT_TAG AND digital_map_to_fes.RTU_TAG = background_x_fes_digtal.RTU_TAG;

    
    update digital_map_to_fes
    SET DOT_NO4 = background_x_fes_digtal.RTU_DOT_NO
    from background_x_fes_digtal
    WHERE background_x_fes_digtal.BG_POINT_OFFSET = 4 AND digital_map_to_fes.KEY_ID_TAG = background_x_fes_digtal.BG_POINT_TAG AND digital_map_to_fes.RTU_TAG = background_x_fes_digtal.RTU_TAG;

    
    update digital_map_to_fes
    SET DOT_NO5 = background_x_fes_digtal.RTU_DOT_NO
    from background_x_fes_digtal
    WHERE background_x_fes_digtal.BG_POINT_OFFSET = 5 AND digital_map_to_fes.KEY_ID_TAG = background_x_fes_digtal.BG_POINT_TAG AND digital_map_to_fes.RTU_TAG = background_x_fes_digtal.RTU_TAG;

    
    update mix_map_to_fes
    SET DOT_NO = background_x_fes_mix.RTU_DOT_NO
    from background_x_fes_mix
    WHERE mix_map_to_fes.KEY_ID_TAG = background_x_fes_mix.BG_POINT_TAG AND mix_map_to_fes.RTU_TAG = background_x_fes_mix.RTU_TAG;

    --写入ctrl表,注意此时background_x_fes__ctrl中的RTU必须和analog_control中的RTU保持一致,确保该点是同一个RTU
    update analog_control
    SET OFFSET_NO = background_x_fes_ana_ctrl.RTU_DOT_NO
    from background_x_fes_ana_ctrl
    WHERE analog_control.TAG_NAME = background_x_fes_ana_ctrl.BG_POINT_TAG AND analog_control.RTU_TAG = background_x_fes_ana_ctrl.RTU_TAG;

    
    update digital_control
    SET OFFSET_NO1 = background_x_fes_dig_ctrl.RTU_DOT_NO
    from background_x_fes_dig_ctrl
    WHERE digital_control.TAG_NAME = background_x_fes_dig_ctrl.BG_POINT_TAG AND background_x_fes_dig_ctrl.BG_POINT_OFFSET = 1 AND digital_control.RTU_TAG = background_x_fes_dig_ctrl.RTU_TAG;

    
    update digital_control
    SET OFFSET_NO2 = background_x_fes_dig_ctrl.RTU_DOT_NO
    from background_x_fes_dig_ctrl
    WHERE digital_control.TAG_NAME = background_x_fes_dig_ctrl.BG_POINT_TAG AND background_x_fes_dig_ctrl.BG_POINT_OFFSET = 2 AND digital_control.RTU_TAG = background_x_fes_dig_ctrl.RTU_TAG;

    
    update digital_control
    SET OFFSET_NO3 = background_x_fes_dig_ctrl.RTU_DOT_NO
    from background_x_fes_dig_ctrl
    WHERE digital_control.TAG_NAME = background_x_fes_dig_ctrl.BG_POINT_TAG AND background_x_fes_dig_ctrl.BG_POINT_OFFSET = 3 AND digital_control.RTU_TAG = background_x_fes_dig_ctrl.RTU_TAG;

    
    update digital_control
    SET OFFSET_NO4 = background_x_fes_dig_ctrl.RTU_DOT_NO
    from background_x_fes_dig_ctrl
    WHERE digital_control.TAG_NAME = background_x_fes_dig_ctrl.BG_POINT_TAG AND background_x_fes_dig_ctrl.BG_POINT_OFFSET = 4 AND digital_control.RTU_TAG = background_x_fes_dig_ctrl.RTU_TAG;

    
    update digital_control
    SET OFFSET_NO5 = background_x_fes_dig_ctrl.RTU_DOT_NO
    from background_x_fes_dig_ctrl
    WHERE digital_control.TAG_NAME = background_x_fes_dig_ctrl.BG_POINT_TAG AND background_x_fes_dig_ctrl.BG_POINT_OFFSET = 5 AND digital_control.RTU_TAG = background_x_fes_dig_ctrl.RTU_TAG;

    
    update mix_control
    SET OFFSET_NO = background_x_fes_mix_ctrl.RTU_DOT_NO
    from background_x_fes_mix_ctrl
    WHERE mix_control.TAG_NAME = background_x_fes_mix_ctrl.BG_POINT_TAG AND mix_control.RTU_TAG = background_x_fes_mix_ctrl.RTU_TAG;

    --写入完毕,下面为描述的处理
    
    --可选,保持实例前置设备(非默认设备)和excel文件中的一致
    IF importFesPointDesc = 1 THEN
    BEGIN
        
    update fes_accuml
    SET DESCRIPTION = background_x_fes_accuml.RTU_DOT_DESC
    from background_x_fes_accuml
    WHERE fes_accuml.TAG_NAME = concat(background_x_fes_accuml.RTU_TAG,'.',background_x_fes_accuml.RTU_DOT_NO) AND fes_accuml.DEV_TAG <> concat(fes_accuml.RTU_TAG,'.','defaultDev');

        
    update fes_analog
    SET DESCRIPTION = background_x_fes_analog.RTU_DOT_DESC
    from background_x_fes_analog
    WHERE fes_analog.TAG_NAME = concat(background_x_fes_analog.RTU_TAG,'.',background_x_fes_analog.RTU_DOT_NO) AND fes_analog.DEV_TAG <> concat(fes_analog.RTU_TAG,'.','defaultDev');

        
    update fes_digital
    SET DESCRIPTION = background_x_fes_digtal.RTU_DOT_DESC
    from background_x_fes_digtal
    WHERE fes_digital.TAG_NAME = concat(background_x_fes_digtal.RTU_TAG,'.', background_x_fes_digtal.RTU_DOT_NO) AND fes_digital.DEV_TAG <> concat(fes_digital.RTU_TAG,'.','defaultDev');

        
    update fes_mix
    SET DESCRIPTION = background_x_fes_mix.RTU_DOT_DESC
    from background_x_fes_mix
    WHERE fes_mix.TAG_NAME = concat(background_x_fes_mix.RTU_TAG,'.', background_x_fes_mix.RTU_DOT_NO) AND fes_mix.DEV_TAG <> concat(fes_mix.RTU_TAG,'.','defaultDev');

        
    update fes_analog_ctrl
    SET DESCRIPTION = background_x_fes_ana_ctrl.RTU_DOT_DESC
    from background_x_fes_ana_ctrl
    WHERE fes_analog_ctrl.TAG_NAME = concat(background_x_fes_ana_ctrl.RTU_TAG,'.', background_x_fes_ana_ctrl.RTU_DOT_NO) AND fes_analog_ctrl.DEV_TAG <> concat(fes_analog_ctrl.RTU_TAG,'.','defaultDev');

        
    update fes_digital_ctrl
    SET DESCRIPTION = background_x_fes_dig_ctrl.RTU_DOT_DESC
    from background_x_fes_dig_ctrl
    WHERE fes_digital_ctrl.TAG_NAME = concat(background_x_fes_dig_ctrl.RTU_TAG,'.',background_x_fes_dig_ctrl.RTU_DOT_NO) AND fes_digital_ctrl.DEV_TAG <> concat(fes_digital_ctrl.RTU_TAG,'.','defaultDev');

        
    update fes_mix_ctrl
    SET DESCRIPTION = background_x_fes_mix_ctrl.RTU_DOT_DESC
    from background_x_fes_mix_ctrl
    WHERE fes_mix_ctrl.TAG_NAME = concat(background_x_fes_mix_ctrl.RTU_TAG,'.',background_x_fes_mix_ctrl.RTU_DOT_NO) AND fes_mix_ctrl.DEV_TAG <> concat(fes_mix_ctrl.RTU_TAG,'.','defaultDev');

        
    END;
    END IF;
    
    --把前置描述导入后台,检索map_to_fes表,查找联系
    IF fesDescToBG = 1 THEN 
    BEGIN 
        UPDATE accuml 
        SET DESCRIPTION = fes_accuml.DESCRIPTION
		FROM accuml_map_to_fes,fes_accuml 
        WHERE accuml_map_to_fes.KEY_ID_TAG = concat('accuml','.',accuml.TAG_NAME) 
		AND COALESCE(accuml_map_to_fes.RTU_TAG,'')<>'' 
		AND COALESCE(accuml_map_to_fes.DOT_NO,'')<>''
        AND fes_accuml.TAG_NAME = concat( accuml_map_to_fes.RTU_TAG ,'.', accuml_map_to_fes.DOT_NO);
        
        UPDATE analog
        SET DESCRIPTION = fes_analog.DESCRIPTION
		FROM analog_map_to_fes,fes_analog 
        WHERE analog_map_to_fes.KEY_ID_TAG = concat('analog','.',analog.TAG_NAME) 
		AND COALESCE(analog_map_to_fes.RTU_TAG,'')<>'' 
		AND COALESCE(analog_map_to_fes.DOT_NO,'')<>''
        AND fes_analog.TAG_NAME = concat(analog_map_to_fes.RTU_TAG ,'.',  analog_map_to_fes.DOT_NO);
		
        --更新mix的
        UPDATE mix
        SET DESCRIPTION = fes_mix.DESCRIPTION
		FROM mix_map_to_fes,fes_mix 
        WHERE mix_map_to_fes.KEY_ID_TAG = concat('mix','.',mix.TAG_NAME) 
		AND COALESCE(mix_map_to_fes.RTU_TAG,'')<>'' 
		AND COALESCE(mix_map_to_fes.DOT_NO,'')<>''
        AND fes_mix.TAG_NAME = concat(mix_map_to_fes.RTU_TAG ,'.', mix_map_to_fes.DOT_NO) ;
		
        --更新dig的,以第一个点为准,其他点的描述不考虑
        UPDATE digital 
        SET DESCRIPTION = fes_digital.DESCRIPTION
		FROM digital_map_to_fes,fes_digital 
        WHERE digital_map_to_fes.KEY_ID_TAG = concat('digtal','.',digital.TAG_NAME) 
		AND COALESCE(digital_map_to_fes.RTU_TAG,'')<>'' 
		AND COALESCE(digital_map_to_fes.DOT_NO1,'')<>''
        AND fes_digital.TAG_NAME = concat(digital_map_to_fes.RTU_TAG ,'.', digital_map_to_fes.DOT_NO1) ;
        
    END;
    END IF;
    
    --把同步实时库的功能重新打开
    IF openTriggerInfo <> 1 THEN 
    UPDATE sys_trigger_flag SET FLAG_ID = 1;
    END IF;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION importPsc3000(rtuTag varchar(64))
RETURNS VOID
AS $$
    DECLARE count1 int DEFAULT 0;
    DECLARE count2 int DEFAULT 0;
BEGIN
    /*
     * 导入PSC3000的数据,数据在调用本存储过程前已经把excel表格中的数据存入PSC3000开头的表格中
     * 1.对于设备的处理,以excel表中设备ID和设备模板为判断基准,
               数据库中不存在该ID和模板一样的设备,则往数据库中增加该设备
                对于数据库中存在,而excel表中不存在的设备,删除数据库中的设备.
                对于数据库中存在ID和模板与excel一样的设备,对设备的名称和TAG_NAME和描述,这三个字段进行更新
        2.对于测点的处理,以设备ID和点名缩写为判断基准
                对于转发信息表中存在,而数据库中不存在的测点,进行添加操作
                对于转发信息表和数据库中都存在的测点,进行更新操作,更新字段规约参数1,点描述,变比系数,修正值,值描述文件(定值)
     * 遥控由于没有点名缩写,所以使用描述为判断基准
     */
    
    IF COALESCE(rtuTag,'') = '' THEN RETURN;
    END IF;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpImportPsc3000Tbl(
        TAG_NAME varchar(64),
        DEV_ID decimal(10,0),
        SHORT_NAME varchar(64),
        SEQ_NO decimal(10,0)
        );
    DELETE FROM tmpImportPsc3000Tbl;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpImportPsc3000ForAdd(
        DEV_ID decimal(10,0),
        SHORT_NAME varchar(64),
        SEQ_NO decimal(10,0)
        );
    DELETE FROM tmpImportPsc3000ForAdd;
    
    --删除数据库中多余的设备
    DELETE FROM fes_dev_info WHERE RTU_TAG = rtuTag AND NOT EXISTS
    (
        SELECT psc3000_dev.DEV_ID FROM psc3000_dev 
        WHERE psc3000_dev.DEV_ID = fes_dev_info.DEV_ID 
        AND psc3000_dev.DEV_MODEL = fes_dev_info.DEV_TP_NAME
    );
    
    --更新两边都存在的设备
    update fes_dev_info
    SET TAG_NAME = concat(rtuTag,'.',psc3000_dev.TAG_NAME), fes_dev_info.DEV_NAME = psc3000_dev.TAG_NAME, fes_dev_info.DEV_DESC = psc3000_dev.DESCRIPTION
    from psc3000_dev
    WHERE fes_dev_info.RTU_TAG = rtuTag AND fes_dev_info.DEV_ID = psc3000_dev.DEV_ID AND fes_dev_info.DEV_TP_NAME = psc3000_dev.DEV_MODEL;

    
    --筛选出要插入的设备  
    DELETE FROM tmpImportPsc3000Tbl;
    
    INSERT INTO tmpImportPsc3000Tbl (TAG_NAME,DEV_ID)
    SELECT psc3000_dev.DEV_MODEL,psc3000_dev.DEV_ID FROM psc3000_dev WHERE NOT EXISTS 
    (
       SELECT fes_dev_info.TAG_NAME FROM fes_dev_info 
       WHERE fes_dev_info.RTU_TAG = rtuTag 
       AND fes_dev_info.DEV_ID = psc3000_dev.DEV_ID
       AND fes_dev_info.DEV_TP_NAME = psc3000_dev.DEV_MODEL
    );
    
    INSERT INTO fes_dev_info 
    (TAG_NAME,                              DEV_NAME,            LOCATION_ID,             SUB_SYSTEM,             DEV_ID,             DEV_TYPE_ID,                RTU_TAG,RTU_NO,             DEV_DESC,               DEV_TP_NAME         ) SELECT 
    concat(rtuTag,'.',psc3000_dev.TAG_NAME),psc3000_dev.TAG_NAME,fes_rtu_para.LOCATION_ID,fes_rtu_para.SUB_SYSTEM,psc3000_dev.DEV_ID,fes_dev_temp_def.DEV_TYPE_ID,rtuTag, fes_rtu_para.RTU_NO,psc3000_dev.DESCRIPTION,psc3000_dev.DEV_MODEL
    FROM tmpImportPsc3000Tbl,fes_dev_temp_def,psc3000_dev,fes_rtu_para
    WHERE tmpImportPsc3000Tbl.TAG_NAME = fes_dev_temp_def.TAG_NAME
    AND tmpImportPsc3000Tbl.DEV_ID = psc3000_dev.DEV_ID
    AND tmpImportPsc3000Tbl.TAG_NAME = psc3000_dev.DEV_MODEL
    AND fes_rtu_para.TAG_NAME = rtuTag;
    
    --测点处理
    --更新双方都存在的测点,系数和基值是否更改还需确定,这里先不更改,要更改可以在update语句中添加即可
    --acc
    DELETE FROM tmpImportPsc3000Tbl;
    
    INSERT INTO tmpImportPsc3000Tbl (TAG_NAME,DEV_ID,SHORT_NAME)
    SELECT fes_accuml.TAG_NAME,psc3000_acc.DEV_ID, psc3000_acc.TAG_NAME
    FROM fes_accuml,psc3000_dev,psc3000_acc
    WHERE fes_accuml.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_accuml.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_acc.DEV_ID
    AND fes_accuml.FES_POINT_TP_NAME = concat(psc3000_dev.DEV_MODEL,'.',psc3000_acc.TAG_NAME);
    
    UPDATE fes_accuml
    SET DESCRIPTION = psc3000_acc.DESCRIPTION,
    RES_PARA_INT1 = psc3000_acc.REMOTE_NO,
    COEFF = psc3000_acc.RATIO
	FROM tmpImportPsc3000Tbl,psc3000_acc 
	WHERE tmpImportPsc3000Tbl.TAG_NAME = fes_accuml.TAG_NAME 
    AND tmpImportPsc3000Tbl.DEV_ID = psc3000_acc.DEV_ID 
    AND tmpImportPsc3000Tbl.SHORT_NAME = psc3000_acc.TAG_NAME;
    
    --添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    SELECT count(*) INTO count1 FROM tmpImportPsc3000Tbl;
    SELECT count(*) INTO count2 FROM psc3000_acc;
    --更新的记录数少于转发信息表的数量,需要添加
    IF count1 < count2 THEN 
    BEGIN
        DELETE FROM tmpImportPsc3000ForAdd;
        
        INSERT INTO tmpImportPsc3000ForAdd (DEV_ID, SHORT_NAME)
        SELECT  psc3000_acc.DEV_ID,psc3000_acc.TAG_NAME
        FROM psc3000_acc LEFT JOIN tmpImportPsc3000Tbl 
        ON psc3000_acc.DEV_ID = tmpImportPsc3000Tbl.DEV_ID AND psc3000_acc.TAG_NAME = tmpImportPsc3000Tbl.SHORT_NAME 
        WHERE tmpImportPsc3000Tbl.DEV_ID IS NULL;
        
        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(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_ACC'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM ,getMaxRtuDotNo(rtuTag,getFesPointType('FES_ACC')), rtuTag, concat(rtuTag,'.',psc3000_dev.TAG_NAME), getMaxDefaultDevSeqNo(concat(rtuTag,'.',psc3000_dev.TAG_NAME),getFesPointType('FES_ACC')),  '',             '',            '',            '0',  psc3000_acc.RATIO,  fes_rtu_para.RTU_NO , psc3000_acc.REMOTE_NO, 0,             0,            0,             psc3000_acc.DESCRIPTION , '',                '',        0                             
        FROM tmpImportPsc3000ForAdd,psc3000_acc,psc3000_dev, fes_rtu_para 
        WHERE tmpImportPsc3000ForAdd.DEV_ID = psc3000_acc.DEV_ID
        AND tmpImportPsc3000ForAdd.SHORT_NAME = psc3000_acc.TAG_NAME
        AND psc3000_acc.DEV_ID = psc3000_dev.DEV_ID
        AND fes_rtu_para.TAG_NAME = rtuTag
        ; 
        
    END;
    END IF;
    
    --ana
    DELETE FROM tmpImportPsc3000Tbl;
    INSERT INTO tmpImportPsc3000Tbl (TAG_NAME,DEV_ID,SHORT_NAME)
    SELECT fes_analog.TAG_NAME,psc3000_ana.DEV_ID, psc3000_ana.TAG_NAME
    FROM fes_analog,psc3000_dev,psc3000_ana
    WHERE fes_analog.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_analog.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_ana.DEV_ID
    AND fes_analog.FES_POINT_TP_NAME = concat(psc3000_dev.DEV_MODEL,'.',psc3000_ana.TAG_NAME);
    
    UPDATE fes_analog 
    SET DESCRIPTION = psc3000_ana.DESCRIPTION,
    RES_PARA_INT1 = psc3000_ana.REMOTE_NO,
    COEFF = psc3000_ana.RATIO
	FROM tmpImportPsc3000Tbl,psc3000_ana 
	WHERE tmpImportPsc3000Tbl.TAG_NAME = fes_analog.TAG_NAME
    AND tmpImportPsc3000Tbl.DEV_ID = psc3000_ana.DEV_ID 
    AND tmpImportPsc3000Tbl.SHORT_NAME = psc3000_ana.TAG_NAME;
    
    --添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    SELECT count(*) INTO count1 FROM tmpImportPsc3000Tbl;
    SELECT count(*) INTO count2 FROM psc3000_ana;
    --更新的记录数少于转发信息表的数量,需要添加
    IF count1 < count2 THEN 
    BEGIN
        DELETE FROM tmpImportPsc3000ForAdd;
        
        INSERT INTO tmpImportPsc3000ForAdd (DEV_ID, SHORT_NAME)
        SELECT  psc3000_ana.DEV_ID,psc3000_ana.TAG_NAME
        FROM psc3000_ana LEFT JOIN tmpImportPsc3000Tbl 
        ON psc3000_ana.DEV_ID = tmpImportPsc3000Tbl.DEV_ID AND psc3000_ana.TAG_NAME = tmpImportPsc3000Tbl.SHORT_NAME 
        WHERE tmpImportPsc3000Tbl.DEV_ID IS NULL;
        
        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(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_ANA'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , concat(rtuTag,'.',psc3000_dev.TAG_NAME),getMaxDefaultDevSeqNo(concat(rtuTag,'.',psc3000_dev.TAG_NAME),getFesPointType('FES_ANA') ), rtuTag,   fes_rtu_para.RTU_NO,getMaxRtuDotNo(rtuTag,getFesPointType('FES_ANA')),'',            '',            '',               '0',       '0',     '0',           '0',       '0',     '0',  psc3000_ana.RATIO,   '65536',    '0',      psc3000_ana.REMOTE_NO,  0,            0,               0,             psc3000_ana.DESCRIPTION, '',                 '',       0
        FROM tmpImportPsc3000ForAdd,psc3000_ana,psc3000_dev, fes_rtu_para 
        WHERE tmpImportPsc3000ForAdd.DEV_ID = psc3000_ana.DEV_ID
        AND tmpImportPsc3000ForAdd.SHORT_NAME = psc3000_ana.TAG_NAME
        AND psc3000_ana.DEV_ID = psc3000_dev.DEV_ID
        AND fes_rtu_para.TAG_NAME = rtuTag
        ; 
         
    END;
    END IF;
    
    --dig
    DELETE FROM tmpImportPsc3000Tbl;
    INSERT INTO tmpImportPsc3000Tbl (TAG_NAME,DEV_ID,SHORT_NAME)
    SELECT fes_digital.TAG_NAME,psc3000_dig.DEV_ID, psc3000_dig.TAG_NAME
    FROM fes_digital,psc3000_dev,psc3000_dig
    WHERE fes_digital.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_digital.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_dig.DEV_ID
    AND fes_digital.FES_POINT_TP_NAME = concat(psc3000_dev.DEV_MODEL,'.',psc3000_dig.TAG_NAME);
    
    UPDATE fes_digital
    SET DESCRIPTION = psc3000_dig.DESCRIPTION,
    RES_PARA_INT1 = psc3000_dig.REMOTE_NO
	FROM tmpImportPsc3000Tbl,psc3000_dig
	WHERE tmpImportPsc3000Tbl.TAG_NAME = fes_digital.TAG_NAME
    AND tmpImportPsc3000Tbl.DEV_ID = psc3000_dig.DEV_ID 
    AND tmpImportPsc3000Tbl.SHORT_NAME = psc3000_dig.TAG_NAME;
    
    --添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    SELECT count(*) INTO count1 FROM tmpImportPsc3000Tbl;
    SELECT count(*) INTO count2 FROM psc3000_dig;
    --更新的记录数少于转发信息表的数量,需要添加
    IF count1 < count2 THEN 
    BEGIN
        DELETE FROM tmpImportPsc3000ForAdd;
        
        INSERT INTO tmpImportPsc3000ForAdd (DEV_ID, SHORT_NAME)
        SELECT  psc3000_dig.DEV_ID,psc3000_dig.TAG_NAME
        FROM psc3000_dig LEFT JOIN tmpImportPsc3000Tbl 
        ON psc3000_dig.DEV_ID = tmpImportPsc3000Tbl.DEV_ID AND psc3000_dig.TAG_NAME = tmpImportPsc3000Tbl.SHORT_NAME 
        WHERE tmpImportPsc3000Tbl.DEV_ID IS NULL;
        
        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(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_DIG'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , concat(rtuTag,'.',psc3000_dev.TAG_NAME),getMaxDefaultDevSeqNo(concat(rtuTag,'.',psc3000_dev.TAG_NAME),getFesPointType('FES_DIG') ), rtuTag,  getMaxRtuDotNo(rtuTag,getFesPointType('FES_DIG')),'',             '',            '',             '1',        '1',            '0',           '0',     fes_rtu_para.RTU_NO,psc3000_dig.REMOTE_NO, 0,            0,              0,              psc3000_dig.DESCRIPTION,'',                 '',        0
        FROM tmpImportPsc3000ForAdd,psc3000_dig,psc3000_dev, fes_rtu_para 
        WHERE tmpImportPsc3000ForAdd.DEV_ID = psc3000_dig.DEV_ID
        AND tmpImportPsc3000ForAdd.SHORT_NAME = psc3000_dig.TAG_NAME
        AND psc3000_dig.DEV_ID = psc3000_dev.DEV_ID
        AND fes_rtu_para.TAG_NAME = rtuTag
        ; 
         
    END;
    END IF;
    
    
    --dig_ctrl,使用描述为判断基准
    DELETE FROM tmpImportPsc3000Tbl;
    INSERT INTO tmpImportPsc3000Tbl (TAG_NAME,DEV_ID,SHORT_NAME)
    SELECT fes_digital_ctrl.TAG_NAME,psc3000_dig_ctrl.DEV_ID, psc3000_dig_ctrl.DESCRIPTION
    FROM fes_digital_ctrl,psc3000_dev,psc3000_dig_ctrl
    WHERE fes_digital_ctrl.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_digital_ctrl.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_dig_ctrl.DEV_ID
    AND fes_digital_ctrl.DESCRIPTION = psc3000_dig_ctrl.DESCRIPTION;
    
    UPDATE fes_digital_ctrl
    SET DESCRIPTION = psc3000_dig_ctrl.DESCRIPTION,
    RES_PARA_INT1 = psc3000_dig_ctrl.REMOTE_NO
	FROM tmpImportPsc3000Tbl,psc3000_dig_ctrl 
	WHERE tmpImportPsc3000Tbl.TAG_NAME = fes_digital_ctrl.TAG_NAME 
    AND tmpImportPsc3000Tbl.DEV_ID = psc3000_dig_ctrl.DEV_ID 
    AND tmpImportPsc3000Tbl.SHORT_NAME = psc3000_dig_ctrl.DESCRIPTION;
    
    --添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    SELECT count(*) INTO count1 FROM tmpImportPsc3000Tbl;
    SELECT count(*) INTO count2 FROM psc3000_dig_ctrl;
    --更新的记录数少于转发信息表的数量,需要添加
    IF count1 < count2 THEN 
    BEGIN
        DELETE FROM tmpImportPsc3000ForAdd;
        
        INSERT INTO tmpImportPsc3000ForAdd (DEV_ID, SHORT_NAME)
        SELECT  psc3000_dig_ctrl.DEV_ID,psc3000_dig_ctrl.DESCRIPTION
        FROM psc3000_dig_ctrl LEFT JOIN tmpImportPsc3000Tbl 
        ON psc3000_dig_ctrl.DEV_ID = tmpImportPsc3000Tbl.DEV_ID AND psc3000_dig_ctrl.DESCRIPTION = tmpImportPsc3000Tbl.SHORT_NAME 
        WHERE tmpImportPsc3000Tbl.DEV_ID IS NULL;
        
        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)SELECT   
        concat(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_DIG_CTRL'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , rtuTag,  fes_rtu_para.RTU_NO,getMaxRtuDotNo(rtuTag,getFesPointType('FES_DIG_CTRL')), concat(rtuTag,'.',psc3000_dev.TAG_NAME),psc3000_dig_ctrl.DESCRIPTION,'0' ,   '',           getMaxDefaultDevSeqNo(concat(rtuTag,'.',psc3000_dev.TAG_NAME),getFesPointType('FES_DIG_CTRL') ), psc3000_dig_ctrl.REMOTE_NO, 0,             0,              0,              '',       0,             '',             '',           '',            '',                     '',                 '',                    0
        FROM tmpImportPsc3000ForAdd,psc3000_dig_ctrl,psc3000_dev, fes_rtu_para 
        WHERE tmpImportPsc3000ForAdd.DEV_ID = psc3000_dig_ctrl.DEV_ID
        AND tmpImportPsc3000ForAdd.SHORT_NAME = psc3000_dig_ctrl.DESCRIPTION
        AND psc3000_dig_ctrl.DEV_ID = psc3000_dev.DEV_ID
        AND fes_rtu_para.TAG_NAME = rtuTag
        ; 
         
    END;
    END IF;
    
    
    --DZ定值
    DELETE FROM tmpImportPsc3000Tbl;
    INSERT INTO tmpImportPsc3000Tbl (TAG_NAME,DEV_ID,SEQ_NO)
    SELECT fes_const.TAG_NAME,psc3000_const.DEV_ID, psc3000_const.SEQ_NO
    FROM fes_const,psc3000_dev,psc3000_const
    WHERE fes_const.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_const.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_const.DEV_ID
    AND fes_const.SEQ_NO = psc3000_const.SEQ_NO;
    
    UPDATE fes_const
    SET DESCRIPTION = psc3000_const.DESCRIPTION,
    VALUE_RATIO = psc3000_const.RATIO,
    VALUE_ADDED = psc3000_const.REVISE,
    VALUE_TEXT = psc3000_const.VALUE_TEXT
	FROM tmpImportPsc3000Tbl,psc3000_const 
	WHERE tmpImportPsc3000Tbl.TAG_NAME = fes_const.TAG_NAME 
    AND tmpImportPsc3000Tbl.DEV_ID = psc3000_const.DEV_ID 
    AND tmpImportPsc3000Tbl.SEQ_NO = psc3000_const.SEQ_NO;
    
    --添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    SELECT count(*) INTO count1 FROM tmpImportPsc3000Tbl;
    SELECT count(*) INTO count2 FROM psc3000_const;
    --更新的记录数少于转发信息表的数量,需要添加
    IF count1 < count2 THEN 
    BEGIN
        DELETE FROM tmpImportPsc3000ForAdd;
        
        INSERT INTO tmpImportPsc3000ForAdd (DEV_ID, SEQ_NO)
        SELECT  psc3000_const.DEV_ID,psc3000_const.SEQ_NO
        FROM psc3000_const LEFT JOIN tmpImportPsc3000Tbl 
        ON psc3000_const.DEV_ID = tmpImportPsc3000Tbl.DEV_ID AND psc3000_const.SEQ_NO = tmpImportPsc3000Tbl.SEQ_NO 
        WHERE tmpImportPsc3000Tbl.DEV_ID IS NULL;
        
        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(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_CONST'))),  psc3000_const.DESCRIPTION ,0 ,        rtuTag,  fes_rtu_para.RTU_NO,  getMaxRtuDotNo(rtuTag,getFesPointType('FES_CONST')),  getMaxDefaultDevSeqNo(concat(rtuTag,'.',psc3000_dev.TAG_NAME),getFesPointType('FES_CONST') ),fes_rtu_para.SUB_SYSTEM,fes_rtu_para.LOCATION_ID, concat(rtuTag,'.',psc3000_dev.TAG_NAME), 0,      0,            0,       psc3000_const.VALUE_TEXT,psc3000_const.RATIO,psc3000_const.REVISE, 0,          0,         0,               0,              '',       '',                  '',          0
        FROM tmpImportPsc3000ForAdd,psc3000_const,psc3000_dev, fes_rtu_para 
        WHERE tmpImportPsc3000ForAdd.DEV_ID = psc3000_const.DEV_ID
        AND tmpImportPsc3000ForAdd.SEQ_NO = psc3000_const.SEQ_NO
        AND psc3000_const.DEV_ID = psc3000_dev.DEV_ID
        AND fes_rtu_para.TAG_NAME = rtuTag
        ; 
         
    END;
    END IF;
    
    
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION relationByPlugin(pluginName varchar(64), devTag varchar(64),fesDevTag varchar(64))
RETURNS VOID
AS $$
    DECLARE rtuTag varchar(64) DEFAULT '';
    DECLARE tempName varchar(64) DEFAULT '';
    DECLARE fesTempName varchar(64) DEFAULT '';
BEGIN
    --使用套件中的关联关系,关联前置和后台设备,仅关联套件中指定了关联关系的测点,需注意,为了加快速度,默认此时的后台设备的RTU标签为空,若不为空,调用此存储过程前,请先置空
    --pluginName 套件名
    --devTag 后台设备名
    --fesDevTag 前置设备名
    
    SELECT DEV_TP_NAME INTO tempName FROM dev_info WHERE TAG_NAME = devTag;
    SELECT RTU_TAG,DEV_TP_NAME INTO rtuTag,fesTempName FROM fes_dev_info WHERE TAG_NAME = fesDevTag;
    IF COALESCE(tempName,'') = '' OR COALESCE(fesTempName,'') = '' OR COALESCE(rtuTag,'') = ''
    THEN 
    RETURN; 
    END IF;
    
    --创建临时表
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpRelationByPluginTbl(
        KEY_ID_TAG varchar(128) NOT NULL,
        DOT_NO varchar(48) NOT NULL,
        OFFSET_NO decimal(10) NOT NULL DEFAULT 0,
        PRIMARY KEY(KEY_ID_TAG,OFFSET_NO)
    );
    
    --以accuml为例,进行说明.把后台设备下的所有accuml点,和对应的前置设备下的fes_accuml点的Key_ID_TAG和DOT_NO写入临时表
    DELETE FROM tmpRelationByPluginTbl;
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO) 
    SELECT accuml_map_to_fes.KEY_ID_TAG,fes_accuml.DOT_NO
    FROM (temp_plugin_point_map_acc INNER JOIN accuml_map_to_fes 
    ON temp_plugin_point_map_acc.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_acc.TEMP_NAME = tempName AND 
    temp_plugin_point_map_acc.FES_TEMP_NAME = fesTempName AND
    accuml_map_to_fes.KEY_ID_TAG = concat('accuml','.' ,devTag,'.',TEMP_POINT_NAME)  )
    INNER JOIN fes_accuml 
    ON fes_accuml.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_accuml.DEV_TAG = fesDevTag AND
    fes_accuml.FES_POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_POINT_NAME);
    
    --假如前置点已经被占用,先清除
    update fes_accuml
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationbyplugintbl
    where fes_accuml.TAG_NAME = concat(rtuTag,'.', tmpRelationByPluginTbl.DOT_NO);

    
    --模拟手动操作,先关联对应RTU,再关联对应点
    update accuml
    set rtu_tag = rtutag 
    from tmprelationbyplugintbl
    where tag_name = substring(key_id_tag,8,char_length(key_id_tag) - 7);

    
    update accuml_map_to_fes
    SET DOT_NO = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE accuml_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG;

    
    --analog
    DELETE FROM tmpRelationByPluginTbl;
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO) 
    SELECT analog_map_to_fes.KEY_ID_TAG,fes_analog.DOT_NO
    FROM (temp_plugin_point_map_ana INNER JOIN analog_map_to_fes 
    ON temp_plugin_point_map_ana.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_ana.TEMP_NAME = tempName AND 
    temp_plugin_point_map_ana.FES_TEMP_NAME = fesTempName AND
    analog_map_to_fes.KEY_ID_TAG = concat('analog','.' ,devTag,'.',TEMP_POINT_NAME) )
    INNER JOIN fes_analog ON 
    fes_analog.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_analog.DEV_TAG = fesDevTag AND
    fes_analog.FES_POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_POINT_NAME);
    
    --假如前置点已经被占用,先清除
    update fes_analog
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationbyplugintbl
    where fes_analog.TAG_NAME = concat(rtuTag,'.', tmpRelationByPluginTbl.DOT_NO);

    
    --模拟手动操作,先关联对应RTU,再关联对应点
    update analog
    set rtu_tag = rtutag 
    from tmprelationbyplugintbl
    where tag_name = substring(key_id_tag,8,char_length(key_id_tag) - 7);

    
    update analog_map_to_fes
    SET DOT_NO = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE analog_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG;

    
    --mix
    DELETE FROM tmpRelationByPluginTbl;
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO) 
    SELECT mix_map_to_fes.KEY_ID_TAG,fes_mix.DOT_NO
    FROM (temp_plugin_point_map_mix INNER JOIN mix_map_to_fes
    ON temp_plugin_point_map_mix.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_mix.TEMP_NAME = tempName AND 
    temp_plugin_point_map_mix.FES_TEMP_NAME = fesTempName AND
    mix_map_to_fes.KEY_ID_TAG = concat('mix','.' ,devTag,'.',TEMP_POINT_NAME) )
    INNER JOIN fes_mix ON  
    fes_mix.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_mix.DEV_TAG = fesDevTag AND
    fes_mix.FES_POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_POINT_NAME);
    
    --假如前置点已经被占用,先清除
    update fes_mix
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationbyplugintbl
    where fes_mix.TAG_NAME = concat(rtuTag,'.', tmpRelationByPluginTbl.DOT_NO);

    
    --模拟手动操作,先关联对应RTU,再关联对应点
    update mix
    set rtu_tag = rtutag 
    from tmprelationbyplugintbl
    where tag_name = substring(key_id_tag,5,char_length(key_id_tag) - 4);

    
    update mix_map_to_fes
    SET DOT_NO = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE mix_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG;

    
    --dig
    DELETE FROM tmpRelationByPluginTbl;
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_map_to_fes.KEY_ID_TAG,fes_digital.DOT_NO,1
    FROM (temp_plugin_point_map_dig INNER JOIN digital_map_to_fes 
    ON temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND 
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME1 <> '' AND 
    digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,devTag,'.',TEMP_POINT_NAME) )
    INNER JOIN fes_digital ON 
    fes_digital.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital.DEV_TAG = fesDevTag AND
    fes_digital.FES_POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_POINT_NAME1);
    
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_map_to_fes.KEY_ID_TAG,fes_digital.DOT_NO,2
    FROM (temp_plugin_point_map_dig INNER JOIN digital_map_to_fes
    ON temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND 
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME2 <> '' AND 
    digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,devTag,'.',TEMP_POINT_NAME))
    INNER JOIN fes_digital ON 
    fes_digital.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital.DEV_TAG = fesDevTag AND
    fes_digital.FES_POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_POINT_NAME2);
    
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_map_to_fes.KEY_ID_TAG,fes_digital.DOT_NO,3
    FROM (temp_plugin_point_map_dig INNER JOIN digital_map_to_fes 
    ON temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND 
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME3 <> '' AND 
    digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,devTag,'.',TEMP_POINT_NAME))
    INNER JOIN fes_digital ON 
    fes_digital.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital.DEV_TAG = fesDevTag AND
    fes_digital.FES_POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_POINT_NAME3);
    
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_map_to_fes.KEY_ID_TAG,fes_digital.DOT_NO,4
    FROM (temp_plugin_point_map_dig INNER JOIN digital_map_to_fes 
    ON temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND 
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME4 <> '' AND 
    digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,devTag,'.',TEMP_POINT_NAME))
    INNER JOIN fes_digital ON 
    fes_digital.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital.DEV_TAG = fesDevTag AND
    fes_digital.FES_POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_POINT_NAME4);
    
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_map_to_fes.KEY_ID_TAG,fes_digital.DOT_NO,5
    FROM (temp_plugin_point_map_dig INNER JOIN digital_map_to_fes
    ON temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND 
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME5 <> '' AND 
    digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,devTag,'.',TEMP_POINT_NAME) )
    INNER JOIN fes_digital ON 
    fes_digital.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital.DEV_TAG = fesDevTag AND
    fes_digital.FES_POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_POINT_NAME5);
    
    --假如前置点已经被占用,先清除
    update fes_digital
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationbyplugintbl
    where fes_digital.TAG_NAME = concat(rtuTag,'.', tmpRelationByPluginTbl.DOT_NO);

    
    --模拟手动操作,先关联对应RTU,再关联对应点
    update digital
    set rtu_tag = rtutag 
    from tmprelationbyplugintbl
    where tag_name = substring(key_id_tag,9,char_length(key_id_tag) - 8);

    
    update digital_map_to_fes
    SET DOT_NO1 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 1;

    
    update digital_map_to_fes
    SET DOT_NO2 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 2;

    
    update digital_map_to_fes
    SET DOT_NO3 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 3;

    
    update digital_map_to_fes
    SET DOT_NO4 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 4;

    
    update digital_map_to_fes
    SET DOT_NO5 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 5;

    
    --对于控制点
    --analog_ctrl
    DELETE FROM tmpRelationByPluginTbl;
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO) 
    SELECT analog_control.TAG_NAME,fes_analog_ctrl.DOT_NO
    FROM temp_plugin_point_map_ana,analog_control,fes_analog_ctrl
    WHERE temp_plugin_point_map_ana.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_ana.TEMP_NAME = tempName AND 
    temp_plugin_point_map_ana.FES_TEMP_NAME = fesTempName AND
    analog_control.TAG_NAME = concat(devTag,'.',TEMP_POINT_NAME) AND
    fes_analog_ctrl.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_analog_ctrl.DEV_TAG = fesDevTag AND
    fes_analog_ctrl.POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_OUT_NAME);

    --修改如果没有采集点，有遥控点也要新建测点
    update analog
    SET RTU_TAG = rtuTag 
    from tmprelationbyplugintbl
    WHERE TAG_NAME = KEY_ID_TAG;
    
    update analog_control
    SET OFFSET_NO = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE analog_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG;

    
    --mix_ctrl
    DELETE FROM tmpRelationByPluginTbl;
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO) 
    SELECT mix_control.TAG_NAME,fes_mix_ctrl.DOT_NO
    FROM temp_plugin_point_map_mix,mix_control,fes_mix_ctrl
    WHERE temp_plugin_point_map_mix.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_mix.TEMP_NAME = tempName AND 
    temp_plugin_point_map_mix.FES_TEMP_NAME = fesTempName AND
    mix_control.TAG_NAME = concat(devTag,'.',TEMP_POINT_NAME) AND
    fes_mix_ctrl.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_mix_ctrl.DEV_TAG = fesDevTag AND
    fes_mix_ctrl.POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_OUT_NAME);

    --修改如果没有采集点，有遥控点也要新建测点
    update mix
    SET RTU_TAG = rtuTag 
    from tmprelationbyplugintbl
    WHERE TAG_NAME = KEY_ID_TAG;
    
    update mix_control
    SET OFFSET_NO = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE mix_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG;

    
    --digital_ctrl
    DELETE FROM tmpRelationByPluginTbl;
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_control.TAG_NAME,fes_digital_ctrl.DOT_NO, 1
    FROM temp_plugin_point_map_dig,digital_control,fes_digital_ctrl
    WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND
    temp_plugin_point_map_dig.FES_TEMP_OUT_NAME1 <> '' AND 
    digital_control.TAG_NAME = concat(devTag,'.',TEMP_POINT_NAME) AND
    fes_digital_ctrl.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital_ctrl.DEV_TAG = fesDevTag AND
    fes_digital_ctrl.POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_OUT_NAME1);
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_control.TAG_NAME,fes_digital_ctrl.DOT_NO, 2
    FROM temp_plugin_point_map_dig,digital_control,fes_digital_ctrl
    WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND
    temp_plugin_point_map_dig.FES_TEMP_OUT_NAME2 <> '' AND 
    digital_control.TAG_NAME = concat(devTag,'.',TEMP_POINT_NAME) AND
    fes_digital_ctrl.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital_ctrl.DEV_TAG = fesDevTag AND
    fes_digital_ctrl.POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_OUT_NAME2);
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_control.TAG_NAME,fes_digital_ctrl.DOT_NO, 3
    FROM temp_plugin_point_map_dig,digital_control,fes_digital_ctrl
    WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND
    temp_plugin_point_map_dig.FES_TEMP_OUT_NAME3 <> '' AND 
    digital_control.TAG_NAME = concat(devTag,'.',TEMP_POINT_NAME) AND
    fes_digital_ctrl.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital_ctrl.DEV_TAG = fesDevTag AND
    fes_digital_ctrl.POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_OUT_NAME3);
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_control.TAG_NAME,fes_digital_ctrl.DOT_NO, 4
    FROM temp_plugin_point_map_dig,digital_control,fes_digital_ctrl
    WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND
    temp_plugin_point_map_dig.FES_TEMP_OUT_NAME4 <> '' AND 
    digital_control.TAG_NAME = concat(devTag,'.',TEMP_POINT_NAME) AND
    fes_digital_ctrl.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital_ctrl.DEV_TAG = fesDevTag AND
    fes_digital_ctrl.POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_OUT_NAME4);
    
    INSERT INTO tmpRelationByPluginTbl (KEY_ID_TAG , DOT_NO,OFFSET_NO) 
    SELECT digital_control.TAG_NAME,fes_digital_ctrl.DOT_NO, 5
    FROM temp_plugin_point_map_dig,digital_control,fes_digital_ctrl
    WHERE temp_plugin_point_map_dig.PLUGIN_NAME = pluginName AND
    temp_plugin_point_map_dig.TEMP_NAME = tempName AND 
    temp_plugin_point_map_dig.FES_TEMP_NAME = fesTempName AND
    temp_plugin_point_map_dig.FES_TEMP_OUT_NAME5 <> '' AND 
    digital_control.TAG_NAME = concat(devTag,'.',TEMP_POINT_NAME) AND
    fes_digital_ctrl.TAG_NAME LIKE concat(rtuTag,'.','%') AND
    fes_digital_ctrl.DEV_TAG = fesDevTag AND
    fes_digital_ctrl.POINT_TP_NAME = concat(FES_TEMP_NAME,'.',FES_TEMP_OUT_NAME5);
    
    --主要为了对应测点只遥控无采集的情况
    update digital
    SET RTU_TAG = rtuTag 
    from tmprelationbyplugintbl
    WHERE TAG_NAME = KEY_ID_TAG AND OFFSET_NO = 1;

    
    update digital_control
    SET OFFSET_NO1 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 1;

    
    update digital_control
    SET OFFSET_NO2 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 2;

    
    update digital_control
    SET OFFSET_NO3 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 3;

    
    update digital_control
    SET OFFSET_NO4 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 4;

    
    update digital_control
    SET OFFSET_NO5 = tmpRelationByPluginTbl.DOT_NO
    from tmprelationbyplugintbl
    WHERE digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 5;

    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION importPsc3000ByPlugin(rtuTag varchar(64),regionId decimal(10,0))
RETURNS VOID
AS $$
    DECLARE locationTag varchar(64) DEFAULT '';
    DECLARE locationId decimal(10,0) DEFAULT 0;
    DECLARE subsystemId decimal(10,0) DEFAULT 0;
    DECLARE rtuNo decimal(10,0) DEFAULT 0;
    
    DECLARE devGrpTag varchar(64) DEFAULT '';
    DECLARE pluginName varchar(64) DEFAULT '';
    DECLARE tempName varchar(64) DEFAULT '';
    DECLARE fesTempName varchar(64) DEFAULT '';
    
    DECLARE fesDevTag varchar(64) DEFAULT '';
    DECLARE myDevTag varchar(64) DEFAULT '';
	
	--在此游标中临时表的第一列是套件名,第二列是设备组名,游标获取4列,分别是设备组,套件名,后台模板名,前置模板名
    DECLARE cur_1 REFCURSOR;
    
    --CURSOR cur_2 FOR
    --SELECT PLUGIN_NAME
    --FROM tmpImportPsc3000PluginTbl WHERE tmpImportPsc3000PluginTbl.DEV_GRP = '';
  
BEGIN
    /*
     * 导入PSC3000数据通过构建套件的形式
     * rtuTag 要导入的RTU
     * regionId 责任区ID
     * PSC3000_plugin表中存着要导入的套件
     * PSC3000_DEV 表中存着导入的前置设备
     * PSC3000_acc,ana,dig,mix,dig_ctrl,const存着对应的测点数据
     */
    
  
    
    SELECT fes_rtu_para.LOCATION_ID,SUB_SYSTEM,RTU_NO,sys_model_location_info.TAG_NAME INTO locationId,subsystemId,rtuNo,locationTag 
    FROM fes_rtu_para,sys_model_location_info 
    WHERE fes_rtu_para.TAG_NAME = rtuTag AND fes_rtu_para.LOCATION_ID = sys_model_location_info.LOCATION_ID;
    
    --清空前置与后台的数据
    --DELETE FROM fes_dev_info WHERE RTU_TAG = rtuTag;
    --DELETE dev_group FROM dev_group,psc3000_plugin WHERE  dev_group.TAG_NAME = concat(locationTag,'.',psc3000_plugin.DEV_GROUP_TAG);
    
    --创建临时表,
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpImportPsc3000PluginTbl(
        PLUGIN_NAME varchar(64) NOT NULL,
        DEV_GRP varchar(64) NOT NULL
    );
    DELETE FROM tmpImportPsc3000PluginTbl;
    
    --用于第十一步的临时表
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpPsc3000PluginFesTempTbl(
        TAG_NAME varchar(64) NOT NULL,
        FES_TEMP varchar(64) NOT NULL,
        DEV_ID decimal(10,0)
    );
    DELETE FROM tmpPsc3000PluginFesTempTbl;
    
    --第一步,创建设备组
    INSERT INTO DEV_GROUP 
    (DESCRIPTION,                 GROUP_TAG,IS_SUMMARY,LOCATION_ID,PGROUP_TAG_NAME,REGION_ID,SUB_SYSTEM,SUMMARY_STATUS,SUMMARY_VALUE,TAG_NAME,RTU_NO) 
    SELECT  
    psc3000_plugin.DEV_GROUP_DESC,'',        '0',      locationId,  '',             regionId,subsystemId,'0',           '0',          concat(locationTag,'.',psc3000_plugin.DEV_GROUP_TAG),rtuNo
    FROM psc3000_plugin;
    
    --第二步 实例化后台和前置的设备,对于后台设备 一个设备组对应一个套件
    DELETE FROM tmpImportPsc3000PluginTbl;
    --此时临时表中第一列是套件名,第二列是设备组名
    INSERT INTO tmpImportPsc3000PluginTbl (PLUGIN_NAME , DEV_GRP) 
    SELECT PLUGIN_NAME,DEV_GROUP_TAG
    FROM psc3000_plugin;
    
    INSERT INTO DEV_INFO 
    (TAG_NAME,                                      DESCRIPTION, DEV_NAME, LOCATION_ID, SUB_SYSTEM, GROUP_TAG_NAME,                   VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME)
    SELECT
    concat(locationTag,'.',tblA.DEV_GRP,'_',TEMP_NAME),DESCRIPTION,  '',      locationId, subsystemId,concat(locationTag,'.',tblA.DEV_GRP),  '0',         DEV_TYPE_ID,'0', '0',       '0',    '0',    '0',   '0',      '0',      '0',      '0',       '0',     '0',      '0',      '0',      '0',      '0',  regionId,  '',                 '0',       '0',            '0',            TEMP_NAME    
    FROM tmpImportPsc3000PluginTbl AS tblA,temp_plugin_dev_map,dev_temp_def 
    WHERE tblA.PLUGIN_NAME = temp_plugin_dev_map.PLUGIN_NAME AND temp_plugin_dev_map.TEMP_NAME = dev_temp_def.TAG_NAME;
    
    --实例化前置设备,以psc3000_dev中指定的设备为基准,模板以套件中的为准
    INSERT INTO tmpPsc3000PluginFesTempTbl(TAG_NAME,FES_TEMP,DEV_ID)
    SELECT TAG_NAME,DEV_MODEL,DEV_ID
    FROM psc3000_dev;
    
    UPDATE psc3000_dev 
    SET DEV_MODEL = IF(COALESCE(FES_TEMP_NAME,DEV_MODEL) != DEV_MODEL,COALESCE(FES_TEMP_NAME,DEV_MODEL), DEV_MODEL)
	FROM psc3000_plugin 
	WHERE DEV_ID = FES_DEV_INST_ID;
    
	delete from psc3000_dev where psc3000_dev.DEV_ID not in(select FES_DEV_INST_ID FROM psc3000_plugin);
    
	delete from tmpPsc3000PluginFesTempTbl where tmpPsc3000PluginFesTempTbl.DEV_ID not in(select FES_DEV_INST_ID FROM psc3000_plugin);
    
    INSERT INTO FES_DEV_INFO 
    (DEV_PORT,DEV_DESC,               DEV_ID,             DEV_NAME,            DEV_TP_NAME,              DEV_TYPE_ID,LOCATION_ID,RTU_NO,RTU_TAG,SUB_SYSTEM,TAG_NAME) 
    SELECT 
    psc3000_dev.DEV_PORT,psc3000_dev.DESCRIPTION, psc3000_dev.DEV_ID,psc3000_dev.TAG_NAME ,fes_dev_temp_def.TAG_NAME,DEV_TYPE_ID,locationId, rtuNo, rtuTag, subsystemId,concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    FROM psc3000_dev,fes_dev_temp_def 
    WHERE psc3000_dev.DEV_MODEL = fes_dev_temp_def.TAG_NAME;
    
    --第三步 关联
    DELETE FROM tmpImportPsc3000PluginTbl;
    --此时临时表中第一列是套件名,第二列是设备组名
    INSERT INTO tmpImportPsc3000PluginTbl (PLUGIN_NAME , DEV_GRP) 
    SELECT PLUGIN_NAME,DEV_GROUP_TAG
    FROM psc3000_plugin;
    
    --创建临时表,存储用于第五步骤的信息
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpImportPsc3000Dev(
        PLUGIN_NAME varchar(64) NOT NULL,
        DEV_TAG varchar(64) NOT NULL,
        FES_DEV_TAG varchar(64) NOT NULL
    );
    DELETE FROM tmpImportPsc3000Dev;
    
    --循环调用存储过程进行关联
    OPEN cur_1 FOR
   	SELECT DEV_GRP,temp_plugin_fes_dev_map.PLUGIN_NAME,TEMP_NAME,FES_TEMP_NAME
    FROM tmpImportPsc3000PluginTbl,temp_plugin_fes_dev_map 
    WHERE tmpImportPsc3000PluginTbl.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME;
    
    LOOP
        
        FETCH cur_1 INTO devGrpTag,pluginName,tempName,fesTempName;
        EXIT WHEN NOT FOUND; 
        fesDevTag = (SELECT psc3000_dev.TAG_NAME FROM psc3000_plugin,psc3000_dev 
                        WHERE DEV_GROUP_TAG = devGrpTag AND PLUGIN_NAME = pluginName AND FES_TEMP_NAME = fesTempName 
                        AND FES_DEV_INST_ID = DEV_ID);
        
        IF fesDevTag IS NOT NULL THEN 
        BEGIN
            CALL relationByPlugin(pluginName,concat(locationTag,'.',devGrpTag,'_',tempName), concat(rtuTag,'.',fesDevTag));
            
            INSERT INTO tmpImportPsc3000Dev(PLUGIN_NAME,DEV_TAG,FES_DEV_TAG) 
            VALUES (pluginName,concat(locationTag,'.',devGrpTag,'_',tempName), concat(rtuTag,'.',fesDevTag));
        END; 
        END IF;
       
    END LOOP;
    CLOSE cur_1;

	--更新设备组编号
	UPDATE dev_group SET DEV_GROUP_NO = psc3000_plugin.FES_DEV_INST_ID FROM psc3000_plugin WHERE dev_group.TAG_NAME = concat(locationTag,'.',psc3000_plugin.DEV_GROUP_TAG);
    
    
    --第四步 刷新前置设备的测点 规约参数和描述等信息,对于在文件中存在,而模板中不存在的测点进行添加
    CREATE TEMPORARY TABLE IF NOT EXISTS tblImportPsc3000Point(
        TAG_NAME varchar(64),
        DEV_ID decimal(10,0),
        SHORT_NAME varchar(64),
        SEQ_NO decimal(10,0)
        );
        
    CREATE TEMPORARY TABLE IF NOT EXISTS tblImportPsc3000PointForAdd(
        DEV_ID decimal(10,0),
        SHORT_NAME varchar(64),
        SEQ_NO decimal(10,0)
    );
        
    --acc
    DELETE FROM tblImportPsc3000Point;
    
    INSERT INTO tblImportPsc3000Point (TAG_NAME,DEV_ID,SHORT_NAME)
    SELECT fes_accuml.TAG_NAME,psc3000_acc.DEV_ID, psc3000_acc.TAG_NAME
    FROM fes_accuml,psc3000_dev,psc3000_acc
    WHERE fes_accuml.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_accuml.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_acc.DEV_ID
    AND getAfterLastDot(fes_accuml.FES_POINT_TP_NAME) = psc3000_acc.TAG_NAME;
    --更新存在的点
    UPDATE fes_accuml
    SET DESCRIPTION = psc3000_acc.DESCRIPTION,
    RES_PARA_INT1 = psc3000_acc.REMOTE_NO,
	SEQ_NO = psc3000_acc.REMOTE_NO,
    COEFF = psc3000_acc.RATIO,
	FES_POINT_TP_NAME = concat(getBeforeFirstDot(fes_accuml.FES_POINT_TP_NAME),'.',psc3000_acc.TAG_NAME)
	FROM tblImportPsc3000Point,psc3000_acc
	WHERE tblImportPsc3000Point.TAG_NAME = fes_accuml.TAG_NAME 
    AND tblImportPsc3000Point.DEV_ID = psc3000_acc.DEV_ID 
    AND tblImportPsc3000Point.SHORT_NAME = psc3000_acc.TAG_NAME;
    
    --添加只有CSV文件有的点
    DELETE FROM tblImportPsc3000PointForAdd;
    
    INSERT INTO tblImportPsc3000PointForAdd (DEV_ID, SHORT_NAME)
    SELECT  psc3000_acc.DEV_ID,psc3000_acc.TAG_NAME
    FROM psc3000_acc LEFT JOIN tblImportPsc3000Point 
    ON psc3000_acc.DEV_ID = tblImportPsc3000Point.DEV_ID AND psc3000_acc.TAG_NAME = tblImportPsc3000Point.SHORT_NAME 
    WHERE tblImportPsc3000Point.DEV_ID IS NULL;
    
    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(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_ACC'))),  locationId , subsystemId ,getMaxRtuDotNo(rtuTag,getFesPointType('FES_ACC')), rtuTag, concat(rtuTag,'.',psc3000_dev.TAG_NAME), psc3000_acc.REMOTE_NO,  '',             '',            '',            '0',  psc3000_acc.RATIO, rtuNo , psc3000_acc.REMOTE_NO, 0,             0,            0,             psc3000_acc.DESCRIPTION , psc3000_acc.TAG_NAME,                '',        0                             
    FROM tblImportPsc3000PointForAdd,psc3000_acc,psc3000_dev 
    WHERE tblImportPsc3000PointForAdd.DEV_ID = psc3000_acc.DEV_ID
    AND tblImportPsc3000PointForAdd.SHORT_NAME = psc3000_acc.TAG_NAME
    AND psc3000_acc.DEV_ID = psc3000_dev.DEV_ID
    ; 
    
    --ana
    DELETE FROM tblImportPsc3000Point;
    INSERT INTO tblImportPsc3000Point (TAG_NAME,DEV_ID,SHORT_NAME)
    SELECT fes_analog.TAG_NAME,psc3000_ana.DEV_ID, psc3000_ana.TAG_NAME
    FROM fes_analog,psc3000_dev,psc3000_ana
    WHERE fes_analog.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_analog.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_ana.DEV_ID
    AND getAfterLastDot(FES_POINT_TP_NAME) = psc3000_ana.TAG_NAME;
    
    UPDATE fes_analog
    SET DESCRIPTION = psc3000_ana.DESCRIPTION,
    RES_PARA_INT1 = psc3000_ana.REMOTE_NO,
	SEQ_NO = psc3000_ana.REMOTE_NO,
    COEFF = psc3000_ana.RATIO,
	FES_POINT_TP_NAME = concat(getBeforeFirstDot(fes_analog.FES_POINT_TP_NAME),'.',psc3000_ana.TAG_NAME)
	FROM tblImportPsc3000Point,psc3000_ana
	WHERE tblImportPsc3000Point.TAG_NAME = fes_analog.TAG_NAME 
    AND tblImportPsc3000Point.DEV_ID = psc3000_ana.DEV_ID 
    AND tblImportPsc3000Point.SHORT_NAME = psc3000_ana.TAG_NAME;
    
    --添加只有CSV文件有的点
    DELETE FROM tblImportPsc3000PointForAdd;
        
    INSERT INTO tblImportPsc3000PointForAdd (DEV_ID, SHORT_NAME)
    SELECT  psc3000_ana.DEV_ID,psc3000_ana.TAG_NAME
    FROM psc3000_ana LEFT JOIN tblImportPsc3000Point 
    ON psc3000_ana.DEV_ID = tblImportPsc3000Point.DEV_ID AND psc3000_ana.TAG_NAME = tblImportPsc3000Point.SHORT_NAME 
    WHERE tblImportPsc3000Point.DEV_ID IS NULL;
        
    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(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_ANA'))),  locationId , subsystemId , concat(rtuTag,'.',psc3000_dev.TAG_NAME),psc3000_ana.REMOTE_NO, rtuTag,   rtuNo,  getMaxRtuDotNo(rtuTag,getFesPointType('FES_ANA')),'',            '',            '',               '0',       '0',     '0',           '0',       '0',     '0',  psc3000_ana.RATIO,   '65536',    '0',      psc3000_ana.REMOTE_NO,  0,            0,               0,             psc3000_ana.DESCRIPTION, psc3000_ana.TAG_NAME,                 '',       0
    FROM tblImportPsc3000PointForAdd,psc3000_ana,psc3000_dev  
    WHERE tblImportPsc3000PointForAdd.DEV_ID = psc3000_ana.DEV_ID
    AND tblImportPsc3000PointForAdd.SHORT_NAME = psc3000_ana.TAG_NAME
    AND psc3000_ana.DEV_ID = psc3000_dev.DEV_ID
    ; 
    
    --dig
    DELETE FROM tblImportPsc3000Point;
    INSERT INTO tblImportPsc3000Point (TAG_NAME,DEV_ID,SHORT_NAME)
    SELECT fes_digital.TAG_NAME,psc3000_dig.DEV_ID, psc3000_dig.TAG_NAME
    FROM fes_digital,psc3000_dev,psc3000_dig
    WHERE fes_digital.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_digital.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_dig.DEV_ID
    AND getAfterLastDot(FES_POINT_TP_NAME) = psc3000_dig.TAG_NAME;
    
    UPDATE fes_digital
    SET DESCRIPTION = psc3000_dig.DESCRIPTION,
    RES_PARA_INT1 = psc3000_dig.REMOTE_NO,
	SEQ_NO = psc3000_dig.REMOTE_NO,
	FES_POINT_TP_NAME = concat(getBeforeFirstDot(fes_digital.FES_POINT_TP_NAME),'.',psc3000_dig.TAG_NAME)
	FROM tblImportPsc3000Point,psc3000_dig
	WHERE tblImportPsc3000Point.TAG_NAME = fes_digital.TAG_NAME 
    AND tblImportPsc3000Point.DEV_ID = psc3000_dig.DEV_ID 
    AND tblImportPsc3000Point.SHORT_NAME = psc3000_dig.TAG_NAME;
    
    --添加只有CSV文件有的点
    DELETE FROM tblImportPsc3000PointForAdd;
        
    INSERT INTO tblImportPsc3000PointForAdd (DEV_ID, SHORT_NAME)
    SELECT  psc3000_dig.DEV_ID,psc3000_dig.TAG_NAME
    FROM psc3000_dig LEFT JOIN tblImportPsc3000Point 
    ON psc3000_dig.DEV_ID = tblImportPsc3000Point.DEV_ID AND psc3000_dig.TAG_NAME = tblImportPsc3000Point.SHORT_NAME 
    WHERE tblImportPsc3000Point.DEV_ID IS NULL;
        
    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(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_DIG'))),  locationId , subsystemId , concat(rtuTag,'.',psc3000_dev.TAG_NAME),psc3000_dig.REMOTE_NO, rtuTag,  getMaxRtuDotNo(rtuTag,getFesPointType('FES_DIG')),'',             '',            '',             '1',        '1',            '0',           '0',     rtuNo,  psc3000_dig.REMOTE_NO, 0,            0,              0,              psc3000_dig.DESCRIPTION,psc3000_dig.TAG_NAME,                 '',        0
    FROM tblImportPsc3000PointForAdd,psc3000_dig,psc3000_dev 
    WHERE tblImportPsc3000PointForAdd.DEV_ID = psc3000_dig.DEV_ID
    AND tblImportPsc3000PointForAdd.SHORT_NAME = psc3000_dig.TAG_NAME
    AND psc3000_dig.DEV_ID = psc3000_dev.DEV_ID
    ; 
         
    --dig_ctrl
    DELETE FROM tblImportPsc3000Point;
    
    INSERT INTO tblImportPsc3000Point (TAG_NAME,DEV_ID,SHORT_NAME)
    SELECT fes_digital_ctrl.TAG_NAME,psc3000_dig_ctrl.DEV_ID, psc3000_dig_ctrl.TAG_NAME
    FROM fes_digital_ctrl,psc3000_dev,psc3000_dig_ctrl
    WHERE fes_digital_ctrl.TAG_NAME LIKE concat(rtuTag,'%')
    AND fes_digital_ctrl.DEV_TAG = concat(rtuTag,'.',psc3000_dev.TAG_NAME)
    AND psc3000_dev.DEV_ID = psc3000_dig_ctrl.DEV_ID
    AND getAfterLastDot(fes_digital_ctrl.POINT_TP_NAME) = psc3000_dig_ctrl.TAG_NAME;
    
    UPDATE fes_digital_ctrl
    SET DESCRIPTION = psc3000_dig_ctrl.DESCRIPTION,
    RES_PARA_INT1 = psc3000_dig_ctrl.REMOTE_NO,
	SEQ_NO = psc3000_dig_ctrl.REMOTE_NO,
	POINT_TP_NAME = concat(getBeforeFirstDot(fes_digital_ctrl.POINT_TP_NAME),'.',psc3000_dig_ctrl.TAG_NAME)
	FROM tblImportPsc3000Point,psc3000_dig_ctrl
	WHERE tblImportPsc3000Point.TAG_NAME = fes_digital_ctrl.TAG_NAME 
    AND tblImportPsc3000Point.DEV_ID = psc3000_dig_ctrl.DEV_ID 
    AND tblImportPsc3000Point.SHORT_NAME = psc3000_dig_ctrl.TAG_NAME;
    
    --添加只有CSV文件有的点
    DELETE FROM tblImportPsc3000PointForAdd;
        
    INSERT INTO tblImportPsc3000PointForAdd (DEV_ID, SHORT_NAME)
    SELECT  psc3000_dig_ctrl.DEV_ID,psc3000_dig_ctrl.TAG_NAME
    FROM psc3000_dig_ctrl LEFT JOIN tblImportPsc3000Point 
    ON psc3000_dig_ctrl.DEV_ID = tblImportPsc3000Point.DEV_ID AND psc3000_dig_ctrl.TAG_NAME = tblImportPsc3000Point.SHORT_NAME 
    WHERE tblImportPsc3000Point.DEV_ID IS NULL;
       
    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)SELECT   
    concat(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_DIG_CTRL'))),  locationId , subsystemId , rtuTag,  rtuNo,getMaxRtuDotNo(rtuTag,getFesPointType('FES_DIG_CTRL')), concat(rtuTag,'.',psc3000_dev.TAG_NAME),psc3000_dig_ctrl.DESCRIPTION,'0' ,   psc3000_dig_ctrl.TAG_NAME, psc3000_dig_ctrl.REMOTE_NO, psc3000_dig_ctrl.REMOTE_NO, 0,             0,              0,              '',       0,             '',             '',           '',            '',                     '',                 '',                    0
    FROM tblImportPsc3000PointForAdd,psc3000_dig_ctrl,psc3000_dev 
    WHERE tblImportPsc3000PointForAdd.DEV_ID = psc3000_dig_ctrl.DEV_ID
    AND tblImportPsc3000PointForAdd.SHORT_NAME = psc3000_dig_ctrl.TAG_NAME
    AND psc3000_dig_ctrl.DEV_ID = psc3000_dev.DEV_ID
    ; 
         
    --DZ定值
    DELETE FROM fes_const WHERE fes_const.RTU_TAG = rtuTag AND fes_const.RES_PARA_INT1 = '-1';
    
    --添加只有CSV文件有的点
    DELETE FROM tblImportPsc3000PointForAdd;
        
    INSERT INTO tblImportPsc3000PointForAdd (DEV_ID, SEQ_NO)
    SELECT  psc3000_const.DEV_ID,psc3000_const.SEQ_NO
    FROM psc3000_const LEFT JOIN tblImportPsc3000Point 
    ON psc3000_const.DEV_ID = tblImportPsc3000Point.DEV_ID AND psc3000_const.SEQ_NO = tblImportPsc3000Point.SEQ_NO 
    WHERE tblImportPsc3000Point.DEV_ID IS NULL;
       
    INSERT INTO fes_const(
    TAG_NAME,                                                                      DESCRIPTION,               DZ_SEQ,                        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(rtuTag, '.' , getMaxRtuDotNo(rtuTag,getFesPointType('FES_CONST'))),  psc3000_const.DESCRIPTION ,psc3000_const.DZ_SEQ  ,psc3000_const.GROUP_NO ,        rtuTag,  rtuNo,   getMaxRtuDotNo(rtuTag,getFesPointType('FES_CONST')),  psc3000_const.SEQ_NO,subsystemId,locationId,  concat(rtuTag,'.',psc3000_dev.TAG_NAME), 0,      0,            0,       psc3000_const.VALUE_TEXT,psc3000_const.RATIO,psc3000_const.REVISE, 0,          0,         0,               0,              '',       '',                  '',          0
    FROM tblImportPsc3000PointForAdd,psc3000_const,psc3000_dev 
    WHERE tblImportPsc3000PointForAdd.DEV_ID = psc3000_const.DEV_ID
    AND tblImportPsc3000PointForAdd.SEQ_NO = psc3000_const.SEQ_NO
    AND psc3000_const.DEV_ID = psc3000_dev.DEV_ID
    ; 
   
    
    --第五步,对于此时设备中未关联的前置测点,放到间隔信号中 tmpImportPsc3000Dev
    DELETE FROM tmpImportPsc3000Dev WHERE DEV_TAG NOT LIKE '%jgxh%';
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tblPsc3000PointToAdd(
        DEV_TAG varchar(64),
        TAG_NAME varchar(64), 
        FES_TAG_NAME varchar(64),
        DOT_NO decimal(10,0),
        DESCRIPTION varchar(128)
    );
   
    
    --acc,
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT tmpImportPsc3000Dev.DEV_TAG,
    concat(tmpImportPsc3000Dev.DEV_TAG,'.', IF(getAfterLastDot(fes_accuml.FES_POINT_TP_NAME) = '',findPsc3000Tag(fes_accuml.DESCRIPTION,fes_accuml.RES_PARA_INT1,fes_dev_info.DEV_ID,'psc_acc'),getAfterLastDot(fes_accuml.FES_POINT_TP_NAME) )) ,
    fes_accuml.TAG_NAME,fes_accuml.DOT_NO,fes_accuml.DESCRIPTION
    FROM tmpImportPsc3000Dev,fes_dev_info,fes_accuml
    WHERE tmpImportPsc3000Dev.FES_DEV_TAG = fes_dev_info.TAG_NAME AND 
    fes_accuml.TAG_NAME LIKE  concat(rtuTag,'.%') AND 
    fes_accuml.DEV_TAG = fes_dev_info.TAG_NAME AND 
    fes_accuml.APP_TABLE_NAME = '' AND
    fes_accuml.APP_TAG_NAME = '' AND
    fes_accuml.APP_COLUMN_NAME = '';
    
    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   
    tblPsc3000PointToAdd.TAG_NAME, tblPsc3000PointToAdd.DESCRIPTION,dev_info.TAG_NAME,getMaxSeq(dev_info.TAG_NAME,'accuml')+1, locationId,  subsystemId, '',     dev_info.GRAY, dev_info.CONTRAST, dev_info.QCOLOR, '0',      dev_info.VALUE, dev_info.STATUS, 0,                0,                '1',     '',          '',          '',         regionId,  dev_info.OPT_HANDOVER_GROUP, '0',      '3',           '0',         '0',            '201',     dev_info.DEV_TYPE, '',            '0',        '0',           '5',           '0.01',         '',       ''
    FROM tblPsc3000PointToAdd,dev_info 
    WHERE tblPsc3000PointToAdd.DEV_TAG = dev_info.TAG_NAME ;
    
    update accuml
    SET RTU_TAG = rtuTag
    from tblpsc3000pointtoadd
    WHERE accuml.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    update accuml_map_to_fes
    SET DOT_NO = tblPsc3000PointToAdd.DOT_NO
    from tblpsc3000pointtoadd
    WHERE accuml_map_to_fes.KEY_ID_TAG = concat('accuml.',tblPsc3000PointToAdd.TAG_NAME);

    
    
    --ana
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT tmpImportPsc3000Dev.DEV_TAG,
    concat(tmpImportPsc3000Dev.DEV_TAG,'.', IF(getAfterLastDot(fes_analog.FES_POINT_TP_NAME) = '',findPsc3000Tag(fes_analog.DESCRIPTION,fes_analog.RES_PARA_INT1,fes_dev_info.DEV_ID,'psc_ana'),getAfterLastDot(fes_analog.FES_POINT_TP_NAME) )) ,
    fes_analog.TAG_NAME,fes_analog.DOT_NO,fes_analog.DESCRIPTION
    FROM tmpImportPsc3000Dev,fes_dev_info,fes_analog
    WHERE tmpImportPsc3000Dev.FES_DEV_TAG = fes_dev_info.TAG_NAME AND 
    fes_analog.TAG_NAME LIKE  concat(rtuTag,'.%') AND 
    fes_analog.DEV_TAG = fes_dev_info.TAG_NAME AND 
    fes_analog.APP_TABLE_NAME = '' AND
    fes_analog.APP_TAG_NAME = '' AND
    fes_analog.APP_COLUMN_NAME = '' ;
    
    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   
    tblPsc3000PointToAdd.TAG_NAME, tblPsc3000PointToAdd.DESCRIPTION,'',      dev_info.DEV_TYPE,dev_info.TAG_NAME,getMaxSeq(dev_info.TAG_NAME,'analog')+1, locationId,  subsystemId, '0',         dev_info.GRAY, dev_info.CONTRAST, dev_info.QCOLOR,dev_info.VALUE, dev_info.STATUS, 0,                0,                '1',     '',          '',          '',         regionId,  dev_info.OPT_HANDOVER_GROUP, '0',        '0',          '0',             '0',               '1',          '',            '0',          '0',             '5',              '0.01',            '',       ''
    FROM tblPsc3000PointToAdd,dev_info 
    WHERE tblPsc3000PointToAdd.DEV_TAG = dev_info.TAG_NAME ;
    
    update analog
    SET RTU_TAG = rtuTag
    from tblpsc3000pointtoadd
    WHERE analog.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    update analog_map_to_fes
    SET DOT_NO = tblPsc3000PointToAdd.DOT_NO
    from tblpsc3000pointtoadd
    WHERE analog_map_to_fes.KEY_ID_TAG = concat('analog.',tblPsc3000PointToAdd.TAG_NAME);

    
    --dig
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT tmpImportPsc3000Dev.DEV_TAG,
    concat(tmpImportPsc3000Dev.DEV_TAG,'.', IF(getAfterLastDot(fes_digital.FES_POINT_TP_NAME) = '',findPsc3000Tag(fes_digital.DESCRIPTION,fes_digital.RES_PARA_INT1,fes_dev_info.DEV_ID,'psc_dig'),getAfterLastDot(fes_digital.FES_POINT_TP_NAME) )) ,
    fes_digital.TAG_NAME,fes_digital.DOT_NO,fes_digital.DESCRIPTION
    FROM tmpImportPsc3000Dev,fes_dev_info,fes_digital
    WHERE tmpImportPsc3000Dev.FES_DEV_TAG = fes_dev_info.TAG_NAME AND 
    fes_digital.TAG_NAME LIKE  concat(rtuTag,'.%') AND 
    fes_digital.DEV_TAG = fes_dev_info.TAG_NAME AND 
    fes_digital.APP_TABLE_NAME = '' AND
    fes_digital.APP_TAG_NAME = '' AND
    fes_digital.APP_COLUMN_NAME = '';
    
    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   
    tblPsc3000PointToAdd.TAG_NAME, tblPsc3000PointToAdd.DESCRIPTION,dev_info.TAG_NAME,getMaxSeq(dev_info.TAG_NAME,'digital')+1, locationId,  subsystemId, '',     '0',            '',    '1',       dev_info.GRAY, dev_info.CONTRAST, dev_info.QCOLOR,dev_info.VALUE, dev_info.STATUS, 0,                0,       0,       '0',              0,       0,       0,                0,       0,       0,                0,       0,       0,                0,       0,       0,                 '1',            '',        '',         '',         '',           '5',             '1',               '0',           '1',            '0',              '0',                regionId,  dev_info.OPT_HANDOVER_GROUP, '0',        '0',            '0',           dev_info.DEV_TYPE, '',            '1',          '0',            '5',             '0',             '10',           '计次报警描述',       '0',                '5',         '计时报警描述',        '30',               '',          '1',          '0',          '0',                '400',       '',         ''
    FROM tblPsc3000PointToAdd,dev_info 
    WHERE tblPsc3000PointToAdd.DEV_TAG = dev_info.TAG_NAME ;
    
    update digital
    SET RTU_TAG = rtuTag
    from tblpsc3000pointtoadd
    WHERE digital.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    update digital_map_to_fes
    SET DOT_NO1 = tblPsc3000PointToAdd.DOT_NO
    from tblpsc3000pointtoadd
    WHERE digital_map_to_fes.KEY_ID_TAG = concat('digital.',tblPsc3000PointToAdd.TAG_NAME);

   
    
    
    --mix 混合量在psc3000中是没有的,这里只做简单处理
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT tmpImportPsc3000Dev.DEV_TAG,
    concat(tmpImportPsc3000Dev.DEV_TAG,'.', getAfterLastDot(fes_mix.FES_POINT_TP_NAME)) ,
    fes_mix.TAG_NAME,fes_mix.DOT_NO,fes_mix.DESCRIPTION
    FROM tmpImportPsc3000Dev,fes_dev_info,fes_mix
    WHERE tmpImportPsc3000Dev.FES_DEV_TAG = fes_dev_info.TAG_NAME AND 
    fes_mix.TAG_NAME LIKE  concat(rtuTag,'.%') AND 
    fes_mix.DEV_TAG = fes_dev_info.TAG_NAME AND 
    fes_mix.APP_TABLE_NAME = '' AND
    fes_mix.APP_TAG_NAME = '' AND
    fes_mix.APP_COLUMN_NAME = '';
    
    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   
    tblPsc3000PointToAdd.TAG_NAME,tblPsc3000PointToAdd.DESCRIPTION,dev_info.TAG_NAME,getMaxSeq(dev_info.TAG_NAME,'mix')+1,locationId,   subsystemId, '',     '0',        '', GRAY, CONTRAST, QCOLOR, '0',       VALUE, STATUS, '0',              '0',             '',      '3',               '',       '',         '',          '',           0,                1,           0,             0,              0,                   regionId,  OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TYPE, '',            0,          0,              5,             2,           0,            0,              701,         '',      ''
    FROM tblPsc3000PointToAdd,dev_info 
    WHERE tblPsc3000PointToAdd.DEV_TAG = dev_info.TAG_NAME ;
    
    update mix
    SET RTU_TAG = rtuTag
    from tblpsc3000pointtoadd
    WHERE mix.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    update mix_map_to_fes
    SET DOT_NO = tblPsc3000PointToAdd.DOT_NO
    from tblpsc3000pointtoadd
    WHERE mix_map_to_fes.KEY_ID_TAG = concat('mix.',tblPsc3000PointToAdd.TAG_NAME);

   
    --第六步,应施建全和王重生的要求,对于后台生成的设备组中未关联的测点,删除,对于前置中不存在与csv文件的点删除
    DELETE FROM tblPsc3000PointToAdd;
    DELETE FROM tmpImportPsc3000PluginTbl;
    
    --删除后台的未关联的点
    --这里PLUGIN_NAME是设备TAG
    INSERT INTO tmpImportPsc3000PluginTbl 
    (PLUGIN_NAME , DEV_GRP ) 
    SELECT  
    dev_info.TAG_NAME,dev_info.GROUP_TAG_NAME
    FROM psc3000_plugin INNER JOIN dev_info 
    ON dev_info.GROUP_TAG_NAME = concat(locationTag,'.',psc3000_plugin.DEV_GROUP_TAG);
    
    DELETE FROM accuml 
	USING tmpImportPsc3000PluginTbl 
	WHERE accuml.DEVICE = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    AND COALESCE(accuml.RTU_TAG,'') = '';
    
    DELETE FROM analog 
	USING tmpImportPsc3000PluginTbl 
	WHERE analog.DEVICE = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    AND COALESCE(analog.RTU_TAG,'') = '';
    
    DELETE FROM digital 
	USING tmpImportPsc3000PluginTbl 
	WHERE digital.DEVICE = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    AND COALESCE(digital.RTU_TAG,'') = '';
    
    DELETE FROM mix 
	USING tmpImportPsc3000PluginTbl 
	WHERE mix.DEVICE = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    AND COALESCE(mix.RTU_TAG,'') = '';
    
    
    --删除前置,有可能前置的测点已经和后台的关联上了,所以这里对后台的测点再删除一遍,再删除前置的
    --acc
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '', fes_accuml.APP_TAG_NAME,fes_accuml.TAG_NAME,0,fes_accuml.DESCRIPTION
    FROM fes_accuml WHERE fes_accuml.RTU_TAG = rtuTag AND  fes_accuml.RES_PARA_INT1 = '-1';
    
    DELETE FROM accuml 
	USING tblPsc3000PointToAdd 
	WHERE accuml.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    DELETE FROM fes_accuml 
	USING tblPsc3000PointToAdd 
	WHERE fes_accuml.TAG_NAME = tblPsc3000PointToAdd.FES_TAG_NAME;
    
    --ana
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '', fes_analog.APP_TAG_NAME,fes_analog.TAG_NAME,0,fes_analog.DESCRIPTION
    FROM fes_analog WHERE fes_analog.RTU_TAG = rtuTag AND  fes_analog.RES_PARA_INT1 = '-1';
    
    DELETE FROM analog 
	USING tblPsc3000PointToAdd 
	WHERE analog.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    DELETE FROM fes_analog 
	USING tblPsc3000PointToAdd 
	WHERE fes_analog.TAG_NAME = tblPsc3000PointToAdd.FES_TAG_NAME;
    
    
    --dig 要考虑五个分量,只要有一个分量是明确关联前置的,不能删除
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT fes_digital.APP_COLUMN_NAME,  fes_digital.APP_TAG_NAME, fes_digital.TAG_NAME, 0, fes_digital.DESCRIPTION
    FROM fes_digital WHERE fes_digital.RTU_TAG = rtuTag AND  fes_digital.RES_PARA_INT1 = '-1';
    
    --特殊处理,先删除前置使得map_to_fes表置-1再判断删除
    DELETE FROM fes_digital 
	USING tblPsc3000PointToAdd 
	WHERE fes_digital.TAG_NAME = tblPsc3000PointToAdd.FES_TAG_NAME;
    
    --先标记要删除的点
    update tblpsc3000pointtoadd
    SET DOT_NO = -1
    from digital_map_to_fes
    WHERE concat('digital.',tblPsc3000PointToAdd.TAG_NAME) = digital_map_to_fes.KEY_ID_TAG AND digital_map_to_fes.DOT_NO1 = '-1' AND digital_map_to_fes.DOT_NO2 = '-1' AND digital_map_to_fes.DOT_NO3 = '-1' AND digital_map_to_fes.DOT_NO4 = '-1' AND digital_map_to_fes.DOT_NO5 = '-1';

    
    DELETE FROM digital 
	USING tblPsc3000PointToAdd 
    WHERE digital.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    AND tblPsc3000PointToAdd.DOT_NO = -1;
    
    --mix
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '', fes_mix.APP_TAG_NAME,fes_mix.TAG_NAME,0,fes_mix.DESCRIPTION
    FROM fes_mix WHERE fes_mix.RTU_TAG = rtuTag AND  fes_mix.RES_PARA_INT1 = '-1';
    
    DELETE FROM mix 
	USING tblPsc3000PointToAdd 
	WHERE mix.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    DELETE FROM fes_mix 
	USING tblPsc3000PointToAdd 
	WHERE fes_mix.TAG_NAME = tblPsc3000PointToAdd.FES_TAG_NAME;
    
    --ana_ctrl
    DELETE FROM tblPsc3000PointToAdd;
    
    DELETE FROM fes_analog_ctrl WHERE fes_analog_ctrl.RTU_TAG = rtuTag AND fes_analog_ctrl.RES_PARA_INT1 = '-1';
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',A.TAG_NAME,'','0','' 
    FROM 
    (SELECT TAG_NAME,RTU_TAG,OFFSET_NO FROM analog_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON analog_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%') ) AS A
    LEFT JOIN fes_analog_ctrl ON fes_analog_ctrl.TAG_NAME = concat(A.RTU_TAG,'.',A.OFFSET_NO)
    WHERE fes_analog_ctrl.TAG_NAME IS NULL;
    
    DELETE FROM analog_control 
	USING tblPsc3000PointToAdd 
	WHERE analog_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    
    --dig_ctrl 要考虑五个分量
    DELETE FROM tblPsc3000PointToAdd;
    
    DELETE FROM fes_digital_ctrl WHERE fes_digital_ctrl.RTU_TAG = rtuTag AND fes_digital_ctrl.RES_PARA_INT1 = '-1';
    
    --第一个分量
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',A.TAG_NAME,'','0','' 
    FROM 
    (SELECT TAG_NAME,RTU_TAG,OFFSET_NO1 FROM digital_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON digital_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%') ) AS A
    LEFT JOIN fes_digital_ctrl ON fes_digital_ctrl.TAG_NAME = concat(A.RTU_TAG,'.',A.OFFSET_NO1)
    WHERE fes_digital_ctrl.TAG_NAME IS NULL;
    
    update digital_control
    SET OFFSET_NO1 = -1
    from tblpsc3000pointtoadd
    WHERE digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    --第二个分量
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',A.TAG_NAME,'','0','' 
    FROM 
    (SELECT TAG_NAME,RTU_TAG,OFFSET_NO2 FROM digital_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON digital_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%') ) AS A
    LEFT JOIN fes_digital_ctrl ON fes_digital_ctrl.TAG_NAME = concat(A.RTU_TAG,'.',A.OFFSET_NO2)
    WHERE fes_digital_ctrl.TAG_NAME IS NULL;
    
    update digital_control
    SET OFFSET_NO2 = -1
    from tblpsc3000pointtoadd
    WHERE digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    --第三个分量
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',A.TAG_NAME,'','0','' 
    FROM 
    (SELECT TAG_NAME,RTU_TAG,OFFSET_NO3 FROM digital_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON digital_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%') ) AS A
    LEFT JOIN fes_digital_ctrl ON fes_digital_ctrl.TAG_NAME = concat(A.RTU_TAG,'.',A.OFFSET_NO3)
    WHERE fes_digital_ctrl.TAG_NAME IS NULL;
    
    update digital_control
    SET OFFSET_NO3 = -1
    from tblpsc3000pointtoadd
    WHERE digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    --第四个分量
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',A.TAG_NAME,'','0','' 
    FROM 
    (SELECT TAG_NAME,RTU_TAG,OFFSET_NO4 FROM digital_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON digital_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%') ) AS A
    LEFT JOIN fes_digital_ctrl ON fes_digital_ctrl.TAG_NAME = concat(A.RTU_TAG,'.',A.OFFSET_NO4)
    WHERE fes_digital_ctrl.TAG_NAME IS NULL;
    
    update digital_control
    SET OFFSET_NO4 = -1
    from tblpsc3000pointtoadd
    WHERE digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    --第五个分量
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',A.TAG_NAME,'','0','' 
    FROM 
    (SELECT TAG_NAME,RTU_TAG,OFFSET_NO5 FROM digital_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON digital_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%') ) AS A
    LEFT JOIN fes_digital_ctrl ON fes_digital_ctrl.TAG_NAME = concat(A.RTU_TAG,'.',A.OFFSET_NO5)
    WHERE fes_digital_ctrl.TAG_NAME IS NULL;
    
    update digital_control
    SET OFFSET_NO5 = -1
    from tblpsc3000pointtoadd
    WHERE digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;

    
    --设置未关联的digital控制属性为否
		CREATE TEMPORARY TABLE IF NOT EXISTS tmpDelete(
        TAG_NAME varchar(64) NOT NULL
    );
    DELETE FROM tmpDelete;
	INSERT INTO tmpDelete(TAG_NAME)
	SELECT TAG_NAME FROM digital_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON digital_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%')
    AND digital_control.OFFSET_NO1 = '-1'
    AND digital_control.OFFSET_NO2 = '-1'
    AND digital_control.OFFSET_NO3 = '-1'
    AND digital_control.OFFSET_NO4 = '-1'
    AND digital_control.OFFSET_NO5 = '-1'
    ;
	UPDATE digital set IS_CONTROL = 0 WHERE TAG_NAME IN
	(SELECT TAG_NAME FROM tmpDelete)
	;
    
    --mix_ctrl
    DELETE FROM tblPsc3000PointToAdd;
    
    DELETE FROM fes_mix_ctrl WHERE fes_mix_ctrl.RTU_TAG = rtuTag AND fes_mix_ctrl.RES_PARA_INT1 = '-1';
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',A.TAG_NAME,'','0','' 
    FROM 
    (SELECT TAG_NAME,RTU_TAG,OFFSET_NO FROM mix_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON mix_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%') ) AS A
    LEFT JOIN fes_mix_ctrl ON fes_mix_ctrl.TAG_NAME = concat(A.RTU_TAG,'.',A.OFFSET_NO)
    WHERE fes_mix_ctrl.TAG_NAME IS NULL;
    
    DELETE FROM mix_control 
	USING tblPsc3000PointToAdd 
	WHERE mix_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    --第七步,修改单分量属性和双分量属性
    --单分量属性
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',digital_map_to_fes.KEY_ID_TAG,'','0','' 
    FROM  digital_map_to_fes INNER JOIN tmpImportPsc3000PluginTbl 
    ON DOT_NO1 != '-1' AND DOT_NO2 = '-1' AND DOT_NO3 = '-1' AND  DOT_NO4 = '-1' AND DOT_NO5 = '-1'
    AND digital_map_to_fes.KEY_ID_TAG LIKE concat('digital.', tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%')
    ;
    
    update digital
    SET VALUE_NUM = 1, STATE_TEXT_NAME = IF(position('(' IN STATE_TEXT_NAME) = 0,STATE_TEXT_NAME,left(STATE_TEXT_NAME,position('(' IN STATE_TEXT_NAME) - 1)::varchar)
    from tblpsc3000pointtoadd
    where concat('digital.',digital.tag_name) = tblpsc3000pointtoadd.tag_name;

    
    --双分量属性
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',digital_map_to_fes.KEY_ID_TAG,'','0','' 
    FROM  digital_map_to_fes INNER JOIN tmpImportPsc3000PluginTbl 
    ON DOT_NO1 != '-1' AND DOT_NO2 != '-1' AND DOT_NO3 = '-1' AND  DOT_NO4 = '-1' AND DOT_NO5 = '-1'
    AND digital_map_to_fes.KEY_ID_TAG LIKE concat('digital.', tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%')
    ;
    
    update digital
    SET VALUE_NUM = 2
    from tblpsc3000pointtoadd
    where concat('digital.',digital.tag_name) = tblpsc3000pointtoadd.tag_name;

    
    --对于单控制
    
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',digital_control.TAG_NAME,'','0','' 
    FROM  digital_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON OFFSET_NO1 != '-1' AND OFFSET_NO2 = '-1' AND OFFSET_NO3 = '-1' AND  OFFSET_NO4 = '-1' AND OFFSET_NO5 = '-1'
    AND digital_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%')
    ;
    
    update digital_control
    SET OFFSET_NUM = 1
    from tblpsc3000pointtoadd
    where digital_control.tag_name = tblpsc3000pointtoadd.tag_name;

    
    
    --对于双控制
    DELETE FROM tblPsc3000PointToAdd;
    
    INSERT INTO tblPsc3000PointToAdd(DEV_TAG,TAG_NAME,FES_TAG_NAME,DOT_NO,DESCRIPTION)
    SELECT '',digital_control.TAG_NAME,'','0','' 
    FROM  digital_control INNER JOIN tmpImportPsc3000PluginTbl 
    ON OFFSET_NO1 != '-1' AND OFFSET_NO2 != '-1' AND OFFSET_NO3 = '-1' AND  OFFSET_NO4 = '-1' AND OFFSET_NO5 = '-1'
    AND digital_control.TAG_NAME LIKE concat(tmpImportPsc3000PluginTbl.PLUGIN_NAME,'.%')
    ;
    
    update digital_control
    SET OFFSET_NUM = 2
    from tblpsc3000pointtoadd
    where digital_control.tag_name = tblpsc3000pointtoadd.tag_name;

    
    --*****************************赵勇新增功能(给后台添加控制点与前置未关联的控制点进行关联)******************************
	--临时表 1:前置标签名，2：前置点号，3：后台标签名, 4:后台点描述
	CREATE TABLE IF NOT EXISTS tmpRelationControlTbl(
    FES_TAG_NAME varchar(64),
		DOT_NO decimal(10,0),
        TAG_NAME varchar(64),
		DESCRIPTION varchar(128),
		DEVICE varchar(64)
    );
	DELETE FROM tmpRelationControlTbl;
	--digital_control 先给 digital中添加点,设置控制属性为是，再关联digital_control中的点
	INSERT INTO tmpRelationControlTbl(FES_TAG_NAME,DOT_NO,TAG_NAME,DESCRIPTION,DEVICE)
	SELECT FES_DIGITAL_CTRL.TAG_NAME,FES_DIGITAL_CTRL.DOT_NO,CONCAT(locationTag,'.',DEV_GROUP_TAG,'_jgxh','.',psc3000_dig_ctrl.TAG_NAME),psc3000_dig_ctrl.DESCRIPTION,CONCAT(locationTag,'.',DEV_GROUP_TAG,'_jgxh') FROM FES_DIGITAL_CTRL
	LEFT JOIN psc3000_dig_ctrl on RES_PARA_INT1 = REMOTE_NO
	LEFT JOIN psc3000_plugin on psc3000_dig_ctrl.DEV_ID = FES_DEV_INST_ID 
	WHERE fes_digital_ctrl.RTU_TAG = rtuTag 
	and fes_digital_ctrl.DOT_NO::varchar not in
	(SELECT OFFSET_NO1 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO1 <> '-1') 
	and fes_digital_ctrl.DOT_NO::varchar not in
	(SELECT OFFSET_NO2 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO2 <> '-1') 
	and fes_digital_ctrl.DOT_NO::varchar not in
	(SELECT OFFSET_NO3 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO3 <> '-1') 
	and fes_digital_ctrl.DOT_NO::varchar not in
	(SELECT OFFSET_NO4 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO4 <> '-1') 
	and fes_digital_ctrl.DOT_NO::varchar not in
	(SELECT OFFSET_NO5 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO5 <> '-1')
	;
		
	--执行插入操作
	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   
    tmpRelationControlTbl.TAG_NAME, tmpRelationControlTbl.DESCRIPTION,dev_info.TAG_NAME,getMaxSeq(dev_info.TAG_NAME,'digital')+1, locationId,  subsystemId, rtuTag,     '1',            '',    '1',       dev_info.GRAY, dev_info.CONTRAST, dev_info.QCOLOR,dev_info.VALUE, dev_info.STATUS, 0,                0,       0,       '0',              0,       0,       0,                0,       0,       0,                0,       0,       0,                0,       0,       0,                 '1',            '',        '',         '',         '',           '5',             '1',               '0',           '1',            '0',              '0',                regionId,  dev_info.OPT_HANDOVER_GROUP, '0',        '0',            '0',           dev_info.DEV_TYPE, '',            '0',          '0',            '5',             '0',             '10',           '计次报警描述',       '0',                '5',         '计时报警描述',        '30',               '',          '1',          '0',          '0',                '300',       '',         ''
    FROM tmpRelationControlTbl,dev_info 
    WHERE tmpRelationControlTbl.DEVICE = dev_info.TAG_NAME ;
	--关联digital_control
    update digital_control
    SET OFFSET_NUM = 1,OFFSET_NO1 = tmpRelationControlTbl.DOT_NO
    from tmprelationcontroltbl
    where digital_control.tag_name = tmprelationcontroltbl.tag_name;

	--analog_control
		
	--*****************************赵勇添加结束****************************************************************************
    
	--第八步,删除空设备,临时表tmpImportPsc3000PluginTbl中存储设备信息
    update tmpimportpsc3000plugintbl
    SET DEV_GRP = ''
    from digital
    where tmpimportpsc3000plugintbl.plugin_name = digital.device;

    
    update tmpimportpsc3000plugintbl
    SET DEV_GRP = ''
    from accuml
    where tmpimportpsc3000plugintbl.plugin_name = accuml.device;

    
    update tmpimportpsc3000plugintbl
    SET DEV_GRP = ''
    from analog
    where tmpimportpsc3000plugintbl.plugin_name = analog.device;

    
    update tmpimportpsc3000plugintbl
    SET DEV_GRP = ''
    from mix
    where tmpimportpsc3000plugintbl.plugin_name = mix.device;

    
    --此时空设备的DEV_GRP不为空,删除
    DELETE FROM dev_info 
	USING tmpImportPsc3000PluginTbl 
	WHERE dev_info.TAG_NAME = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    AND tmpImportPsc3000PluginTbl.DEV_GRP != '';
    
    --第九步,要求自动同步描述
    /*SET hasDone = 0;
    OPEN cur_2;
    
    LOOP
        
        FETCH cur_2 INTO myDevTag;
        EXIT WHEN NOT FOUND;

        IF myDevTag IS NOT NULL THEN 
        BEGIN
            CALL updateDescByFes(myDevTag);
        END; 
        END IF;
         
        
    END LOOP;
    CLOSE cur_2;*/
    
    --第十步，先删除保护定值和数据块，再导入模板里的保护定值和数据块,此时tmpPsc3000PluginFesTempTbl表中是导入的前置设备
    DELETE FROM fes_data_block 
	USING tmpPsc3000PluginFesTempTbl 
	WHERE fes_data_block.DEV_TAG = concat(rtuTag,'.',tmpPsc3000PluginFesTempTbl.TAG_NAME);
    
    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) SELECT   
    concat(rtuTag,'.',tmpPsc3000PluginFesTempTbl.TAG_NAME,'.',getAfterLastDot(fes_data_block_temp.TAG_NAME)), BLOCK_ID,rtuTag, rtuNo,  FUNC_CODE,STAR_ADDR,DATA_LEN, DATA_FARME_TYPE, REF_CYCLE, IS_CREATE_SOE, PARA_INT1, PARA_INT2, DESCRIPTION,locationId,  subsystemId,LD, CALL_MODE, DATA_SET,DATA_SET_TYPE,DATA_SET_CALL_TIME,REPORT_CTRL,'',               concat(rtuTag,'.',tmpPsc3000PluginFesTempTbl.TAG_NAME)
    FROM tmpPsc3000PluginFesTempTbl INNER JOIN fes_data_block_temp ON tmpPsc3000PluginFesTempTbl.FES_TEMP = fes_data_block_temp.DEV_TP_NAME;
    
    --第十一步,修改PLC的设备ID,超过一万的要修改
    UPDATE fes_dev_info SET DEV_ID = IF(DIV(DEV_ID,10000) = 0,DEV_ID,DIV(DEV_ID,10000)) WHERE RTU_TAG = rtuTag;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION InsertForwardAcc(rtuTagName varchar(64), startNum decimal(10,0))
RETURNS VOID
AS $$
    DECLARE countNum int DEFAULT 0;
BEGIN
    --使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    --批量插入前置表,
    --rtuTagName 指明插入哪个转发RTU
    --startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    SELECT count(*) INTO countNum FROM export_seq; 
    
    --相当于对要空出要插入的位置
    UPDATE fes_forwarding_accuml 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum;
    
    INSERT INTO fes_forwarding_accuml 
    (RTU_TAG, ID,              REMOTE_NO,      TAG_NAME, DESCRIPTION, SHORT_NAME, FES_RTU_TAG, FES_RTU_NO, FES_DOT_NO, GROUP_NO, COEFF, BASE, PROPERTY, PARALONG_1, PARALONG_2, PARASTRING)
    SELECT
    rtuTagName,export_seq.NO,export_seq.NO,TAG_NAME, DESCRIPTION, '',         RTU_TAG,     RTU_NO,      DOT_NO,    '0',      '0',   '0',  '0',      '0',        '0',        ''  
    FROM export_seq,fes_accuml WHERE export_seq.DEV_TAG = fes_accuml.TAG_NAME;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION InsertForwardAna(rtuTagName varchar(64), startNum decimal(10,0))
RETURNS VOID
AS $$
    DECLARE countNum int DEFAULT 0;
BEGIN
    --使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    --批量插入前置表,
    --rtuTagName 指明插入哪个转发RTU
    --startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    SELECT count(*) INTO countNum FROM export_seq; 
    
    --相当于对要空出要插入的位置
    UPDATE fes_forwarding_anaolg 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum;
    
    INSERT INTO fes_forwarding_anaolg 
    (RTU_TAG, ID,               REMOTE_NO,     TAG_NAME, DESCRIPTION, SHORT_NAME, FES_RTU_TAG, FES_RTU_NO, FES_DOT_NO, GROUP_NO, COEFF, BASE, DEADBAND, PROPERTY, PARALONG_1, PARALONG_2, PARASTRING)
    SELECT
    rtuTagName,export_seq.NO,export_seq.NO,TAG_NAME, DESCRIPTION, '',         RTU_TAG,     RTU_NO,      DOT_NO,    '0',      '0',   '0',  '0',      '0',        '0',        '0',      ''  
    FROM export_seq,fes_analog WHERE export_seq.DEV_TAG = fes_analog.TAG_NAME;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION InsertForwardAnaCtrl(rtuTagName varchar(64), startNum decimal(10,0))
RETURNS VOID
AS $$
    DECLARE countNum int DEFAULT 0;
BEGIN
    --使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    --批量插入前置表,
    --rtuTagName 指明插入哪个转发RTU
    --startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    SELECT count(*) INTO countNum FROM export_seq; 
    
    --相当于对要空出要插入的位置
    UPDATE fes_forwarding_anaolg_ctrl 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum;
    
    INSERT INTO fes_forwarding_anaolg_ctrl 
    (RTU_TAG,  ID,             REMOTE_NO,      TAG_NAME, DESCRIPTION, SHORT_NAME, FES_RTU_TAG, FES_RTU_NO, FES_DOT_NO, GROUP_NO, PROPERTY, PARALONG_1, PARALONG_2, PARASTRING)
    SELECT
    rtuTagName,export_seq.NO,export_seq.NO,TAG_NAME, DESCRIPTION, '',         RTU_TAG,     RTU_NO,      DOT_NO,    '0',      '0',       '0',        '0',        ''  
    FROM export_seq,fes_analog_ctrl WHERE export_seq.DEV_TAG = fes_analog_ctrl.TAG_NAME;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION InsertForwardDig(rtuTagName varchar(64), startNum decimal(10,0))
RETURNS VOID
AS $$
    DECLARE countNum int DEFAULT 0;
BEGIN
    --使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    --批量插入前置表,
    --rtuTagName 指明插入哪个转发RTU
    --startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    SELECT count(*) INTO countNum FROM export_seq; 
    
    --相当于对要空出要插入的位置
    UPDATE fes_forwarding_digital 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum;
    
    INSERT INTO fes_forwarding_digital 
    (RTU_TAG, ID,              REMOTE_NO,      TAG_NAME, DESCRIPTION, SHORT_NAME, FES_RTU_TAG, FES_RTU_NO, FES_DOT_NO, GROUP_NO, REVERT, PROPERTY, PARALONG_1, PARALONG_2, PARASTRING)
    SELECT
    rtuTagName,export_seq.NO,export_seq.NO,TAG_NAME, DESCRIPTION, '',         RTU_TAG,     RTU_NO,      DOT_NO,    '0',      '0',   '0',        '0',        '0',        ''  
    FROM export_seq,fes_digital WHERE export_seq.DEV_TAG = fes_digital.TAG_NAME;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION InsertForwardDigCtrl(rtuTagName varchar(64), startNum decimal(10,0))
RETURNS VOID
AS $$
    DECLARE countNum int DEFAULT 0;
BEGIN
    --使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    --批量插入前置表,
    --rtuTagName 指明插入哪个转发RTU
    --startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    SELECT count(*) INTO countNum FROM export_seq; 
    
    --相当于对要空出要插入的位置
    UPDATE fes_forwarding_digital_ctrl 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum;
    
    INSERT INTO fes_forwarding_digital_ctrl 
    (RTU_TAG,   ID,            REMOTE_NO,     TAG_NAME, DESCRIPTION, SHORT_NAME, FES_RTU_TAG, FES_RTU_NO, FES_DOT_NO, GROUP_NO, PROPERTY, PARALONG_1, PARALONG_2, PARASTRING)
    SELECT
    rtuTagName,export_seq.NO,export_seq.NO,TAG_NAME, DESCRIPTION, '',         RTU_TAG,     RTU_NO,      DOT_NO,    '0',      '0',      '0',        '0',        ''  
    FROM export_seq,fes_digital_ctrl WHERE export_seq.DEV_TAG = fes_digital_ctrl.TAG_NAME;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION instDevTempGroup(srcTempGroupName varchar(64), dstDevGroupName varchar(64))
RETURNS VOID
AS $$
    DECLARE locationTagName varchar(64) DEFAULT '';
   -- DECLARE totalInst int DEFAULT 1;
BEGIN
    /*
     * 实例化整个模板组
     */
    
    IF COALESCE(srcTempGroupName,'') = '' OR COALESCE(dstDevGroupName,'')=''
    THEN RETURN;
    END IF;
   
    SELECT sys_model_location_info.TAG_NAME INTO locationTagName 
    FROM dev_group,sys_model_location_info 
    WHERE 
    dev_group.TAG_NAME = dstDevGroupName AND 
    dev_group.LOCATION_ID = sys_model_location_info.LOCATION_ID;
    
    IF COALESCE(locationTagName,'') = ''
    THEN RETURN;
    END IF;
    /*
    SELECT count(*) INTO totalInst FROM dev_info WHERE TAG_NAME LIKE '%Xinst%';
    SET totalInst = totalInst + 1;
    */
    
    INSERT INTO dev_info(
    TAG_NAME,                                                                             DESCRIPTION,              LOCATION_ID,           SUB_SYSTEM,           GROUP_TAG_NAME, VOL_LEVEL_ID, DEV_TYPE,   GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME)  SELECT      
    concat(locationTagName,'.',getAfterLastDot(dstDevGroupName), dev_temp_def.TAG_NAME), dev_temp_def.DESCRIPTION, dev_group.LOCATION_ID, dev_group.SUB_SYSTEM, dstDevGroupName,'0',          DEV_TYPE_ID, '0', '0',      '0',    '0',   '0',    '0',      '0',      '0',      '0',      '0',      '0',       '0',     '0',      '0',      '0',       REGION_ID, '',                 '0',         '0',           '0',           dev_temp_def.TAG_NAME
    FROM dev_group,dev_temp_group,dev_temp_def  
    WHERE 
    dev_group.TAG_NAME = dstDevGroupName AND
    dev_temp_group.TAG_NAME = srcTempGroupName AND
    dev_temp_group.TAG_NAME = dev_temp_def.DEV_TEMP_GRP;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION instPlugin(pluginName varchar(64),rtuTag varchar(64),devGroupName varchar(64), isBatchInGrp decimal(10,0),startNo decimal(10,0),countNo decimal(10,0))
RETURNS VOID
AS $$
	DECLARE locationId decimal(10,0) DEFAULT 0;
    DECLARE subsystemId decimal(10,0) DEFAULT 0;
    DECLARE rtuNo decimal(10,0) DEFAULT 0;
    DECLARE regionId decimal(10,0) DEFAULT 0;
    DECLARE devGrpDesc varchar(128) DEFAULT '';
    DECLARE tempName varchar(64) DEFAULT '';
    DECLARE fesTempName varchar(64) DEFAULT '';
    DECLARE sampleGrpName varchar(64) DEFAULT '';
    DECLARE stepCount int DEFAULT 0;
	DECLARE fesDevCount int DEFAULT 0;
	DECLARE cur_relation CURSOR FOR 
    SELECT TEMP_NAME,FES_TEMP_NAME FROM temp_plugin_fes_dev_map WHERE PLUGIN_NAME = pluginName;

BEGIN
    --先实例化后台和前置设备,再关联
    --pluginName  套件名
    --rtuTag rtu名
    --devGroupName 设备组名
    --isBatchInGrp 是否在组内批量实例化
    --startNo 批量化的起始序号
    --countNo 批量的数量
    
    --前置的命名规则更后台要有一一对应,批量时有编号,不批量时无编号
    --后台是设备组_模板名+编号
    --前置也是设备组_模板+编号
    
    SELECT LOCATION_ID,SUB_SYSTEM,RTU_NO INTO locationId,subsystemId,rtuNo FROM fes_rtu_para WHERE TAG_NAME = rtuTag;
    SELECT REGION_ID,DESCRIPTION INTO regionId,devGrpDesc FROM dev_group WHERE TAG_NAME = devGroupName;
    sampleGrpName = getAfterLastDot(devGroupName);
	fesDevCount = (SELECT COUNT(*) FROM fes_dev_info WHERE RTU_NO = rtuNo);
    
    --不批量时
    IF isBatchInGrp <> 1 THEN
    BEGIN
        --实例化后台设备,注意TAG_NAME的命名规则
        INSERT INTO DEV_INFO 
        (TAG_NAME,                         DESCRIPTION, DEV_NAME, LOCATION_ID, SUB_SYSTEM, GROUP_TAG_NAME, VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME)
        SELECT
        concat(devGroupName,'_',TEMP_NAME),DESCRIPTION,'',        locationId, subsystemId,devGroupName,    '0',        DEV_TYPE_ID,'0', '0',       '0',    '0',    '0',   '0',      '0',      '0',      '0',       '0',     '0',      '0',      '0',      '0',      '0',       regionId,  '',                 '0',       '0',            '0',            TEMP_NAME    
        FROM temp_plugin_dev_map,dev_temp_def WHERE PLUGIN_NAME = pluginName AND temp_plugin_dev_map.TEMP_NAME = dev_temp_def.TAG_NAME;
        --实例化前置设备
        INSERT INTO FES_DEV_INFO 
        (DEV_DESC,                         DEV_ID,DEV_NAME,                                DEV_TP_NAME,  DEV_TYPE_ID,LOCATION_ID,RTU_NO,RTU_TAG,SUB_SYSTEM,TAG_NAME) 
        SELECT 
        concat(devGrpDesc,'_',DESCRIPTION), fesDevCount, concat(sampleGrpName,'_',FES_TEMP_NAME) ,FES_TEMP_NAME,DEV_TYPE_ID,locationId, rtuNo, rtuTag, subsystemId,concat(rtuTag,'.',sampleGrpName,'_',FES_TEMP_NAME)
        FROM temp_plugin_fes_dev_map, fes_dev_temp_def WHERE PLUGIN_NAME = pluginName AND temp_plugin_fes_dev_map.FES_TEMP_NAME = fes_dev_temp_def.TAG_NAME
        GROUP BY temp_plugin_fes_dev_map.FES_TEMP_NAME,DESCRIPTION,DEV_TYPE_ID;
        
        --循环调用存储过程进行关联
        OPEN cur_relation;
        LOOP
            FETCH cur_relation INTO tempName,fesTempName;
            EXIT WHEN NOT FOUND; 
            CALL relationByPlugin(pluginName,concat(devGroupName,'_',tempName), concat(rtuTag,'.',sampleGrpName,'_',fesTempName));
        END LOOP;
       CLOSE cur_relation;
    END;
    ELSE
    BEGIN
        --批量时,设备都带上编号
        --实例化后台设备,注意TAG_NAME的命名规则
        stepCount = startNo;
        WHILE stepCount < (startNo + countNo) LOOP
            INSERT INTO DEV_INFO 
            (TAG_NAME,                                   DESCRIPTION,                  DEV_NAME, LOCATION_ID, SUB_SYSTEM, GROUP_TAG_NAME, VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME)
            SELECT
            concat(devGroupName,'_',TEMP_NAME,stepCount),concat(DESCRIPTION,stepCount),'',        locationId, subsystemId,devGroupName,    '0',        DEV_TYPE_ID,'0', '0',       '0',    '0',    '0',   '0',      '0',      '0',      '0',       '0',     '0',      '0',      '0',      '0',      '0',       regionId,  '',                 '0',       '0',            '0',            TEMP_NAME    
            FROM temp_plugin_dev_map,dev_temp_def WHERE PLUGIN_NAME = pluginName AND temp_plugin_dev_map.TEMP_NAME = dev_temp_def.TAG_NAME;
            --实例化前置设备
            INSERT INTO FES_DEV_INFO 
            (DEV_DESC,                                   DEV_ID,DEV_NAME,                                          DEV_TP_NAME,  DEV_TYPE_ID,LOCATION_ID,RTU_NO,RTU_TAG,SUB_SYSTEM,TAG_NAME) 
            SELECT 
            concat(devGrpDesc,'_',DESCRIPTION,stepCount),fesDevCount, concat(sampleGrpName,'_',FES_TEMP_NAME,stepCount) ,FES_TEMP_NAME,DEV_TYPE_ID,locationId, rtuNo, rtuTag, subsystemId,concat(rtuTag,'.',sampleGrpName,'_',FES_TEMP_NAME,stepCount)
            FROM temp_plugin_fes_dev_map, fes_dev_temp_def WHERE PLUGIN_NAME = pluginName AND temp_plugin_fes_dev_map.FES_TEMP_NAME = fes_dev_temp_def.TAG_NAME
            GROUP BY temp_plugin_fes_dev_map.FES_TEMP_NAME,DESCRIPTION,DEV_TYPE_ID;
            
            --循环调用存储过程进行关联
            OPEN cur_relation;
            LOOP
                FETCH cur_relation INTO tempName,fesTempName;
                EXIT WHEN NOT FOUND;
                CALL relationByPlugin(pluginName,concat(devGroupName,'_',tempName,stepCount), concat(rtuTag,'.',sampleGrpName,'_',fesTempName,stepCount));
            END LOOP;
        
            CLOSE cur_relation;  
        
            stepCount = stepCount + 1;
        END LOOP;
        
    END; 
    END IF;
    
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION instPluginByFesDev(pluginName varchar(64),rtuTag varchar(64),devGroupName varchar(64), fesDevTag varchar(64),fesDevDes varchar(64))
RETURNS VOID
AS $$
	DECLARE locationId decimal(10,0) DEFAULT 0;
    DECLARE subsystemId decimal(10,0) DEFAULT 0;
    DECLARE rtuNo decimal(10,0) DEFAULT 0;
    DECLARE regionId decimal(10,0) DEFAULT 0;
    DECLARE devGrpDesc varchar(128) DEFAULT '';
    DECLARE tempName varchar(64) DEFAULT '';
    DECLARE sampleGrpName varchar(64) DEFAULT '';
    DECLARE fesDevCount int DEFAULT 0;
    DECLARE cur_relation CURSOR FOR 
    SELECT TEMP_NAME FROM temp_plugin_fes_dev_map WHERE PLUGIN_NAME = pluginName;
BEGIN
    --先实例化后台和前置设备,再关联
    --pluginName  套件名
    --rtuTag rtu名
    --devGroupName 设备组名
    --fesDevTag 前置设备组标签
    --fesDevDes 前置设备组描述
    
    
    
    SELECT LOCATION_ID,SUB_SYSTEM,RTU_NO INTO locationId,subsystemId,rtuNo FROM fes_rtu_para WHERE TAG_NAME = rtuTag;
    SELECT REGION_ID,DESCRIPTION INTO regionId,devGrpDesc FROM dev_group WHERE TAG_NAME = devGroupName;
    sampleGrpName = getAfterLastDot(devGroupName);
    fesDevCount = (SELECT COUNT(*) FROM fes_dev_info WHERE RTU_NO = rtuNo);
    
    BEGIN
        --实例化后台设备,注意TAG_NAME的命名规则
        INSERT INTO DEV_INFO 
        (TAG_NAME,                         DESCRIPTION, DEV_NAME, LOCATION_ID, SUB_SYSTEM, GROUP_TAG_NAME, VOL_LEVEL_ID, DEV_TYPE, GRAY, CONTRAST, QCOLOR, VALUE, STATUS, NODE_NO1, NODE_NO2, NODE_NO3, NODE_NO4, NODE_NO5, NODE_NO6, NODE_NO7, NODE_NO8, NODE_NO9, NODE_NO10, REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TP_NAME)
        SELECT
        concat(devGroupName,'_',TEMP_NAME),DESCRIPTION,'',        locationId, subsystemId,devGroupName,    '0',        DEV_TYPE_ID,'0', '0',       '0',    '0',    '0',   '0',      '0',      '0',      '0',       '0',     '0',      '0',      '0',      '0',      '0',       regionId,  '',                 '0',       '0',            '0',            TEMP_NAME    
        FROM temp_plugin_dev_map,dev_temp_def WHERE PLUGIN_NAME = pluginName AND temp_plugin_dev_map.TEMP_NAME = dev_temp_def.TAG_NAME;
        --实例化前置设备
        INSERT INTO FES_DEV_INFO 
        (DEV_DESC, DEV_ID,       DEV_NAME, DEV_TP_NAME, DEV_TYPE_ID,LOCATION_ID,RTU_NO,RTU_TAG,SUB_SYSTEM,TAG_NAME) 
        SELECT 
        fesDevDes, fesDevCount, fesDevTag,FES_TEMP_NAME,DEV_TYPE_ID,locationId, rtuNo, rtuTag, subsystemId,concat(rtuTag,'.',fesDevTag)
        FROM temp_plugin_fes_dev_map, fes_dev_temp_def WHERE PLUGIN_NAME = pluginName AND temp_plugin_fes_dev_map.FES_TEMP_NAME = fes_dev_temp_def.TAG_NAME
        GROUP BY temp_plugin_fes_dev_map.FES_TEMP_NAME,DEV_TYPE_ID;
        
        --循环调用存储过程进行关联
        OPEN cur_relation;

        LOOP
            FETCH cur_relation INTO tempName;
				      EXIT WHEN NOT FOUND; 
            CALL relationByPlugin(pluginName,concat(devGroupName,'_',tempName), concat(rtuTag,'.',fesDevTag));
        END LOOP;
        
        CLOSE cur_relation;
    END; 
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION relationByDevGroup(groupName varchar(64))
RETURNS VOID
AS $$
BEGIN
    /*
     * 后台和前置相同设备组之间进行关联
     */
    
    --创建临时表,分别存入map_to_fes的Key_ID_TAG,对应的RTU_TAG,和DOT_NO, OFFSET_NO对应的是digital有5个分量的情况
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpRelationGroupTbl(
        KEY_ID_TAG varchar(128) NOT NULL,
        RTU_TAG varchar(64) NOT NULL,
        DOT_NO varchar(48) NOT NULL,
        OFFSET_NO decimal(10)
    );
    DELETE FROM tmpRelationGroupTbl;
    --以accuml为例,进行说明.把后台设备下的所有accuml点,和对应的前置设备下的fes_accuml点的Key_ID_TAG和DOT_NO写入临时表
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.RTU_TAG, tb2.DOT_NO 
    FROM
    (SELECT accuml_map_to_fes.KEY_ID_TAG,accuml_temp_define.SEC_DEVTP_NAME,accuml_temp_define.SEC_DEV_IN_GRP,accuml_temp_define.SEC_SEQ_NO
    FROM dev_info, accuml, accuml_map_to_fes,accuml_temp_define
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        accuml.DEVICE = dev_info.TAG_NAME AND 
        accuml_map_to_fes.KEY_ID_TAG = concat('accuml','.' ,accuml.TAG_NAME) AND  
        accuml_map_to_fes.POINT_TP_NAME = accuml_temp_define.TAG_NAME AND 
        COALESCE(accuml_temp_define.SEC_SEQ_NO,-1) >= 0 
    )tb1 INNER JOIN 
    (SELECT fes_accuml.RTU_TAG,fes_accuml.DOT_NO,fes_accuml.FES_POINT_TP_NAME,fes_accuml.SEQ_NO,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info, fes_accuml
    WHERE
        fes_dev_info.DEV_GROUP = groupName AND
        fes_accuml.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
       tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.FES_POINT_TP_NAME) AND 
       tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP AND
       tb1.SEC_SEQ_NO = tb2.SEQ_NO;
    --假如前置点已经被占用,先清除
    update fes_accuml
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationgrouptbl
    where fes_accuml.TAG_NAME = concat(tmpRelationGroupTbl.RTU_TAG,'.', tmpRelationGroupTbl.DOT_NO);

    
    --清除后台的
    update accuml
    SET RTU_TAG = ''
    from dev_info
    WHERE dev_info.GROUP_TAG_NAME = groupName AND accuml.DEVICE = dev_info.TAG_NAME;

    
    --模拟手动操作,先关联对应RTU,再关联对应点
    update accuml
    SET RTU_TAG = tmpRelationGroupTbl.RTU_TAG
    from tmprelationgrouptbl
    WHERE accuml.TAG_NAME = getAfterFirstDot(tmpRelationGroupTbl.KEY_ID_TAG);

    
    update accuml_map_to_fes
    SET DOT_NO = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG ) t1
    WHERE accuml_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG;

    
    --analog
    DELETE FROM tmpRelationGroupTbl;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.RTU_TAG, tb2.DOT_NO 
    FROM
    (SELECT analog_map_to_fes.KEY_ID_TAG,analog_temp_define.SEC_DEVTP_NAME,analog_temp_define.SEC_DEV_IN_GRP,analog_temp_define.SEC_SEQ_NO
    FROM dev_info,analog, analog_map_to_fes,analog_temp_define
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        analog.DEVICE = dev_info.TAG_NAME AND 
        analog_map_to_fes.KEY_ID_TAG = concat('analog','.' ,analog.TAG_NAME) AND 
        analog_map_to_fes.POINT_TP_NAME = analog_temp_define.TAG_NAME AND 
        COALESCE(analog_temp_define.SEC_SEQ_NO,-1) >= 0 
    )tb1 INNER JOIN 
    (SELECT fes_analog.RTU_TAG,fes_analog.DOT_NO,fes_analog.FES_POINT_TP_NAME,fes_analog.SEQ_NO,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info, fes_analog
    WHERE
        fes_dev_info.DEV_GROUP = groupName AND
        fes_analog.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
       tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.FES_POINT_TP_NAME) AND 
       tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP AND
       tb1.SEC_SEQ_NO = tb2.SEQ_NO;
  
    --假如前置点已经被占用,先清除
    update fes_analog
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationgrouptbl
    where fes_analog.TAG_NAME = concat(tmpRelationGroupTbl.RTU_TAG,'.', tmpRelationGroupTbl.DOT_NO);

    --清除后台的
    update analog
    SET RTU_TAG = ''
    from dev_info
    WHERE dev_info.GROUP_TAG_NAME = groupName AND analog.DEVICE = dev_info.TAG_NAME;

    
    update analog
    SET RTU_TAG = tmpRelationGroupTbl.RTU_TAG
    from tmprelationgrouptbl
    WHERE analog.TAG_NAME = getAfterFirstDot(tmpRelationGroupTbl.KEY_ID_TAG);

    
    update analog_map_to_fes
    SET DOT_NO = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG ) t1
    WHERE analog_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG;

    
    --DIGITAL
    
    DELETE FROM  tmpRelationGroupTbl;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.RTU_TAG, tb2.DOT_NO ,1
    FROM
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP,digital_temp_define.SEC_SEQ_NO1
    FROM dev_info,digital, digital_map_to_fes,digital_temp_define
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,digital.TAG_NAME) AND 
        digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.SEC_SEQ_NO1,-1) >= 0 
    )tb1 INNER JOIN 
    (SELECT fes_digital.RTU_TAG,fes_digital.DOT_NO,fes_digital.FES_POINT_TP_NAME,fes_digital.SEQ_NO,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info, fes_digital
    WHERE
        fes_dev_info.DEV_GROUP = groupName AND
        fes_digital.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
       tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.FES_POINT_TP_NAME) AND 
       tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP AND
       tb1.SEC_SEQ_NO1 = tb2.SEQ_NO;
       
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.RTU_TAG, tb2.DOT_NO ,2
    FROM
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP,digital_temp_define.SEC_SEQ_NO2
    FROM dev_info,digital, digital_map_to_fes,digital_temp_define
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,digital.TAG_NAME) AND 
        digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.SEC_SEQ_NO2,-1) >= 0 
    )tb1 INNER JOIN 
    (SELECT fes_digital.RTU_TAG,fes_digital.DOT_NO,fes_digital.FES_POINT_TP_NAME,fes_digital.SEQ_NO,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info, fes_digital
    WHERE
        fes_dev_info.DEV_GROUP = groupName AND
        fes_digital.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
       tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.FES_POINT_TP_NAME) AND 
       tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP AND
       tb1.SEC_SEQ_NO2 = tb2.SEQ_NO;
       
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.RTU_TAG, tb2.DOT_NO ,3
    FROM
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP,digital_temp_define.SEC_SEQ_NO3
    FROM dev_info,digital, digital_map_to_fes,digital_temp_define
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,digital.TAG_NAME) AND  
        digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.SEC_SEQ_NO3,-1) >= 0 
    )tb1 INNER JOIN 
    (SELECT fes_digital.RTU_TAG,fes_digital.DOT_NO,fes_digital.FES_POINT_TP_NAME,fes_digital.SEQ_NO,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info, fes_digital
    WHERE
        fes_dev_info.DEV_GROUP = groupName AND
        fes_digital.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
       tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.FES_POINT_TP_NAME) AND 
       tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP AND
       tb1.SEC_SEQ_NO3 = tb2.SEQ_NO;
       
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.RTU_TAG, tb2.DOT_NO ,4
    FROM
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP,digital_temp_define.SEC_SEQ_NO4
    FROM dev_info,digital, digital_map_to_fes,digital_temp_define
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,digital.TAG_NAME) AND  
        digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.SEC_SEQ_NO4,-1) >= 0 
    )tb1 INNER JOIN 
    (SELECT fes_digital.RTU_TAG,fes_digital.DOT_NO,fes_digital.FES_POINT_TP_NAME,fes_digital.SEQ_NO,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info, fes_digital
    WHERE
        fes_dev_info.DEV_GROUP = groupName AND
        fes_digital.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
       tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.FES_POINT_TP_NAME) AND 
       tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP AND
       tb1.SEC_SEQ_NO4 = tb2.SEQ_NO;
       
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.RTU_TAG, tb2.DOT_NO ,5
    FROM
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP,digital_temp_define.SEC_SEQ_NO5
    FROM dev_info,digital, digital_map_to_fes,digital_temp_define
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_map_to_fes.KEY_ID_TAG = concat('digital','.' ,digital.TAG_NAME) AND  
        digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.SEC_SEQ_NO5,-1) >= 0 
    )tb1 INNER JOIN 
    (SELECT fes_digital.RTU_TAG,fes_digital.DOT_NO,fes_digital.FES_POINT_TP_NAME,fes_digital.SEQ_NO,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info, fes_digital
    WHERE
        fes_dev_info.DEV_GROUP = groupName AND
        fes_digital.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
       tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.FES_POINT_TP_NAME) AND 
       tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP AND
       tb1.SEC_SEQ_NO5 = tb2.SEQ_NO;
    
   
   --假如前置点已经被占用,先清除
    update fes_digital
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationgrouptbl
    where fes_digital.TAG_NAME = concat(tmpRelationGroupTbl.RTU_TAG,'.', tmpRelationGroupTbl.DOT_NO);

    --清除后台的
    update digital
    SET RTU_TAG = ''
    from dev_info
    WHERE dev_info.GROUP_TAG_NAME = groupName AND digital.DEVICE = dev_info.TAG_NAME;

    
    update digital
    SET RTU_TAG = tmpRelationGroupTbl.RTU_TAG
    from tmprelationgrouptbl
    WHERE digital.TAG_NAME = getAfterFirstDot(tmpRelationGroupTbl.KEY_ID_TAG);

    
    update digital_map_to_fes
    SET DOT_NO1 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 1;

    
    update digital_map_to_fes
    SET DOT_NO2 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 2;

    
    update digital_map_to_fes
    SET DOT_NO3 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 3;

    
    update digital_map_to_fes
    SET DOT_NO4 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 4;

    
    update digital_map_to_fes
    SET DOT_NO5 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 5;

    
    --mix
    DELETE FROM  tmpRelationGroupTbl;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.RTU_TAG, tb2.DOT_NO 
    FROM
    (SELECT mix_map_to_fes.KEY_ID_TAG,mix_temp_define.SEC_DEVTP_NAME,mix_temp_define.SEC_DEV_IN_GRP,mix_temp_define.SEC_SEQ_NO
    FROM dev_info,mix, mix_map_to_fes,mix_temp_define
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        mix.DEVICE = dev_info.TAG_NAME AND 
        mix_map_to_fes.KEY_ID_TAG = concat('mix','.' ,mix.TAG_NAME) AND  
        mix_map_to_fes.POINT_TP_NAME = mix_temp_define.TAG_NAME AND 
        COALESCE(mix_temp_define.SEC_SEQ_NO,-1) >= 0 
    )tb1 INNER JOIN 
    (SELECT fes_mix.RTU_TAG,fes_mix.DOT_NO,fes_mix.FES_POINT_TP_NAME,fes_mix.SEQ_NO,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info, fes_mix
    WHERE
        fes_dev_info.DEV_GROUP = groupName AND
        fes_mix.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
       tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.FES_POINT_TP_NAME) AND 
       tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP AND
       tb1.SEC_SEQ_NO = tb2.SEQ_NO;
        
   
    --假如前置点已经被占用,先清除
    update fes_mix
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationgrouptbl
    where fes_mix.TAG_NAME = concat(tmpRelationGroupTbl.RTU_TAG,'.', tmpRelationGroupTbl.DOT_NO);

    --清除后台的
    update mix
    SET RTU_TAG = ''
    from dev_info
    WHERE dev_info.GROUP_TAG_NAME = groupName AND mix.DEVICE = dev_info.TAG_NAME;

    
    update mix
    SET RTU_TAG = tmpRelationGroupTbl.RTU_TAG
    from tmprelationgrouptbl
    WHERE mix.TAG_NAME = getAfterFirstDot(tmpRelationGroupTbl.KEY_ID_TAG);

    
    update mix_map_to_fes
    SET DOT_NO = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG ) t1
    WHERE mix_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG;

    
    --控制点的关联,因为有控制点,肯定有前置点,所以控制表Ctrl中的RTU在上面已经填入了,这里只需要填写控制的点号
    --analog_ctrl
    DELETE FROM  tmpRelationGroupTbl;
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO ) 
    SELECT tb1.TAG_NAME,tb2.RTU_TAG, tb2.DOT_NO  
    FROM 
    (SELECT analog_control.TAG_NAME,analog_temp_define.OUT_SEC_SEQ_NO ,analog_temp_define.SEC_DEVTP_NAME,analog_temp_define.SEC_DEV_IN_GRP
    FROM  dev_info,analog, analog_control,analog_temp_define 
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        analog.DEVICE = dev_info.TAG_NAME AND 
        analog_control.TAG_NAME = analog.TAG_NAME AND     
        analog_control.POINT_TP_NAME= analog_temp_define.TAG_NAME AND 
        COALESCE(analog_temp_define.OUT_SEC_SEQ_NO,-1) >= 0 
    )tb1 
    INNER JOIN 
    (SELECT fes_analog_ctrl.DOT_NO,fes_analog_ctrl.RTU_TAG , fes_analog_ctrl.SEQ_NO ,fes_analog_ctrl.POINT_TP_NAME,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info,fes_analog_ctrl 
    WHERE 
        fes_dev_info.DEV_GROUP = groupName AND 
        fes_analog_ctrl.DEV_TAG = fes_dev_info.TAG_NAME 
    )tb2
    ON 
    tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.POINT_TP_NAME) AND 
    tb1.OUT_SEC_SEQ_NO = tb2.SEQ_NO AND
    tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP;
   
    update analog_control
    SET OFFSET_NO = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG ) t1
    WHERE analog_control.TAG_NAME = t1.KEY_ID_TAG;

    
    --digital_Ctrl
    DELETE FROM  tmpRelationGroupTbl;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME,tb2.RTU_TAG, tb2.DOT_NO  ,1
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO1 ,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP
    FROM  dev_info,digital, digital_control,digital_temp_define 
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_control.TAG_NAME = digital.TAG_NAME AND     
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO1,-1) >= 0 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO,fes_digital_ctrl.RTU_TAG, fes_digital_ctrl.SEQ_NO ,fes_digital_ctrl.POINT_TP_NAME,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info,fes_digital_ctrl 
    WHERE 
        fes_dev_info.DEV_GROUP = groupName AND 
        fes_digital_ctrl.DEV_TAG = fes_dev_info.TAG_NAME
    )tb2
    ON 
    tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.POINT_TP_NAME) AND 
    tb1.OUT_SEC_SEQ_NO1 = tb2.SEQ_NO AND
    tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME,tb2.RTU_TAG, tb2.DOT_NO  ,2
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO2 ,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP
    FROM  dev_info,digital, digital_control,digital_temp_define 
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_control.TAG_NAME = digital.TAG_NAME AND        
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO2,-1) >= 0 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO, fes_digital_ctrl.RTU_TAG,fes_digital_ctrl.SEQ_NO ,fes_digital_ctrl.POINT_TP_NAME,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info,fes_digital_ctrl 
    WHERE 
        fes_dev_info.DEV_GROUP = groupName AND 
        fes_digital_ctrl.DEV_TAG = fes_dev_info.TAG_NAME
    )tb2
    ON 
    tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.POINT_TP_NAME) AND 
    tb1.OUT_SEC_SEQ_NO2 = tb2.SEQ_NO AND
    tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME,tb2.RTU_TAG, tb2.DOT_NO  ,3
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO3 ,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP
    FROM  dev_info,digital, digital_control,digital_temp_define 
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_control.TAG_NAME = digital.TAG_NAME AND      
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO3,-1) >= 0 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO,fes_digital_ctrl.RTU_TAG, fes_digital_ctrl.SEQ_NO ,fes_digital_ctrl.POINT_TP_NAME,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info,fes_digital_ctrl 
    WHERE 
        fes_dev_info.DEV_GROUP = groupName AND 
        fes_digital_ctrl.DEV_TAG = fes_dev_info.TAG_NAME
    )tb2
    ON 
    tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.POINT_TP_NAME) AND 
    tb1.OUT_SEC_SEQ_NO3 = tb2.SEQ_NO AND
    tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME,tb2.RTU_TAG, tb2.DOT_NO  ,4
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO4 ,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP
    FROM  dev_info,digital,digital_control,digital_temp_define 
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_control.TAG_NAME = digital.TAG_NAME AND       
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO4,-1) >= 0 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO,fes_digital_ctrl.RTU_TAG, fes_digital_ctrl.SEQ_NO ,fes_digital_ctrl.POINT_TP_NAME,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info,fes_digital_ctrl 
    WHERE 
        fes_dev_info.DEV_GROUP = groupName AND 
        fes_digital_ctrl.DEV_TAG = fes_dev_info.TAG_NAME
    )tb2
    ON 
    tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.POINT_TP_NAME) AND 
    tb1.OUT_SEC_SEQ_NO4 = tb2.SEQ_NO AND
    tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME,tb2.RTU_TAG, tb2.DOT_NO  ,5
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO5 ,digital_temp_define.SEC_DEVTP_NAME,digital_temp_define.SEC_DEV_IN_GRP
    FROM  dev_info,digital,digital_control,digital_temp_define 
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        digital.DEVICE = dev_info.TAG_NAME AND 
        digital_control.TAG_NAME = digital.TAG_NAME AND        
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO5,-1) >= 0 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO,fes_digital_ctrl.RTU_TAG, fes_digital_ctrl.SEQ_NO ,fes_digital_ctrl.POINT_TP_NAME,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info,fes_digital_ctrl 
    WHERE 
        fes_dev_info.DEV_GROUP = groupName AND 
        fes_digital_ctrl.DEV_TAG = fes_dev_info.TAG_NAME
    )tb2
    ON 
    tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.POINT_TP_NAME) AND 
    tb1.OUT_SEC_SEQ_NO5 = tb2.SEQ_NO AND
    tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP;
    
    update digital_control
    SET OFFSET_NO1 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 1;

    
    update digital_control
    SET OFFSET_NO2 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 2;

    
    update digital_control
    SET OFFSET_NO3 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 3;

    
    update digital_control
    SET OFFSET_NO4 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 4;

    
    update digital_control
    SET OFFSET_NO5 = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG,OFFSET_NO ) t1
    WHERE digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 5;

    
    --mix_ctrl
    DELETE FROM  tmpRelationGroupTbl;
    
    INSERT INTO tmpRelationGroupTbl (KEY_ID_TAG ,RTU_TAG, DOT_NO ) 
    SELECT tb1.TAG_NAME, tb2.RTU_TAG,tb2.DOT_NO  
    FROM 
    (SELECT mix_control.TAG_NAME,mix_temp_define.OUT_SEC_SEQ_NO ,mix_temp_define.SEC_DEVTP_NAME,mix_temp_define.SEC_DEV_IN_GRP
    FROM  dev_info,mix, mix_control,mix_temp_define 
    WHERE 
        dev_info.GROUP_TAG_NAME = groupName AND 
        mix.DEVICE = dev_info.TAG_NAME AND 
        mix_control.TAG_NAME = mix.TAG_NAME AND        
        mix_control.POINT_TP_NAME= mix_temp_define.TAG_NAME AND 
        COALESCE(mix_temp_define.OUT_SEC_SEQ_NO,-1) >= 0 
    )tb1 
    INNER JOIN 
    (SELECT fes_mix_ctrl.DOT_NO,fes_mix_ctrl.RTU_TAG, fes_mix_ctrl.SEQ_NO ,fes_mix_ctrl.POINT_TP_NAME,fes_dev_info.NO_IN_DEV_GRP
    FROM fes_dev_info,fes_mix_ctrl 
    WHERE 
        fes_dev_info.DEV_GROUP = groupName AND 
        fes_mix_ctrl.DEV_TAG = fes_dev_info.TAG_NAME
    )tb2
    ON 
    tb1.SEC_DEVTP_NAME = getBeforeLastDot(tb2.POINT_TP_NAME) AND 
    tb1.OUT_SEC_SEQ_NO = tb2.SEQ_NO AND
    tb1.SEC_DEV_IN_GRP = tb2.NO_IN_DEV_GRP;
  
    update mix_control
    SET OFFSET_NO = t1.DOT_NO
    from (SELECT KEY_ID_TAG,DOT_NO FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO,KEY_ID_TAG ) t1
    WHERE mix_control.TAG_NAME = t1.KEY_ID_TAG;

    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION relationFesByName(devTag varchar(64),fesDevTag varchar(64))
RETURNS VOID
AS $$
    DECLARE rtuTag varchar(64);
BEGIN
    /*
     * 后台和FES的两个设备之间名称相同的测点做关联
     */
    
    IF COALESCE(devTag,'') = '' OR COALESCE(fesDevTag,'')=''
    THEN RETURN;
    END IF;
    
    SELECT RTU_TAG INTO rtuTag FROM fes_dev_info WHERE TAG_NAME = fesDevTag;
    
    IF COALESCE(rtuTag,'') = '' 
    THEN RETURN;
    END IF;
   
    --首先清空本设备的关联
    CALL bingRtu(devTag,'');
    UPDATE fes_accuml SET APP_COLUMN_NAME = '',APP_TABLE_NAME = '' ,APP_TAG_NAME = '' WHERE DEV_TAG = fesDevTag;
    UPDATE fes_analog SET APP_COLUMN_NAME = '',APP_TABLE_NAME = '' ,APP_TAG_NAME = '' WHERE DEV_TAG = fesDevTag;
    UPDATE fes_digital SET APP_COLUMN_NAME = '',APP_TABLE_NAME = '' ,APP_TAG_NAME = '' WHERE DEV_TAG = fesDevTag;
    UPDATE fes_mix SET APP_COLUMN_NAME = '',APP_TABLE_NAME = '' ,APP_TAG_NAME = '' WHERE DEV_TAG = fesDevTag;
    
    --关联acc
    UPDATE accuml SET RTU_TAG = rtuTag WHERE DEVICE = devTag;
    UPDATE analog SET RTU_TAG = rtuTag WHERE DEVICE = devTag;
    UPDATE digital SET RTU_TAG = rtuTag WHERE DEVICE = devTag;
    UPDATE mix SET RTU_TAG = rtuTag WHERE DEVICE = devTag;
    
    
    --创建临时表
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpRelationFesByNameTbl(
        KEY_ID_TAG varchar(128) NOT NULL,
        DOT_NO varchar(48) NOT NULL
    );
    
    --acc
    DELETE FROM tmpRelationFesByNameTbl;
    
    INSERT INTO tmpRelationFesByNameTbl(KEY_ID_TAG,DOT_NO)
    SELECT accuml_map_to_fes.KEY_ID_TAG,fes_accuml.DOT_NO
    FROM accuml_map_to_fes INNER JOIN fes_accuml 
    ON  accuml_map_to_fes.KEY_ID_TAG LIKE concat('accuml.',devTag,'.%')
    AND fes_accuml.TAG_NAME  LIKE concat(rtuTag,'.%')
    AND fes_accuml.DEV_TAG = fesDevTag
    AND getAfterLastDot(accuml_map_to_fes.KEY_ID_TAG) = getAfterLastDot(fes_accuml.FES_POINT_TP_NAME);
    
    update accuml_map_to_fes
    SET RTU_TAG = rtuTag,DOT_NO = tmpRelationFesByNameTbl.DOT_NO
    from tmprelationfesbynametbl
    WHERE accuml_map_to_fes.KEY_ID_TAG = tmpRelationFesByNameTbl.KEY_ID_TAG;

    
    --ana
    DELETE FROM tmpRelationFesByNameTbl;
    
    INSERT INTO tmpRelationFesByNameTbl(KEY_ID_TAG,DOT_NO)
    SELECT analog_map_to_fes.KEY_ID_TAG,fes_analog.DOT_NO
    FROM analog_map_to_fes INNER JOIN fes_analog 
    ON  analog_map_to_fes.KEY_ID_TAG LIKE concat('analog.',devTag,'.%')
    AND fes_analog.TAG_NAME  LIKE concat(rtuTag,'.%')
    AND fes_analog.DEV_TAG = fesDevTag
    AND getAfterLastDot(analog_map_to_fes.KEY_ID_TAG) = getAfterLastDot(fes_analog.FES_POINT_TP_NAME);
    
    update analog_map_to_fes
    SET RTU_TAG = rtuTag,DOT_NO = tmpRelationFesByNameTbl.DOT_NO
    from tmprelationfesbynametbl
    WHERE analog_map_to_fes.KEY_ID_TAG = tmpRelationFesByNameTbl.KEY_ID_TAG;

    
    --dig
    DELETE FROM tmpRelationFesByNameTbl;
    
    INSERT INTO tmpRelationFesByNameTbl(KEY_ID_TAG,DOT_NO)
    SELECT digital_map_to_fes.KEY_ID_TAG,fes_digital.DOT_NO
    FROM digital_map_to_fes INNER JOIN fes_digital 
    ON  digital_map_to_fes.KEY_ID_TAG LIKE concat('digital.',devTag,'.%')
    AND fes_digital.TAG_NAME  LIKE concat(rtuTag,'.%')
    AND fes_digital.DEV_TAG = fesDevTag
    AND getAfterLastDot(digital_map_to_fes.KEY_ID_TAG) = getAfterLastDot(fes_digital.FES_POINT_TP_NAME);
    
    update digital_map_to_fes
    SET RTU_TAG = rtuTag,DOT_NO1 = tmpRelationFesByNameTbl.DOT_NO
    from tmprelationfesbynametbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationFesByNameTbl.KEY_ID_TAG;

    
    --mix
    DELETE FROM tmpRelationFesByNameTbl;
    
    INSERT INTO tmpRelationFesByNameTbl(KEY_ID_TAG,DOT_NO)
    SELECT mix_map_to_fes.KEY_ID_TAG,fes_mix.DOT_NO
    FROM mix_map_to_fes INNER JOIN fes_mix 
    ON  mix_map_to_fes.KEY_ID_TAG LIKE concat('mix.',devTag,'.%')
    AND fes_mix.TAG_NAME  LIKE concat(rtuTag,'.%')
    AND fes_mix.DEV_TAG = fesDevTag
    AND getAfterLastDot(mix_map_to_fes.KEY_ID_TAG) = getAfterLastDot(fes_mix.FES_POINT_TP_NAME);
    
    update mix_map_to_fes
    SET RTU_TAG = rtuTag,DOT_NO = tmpRelationFesByNameTbl.DOT_NO
    from tmprelationfesbynametbl
    WHERE mix_map_to_fes.KEY_ID_TAG = tmpRelationFesByNameTbl.KEY_ID_TAG;

    
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION relationToFes(devTagName varchar(64), fesDevTagName varchar(64))
RETURNS VOID
AS $$
    DECLARE rtuTagName varchar(64);
    DECLARE fesDevTempTagName varchar(64);
BEGIN
    /*
     * 后台和FES的两个设备之间做关联,使用前,先检查后台设备的模板和前置的模板是否有所关联,已经关联的测点不会再关联,若想关联,先清除
     */
    
    IF COALESCE(devTagName,'') = '' OR COALESCE(fesDevTagName,'')=''
    THEN RETURN;
    END IF;
    
    SELECT RTU_TAG,DEV_TP_NAME INTO rtuTagName,fesDevTempTagName FROM fes_dev_info WHERE TAG_NAME = fesDevTagName;
    
    IF COALESCE(rtuTagName,'') = '' OR COALESCE(fesDevTempTagName,'')=''
    THEN RETURN;
    END IF;
   
    --创建临时表
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpRelationToFesTbl(
        KEY_ID_TAG varchar(128) NOT NULL,
        DOT_NO varchar(48) NOT NULL,
        OFFSET_NO decimal(10)
    );
    DELETE FROM tmpRelationToFesTbl;
    
    --以accuml为例,进行说明.把后台设备下的所有accuml点,和对应的前置设备下的fes_accuml点的Key_ID_TAG和DOT_NO写入临时表
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.DOT_NO 
    FROM 
    (SELECT accuml_map_to_fes.KEY_ID_TAG,accuml_temp_define.SEC_SEQ_NO 
    FROM  accuml_map_to_fes,accuml_temp_define 
    WHERE 
        accuml_map_to_fes.KEY_ID_TAG like concat('accuml','.' ,devTagName,'.','%') AND   
        accuml_map_to_fes.POINT_TP_NAME= accuml_temp_define.TAG_NAME AND 
        accuml_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND
        COALESCE(accuml_temp_define.SEC_SEQ_NO,-1) >= 0
    ) tb1 
    INNER JOIN 
    (SELECT fes_accuml.DOT_NO, fes_accuml.SEQ_NO 
    FROM fes_accuml 
    WHERE 
        fes_accuml.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_accuml.FES_POINT_TP_NAME) = fesDevTempTagName
    ) tb2
    ON 
    tb1.SEC_SEQ_NO = tb2.SEQ_NO;
    
    --假如前置点已经被占用,先清除
    update fes_accuml
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationtofestbl
    where fes_accuml.TAG_NAME = concat(rtuTagName,'.', tmpRelationToFesTbl.DOT_NO);

    --清除后台的
    update accuml
    set rtu_tag = ''
    from tmprelationtofestbl
    where tag_name = getafterfirstdot(tmprelationtofestbl.key_id_tag);

    
    --模拟手动操作,先关联对应RTU,再关联对应点
    update accuml
    set rtu_tag = rtutagname 
    from tmprelationtofestbl
    where tag_name = getafterfirstdot(tmprelationtofestbl.key_id_tag);

    
    update accuml_map_to_fes
    SET DOT_NO = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE accuml_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG;

    
    --analog
    DELETE FROM tmpRelationToFesTbl;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.DOT_NO 
    FROM 
    (SELECT analog_map_to_fes.KEY_ID_TAG,analog_temp_define.SEC_SEQ_NO 
    FROM  analog_map_to_fes,analog_temp_define 
    WHERE 
        analog_map_to_fes.KEY_ID_TAG like concat('analog','.' ,devTagName,'.','%') AND 
        analog_map_to_fes.POINT_TP_NAME= analog_temp_define.TAG_NAME AND 
        analog_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND
        COALESCE(analog_temp_define.SEC_SEQ_NO,-1) >= 0
    )tb1 
    INNER JOIN 
    (SELECT fes_analog.DOT_NO, fes_analog.SEQ_NO 
    FROM fes_analog 
    WHERE 
        fes_analog.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_analog.FES_POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.SEC_SEQ_NO = tb2.SEQ_NO;
    
    --假如前置点已经被占用,先清除
    update fes_analog
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationtofestbl
    where fes_analog.TAG_NAME = concat(rtuTagName,'.', tmpRelationToFesTbl.DOT_NO);

    --清除后台的
    update analog
    set rtu_tag = ''
    from tmprelationtofestbl
    where tag_name = getafterfirstdot(tmprelationtofestbl.key_id_tag);

    
    update analog
    set rtu_tag = rtutagname
    from tmprelationtofestbl
    where tag_name = getafterfirstdot(tmprelationtofestbl.key_id_tag);

    
    update analog_map_to_fes
    SET DOT_NO = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE analog_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG;

    
    --digital有五个分量,要注意
    DELETE FROM  tmpRelationToFesTbl;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.DOT_NO  ,1
    FROM 
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_SEQ_NO1 
    FROM  digital_map_to_fes,digital_temp_define 
    WHERE 
        digital_map_to_fes.KEY_ID_TAG like concat('digital','.' ,devTagName,'.','%') AND 
        digital_map_to_fes.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND
        COALESCE(digital_temp_define.SEC_SEQ_NO1,-1) >= 0
    )tb1 
    INNER JOIN 
    (SELECT fes_digital.DOT_NO, fes_digital.SEQ_NO 
    FROM fes_digital 
    WHERE 
        fes_digital.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital.FES_POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.SEC_SEQ_NO1 = tb2.SEQ_NO;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.DOT_NO  ,2
    FROM 
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_SEQ_NO2 
    FROM  digital_map_to_fes,digital_temp_define 
    WHERE 
        digital_map_to_fes.KEY_ID_TAG like concat('digital','.' ,devTagName,'.','%') AND 
        digital_map_to_fes.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND
        COALESCE(digital_temp_define.SEC_SEQ_NO2,-1) >= 0
    )tb1 
    INNER JOIN 
    (SELECT fes_digital.DOT_NO, fes_digital.SEQ_NO 
    FROM fes_digital 
    WHERE 
        fes_digital.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital.FES_POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.SEC_SEQ_NO2 = tb2.SEQ_NO;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.DOT_NO  ,3
    FROM 
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_SEQ_NO3 
    FROM  digital_map_to_fes,digital_temp_define 
    WHERE 
        digital_map_to_fes.KEY_ID_TAG like concat('digital','.' ,devTagName,'.','%') AND 
        digital_map_to_fes.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND
        COALESCE(digital_temp_define.SEC_SEQ_NO3,-1) >= 0
    )tb1 
    INNER JOIN 
    (SELECT fes_digital.DOT_NO, fes_digital.SEQ_NO 
    FROM fes_digital 
    WHERE 
        fes_digital.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital.FES_POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.SEC_SEQ_NO3 = tb2.SEQ_NO;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.DOT_NO  ,4
    FROM 
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_SEQ_NO4 
    FROM  digital_map_to_fes,digital_temp_define 
    WHERE 
        digital_map_to_fes.KEY_ID_TAG like concat('digital','.' ,devTagName,'.','%') AND 
        digital_map_to_fes.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND
        COALESCE(digital_temp_define.SEC_SEQ_NO4,-1) >= 0
    )tb1 
    INNER JOIN 
    (SELECT fes_digital.DOT_NO, fes_digital.SEQ_NO 
    FROM fes_digital 
    WHERE 
        fes_digital.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital.FES_POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.SEC_SEQ_NO4 = tb2.SEQ_NO;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.KEY_ID_TAG, tb2.DOT_NO  ,5
    FROM 
    (SELECT digital_map_to_fes.KEY_ID_TAG,digital_temp_define.SEC_SEQ_NO5 
    FROM  digital_map_to_fes,digital_temp_define 
    WHERE 
        digital_map_to_fes.KEY_ID_TAG like concat('digital','.' ,devTagName,'.','%') AND  
        digital_map_to_fes.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName AND
        COALESCE(digital_temp_define.SEC_SEQ_NO5,-1) >= 0
    )tb1 
    INNER JOIN 
    (SELECT fes_digital.DOT_NO, fes_digital.SEQ_NO 
    FROM fes_digital 
    WHERE 
        fes_digital.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital.FES_POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.SEC_SEQ_NO5 = tb2.SEQ_NO;
    
    --假如前置点已经被占用,先清除
    update fes_digital
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationtofestbl
    where fes_digital.TAG_NAME = concat(rtuTagName,'.', tmpRelationToFesTbl.DOT_NO);

    --清除后台的
    update digital
    set rtu_tag = '' 
    from tmprelationtofestbl
    where tag_name = getafterfirstdot(tmprelationtofestbl.key_id_tag);

    
    update digital
    set rtu_tag = rtutagname
    from tmprelationtofestbl
    where tag_name = getafterfirstdot(tmprelationtofestbl.key_id_tag);

    
    update digital_map_to_fes
    SET DOT_NO1 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 1;

    
    update digital_map_to_fes
    SET DOT_NO2 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 2;

   
    update digital_map_to_fes
    SET DOT_NO3 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 3;

   
    update digital_map_to_fes
    SET DOT_NO4 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 4;

    
    update digital_map_to_fes
    SET DOT_NO5 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 5;

    
    
    --mix
    DELETE FROM  tmpRelationToFesTbl;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ) 
    SELECT tb1.KEY_ID_TAG, tb2.DOT_NO  
    FROM 
    (SELECT mix_map_to_fes.KEY_ID_TAG,mix_temp_define.SEC_SEQ_NO 
    FROM  mix_map_to_fes,mix_temp_define 
    WHERE 
        mix_map_to_fes.KEY_ID_TAG like concat('mix','.' ,devTagName,'.','%') AND 
        mix_map_to_fes.POINT_TP_NAME= mix_temp_define.TAG_NAME AND 
        mix_temp_define.SEC_DEVTP_NAME = fesDevTempTagName 
    )tb1 
    INNER JOIN 
    (SELECT fes_mix.DOT_NO, fes_mix.SEQ_NO 
    FROM fes_mix 
    WHERE 
        fes_mix.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_mix.FES_POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.SEC_SEQ_NO = tb2.SEQ_NO;
    
    --假如前置点已经被占用,先清除
    update fes_mix
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME=''
    from tmprelationtofestbl
    where fes_mix.TAG_NAME = concat(rtuTagName,'.', tmpRelationToFesTbl.DOT_NO);

    --清除后台的
    update mix
    set rtu_tag = '' 
    from tmprelationtofestbl
    where tag_name = getafterfirstdot(tmprelationtofestbl.key_id_tag);

    
    update mix
    set rtu_tag = rtutagname
    from tmprelationtofestbl
    where tag_name = getafterfirstdot(tmprelationtofestbl.key_id_tag);

    
    update mix_map_to_fes
    SET DOT_NO = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE mix_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG;

    
    
    --最后只剩下对应控制点,没有关联
    --analog_ctrl
    DELETE FROM  tmpRelationToFesTbl;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ) 
    SELECT tb1.TAG_NAME, tb2.DOT_NO  
    FROM 
    (SELECT analog_control.TAG_NAME,analog_temp_define.OUT_SEC_SEQ_NO 
    FROM  analog_control,analog_temp_define 
    WHERE 
        analog_control.TAG_NAME like concat(devTagName,'.','%') AND 
        analog_control.POINT_TP_NAME= analog_temp_define.TAG_NAME AND 
        COALESCE(analog_temp_define.OUT_SEC_SEQ_NO,-1) >= 0 AND 
        analog_temp_define.SEC_DEVTP_NAME = fesDevTempTagName 
    )tb1 
    INNER JOIN 
    (SELECT fes_analog_ctrl.DOT_NO, fes_analog_ctrl.SEQ_NO 
    FROM fes_analog_ctrl 
    WHERE 
        fes_analog_ctrl.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_analog_ctrl.POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.OUT_SEC_SEQ_NO = tb2.SEQ_NO;
        
    update analog_control
    SET RTU_TAG = rtuTagName, OFFSET_NO = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE analog_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG;

       
    --digital_Ctrl
    DELETE FROM  tmpRelationToFesTbl;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME, tb2.DOT_NO ,1 
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO1 
    FROM  digital_control,digital_temp_define 
    WHERE 
        digital_control.TAG_NAME like concat(devTagName,'.','%') AND 
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO1,-1) >= 0 AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO, fes_digital_ctrl.SEQ_NO 
    FROM fes_digital_ctrl 
    WHERE 
        fes_digital_ctrl.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital_ctrl.POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.OUT_SEC_SEQ_NO1 = tb2.SEQ_NO;
    
   INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME, tb2.DOT_NO ,2 
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO2 
    FROM  digital_control,digital_temp_define 
    WHERE 
        digital_control.TAG_NAME like concat(devTagName,'.','%') AND 
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO2,-1) >= 0 AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO, fes_digital_ctrl.SEQ_NO 
    FROM fes_digital_ctrl 
    WHERE 
        fes_digital_ctrl.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital_ctrl.POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.OUT_SEC_SEQ_NO2 = tb2.SEQ_NO;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME, tb2.DOT_NO ,3 
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO3 
    FROM  digital_control,digital_temp_define 
    WHERE 
        digital_control.TAG_NAME like concat(devTagName,'.','%') AND 
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO3,-1) >= 0 AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO, fes_digital_ctrl.SEQ_NO 
    FROM fes_digital_ctrl 
    WHERE 
        fes_digital_ctrl.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital_ctrl.POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.OUT_SEC_SEQ_NO3 = tb2.SEQ_NO;
    
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME, tb2.DOT_NO ,4 
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO4 
    FROM  digital_control,digital_temp_define 
    WHERE 
        digital_control.TAG_NAME like concat(devTagName,'.','%') AND 
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO4,-1) >= 0 AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO, fes_digital_ctrl.SEQ_NO 
    FROM fes_digital_ctrl 
    WHERE 
        fes_digital_ctrl.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital_ctrl.POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.OUT_SEC_SEQ_NO4 = tb2.SEQ_NO;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ,OFFSET_NO) 
    SELECT tb1.TAG_NAME, tb2.DOT_NO ,5 
    FROM 
    (SELECT digital_control.TAG_NAME,digital_temp_define.OUT_SEC_SEQ_NO5 
    FROM  digital_control,digital_temp_define 
    WHERE 
        digital_control.TAG_NAME like concat(devTagName,'.','%') AND 
        digital_control.POINT_TP_NAME= digital_temp_define.TAG_NAME AND 
        COALESCE(digital_temp_define.OUT_SEC_SEQ_NO5,-1) >= 0 AND 
        digital_temp_define.SEC_DEVTP_NAME = fesDevTempTagName 
    )tb1 
    INNER JOIN 
    (SELECT fes_digital_ctrl.DOT_NO, fes_digital_ctrl.SEQ_NO 
    FROM fes_digital_ctrl 
    WHERE 
        fes_digital_ctrl.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_digital_ctrl.POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.OUT_SEC_SEQ_NO5 = tb2.SEQ_NO;
    
        
    update digital_control
    SET RTU_TAG = rtuTagName, OFFSET_NO1 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 1;

    
    update digital_control
    SET RTU_TAG = rtuTagName, OFFSET_NO2 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 2;

    
    update digital_control
    SET RTU_TAG = rtuTagName, OFFSET_NO3 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 3;

    
    update digital_control
    SET RTU_TAG = rtuTagName, OFFSET_NO4 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 4;

    
    update digital_control
    SET RTU_TAG = rtuTagName, OFFSET_NO5 = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 5;

    
    
    --mix_ctrl
    DELETE FROM  tmpRelationToFesTbl;
    
    INSERT INTO tmpRelationToFesTbl (KEY_ID_TAG , DOT_NO ) 
    SELECT tb1.TAG_NAME, tb2.DOT_NO  
    FROM 
    (SELECT mix_control.TAG_NAME,mix_temp_define.OUT_SEC_SEQ_NO 
    FROM  mix_control,mix_temp_define 
    WHERE 
        mix_control.TAG_NAME like concat(devTagName,'.','%') AND
        mix_control.POINT_TP_NAME= mix_temp_define.TAG_NAME AND 
        COALESCE(mix_temp_define.OUT_SEC_SEQ_NO,-1) >= 0 AND 
        mix_temp_define.SEC_DEVTP_NAME = fesDevTempTagName 
    )tb1 
    INNER JOIN 
    (SELECT fes_mix_ctrl.DOT_NO, fes_mix_ctrl.SEQ_NO 
    FROM fes_mix_ctrl 
    WHERE 
        fes_mix_ctrl.DEV_TAG = fesDevTagName AND
        getBeforeLastDot(fes_mix_ctrl.POINT_TP_NAME) = fesDevTempTagName
    )tb2
    ON 
    tb1.OUT_SEC_SEQ_NO = tb2.SEQ_NO;
    
    
    update mix_control
    SET RTU_TAG = rtuTagName, OFFSET_NO = tmpRelationToFesTbl.DOT_NO
    from tmprelationtofestbl
    WHERE mix_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG;

    
    
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION sortResPara1(rtuTag varchar(64), startNum decimal(10,0))
RETURNS VOID
AS $$
BEGIN
    -- 依据设备ID重排规约参数1
	
    --acc
    UPDATE fes_accuml
    set RES_PARA_INT1 = seqno + startNum FROM
	(SELECT fes_accuml.TAG_NAME,fes_accuml.DESCRIPTION,fes_dev_info.DEV_DESC, fes_accuml.RES_PARA_INT1,
	row_number() over (order by fes_dev_info.DEV_ID,fes_accuml.SEQ_NO ) AS seqno
    FROM fes_accuml LEFT JOIN fes_dev_info on fes_accuml.DEV_TAG = fes_dev_info.TAG_NAME 
    WHERE fes_accuml.TAG_NAME LIKE concat(rtuTag,'.%')) AS temp
	WHERE fes_accuml.TAG_NAME = temp.TAG_NAME;
    
    --ana
    UPDATE fes_analog
    set RES_PARA_INT1 = seqno + startNum FROM
    (SELECT fes_analog.TAG_NAME,fes_analog.DESCRIPTION,fes_dev_info.DEV_DESC, fes_analog.RES_PARA_INT1,
	row_number() over (order by fes_dev_info.DEV_ID,fes_analog.SEQ_NO ) AS seqno
    FROM fes_analog LEFT JOIN fes_dev_info on fes_analog.DEV_TAG = fes_dev_info.TAG_NAME 
    WHERE fes_analog.TAG_NAME LIKE concat(rtuTag,'.%')) AS temp
	WHERE fes_analog.TAG_NAME = temp.TAG_NAME;
    
    --dig
    UPDATE fes_digital
    set RES_PARA_INT1 = seqno + startNum FROM
    (SELECT fes_digital.TAG_NAME,fes_digital.DESCRIPTION,fes_dev_info.DEV_DESC, fes_digital.RES_PARA_INT1,
	row_number() over (order by fes_dev_info.DEV_ID,fes_digital.SEQ_NO ) AS seqno
    FROM fes_digital LEFT JOIN fes_dev_info on fes_digital.DEV_TAG = fes_dev_info.TAG_NAME 
    WHERE fes_digital.TAG_NAME LIKE concat(rtuTag,'.%')) AS temp
	WHERE fes_digital.TAG_NAME = temp.TAG_NAME;
    
    --mix
    UPDATE fes_mix
    set RES_PARA_INT1 = seqno + startNum FROM
    (SELECT fes_mix.TAG_NAME,fes_mix.DESCRIPTION,fes_dev_info.DEV_DESC, fes_mix.RES_PARA_INT1,
	row_number() over (order by fes_dev_info.DEV_ID,fes_mix.SEQ_NO ) AS seqno
    FROM fes_mix LEFT JOIN fes_dev_info on fes_mix.DEV_TAG = fes_dev_info.TAG_NAME 
    WHERE fes_mix.TAG_NAME LIKE concat(rtuTag,'.%')) temp
	WHERE fes_mix.TAG_NAME = temp.TAG_NAME;
    
    --anaCtrl
    UPDATE fes_analog_ctrl
    set RES_PARA_INT1 = seqno + startNum FROM
    (SELECT fes_analog_ctrl.TAG_NAME,fes_analog_ctrl.DESCRIPTION,fes_dev_info.DEV_DESC, fes_analog_ctrl.RES_PARA_INT1,
	row_number() over (order by fes_dev_info.DEV_ID,fes_analog_ctrl.SEQ_NO ) AS seqno
    FROM fes_analog_ctrl LEFT JOIN fes_dev_info on fes_analog_ctrl.DEV_TAG = fes_dev_info.TAG_NAME 
    WHERE fes_analog_ctrl.TAG_NAME LIKE concat(rtuTag,'.%')) temp
	WHERE fes_analog_ctrl.TAG_NAME = temp.TAG_NAME;
    
    --digCtrl
    UPDATE fes_digital_ctrl
    set RES_PARA_INT1 = seqno + startNum FROM
    (SELECT fes_digital_ctrl.TAG_NAME,fes_digital_ctrl.DESCRIPTION,fes_dev_info.DEV_DESC, fes_digital_ctrl.RES_PARA_INT1,
	row_number() over (order by fes_dev_info.DEV_ID,fes_digital_ctrl.SEQ_NO ) AS seqno
    FROM fes_digital_ctrl LEFT JOIN fes_dev_info on fes_digital_ctrl.DEV_TAG = fes_dev_info.TAG_NAME 
    WHERE fes_digital_ctrl.TAG_NAME LIKE concat(rtuTag,'.%')) temp
	WHERE fes_digital_ctrl.TAG_NAME = temp.TAG_NAME;
	
    --mixCtrl
    UPDATE fes_mix_ctrl
    set RES_PARA_INT1 = seqno + startNum FROM
    (SELECT fes_mix_ctrl.TAG_NAME,fes_mix_ctrl.DESCRIPTION,fes_dev_info.DEV_DESC, fes_mix_ctrl.RES_PARA_INT1,
	row_number() over (order by fes_dev_info.DEV_ID,fes_mix_ctrl.SEQ_NO ) AS seqno
    FROM fes_mix_ctrl LEFT JOIN fes_dev_info on fes_mix_ctrl.DEV_TAG = fes_dev_info.TAG_NAME 
    WHERE fes_mix_ctrl.TAG_NAME LIKE concat(rtuTag,'.%')) temp
	WHERE fes_mix_ctrl.TAG_NAME = temp.TAG_NAME;
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION updateDescByFes(devTagName varchar(64))
RETURNS VOID
AS $$
BEGIN
    --使用前置设备测点的描述,更新后台设备的测点的描述
    --需要使用临时表先存储起来
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpUpdateDescByFesTbl(
        TAG_NAME varchar(64),
        DESCRIPTION varchar(128)
        );
    
        --acc
    DELETE FROM tmpUpdateDescByFesTbl;
    
    INSERT INTO tmpUpdateDescByFesTbl(TAG_NAME,DESCRIPTION)
    SELECT accuml.TAG_NAME,fes_accuml.DESCRIPTION 
    FROM (accuml INNER JOIN accuml_map_to_fes ON accuml.DEVICE = devTagName AND accuml_map_to_fes.KEY_ID_TAG = concat('accuml.',accuml.TAG_NAME))
    INNER JOIN fes_accuml ON fes_accuml.TAG_NAME = concat(accuml_map_to_fes.RTU_TAG,'.',accuml_map_to_fes.DOT_NO);
    
    update accuml
    SET DESCRIPTION = tmpUpdateDescByFesTbl.DESCRIPTION
    from tmpupdatedescbyfestbl
    WHERE accuml.TAG_NAME = tmpUpdateDescByFesTbl.TAG_NAME;

    --ana
    DELETE FROM tmpUpdateDescByFesTbl;
    INSERT INTO tmpUpdateDescByFesTbl(TAG_NAME,DESCRIPTION)
    SELECT analog.TAG_NAME,fes_analog.DESCRIPTION 
    FROM (analog INNER JOIN analog_map_to_fes ON analog.DEVICE = devTagName AND analog_map_to_fes.KEY_ID_TAG = concat('analog.',analog.TAG_NAME))
    INNER JOIN fes_analog ON fes_analog.TAG_NAME = concat(analog_map_to_fes.RTU_TAG,'.',analog_map_to_fes.DOT_NO);
    
    update analog
    SET DESCRIPTION = tmpUpdateDescByFesTbl.DESCRIPTION
    from tmpupdatedescbyfestbl
    WHERE analog.TAG_NAME = tmpUpdateDescByFesTbl.TAG_NAME;

    --dig
    DELETE FROM tmpUpdateDescByFesTbl;
    
    INSERT INTO tmpUpdateDescByFesTbl(TAG_NAME,DESCRIPTION)
    SELECT digital.TAG_NAME,fes_digital.DESCRIPTION 
    FROM (digital INNER JOIN digital_map_to_fes ON digital.DEVICE = devTagName AND digital_map_to_fes.KEY_ID_TAG = concat('digital.',digital.TAG_NAME))
    INNER JOIN fes_digital ON fes_digital.TAG_NAME = concat(digital_map_to_fes.RTU_TAG,'.',digital_map_to_fes.DOT_NO1);
    
    update digital
    SET DESCRIPTION = tmpUpdateDescByFesTbl.DESCRIPTION
    from tmpupdatedescbyfestbl
    WHERE digital.TAG_NAME = tmpUpdateDescByFesTbl.TAG_NAME;

    --mix
    DELETE FROM tmpUpdateDescByFesTbl;
    
    INSERT INTO tmpUpdateDescByFesTbl(TAG_NAME,DESCRIPTION)
    SELECT mix.TAG_NAME,fes_mix.DESCRIPTION 
    FROM (mix INNER JOIN mix_map_to_fes ON mix.DEVICE = devTagName AND mix_map_to_fes.KEY_ID_TAG = concat('mix.',mix.TAG_NAME))
    INNER JOIN fes_mix ON fes_mix.TAG_NAME = concat(mix_map_to_fes.RTU_TAG,'.',mix_map_to_fes.DOT_NO);
    
    update mix
    SET DESCRIPTION = tmpUpdateDescByFesTbl.DESCRIPTION
    from tmpupdatedescbyfestbl
    WHERE mix.TAG_NAME = tmpUpdateDescByFesTbl.TAG_NAME;

    
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION updateForwardBase(tableName varchar(64), rtuTagName varchar(64), bs decimal(22,6))
RETURNS VOID
AS $$
BEGIN
    /*
     * 修改转发表的修正值
     */
   IF lower(tableName) = 'fes_forwarding_accuml' THEN
        UPDATE fes_forwarding_accuml SET BASE = bs WHERE RTU_TAG = rtuTagName ;
    ELSEIF lower(tableName) = 'fes_forwarding_anaolg' THEN 
        UPDATE fes_forwarding_anaolg SET BASE = bs WHERE RTU_TAG = rtuTagName ;
    END IF;
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION updateForwardCoeff( tableName varchar(64), rtuTagName varchar(64), coe decimal(22,6))
RETURNS VOID
AS $$
BEGIN
    --修改转发表的系数
    
    IF lower(tableName) = 'fes_forwarding_accuml' THEN
        UPDATE fes_forwarding_accuml SET COEFF = coe WHERE RTU_TAG = rtuTagName ;
    ELSEIF lower(tableName) = 'fes_forwarding_anaolg' THEN 
        UPDATE fes_forwarding_anaolg SET COEFF = coe WHERE RTU_TAG = rtuTagName ;
    END IF;
        
    
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION updateUsergRegion(roleId decimal(10,0))
RETURNS VOID
AS $$
BEGIN
    --更新用户组的责任区
    --更新rm_userg_rsp_info表
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpUserGroupTbl(
        PERM_USER_GROUP decimal(10,0)
        );
    DELETE FROM tmpUserGroupTbl;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpUURegion(
        PERM_USER_GROUP decimal(10,0),
        PERM_ROLE decimal(10,0),
        PERM_SPEFUNC decimal(10,0),
        PERM_REGION1 decimal(10,0),
        PERM_REGION2 decimal(10,0),
        PERM_REGION3 decimal(10,0),
        PERM_REGION4 decimal(10,0),
        PERM_LOCATION1 decimal(10,0),
        PERM_LOCATION2 decimal(10,0),
        PERM_LOCATION3 decimal(10,0),
        PERM_LOCATION4 decimal(10,0),
        PERM_LOCATION5 decimal(10,0),
        PERM_LOCATION6 decimal(10,0),
        PERM_LOCATION7 decimal(10,0),
        PERM_LOCATION8 decimal(10,0)
    );
    DELETE FROM tmpUURegion;
    
    INSERT INTO tmpUserGroupTbl(PERM_USER_GROUP)
    SELECT PERM_USER_GROUP FROM rm_userg_rsp_info WHERE PERM_ROLE = roleId GROUP BY PERM_USER_GROUP;
    
    INSERT INTO tmpUURegion (PERM_USER_GROUP, PERM_ROLE, PERM_SPEFUNC, PERM_REGION1, PERM_REGION2, PERM_REGION3, PERM_REGION4, PERM_LOCATION1, PERM_LOCATION2, PERM_LOCATION3, PERM_LOCATION4, PERM_LOCATION5, PERM_LOCATION6, PERM_LOCATION7, PERM_LOCATION8) 
    SELECT PERM_USER_GROUP, PERM_ROLE, PERM_SPEFUNC, PERM_REGION1, PERM_REGION2, PERM_REGION3, PERM_REGION4, PERM_LOCATION1, PERM_LOCATION2, PERM_LOCATION3, PERM_LOCATION4, PERM_LOCATION5, PERM_LOCATION6, PERM_LOCATION7, PERM_LOCATION8 
    FROM rm_userg_rsp_info WHERE PERM_ROLE = roleId;
   
    
    
    --删除旧的数据再重新插入
    
    DELETE FROM rm_userg_rsp_info WHERE PERM_ROLE = roleId;
    INSERT INTO rm_userg_rsp_info(PERM_USER_GROUP, PERM_ROLE, PERM_SPEFUNC, PERM_REGION1, PERM_REGION2, PERM_REGION3, PERM_REGION4, PERM_LOCATION1, PERM_LOCATION2, PERM_LOCATION3, PERM_LOCATION4, PERM_LOCATION5, PERM_LOCATION6, PERM_LOCATION7, PERM_LOCATION8) 
    SELECT tmpUserGroupTbl.PERM_USER_GROUP,roleId,rm_role_spef_reg.PERM_SPEFUNC,rm_role_spef_reg.PERM_REGION1,rm_role_spef_reg.PERM_REGION2,rm_role_spef_reg.PERM_REGION3,rm_role_spef_reg.PERM_REGION4,0,0,0,0,0,0,0,0
    FROM rm_role_spef_reg ,tmpUserGroupTbl WHERE rm_role_spef_reg.PERM_ROLE=roleId;
    
    update rm_userg_rsp_info
    SET PERM_LOCATION1 = tmpUURegion.PERM_LOCATION1, PERM_LOCATION2 = tmpUURegion.PERM_LOCATION2, PERM_LOCATION3 = tmpUURegion.PERM_LOCATION3, PERM_LOCATION4 = tmpUURegion.PERM_LOCATION4, PERM_LOCATION5 = tmpUURegion.PERM_LOCATION5, PERM_LOCATION6 = tmpUURegion.PERM_LOCATION6, PERM_LOCATION7 = tmpUURegion.PERM_LOCATION7, PERM_LOCATION8 = tmpUURegion.PERM_LOCATION8
    from tmpuuregion
    WHERE rm_userg_rsp_info.PERM_USER_GROUP = tmpUURegion.PERM_USER_GROUP AND rm_userg_rsp_info.PERM_ROLE = roleId AND tmpUURegion.PERM_ROLE = roleId AND rm_userg_rsp_info.PERM_SPEFUNC = tmpUURegion.PERM_SPEFUNC;

    
  
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION updateUsergRsp(userGId decimal(10,0))
RETURNS VOID
AS $$
BEGIN
    --更新rm_userg_rsp_info表
    update rm_userg_rsp_info
    SET PERM_REGION1 = rm_role_spef_reg.PERM_REGION1, PERM_REGION2 = rm_role_spef_reg.PERM_REGION2, PERM_REGION3 = rm_role_spef_reg.PERM_REGION3, PERM_REGION4 = rm_role_spef_reg.PERM_REGION4
    from rm_role_spef_reg
    WHERE rm_userg_rsp_info.PERM_ROLE = rm_role_spef_reg.PERM_ROLE AND rm_userg_rsp_info.PERM_SPEFUNC = rm_role_spef_reg.PERM_SPEFUNC AND rm_userg_rsp_info.PERM_USER_GROUP = userGId;

    
       
END;
$$LANGUAGE PLPGSQL;




CREATE OR REPLACE FUNCTION userGRoleModify(userGroupId decimal(10,0), roleIdList varchar(100))
RETURNS VOID
AS $$
BEGIN
    --针对用户组所属角色有所更改的情况
    --userGroupId 用户组ID,不可为空
    --roleIdList 该用户组所属的角色的ID号列表,以逗号分隔
    --删除不在指定roleIdList表中的角色
    DELETE FROM rm_userg_rsp_info WHERE PERM_USER_GROUP = userGroupId AND PERM_ROLE::text!=all(string_to_array(roleIdList,','));
END;
$$LANGUAGE PLPGSQL;







/*******************************************/
/*rule 用于适配 MySQL 的 insert ignore 语法*/
create or replace rule rule_rm_userg_report_info as on 
insert to rm_userg_report_info where exists 
(select 1 from rm_userg_report_info where PERM_USER_GROUP = new.PERM_USER_GROUP and REPORT_CODE = new.REPORT_CODE ) 
do instead nothing;

create or replace rule rule_accuml as on 
insert to accuml where exists 
(select 1 from accuml where tag_name = new.tag_name) 
do instead nothing;

create or replace rule rule_analog as on 
insert to analog where exists 
(select 1 from analog where tag_name = new.tag_name) 
do instead nothing;

create or replace rule rule_digital as on 
insert to digital where exists 
(select 1 from digital where tag_name = new.tag_name) 
do instead nothing;

create or replace rule rule_mix as on 
insert to mix where exists 
(select 1 from mix where tag_name = new.tag_name) 
do instead nothing;
/*******************************************/

