drop table if exists accuml_limit_info;
CREATE TABLE `accuml_limit_info` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `LIMIT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '越限类型',
  `LIMIT_NUM` decimal(10,0) DEFAULT NULL COMMENT '越限级别数量',
  `LIMIT_UP1` decimal(22,6) DEFAULT NULL COMMENT '一级上限',
  `LIMIT_LOW1` decimal(22,6) DEFAULT NULL COMMENT '一级下限',
  `LIMIT_UP2` decimal(22,6) DEFAULT NULL COMMENT '二级上限',
  `LIMIT_LOW2` decimal(22,6) DEFAULT NULL COMMENT '二级下限',
  `LIMIT_UP3` decimal(22,6) DEFAULT NULL COMMENT '三级上限',
  `LIMIT_LOW3` decimal(22,6) DEFAULT NULL COMMENT '三级下限',
  `CROSS_PERCENT` decimal(22,6) DEFAULT NULL COMMENT '越线百分比',
  `RETURN_PERCENT` decimal(22,6) DEFAULT NULL COMMENT '返回百分比',
  `CROSS_TIME` decimal(20,0) DEFAULT NULL COMMENT '最新越线时间',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '告警优先级',
  `PIC_NAME` varchar(64) DEFAULT NULL COMMENT '越限联动画面',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延迟时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否确认自动返回',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认删除动作',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名称',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='累计量越限参数表';

drop trigger if exists TIA_ACCUML_LIMIT_INFO;
DELIMITER ;;
create trigger TIA_ACCUML_LIMIT_INFO AFTER INSERT on accuml_limit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'accuml_limit_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('accuml_limit_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ACCUML_LIMIT_INFO;
DELIMITER ;;
create trigger TUA_ACCUML_LIMIT_INFO AFTER UPDATE on accuml_limit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'accuml_limit_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('accuml_limit_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ACCUML_LIMIT_INFO;
DELIMITER ;;
create trigger TDA_ACCUML_LIMIT_INFO AFTER DELETE on accuml_limit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'accuml_limit_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('accuml_limit_info'));
END IF;
END;;
DELIMITER ;

drop table if exists accuml_map_to_fes;
CREATE TABLE `accuml_map_to_fes` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '标签点名（表名.测点标签）',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU',
  `DOT_NO` varchar(48) DEFAULT NULL COMMENT '前置序号',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基数',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数',
  `RANGE_UP` decimal(22,6) DEFAULT NULL,
  `RANGE_LOW` decimal(22,6) DEFAULT NULL COMMENT '最小量程',
  `IS_FILTER` decimal(10,0) DEFAULT NULL COMMENT '是否过滤突变',
  `PERCENT` decimal(10,0) DEFAULT NULL COMMENT '突变百分比',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='累积量前置映射表';

drop trigger if exists TI_ACCUML_MAP_TO_FES;
DELIMITER ;;
create trigger TI_ACCUML_MAP_TO_FES AFTER INSERT on accuml_map_to_fes for each ROW 
BEGIN
    
    #首先禁止FES触发器动作
    SET @disableFesAccumlTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableAccumlMapToFesTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN
                UPDATE fes_accuml SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
    SET @disableFesAccumlTrigger = 0;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'accuml_map_to_fes', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('accuml_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_ACCUML_MAP_TO_FES;
DELIMITER ;;
create trigger TU_ACCUML_MAP_TO_FES BEFORE UPDATE on accuml_map_to_fes for each ROW 
body_label:BEGIN
    IF NEW.RTU_TAG = OLD.RTU_TAG AND 
        NEW.DOT_NO = OLD.DOT_NO AND 
        NEW.KEY_ID_TAG = OLD.KEY_ID_TAG
        THEN LEAVE body_label;
    END IF;
    
    
    #首先禁止FES触发器动作
    SET @disableFesAccumlTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableAccumlMapToFesTrigger,0) = 0 THEN
    BEGIN
        #清除旧的
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN
                UPDATE fes_accuml SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
        
        #添加新的
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN
                UPDATE fes_accuml SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        ELSE SET NEW.DOT_NO = '-1';
        END IF;
    END;
    END IF;
    SET @disableFesAccumlTrigger = 0;
END;;
DELIMITER ;

drop trigger if exists TUA_ACCUML_MAP_TO_FES;
DELIMITER ;;
create trigger TUA_ACCUML_MAP_TO_FES AFTER UPDATE on accuml_map_to_fes for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'accuml_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('accuml_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_ACCUML_MAP_TO_FES;
DELIMITER ;;
create trigger TD_ACCUML_MAP_TO_FES BEFORE DELETE on accuml_map_to_fes for each ROW 
BEGIN
    
    
    #首先禁止FES触发器动作
    SET @disableFesAccumlTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableAccumlMapToFesTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN
                UPDATE fes_accuml SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
    SET @disableFesAccumlTrigger = 0;
END;;
DELIMITER ;

drop trigger if exists TDA_ACCUML_MAP_TO_FES;
DELIMITER ;;
create trigger TDA_ACCUML_MAP_TO_FES AFTER DELETE on accuml_map_to_fes for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'accuml_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('accuml_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop table if exists accuml_temp_define;
CREATE TABLE `accuml_temp_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '点标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `DEV_TP_NAME` varchar(64) NOT NULL COMMENT '设备模板名称',
  `SEQ_NO` decimal(10,0) NOT NULL COMMENT '点序号',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类型（ANA/DIG/MIX/ACC）',
  `POINT_CLASS` decimal(10,0) DEFAULT NULL COMMENT '点分类',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点属性（采集点/计算点/虚拟点...）',
  `POINT_SORT` decimal(10,0) DEFAULT NULL COMMENT '测点种类 ',
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位ID',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否采样',
  `SAMPLE_DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '采样死区',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL,
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL COMMENT '是否统计',
  `IS_LIMIT` decimal(10,0) DEFAULT NULL COMMENT '是否越限告警',
  `LIMIT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '越限告警类型',
  `LIMIT_NUM` decimal(10,0) DEFAULT NULL COMMENT '越限数量',
  `LIMIT_UP1` decimal(22,6) DEFAULT NULL COMMENT '一级越限上',
  `LIMIT_LOW1` decimal(22,6) DEFAULT NULL COMMENT '一级越限下',
  `LIMIT_UP2` decimal(22,6) DEFAULT NULL COMMENT '二级越限上',
  `LIMIT_LOW2` decimal(22,6) DEFAULT NULL COMMENT '二级越限下',
  `LIMIT_UP3` decimal(22,6) DEFAULT NULL COMMENT '三级越限上',
  `LIMIT_LOW3` decimal(22,6) DEFAULT NULL COMMENT '三级越限下',
  `CROSS_PERCENT` decimal(22,6) DEFAULT NULL COMMENT 'CROSS_PERCENT',
  `RETURN_PERCENT` decimal(22,6) DEFAULT NULL COMMENT 'RETURN_PERCENT',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '告警优先级',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延时时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回自动确认',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认后自动删除',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  `IS_FILTER` decimal(10,0) DEFAULT NULL COMMENT '是否过滤1',
  `PERCENT` decimal(10,0) DEFAULT NULL COMMENT 'PERCENT1',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基数-AO表',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数-AO表',
  `RANGE_UP` decimal(22,6) DEFAULT NULL COMMENT '最大量程',
  `RANGE_LOW` decimal(22,6) DEFAULT NULL COMMENT '最小量程',
  `SEC_DEV_IN_GRP` decimal(10,0) DEFAULT NULL,
  `SEC_DEVTP_NAME` varchar(64) DEFAULT NULL COMMENT '二次设备模板名',
  `SEC_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '输入点号',
  `KEY_VALUE` decimal(10,0) DEFAULT NULL COMMENT '关键数据标志',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='累计量定义表';

drop trigger if exists TI_ACCUML_TEMP_DEFINE;
DELIMITER ;;
create trigger TI_ACCUML_TEMP_DEFINE AFTER INSERT on accuml_temp_define for each ROW 
BEGIN
    /*
INSERT INTO accuml(
TAG_NAME,                                              DESCRIPTION,    DEVICE,   SEQ_NO,    LOCATION_ID, SUB_SYSTEM, RTU_TAG, GRAY, CONTRAST, QCOLOR, RAW_VALUE, VALUE, STATUS, LAST_UPDATE_TIME, LAST_CHANGE_TIME, UNIT_ID, SOUND_NAME1, SOUND_NAME2, SOUND_NAME3, REGION_ID, OPT_HANDOVER_GROUP, IS_LIMIT,     POINT_TYPE,    POINT_CLASS,    POINT_PROPERTY,    POINT_SORT,    DEV_TYPE, POINT_TP_NAME, IS_SAMPLE, IS_STATISTICS,       SAMPLE_PERIOD,    SAMPLE_DEADBAND,    CAMERA_TAG, PRESET_ID) 
SELECT   
concat(TAG_NAME, '.' , getAfterLastDot(NEW.TAG_NAME)), NEW.DESCRIPTION,TAG_NAME, NEW.SEQ_NO,LOCATION_ID, SUB_SYSTEM, '',      GRAY, CONTRAST, QCOLOR, '0',       VALUE, STATUS, 0,                0,                NEW.UNIT_ID,  '',      '',          '',         REGION_ID, OPT_HANDOVER_GROUP, NEW.IS_LIMIT, NEW.POINT_TYPE,NEW.POINT_CLASS,NEW.POINT_PROPERTY,NEW.POINT_SORT,DEV_TYPE, NEW.TAG_NAME, NEW.IS_SAMPLE,NEW.IS_STATISTICS, NEW.SAMPLE_PERIOD,NEW.SAMPLE_DEADBAND, '',       ''
FROM dev_info WHERE DEV_TP_NAME = NEW.DEV_TP_NAME;
*/
    INSERT INTO temp_plugin_point_map_acc
    (PLUGIN_NAME, TEMP_NAME, TEMP_POINT_NAME, FES_TEMP_NAME, FES_TEMP_POINT_NAME)
    SELECT 
    PLUGIN_NAME, TEMP_NAME, getAfterLastDot(NEW.TAG_NAME),'',''
    FROM temp_plugin_dev_map WHERE TEMP_NAME = NEW.DEV_TP_NAME;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'accuml_temp_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('accuml_temp_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_ACCUML_TEMP_DEFINE;
DELIMITER ;;
create trigger TU_ACCUML_TEMP_DEFINE AFTER UPDATE on accuml_temp_define for each ROW 
BEGIN
    /*
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.DESCRIPTION <> OLD.DESCRIPTION OR NEW.UNIT_ID <> OLD.UNIT_ID OR NEW.IS_LIMIT <> OLD.IS_LIMIT OR NEW.POINT_CLASS <> OLD.POINT_CLASS OR
    NEW.IS_SAMPLE <> OLD.IS_SAMPLE OR NEW.SAMPLE_DEADBAND <> OLD.SAMPLE_DEADBAND OR NEW.POINT_TYPE <> OLD.POINT_TYPE OR NEW.SEQ_NO <> OLD.SEQ_NO OR 
    NEW.IS_STATISTICS <> OLD.IS_STATISTICS OR NEW.SAMPLE_PERIOD <> OLD.SAMPLE_PERIOD OR NEW.POINT_PROPERTY <> OLD.POINT_PROPERTY OR NEW.POINT_SORT <> OLD.POINT_SORT
    THEN 
        UPDATE accuml SET 
        accuml.TAG_NAME = concat(accuml.DEVICE, '.', getAfterLastDot(NEW.TAG_NAME)),
        accuml.DESCRIPTION = NEW.DESCRIPTION,
        accuml.UNIT_ID = NEW.UNIT_ID,
        accuml.IS_LIMIT = NEW.IS_LIMIT,
        accuml.POINT_CLASS = NEW.POINT_CLASS,
        accuml.POINT_TP_NAME = NEW.TAG_NAME,
        accuml.IS_SAMPLE = NEW.IS_SAMPLE,
        accuml.SAMPLE_DEADBAND = NEW.SAMPLE_DEADBAND,
        accuml.POINT_TYPE = NEW.POINT_TYPE,
        accuml.SEQ_NO = NEW.SEQ_NO,
        accuml.IS_STATISTICS = NEW.IS_STATISTICS,
        accuml.SAMPLE_PERIOD = NEW.SAMPLE_PERIOD,
        accuml.POINT_PROPERTY = NEW.POINT_PROPERTY,
        accuml.POINT_SORT = NEW.POINT_SORT
        WHERE accuml.POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    
    IF NEW.LIMIT_TYPE <> OLD.LIMIT_TYPE OR NEW.LIMIT_NUM <> OLD.LIMIT_NUM OR NEW.LIMIT_UP1 <> OLD.LIMIT_UP1 OR NEW.LIMIT_LOW1 <> OLD.LIMIT_LOW1 OR NEW.LIMIT_UP2 <> OLD.LIMIT_UP2 OR
    NEW.LIMIT_LOW2 <> OLD.LIMIT_LOW2 OR NEW.LIMIT_UP3 <> OLD.LIMIT_UP3 OR NEW.LIMIT_LOW3 <> OLD.LIMIT_LOW3 OR NEW.CROSS_PERCENT <> OLD.CROSS_PERCENT OR 
    NEW.RETURN_PERCENT <> OLD.RETURN_PERCENT OR NEW.ALARM_PRIORITY <> OLD.ALARM_PRIORITY OR NEW.ALARM_DELAY_TIME <> OLD.ALARM_DELAY_TIME OR NEW.IS_WATER_ALM <> OLD.IS_WATER_ALM OR
    NEW.IS_ACK_ON_RTN <> OLD.IS_ACK_ON_RTN OR NEW.DEL_ACT_ON_ACK <> OLD.DEL_ACT_ON_ACK OR NEW.IS_NEVER_ALM_ON_RTN <> OLD.IS_NEVER_ALM_ON_RTN OR NEW.TAG_NAME <> OLD.TAG_NAME 
    THEN
        UPDATE accuml_limit_info SET 
        LIMIT_TYPE = NEW.LIMIT_TYPE,
        LIMIT_NUM = NEW.LIMIT_NUM,
        LIMIT_UP1 = NEW.LIMIT_UP1,
        LIMIT_LOW1 = NEW.LIMIT_LOW1,
        LIMIT_UP2 = NEW.LIMIT_UP2,
        LIMIT_LOW2 = NEW.LIMIT_LOW2,
        LIMIT_UP3 = NEW.LIMIT_UP3,
        LIMIT_LOW3 = NEW.LIMIT_LOW3,
        CROSS_PERCENT = NEW.CROSS_PERCENT,
        RETURN_PERCENT = NEW.RETURN_PERCENT,
        ALARM_PRIORITY = NEW.ALARM_PRIORITY,
        ALARM_DELAY_TIME = NEW.ALARM_DELAY_TIME,
        IS_WATER_ALM = NEW.IS_WATER_ALM,
        IS_ACK_ON_RTN = NEW.IS_ACK_ON_RTN,
        DEL_ACT_ON_ACK = NEW.DEL_ACT_ON_ACK,
        IS_NEVER_ALM_ON_RTN = NEW.IS_NEVER_ALM_ON_RTN,
        POINT_TP_NAME = NEW.TAG_NAME
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
     END IF;
    IF NEW.RANGE_UP <> OLD.RANGE_UP OR NEW.RANGE_LOW <> OLD.RANGE_LOW OR NEW.IS_FILTER <> OLD.IS_FILTER OR NEW.PERCENT <> OLD.PERCENT OR
    NEW.BASE <> OLD.BASE OR NEW.COEFF <> OLD.COEFF 
    THEN
        UPDATE accuml_map_to_fes SET 
        RANGE_UP = NEW.RANGE_UP,
        RANGE_LOW = NEW.RANGE_LOW,
        IS_FILTER = NEW.IS_FILTER,
        PERCENT = NEW.PERCENT,
        BASE = NEW.BASE,
        COEFF = NEW.COEFF
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    */
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
        UPDATE temp_plugin_point_map_acc
        SET TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME)
        WHERE TEMP_NAME = OLD.DEV_TP_NAME AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    END IF;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'accuml_temp_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('accuml_temp_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_ACCUML_TEMP_DEFINE;
DELIMITER ;;
create trigger TD_ACCUML_TEMP_DEFINE BEFORE DELETE on accuml_temp_define for each ROW 
BEGIN
    
    DELETE FROM temp_plugin_point_map_acc WHERE TEMP_NAME = OLD.DEV_TP_NAME  AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
/*
DELETE FROM accuml WHERE POINT_TP_NAME = OLD.TAG_NAME;
*/
END;;
DELIMITER ;

drop trigger if exists TDA_ACCUML_TEMP_DEFINE;
DELIMITER ;;
create trigger TDA_ACCUML_TEMP_DEFINE AFTER DELETE on accuml_temp_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'accuml_temp_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('accuml_temp_define'));
END IF;
END;;
DELIMITER ;

drop table if exists alarm_link_enable_info;
CREATE TABLE `alarm_link_enable_info` (
  `TAG_NAME` varchar(128) NOT NULL COMMENT '名称',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `ENABLE` decimal(10,0) NOT NULL COMMENT '是否启用',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警联动启用信息表';

drop trigger if exists TIA_ALARM_LINK_ENABLE_INFO;
DELIMITER ;;
create trigger TIA_ALARM_LINK_ENABLE_INFO AFTER INSERT on alarm_link_enable_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'alarm_link_enable_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('alarm_link_enable_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_LINK_ENABLE_INFO;
DELIMITER ;;
create trigger TUA_ALARM_LINK_ENABLE_INFO AFTER UPDATE on alarm_link_enable_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'alarm_link_enable_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('alarm_link_enable_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_LINK_ENABLE_INFO;
DELIMITER ;;
create trigger TDA_ALARM_LINK_ENABLE_INFO AFTER DELETE on alarm_link_enable_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'alarm_link_enable_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('alarm_link_enable_info'));
END IF;
END;;
DELIMITER ;

drop table if exists alarm_sms_config;
CREATE TABLE `alarm_sms_config` (
  `NO` decimal(10,0) NOT NULL COMMENT '序号',
  `NAME` varchar(128) NOT NULL COMMENT '名称',
  `ALARM_LEVEL` decimal(10,0) NOT NULL COMMENT '告警等级',
  `USER_GROUP` varchar(128) NOT NULL COMMENT '用户组',
  `CONTACT` varchar(512) NOT NULL COMMENT '短信接收人',
  `FILTER_RULE` decimal(10,0) NOT NULL COMMENT '过滤规则',
  `DEVICE_ID` decimal(10,0) NOT NULL COMMENT '短信设备',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='短信发送配置表';

drop trigger if exists TIA_ALARM_SMS_CONFIG;
DELIMITER ;;
create trigger TIA_ALARM_SMS_CONFIG AFTER INSERT on alarm_sms_config for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'alarm_sms_config', 'NO', '', NEW.NO, getTableSubSystem('alarm_sms_config'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_SMS_CONFIG;
DELIMITER ;;
create trigger TUA_ALARM_SMS_CONFIG AFTER UPDATE on alarm_sms_config for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'alarm_sms_config', 'NO', OLD.NO, NEW.NO, getTableSubSystem('alarm_sms_config'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_SMS_CONFIG;
DELIMITER ;;
create trigger TDA_ALARM_SMS_CONFIG AFTER DELETE on alarm_sms_config for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'alarm_sms_config', 'NO', OLD.NO, '', getTableSubSystem('alarm_sms_config'));
END IF;
END;;
DELIMITER ;

drop table if exists alarm_sms_device;
CREATE TABLE `alarm_sms_device` (
  `DEVICE_ID` decimal(10,0) NOT NULL COMMENT '设备编号',
  `DEVICE_NAME` varchar(64) NOT NULL COMMENT '设备名称',
  `PORT_TYPE` decimal(10,0) NOT NULL COMMENT '端口类型',
  `IP_ADDRESS` varchar(32) NOT NULL COMMENT 'IP地址',
  `NET_PORT` varchar(32) NOT NULL COMMENT '网络端口',
  `COMM_PORT` varchar(64) NOT NULL COMMENT '串口地址',
  `COMM_BAUDRATE` varchar(32) NOT NULL COMMENT '串口波特率',
  `SMS_CENTER` varchar(32) NOT NULL COMMENT '短信中心号',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '设备备注',
  PRIMARY KEY (`DEVICE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='短信设备信息表';

drop trigger if exists TIA_ALARM_SMS_DEVICE;
DELIMITER ;;
create trigger TIA_ALARM_SMS_DEVICE AFTER INSERT on alarm_sms_device for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'alarm_sms_device', 'DEVICE_ID', '', NEW.DEVICE_ID, getTableSubSystem('alarm_sms_device'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_SMS_DEVICE;
DELIMITER ;;
create trigger TUA_ALARM_SMS_DEVICE AFTER UPDATE on alarm_sms_device for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'alarm_sms_device', 'DEVICE_ID', OLD.DEVICE_ID, NEW.DEVICE_ID, getTableSubSystem('alarm_sms_device'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_SMS_DEVICE;
DELIMITER ;;
create trigger TDA_ALARM_SMS_DEVICE AFTER DELETE on alarm_sms_device for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'alarm_sms_device', 'DEVICE_ID', OLD.DEVICE_ID, '', getTableSubSystem('alarm_sms_device'));
END IF;
END;;
DELIMITER ;

drop table if exists alarm_sms_rule;
CREATE TABLE `alarm_sms_rule` (
  `RULE_ID` decimal(10,0) NOT NULL COMMENT '规则编号',
  `RULE_NAME` varchar(64) NOT NULL COMMENT '规则名称',
  `DEV_PERM_TYPE` decimal(10,0) NOT NULL COMMENT '设备允许类型',
  `DEVICE_LIST` varchar(1024) NOT NULL COMMENT '设备列表',
  `ALM_PERM_TYPE` decimal(10,0) NOT NULL COMMENT '告警允许类型',
  `ALM_STATUS_LIST` varchar(512) NOT NULL COMMENT '告警列表',
  PRIMARY KEY (`RULE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='短信过滤规则表';

drop trigger if exists TIA_ALARM_SMS_RULE;
DELIMITER ;;
create trigger TIA_ALARM_SMS_RULE AFTER INSERT on alarm_sms_rule for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'alarm_sms_rule', 'RULE_ID', '', NEW.RULE_ID, getTableSubSystem('alarm_sms_rule'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_SMS_RULE;
DELIMITER ;;
create trigger TUA_ALARM_SMS_RULE AFTER UPDATE on alarm_sms_rule for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'alarm_sms_rule', 'RULE_ID', OLD.RULE_ID, NEW.RULE_ID, getTableSubSystem('alarm_sms_rule'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_SMS_RULE;
DELIMITER ;;
create trigger TDA_ALARM_SMS_RULE AFTER DELETE on alarm_sms_rule for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'alarm_sms_rule', 'RULE_ID', OLD.RULE_ID, '', getTableSubSystem('alarm_sms_rule'));
END IF;
END;;
DELIMITER ;

drop table if exists analog_control;
CREATE TABLE `analog_control` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL,
  `IS_TAGT_STATE` decimal(10,0) DEFAULT NULL COMMENT '是否等待目标状态',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `RESV_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制保留时间',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基数',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数',
  `RANGE_UP` decimal(22,6) DEFAULT NULL COMMENT '度下限--',
  `RANGE_LOW` decimal(22,6) DEFAULT NULL COMMENT '量程下限',
  `CTRL_TOLERANCE` decimal(22,6) DEFAULT NULL COMMENT '控制目标误差',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT 'RTU标签',
  `OFFSET_NO` varchar(48) DEFAULT NULL COMMENT 'AO点号',
  `CTRL_ALIAS` varchar(128) DEFAULT NULL COMMENT '遥控别名',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_ANALOG_CONTROL` (`RTU_TAG`,`OFFSET_NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模拟量输出参数表';

drop trigger if exists TIA_ANALOG_CONTROL;
DELIMITER ;;
create trigger TIA_ANALOG_CONTROL AFTER INSERT on analog_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'analog_control', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('analog_control'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_ANALOG_CONTROL;
DELIMITER ;;
create trigger TU_ANALOG_CONTROL BEFORE UPDATE on analog_control for each ROW 
body:BEGIN
    
    #修改功能名称
    IF COALESCE(NEW.RTU_TAG,'') = '' THEN
    BEGIN
        SET NEW.OFFSET_NO = '-1';
    END;
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_ANALOG_CONTROL;
DELIMITER ;;
create trigger TUA_ANALOG_CONTROL AFTER UPDATE on analog_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'analog_control', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('analog_control'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ANALOG_CONTROL;
DELIMITER ;;
create trigger TDA_ANALOG_CONTROL AFTER DELETE on analog_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'analog_control', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('analog_control'));
END IF;
END;;
DELIMITER ;

drop table if exists analog_limit_info;
CREATE TABLE `analog_limit_info` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `LIMIT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '越限类型',
  `LIMIT_NUM` decimal(10,0) DEFAULT NULL COMMENT '越限级别数量',
  `LIMIT_UP1` decimal(22,6) DEFAULT NULL COMMENT '一级上限',
  `LIMIT_LOW1` decimal(22,6) DEFAULT NULL COMMENT '一级下限',
  `LIMIT_UP2` decimal(22,6) DEFAULT NULL COMMENT '二级上限',
  `LIMIT_LOW2` decimal(22,6) DEFAULT NULL COMMENT '二级下限',
  `LIMIT_UP3` decimal(22,6) DEFAULT NULL COMMENT '三级上限',
  `LIMIT_LOW3` decimal(22,6) DEFAULT NULL COMMENT '三级下限',
  `CROSS_PERCENT` decimal(22,6) DEFAULT NULL COMMENT '越线百分比',
  `RETURN_PERCENT` decimal(22,6) DEFAULT NULL COMMENT '返回百分比',
  `CROSS_TIME` decimal(20,0) DEFAULT NULL COMMENT '最新越线时间',
  `IS_OBJ_ALM_MANNER` decimal(10,0) DEFAULT '0' COMMENT '是否自定义告警',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '告警优先级',
  `PIC_NAME` varchar(64) DEFAULT NULL COMMENT '越限联动画面',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延迟时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否确认自动返回',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认删除动作',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名称',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模拟量越限参数表';

drop trigger if exists TIA_ANALOG_LIMIT_INFO;
DELIMITER ;;
create trigger TIA_ANALOG_LIMIT_INFO AFTER INSERT on analog_limit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'analog_limit_info', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('analog_limit_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ANALOG_LIMIT_INFO;
DELIMITER ;;
create trigger TUA_ANALOG_LIMIT_INFO AFTER UPDATE on analog_limit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'analog_limit_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('analog_limit_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ANALOG_LIMIT_INFO;
DELIMITER ;;
create trigger TDA_ANALOG_LIMIT_INFO AFTER DELETE on analog_limit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'analog_limit_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('analog_limit_info'));
END IF;
END;;
DELIMITER ;

drop table if exists analog_map_to_fes;
CREATE TABLE `analog_map_to_fes` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '标签点名（表名.测点标签）',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名称',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU',
  `DOT_NO` varchar(48) DEFAULT NULL COMMENT '前置序号',
  `IS_FILTER` decimal(10,0) DEFAULT NULL COMMENT '是否过滤突变',
  `PERCENT` decimal(10,0) DEFAULT NULL COMMENT '突变百分比',
  `IS_VALID` decimal(10,0) DEFAULT NULL COMMENT '是否有效',
  `DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '死区值',
  `ZEROBAND` decimal(22,6) DEFAULT NULL COMMENT '归零区值',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基数',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数',
  `RANGE_UP` decimal(22,6) DEFAULT NULL COMMENT '量程上限 ',
  `RANGE_LOW` decimal(22,6) DEFAULT NULL COMMENT '量程下限',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模拟量前置映射表';

drop trigger if exists TI_ANALOG_MAP_TO_FES;
DELIMITER ;;
create trigger TI_ANALOG_MAP_TO_FES AFTER INSERT on analog_map_to_fes for each ROW 
BEGIN
    
    
    SET @disableFesAnalogTrigger = 1;
    
    IF COALESCE(@disableAnalogMapToFesTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN
                UPDATE fes_analog SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
    SET @disableFesAnalogTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'analog_map_to_fes', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('analog_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_ANALOG_MAP_TO_FES;
DELIMITER ;;
create trigger TU_ANALOG_MAP_TO_FES BEFORE UPDATE on analog_map_to_fes for each ROW 
body_label:BEGIN
    
    IF NEW.RTU_TAG = OLD.RTU_TAG AND 
        NEW.DOT_NO = OLD.DOT_NO AND 
        NEW.KEY_ID_TAG = OLD.KEY_ID_TAG
        THEN LEAVE body_label;
    END IF;
        
    SET @disableFesAnalogTrigger = 1;
    
    IF COALESCE(@disableAnalogMapToFesTrigger,0) = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN
                UPDATE fes_analog SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
        
        
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN
                UPDATE fes_analog SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO); 
            END IF;
            
        END;
        ELSE SET NEW.DOT_NO = '-1';
        END IF;
     END;
     END IF;
    
     SET @disableFesAnalogTrigger = 0;
END;;
DELIMITER ;

drop trigger if exists TUA_ANALOG_MAP_TO_FES;
DELIMITER ;;
create trigger TUA_ANALOG_MAP_TO_FES AFTER UPDATE on analog_map_to_fes for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'analog_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('analog_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_ANALOG_MAP_TO_FES;
DELIMITER ;;
create trigger TD_ANALOG_MAP_TO_FES BEFORE DELETE on analog_map_to_fes for each ROW 
body_label:BEGIN
        
    SET @disableFesAnalogTrigger = 1;
    
    IF COALESCE(@disableAnalogMapToFesTrigger,0) = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN
                UPDATE fes_analog SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
    SET @disableFesAnalogTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TDA_ANALOG_MAP_TO_FES;
DELIMITER ;;
create trigger TDA_ANALOG_MAP_TO_FES AFTER DELETE on analog_map_to_fes for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'analog_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('analog_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop table if exists analog_temp_define;
CREATE TABLE `analog_temp_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '点标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `DEV_TP_NAME` varchar(64) NOT NULL COMMENT '设备模板名称',
  `SEQ_NO` decimal(10,0) NOT NULL COMMENT '点序号',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类型',
  `POINT_CLASS` decimal(10,0) DEFAULT NULL COMMENT '点分类 ',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点属性（采集点/计算点/虚拟点...）',
  `POINT_SORT` decimal(10,0) DEFAULT NULL COMMENT '测点种类',
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位ID',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否采样',
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL COMMENT '是否统计',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL,
  `SAMPLE_DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '采样死区',
  `IS_LIMIT` decimal(10,0) DEFAULT NULL COMMENT '是否越限告警',
  `LIMIT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '越限告警类型',
  `LIMIT_NUM` decimal(10,0) DEFAULT NULL COMMENT '越限数量',
  `LIMIT_UP1` decimal(22,6) DEFAULT NULL COMMENT '一级越限上',
  `LIMIT_LOW1` decimal(22,6) DEFAULT NULL COMMENT '一级越限下',
  `LIMIT_UP2` decimal(22,6) DEFAULT NULL COMMENT '二级越限上',
  `LIMIT_LOW2` decimal(22,6) DEFAULT NULL COMMENT '二级越限下',
  `LIMIT_UP3` decimal(22,6) DEFAULT NULL COMMENT '三级越限上',
  `LIMIT_LOW3` decimal(22,6) DEFAULT NULL COMMENT '三级越限下',
  `CROSS_PERCENT` decimal(22,6) DEFAULT NULL COMMENT 'CROSS_PERCENT',
  `RETURN_PERCENT` decimal(22,6) DEFAULT NULL COMMENT 'RETURN_PERCENT',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '告警优先级',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延时时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回自动确认',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认后自动删除',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  `IS_FILTER` decimal(10,0) DEFAULT NULL COMMENT '是否过滤突变',
  `PERCENT` decimal(10,0) DEFAULT NULL COMMENT '突变百分比',
  `IS_VALID` decimal(10,0) DEFAULT NULL COMMENT '是否有效1',
  `DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '采集死区',
  `ZEROBAND` decimal(22,6) DEFAULT NULL COMMENT '采集归零死区',
  `IS_CONTROL` decimal(10,0) DEFAULT NULL COMMENT '是否控制',
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL,
  `IS_TAGT_STATE` decimal(10,0) DEFAULT NULL COMMENT '是否等待目标状态',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `RESV_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制保留时间',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基数',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数',
  `RANGE_UP` decimal(22,6) DEFAULT NULL COMMENT '量程最大值',
  `RANGE_LOW` decimal(22,6) DEFAULT NULL COMMENT '量程最小值 ',
  `CTRL_TOLERANCE` decimal(22,6) DEFAULT NULL COMMENT '控制目标误差',
  `SEC_DEV_IN_GRP` decimal(10,0) DEFAULT NULL,
  `SEC_DEVTP_NAME` varchar(64) DEFAULT NULL COMMENT '二次设备模板',
  `SEC_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '输入点号',
  `OUT_SEC_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '输出点号',
  `KEY_VALUE` decimal(10,0) DEFAULT NULL COMMENT '关键数据标志',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模拟量模板表';

drop trigger if exists TI_ANALOG_TEMP_DEFINE;
DELIMITER ;;
create trigger TI_ANALOG_TEMP_DEFINE AFTER INSERT on analog_temp_define for each ROW 
BEGIN
    
/*
INSERT INTO analog(
TAG_NAME,                                               DESCRIPTION,     RTU_TAG, DEV_TYPE, DEVICE,  SEQ_NO,    LOCATION_ID, SUB_SYSTEM, IS_CONTROL,    GRAY, CONTRAST, QCOLOR, VALUE, STATUS, LAST_UPDATE_TIME, LAST_CHANGE_TIME, UNIT_ID,     SOUND_NAME1, SOUND_NAME2, SOUND_NAME3, REGION_ID, OPT_HANDOVER_GROUP, IS_LIMIT,    POINT_TYPE,   POINT_CLASS,    POINT_PROPERTY,    POINT_SORT,   POINT_TP_NAME, IS_SAMPLE,    IS_STATISTICS,   SAMPLE_PERIOD,    SAMPLE_DEADBAND,   CAMERA_TAG, PRESET_ID) 
SELECT 
concat(TAG_NAME, '.' , getAfterLastDot(NEW.TAG_NAME)) , NEW.DESCRIPTION ,'',      DEV_TYPE, TAG_NAME,NEW.SEQ_NO,LOCATION_ID, SUB_SYSTEM, NEW.IS_CONTROL,GRAY, CONTRAST, QCOLOR, VALUE, STATUS,  0,                0,               NEW.UNIT_ID, '',          '',          '',          REGION_ID, OPT_HANDOVER_GROUP,NEW.IS_LIMIT,NEW.POINT_TYPE,NEW.POINT_CLASS,NEW.POINT_PROPERTY,NEW.POINT_SORT,NEW.TAG_NAME, NEW.IS_SAMPLE,NEW.IS_STATISTICS,NEW.SAMPLE_PERIOD,NEW.SAMPLE_DEADBAND, '',      ''
FROM dev_info WHERE DEV_TP_NAME = NEW.DEV_TP_NAME;
*/
    
    INSERT INTO temp_plugin_point_map_ana
    (PLUGIN_NAME, TEMP_NAME, TEMP_POINT_NAME,            FES_TEMP_NAME, FES_TEMP_POINT_NAME, FES_TEMP_OUT_NAME)
    SELECT 
    PLUGIN_NAME, TEMP_NAME, getAfterLastDot(NEW.TAG_NAME),'','',''
    FROM temp_plugin_dev_map WHERE TEMP_NAME = NEW.DEV_TP_NAME;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'analog_temp_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('analog_temp_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_ANALOG_TEMP_DEFINE;
DELIMITER ;;
create trigger TU_ANALOG_TEMP_DEFINE AFTER UPDATE on analog_temp_define for each ROW 
BEGIN
    /*
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.DESCRIPTION <> OLD.DESCRIPTION OR NEW.SEQ_NO <> OLD.SEQ_NO OR NEW.IS_CONTROL <> OLD.IS_CONTROL OR NEW.UNIT_ID <> OLD.UNIT_ID OR
    NEW.IS_LIMIT <> OLD.IS_LIMIT OR NEW.POINT_TYPE <> OLD.POINT_TYPE OR NEW.POINT_CLASS <> OLD.POINT_CLASS OR NEW.POINT_PROPERTY <> OLD.POINT_PROPERTY OR NEW.POINT_SORT <> OLD.POINT_SORT OR
    NEW.IS_SAMPLE <> OLD.IS_SAMPLE OR NEW.IS_STATISTICS <> OLD.IS_STATISTICS OR NEW.SAMPLE_PERIOD <> OLD.SAMPLE_PERIOD OR NEW.SAMPLE_DEADBAND <> OLD.SAMPLE_DEADBAND 
    THEN 
        UPDATE analog  SET 
        TAG_NAME = concat(DEVICE, '.', getAfterLastDot(NEW.TAG_NAME)),
        DESCRIPTION = NEW.DESCRIPTION,
        SEQ_NO = NEW.SEQ_NO,
        IS_CONTROL = NEW.IS_CONTROL,
        UNIT_ID = NEW.UNIT_ID,
        IS_LIMIT = NEW.IS_LIMIT,
        POINT_TYPE = NEW.POINT_TYPE,
        POINT_CLASS = NEW.POINT_CLASS,
        POINT_PROPERTY = NEW.POINT_PROPERTY,
        POINT_SORT = NEW.POINT_SORT,
        POINT_TP_NAME = NEW.TAG_NAME,
        IS_SAMPLE = NEW.IS_SAMPLE,
        IS_STATISTICS = NEW.IS_STATISTICS,
        SAMPLE_PERIOD = NEW.SAMPLE_PERIOD,
        SAMPLE_DEADBAND = NEW.SAMPLE_DEADBAND
        WHERE POINT_TP_NAME=OLD.TAG_NAME;
    END IF;
    
    IF NEW.IS_TAGT_STATE <> OLD.IS_TAGT_STATE OR NEW.CTRL_TIMEOUT <> OLD.CTRL_TIMEOUT OR NEW.RESV_TIMEOUT <> OLD.RESV_TIMEOUT OR NEW.BASE <> OLD.BASE OR NEW.COEFF <> OLD.COEFF OR
    NEW.RANGE_UP <> OLD.RANGE_UP OR NEW.RANGE_LOW <> OLD.RANGE_LOW OR NEW.CTRL_TOLERANCE <> OLD.CTRL_TOLERANCE OR NEW.TAG_NAME <> OLD.TAG_NAME 
    THEN
        UPDATE analog_control SET
        IS_TAGT_STATE = NEW.IS_TAGT_STATE,
        CTRL_TIMEOUT = NEW.CTRL_TIMEOUT,
        RESV_TIMEOUT = NEW.RESV_TIMEOUT,
        BASE = NEW.BASE,
        COEFF = NEW.COEFF,
        RANGE_UP = NEW.RANGE_UP,
        RANGE_LOW = NEW.RANGE_LOW,
        CTRL_TOLERANCE = NEW.CTRL_TOLERANCE,
        POINT_TP_NAME = NEW.TAG_NAME
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    
    IF NEW.LIMIT_TYPE <> OLD.LIMIT_TYPE OR NEW.LIMIT_NUM <> OLD.LIMIT_NUM OR NEW.LIMIT_UP1 <> OLD.LIMIT_UP1 OR NEW.LIMIT_LOW1 <> OLD.LIMIT_LOW1 OR NEW.LIMIT_UP2 <> OLD.LIMIT_UP2 OR
    NEW.LIMIT_LOW2 <> OLD.LIMIT_LOW2 OR NEW.LIMIT_UP3 <> OLD.LIMIT_UP3 OR NEW.LIMIT_LOW3 <> OLD.LIMIT_LOW3 OR NEW.CROSS_PERCENT <> OLD.CROSS_PERCENT OR NEW.RETURN_PERCENT <> OLD.RETURN_PERCENT OR
    NEW.ALARM_PRIORITY <> OLD.ALARM_PRIORITY OR NEW.ALARM_DELAY_TIME <> OLD.ALARM_DELAY_TIME OR NEW.IS_WATER_ALM <> OLD.IS_WATER_ALM OR NEW.IS_ACK_ON_RTN <> OLD.IS_ACK_ON_RTN OR
    NEW.DEL_ACT_ON_ACK <> OLD.DEL_ACT_ON_ACK OR NEW.IS_NEVER_ALM_ON_RTN <> OLD.IS_NEVER_ALM_ON_RTN OR NEW.TAG_NAME <> OLD.TAG_NAME 
    THEN 
        UPDATE analog_limit_info SET
        LIMIT_TYPE = NEW.LIMIT_TYPE ,
        LIMIT_NUM = NEW.LIMIT_NUM ,
        LIMIT_UP1 = NEW.LIMIT_UP1 ,
        LIMIT_LOW1 = NEW.LIMIT_LOW1 ,
        LIMIT_UP2 = NEW.LIMIT_UP2 ,
        LIMIT_LOW2 = NEW.LIMIT_LOW2 ,
        LIMIT_UP3 = NEW.LIMIT_UP3 ,
        LIMIT_LOW3 = NEW.LIMIT_LOW3 ,
        CROSS_PERCENT = NEW.CROSS_PERCENT ,
        RETURN_PERCENT = NEW.RETURN_PERCENT ,
        ALARM_PRIORITY = NEW.ALARM_PRIORITY ,
        ALARM_DELAY_TIME = NEW.ALARM_DELAY_TIME ,
        IS_WATER_ALM = NEW.IS_WATER_ALM ,
        IS_ACK_ON_RTN = NEW.IS_ACK_ON_RTN ,
        DEL_ACT_ON_ACK = NEW.DEL_ACT_ON_ACK ,
        IS_NEVER_ALM_ON_RTN = NEW.IS_NEVER_ALM_ON_RTN,
        POINT_TP_NAME = NEW.TAG_NAME
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.IS_FILTER <> OLD.IS_FILTER OR NEW.PERCENT <> OLD.PERCENT OR NEW.IS_VALID <> OLD.IS_VALID OR NEW.DEADBAND <> OLD.DEADBAND OR
    NEW.ZEROBAND <> OLD.ZEROBAND OR NEW.BASE <> OLD.BASE OR NEW.COEFF <> OLD.COEFF OR NEW.RANGE_UP <> OLD.RANGE_UP OR NEW.RANGE_LOW <> OLD.RANGE_LOW 
    THEN
        UPDATE analog_map_to_fes SET
        POINT_TP_NAME = NEW.TAG_NAME,
        IS_FILTER = NEW.IS_FILTER,
        PERCENT =  NEW.PERCENT,
        IS_VALID =  NEW.IS_VALID,
        DEADBAND =  NEW.DEADBAND,
        ZEROBAND =  NEW.ZEROBAND,
        BASE = NEW.BASE,
        COEFF = NEW.COEFF,
        RANGE_UP = NEW.RANGE_UP,
        RANGE_LOW = NEW.RANGE_LOW
        WHERE POINT_TP_NAME = OLD.TAG_NAME;
    END IF;
    */
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
        UPDATE temp_plugin_point_map_ana
        SET TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME)
        WHERE TEMP_NAME = OLD.DEV_TP_NAME AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    END IF;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'analog_temp_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('analog_temp_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_ANALOG_TEMP_DEFINE;
DELIMITER ;;
create trigger TD_ANALOG_TEMP_DEFINE BEFORE DELETE on analog_temp_define for each ROW 
BEGIN
    DELETE FROM temp_plugin_point_map_ana WHERE TEMP_NAME = OLD.DEV_TP_NAME  AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
/*
    DELETE FROM analog WHERE POINT_TP_NAME = OLD.TAG_NAME;
*/
END;;
DELIMITER ;

drop trigger if exists TDA_ANALOG_TEMP_DEFINE;
DELIMITER ;;
create trigger TDA_ANALOG_TEMP_DEFINE AFTER DELETE on analog_temp_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'analog_temp_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('analog_temp_define'));
END IF;
END;;
DELIMITER ;

drop table if exists apc_analog;
CREATE TABLE `apc_analog` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '测点唯一标识，表示此配置的值输出到该测点。 配置工具选择AI计算量点进行配置。新建后不允许修改。',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述。 配置工具文本输入。',
  `TIMEOUT_OUTPUT_DI` varchar(128) DEFAULT NULL COMMENT '超时状态输出DI点，关联一个本站，本专业的计算量DI点的KEY_ID_TAG。 超时将该点值设为1，否则设为0。可用于FBD逻辑。',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '位置ID。 配置工具自动从KEY_ID_TAG测点的位置ID复制。',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业ID。 配置工具自动从KEY_ID_TAG测点的专业ID复制。',
  `RCV_CTRL_TIMEOUT` decimal(10,0) NOT NULL COMMENT '接收控制指令超时时间，单位ms，小于等于0表示不使用。 用于实现超时未收到调度指令的告警及后续逻辑（比如切换本地值）。',
  `VALUE` decimal(22,6) NOT NULL COMMENT '值，比如投入1，退出0。 配置工具可配，接口服务接收遥控后通过模型修改服务修改。',
  `NO_RDB_SYNC` decimal(3,0) DEFAULT NULL COMMENT '接口服务自己保存的value，不能更新到实时库，否则可能抖动。当此列写入1时，触发器不产生sys_trigger_info记录。',
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='功率控制接口服务模拟量配置表。';

drop trigger if exists TIA_APC_ANALOG;
DELIMITER ;;
create trigger TIA_APC_ANALOG AFTER INSERT on apc_analog for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_analog', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('apc_analog'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUB_APC_ANALOG;
DELIMITER ;;
create trigger TUB_APC_ANALOG BEFORE UPDATE on apc_analog for each ROW 
BEGIN
IF NEW.NO_RDB_SYNC = 1 THEN
SET NEW.NO_RDB_SYNC = NULL;
ELSEIF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_analog', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('apc_analog'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_APC_ANALOG;
DELIMITER ;;
create trigger TDA_APC_ANALOG AFTER DELETE on apc_analog for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_analog', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('apc_analog'));
END IF;
END;;
DELIMITER ;

drop table if exists apc_curve;
CREATE TABLE `apc_curve` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '输出测点唯一标识，也作为曲线唯一标识，主键',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `TYPE_ID` decimal(10,0) NOT NULL COMMENT '类型ID',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业ID',
  `CURRENT_DAY_RULE_ID` decimal(10,0) DEFAULT NULL COMMENT '今天的日规则，非配置，由接口服务输出到此，以方便界面使用',
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计划曲线';

drop trigger if exists TIA_apc_curve;
DELIMITER ;;
create trigger TIA_apc_curve AFTER INSERT on apc_curve for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('apc_curve'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_apc_curve;
DELIMITER ;;
create trigger TUA_apc_curve AFTER UPDATE on apc_curve for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('apc_curve'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_apc_curve;
DELIMITER ;;
create trigger TDA_apc_curve AFTER DELETE on apc_curve for each ROW 
BEGIN
delete from apc_curve_day_index where curve_key_id_tag = old.key_id_tag;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('apc_curve'));
END IF;
END;;
DELIMITER ;

drop table if exists apc_curve_day_index;
CREATE TABLE `apc_curve_day_index` (
  `CURVE_KEY_ID_TAG` varchar(128) NOT NULL COMMENT '曲线标识，对应APC_CURVE表的KEY_ID_TAG',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `YEAR_RULE_ID` decimal(10,0) NOT NULL COMMENT '年规则ID',
  `WEEK_RULE_ID` decimal(10,0) NOT NULL COMMENT '周规则ID',
  `DAY_RULE_ID` decimal(10,0) NOT NULL COMMENT '日规则ID',
  PRIMARY KEY (`CURVE_KEY_ID_TAG`,`YEAR_RULE_ID`,`WEEK_RULE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计划曲线日索引';

drop trigger if exists TIA_APC_CURVE_DAY_INDEX;
DELIMITER ;;
create trigger TIA_APC_CURVE_DAY_INDEX AFTER INSERT on apc_curve_day_index for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                             KEY_OLD_VALUE,KEY_NEW_VALUE,                                                          SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_day_index', 'curve_key_id_tag,year_rule_id,week_rule_id', '',          concat(NEW.CURVE_KEY_ID_TAG,',',NEW.YEAR_RULE_ID,',',NEW.WEEK_RULE_ID), getTableSubSystem('apc_curve_day_index'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_APC_CURVE_DAY_INDEX;
DELIMITER ;;
create trigger TUA_APC_CURVE_DAY_INDEX AFTER UPDATE on apc_curve_day_index for each ROW 
BEGIN
IF((select count(*) from apc_curve_day_index where day_rule_id = old.day_rule_id)<1) THEN
delete from apc_curve_day_value where day_rule_id = old.day_rule_id;
END IF;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                              KEY_OLD_VALUE,                                                          KEY_NEW_VALUE,                                                          SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve_day_index', 'curve_key_id_tag,year_rule_id,week_rule_id', concat(OLD.CURVE_KEY_ID_TAG,',',OLD.YEAR_RULE_ID,',',OLD.WEEK_RULE_ID), concat(NEW.CURVE_KEY_ID_TAG,',',NEW.YEAR_RULE_ID,',',NEW.WEEK_RULE_ID), getTableSubSystem('apc_curve_day_index'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_APC_CURVE_DAY_INDEX;
DELIMITER ;;
create trigger TDA_APC_CURVE_DAY_INDEX AFTER DELETE on apc_curve_day_index for each ROW 
BEGIN
delete from apc_curve_day_value where day_rule_id = old.day_rule_id;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                             KEY_OLD_VALUE,                                                         KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_day_index', 'curve_key_id_tag,year_rule_id,week_rule_id',concat(OLD.CURVE_KEY_ID_TAG,',',OLD.YEAR_RULE_ID,',',OLD.WEEK_RULE_ID), '', getTableSubSystem('apc_curve_day_index'));
END IF;
END;;
DELIMITER ;

drop table if exists apc_curve_day_tmpl;
CREATE TABLE `apc_curve_day_tmpl` (
  `ID` decimal(10,0) NOT NULL COMMENT '日时段模板ID',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '时段描述',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计划曲线日模板定义';

drop table if exists apc_curve_day_tmpl_section;
CREATE TABLE `apc_curve_day_tmpl_section` (
  `DAY_TMPL_ID` decimal(10,0) NOT NULL COMMENT '所属日模板ID',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '时段描述',
  `START_SEC` decimal(10,0) NOT NULL COMMENT '开始秒数',
  `END_SEC` decimal(10,0) NOT NULL COMMENT '结束秒数',
  PRIMARY KEY (`DAY_TMPL_ID`,`START_SEC`,`END_SEC`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计划曲线日模板时段定义';

drop table if exists apc_curve_day_value;
CREATE TABLE `apc_curve_day_value` (
  `DAY_RULE_ID` decimal(10,0) NOT NULL COMMENT '日规则ID',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `START_SEC` decimal(10,0) NOT NULL COMMENT '开始秒数，从0开始，包含该秒，即从该秒开始时起',
  `END_SEC` decimal(10,0) NOT NULL COMMENT '结束秒数，包含该秒，即到下一秒开始时结束',
  `VALUE` decimal(22,6) NOT NULL COMMENT '值',
  PRIMARY KEY (`DAY_RULE_ID`,`START_SEC`,`END_SEC`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计划曲线一天内时间、值定义';

drop trigger if exists TIA_APC_CURVE_DAY_VALUE;
DELIMITER ;;
create trigger TIA_APC_CURVE_DAY_VALUE AFTER INSERT on apc_curve_day_value for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                 KEY_OLD_VALUE,KEY_NEW_VALUE,                       SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_day_value', 'day_rule_id,start_sec,end_sec',  '',          concat(NEW.DAY_RULE_ID,',',NEW.START_SEC,',',NEW.END_SEC), getTableSubSystem('apc_curve_day_value'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_APC_CURVE_DAY_VALUE;
DELIMITER ;;
create trigger TUA_APC_CURVE_DAY_VALUE AFTER UPDATE on apc_curve_day_value for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,                  KEY_OLD_VALUE,                                              KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve_day_value', 'day_rule_id,start_sec,end_sec', concat(OLD.DAY_RULE_ID,',',OLD.START_SEC,',',OLD.END_SEC), concat(NEW.DAY_RULE_ID,',',NEW.START_SEC,',',NEW.END_SEC), getTableSubSystem('apc_curve_day_value'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_APC_CURVE_DAY_VALUE;
DELIMITER ;;
create trigger TDA_APC_CURVE_DAY_VALUE AFTER DELETE on apc_curve_day_value for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                      KEY_OLD_VALUE,                                            KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_day_value', 'day_rule_id,start_sec,end_sec', concat(OLD.DAY_RULE_ID,',',OLD.START_SEC,',',OLD.END_SEC), '',           getTableSubSystem('apc_curve_day_value'));
END IF;
END;;
DELIMITER ;

drop table if exists apc_curve_type;
CREATE TABLE `apc_curve_type` (
  `TYPE_ID` decimal(10,0) NOT NULL COMMENT '类型ID',
  `DESCRIPTION` varchar(255) DEFAULT NULL COMMENT '描述',
  PRIMARY KEY (`TYPE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计划曲线类型';

drop table if exists apc_curve_week;
CREATE TABLE `apc_curve_week` (
  `TYPE_ID` decimal(10,0) NOT NULL COMMENT '类型ID',
  `WEEK_DAY` decimal(10,0) NOT NULL COMMENT '周几，周日为0，周一到周六为1到6',
  `WEEK_RULE_ID` decimal(10,0) NOT NULL COMMENT '周规则ID',
  `DESCRIPTION` varchar(255) DEFAULT NULL COMMENT '描述',
  PRIMARY KEY (`TYPE_ID`,`WEEK_DAY`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计划曲线周';

drop trigger if exists TIA_APC_CURVE_WEEK;
DELIMITER ;;
create trigger TIA_APC_CURVE_WEEK AFTER INSERT on apc_curve_week for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,    KEY_COLUMN_NAME,       KEY_OLD_VALUE,KEY_NEW_VALUE,                       SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_week', 'type_id,week_day',  '',          concat(NEW.TYPE_ID,',',NEW.WEEK_DAY), getTableSubSystem('apc_curve_week'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_APC_CURVE_WEEK;
DELIMITER ;;
create trigger TUA_APC_CURVE_WEEK AFTER UPDATE on apc_curve_week for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,      KEY_OLD_VALUE,                        KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_curve_week', 'type_id,week_day', concat(OLD.TYPE_ID,',',OLD.WEEK_DAY), concat(NEW.TYPE_ID,',',NEW.WEEK_DAY), getTableSubSystem('apc_curve_week'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_APC_CURVE_WEEK;
DELIMITER ;;
create trigger TDA_APC_CURVE_WEEK AFTER DELETE on apc_curve_week for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,      KEY_OLD_VALUE,                    KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_week', 'type_id,week_day', concat(OLD.TYPE_ID,',',OLD.WEEK_DAY), '',           getTableSubSystem('apc_curve_week'));
END IF;
END;;
DELIMITER ;

drop table if exists apc_curve_year;
CREATE TABLE `apc_curve_year` (
  `YEAR_RULE_ID` decimal(10,0) NOT NULL COMMENT '年规则ID',
  `TYPE_ID` decimal(10,0) NOT NULL COMMENT '类型ID',
  `DESCRIPTION` varchar(255) DEFAULT NULL COMMENT '描述',
  `MONTH_BEGIN` decimal(10,0) NOT NULL COMMENT '开始月',
  `DAY_BEGIN` decimal(10,0) NOT NULL COMMENT '开始日，包含该日，即从该日0点起',
  `MONTH_END` decimal(10,0) NOT NULL COMMENT '结束月',
  `DAY_END` decimal(10,0) NOT NULL COMMENT '结束日，包含该日，即到下一日0点结束',
  `EXCTPTION` decimal(10,0) NOT NULL COMMENT '是否例外',
  PRIMARY KEY (`TYPE_ID`,`MONTH_BEGIN`,`DAY_BEGIN`,`MONTH_END`,`DAY_END`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计划曲线年';

drop trigger if exists TIA_APC_CURVE_YEAR;
DELIMITER ;;
create trigger TIA_APC_CURVE_YEAR AFTER INSERT on apc_curve_year for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,       KEY_COLUMN_NAME,                                   KEY_OLD_VALUE,KEY_NEW_VALUE,                       SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_year', 'type_id,month_begin,day_begin,month_end,day_end',  '',          concat(NEW.TYPE_ID,',',NEW.MONTH_BEGIN,',',NEW.DAY_BEGIN,',',NEW.MONTH_END,',',NEW.DAY_END), getTableSubSystem('apc_curve_year'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_APC_CURVE_YEAR;
DELIMITER ;;
create trigger TUA_APC_CURVE_YEAR AFTER UPDATE on apc_curve_year for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                                  KEY_OLD_VALUE,                                                                             KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_year', 'type_id,month_begin,day_begin,month_end,day_end', concat(OLD.TYPE_ID,',',OLD.MONTH_BEGIN,',',OLD.DAY_BEGIN,',',OLD.MONTH_END,',',OLD.DAY_END), '',           getTableSubSystem('apc_curve_year'));
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,       KEY_COLUMN_NAME,                                   KEY_OLD_VALUE,KEY_NEW_VALUE,                       SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_curve_year', 'type_id,month_begin,day_begin,month_end,day_end',  '',          concat(NEW.TYPE_ID,',',NEW.MONTH_BEGIN,',',NEW.DAY_BEGIN,',',NEW.MONTH_END,',',NEW.DAY_END), getTableSubSystem('apc_curve_year'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_APC_CURVE_YEAR;
DELIMITER ;;
create trigger TDA_APC_CURVE_YEAR AFTER DELETE on apc_curve_year for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                                  KEY_OLD_VALUE,                                                                             KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_curve_year', 'type_id,month_begin,day_begin,month_end,day_end', concat(OLD.TYPE_ID,',',OLD.MONTH_BEGIN,',',OLD.DAY_BEGIN,',',OLD.MONTH_END,',',OLD.DAY_END), '',           getTableSubSystem('apc_curve_year'));
END IF;
END;;
DELIMITER ;

drop table if exists apc_digital;
CREATE TABLE `apc_digital` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '测点唯一标识，表示此配置的值输出到该测点。 配置工具选择DI计算量点进行配置。新建后不允许修改。',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述。 配置工具文本输入。',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '位置ID。 配置工具自动从KEY_ID_TAG测点的位置ID复制。',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业ID。 配置工具自动从KEY_ID_TAG测点的专业ID复制。',
  `VALUE` decimal(10,0) NOT NULL COMMENT '值，比如投入1，退出0。 配置工具可配，接口服务接收遥控后通过模型修改服务修改。',
  `NO_RDB_SYNC` decimal(3,0) DEFAULT NULL COMMENT '接口服务自己保存的value，不能更新到实时库，否则可能抖动。当此列写入1时，触发器不产生sys_trigger_info记录。',
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='功率控制接口服务数字量配置表。';

drop trigger if exists TIA_APC_DIGITAL;
DELIMITER ;;
create trigger TIA_APC_DIGITAL AFTER INSERT on apc_digital for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,    KEY_COLUMN_NAME,  KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'apc_digital', 'key_id_tag',      '',           NEW.KEY_ID_TAG, getTableSubSystem('apc_digital'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUB_APC_DIGITAL;
DELIMITER ;;
create trigger TUB_APC_DIGITAL BEFORE UPDATE on apc_digital for each ROW 
BEGIN
IF NEW.NO_RDB_SYNC = 1 THEN
SET NEW.NO_RDB_SYNC = NULL;
ELSEIF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,      KEY_OLD_VALUE,  KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'apc_digital', 'key_id_tag',          OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('apc_digital'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_APC_DIGITAL;
DELIMITER ;;
create trigger TDA_APC_DIGITAL AFTER DELETE on apc_digital for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,     KEY_COLUMN_NAME,   KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'apc_digital', 'key_id_tag',       OLD.KEY_ID_TAG, '',           getTableSubSystem('apc_digital'));
END IF;
END;;
DELIMITER ;

drop table if exists asset_info;
CREATE TABLE `asset_info` (
  `dev_id` varchar(64) NOT NULL COMMENT 'id',
  `dev_name` varchar(64) NOT NULL COMMENT '名称',
  `dev_type` varchar(64) DEFAULT NULL COMMENT '型号',
  `dev_param` varchar(64) DEFAULT NULL COMMENT '参数',
  `setup_date` decimal(20,0) DEFAULT NULL COMMENT '安装日期',
  `dev_status` varchar(64) DEFAULT NULL COMMENT '状态',
  `contact` varchar(255) DEFAULT NULL COMMENT '联系方式',
  `backup` varchar(255) DEFAULT NULL COMMENT '备注',
  `dev_group` varchar(64) DEFAULT NULL COMMENT '设备组',
  `maintain_period` decimal(10,0) DEFAULT NULL COMMENT '维保周期(年)',
  PRIMARY KEY (`dev_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='资产信息表';

drop table if exists asset_records;
CREATE TABLE `asset_records` (
  `record_id` varchar(64) NOT NULL COMMENT '记录id',
  `dev_id` varchar(64) NOT NULL COMMENT '设备id',
  `record_name` varchar(64) DEFAULT NULL COMMENT '记录名称',
  `opt_user` varchar(64) DEFAULT NULL COMMENT '操作人',
  `start_time` decimal(20,0) DEFAULT NULL COMMENT '开始时间',
  `end_time` decimal(20,0) DEFAULT NULL COMMENT '结束时间',
  `content` varchar(255) DEFAULT NULL COMMENT '维护内容',
  PRIMARY KEY (`record_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='资产维护记录表';

drop table if exists ats_casco_dynamic_train_data;
CREATE TABLE `ats_casco_dynamic_train_data` (
  `ZDATE` decimal(10,0) NOT NULL COMMENT '零点时间',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '站号',
  `PLATFORM_ID` decimal(10,0) NOT NULL COMMENT '站台号',
  `TRAIN_ID` decimal(10,0) NOT NULL COMMENT '车次号',
  `GROUP_ID` decimal(10,0) NOT NULL,
  `SERVER_ID` decimal(10,0) NOT NULL,
  `DESTINATION_ID` decimal(10,0) NOT NULL COMMENT '目的地',
  `A_TIME` decimal(10,0) NOT NULL COMMENT '到达时间，1970零点年至今的秒数',
  `D_TIME` decimal(10,0) NOT NULL COMMENT '发车时间，1970零点年至今的秒数',
  `FLAG` decimal(10,0) NOT NULL COMMENT '标志（bit0~7）,bit4:出库 Bit5:入库  bit6:下行',
  `TYPE` decimal(10,0) NOT NULL COMMENT '保留',
  PRIMARY KEY (`ZDATE`,`LOCATION_ID`,`PLATFORM_ID`,`TRAIN_ID`,`GROUP_ID`,`SERVER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='CASCO ATS计划协议表';

drop table if exists ats_casco_planned_train_data;
CREATE TABLE `ats_casco_planned_train_data` (
  `ZDATE` decimal(10,0) NOT NULL COMMENT '零点时间',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '站号',
  `PLATFORM_ID` decimal(10,0) NOT NULL COMMENT '站台号',
  `TABLE_ID` decimal(10,0) NOT NULL COMMENT '计划表号',
  `TRAIN_ID` decimal(10,0) NOT NULL COMMENT '车次号',
  `DESTINATION_ID` decimal(10,0) NOT NULL COMMENT '目的地',
  `A_TIME` decimal(10,0) NOT NULL COMMENT '到达时间，1970零点年至今的秒数',
  `D_TIME` decimal(10,0) NOT NULL COMMENT '发车时间，1970零点年至今的秒数',
  `FLAG` decimal(10,0) NOT NULL COMMENT '标志（bit0~7）,bit4:出库 Bit5:入库  bit6:下行',
  `RESERVE` decimal(10,0) DEFAULT NULL COMMENT '保留',
  PRIMARY KEY (`ZDATE`,`LOCATION_ID`,`PLATFORM_ID`,`TABLE_ID`,`TRAIN_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='CASCO ATS计划列车信息';

drop trigger if exists TIA_ATS_CASCO_PLANNED_TRAIN_DATA;
DELIMITER ;;
create trigger TIA_ATS_CASCO_PLANNED_TRAIN_DATA AFTER INSERT on ats_casco_planned_train_data for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'ats_casco_planned_train_data', 'zdate,location_id,platform_id,table_id,train_id', '', concat(NEW.ZDATE,',',NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.TABLE_ID,',',NEW.TRAIN_ID), getTableSubSystem('ats_casco_planned_train_data'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ATS_CASCO_PLANNED_TRAIN_DATA;
DELIMITER ;;
create trigger TUA_ATS_CASCO_PLANNED_TRAIN_DATA AFTER UPDATE on ats_casco_planned_train_data for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'ats_casco_planned_train_data', 'zdate,location_id,platform_id,table_id,train_id', concat(OLD.ZDATE,',',OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.TABLE_ID,',',OLD.TRAIN_ID), concat(NEW.ZDATE,',',NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.TABLE_ID,',',NEW.TRAIN_ID), getTableSubSystem('ats_casco_planned_train_data'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ATS_CASCO_PLANNED_TRAIN_DATA;
DELIMITER ;;
create trigger TDA_ATS_CASCO_PLANNED_TRAIN_DATA AFTER DELETE on ats_casco_planned_train_data for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'ats_casco_planned_train_data', 'zdate,location_id,platform_id,table_id,train_id', concat(OLD.ZDATE,',',OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.TABLE_ID,',',OLD.TRAIN_ID), '', getTableSubSystem('ats_casco_planned_train_data'));
END IF;
END;;
DELIMITER ;

drop table if exists ats_casco_practical_train_data;
CREATE TABLE `ats_casco_practical_train_data` (
  `ZDATE` decimal(10,0) NOT NULL COMMENT '零点时间',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '站号',
  `PLATFORM_ID` decimal(10,0) NOT NULL COMMENT '站台号',
  `TABLE_ID` decimal(10,0) NOT NULL,
  `TRAIN_ID` decimal(10,0) NOT NULL COMMENT '车次号',
  `GROUP_ID` decimal(10,0) NOT NULL,
  `SERVER_ID` decimal(10,0) NOT NULL COMMENT '服务号',
  `DESTINATION_ID` decimal(10,0) NOT NULL COMMENT '目的地',
  `A_TIME` decimal(10,0) DEFAULT NULL COMMENT '到达时间，1970零点年至今的秒数',
  `D_TIME` decimal(10,0) DEFAULT NULL COMMENT '发车时间，1970零点年至今的秒数',
  `FLAG` decimal(10,0) NOT NULL COMMENT '标志，1:下行 2：上行 0：未知',
  `TYPE` decimal(10,0) NOT NULL COMMENT '1:计划车  0：非计划车',
  `RESERVE` decimal(10,0) DEFAULT NULL COMMENT '保留',
  PRIMARY KEY (`ZDATE`,`LOCATION_ID`,`PLATFORM_ID`,`TABLE_ID`,`TRAIN_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='CASCO ATS实际列车信息';

drop trigger if exists TIA_ATS_CASCO_PRACTICAL_TRAIN_DATA;
DELIMITER ;;
create trigger TIA_ATS_CASCO_PRACTICAL_TRAIN_DATA AFTER INSERT on ats_casco_practical_train_data for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'ats_casco_practical_train_data', 'zdate,location_id,platform_id,table_id,train_id', '', concat(NEW.ZDATE,',',NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.TABLE_ID,',',NEW.TRAIN_ID), getTableSubSystem('ats_casco_practical_train_data'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ATS_CASCO_PRACTICAL_TRAIN_DATA;
DELIMITER ;;
create trigger TUA_ATS_CASCO_PRACTICAL_TRAIN_DATA AFTER UPDATE on ats_casco_practical_train_data for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'ats_casco_practical_train_data', 'zdate,location_id,platform_id,table_id,train_id', concat(OLD.ZDATE,',',OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.TABLE_ID,',',OLD.TRAIN_ID), concat(NEW.ZDATE,',',NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.TABLE_ID,',',NEW.TRAIN_ID), getTableSubSystem('ats_casco_practical_train_data'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ATS_CASCO_PRACTICAL_TRAIN_DATA;
DELIMITER ;;
create trigger TDA_ATS_CASCO_PRACTICAL_TRAIN_DATA AFTER DELETE on ats_casco_practical_train_data for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'ats_casco_practical_train_data', 'zdate,location_id,platform_id,table_id,train_id', concat(OLD.ZDATE,',',OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.TABLE_ID,',',OLD.TRAIN_ID), '', getTableSubSystem('ats_casco_practical_train_data'));
END IF;
END;;
DELIMITER ;

drop table if exists ats_fes_location_info;
CREATE TABLE `ats_fes_location_info` (
  `ATS_LOCATION_ID` decimal(10,0) NOT NULL COMMENT 'ATS车站ID',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT 'ISCS车站号',
  PRIMARY KEY (`ATS_LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置车站配置表';

drop trigger if exists TIA_ATS_FES_LOCATION_INFO;
DELIMITER ;;
create trigger TIA_ATS_FES_LOCATION_INFO AFTER INSERT on ats_fes_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'ats_fes_location_info', 'ats_location_id', '', NEW.ATS_LOCATION_ID, getTableSubSystem('ats_fes_location_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ATS_FES_LOCATION_INFO;
DELIMITER ;;
create trigger TUA_ATS_FES_LOCATION_INFO AFTER UPDATE on ats_fes_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'ats_fes_location_info', 'ats_location_id', OLD.ATS_LOCATION_ID, NEW.ATS_LOCATION_ID, getTableSubSystem('ats_fes_location_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ATS_FES_LOCATION_INFO;
DELIMITER ;;
create trigger TDA_ATS_FES_LOCATION_INFO AFTER DELETE on ats_fes_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'ats_fes_location_info', 'ats_location_id', OLD.ATS_LOCATION_ID, '', getTableSubSystem('ats_fes_location_info'));
END IF;
END;;
DELIMITER ;

drop table if exists ats_fes_platform_info;
CREATE TABLE `ats_fes_platform_info` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT 'ISCS车站号',
  `PLATFORM_ID` decimal(10,0) NOT NULL COMMENT '站台号',
  `PLATFORM_DES` varchar(128) NOT NULL COMMENT '站台描述',
  PRIMARY KEY (`LOCATION_ID`,`PLATFORM_ID`,`PLATFORM_DES`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置站台配置表';

drop trigger if exists TIA_ATS_FES_PLATFORM_INFO;
DELIMITER ;;
create trigger TIA_ATS_FES_PLATFORM_INFO AFTER INSERT on ats_fes_platform_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'ats_fes_platform_info', 'location_id,platform_id,platform_des', '', concat(NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.PLATFORM_DES), getTableSubSystem('ats_fes_platform_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_ATS_FES_PLATFORM_INFO;
DELIMITER ;;
create trigger TUA_ATS_FES_PLATFORM_INFO AFTER UPDATE on ats_fes_platform_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'ats_fes_platform_info', 'location_id,platform_id,platform_des', concat(OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.PLATFORM_DES), concat(NEW.LOCATION_ID,',',NEW.PLATFORM_ID,',',NEW.PLATFORM_DES), getTableSubSystem('ats_fes_platform_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_ATS_FES_PLATFORM_INFO;
DELIMITER ;;
create trigger TDA_ATS_FES_PLATFORM_INFO AFTER DELETE on ats_fes_platform_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'ats_fes_platform_info', 'location_id,platform_id,platform_des', concat(OLD.LOCATION_ID,',',OLD.PLATFORM_ID,',',OLD.PLATFORM_DES), '', getTableSubSystem('ats_fes_platform_info'));
END IF;
END;;
DELIMITER ;

drop table if exists bas_timetable_ctrl_param;
CREATE TABLE `bas_timetable_ctrl_param` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `DOWNLOAD_TIME` varchar(8) NOT NULL COMMENT '时间表下载时间,08:00',
  `DOWNLOAD_TIMEOUT` decimal(10,0) NOT NULL COMMENT '下载超时时间，单位秒',
  `READ_TIMEOUT` decimal(10,0) NOT NULL COMMENT '读取超时时间，单位秒',
  `CTRL_KEY_ID_TAG` varchar(64) NOT NULL COMMENT '控制测点名',
  `TIMETABLE_ID_TAG` varchar(64) NOT NULL COMMENT '当前PLC运行时间表ID的测点名',
  `TIMETABLE_VERSION_TAG` varchar(64) NOT NULL COMMENT '当前PLC运行时间表版本号的测点名',
  `DCF_KEY_ID_TAG` varchar(64) NOT NULL COMMENT '时间表下载完成标志位测点名',
  `URF_KEY_ID_TAG` varchar(64) NOT NULL COMMENT '时间表回读请求标志位测点名',
  `RRF_KEY_ID_TAG` varchar(64) NOT NULL COMMENT '时间表回读标志位测点名',
  PRIMARY KEY (`LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='时间表控制参数配置';

drop trigger if exists TIA_BAS_TIMETABLE_CTRL_PARAM;
DELIMITER ;;
create trigger TIA_BAS_TIMETABLE_CTRL_PARAM AFTER INSERT on bas_timetable_ctrl_param for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'bas_timetable_ctrl_param', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('bas_timetable_ctrl_param'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_BAS_TIMETABLE_CTRL_PARAM;
DELIMITER ;;
create trigger TUA_BAS_TIMETABLE_CTRL_PARAM AFTER UPDATE on bas_timetable_ctrl_param for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'bas_timetable_ctrl_param', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('bas_timetable_ctrl_param'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_BAS_TIMETABLE_CTRL_PARAM;
DELIMITER ;;
create trigger TDA_BAS_TIMETABLE_CTRL_PARAM AFTER DELETE on bas_timetable_ctrl_param for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'bas_timetable_ctrl_param', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('bas_timetable_ctrl_param'));
END IF;
END;;
DELIMITER ;

drop table if exists bas_timetable_def;
CREATE TABLE `bas_timetable_def` (
  `ID` decimal(10,0) NOT NULL COMMENT '时间表ID',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `NAME` varchar(32) NOT NULL COMMENT '时间表名',
  `VERSION` decimal(10,0) NOT NULL COMMENT '当前版本号（组态版本号）',
  PRIMARY KEY (`ID`,`LOCATION_ID`),
  UNIQUE KEY `UN_BAS_TIMETABLE_DEF` (`LOCATION_ID`,`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='时间表定义';

drop trigger if exists TIA_BAS_TIMETABLE_DEF;
DELIMITER ;;
create trigger TIA_BAS_TIMETABLE_DEF AFTER INSERT on bas_timetable_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'bas_timetable_def', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('bas_timetable_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_BAS_TIMETABLE_DEF;
DELIMITER ;;
create trigger TUA_BAS_TIMETABLE_DEF AFTER UPDATE on bas_timetable_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'bas_timetable_def', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('bas_timetable_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_BAS_TIMETABLE_DEF;
DELIMITER ;;
create trigger TDA_BAS_TIMETABLE_DEF AFTER DELETE on bas_timetable_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'bas_timetable_def', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('bas_timetable_def'));
END IF;
END;;
DELIMITER ;

drop table if exists bas_timetable_info;
CREATE TABLE `bas_timetable_info` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `TIMETABLE_ID` decimal(10,0) NOT NULL COMMENT '时间表ID',
  `BEGIN_TIME` varchar(8) NOT NULL COMMENT '起始时间，格式：08:00',
  `CRAFT_ID` decimal(10,0) NOT NULL COMMENT '所属工艺',
  `MODE_ID` decimal(10,0) NOT NULL COMMENT '模式ID',
  PRIMARY KEY (`LOCATION_ID`,`TIMETABLE_ID`,`BEGIN_TIME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='时间表信息表';

drop trigger if exists TIA_BAS_TIMETABLE_INFO;
DELIMITER ;;
create trigger TIA_BAS_TIMETABLE_INFO AFTER INSERT on bas_timetable_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'bas_timetable_info', 'location_id,timetable_id,begin_time', '', concat(NEW.LOCATION_ID,',',NEW.TIMETABLE_ID,',',NEW.BEGIN_TIME), getTableSubSystem('bas_timetable_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_BAS_TIMETABLE_INFO;
DELIMITER ;;
create trigger TUA_BAS_TIMETABLE_INFO AFTER UPDATE on bas_timetable_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'bas_timetable_info', 'location_id,timetable_id,begin_time', concat(OLD.LOCATION_ID,',',OLD.TIMETABLE_ID,',',OLD.BEGIN_TIME), concat(NEW.LOCATION_ID,',',NEW.TIMETABLE_ID,',',NEW.BEGIN_TIME), getTableSubSystem('bas_timetable_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_BAS_TIMETABLE_INFO;
DELIMITER ;;
create trigger TDA_BAS_TIMETABLE_INFO AFTER DELETE on bas_timetable_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'bas_timetable_info', 'location_id,timetable_id,begin_time', concat(OLD.LOCATION_ID,',',OLD.TIMETABLE_ID,',',OLD.BEGIN_TIME), '', getTableSubSystem('bas_timetable_info'));
END IF;
END;;
DELIMITER ;

drop table if exists bas_timetable_schedule;
CREATE TABLE `bas_timetable_schedule` (
  `ID` decimal(10,0) NOT NULL COMMENT '计划时间表ID',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `NAME` varchar(32) NOT NULL COMMENT '计划时间表名',
  `BEGIN_DATE` varchar(8) NOT NULL COMMENT '开始时间，格式如：06-01',
  `END_DATE` varchar(8) NOT NULL COMMENT '结束时间，格式如：06-01',
  `DATE_TYPE` decimal(10,0) DEFAULT NULL COMMENT '日期类型，1-特殊日/2-普通日',
  `MON_TIMETABLE_ID` decimal(10,0) DEFAULT NULL COMMENT '星期一时间表ID',
  `TUES_TIMETABLE_ID` decimal(10,0) DEFAULT NULL COMMENT '星期二时间表ID',
  `WED_TIMETABLE_ID` decimal(10,0) DEFAULT NULL COMMENT '星期三时间表ID',
  `THUR_TIMETABLE_ID` decimal(10,0) DEFAULT NULL COMMENT '星期四时间表ID',
  `FRI_TIMETABLE_ID` decimal(10,0) DEFAULT NULL COMMENT '星期五时间表ID',
  `SAT_TIMETABLE_ID` decimal(10,0) DEFAULT NULL COMMENT '星期六时间表ID',
  `SUN_TIMETABLE_ID` decimal(10,0) DEFAULT NULL COMMENT '星期日时间表ID',
  PRIMARY KEY (`ID`,`LOCATION_ID`),
  UNIQUE KEY `UN_BAS_TIMETABLE_SCHEDULE` (`LOCATION_ID`,`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='时间表计划';

drop trigger if exists TIA_BAS_TIMETABLE_SCHEDULE;
DELIMITER ;;
create trigger TIA_BAS_TIMETABLE_SCHEDULE AFTER INSERT on bas_timetable_schedule for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'bas_timetable_schedule', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('bas_timetable_schedule'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_BAS_TIMETABLE_SCHEDULE;
DELIMITER ;;
create trigger TUA_BAS_TIMETABLE_SCHEDULE AFTER UPDATE on bas_timetable_schedule for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'bas_timetable_schedule', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('bas_timetable_schedule'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_BAS_TIMETABLE_SCHEDULE;
DELIMITER ;;
create trigger TDA_BAS_TIMETABLE_SCHEDULE AFTER DELETE on bas_timetable_schedule for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'bas_timetable_schedule', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('bas_timetable_schedule'));
END IF;
END;;
DELIMITER ;

drop table if exists calc_func_def;
CREATE TABLE `calc_func_def` (
  `FUNC_NAME` varchar(64) NOT NULL COMMENT '函数名',
  `GROUP_ID` decimal(10,0) NOT NULL COMMENT '分类ID',
  `DESCRIPTION` varchar(256) DEFAULT NULL COMMENT '函数描述',
  `FUNC_STR` varchar(4096) DEFAULT NULL COMMENT 'LUA函数字符串',
  `PARAM_NUM` decimal(10,0) NOT NULL DEFAULT '0' COMMENT '参数个数',
  PRIMARY KEY (`FUNC_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计算量函数表';

drop trigger if exists TI_CALC_FUNC_DEF;
DELIMITER ;;
create trigger TI_CALC_FUNC_DEF AFTER INSERT on calc_func_def for each ROW 
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_func_def', 'func_name', '', NEW.FUNC_NAME, getTableSubSystem('calc_func_def'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TU_CALC_FUNC_DEF;
DELIMITER ;;
create trigger TU_CALC_FUNC_DEF AFTER UPDATE on calc_func_def for each ROW 
BEGIN
   
    UPDATE calc_out_para SET
    FUNC_NAME = NEW.FUNC_NAME,
    CALC_FLAG = '1'
    WHERE FUNC_NAME = OLD.FUNC_NAME;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,   KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_func_def', 'func_name',     OLD.FUNC_NAME,  NEW.FUNC_NAME, getTableSubSystem('calc_func_def'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TD_CALC_FUNC_DEF;
DELIMITER ;;
create trigger TD_CALC_FUNC_DEF AFTER DELETE on calc_func_def for each ROW 
BEGIN
    DELETE FROM calc_out_para WHERE FUNC_NAME = OLD.FUNC_NAME;
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_func_def', 'func_name', OLD.FUNC_NAME, '', getTableSubSystem('calc_func_def'));
    END IF;
END;;
DELIMITER ;

drop table if exists calc_group_def;
CREATE TABLE `calc_group_def` (
  `GROUP_ID` decimal(10,0) NOT NULL,
  `GROUP_NAME` varchar(64) NOT NULL COMMENT '分类名',
  `DESCRIPTION` varchar(256) DEFAULT NULL COMMENT '分类描述',
  PRIMARY KEY (`GROUP_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='公式分类表';

drop trigger if exists TI_CALC_GROUP_DEF;
DELIMITER ;;
create trigger TI_CALC_GROUP_DEF AFTER INSERT on calc_group_def for each ROW 
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_group_def', 'group_id', '', NEW.GROUP_ID, getTableSubSystem('calc_group_def'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TU_CALC_GROUP_DEF;
DELIMITER ;;
create trigger TU_CALC_GROUP_DEF AFTER UPDATE on calc_group_def for each ROW 
BEGIN
   
    IF NEW.GROUP_ID <> OLD.GROUP_ID THEN 
    UPDATE calc_func_def SET GROUP_ID = NEW.GROUP_ID WHERE GROUP_ID = OLD.GROUP_ID;
    END IF;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,   KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_group_def', 'group_id',     OLD.GROUP_ID,  NEW.GROUP_ID, getTableSubSystem('calc_group_def'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TD_CALC_GROUP_DEF;
DELIMITER ;;
create trigger TD_CALC_GROUP_DEF AFTER DELETE on calc_group_def for each ROW 
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_group_def', 'group_id', OLD.GROUP_ID, '', getTableSubSystem('calc_group_def'));
    END IF;
END;;
DELIMITER ;

drop table if exists calc_in_para;
CREATE TABLE `calc_in_para` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '输出测点名',
  `PARA_SEQ_NO` decimal(10,0) NOT NULL COMMENT '参数序号',
  `PARA_KEY_TAG` varchar(64) NOT NULL COMMENT '输入测点标签',
  `DESCRIPTION` varchar(256) DEFAULT NULL,
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业ID',
  `CALC_FLAG` decimal(10,0) DEFAULT NULL COMMENT '标识符',
  PRIMARY KEY (`KEY_ID_TAG`,`PARA_SEQ_NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计算量输入配置表';

drop trigger if exists TI_CALC_IN_PARA;
DELIMITER ;;
create trigger TI_CALC_IN_PARA AFTER INSERT on calc_in_para for each ROW 
BEGIN
   
    UPDATE calc_out_para SET CALC_FLAG = '1' WHERE KEY_ID_TAG = NEW.KEY_ID_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_in_para', 'key_id_tag,para_seq_no', '', concat(NEW.KEY_ID_TAG,',',NEW.PARA_SEQ_NO), getTableSubSystem('calc_in_para'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TU_CALC_IN_PARA;
DELIMITER ;;
create trigger TU_CALC_IN_PARA AFTER UPDATE on calc_in_para for each ROW 
BEGIN
   
    UPDATE calc_out_para SET CALC_FLAG = '1' WHERE KEY_ID_TAG = NEW.KEY_ID_TAG;
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_in_para', 'key_id_tag,para_seq_no', concat(OLD.KEY_ID_TAG,',',OLD.PARA_SEQ_NO), concat(NEW.KEY_ID_TAG,',',NEW.PARA_SEQ_NO), getTableSubSystem('calc_in_para'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TD_CALC_IN_PARA;
DELIMITER ;;
create trigger TD_CALC_IN_PARA AFTER DELETE on calc_in_para for each ROW 
BEGIN
    
    UPDATE calc_out_para SET CALC_FLAG = '1' WHERE KEY_ID_TAG = OLD.KEY_ID_TAG;
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_in_para', 'key_id_tag,para_seq_no', concat(OLD.KEY_ID_TAG,',',OLD.PARA_SEQ_NO), '', getTableSubSystem('calc_in_para'));
    END IF;
END;;
DELIMITER ;

drop table if exists calc_out_para;
CREATE TABLE `calc_out_para` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '输出测点名',
  `DESCRIPTION` varchar(256) DEFAULT NULL COMMENT '描述',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业ID',
  `FUNC_NAME` varchar(64) NOT NULL COMMENT '计算量函数名',
  `CALC_FLAG` decimal(10,0) DEFAULT NULL COMMENT '标识符',
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计算量输出配置表';

drop trigger if exists TI_CALC_OUT_PARA;
DELIMITER ;;
create trigger TI_CALC_OUT_PARA AFTER INSERT on calc_out_para for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_out_para', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('calc_out_para'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TU_CALC_OUT_PARA;
DELIMITER ;;
create trigger TU_CALC_OUT_PARA AFTER UPDATE on calc_out_para for each ROW 
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_out_para', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('calc_out_para'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TD_CALC_OUT_PARA;
DELIMITER ;;
create trigger TD_CALC_OUT_PARA AFTER DELETE on calc_out_para for each ROW 
BEGIN
   
    #这里不能触发删除本输出的输入,因为输入的删除触发器中调用了本表
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_out_para', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('calc_out_para'));
    END IF;
END;;
DELIMITER ;

drop table if exists calc_para_def;
CREATE TABLE `calc_para_def` (
  `FUNC_NAME` varchar(64) NOT NULL COMMENT '函数名',
  `PARA_SEQ_NO` decimal(10,0) NOT NULL COMMENT '参数序号',
  `DESCRIPTION` varchar(256) DEFAULT NULL COMMENT '参数描述',
  PRIMARY KEY (`FUNC_NAME`,`PARA_SEQ_NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计算量参数描述表';

drop trigger if exists TI_CALC_PARA_DEF;
DELIMITER ;;
create trigger TI_CALC_PARA_DEF AFTER INSERT on calc_para_def for each ROW 
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'calc_para_def', 'func_name,para_seq_no', '', concat(NEW.FUNC_NAME,',',NEW.PARA_SEQ_NO), getTableSubSystem('calc_para_def'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TU_CALC_PARA_DEF;
DELIMITER ;;
create trigger TU_CALC_PARA_DEF AFTER UPDATE on calc_para_def for each ROW 
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'calc_para_def', 'func_name,para_seq_no', concat(OLD.FUNC_NAME,',',OLD.PARA_SEQ_NO), concat(NEW.FUNC_NAME,',',NEW.PARA_SEQ_NO), getTableSubSystem('calc_para_def'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TD_CALC_PARA_DEF;
DELIMITER ;;
create trigger TD_CALC_PARA_DEF AFTER DELETE on calc_para_def for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'calc_para_def', 'func_name,para_seq_no', concat(OLD.FUNC_NAME,',',OLD.PARA_SEQ_NO), '', getTableSubSystem('calc_para_def'));
    END IF;
END;;
DELIMITER ;

drop table if exists cctv_camera_info;
CREATE TABLE `cctv_camera_info` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签',
  `CAMERA_NAME` varchar(64) NOT NULL COMMENT '摄像头名',
  `CAMERA_TYPE` decimal(10,0) NOT NULL COMMENT '摄像头类型',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '位置id',
  `CAMERA_IP` varchar(32) NOT NULL COMMENT 'ip地址',
  `CAMERA_PTZ_IP` varchar(32) NOT NULL COMMENT '云台ip',
  `CAMERA_PORT` decimal(10,0) NOT NULL COMMENT '端口',
  `CAMERA_NO` decimal(10,0) NOT NULL COMMENT '摄像头序号',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `CHAN_NO` decimal(10,0) DEFAULT NULL COMMENT '通道号',
  `USERNAME` varchar(128) DEFAULT NULL COMMENT '用户名',
  `PASSWORD` varchar(128) DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='视频设备信息表';

drop table if exists cctv_monitor_info;
CREATE TABLE `cctv_monitor_info` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT 'TAG_NAME',
  `DESCRIPTION` varchar(100) NOT NULL COMMENT 'DESCRIPTION',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT 'LOCATION_ID',
  `VIDEO_SOURCE_TYPE` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_SOURCE_TYPE',
  `DIV_MODE` decimal(10,0) DEFAULT NULL COMMENT 'DIV_MODE',
  `SEPARATOR_KEY_ID_TAG4` varchar(100) DEFAULT NULL COMMENT 'SEPARATOR_KEY_ID_TAG4',
  `SEPARATOR_KEY_ID_TAG9` varchar(100) DEFAULT NULL COMMENT 'SEPARATOR_KEY_ID_TAG9',
  `SEPARATOR_KEY_ID_TAG16` varchar(100) DEFAULT NULL COMMENT 'SEPARATOR_KEY_ID_TAG16',
  `VIDEO_SOURCE_KEY_ID_TAG` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG',
  `MONITOR_NO` decimal(10,0) DEFAULT NULL COMMENT 'MONITOR_NO',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

drop table if exists cctv_orientation;
CREATE TABLE `cctv_orientation` (
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT 'LOCATION_ID',
  `DESCRIPTION` varchar(100) DEFAULT NULL COMMENT 'DESCRIPTION',
  `ORIENTATION_NO` decimal(10,0) DEFAULT NULL COMMENT 'ORIENTATION_NO',
  `TAG_NAME` varchar(64) NOT NULL COMMENT 'TAG_NAME',
  `CAMERA_TAG_NAME` varchar(64) NOT NULL COMMENT 'CAMERA_TAG_NAME',
  `PRESET_TOKEN` varchar(128) DEFAULT NULL COMMENT 'onvif与指点标识符',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

drop table if exists cctv_separator_info;
CREATE TABLE `cctv_separator_info` (
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT 'LOCATION_ID',
  `DESCRIPTION` varchar(100) DEFAULT NULL COMMENT 'DESCRIPTION',
  `SEPARATOR_TYPE` decimal(10,0) NOT NULL COMMENT 'SEPARATOR_TYPE',
  `IF_FULL_SEQUENCE` decimal(10,0) DEFAULT NULL COMMENT 'IF_FULL_SEQUENCE',
  `SEQUENCE_ORDER` decimal(10,0) DEFAULT NULL COMMENT 'SEQUENCE_ORDER',
  `SEPARATOR_NO` decimal(10,0) DEFAULT NULL COMMENT 'SEPARATOR_NO',
  `TAG_NAME` varchar(64) NOT NULL COMMENT 'TAG_NAME',
  `VIDEO_TYPE1` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE1',
  `VIDEO_TYPE2` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE2',
  `VIDEO_SOURCE_KEY_ID_TAG1` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG1',
  `VIDEO_SOURCE_KEY_ID_TAG2` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG2',
  `VIDEO_SOURCE_KEY_ID_TAG3` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG3',
  `VIDEO_TYPE3` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE3',
  `VIDEO_TYPE4` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE4',
  `VIDEO_TYPE5` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE5',
  `VIDEO_TYPE6` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE6',
  `VIDEO_TYPE7` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE7',
  `VIDEO_TYPE8` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE8',
  `VIDEO_TYPE9` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE9',
  `VIDEO_TYPE10` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE10',
  `VIDEO_TYPE12` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE12',
  `VIDEO_TYPE11` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE11',
  `VIDEO_TYPE13` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE13',
  `VIDEO_TYPE14` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE14',
  `VIDEO_TYPE15` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE15',
  `VIDEO_TYPE16` decimal(10,0) DEFAULT NULL COMMENT 'VIDEO_TYPE16',
  `VIDEO_SOURCE_KEY_ID_TAG4` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG4',
  `VIDEO_SOURCE_KEY_ID_TAG5` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG5',
  `VIDEO_SOURCE_KEY_ID_TAG6` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG6',
  `VIDEO_SOURCE_KEY_ID_TAG7` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG7',
  `VIDEO_SOURCE_KEY_ID_TAG8` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG8',
  `VIDEO_SOURCE_KEY_ID_TAG9` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG9',
  `VIDEO_SOURCE_KEY_ID_TAG10` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG10',
  `VIDEO_SOURCE_KEY_ID_TAG11` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG11',
  `VIDEO_SOURCE_KEY_ID_TAG16` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG16',
  `VIDEO_SOURCE_KEY_ID_TAG15` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG15',
  `VIDEO_SOURCE_KEY_ID_TAG14` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG14',
  `VIDEO_SOURCE_KEY_ID_TAG13` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG13',
  `VIDEO_SOURCE_KEY_ID_TAG12` varchar(100) DEFAULT NULL COMMENT 'VIDEO_SOURCE_KEY_ID_TAG12',
  `FULL_SEQUENCE_KEY_ID_TAG` varchar(100) DEFAULT NULL COMMENT 'FULL_SEQUENCE_KEY_ID_TAG',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='CCTV画面分割器表';

drop table if exists cctv_sequence;
CREATE TABLE `cctv_sequence` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT 'TAG_NAME',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT 'LOCATION_ID',
  `SEQUENCE_NO` decimal(10,0) NOT NULL COMMENT 'SEQUENCE_NO',
  `DESCRIPTION` varchar(100) DEFAULT NULL COMMENT 'DESCRIPTION',
  `SEQUENCE_TYPE` decimal(10,0) NOT NULL COMMENT 'SEQUENCE_TYPE',
  `CAMERA_NUM` decimal(10,0) NOT NULL COMMENT 'CAMERA_NUM',
  `CAMERA_SHOWTIME` decimal(10,0) NOT NULL COMMENT 'CAMERA_SHOWTIME',
  `OPERATOR_ID` decimal(10,0) DEFAULT NULL COMMENT 'OPERATOR_ID',
  `OPERATOR_NODE_NAME` varchar(64) DEFAULT NULL COMMENT 'OPERATOR_NODE_NAME',
  `OPERATE_TIME` decimal(10,0) DEFAULT NULL COMMENT 'OPERATE_TIME',
  `REQUEST_ID` decimal(10,0) DEFAULT NULL COMMENT 'REQUEST_ID',
  `CAMERA_TAG1` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG1',
  `CAMERA_TAG2` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG2',
  `CAMERA_TAG3` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG3',
  `CAMERA_TAG4` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG4',
  `CAMERA_TAG5` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG5',
  `CAMERA_TAG6` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG6',
  `CAMERA_TAG7` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG7',
  `CAMERA_TAG8` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG8',
  `CAMERA_TAG9` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG9',
  `CAMERA_TAG10` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG10',
  `CAMERA_TAG11` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG11',
  `CAMERA_TAG12` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG12',
  `CAMERA_TAG13` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG13',
  `CAMERA_TAG14` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG14',
  `CAMERA_TAG15` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG15',
  `CAMERA_TAG16` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG16',
  `CAMERA_TAG17` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG17',
  `CAMERA_TAG18` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG18',
  `CAMERA_TAG19` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG19',
  `CAMERA_TAG20` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG20',
  `CAMERA_TAG21` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG21',
  `CAMERA_TAG22` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG22',
  `CAMERA_TAG23` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG23',
  `CAMERA_TAG24` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG24',
  `CAMERA_TAG25` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG25',
  `CAMERA_TAG26` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG26',
  `CAMERA_TAG27` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG27',
  `CAMERA_TAG28` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG28',
  `CAMERA_TAG29` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG29',
  `CAMERA_TAG30` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG30',
  `CAMERA_TAG31` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG31',
  `CAMERA_TAG32` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG32',
  `CAMERA_TAG33` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG33',
  `CAMERA_TAG34` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG34',
  `CAMERA_TAG35` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG35',
  `CAMERA_TAG36` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG36',
  `CAMERA_TAG37` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG37',
  `CAMERA_TAG38` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG38',
  `CAMERA_TAG39` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG39',
  `CAMERA_TAG40` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG40',
  `CAMERA_TAG41` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG41',
  `CAMERA_TAG42` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG42',
  `CAMERA_TAG43` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG43',
  `CAMERA_TAG44` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG44',
  `CAMERA_TAG45` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG45',
  `CAMERA_TAG46` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG46',
  `CAMERA_TAG47` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG47',
  `CAMERA_TAG48` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG48',
  `CAMERA_TAG49` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG49',
  `CAMERA_TAG50` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG50',
  `CAMERA_TAG51` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG51',
  `CAMERA_TAG52` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG52',
  `CAMERA_TAG53` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG53',
  `CAMERA_TAG54` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG54',
  `CAMERA_TAG55` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG55',
  `CAMERA_TAG56` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG56',
  `CAMERA_TAG57` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG57',
  `CAMERA_TAG58` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG58',
  `CAMERA_TAG59` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG59',
  `CAMERA_TAG60` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG60',
  `CAMERA_TAG61` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG61',
  `CAMERA_TAG62` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG62',
  `CAMERA_TAG63` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG63',
  `CAMERA_TAG64` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG64',
  `CAMERA_TAG65` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG65',
  `CAMERA_TAG66` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG66',
  `CAMERA_TAG67` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG67',
  `CAMERA_TAG68` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG68',
  `CAMERA_TAG69` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG69',
  `CAMERA_TAG70` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG70',
  `CAMERA_TAG71` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG71',
  `CAMERA_TAG72` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG72',
  `CAMERA_TAG73` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG73',
  `CAMERA_TAG74` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG74',
  `CAMERA_TAG75` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG75',
  `CAMERA_TAG76` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG76',
  `CAMERA_TAG77` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG77',
  `CAMERA_TAG78` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG78',
  `CAMERA_TAG79` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG79',
  `CAMERA_TAG80` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG80',
  `CAMERA_TAG81` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG81',
  `CAMERA_TAG82` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG82',
  `CAMERA_TAG83` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG83',
  `CAMERA_TAG84` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG84',
  `CAMERA_TAG85` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG85',
  `CAMERA_TAG86` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG86',
  `CAMERA_TAG87` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG87',
  `CAMERA_TAG88` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG88',
  `CAMERA_TAG89` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG89',
  `CAMERA_TAG90` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG90',
  `CAMERA_TAG91` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG91',
  `CAMERA_TAG92` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG92',
  `CAMERA_TAG93` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG93',
  `CAMERA_TAG94` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG94',
  `CAMERA_TAG95` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG95',
  `CAMERA_TAG96` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG96',
  `CAMERA_TAG97` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG97',
  `CAMERA_TAG98` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG98',
  `CAMERA_TAG99` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG99',
  `CAMERA_TAG100` varchar(64) DEFAULT NULL COMMENT 'CAMERA_TAG100',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='视频监控队列表';

drop table if exists cfg_dev_type_map;
CREATE TABLE `cfg_dev_type_map` (
  `IED_NAME` varchar(100) NOT NULL COMMENT '文件iedName',
  `RECORDWAVE_ENABLE` decimal(10,0) NOT NULL COMMENT '是否启用录波',
  `RECORDWAVE_PATH` varchar(256) NOT NULL COMMENT '录波路径',
  `RECORDWAVE_TIME` decimal(10,0) NOT NULL COMMENT '录波召唤时间',
  PRIMARY KEY (`IED_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='61850设备类型映射表';

drop table if exists cfg_device_inst;
CREATE TABLE `cfg_device_inst` (
  `id` decimal(10,0) NOT NULL COMMENT '主键',
  `Stationid` decimal(10,0) DEFAULT NULL COMMENT '厂站ID',
  `iedname` varchar(128) DEFAULT NULL COMMENT 'iedName',
  `IedDesc` varchar(128) DEFAULT NULL COMMENT 'ied描述',
  `DeviceTagName` varchar(128) DEFAULT NULL COMMENT '实例装置的TagName',
  `waveremotepath` varchar(128) DEFAULT NULL COMMENT '录波路径',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='二次装置实例表';

drop trigger if exists TIA_CFG_DEVICE_INST;
DELIMITER ;;
create trigger TIA_CFG_DEVICE_INST AFTER INSERT on cfg_device_inst for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'cfg_device_inst', 'id', '', NEW.id, getTableSubSystem('cfg_device_inst'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_CFG_DEVICE_INST;
DELIMITER ;;
create trigger TUA_CFG_DEVICE_INST AFTER UPDATE on cfg_device_inst for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'cfg_device_inst', 'id', OLD.id, NEW.id, getTableSubSystem('cfg_device_inst'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_CFG_DEVICE_INST;
DELIMITER ;;
create trigger TDA_CFG_DEVICE_INST AFTER DELETE on cfg_device_inst for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'cfg_device_inst', 'id', OLD.id, '', getTableSubSystem('cfg_device_inst'));
END IF;
END;;
DELIMITER ;

drop table if exists cfg_signal_inst;
CREATE TABLE `cfg_signal_inst` (
  `id` decimal(10,0) NOT NULL COMMENT '主键',
  `iedname` varchar(128) DEFAULT NULL COMMENT 'iedname',
  `pointtype` decimal(10,0) DEFAULT NULL COMMENT '测点类型',
  `pointid` decimal(10,0) DEFAULT NULL COMMENT 'fes测点ID',
  `pointref` varchar(128) DEFAULT NULL COMMENT '测点的mms索引',
  `Describtion` varchar(128) DEFAULT NULL COMMENT '测点描述',
  `Typeid` decimal(10,0) DEFAULT NULL COMMENT '测点的Typeid',
  `basictype` decimal(10,0) DEFAULT NULL COMMENT '基本类型',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='实例信号测点';

drop trigger if exists TIA_CFG_SIGNAL_INST;
DELIMITER ;;
create trigger TIA_CFG_SIGNAL_INST AFTER INSERT on cfg_signal_inst for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'cfg_signal_inst', 'id', '', NEW.id, getTableSubSystem('cfg_signal_inst'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_CFG_SIGNAL_INST;
DELIMITER ;;
create trigger TUA_CFG_SIGNAL_INST AFTER UPDATE on cfg_signal_inst for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'cfg_signal_inst', 'id', OLD.id, NEW.id, getTableSubSystem('cfg_signal_inst'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_CFG_SIGNAL_INST;
DELIMITER ;;
create trigger TDA_CFG_SIGNAL_INST AFTER DELETE on cfg_signal_inst for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'cfg_signal_inst', 'id', OLD.id, '', getTableSubSystem('cfg_signal_inst'));
END IF;
END;;
DELIMITER ;

drop table if exists dev_temp_def;
CREATE TABLE `dev_temp_def` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '设备名称',
  `DEV_TYPE_ID` decimal(10,0) NOT NULL COMMENT '设备类型',
  `DEV_MODEL` varchar(64) DEFAULT NULL COMMENT '设备型号',
  `DESCRIPTION` varchar(64) DEFAULT NULL COMMENT '设备描述',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '专业子系统',
  `PARA_STR1` varchar(64) DEFAULT NULL COMMENT '备用字段1',
  `PARA_STR2` varchar(64) DEFAULT NULL COMMENT '备用字段2',
  `DEV_TEMP_GRP` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备模板定义表';

drop trigger if exists TIA_DEV_TEMP_DEF;
DELIMITER ;;
create trigger TIA_DEV_TEMP_DEF AFTER INSERT on dev_temp_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'dev_temp_def', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('dev_temp_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_DEV_TEMP_DEF;
DELIMITER ;;
create trigger TU_DEV_TEMP_DEF AFTER UPDATE on dev_temp_def for each ROW 
BEGIN
IF OLD.TAG_NAME != NEW.TAG_NAME THEN
BEGIN
    UPDATE accuml set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
    UPDATE analog set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
    UPDATE digital set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
    UPDATE mix set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
END;
END IF;
    
IF OLD.TAG_NAME != NEW.TAG_NAME OR OLD.DESCRIPTION != NEW.DESCRIPTION  THEN 
BEGIN 
    UPDATE accuml_temp_define SET
    DEV_TP_NAME=NEW.TAG_NAME ,
    TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)) 
    WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
    
    UPDATE analog_temp_define SET
    DEV_TP_NAME=NEW.TAG_NAME ,
    TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)) 
    WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
    
    
    UPDATE digital_temp_define SET
    DEV_TP_NAME=NEW.TAG_NAME ,
    TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)) 
    WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
    
    UPDATE mix_temp_define SET
    DEV_TP_NAME=NEW.TAG_NAME ,
    TAG_NAME=concat(NEW.TAG_NAME, '.' , getAfterLastDot(TAG_NAME)) 
    WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
    #更新对应的套件
    IF OLD.TAG_NAME != NEW.TAG_NAME THEN 
        BEGIN
            UPDATE temp_plugin_dev_map SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_fes_dev_map SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_point_map_acc SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_point_map_ana SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_point_map_dig SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
            UPDATE temp_plugin_point_map_mix SET TEMP_NAME = NEW.TAG_NAME WHERE TEMP_NAME = OLD.TAG_NAME;
        END;
    END IF;
END;
END IF;
/*
IF OLD.TAG_NAME != NEW.TAG_NAME OR OLD.DEV_TYPE_ID != NEW.DEV_TYPE_ID OR OLD.SUB_SYSTEM != NEW.SUB_SYSTEM
THEN 
UPDATE dev_info SET 
DEV_TP_NAME=NEW.TAG_NAME, 
DEV_TYPE=NEW.DEV_TYPE_ID,
SUB_SYSTEM = NEW.SUB_SYSTEM
WHERE DEV_TP_NAME=OLD.TAG_NAME  ;
END IF;
*/
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'dev_temp_def', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('dev_temp_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_DEV_TEMP_DEF;
DELIMITER ;;
create trigger TD_DEV_TEMP_DEF BEFORE DELETE on dev_temp_def for each ROW 
BEGIN
    
/*   
DELETE FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME;
*/
DELETE FROM accuml_temp_define WHERE DEV_TP_NAME = OLD.TAG_NAME;
DELETE FROM analog_temp_define WHERE DEV_TP_NAME = OLD.TAG_NAME;
DELETE FROM digital_temp_define WHERE DEV_TP_NAME = OLD.TAG_NAME;
DELETE FROM mix_temp_define WHERE DEV_TP_NAME = OLD.TAG_NAME;
#套件的处理
DELETE FROM temp_plugin_dev_map WHERE TEMP_NAME = OLD.TAG_NAME;
UPDATE accuml set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
UPDATE analog set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
UPDATE digital set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
UPDATE mix set POINT_TP_NAME = '' WHERE DEVICE IN (SELECT dev_info.TAG_NAME FROM dev_info WHERE DEV_TP_NAME = OLD.TAG_NAME);
END;;
DELIMITER ;

drop trigger if exists TDA_DEV_TEMP_DEF;
DELIMITER ;;
create trigger TDA_DEV_TEMP_DEF AFTER DELETE on dev_temp_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'dev_temp_def', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('dev_temp_def'));
END IF;
END;;
DELIMITER ;

drop table if exists dev_temp_group;
CREATE TABLE `dev_temp_group` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备模板组定义';

drop trigger if exists TU_DEV_TEMP_GROUP;
DELIMITER ;;
create trigger TU_DEV_TEMP_GROUP AFTER UPDATE on dev_temp_group for each ROW 
BEGIN
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN 
    UPDATE dev_temp_def SET DEV_TEMP_GRP = NEW.TAG_NAME WHERE DEV_TEMP_GRP = OLD.TAG_NAME;
    END IF;
END;;
DELIMITER ;

drop trigger if exists TD_DEV_TEMP_GROUP;
DELIMITER ;;
create trigger TD_DEV_TEMP_GROUP AFTER DELETE on dev_temp_group for each ROW 
BEGIN
    
    DELETE FROM dev_temp_def WHERE DEV_TEMP_GRP = OLD.TAG_NAME;
    
END;;
DELIMITER ;

drop table if exists dev_type_def_61850;
CREATE TABLE `dev_type_def_61850` (
  `DEV_TYPE_ID` decimal(10,0) NOT NULL COMMENT '设备分类',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '分类名称',
  `DESCRIPTION` varchar(64) NOT NULL COMMENT '分类描述',
  `RECORDWAVE_ENABLE` decimal(10,0) DEFAULT NULL COMMENT '是否启用录波',
  `RECORDWAVE_PATH` varchar(64) DEFAULT NULL COMMENT '录波路径',
  `RECORDWAVE_TIME` decimal(10,0) DEFAULT NULL COMMENT '录波召唤时间',
  PRIMARY KEY (`DEV_TYPE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='61850设备类型定义表';

drop table if exists digital_control;
CREATE TABLE `digital_control` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `CTRL_ACT_NAME` varchar(64) DEFAULT NULL COMMENT '控制动作组名称',
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL COMMENT '直控,选择再执行，自动控制',
  `IS_TAGT_STATE` decimal(10,0) DEFAULT NULL COMMENT '是否等待目标状态',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `RESV_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制保留时间',
  `PULSE_DURATION` decimal(10,0) DEFAULT NULL COMMENT '脉冲保留时间',
  `RTU_TAG` varchar(64) DEFAULT NULL,
  `OFFSET_NUM` decimal(10,0) NOT NULL COMMENT 'DO点个数',
  `OFFSET_NO1` varchar(48) DEFAULT NULL COMMENT 'DO点号1',
  `OFFSET_NO2` varchar(48) DEFAULT NULL COMMENT 'DO点号2',
  `OFFSET_NO3` varchar(48) DEFAULT NULL COMMENT 'DO点号3',
  `OFFSET_NO4` varchar(48) DEFAULT NULL COMMENT 'DO点号4',
  `OFFSET_NO5` varchar(48) DEFAULT NULL COMMENT 'DO点号5',
  `CTRL_ALIAS` varchar(128) DEFAULT NULL COMMENT '遥控别名',
  `IS_CHECK_PREVENTION` decimal(10,0) DEFAULT NULL COMMENT '是否五仿校验',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_DIGITAL_CONTROL` (`RTU_TAG`,`OFFSET_NO1`,`OFFSET_NO2`,`OFFSET_NO3`,`OFFSET_NO4`,`OFFSET_NO5`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='数字量输出参数表';

drop trigger if exists TIA_DIGITAL_CONTROL;
DELIMITER ;;
create trigger TIA_DIGITAL_CONTROL AFTER INSERT on digital_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'digital_control', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('digital_control'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_DIGITAL_CONTROL;
DELIMITER ;;
create trigger TU_DIGITAL_CONTROL BEFORE UPDATE on digital_control for each ROW 
body:BEGIN
    
    #修改功能名称
    IF COALESCE(NEW.RTU_TAG,'') = '' THEN
    BEGIN
        SET NEW.OFFSET_NO1 = '-1';
        SET NEW.OFFSET_NO2 = '-1';
        SET NEW.OFFSET_NO3 = '-1';
        SET NEW.OFFSET_NO4 = '-1';
        SET NEW.OFFSET_NO5 = '-1';
    END;
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_DIGITAL_CONTROL;
DELIMITER ;;
create trigger TUA_DIGITAL_CONTROL AFTER UPDATE on digital_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'digital_control', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('digital_control'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_DIGITAL_CONTROL;
DELIMITER ;;
create trigger TDA_DIGITAL_CONTROL AFTER DELETE on digital_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'digital_control', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('digital_control'));
END IF;
END;;
DELIMITER ;

drop table if exists digital_map_to_fes;
CREATE TABLE `digital_map_to_fes` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '标签名（表名.测点标签）',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签',
  `VALUE_NUM` decimal(10,0) DEFAULT NULL COMMENT '分量数',
  `DOT_NO1` varchar(48) DEFAULT NULL COMMENT '前置点号-分量1',
  `DOT_NO2` varchar(48) DEFAULT NULL COMMENT '前置点号-分量1',
  `DOT_NO3` varchar(48) DEFAULT NULL COMMENT '前置点号-分量3',
  `DOT_NO4` varchar(48) DEFAULT NULL COMMENT '前置点号-分量4',
  `DOT_NO5` varchar(48) DEFAULT NULL COMMENT '前置点号-分量5',
  `IS_FILTER_ERR` decimal(10,0) DEFAULT NULL COMMENT '是否过滤错误DI',
  `IS_FILTER_DISTURB` decimal(10,0) DEFAULT NULL COMMENT '是否过滤抖动',
  `DISTURB_TIME` decimal(10,0) DEFAULT NULL COMMENT '抖动时间限',
  `POLARITY` decimal(10,0) DEFAULT NULL COMMENT '极性',
  `DESCRIPTION` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='数字量前置映射表';

drop trigger if exists TI_DIGITAL_MAP_TO_FES;
DELIMITER ;;
create trigger TI_DIGITAL_MAP_TO_FES AFTER INSERT on digital_map_to_fes for each ROW 
BEGIN    
    SET @disableFesDigitalTrigger = 1;
    
    IF COALESCE(@disableDigitalMapToFesTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(NEW.DOT_NO1,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value1' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO1);
            END IF;
            
            IF COALESCE(NEW.DOT_NO2,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value2' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO2);
            END IF;
            
            IF COALESCE(NEW.DOT_NO3,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value3' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO3);
            END IF;
            
            IF COALESCE(NEW.DOT_NO4,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value4' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO4);
            END IF;
            
            IF COALESCE(NEW.DOT_NO5,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value5' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO5);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    SET @disableFesDigitalTrigger = 0;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'digital_map_to_fes', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('digital_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_DIGITAL_MAP_TO_FES;
DELIMITER ;;
create trigger TU_DIGITAL_MAP_TO_FES BEFORE UPDATE on digital_map_to_fes for each ROW 
body_label: BEGIN
    
    IF NEW.RTU_TAG = OLD.RTU_TAG AND 
        NEW.DOT_NO1 = OLD.DOT_NO1 AND 
        NEW.DOT_NO2 = OLD.DOT_NO2 AND 
        NEW.DOT_NO3 = OLD.DOT_NO3 AND 
        NEW.DOT_NO4 = OLD.DOT_NO4 AND 
        NEW.DOT_NO5 = OLD.DOT_NO5 AND 
        NEW.KEY_ID_TAG = OLD.KEY_ID_TAG 
        THEN LEAVE body_label;
    END IF;
        
    SET @disableFesDigitalTrigger = 1;
    
    IF COALESCE(@disableDigitalMapToFesTrigger,0) = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(OLD.DOT_NO1,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO1);
            END IF;
            
            IF COALESCE(OLD.DOT_NO2,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO2);
            END IF;
            
            IF COALESCE(OLD.DOT_NO3,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO3);
            END IF;
            
            IF COALESCE(OLD.DOT_NO4,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO4);
            END IF;
            
            IF COALESCE(OLD.DOT_NO5,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO5);
            END IF;
            
        END;
        END IF;
        
        
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(NEW.DOT_NO1,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value1' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO1);
            END IF;
            
            IF COALESCE(NEW.DOT_NO2,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value2' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO2);
            END IF;
            
            IF COALESCE(NEW.DOT_NO3,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value3' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO3);
            END IF;
            
            IF COALESCE(NEW.DOT_NO4,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value4' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO4);
            END IF;
            
            IF COALESCE(NEW.DOT_NO5,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value5' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO5);
            END IF;
            
        END;
        ELSE 
        BEGIN
            SET NEW.DOT_NO1 = '-1';
            SET NEW.DOT_NO2 = '-1';
            SET NEW.DOT_NO3 = '-1';
            SET NEW.DOT_NO4 = '-1';
            SET NEW.DOT_NO5 = '-1';
        END; 
        END IF;
    END;
    END IF;
    SET @disableFesDigitalTrigger = 0;
END;;
DELIMITER ;

drop trigger if exists TUA_DIGITAL_MAP_TO_FES;
DELIMITER ;;
create trigger TUA_DIGITAL_MAP_TO_FES AFTER UPDATE on digital_map_to_fes for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'digital_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('digital_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_DIGITAL_MAP_TO_FES;
DELIMITER ;;
create trigger TD_DIGITAL_MAP_TO_FES BEFORE DELETE on digital_map_to_fes for each ROW 
BEGIN    
    SET @disableFesDigitalTrigger = 1;
    
    IF COALESCE(@disableDigitalMapToFesTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(OLD.DOT_NO1,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO1);
            END IF;
            
            IF COALESCE(OLD.DOT_NO2,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO2);
            END IF;
            
            IF COALESCE(OLD.DOT_NO3,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO3);
            END IF;
            
            IF COALESCE(OLD.DOT_NO4,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO4);
            END IF;
            
            IF COALESCE(OLD.DOT_NO5,'') <> '' THEN
                UPDATE fes_digital SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO5);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
    SET @disableFesDigitalTrigger = 0;
END;;
DELIMITER ;

drop trigger if exists TDA_DIGITAL_MAP_TO_FES;
DELIMITER ;;
create trigger TDA_DIGITAL_MAP_TO_FES AFTER DELETE on digital_map_to_fes for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'digital_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('digital_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop table if exists digital_temp_define;
CREATE TABLE `digital_temp_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `DEV_TP_NAME` varchar(64) NOT NULL COMMENT '设备模板名称',
  `SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '序号',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类型',
  `POINT_CLASS` decimal(10,0) DEFAULT NULL COMMENT '点类别',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点列表',
  `POINT_SORT` decimal(10,0) DEFAULT NULL COMMENT '测点种类 ',
  `STATE_TEXT_NAME` varchar(64) DEFAULT NULL COMMENT '数字量文本名',
  `VALUE_NUM` decimal(10,0) DEFAULT NULL COMMENT '分量数',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '变位告警优先级',
  `ALARM_PRIORITY_SOE` decimal(10,0) DEFAULT NULL COMMENT 'SOE告警优先级',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延时时间',
  `INPUT_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '多位延迟时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回自动确认',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认后删除动作',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  `IS_SUMMARY` decimal(10,0) DEFAULT NULL COMMENT '是否摘要',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否采样',
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL COMMENT '是否统计',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL,
  `FILTER_ERR` decimal(10,0) DEFAULT NULL COMMENT '是否过滤误DI',
  `FILTER_DISTURB` decimal(10,0) DEFAULT NULL COMMENT '是否过滤误抖动',
  `DISTURB_TIME` decimal(10,0) DEFAULT NULL COMMENT '抖动时限',
  `POLARITY` decimal(10,0) DEFAULT NULL COMMENT '极性',
  `IS_CONTROL` decimal(10,0) DEFAULT NULL COMMENT '是否控制',
  `CTRL_ACT_NAME` varchar(64) DEFAULT NULL COMMENT '控制动作组名',
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL COMMENT '是否遥控选择',
  `IS_TAGT_STATE` decimal(10,0) DEFAULT NULL COMMENT '是否等待目标状态',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `RESV_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制保留时间',
  `PULSE_DURATION` decimal(10,0) DEFAULT NULL COMMENT '脉冲保持时间',
  `OFFSET_NUM` decimal(10,0) DEFAULT NULL COMMENT 'DO点个数',
  `IS_ALARM_FREQ` decimal(10,0) DEFAULT NULL COMMENT '是否告警计次',
  `ALARM_FREQ` decimal(10,0) DEFAULT NULL COMMENT '设定告警计次',
  `ALARM_FREQ_DESC` varchar(64) DEFAULT NULL COMMENT '计次告警描述',
  `IS_ALARM_TIME` decimal(10,0) DEFAULT NULL COMMENT '是否告警计时',
  `ALARM_TIME` decimal(10,0) DEFAULT NULL COMMENT '设定告警计时（秒）',
  `ALARM_TIME_DESC` varchar(64) DEFAULT NULL COMMENT '计时告警描述',
  `IS_CHECK_PREVENTION` decimal(10,0) DEFAULT NULL COMMENT '是否五防校验',
  `ALARM_FREQ_RANGE` decimal(10,0) DEFAULT NULL COMMENT '计次告警事件范围（秒）',
  `SEC_DEV_IN_GRP` decimal(10,0) DEFAULT NULL COMMENT '二次设备在设备组内序号',
  `SEC_DEVTP_NAME` varchar(64) DEFAULT NULL COMMENT '二次设备模板名',
  `SEC_SEQ_NO1` decimal(10,0) DEFAULT NULL COMMENT '输入点号1',
  `SEC_SEQ_NO2` decimal(10,0) DEFAULT NULL COMMENT '输入点号2',
  `SEC_SEQ_NO3` decimal(10,0) DEFAULT NULL COMMENT '输入点号3',
  `SEC_SEQ_NO4` decimal(10,0) DEFAULT NULL COMMENT '输入点号4',
  `SEC_SEQ_NO5` decimal(10,0) DEFAULT NULL COMMENT '输入点号5',
  `OUT_SEC_SEQ_NO1` decimal(10,0) DEFAULT NULL COMMENT '输出点号1',
  `OUT_SEC_SEQ_NO2` decimal(10,0) DEFAULT NULL COMMENT '输出点号2',
  `OUT_SEC_SEQ_NO3` decimal(10,0) DEFAULT NULL COMMENT '输出点号3',
  `OUT_SEC_SEQ_NO4` decimal(10,0) DEFAULT NULL COMMENT '输出点号4',
  `OUT_SEC_SEQ_NO5` decimal(10,0) DEFAULT NULL COMMENT '输出点号5',
  `KEY_VALUE` decimal(10,0) DEFAULT NULL COMMENT '关键数据标志',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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

drop trigger if exists TU_DIGITAL_TEMP_DEFINE;
DELIMITER ;;
create trigger TU_DIGITAL_TEMP_DEFINE AFTER UPDATE on digital_temp_define for each ROW 
BEGIN
    /*
    UPDATE digital SET 
    TAG_NAME = concat(DEVICE, '.', getAfterLastDot(NEW.TAG_NAME)),
    DESCRIPTION = NEW.DESCRIPTION,
    SEQ_NO = NEW.SEQ_NO,
    IS_CONTROL = NEW.IS_CONTROL,
    STATE_TEXT_NAME = NEW.STATE_TEXT_NAME,
    VALUE_NUM = NEW.VALUE_NUM,
    ALARM_PRIORITY = NEW.ALARM_PRIORITY,
    ALARM_DELAY_TIME = NEW.ALARM_DELAY_TIME,
    INPUT_DELAY_TIME = NEW.INPUT_DELAY_TIME,
    IS_WATER_ALM = NEW.IS_WATER_ALM,
    IS_ACK_ON_RTN = NEW.IS_ACK_ON_RTN,
    DEL_ACT_ON_ACK = NEW.DEL_ACT_ON_ACK,
    IS_NEVER_ALM_ON_RTN = NEW.IS_NEVER_ALM_ON_RTN,
    IS_SUMMARY = NEW.IS_SUMMARY,
    POINT_TP_NAME = NEW.TAG_NAME,
    IS_SAMPLE = NEW.IS_SAMPLE,
    IS_STATISTICS = NEW.IS_STATISTICS,
    SAMPLE_PERIOD = NEW.SAMPLE_PERIOD,
    IS_ALARM_FREQ = NEW.IS_ALARM_FREQ,
    ALARM_FREQ = NEW.ALARM_FREQ,
    ALARM_FREQ_DESC = NEW.ALARM_FREQ_DESC,
    IS_ALARM_TIME = NEW.IS_ALARM_TIME,
    ALARM_TIME = NEW.ALARM_TIME,
    ALARM_TIME_DESC = NEW.ALARM_TIME_DESC,
    ALARM_FREQ_RANGE = NEW.ALARM_FREQ_RANGE,
    POINT_TYPE = NEW.POINT_TYPE,
    POINT_CLASS = NEW.POINT_CLASS,
    POINT_PROPERTY = NEW.POINT_PROPERTY,
    POINT_SORT = NEW.POINT_SORT
    WHERE POINT_TP_NAME=OLD.TAG_NAME  ;
    UPDATE digital_control SET
    CTRL_ACT_NAME = NEW.CTRL_ACT_NAME,
    IS_TAGT_STATE = NEW.IS_TAGT_STATE,
    CTRL_TIMEOUT = NEW.CTRL_TIMEOUT,
    RESV_TIMEOUT = NEW.RESV_TIMEOUT,
    PULSE_DURATION = NEW.PULSE_DURATION,
    OFFSET_NUM = NEW.OFFSET_NUM,
    IS_CHECK_PREVENTION = NEW.IS_CHECK_PREVENTION,
    CTRL_TYPE = NEW.CTRL_TYPE,
    POINT_TP_NAME = NEW.TAG_NAME
    WHERE POINT_TP_NAME = OLD.TAG_NAME;
    */
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
        UPDATE temp_plugin_point_map_dig
        SET TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME)
        WHERE TEMP_NAME = OLD.DEV_TP_NAME AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    END IF;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'digital_temp_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('digital_temp_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_DIGITAL_TEMP_DEFINE;
DELIMITER ;;
create trigger TD_DIGITAL_TEMP_DEFINE BEFORE DELETE on digital_temp_define for each ROW 
BEGIN
    DELETE FROM temp_plugin_point_map_dig WHERE TEMP_NAME = OLD.DEV_TP_NAME  AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
/*
DELETE FROM digital WHERE POINT_TP_NAME = OLD.TAG_NAME;
*/
END;;
DELIMITER ;

drop trigger if exists TDA_DIGITAL_TEMP_DEFINE;
DELIMITER ;;
create trigger TDA_DIGITAL_TEMP_DEFINE AFTER DELETE on digital_temp_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'digital_temp_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('digital_temp_define'));
END IF;
END;;
DELIMITER ;

drop table if exists duty_define;
CREATE TABLE `duty_define` (
  `ID` decimal(10,0) NOT NULL COMMENT 'ID',
  `NAME` varchar(32) DEFAULT NULL COMMENT '排班定义名称',
  `START_TIME` varchar(32) DEFAULT NULL COMMENT '开始时间',
  `END_TIME` varchar(32) DEFAULT NULL COMMENT '结束时间',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='排班定义';

drop table if exists duty_info;
CREATE TABLE `duty_info` (
  `ID` decimal(10,0) NOT NULL COMMENT '编号',
  `GROUP_NAME` varchar(64) NOT NULL COMMENT '值班组名',
  `USERS` varchar(256) NOT NULL COMMENT '值班成员',
  `USERS_STATUS` varchar(128) NOT NULL COMMENT '成员签到状态',
  `START_TIME` varchar(32) NOT NULL COMMENT '开始时间',
  `END_TIME` varchar(32) DEFAULT NULL COMMENT '结束时间',
  `NOTE1` varchar(1024) DEFAULT NULL COMMENT '当班处理问题',
  `NOTE2` varchar(1024) DEFAULT NULL COMMENT '当班遗留问题',
  `NOTE3` varchar(1024) DEFAULT NULL COMMENT '关键信息备注',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='交接班信息表';

drop table if exists duty_setting;
CREATE TABLE `duty_setting` (
  `DATE` varchar(32) DEFAULT NULL COMMENT '日期',
  `DUTY_DEFINE_ID` decimal(10,0) DEFAULT NULL COMMENT '排班定义id',
  `USERS` varchar(256) DEFAULT NULL COMMENT '用户集合'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='排班设置';

drop table if exists event_task_link;
CREATE TABLE `event_task_link` (
  `EVENT_UUID_BASE64` varchar(23) NOT NULL COMMENT '事件的uuid-base64',
  `TASK_GRP_UUID` varchar(37) NOT NULL COMMENT '作业组的uuid',
  `TASK_PLAN_UUID` varchar(37) NOT NULL COMMENT '作业组计划的uuid',
  `TASK_METHOD` decimal(10,0) NOT NULL COMMENT '创建方式，1是自动，2是手动',
  PRIMARY KEY (`EVENT_UUID_BASE64`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警作业组关联表';

drop table if exists fault_recall_cond_def;
CREATE TABLE `fault_recall_cond_def` (
  `TRIGGER_TAG_NAME` varchar(64) NOT NULL COMMENT '触发器唯一标识',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属位置（车站）',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `MINUTES_BEFORE` decimal(10,0) DEFAULT NULL COMMENT '事故前记录分钟数（默认2）',
  `MINUTES_AFTER` decimal(10,0) DEFAULT NULL COMMENT '事故后记录分钟数（默认3）',
  `REPLAY_FIRST_PIC` varchar(64) DEFAULT NULL COMMENT '反演首画面',
  PRIMARY KEY (`TRIGGER_TAG_NAME`,`LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='事故追忆触发条件配置';

drop trigger if exists TIA_FAULT_RECALL_COND_DEF;
DELIMITER ;;
create trigger TIA_FAULT_RECALL_COND_DEF AFTER INSERT on fault_recall_cond_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fault_recall_cond_def', 'location_id,trigger_tag_name', '', concat(NEW.LOCATION_ID,',',NEW.TRIGGER_TAG_NAME), getTableSubSystem('fault_recall_cond_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_FAULT_RECALL_COND_DEF;
DELIMITER ;;
create trigger TUA_FAULT_RECALL_COND_DEF AFTER UPDATE on fault_recall_cond_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fault_recall_cond_def', 'location_id,trigger_tag_name', concat(OLD.LOCATION_ID,',',OLD.TRIGGER_TAG_NAME), concat(NEW.LOCATION_ID,',',NEW.TRIGGER_TAG_NAME), getTableSubSystem('fault_recall_cond_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_FAULT_RECALL_COND_DEF;
DELIMITER ;;
create trigger TDA_FAULT_RECALL_COND_DEF AFTER DELETE on fault_recall_cond_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fault_recall_cond_def', 'location_id,trigger_tag_name', concat(OLD.LOCATION_ID,',',OLD.TRIGGER_TAG_NAME), '', getTableSubSystem('fault_recall_cond_def'));
END IF;
END;;
DELIMITER ;

drop table if exists fault_recall_record;
CREATE TABLE `fault_recall_record` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属位置（车站）',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `NAME` varchar(128) NOT NULL COMMENT '事故追忆记录名称',
  `TIME_BEGING` decimal(20,0) NOT NULL COMMENT '开始时间',
  `TIME_END` decimal(20,0) NOT NULL COMMENT '结束时间',
  `REPLAY_FIRST_PIC` varchar(64) DEFAULT NULL COMMENT '反演播放首画面',
  PRIMARY KEY (`LOCATION_ID`,`NAME`,`TIME_BEGING`,`TIME_END`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='事故追忆记录';

drop table if exists fes_accuml;
CREATE TABLE `fes_accuml` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL,
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属应用',
  `DOT_NO` decimal(10,0) NOT NULL COMMENT '点号',
  `DESCRIPTION` varchar(128) DEFAULT NULL,
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位ID',
  `RTU_TAG` varchar(64) NOT NULL COMMENT 'RTU标签',
  `DEV_TAG` varchar(64) DEFAULT NULL,
  `SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '顺序号',
  `APP_TABLE_NAME` varchar(32) DEFAULT NULL COMMENT '后台应用表名',
  `APP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '后台标签名',
  `APP_COLUMN_NAME` varchar(16) DEFAULT NULL COMMENT '后台列名',
  `BASE` decimal(22,6) NOT NULL COMMENT '基值',
  `COEFF` decimal(22,6) NOT NULL COMMENT '系数',
  `RTU_NO` decimal(10,0) NOT NULL,
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `FES_POINT_TP_NAME` varchar(64) DEFAULT NULL,
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_ACCUML` (`DOT_NO`,`RTU_TAG`,`DEV_TAG`,`FES_POINT_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置累计量参数表';

drop trigger if exists TI_FES_ACCUML;
DELIMITER ;;
create trigger TI_FES_ACCUML AFTER INSERT on fes_accuml for each ROW 
BEGIN
    #首先禁止MAP_TO_FES触发器动作
    SET @disableAccumlMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesAccumlTrigger,0) = 0 THEN
    BEGIN
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE accuml_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        
    END;
    END IF;
    
    SET @disableAccumlMapToFesTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_accuml', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_accuml'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_ACCUML;
DELIMITER ;;
create trigger TU_FES_ACCUML BEFORE UPDATE on fes_accuml for each ROW 
body:BEGIN
    IF OLD.APP_TABLE_NAME=NEW.APP_TABLE_NAME AND OLD.APP_TAG_NAME = NEW.APP_TAG_NAME AND OLD.APP_COLUMN_NAME = NEW.APP_COLUMN_NAME
    AND OLD.RTU_TAG = NEW.RTU_TAG AND OLD.DOT_NO = NEW.DOT_NO
    THEN LEAVE body;
    END IF;
        
    SET @disableAccumlMapToFesTrigger = 1;
    
    IF COALESCE(@disableFesAccumlTrigger,0) = 0 THEN
    BEGIN
        #删除旧关联
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE accuml_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        #建立新关联
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE accuml_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
    END;
    END IF;
    
    SET @disableAccumlMapToFesTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TUA_FES_ACCUML;
DELIMITER ;;
create trigger TUA_FES_ACCUML AFTER UPDATE on fes_accuml for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_accuml', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_accuml'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_ACCUML;
DELIMITER ;;
create trigger TD_FES_ACCUML AFTER DELETE on fes_accuml for each ROW 
BEGIN    
    SET @disableAccumlMapToFesTrigger = 1;
    
    IF COALESCE(@disableFesAccumlTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE accuml_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
    END;
    END IF;
    
    SET @disableAccumlMapToFesTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_accuml', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_accuml'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_analog;
CREATE TABLE `fes_analog` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `DEV_TAG` varchar(64) NOT NULL,
  `SEQ_NO` decimal(10,0) NOT NULL COMMENT '设备点序号',
  `RTU_TAG` varchar(64) NOT NULL COMMENT 'RTU标签',
  `RTU_NO` decimal(10,0) NOT NULL,
  `DOT_NO` decimal(10,0) NOT NULL COMMENT '点号/远动号',
  `APP_TABLE_NAME` varchar(32) DEFAULT NULL COMMENT '后台应用表名',
  `APP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '后台标签名',
  `APP_COLUMN_NAME` varchar(16) DEFAULT NULL COMMENT '后台列名',
  `IS_FILTER` decimal(10,0) DEFAULT NULL COMMENT '是否过滤AI突变',
  `PERCENT` decimal(10,0) DEFAULT NULL COMMENT '突变百分比',
  `DEADBAND_TYPE` decimal(10,0) NOT NULL COMMENT '死区类型',
  `DEADBAND` decimal(22,6) NOT NULL COMMENT '死区值',
  `ZEROBAND` decimal(22,6) NOT NULL COMMENT '归零死区',
  `ZEROBAND_H` decimal(22,6) DEFAULT NULL COMMENT '归零死区H',
  `ZEROBAND_L` decimal(22,6) DEFAULT NULL COMMENT '归零死区L',
  `ZEROBAND_TYPE` decimal(10,0) DEFAULT NULL COMMENT '归零死区类型',
  `BASE` decimal(22,6) NOT NULL COMMENT '基值',
  `COEFF` decimal(22,6) NOT NULL COMMENT '系数',
  `MAX_RANGE` decimal(22,6) DEFAULT NULL COMMENT '最大量程',
  `MIN_RANGE` decimal(22,6) DEFAULT NULL COMMENT '最小量程',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `DESCRIPTION` varchar(128) NOT NULL,
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位ID',
  `FES_POINT_TP_NAME` varchar(64) DEFAULT NULL,
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_ANALOG` (`DEV_TAG`,`RTU_TAG`,`DOT_NO`,`FES_POINT_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置模拟量参数表';

drop trigger if exists TI_FES_ANALOG;
DELIMITER ;;
create trigger TI_FES_ANALOG AFTER INSERT on fes_analog for each ROW 
BEGIN
    #首先禁止MAP_TO_FES触发器动作
    SET @disableAnalogMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesAnalogTrigger,0) = 0 THEN
    BEGIN
       
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE analog_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
          
    END;
    END IF;
    
    SET @disableAnalogMapToFesTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_analog', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_analog'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_ANALOG;
DELIMITER ;;
create trigger TU_FES_ANALOG BEFORE UPDATE on fes_analog for each ROW 
body:BEGIN
    IF OLD.APP_TABLE_NAME=NEW.APP_TABLE_NAME AND OLD.APP_TAG_NAME = NEW.APP_TAG_NAME AND OLD.APP_COLUMN_NAME = NEW.APP_COLUMN_NAME
    AND OLD.RTU_TAG = NEW.RTU_TAG AND OLD.DOT_NO = NEW.DOT_NO
    THEN LEAVE body;
    END IF;
    
    #首先禁止MAP_TO_FES触发器动作
    SET @disableAnalogMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesAnalogTrigger,0) = 0 THEN
    BEGIN
        
        #不管是ana还是const都是放在analog_map_to_fes表
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE analog_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE analog_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET @disableAnalogMapToFesTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TUA_FES_ANALOG;
DELIMITER ;;
create trigger TUA_FES_ANALOG AFTER UPDATE on fes_analog for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_analog', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_analog'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_ANALOG;
DELIMITER ;;
create trigger TD_FES_ANALOG AFTER DELETE on fes_analog for each ROW 
BEGIN
    #首先禁止MAP_TO_FES触发器动作
    SET @disableAnalogMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesAnalogTrigger,0) = 0 THEN
    BEGIN
        #不管是ana还是const都是放在analog_map_to_fes表
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE analog_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
    END;
    END IF;
    
    SET @disableAnalogMapToFesTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_analog', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_analog'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_analog_ctrl;
CREATE TABLE `fes_analog_ctrl` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `BASE` decimal(22,6) NOT NULL COMMENT '基数',
  `COEFF` decimal(22,6) NOT NULL COMMENT '系数',
  `MAX_RANGE` decimal(22,6) DEFAULT NULL COMMENT '最大量程',
  `MIN_RANGE` decimal(22,6) DEFAULT NULL COMMENT '最小量程',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RTU_NO` decimal(10,0) NOT NULL,
  `DEV_TAG` varchar(64) DEFAULT NULL,
  `DOT_NO` decimal(10,0) NOT NULL COMMENT 'AO点号',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `SEQ_NO` decimal(10,0) DEFAULT NULL,
  `RTU_TAG` varchar(64) NOT NULL COMMENT 'RTU标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_ANALOG_CTRL` (`DEV_TAG`,`DOT_NO`,`RTU_TAG`,`POINT_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模拟量输出参数表';

drop trigger if exists TIA_FES_ANALOG_CTRL;
DELIMITER ;;
create trigger TIA_FES_ANALOG_CTRL AFTER INSERT on fes_analog_ctrl for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_analog_ctrl', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_analog_ctrl'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_ANALOG_CTRL;
DELIMITER ;;
create trigger TU_FES_ANALOG_CTRL AFTER UPDATE on fes_analog_ctrl for each ROW 
body:BEGIN
    #更新关联了此点的遥控点
    
    IF NEW.RTU_TAG <> OLD.RTU_TAG OR NEW.DOT_NO<> OLD.DOT_NO
    THEN 
    BEGIN
        UPDATE analog_control SET
        RTU_TAG = NEW.RTU_TAG,
        OFFSET_NO = NEW.DOT_NO
        WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO = cast(OLD.DOT_NO AS char);
    END; 
    END IF;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_analog_ctrl', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_analog_ctrl'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_ANALOG_CTRL;
DELIMITER ;;
create trigger TD_FES_ANALOG_CTRL AFTER DELETE on fes_analog_ctrl for each ROW 
BEGIN
    UPDATE analog_control SET
    OFFSET_NO = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO = cast(OLD.DOT_NO AS char);
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_analog_ctrl', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_analog_ctrl'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_channel_para;
CREATE TABLE `fes_channel_para` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '通道标签',
  `CHAN_NAME` varchar(64) NOT NULL COMMENT '通道名称',
  `CHAN_NO` decimal(10,0) NOT NULL,
  `IS_USED` decimal(10,0) NOT NULL,
  `ALARM_ENABLE` decimal(10,0) NOT NULL COMMENT '通道告警使能',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  `COMM_TYPE` decimal(10,0) NOT NULL COMMENT '通讯类型（例如：TCP/UDP/COM）',
  `COMM_MODE` decimal(10,0) NOT NULL,
  `PROTO_TYPE` decimal(10,0) NOT NULL COMMENT '通讯规约',
  `SENDBUF_LEN` decimal(10,0) DEFAULT NULL COMMENT '发送缓冲区长度',
  `RECVBUF_LEN` decimal(10,0) DEFAULT NULL COMMENT '接收缓冲区长度',
  `REGION_ID` decimal(10,0) DEFAULT NULL COMMENT '责任区',
  `LISTEN_PORT_NO` decimal(10,0) DEFAULT NULL COMMENT '侦听端口',
  `QUEUE_LEN` decimal(10,0) DEFAULT NULL COMMENT '侦听队列长度',
  `NET_DESC1` varchar(32) NOT NULL COMMENT '网卡描述1',
  `PORT_NO1` decimal(10,0) NOT NULL COMMENT '端口号1',
  `NET_DESC2` varchar(32) DEFAULT NULL COMMENT '网卡描述2',
  `PORT_NO2` decimal(10,0) DEFAULT NULL COMMENT '端口号2',
  `NET_DESC3` varchar(32) DEFAULT NULL COMMENT '网卡描述3',
  `PORT_NO3` decimal(10,0) DEFAULT NULL COMMENT '端口号3',
  `NET_DESC4` varchar(32) DEFAULT NULL COMMENT '网卡描述4',
  `PORT_NO4` decimal(10,0) DEFAULT NULL COMMENT '端口号4',
  `CONNECTWAIT_SEC` decimal(10,0) DEFAULT NULL COMMENT '连接等待时间',
  `CONNECT_TIMEOUT` decimal(10,0) DEFAULT NULL,
  `CONNECT_RETRYS` decimal(10,0) DEFAULT NULL COMMENT '连接尝试次数',
  `RECV_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '接收超时时间',
  `RESP_TIMEOUT` decimal(10,0) DEFAULT NULL,
  `BAUD` decimal(10,0) DEFAULT NULL COMMENT '波特率',
  `PARITY` decimal(10,0) DEFAULT NULL COMMENT '校验位',
  `DATABIT` decimal(10,0) DEFAULT NULL COMMENT '数据位',
  `STOPBIT` decimal(10,0) DEFAULT NULL COMMENT '停止位',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '备用参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '备用参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '备用参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '备用参数4',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  `ERROR_RATE_LIMIT` decimal(22,6) DEFAULT NULL,
  `BACKUP_CHAN_NAME1` varchar(64) DEFAULT NULL,
  `BACKUP_CHAN_NAME2` varchar(64) DEFAULT NULL,
  `BACKUP_CHAN_NAME3` varchar(64) DEFAULT NULL,
  `BACKUP_CHAN_NO1` decimal(10,0) NOT NULL,
  `BACKUP_CHAN_NO2` decimal(10,0) NOT NULL,
  `BACKUP_CHAN_NO3` decimal(10,0) NOT NULL,
  `DESCRIPTION` varchar(128) DEFAULT NULL,
  `COMM_PROPERTY` decimal(10,0) DEFAULT NULL,
  `STATUS` decimal(10,0) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`),
  UNIQUE KEY `UN_FES_CHANNEL_PARA` (`CHAN_NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置通道参数表';

drop trigger if exists TI_FES_CHANNEL_PARA;
DELIMITER ;;
create trigger TI_FES_CHANNEL_PARA BEFORE INSERT on fes_channel_para for each ROW 
BEGIN
    
    DECLARE locationTag varchar(64) DEFAULT '';
    DECLARE subsystemTag varchar(64) DEFAULT '';
    
    
    SELECT TAG_NAME INTO locationTag FROM sys_model_location_info WHERE LOCATION_ID = NEW.LOCATION_ID;
    SELECT TAG_NAME INTO subsystemTag FROM sys_model_sub_system_info WHERE SUB_SYSTEM_ID = NEW.SUB_SYSTEM;
    SET NEW.TAG_NAME = concat(locationTag,'.',subsystemTag,'.',NEW.CHAN_NAME);
    
END;;
DELIMITER ;

drop trigger if exists TIA_FES_CHANNEL_PARA;
DELIMITER ;;
create trigger TIA_FES_CHANNEL_PARA AFTER INSERT on fes_channel_para for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_channel_para', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_channel_para'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_CHANNEL_PARA;
DELIMITER ;;
create trigger TU_FES_CHANNEL_PARA BEFORE UPDATE on fes_channel_para for each ROW 
body:BEGIN
    
    DECLARE locationTag varchar(64) DEFAULT '';
    DECLARE subsystemTag varchar(64) DEFAULT '';
    
    IF NEW.CHAN_NAME <> OLD.CHAN_NAME THEN 
    BEGIN 
        SELECT TAG_NAME INTO locationTag FROM sys_model_location_info WHERE LOCATION_ID = NEW.LOCATION_ID;
        SELECT TAG_NAME INTO subsystemTag FROM sys_model_sub_system_info WHERE SUB_SYSTEM_ID = NEW.SUB_SYSTEM;
        SET NEW.TAG_NAME = concat(locationTag,'.',subsystemTag,'.',NEW.CHAN_NAME);
    END;
    END IF; 
END;;
DELIMITER ;

drop trigger if exists TUA_FES_CHANNEL_PARA;
DELIMITER ;;
create trigger TUA_FES_CHANNEL_PARA AFTER UPDATE on fes_channel_para for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_channel_para', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_channel_para'));
END IF;
IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.CHAN_NO <> OLD.CHAN_NO OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM THEN
BEGIN
    UPDATE fes_rtu_para SET
    TAG_NAME = concat(NEW.TAG_NAME,'.',RTU_NAME),
    CHAN_TAG = NEW.TAG_NAME,
    CHAN_NO = NEW.CHAN_NO,
    LOCATION_ID = NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM
    WHERE CHAN_TAG = OLD.TAG_NAME;
END;
END IF;
	
END;;
DELIMITER ;

drop trigger if exists TDA_FES_CHANNEL_PARA;
DELIMITER ;;
create trigger TDA_FES_CHANNEL_PARA AFTER DELETE on fes_channel_para for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_channel_para', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_channel_para'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_const;
CREATE TABLE `fes_const` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `GROUP_NO` decimal(10,0) DEFAULT NULL COMMENT '定值组号',
  `RTU_TAG` varchar(64) NOT NULL COMMENT 'RTU标签',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT 'RTU号',
  `DOT_NO` decimal(10,0) NOT NULL COMMENT '点号',
  `SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '序号',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '专业',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '车站',
  `DEV_TAG` varchar(64) DEFAULT NULL COMMENT '设备标签',
  `VALUE` decimal(22,6) DEFAULT NULL COMMENT '类型',
  `VALUE_TYPE` decimal(10,0) DEFAULT NULL COMMENT '定值类型',
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位',
  `VALUE_TEXT` varchar(64) DEFAULT NULL COMMENT '修正值',
  `VALUE_RATIO` decimal(22,6) DEFAULT NULL COMMENT '死区值',
  `VALUE_ADDED` decimal(22,6) DEFAULT NULL COMMENT '修正值',
  `VALUE_MAX` decimal(22,6) DEFAULT NULL COMMENT '最大值',
  `VALUE_MIN` decimal(22,6) DEFAULT NULL COMMENT '最小值',
  `VALUE_DEFAULT` decimal(22,6) DEFAULT NULL COMMENT '默认值',
  `VALUE_VERIFY` decimal(22,6) DEFAULT NULL COMMENT '反校值',
  `EX_INFO` varchar(64) DEFAULT NULL COMMENT '附加信息',
  `FES_POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '所属模板点',
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  `DZ_SEQ` decimal(10,0) DEFAULT NULL COMMENT '定值序号',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_CONST` (`FES_POINT_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置定值管理表';

drop trigger if exists TI_FES_CONST;
DELIMITER ;;
create trigger TI_FES_CONST AFTER INSERT on fes_const for each ROW 
BEGIN
      
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_const', 'tag_name', '',NEW.TAG_NAME,  getTableSubSystem('fes_const'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TU_FES_CONST;
DELIMITER ;;
create trigger TU_FES_CONST AFTER UPDATE on fes_const for each ROW 
BEGIN
      
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_const', 'tag_name', OLD.TAG_NAME,NEW.TAG_NAME,  getTableSubSystem('fes_const'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TD_FES_CONST;
DELIMITER ;;
create trigger TD_FES_CONST AFTER DELETE on fes_const for each ROW 
BEGIN
      
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_const', 'tag_name', OLD.TAG_NAME,'',  getTableSubSystem('fes_const'));
    END IF;
    
END;;
DELIMITER ;

drop table if exists fes_const_temp;
CREATE TABLE `fes_const_temp` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `GROUP_NO` decimal(10,0) DEFAULT NULL COMMENT '定值组号',
  `SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '序号',
  `DEV_TP_NAME` varchar(64) DEFAULT NULL COMMENT '设备标签',
  `VALUE` decimal(22,6) DEFAULT NULL COMMENT '类型',
  `VALUE_TYPE` decimal(10,0) DEFAULT NULL COMMENT '定值类型',
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位',
  `VALUE_TEXT` varchar(64) DEFAULT NULL COMMENT '修正值',
  `VALUE_RATIO` decimal(22,6) DEFAULT NULL COMMENT '死区值',
  `VALUE_ADDED` decimal(22,6) DEFAULT NULL COMMENT '修正值',
  `VALUE_MAX` decimal(22,6) DEFAULT NULL COMMENT '最大值',
  `VALUE_MIN` decimal(22,6) DEFAULT NULL COMMENT '最小值',
  `VALUE_DEFAULT` decimal(22,6) DEFAULT NULL COMMENT '默认值',
  `VALUE_VERIFY` decimal(22,6) DEFAULT NULL COMMENT '反校值',
  `EX_INFO` varchar(64) DEFAULT NULL COMMENT '附加信息',
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  `DZ_SEQ` decimal(10,0) DEFAULT NULL COMMENT '定值序号',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_CONST_TEMP` (`DEV_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置定值模板表';

drop trigger if exists TI_FES_CONST_TEMP;
DELIMITER ;;
create trigger TI_FES_CONST_TEMP AFTER INSERT on fes_const_temp for each ROW 
BEGIN
    /*
    INSERT INTO fes_const(
    TAG_NAME,                                                                                              DESCRIPTION,    GROUP_NO,    RTU_TAG,              RTU_NO,            DOT_NO,                                                           SEQ_NO,    SUB_SYSTEM,               LOCATION_ID,           DEV_TAG,              VALUE,    VALUE_TYPE,   UNIT_ID,    VALUE_TEXT,      VALUE_RATIO,   VALUE_ADDED,    VALUE_MAX,     VALUE_MIN,   VALUE_DEFAULT,     VALUE_VERIFY,    EX_INFO,   FES_POINT_TP_NAME,PATH61850,POSITION61850) SELECT   
    concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_CONST'))), NEW.DESCRIPTION,NEW.GROUP_NO,fes_dev_info.RTU_TAG,fes_rtu_para.RTU_NO,getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_CONST')),NEW.SEQ_NO,fes_rtu_para.SUB_SYSTEM,fes_rtu_para.LOCATION_ID,fes_dev_info.TAG_NAME,NEW.VALUE,NEW.VALUE_TYPE,NEW.UNIT_ID,NEW.VALUE_TEXT, NEW.VALUE_RATIO,NEW.VALUE_ADDED,NEW.VALUE_MAX,NEW.VALUE_MIN,NEW.VALUE_DEFAULT,NEW.VALUE_VERIFY,NEW.EX_INFO,NEW.TAG_NAME,    NEW.PATH61850,NEW.POSITION61850
    FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
    */
    IF getTriggerFlag() = TRUE THEN 
        INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
        VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_const_temp', 'tag_name',    '',           NEW.TAG_NAME, getTableSubSystem('fes_const_temp'));
    END IF;
    
    
END;;
DELIMITER ;

drop trigger if exists TU_FES_CONST_TEMP;
DELIMITER ;;
create trigger TU_FES_CONST_TEMP AFTER UPDATE on fes_const_temp for each ROW 
BEGIN
    /*
    UPDATE fes_const SET 
    DESCRIPTION = NEW.DESCRIPTION,
    GROUP_NO = NEW.GROUP_NO,
    SEQ_NO = NEW.SEQ_NO,
    VALUE = NEW.VALUE,
    VALUE_TYPE = NEW.VALUE_TYPE,
    UNIT_ID = NEW.UNIT_ID,
    VALUE_TEXT = NEW.VALUE_TEXT,
    VALUE_RATIO = NEW.VALUE_RATIO,
    VALUE_ADDED = NEW.VALUE_ADDED,
    VALUE_MAX = NEW.VALUE_MAX,
    VALUE_MIN = NEW.VALUE_MIN,
    VALUE_DEFAULT = NEW.VALUE_DEFAULT,
    VALUE_VERIFY = NEW.VALUE_VERIFY,
    EX_INFO = NEW.EX_INFO,
    FES_POINT_TP_NAME = NEW.TAG_NAME,
    PATH61850 = NEW.PATH61850,
    POSITION61850 = NEW.POSITION61850
    WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
    */
        
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_const_temp', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_const_temp'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TD_FES_CONST_TEMP;
DELIMITER ;;
create trigger TD_FES_CONST_TEMP AFTER DELETE on fes_const_temp for each ROW 
BEGIN
    /*
    DELETE FROM fes_const WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
     */   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_const_temp', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_const_temp'));
    END IF;
    
END;;
DELIMITER ;

drop table if exists fes_data_block;
CREATE TABLE `fes_data_block` (
  `TAG_NAME` varchar(64) NOT NULL,
  `BLOCK_ID` decimal(10,0) NOT NULL COMMENT '序号',
  `LOCATION_ID` decimal(10,0) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  `RTU_TAG` varchar(64) NOT NULL COMMENT 'RTU标签',
  `RTU_NO` decimal(10,0) NOT NULL COMMENT 'RTU号',
  `FUNC_CODE` decimal(10,0) NOT NULL COMMENT '功能码',
  `STAR_ADDR` decimal(10,0) NOT NULL COMMENT '起始地址',
  `DATA_LEN` decimal(10,0) NOT NULL COMMENT '数据长度',
  `DATA_FARME_TYPE` decimal(10,0) NOT NULL COMMENT '帧类别',
  `REF_CYCLE` decimal(10,0) NOT NULL COMMENT '刷新周期',
  `IS_CREATE_SOE` decimal(10,0) DEFAULT NULL,
  `PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '备用参数1',
  `PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '备用参数2',
  `DESCRIPTION` varchar(128) DEFAULT NULL,
  `LD` varchar(64) DEFAULT NULL,
  `CALL_MODE` decimal(10,0) DEFAULT NULL,
  `DATA_SET` varchar(64) DEFAULT NULL,
  `DATA_SET_TYPE` decimal(10,0) DEFAULT NULL COMMENT '数据集类型',
  `DATA_SET_CALL_TIME` decimal(10,0) DEFAULT NULL,
  `REPORT_CTRL` varchar(64) DEFAULT NULL,
  `DEV_TAG` varchar(64) DEFAULT NULL,
  `FES_POINT_TP_NAME` varchar(64) DEFAULT NULL,
  `PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '自定义参数3',
  `PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '自定义参数4',
  `PARA_STR1` varchar(64) DEFAULT NULL,
  `PARA_STR2` varchar(256) DEFAULT NULL,
  `IS_ENABLE` decimal(10,0) DEFAULT NULL COMMENT '块使能',
  PRIMARY KEY (`TAG_NAME`,`BLOCK_ID`,`RTU_NO`,`FUNC_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置数据块表';

drop trigger if exists TIA_FES_DATA_BLOCK;
DELIMITER ;;
create trigger TIA_FES_DATA_BLOCK AFTER INSERT on fes_data_block for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,       KEY_COLUMN_NAME,                                                                  KEY_OLD_VALUE,   KEY_NEW_VALUE,                                                                                                                      SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_data_block', 'tag_name,block_id,rtu_no,func_code', '',             concat(NEW.TAG_NAME,',', NEW.BLOCK_ID,',',NEW.RTU_NO,',',NEW.FUNC_CODE), getTableSubSystem('fes_data_block'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_FES_DATA_BLOCK;
DELIMITER ;;
create trigger TUA_FES_DATA_BLOCK AFTER UPDATE on fes_data_block for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                     KEY_OLD_VALUE,                                                            KEY_NEW_VALUE,                                                         SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_data_block', 'tag_name,block_id,rtu_no,func_code', concat(OLD.TAG_NAME,',', OLD.BLOCK_ID,',',OLD.RTU_NO,',',OLD.FUNC_CODE), concat(NEW.TAG_NAME,',',NEW.BLOCK_ID,',',NEW.RTU_NO,',',NEW.FUNC_CODE), getTableSubSystem('fes_data_block'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_FES_DATA_BLOCK;
DELIMITER ;;
create trigger TDA_FES_DATA_BLOCK AFTER DELETE on fes_data_block for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,        KEY_COLUMN_NAME,                                                                  KEY_OLD_VALUE,         KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_data_block', 'tag_name,block_id,rtu_no,func_code', concat(OLD.TAG_NAME,OLD.BLOCK_ID,',',OLD.RTU_NO,',',OLD.FUNC_CODE), '', getTableSubSystem('fes_data_block'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_data_block_temp;
CREATE TABLE `fes_data_block_temp` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '点标签',
  `BLOCK_ID` decimal(10,0) DEFAULT NULL,
  `FUNC_CODE` decimal(10,0) DEFAULT NULL,
  `STAR_ADDR` decimal(10,0) DEFAULT NULL,
  `DATA_LEN` decimal(10,0) DEFAULT NULL,
  `DATA_FARME_TYPE` decimal(10,0) DEFAULT NULL,
  `REF_CYCLE` decimal(10,0) DEFAULT NULL,
  `IS_CREATE_SOE` decimal(10,0) DEFAULT NULL,
  `PARA_INT1` decimal(10,0) DEFAULT NULL,
  `PARA_INT2` decimal(10,0) DEFAULT NULL,
  `DESCRIPTION` varchar(128) DEFAULT NULL,
  `LD` varchar(64) DEFAULT NULL,
  `CALL_MODE` decimal(10,0) DEFAULT NULL,
  `DATA_SET` varchar(64) DEFAULT NULL,
  `DATA_SET_TYPE` decimal(10,0) DEFAULT NULL COMMENT '数据集类型',
  `DATA_SET_CALL_TIME` decimal(10,0) DEFAULT NULL COMMENT '数据集召唤时间',
  `REPORT_CTRL` varchar(64) DEFAULT NULL,
  `DEV_TP_NAME` varchar(64) NOT NULL COMMENT '设备名称',
  `PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '自定义参数3',
  `PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '自定义参数4',
  `PARA_STR1` varchar(64) DEFAULT NULL,
  `PARA_STR2` varchar(256) DEFAULT NULL,
  `IS_ENABLE` decimal(10,0) DEFAULT NULL COMMENT '块使能',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_DATA_BLOCK_TEMP` (`DEV_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置点模板';

drop trigger if exists TI_FES_DATA_BLOCK_TEMP;
DELIMITER ;;
create trigger TI_FES_DATA_BLOCK_TEMP AFTER INSERT on fes_data_block_temp for each ROW 
BEGIN
    /*
    INSERT INTO fes_data_block(
    TAG_NAME,                                                        BLOCK_ID,     RTU_NO,            FUNC_CODE,    STAR_ADDR,    DATA_LEN,     DATA_FARME_TYPE,     REF_CYCLE,     IS_CREATE_SOE,     PARA_INT1,     PARA_INT2,     DESCRIPTION,    LOCATION_ID,            SUB_SYSTEM,              LD,     CALL_MODE,     DATA_SET,    DATA_SET_TYPE,    DATA_SET_CALL_TIME,    REPORT_CTRL,   FES_POINT_TP_NAME, DEV_TAG) SELECT   
    concat(fes_dev_info.TAG_NAME,'.',getAfterLastDot(NEW.TAG_NAME)), NEW.BLOCK_ID,fes_rtu_para.RTU_NO,NEW.FUNC_CODE,NEW.STAR_ADDR,NEW.DATA_LEN, NEW.DATA_FARME_TYPE, NEW.REF_CYCLE, NEW.IS_CREATE_SOE, NEW.PARA_INT1, NEW.PARA_INT2, NEW.DESCRIPTION,fes_rtu_para.LOCATION_ID,fes_rtu_para.SUB_SYSTEM,NEW.LD, NEW.CALL_MODE, NEW.DATA_SET,NEW.DATA_SET_TYPE,NEW.DATA_SET_CALL_TIME,NEW.REPORT_CTRL,NEW.TAG_NAME,     fes_dev_info.TAG_NAME
    FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
    */
    IF getTriggerFlag() = TRUE THEN 
        INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,           KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
        VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_data_block_temp', 'tag_name',    '',           NEW.TAG_NAME, getTableSubSystem('fes_data_block_temp'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_DATA_BLOCK_TEMP;
DELIMITER ;;
create trigger TU_FES_DATA_BLOCK_TEMP AFTER UPDATE on fes_data_block_temp for each ROW 
BEGIN
    /*       
    UPDATE fes_data_block SET 
    TAG_NAME = concat(getBeforeLastDot(fes_data_block.TAG_NAME),'.',getAfterLastDot(NEW.TAG_NAME)),
    BLOCK_ID = NEW.BLOCK_ID,
    FUNC_CODE = NEW.FUNC_CODE,
    STAR_ADDR = NEW.STAR_ADDR,
    DATA_LEN = NEW.DATA_LEN,
    DATA_FARME_TYPE = NEW.DATA_FARME_TYPE,
    REF_CYCLE = NEW.REF_CYCLE,
    IS_CREATE_SOE = NEW.IS_CREATE_SOE,
    PARA_INT1 = NEW.PARA_INT1,
    PARA_INT2 = NEW.PARA_INT2,
    DESCRIPTION = NEW.DESCRIPTION,
    LD = NEW.LD,
    CALL_MODE = NEW.CALL_MODE,
    DATA_SET = NEW.DATA_SET,
    DATA_SET_TYPE = NEW.DATA_SET_TYPE,
    DATA_SET_CALL_TIME = NEW.DATA_SET_CALL_TIME,
    REPORT_CTRL = NEW.REPORT_CTRL,
    FES_POINT_TP_NAME = NEW.TAG_NAME
    WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
    */
        
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_data_block_temp', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_data_block_temp'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_DATA_BLOCK_TEMP;
DELIMITER ;;
create trigger TD_FES_DATA_BLOCK_TEMP AFTER DELETE on fes_data_block_temp for each ROW 
BEGIN
    /*  
    DELETE FROM fes_data_block WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
      */  
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_data_block_temp', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_data_block_temp'));
    END IF;
END;;
DELIMITER ;

drop table if exists fes_dev_info;
CREATE TABLE `fes_dev_info` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '设备标签',
  `DEV_NAME` varchar(128) NOT NULL COMMENT '设备名称',
  `LOCATION_ID` decimal(10,0) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  `DEV_ID` decimal(10,0) NOT NULL COMMENT '设备ID',
  `DEV_TYPE_ID` decimal(10,0) NOT NULL COMMENT '设备类型ID',
  `RTU_TAG` varchar(64) NOT NULL COMMENT '设备DPU',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT 'RTU序号',
  `DEV_DESC` varchar(64) DEFAULT NULL COMMENT '设备描述',
  `DEV_TP_NAME` varchar(64) DEFAULT NULL,
  `DEV_PORT` varchar(64) DEFAULT NULL COMMENT '管理机端口',
  `PARA_STR1` varchar(64) DEFAULT NULL COMMENT '备用字段1',
  `PARA_STR2` varchar(256) DEFAULT NULL COMMENT '备用字段2',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置设备信息表';

drop trigger if exists TIB_FES_DEV_INFO;
DELIMITER ;;
create trigger TIB_FES_DEV_INFO BEFORE INSERT on fes_dev_info for each ROW 
BEGIN
    
    SET NEW.TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DEV_NAME);
    
END;;
DELIMITER ;

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

drop trigger if exists TUB_FES_DEV_INFO;
DELIMITER ;;
create trigger TUB_FES_DEV_INFO BEFORE UPDATE on fes_dev_info for each ROW 
BEGIN
    
    IF NEW.DEV_NAME <> OLD.DEV_NAME THEN 
        SET NEW.TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DEV_NAME);
    END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_DEV_INFO;
DELIMITER ;;
create trigger TU_FES_DEV_INFO AFTER UPDATE on fes_dev_info for each ROW 
BEGIN
        IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.RTU_TAG <> OLD.RTU_TAG OR NEW.LOCATION_ID <> OLD.LOCATION_ID OR NEW.SUB_SYSTEM <> OLD.SUB_SYSTEM
        OR NEW.RTU_NO <> OLD.RTU_NO
        THEN 
        BEGIN 
        
            UPDATE fes_accuml INNER JOIN fes_rtu_para 
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_accuml.DEV_TAG = OLD.TAG_NAME SET 
            fes_accuml.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_accuml.DOT_NO), 
            fes_accuml.LOCATION_ID = fes_rtu_para.LOCATION_ID,
            fes_accuml.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM,
            fes_accuml.RTU_TAG = NEW.RTU_TAG,
            fes_accuml.DEV_TAG = NEW.TAG_NAME,
            fes_accuml.RTU_NO = fes_rtu_para.RTU_NO;
            
            
            UPDATE fes_analog INNER JOIN fes_rtu_para 
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_analog.DEV_TAG = OLD.TAG_NAME SET 
            fes_analog.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_analog.DOT_NO), 
            fes_analog.LOCATION_ID = fes_rtu_para.LOCATION_ID,
            fes_analog.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM,
            fes_analog.RTU_TAG = NEW.RTU_TAG,
            fes_analog.DEV_TAG = NEW.TAG_NAME,
            fes_analog.RTU_NO = fes_rtu_para.RTU_NO;
            
            
            UPDATE fes_digital INNER JOIN fes_rtu_para 
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_digital.DEV_TAG = OLD.TAG_NAME SET 
            fes_digital.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_digital.DOT_NO ), 
            fes_digital.LOCATION_ID = fes_rtu_para.LOCATION_ID,
            fes_digital.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM,
            fes_digital.RTU_TAG = NEW.RTU_TAG,
            fes_digital.DEV_TAG = NEW.TAG_NAME,
            fes_digital.RTU_NO = fes_rtu_para.RTU_NO;
            
            
            UPDATE fes_mix INNER JOIN fes_rtu_para 
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_mix.DEV_TAG = OLD.TAG_NAME SET 
            fes_mix.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_mix.DOT_NO), 
            fes_mix.LOCATION_ID = fes_rtu_para.LOCATION_ID,
            fes_mix.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM,
            fes_mix.RTU_TAG = NEW.RTU_TAG,
            fes_mix.DEV_TAG = NEW.TAG_NAME,
            fes_mix.RTU_NO = fes_rtu_para.RTU_NO;
            
            
            UPDATE fes_analog_ctrl INNER JOIN fes_rtu_para 
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_analog_ctrl.DEV_TAG = OLD.TAG_NAME SET 
            fes_analog_ctrl.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_analog_ctrl.DOT_NO), 
            fes_analog_ctrl.LOCATION_ID = fes_rtu_para.LOCATION_ID,
            fes_analog_ctrl.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM,
            fes_analog_ctrl.RTU_TAG = NEW.RTU_TAG,
            fes_analog_ctrl.DEV_TAG = NEW.TAG_NAME,
            fes_analog_ctrl.RTU_NO = fes_rtu_para.RTU_NO;
            
            
            UPDATE fes_digital_ctrl INNER JOIN fes_rtu_para 
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_digital_ctrl.DEV_TAG = OLD.TAG_NAME SET 
            fes_digital_ctrl.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_digital_ctrl.DOT_NO), 
            fes_digital_ctrl.LOCATION_ID = fes_rtu_para.LOCATION_ID,
            fes_digital_ctrl.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM,
            fes_digital_ctrl.RTU_TAG = NEW.RTU_TAG,
            fes_digital_ctrl.DEV_TAG = NEW.TAG_NAME,
            fes_digital_ctrl.RTU_NO = fes_rtu_para.RTU_NO;
            
            
            UPDATE fes_mix_ctrl INNER JOIN fes_rtu_para 
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_mix_ctrl.DEV_TAG = OLD.TAG_NAME SET 
            fes_mix_ctrl.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_mix_ctrl.DOT_NO), 
            fes_mix_ctrl.LOCATION_ID = fes_rtu_para.LOCATION_ID,
            fes_mix_ctrl.SUB_SYSTEM = fes_rtu_para.SUB_SYSTEM,
            fes_mix_ctrl.RTU_TAG = NEW.RTU_TAG,
            fes_mix_ctrl.DEV_TAG = NEW.TAG_NAME,
            fes_mix_ctrl.RTU_NO = fes_rtu_para.RTU_NO;
            
            UPDATE fes_data_block INNER JOIN fes_rtu_para 
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_data_block.DEV_TAG = OLD.TAG_NAME 
            SET 
            fes_data_block.LOCATION_ID = NEW.LOCATION_ID,
            fes_data_block.SUB_SYSTEM = NEW.SUB_SYSTEM,
            fes_data_block.DEV_TAG = NEW.TAG_NAME,
			fes_data_block.RTU_TAG = NEW.RTU_TAG,
            fes_data_block.TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(fes_data_block.TAG_NAME)),
            fes_data_block.RTU_NO = fes_rtu_para.RTU_NO;
            
            UPDATE fes_const INNER JOIN fes_rtu_para
            ON fes_rtu_para.TAG_NAME= NEW.RTU_TAG AND fes_const.DEV_TAG = OLD.TAG_NAME
            SET 
            fes_const.LOCATION_ID = NEW.LOCATION_ID,
            fes_const.SUB_SYSTEM = NEW.SUB_SYSTEM,
            fes_const.DEV_TAG = NEW.TAG_NAME,
            fes_const.RTU_TAG = NEW.RTU_TAG,
            fes_const.RTU_NO = fes_rtu_para.RTU_NO,
            fes_const.TAG_NAME = concat(NEW.RTU_TAG, '.' , fes_const.DOT_NO);
            
            
        END;
        END IF;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_dev_info', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_dev_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_DEV_INFO;
DELIMITER ;;
create trigger TD_FES_DEV_INFO BEFORE DELETE on fes_dev_info for each ROW 
BEGIN
DELETE FROM fes_accuml WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND  DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_analog WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND  DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_digital WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_mix WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_analog_ctrl WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_digital_ctrl WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_mix_ctrl WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_data_block WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
DELETE FROM fes_const WHERE TAG_NAME LIKE concat(OLD.RTU_TAG,'%') AND DEV_TAG = OLD.TAG_NAME;
END;;
DELIMITER ;

drop trigger if exists TDA_FES_DEV_INFO;
DELIMITER ;;
create trigger TDA_FES_DEV_INFO AFTER DELETE on fes_dev_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_dev_info', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_dev_info'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_dev_pt_temp_def;
CREATE TABLE `fes_dev_pt_temp_def` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '点标签',
  `POINT_TYPE` decimal(10,0) NOT NULL COMMENT '测点类型',
  `DEV_TP_NAME` varchar(64) NOT NULL COMMENT '设备名称',
  `SEQ_NO` decimal(10,0) NOT NULL COMMENT '设备点序号',
  `FUN_CODE` decimal(10,0) DEFAULT NULL COMMENT '功能号/定值代号',
  `INFO_NO` decimal(10,0) DEFAULT NULL COMMENT '信息号/定值组号',
  `RATIO` decimal(22,6) DEFAULT NULL COMMENT '变比系数',
  `REVISE` decimal(22,6) DEFAULT NULL COMMENT '修正值',
  `APPEND` decimal(22,6) DEFAULT NULL COMMENT '附件值/定值序号',
  `ASS_DIG` decimal(10,0) DEFAULT NULL COMMENT '关联遥信',
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL COMMENT '遥控类型',
  `ATTR` decimal(10,0) DEFAULT NULL COMMENT '点属性',
  `CONST_CFG_FILE` varchar(64) DEFAULT NULL COMMENT '定值描述文件',
  `COEFFICIENT` decimal(22,6) DEFAULT NULL COMMENT '装置系数',
  `SHORT_NAME` varchar(32) DEFAULT NULL COMMENT '点名缩写',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `YK_SELECT61850` varchar(64) DEFAULT NULL,
  `YK_EXE61850` varchar(64) DEFAULT NULL,
  `YK_CANCLE61850` varchar(64) DEFAULT NULL,
  `YK_DOUBLE_SELECT61850` varchar(64) DEFAULT NULL,
  `YK_DOUBLE_EXE61850` varchar(64) DEFAULT NULL,
  `YK_DOUBLE_CANCLE61850` varchar(64) DEFAULT NULL,
  `PULSE_TIME` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  `IS_FILTER` decimal(10,0) DEFAULT NULL,
  `PERCENT` decimal(10,0) DEFAULT NULL,
  `DEADBAND_TYPE` decimal(10,0) DEFAULT NULL,
  `DEADBAND` decimal(22,6) DEFAULT NULL,
  `ZEROBAND` decimal(22,6) DEFAULT NULL,
  `MAX_RANGE` decimal(22,6) DEFAULT NULL,
  `MIN_RANGE` decimal(22,6) DEFAULT NULL,
  `REVERS` decimal(10,0) DEFAULT NULL,
  `FILTER_ERR` decimal(10,0) DEFAULT NULL,
  `FILTER_DISTURB` decimal(10,0) DEFAULT NULL,
  `DISTURB_TIME` decimal(10,0) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`,`POINT_TYPE`),
  KEY `IN_FES_DEV_PT_TEMP_DEF` (`DEV_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置点模板';

drop trigger if exists TI_FES_DEV_PT_TEMP_DEF;
DELIMITER ;;
create trigger TI_FES_DEV_PT_TEMP_DEF AFTER INSERT on fes_dev_pt_temp_def for each ROW 
body:BEGIN
        
        /*
        IF NEW.POINT_TYPE = getFesPointType('FES_ACC') THEN 
            INSERT INTO fes_accuml(
            TAG_NAME,                                                                                              LOCATION_ID,             SUB_SYSTEM,               DOT_NO,                                                         RTU_TAG,              DEV_TAG,              SEQ_NO,    APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, BASE, COEFF,  RTU_NO,              RES_PARA_INT1, RES_PARA_INT2, RES_PARA_INT3, RES_PARA_INT4, DESCRIPTION,    FES_POINT_TP_NAME,PATH61850,POSITION61850) SELECT   
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ACC'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM ,getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ACC')),  fes_dev_info.RTU_TAG,fes_dev_info.TAG_NAME,NEW.SEQ_NO,'',              '',           '',             '0',   '1',   fes_rtu_para.RTU_NO ,NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4, NEW.DESCRIPTION ,NEW.TAG_NAME    ,NEW.PATH61850,NEW.POSITION61850                        
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        IF NEW.POINT_TYPE = getFesPointType('FES_ANA') THEN
            INSERT INTO fes_analog(
            TAG_NAME,                                                            LOCATION_ID,              SUB_SYSTEM,               DEV_TAG,              SEQ_NO,     RTU_TAG,             RTU_NO,                                                 DOT_NO,                                                         APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, IS_FILTER, PERCENT, DEADBAND_TYPE, DEADBAND, ZEROBAND, BASE, COEFF, MAX_RANGE, MIN_RANGE,  RES_PARA_INT1,     RES_PARA_INT2,    RES_PARA_INT3,      RES_PARA_INT4, DESCRIPTION,      FES_POINT_TP_NAME,    PATH61850,    POSITION61850) SELECT   
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ANA'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_dev_info.TAG_NAME,NEW.SEQ_NO, fes_dev_info.RTU_TAG,fes_rtu_para.RTU_NO ,  getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ANA')),'',           '',         '',                   0,         0,      '0',           '0',      '0',     '0',   '1',  65536,      0,          NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4, NEW.DESCRIPTION , NEW.TAG_NAME     ,NEW.PATH61850,NEW.POSITION61850                       
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF; 
        
        IF NEW.POINT_TYPE = getFesPointType('FES_DIG') THEN
            INSERT INTO fes_digital(
            TAG_NAME,                                                                                              LOCATION_ID,                    SUB_SYSTEM,                     DEV_TAG, SEQ_NO,     RTU_TAG,             DOT_NO,                                                           APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME, FILTER_ERR, FILTER_DISTURB, DISTURB_TIME,  REVERS,  RTU_NO,               RES_PARA_INT1, RES_PARA_INT2, RES_PARA_INT3, RES_PARA_INT4, DESCRIPTION,    FES_POINT_TP_NAME,PATH61850,POSITION61850) SELECT   
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_DIG'))),  fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_dev_info.TAG_NAME,NEW.SEQ_NO, fes_dev_info.RTU_TAG,getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_DIG')),   '',            '',           '',              0,          0,              0,             0,       fes_rtu_para.RTU_NO , NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4, NEW.DESCRIPTION , NEW.TAG_NAME   ,NEW.PATH61850,NEW.POSITION61850                          
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_MIX') THEN
            INSERT INTO fes_mix(
            TAG_NAME,                                                                                             DESCRIPTION,      LOCATION_ID,             SUB_SYSTEM,               RTU_TAG,              RTU_NO,                                               DOT_NO,                         DEV_TAG,               SEQ_NO,     APP_TABLE_NAME, APP_TAG_NAME, APP_COLUMN_NAME,    BASE, COEFF, MAX_RANGE, MIN_RANGE,  RES_PARA_INT1, RES_PARA_INT2, RES_PARA_INT3, RES_PARA_INT4, FES_POINT_TP_NAME,PATH61850,POSITION61850) SELECT   
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_MIX'))),  NEW.DESCRIPTION ,fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO, getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_MIX')), fes_dev_info.TAG_NAME, NEW.SEQ_NO, '',             '',           '',                 '0',  '1',   65536,      0,         NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4,  NEW.TAG_NAME    ,NEW.PATH61850,NEW.POSITION61850                         
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_ANA_CTRL') THEN
            INSERT INTO fes_analog_ctrl(
            TAG_NAME,                                                                                                DESCRIPTION,    LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,              RTU_NO,              DOT_NO,                                                                DEV_TAG,              SEQ_NO,     BASE, COEFF, MAX_RANGE, MIN_RANGE, POINT_TP_NAME,RES_PARA_INT1,RES_PARA_INT2,RES_PARA_INT3,RES_PARA_INT4,PATH61850,POSITION61850)SELECT
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ANA_CTRL'))),NEW.DESCRIPTION,fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO, getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_ANA_CTRL')),  fes_dev_info.TAG_NAME,NEW.SEQ_NO, '0',  '1',    65536,     0,        NEW.TAG_NAME ,NEW.RES_PARA_INT1,NEW.RES_PARA_INT2,NEW.RES_PARA_INT3,NEW.RES_PARA_INT4,NEW.PATH61850,NEW.POSITION61850
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_DIG_CTRL') THEN
            INSERT INTO fes_digital_ctrl(
            TAG_NAME,                                                                                                DESCRIPTION,                                                       LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,              RTU_NO,              DOT_NO,             DEV_TAG,              SEQ_NO,     REVERS, POINT_TP_NAME,RES_PARA_INT1,     RES_PARA_INT2,     RES_PARA_INT3,     RES_PARA_INT4,    PATH61850,    POSITION61850,    YK_SELECT61850,     YK_EXE61850,     YK_CANCLE61850,     YK_DOUBLE_SELECT61850,     YK_DOUBLE_EXE61850,     YK_DOUBLE_CANCLE61850,     PULSE_TIME)SELECT
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_DIG_CTRL'))),NEW.DESCRIPTION,fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO, getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_DIG_CTRL')), fes_dev_info.TAG_NAME,NEW.SEQ_NO,  0,      NEW.TAG_NAME, NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4,NEW.PATH61850,NEW.POSITION61850,NEW.YK_SELECT61850, NEW.YK_EXE61850, NEW.YK_CANCLE61850, NEW.YK_DOUBLE_SELECT61850, NEW.YK_DOUBLE_EXE61850, NEW.YK_DOUBLE_CANCLE61850, NEW.PULSE_TIME
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_MIX_CTRL') THEN
            INSERT INTO fes_mix_ctrl(
            TAG_NAME,                                                                                                DESCRIPTION,                                                       LOCATION_ID,              SUB_SYSTEM,               RTU_TAG,              RTU_NO,              DOT_NO,             DEV_TAG,              SEQ_NO,    BASE, COEFF, MIN_RANGE, MAX_RANGE, POINT_TP_NAME,RES_PARA_INT1, RES_PARA_INT2, RES_PARA_INT3, RES_PARA_INT4,PATH61850,POSITION61850)SELECT
            concat(fes_dev_info.RTU_TAG, '.' , getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_MIX_CTRL'))),NEW.DESCRIPTION,fes_rtu_para.LOCATION_ID ,fes_rtu_para.SUB_SYSTEM , fes_rtu_para.TAG_NAME,fes_rtu_para.RTU_NO, getMaxRtuDotNo(fes_dev_info.RTU_TAG,getFesPointType('FES_MIX_CTRL')),  fes_dev_info.TAG_NAME,NEW.SEQ_NO,'0',  '1',    0,       65536,      NEW.TAG_NAME, NEW.RES_PARA_INT1, NEW.RES_PARA_INT2, NEW.RES_PARA_INT3, NEW.RES_PARA_INT4,NEW.PATH61850,NEW.POSITION61850
            FROM fes_dev_info, fes_rtu_para WHERE fes_dev_info.DEV_TP_NAME=NEW.DEV_TP_NAME AND  fes_rtu_para.TAG_NAME=fes_dev_info.RTU_TAG; 
        END IF;
        */
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_dev_pt_temp_def', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_dev_pt_temp_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_DEV_PT_TEMP_DEF;
DELIMITER ;;
create trigger TU_FES_DEV_PT_TEMP_DEF AFTER UPDATE on fes_dev_pt_temp_def for each ROW 
BEGIN
        
        /*
        IF NEW.POINT_TYPE = getFesPointType('FES_ACC') THEN 
            UPDATE fes_accuml SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            FES_POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        IF NEW.POINT_TYPE = getFesPointType('FES_ANA') THEN
            UPDATE fes_analog SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            FES_POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
        END IF; 
        
        IF NEW.POINT_TYPE = getFesPointType('FES_DIG') THEN
            UPDATE fes_digital SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            FES_POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_MIX') THEN
            UPDATE fes_mix SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            FES_POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE FES_POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_ANA_CTRL') THEN
            UPDATE fes_analog_ctrl SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_DIG_CTRL') THEN
            UPDATE fes_digital_ctrl SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850,
            YK_SELECT61850 = NEW.YK_SELECT61850,
            YK_EXE61850 = NEW.YK_EXE61850,
            YK_CANCLE61850 = NEW.YK_CANCLE61850,
            YK_DOUBLE_SELECT61850 = NEW.YK_DOUBLE_SELECT61850,
            YK_DOUBLE_EXE61850 = NEW.YK_DOUBLE_EXE61850,
            YK_DOUBLE_CANCLE61850 = NEW.YK_DOUBLE_CANCLE61850,
            PULSE_TIME = NEW.PULSE_TIME
            WHERE POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        
        
        IF NEW.POINT_TYPE = getFesPointType('FES_MIX_CTRL') THEN
            UPDATE fes_mix_ctrl SET 
            SEQ_NO = NEW.SEQ_NO,
            DESCRIPTION = NEW.DESCRIPTION,
            POINT_TP_NAME= NEW.TAG_NAME,
            PATH61850 = NEW.PATH61850,
            POSITION61850 = NEW.POSITION61850
            WHERE POINT_TP_NAME = OLD.TAG_NAME;
        END IF;
        */
    #更新套件内容
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
    BEGIN
        IF NEW.POINT_TYPE = getFesPointType('FES_ACC') THEN
          UPDATE temp_plugin_point_map_acc SET FES_TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME) 
          WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF NEW.POINT_TYPE = getFesPointType('FES_ANA') THEN
           UPDATE temp_plugin_point_map_ana SET FES_TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME) 
           WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF  NEW.POINT_TYPE = getFesPointType('FES_MIX') THEN
           UPDATE temp_plugin_point_map_mix SET FES_TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME) 
           WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF  NEW.POINT_TYPE = getFesPointType('FES_DIG') THEN
           UPDATE temp_plugin_point_map_dig SET 
           FES_TEMP_POINT_NAME1 = IF(FES_TEMP_POINT_NAME1 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME1),
           FES_TEMP_POINT_NAME2 = IF(FES_TEMP_POINT_NAME2 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME2),
           FES_TEMP_POINT_NAME3 = IF(FES_TEMP_POINT_NAME3 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME3),
           FES_TEMP_POINT_NAME4 = IF(FES_TEMP_POINT_NAME4 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME4),
           FES_TEMP_POINT_NAME5 = IF(FES_TEMP_POINT_NAME5 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_POINT_NAME5)
           WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME;
        ELSEIF NEW.POINT_TYPE = getFesPointType('FES_ANA_CTRL') THEN
           UPDATE temp_plugin_point_map_ana SET FES_TEMP_OUT_NAME = getAfterLastDot(NEW.TAG_NAME)
           WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_OUT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF NEW.POINT_TYPE = getFesPointType('FES_MIX_CTRL') THEN
            UPDATE temp_plugin_point_map_mix SET FES_TEMP_OUT_NAME = getAfterLastDot(NEW.TAG_NAME)
            WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME AND FES_TEMP_OUT_NAME = getAfterLastDot(OLD.TAG_NAME);
        ELSEIF NEW.POINT_TYPE = getFesPointType('FES_DIG_CTRL') THEN
            UPDATE temp_plugin_point_map_dig SET
            FES_TEMP_OUT_NAME1 = IF(FES_TEMP_OUT_NAME1 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME1),
            FES_TEMP_OUT_NAME2 = IF(FES_TEMP_OUT_NAME2 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME2),
            FES_TEMP_OUT_NAME3 = IF(FES_TEMP_OUT_NAME3 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME3),
            FES_TEMP_OUT_NAME4 = IF(FES_TEMP_OUT_NAME4 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME4),
            FES_TEMP_OUT_NAME5 = IF(FES_TEMP_OUT_NAME5 = getAfterLastDot(OLD.TAG_NAME),getAfterLastDot(NEW.TAG_NAME),FES_TEMP_OUT_NAME5)
            WHERE FES_TEMP_NAME = OLD.DEV_TP_NAME;
         END IF;
    END; 
    END IF;         
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_dev_pt_temp_def', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_dev_pt_temp_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_DEV_PT_TEMP_DEF;
DELIMITER ;;
create trigger TD_FES_DEV_PT_TEMP_DEF BEFORE DELETE on fes_dev_pt_temp_def for each ROW 
BEGIN
        
        /*
        IF OLD.POINT_TYPE = getFesPointType('FES_ACC') THEN 
            DELETE FROM fes_accuml WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
        IF OLD.POINT_TYPE = getFesPointType('FES_ANA') THEN
            DELETE FROM fes_analog WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
        END IF; 
        
        IF OLD.POINT_TYPE = getFesPointType('FES_DIG') THEN
            DELETE FROM fes_digital WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
        
        IF OLD.POINT_TYPE = getFesPointType('FES_MIX') THEN
            DELETE FROM fes_mix WHERE FES_POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
        
        IF OLD.POINT_TYPE = getFesPointType('FES_ANA_CTRL') THEN
            DELETE FROM fes_analog_ctrl WHERE POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
         
        IF OLD.POINT_TYPE = getFesPointType('FES_DIG_CTRL') THEN
            DELETE FROM fes_digital_ctrl WHERE POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        
        
        IF OLD.POINT_TYPE = getFesPointType('FES_MIX_CTRL') THEN
            DELETE FROM fes_mix_ctrl WHERE POINT_TP_NAME =  OLD.TAG_NAME;
        END IF;
        */
END;;
DELIMITER ;

drop trigger if exists TDA_FES_DEV_PT_TEMP_DEF;
DELIMITER ;;
create trigger TDA_FES_DEV_PT_TEMP_DEF AFTER DELETE on fes_dev_pt_temp_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_dev_pt_temp_def', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_dev_pt_temp_def'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_dev_temp_def;
CREATE TABLE `fes_dev_temp_def` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '设备名称',
  `DEV_TYPE_ID` decimal(10,0) NOT NULL COMMENT '设备类型',
  `DEV_MODEL` varchar(64) DEFAULT NULL COMMENT '设备型号',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '设备描述',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '专业子系统',
  `PARA_STR1` varchar(64) DEFAULT NULL COMMENT '备用字段1',
  `PARA_STR2` varchar(256) DEFAULT NULL COMMENT '备用字段2',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备模板定义表';

drop trigger if exists TIA_FES_DEV_TEMP_DEF;
DELIMITER ;;
create trigger TIA_FES_DEV_TEMP_DEF AFTER INSERT on fes_dev_temp_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_dev_temp_def', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_dev_temp_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_DEV_TEMP_DEF;
DELIMITER ;;
create trigger TU_FES_DEV_TEMP_DEF AFTER UPDATE on fes_dev_temp_def for each ROW 
BEGIN
        
        UPDATE  fes_dev_pt_temp_def SET 
        TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(TAG_NAME)),
        DEV_TP_NAME = NEW.TAG_NAME
        WHERE DEV_TP_NAME = OLD.TAG_NAME;
        
        UPDATE fes_const_temp SET
        TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(TAG_NAME)),
        DEV_TP_NAME = NEW.TAG_NAME
        WHERE DEV_TP_NAME = OLD.TAG_NAME;
        
        UPDATE fes_data_block_temp SET
        TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(TAG_NAME)),
        DEV_TP_NAME = NEW.TAG_NAME
        WHERE DEV_TP_NAME = OLD.TAG_NAME;
        
        /*
        UPDATE fes_dev_info SET 
        DEV_TP_NAME = NEW.TAG_NAME,
        DEV_TYPE_ID = NEW.DEV_TYPE_ID
        WHERE DEV_TP_NAME = OLD.TAG_NAME;
        */
        
        #更新套件
        IF OLD.TAG_NAME != NEW.TAG_NAME THEN 
            BEGIN
                UPDATE temp_plugin_fes_dev_map SET FES_TEMP_NAME = NEW.TAG_NAME WHERE FES_TEMP_NAME = OLD.TAG_NAME;
            END;
        END IF;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_dev_temp_def', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_dev_temp_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_DEV_TEMP_DEF;
DELIMITER ;;
create trigger TD_FES_DEV_TEMP_DEF BEFORE DELETE on fes_dev_temp_def for each ROW 
BEGIN
        
        DELETE FROM fes_dev_pt_temp_def WHERE  DEV_TP_NAME = OLD.TAG_NAME;
        DELETE FROM fes_const_temp WHERE  DEV_TP_NAME = OLD.TAG_NAME;
        DELETE FROM fes_data_block_temp WHERE  DEV_TP_NAME = OLD.TAG_NAME;
        #套件的处理
        DELETE FROM temp_plugin_fes_dev_map WHERE FES_TEMP_NAME = OLD.TAG_NAME;
        /*
        DELETE FROM fes_dev_info WHERE  DEV_TP_NAME = OLD.TAG_NAME;
         */
        
END;;
DELIMITER ;

drop trigger if exists TDA_FES_DEV_TEMP_DEF;
DELIMITER ;;
create trigger TDA_FES_DEV_TEMP_DEF AFTER DELETE on fes_dev_temp_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_dev_temp_def', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_dev_temp_def'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_dev_type_def;
CREATE TABLE `fes_dev_type_def` (
  `DEV_TYPE_ID` decimal(10,0) NOT NULL COMMENT '设备类型ID',
  `DEV_TYPE_NAME` varchar(64) NOT NULL COMMENT '设备类型名称',
  `DEV_TYPE_DESC` varchar(64) DEFAULT NULL COMMENT '设备类型描述',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '备用参数1',
  `PARA_STR1` varchar(64) DEFAULT NULL COMMENT '备用参数2',
  PRIMARY KEY (`DEV_TYPE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置设备类型表';

drop trigger if exists TIA_FES_DEV_TYPE_DEF;
DELIMITER ;;
create trigger TIA_FES_DEV_TYPE_DEF AFTER INSERT on fes_dev_type_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_dev_type_def', 'dev_type_id', '', NEW.DEV_TYPE_ID, getTableSubSystem('fes_dev_type_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_DEV_TYPE_DEF;
DELIMITER ;;
create trigger TU_FES_DEV_TYPE_DEF BEFORE UPDATE on fes_dev_type_def for each ROW 
body:BEGIN
    IF NEW.DEV_TYPE_ID <> OLD.DEV_TYPE_ID THEN
    BEGIN
        UPDATE fes_dev_temp_def SET
        DEV_TYPE_ID = NEW.DEV_TYPE_ID
        WHERE DEV_TYPE_ID = OLD.DEV_TYPE_ID;
    END;
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_FES_DEV_TYPE_DEF;
DELIMITER ;;
create trigger TUA_FES_DEV_TYPE_DEF AFTER UPDATE on fes_dev_type_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_dev_type_def', 'dev_type_id', OLD.DEV_TYPE_ID, NEW.DEV_TYPE_ID, getTableSubSystem('fes_dev_type_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_FES_DEV_TYPE_DEF;
DELIMITER ;;
create trigger TDA_FES_DEV_TYPE_DEF AFTER DELETE on fes_dev_type_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_dev_type_def', 'dev_type_id', OLD.DEV_TYPE_ID, '', getTableSubSystem('fes_dev_type_def'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_digital;
CREATE TABLE `fes_digital` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属域',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属应用',
  `DEV_TAG` varchar(64) NOT NULL,
  `SEQ_NO` decimal(10,0) NOT NULL COMMENT '顺序号',
  `RTU_TAG` varchar(64) NOT NULL COMMENT 'RTU标签',
  `DOT_NO` decimal(10,0) NOT NULL COMMENT '点号',
  `DESCRIPTION` varchar(128) NOT NULL,
  `APP_TABLE_NAME` varchar(32) DEFAULT NULL COMMENT '后台应用表名',
  `APP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '后台标签名',
  `APP_COLUMN_NAME` varchar(16) DEFAULT NULL COMMENT '后台列名',
  `FILTER_ERR` decimal(10,0) DEFAULT NULL COMMENT '是否过滤错误DI',
  `FILTER_DISTURB` decimal(10,0) DEFAULT NULL COMMENT '是否过滤DI抖动',
  `DISTURB_TIME` decimal(10,0) DEFAULT NULL COMMENT '抖动时限',
  `REVERS` decimal(10,0) DEFAULT NULL COMMENT '极性/取反',
  `RTU_NO` decimal(10,0) NOT NULL,
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `FES_POINT_TP_NAME` varchar(64) DEFAULT NULL,
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  `ATTR` decimal(10,0) DEFAULT NULL COMMENT '点属性',
  `ASS_DIG` decimal(10,0) DEFAULT NULL COMMENT '关联遥信',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_DIGITAL` (`DEV_TAG`,`RTU_TAG`,`DOT_NO`,`FES_POINT_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置数字量参数表';

drop trigger if exists TI_FES_DIGITAL;
DELIMITER ;;
create trigger TI_FES_DIGITAL AFTER INSERT on fes_digital for each ROW 
BEGIN
    #首先禁止MAP_TO_FES触发器动作
    SET @disableDigitalMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesDigitalTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value1' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO1 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value2' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO2 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value3' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO3 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
         
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value4' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO4 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF; 
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value5' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO5 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET @disableDigitalMapToFesTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_digital', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_digital'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_DIGITAL;
DELIMITER ;;
create trigger TU_FES_DIGITAL BEFORE UPDATE on fes_digital for each ROW 
body:BEGIN
    IF OLD.APP_TABLE_NAME=NEW.APP_TABLE_NAME AND OLD.APP_TAG_NAME = NEW.APP_TAG_NAME AND OLD.APP_COLUMN_NAME = NEW.APP_COLUMN_NAME
    AND OLD.RTU_TAG = NEW.RTU_TAG AND OLD.DOT_NO = NEW.DOT_NO
    THEN LEAVE body;
    END IF;
    
    #首先禁止MAP_TO_FES触发器动作
    SET @disableDigitalMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesDigitalTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value1' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO1 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value2' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO2 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value3' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO3 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value4' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO4 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value5' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO5 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value1' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO1 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value2' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO2 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value3' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO3 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
         
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value4' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO4 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF; 
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')='value5' THEN
        BEGIN     
            UPDATE digital_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO5 = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET @disableDigitalMapToFesTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TUA_FES_DIGITAL;
DELIMITER ;;
create trigger TUA_FES_DIGITAL AFTER UPDATE on fes_digital for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_digital', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_digital'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_DIGITAL;
DELIMITER ;;
create trigger TD_FES_DIGITAL AFTER DELETE on fes_digital for each ROW 
BEGIN
    #首先禁止MAP_TO_FES触发器动作
    SET @disableDigitalMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesDigitalTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value1' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO1 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value2' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO2 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value3' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO3 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value4' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO4 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')='value5' THEN
            UPDATE digital_map_to_fes SET
            DOT_NO5 = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
    END;
    END IF;
    
    SET @disableDigitalMapToFesTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_digital', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_digital'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_digital_ctrl;
CREATE TABLE `fes_digital_ctrl` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `RTU_TAG` varchar(64) NOT NULL,
  `RTU_NO` decimal(10,0) NOT NULL,
  `DOT_NO` decimal(10,0) NOT NULL COMMENT 'DO点号1',
  `DEV_TAG` varchar(64) DEFAULT NULL,
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `REVERS` decimal(10,0) DEFAULT NULL,
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  `SEQ_NO` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `PATH61850` varchar(128) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `YK_SELECT61850` varchar(64) DEFAULT NULL,
  `YK_EXE61850` varchar(64) DEFAULT NULL,
  `YK_CANCLE61850` varchar(64) DEFAULT NULL,
  `YK_DOUBLE_SELECT61850` varchar(64) DEFAULT NULL,
  `YK_DOUBLE_EXE61850` varchar(64) DEFAULT NULL,
  `YK_DOUBLE_CANCLE61850` varchar(64) DEFAULT NULL,
  `PULSE_TIME` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  `ATTR` decimal(10,0) DEFAULT NULL,
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_DIGITAL_CTRL` (`RTU_TAG`,`DOT_NO`,`DEV_TAG`,`POINT_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='数字量输出参数表';

drop trigger if exists TIA_FES_DIGITAL_CTRL;
DELIMITER ;;
create trigger TIA_FES_DIGITAL_CTRL AFTER INSERT on fes_digital_ctrl for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_digital_ctrl', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_digital_ctrl'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_DIGITAL_CTRL;
DELIMITER ;;
create trigger TU_FES_DIGITAL_CTRL AFTER UPDATE on fes_digital_ctrl for each ROW 
body:BEGIN
    #更新关联了此点的遥控点
    
     IF NEW.RTU_TAG <> OLD.RTU_TAG OR NEW.DOT_NO<> OLD.DOT_NO
     THEN 
     BEGIN
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO1 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO1 = cast(OLD.DOT_NO AS char);
            
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO2 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO2 = cast(OLD.DOT_NO AS char);
            
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO3 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO3 = cast(OLD.DOT_NO AS char);
            
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO4 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO4 = cast(OLD.DOT_NO AS char);
            
            UPDATE digital_control SET
            RTU_TAG = NEW.RTU_TAG,
            OFFSET_NO5 = NEW.DOT_NO
            WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO5 = cast(OLD.DOT_NO AS char);
         
     END; 
     END IF;
    
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_digital_ctrl', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_digital_ctrl'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_DIGITAL_CTRL;
DELIMITER ;;
create trigger TD_FES_DIGITAL_CTRL AFTER DELETE on fes_digital_ctrl for each ROW 
BEGIN
    UPDATE digital_control SET
    OFFSET_NO1 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO1 = cast(OLD.DOT_NO AS char);
   
    UPDATE digital_control SET
    OFFSET_NO2 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO2 = cast(OLD.DOT_NO AS char);
    
    UPDATE digital_control SET
    OFFSET_NO3 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO3 = cast(OLD.DOT_NO AS char);
    
    UPDATE digital_control SET
    OFFSET_NO4 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO4 = cast(OLD.DOT_NO AS char);
    
    UPDATE digital_control SET
    OFFSET_NO5 = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO5 = cast(OLD.DOT_NO AS char);
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_digital_ctrl', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_digital_ctrl'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_mix;
CREATE TABLE `fes_mix` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `DEV_TAG` varchar(64) NOT NULL,
  `SEQ_NO` decimal(10,0) NOT NULL COMMENT '设备点序号',
  `RTU_TAG` varchar(64) NOT NULL COMMENT 'RTU标签',
  `RTU_NO` decimal(10,0) NOT NULL,
  `DOT_NO` decimal(10,0) NOT NULL COMMENT '点号/远动号',
  `DESCRIPTION` varchar(128) NOT NULL,
  `APP_TABLE_NAME` varchar(32) DEFAULT NULL COMMENT '后台应用表名',
  `APP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '后台标签名',
  `APP_COLUMN_NAME` varchar(16) DEFAULT NULL COMMENT '后台列名',
  `BASE` decimal(10,0) DEFAULT NULL COMMENT '基值',
  `COEFF` decimal(10,0) DEFAULT NULL COMMENT '系数',
  `MAX_RANGE` decimal(10,0) DEFAULT NULL COMMENT '最大量程',
  `MIN_RANGE` decimal(10,0) DEFAULT NULL COMMENT '最小量程',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `FES_POINT_TP_NAME` varchar(64) DEFAULT NULL,
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_MIX` (`DEV_TAG`,`RTU_TAG`,`DOT_NO`,`FES_POINT_TP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置混合量参数表';

drop trigger if exists TI_FES_MIX;
DELIMITER ;;
create trigger TI_FES_MIX AFTER INSERT on fes_mix for each ROW 
BEGIN
    #首先禁止MAP_TO_FES触发器动作
    SET @disableMixMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesMixTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE mix_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET @disableMixMapToFesTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_mix', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_mix'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_MIX;
DELIMITER ;;
create trigger TU_FES_MIX BEFORE UPDATE on fes_mix for each ROW 
body:BEGIN
    IF OLD.APP_TABLE_NAME=NEW.APP_TABLE_NAME AND OLD.APP_TAG_NAME = NEW.APP_TAG_NAME AND OLD.APP_COLUMN_NAME = NEW.APP_COLUMN_NAME
    AND OLD.RTU_TAG = NEW.RTU_TAG AND OLD.DOT_NO = NEW.DOT_NO
    THEN LEAVE body;
    END IF;
    
    #首先禁止MAP_TO_FES触发器动作
    SET @disableMixMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesMixTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE mix_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
        IF COALESCE(NEW.APP_TABLE_NAME,'')<>'' AND COALESCE(NEW.APP_TAG_NAME,'')<>'' AND COALESCE(NEW.APP_COLUMN_NAME,'')<>'' THEN
        BEGIN     
            UPDATE mix_map_to_fes SET
            RTU_TAG = NEW.RTU_TAG,
            DOT_NO = NEW.DOT_NO
            WHERE KEY_ID_TAG = concat(NEW.APP_TABLE_NAME,'.', NEW.APP_TAG_NAME);
        END;
        END IF;
        
    END;
    END IF;
    
    SET @disableMixMapToFesTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TUA_FES_MIX;
DELIMITER ;;
create trigger TUA_FES_MIX AFTER UPDATE on fes_mix for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_mix', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_mix'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_MIX;
DELIMITER ;;
create trigger TD_FES_MIX AFTER DELETE on fes_mix for each ROW 
BEGIN
    #首先禁止MAP_TO_FES触发器动作
    SET @disableMixMapToFesTrigger = 1;
    #然后询问自己是否已经被禁止
    IF COALESCE(@disableFesMixTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(OLD.APP_TABLE_NAME,'')<>'' AND COALESCE(OLD.APP_TAG_NAME,'')<>'' AND COALESCE(OLD.APP_COLUMN_NAME,'')<>'' THEN
            UPDATE mix_map_to_fes SET
            DOT_NO = '-1'
            WHERE KEY_ID_TAG = concat(OLD.APP_TABLE_NAME,'.', OLD.APP_TAG_NAME);
        END IF;
        
    END;
    END IF;
    
    SET @disableMixMapToFesTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_mix', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_mix'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_mix_ctrl;
CREATE TABLE `fes_mix_ctrl` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `RTU_TAG` varchar(64) NOT NULL COMMENT 'RTU标签',
  `RTU_NO` decimal(10,0) NOT NULL,
  `BASE` decimal(10,0) DEFAULT NULL COMMENT '备用',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `DOT_NO` decimal(10,0) NOT NULL COMMENT 'AO点号',
  `COEFF` decimal(10,0) DEFAULT NULL COMMENT '备用',
  `MIN_RANGE` decimal(10,0) DEFAULT NULL COMMENT '最小量程',
  `MAX_RANGE` decimal(10,0) DEFAULT NULL COMMENT '最大量程',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL,
  `SEQ_NO` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL,
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  `DEV_TAG` varchar(64) DEFAULT NULL,
  `PATH61850` varchar(64) DEFAULT NULL,
  `POSITION61850` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL,
  `RES_PARA_INT8` decimal(10,0) DEFAULT NULL,
  `RES_PARA_STR1` varchar(64) DEFAULT NULL,
  `RES_PARA_STR2` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_FES_MIX_CTRL` (`RTU_TAG`,`DOT_NO`,`POINT_TP_NAME`,`DEV_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='混合量输出参数表';

drop trigger if exists TIA_FES_MIX_CTRL;
DELIMITER ;;
create trigger TIA_FES_MIX_CTRL AFTER INSERT on fes_mix_ctrl for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_mix_ctrl', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_mix_ctrl'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_MIX_CTRL;
DELIMITER ;;
create trigger TU_FES_MIX_CTRL AFTER UPDATE on fes_mix_ctrl for each ROW 
body:BEGIN
    #更新关联了此点的遥控点
    
    IF NEW.RTU_TAG = OLD.RTU_TAG AND NEW.DOT_NO= OLD.DOT_NO
    THEN
    BEGIN
        UPDATE mix_control SET
        RTU_TAG = NEW.RTU_TAG,
        OFFSET_NO = NEW.DOT_NO
        WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO = cast(OLD.DOT_NO AS char);
    END; 
    END IF;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_mix_ctrl', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_mix_ctrl'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_MIX_CTRL;
DELIMITER ;;
create trigger TD_FES_MIX_CTRL AFTER DELETE on fes_mix_ctrl for each ROW 
BEGIN
    UPDATE mix_control SET
    OFFSET_NO = '-1'
    WHERE RTU_TAG = OLD.RTU_TAG AND OFFSET_NO = cast(OLD.DOT_NO AS char);
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_mix_ctrl', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_mix_ctrl'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_protocol;
CREATE TABLE `fes_protocol` (
  `PROTOCOL_ID` decimal(10,0) NOT NULL COMMENT '协议ID',
  `PROTOCOL_NAME` varchar(64) NOT NULL COMMENT '协议LIB名称',
  `PROTOCOL_DESC` varchar(128) DEFAULT NULL,
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '备用参数1',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '备用参数2',
  PRIMARY KEY (`PROTOCOL_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置通讯规约表';

drop trigger if exists TIA_FES_PROTOCOL;
DELIMITER ;;
create trigger TIA_FES_PROTOCOL AFTER INSERT on fes_protocol for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_protocol', 'protocol_id', '', NEW.PROTOCOL_ID, getTableSubSystem('fes_protocol'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_FES_PROTOCOL;
DELIMITER ;;
create trigger TU_FES_PROTOCOL BEFORE UPDATE on fes_protocol for each ROW 
body:BEGIN
    IF NEW.PROTOCOL_ID <> OLD.PROTOCOL_ID THEN
    BEGIN
        UPDATE fes_channel_para SET
        PROTO_TYPE = NEW.PROTOCOL_ID
        WHERE PROTO_TYPE = OLD.PROTOCOL_ID;
    END;
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_FES_PROTOCOL;
DELIMITER ;;
create trigger TUA_FES_PROTOCOL AFTER UPDATE on fes_protocol for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_protocol', 'protocol_id', OLD.PROTOCOL_ID, NEW.PROTOCOL_ID, getTableSubSystem('fes_protocol'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_FES_PROTOCOL;
DELIMITER ;;
create trigger TDA_FES_PROTOCOL AFTER DELETE on fes_protocol for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_protocol', 'protocol_id', OLD.PROTOCOL_ID, '', getTableSubSystem('fes_protocol'));
END IF;
END;;
DELIMITER ;

drop table if exists fes_rtu_para;
CREATE TABLE `fes_rtu_para` (
  `TAG_NAME` varchar(64) NOT NULL,
  `RTU_NAME` varchar(64) NOT NULL COMMENT 'RTU名称',
  `RTU_NO` decimal(10,0) NOT NULL,
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  `IS_USED` decimal(10,0) NOT NULL COMMENT '停运标志',
  `ALARM_ENABLE` decimal(10,0) NOT NULL COMMENT 'RTU告警使能',
  `REGION_ID` decimal(10,0) DEFAULT NULL COMMENT '责任区',
  `RTU_ADDR` decimal(10,0) NOT NULL COMMENT 'RTU站址',
  `CHAN_TAG` varchar(64) NOT NULL COMMENT '关联通道1-默认值班通道',
  `CHAN_NO` decimal(10,0) NOT NULL,
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '备用参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '备用参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '备用参数3',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '备用参数4',
  `RES_PARA_STR2` varchar(64) DEFAULT NULL COMMENT '备用参数5',
  `RES_PARA_STR3` varchar(256) DEFAULT NULL COMMENT '备用参数6',
  `RECV_FAIL_NUM` decimal(10,0) DEFAULT NULL,
  `DESCRIPTION` varchar(128) DEFAULT NULL,
  `TCP_KEEP_ALIVE` decimal(10,0) DEFAULT NULL,
  `SERVER61850` decimal(10,0) DEFAULT NULL,
  `CALL_TIME` decimal(10,0) DEFAULT NULL,
  `IS_USED_LB` decimal(10,0) DEFAULT NULL,
  `LB_DIR` varchar(128) DEFAULT NULL,
  `LB_CALL_TIME` decimal(10,0) DEFAULT NULL,
  `LB_DESC` varchar(64) DEFAULT NULL,
  `STATUS` decimal(10,0) DEFAULT NULL,
  `CONTROLDISABLE` decimal(10,0) DEFAULT NULL COMMENT '遥控禁止',
  `CLEARDATAENABLE` decimal(10,0) DEFAULT NULL COMMENT '设备离线数据清零',
  PRIMARY KEY (`TAG_NAME`),
  UNIQUE KEY `UN_FES_RTU_PARA` (`RTU_NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置RTU参数表';

drop trigger if exists TIB_FES_RTU_PARA;
DELIMITER ;;
create trigger TIB_FES_RTU_PARA BEFORE INSERT on fes_rtu_para for each ROW 
BEGIN
    
    DECLARE chNo decimal(10,0) DEFAULT 0;
    
    SET NEW.TAG_NAME = concat(NEW.CHAN_TAG,'.',NEW.RTU_NAME);
    
    SELECT CHAN_NO INTO chNo FROM fes_channel_para WHERE TAG_NAME = NEW.CHAN_TAG;
    
    SET NEW.CHAN_NO = chNo;
END;;
DELIMITER ;

drop trigger if exists TI_FES_RTU_PARA;
DELIMITER ;;
create trigger TI_FES_RTU_PARA AFTER INSERT on fes_rtu_para for each ROW 
BEGIN
  /*  
   * 不再产生默认设备
INSERT INTO fes_dev_info 
(TAG_NAME,                                 DEV_NAME,   LOCATION_ID,    SUB_SYSTEM,    DEV_ID,   DEV_TYPE_ID, RTU_TAG,    DEV_DESC,  RTU_NO,     DEV_TP_NAME) VALUES 
(concat(NEW.TAG_NAME, '.', 'defaultDev' ),'defaultDev',NEW.LOCATION_ID,NEW.SUB_SYSTEM,'-1',     '-1'       ,NEW.TAG_NAME,'默认设备',NEW.RTU_NO  ,'' );
*/
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'fes_rtu_para', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('fes_rtu_para'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUB_FES_RTU_PARA;
DELIMITER ;;
create trigger TUB_FES_RTU_PARA BEFORE UPDATE on fes_rtu_para for each ROW 
BEGIN
    
    DECLARE chNo decimal(10,0) DEFAULT 0;
    
    IF NEW.RTU_NAME <> OLD.RTU_NAME OR NEW.CHAN_TAG <> OLD.CHAN_TAG THEN 
        SET NEW.TAG_NAME = concat(NEW.CHAN_TAG,'.',NEW.RTU_NAME);
    END IF;
	
	    IF NEW.CHAN_TAG <> OLD.CHAN_TAG THEN 
    BEGIN
        SELECT CHAN_NO INTO chNo FROM fes_channel_para WHERE TAG_NAME = NEW.CHAN_TAG;
        SET NEW.CHAN_NO = chNo;
    END; 
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TU_FES_RTU_PARA;
DELIMITER ;;
create trigger TU_FES_RTU_PARA AFTER UPDATE on fes_rtu_para for each ROW 
BEGIN
    
IF NEW.TAG_NAME <> OLD.TAG_NAME OR NEW.RTU_NO <> OLD.RTU_NO OR NEW.DESCRIPTION <> OLD.DESCRIPTION
THEN
BEGIN 
    
    
    UPDATE fes_dev_info SET 
    TAG_NAME = concat(NEW.TAG_NAME, '.', getAfterLastDot(TAG_NAME)) ,
    DEV_DESC = DEV_DESC,
    RTU_TAG = NEW.TAG_NAME,
    RTU_NO = NEW.RTU_NO,
    LOCATION_ID = NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM
    WHERE RTU_TAG = OLD.TAG_NAME;
    
    UPDATE fes_data_block SET 
    RTU_NO = NEW.RTU_NO,
    LOCATION_ID = NEW.LOCATION_ID,
    SUB_SYSTEM = NEW.SUB_SYSTEM
    WHERE RTU_NO = OLD.RTU_NO;
    
    IF NEW.RTU_NO <> OLD.RTU_NO THEN
    BEGIN
        UPDATE dev_group SET
        RTU_NO = NEW.RTU_NO
        WHERE RTU_NO = OLD.RTU_NO;
    END;
    END IF;
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
    BEGIN
        #更新关联的后台测点
        UPDATE accuml SET
        RTU_TAG = NEW.TAG_NAME
        WHERE RTU_TAG = OLD.TAG_NAME;
        
        UPDATE analog SET
        RTU_TAG = NEW.TAG_NAME
        WHERE RTU_TAG = OLD.TAG_NAME;
        
        UPDATE digital SET
        RTU_TAG = NEW.TAG_NAME
        WHERE RTU_TAG = OLD.TAG_NAME;
        
        UPDATE mix SET
        RTU_TAG = NEW.TAG_NAME
        WHERE RTU_TAG = OLD.TAG_NAME;
        
        
    END;
    END IF;
    
END;
END IF;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'fes_rtu_para', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('fes_rtu_para'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_FES_RTU_PARA;
DELIMITER ;;
create trigger TD_FES_RTU_PARA BEFORE DELETE on fes_rtu_para for each ROW 
BEGIN
DELETE FROM fes_dev_info WHERE RTU_TAG = OLD.TAG_NAME;
DELETE FROM fes_data_block WHERE RTU_NO = OLD.RTU_NO;
#更新关联的后台测点
UPDATE accuml SET
RTU_TAG = ''
WHERE RTU_TAG = OLD.TAG_NAME;
UPDATE analog SET
RTU_TAG = ''
WHERE RTU_TAG = OLD.TAG_NAME;
UPDATE digital SET
RTU_TAG = ''
WHERE RTU_TAG = OLD.TAG_NAME;
UPDATE mix SET
RTU_TAG = ''
WHERE RTU_TAG = OLD.TAG_NAME;
END;;
DELIMITER ;

drop trigger if exists TDA_FES_RTU_PARA;
DELIMITER ;;
create trigger TDA_FES_RTU_PARA AFTER DELETE on fes_rtu_para for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'fes_rtu_para', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('fes_rtu_para'));
END IF;
END;;
DELIMITER ;

drop table if exists forward_accuml;
CREATE TABLE `forward_accuml` (
  `REMOTE_NO` decimal(10,0) DEFAULT NULL COMMENT '远动号',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `DP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '标签名',
  `FES_RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '前置RTU号',
  `FES_RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签名',
  `FES_DOT_NO` decimal(10,0) DEFAULT NULL COMMENT '前置点号',
  `DP_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '后台点号',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基值',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数',
  `PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '属性',
  `SRC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点来源',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '规约参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '规约参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '规约参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '规约参数4',
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL COMMENT '规约参数5',
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL COMMENT '规约参数6',
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL COMMENT '规约参数7',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '规约参数8',
  `RES_PARA_STR2` varchar(256) DEFAULT NULL COMMENT '规约参数9',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '转发RTU标签名',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '转发RTU号',
  `SRC_LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '源所属车站',
  `SRC_SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '源所属专业',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='遥脉转发表';

drop table if exists forward_analog;
CREATE TABLE `forward_analog` (
  `REMOTE_NO` decimal(10,0) DEFAULT NULL COMMENT '远动号',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `DP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '标签名',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基值',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数',
  `DEADBAND_TYPE` decimal(10,0) DEFAULT NULL COMMENT '死区类型',
  `DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '死区值',
  `FES_RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '前置RTU号',
  `FES_RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签名',
  `FES_DOT_NO` decimal(10,0) DEFAULT NULL COMMENT '前置点号',
  `DP_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '后台点号',
  `PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '属性',
  `SRC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点来源',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '规约参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '规约参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '规约参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '规约参数4',
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL COMMENT '规约参数5',
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL COMMENT '规约参数6',
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL COMMENT '规约参数7',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '规约参数8',
  `RES_PARA_STR2` varchar(256) DEFAULT NULL COMMENT '规约参数9',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '转发RTU标签名',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '转发RTU号',
  `SRC_LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '源所属车站',
  `SRC_SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '源所属专业',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='遥测转发表';

drop table if exists forward_analog_ctrl;
CREATE TABLE `forward_analog_ctrl` (
  `REMOTE_NO` decimal(10,0) DEFAULT NULL COMMENT '远动号',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基值',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数',
  `FES_RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '前置RTU号',
  `FES_RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签名',
  `FES_DOT_NO` decimal(10,0) DEFAULT NULL COMMENT '前置点号',
  `MAX_RANGE` decimal(22,6) DEFAULT NULL COMMENT '量程上限',
  `MIN_RANGE` decimal(22,6) DEFAULT NULL COMMENT '量程下限',
  `PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '属性',
  `SRC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点来源',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '规约参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '规约参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '规约参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '规约参数4',
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL COMMENT '规约参数5',
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL COMMENT '规约参数6',
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL COMMENT '规约参数7',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '规约参数8',
  `RES_PARA_STR2` varchar(256) DEFAULT NULL COMMENT '规约参数9',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '转发RTU标签名',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '转发RTU号',
  `SRC_LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '源所属车站',
  `SRC_SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '源所属专业',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置遥调转发表';

drop table if exists forward_digital_ctrl;
CREATE TABLE `forward_digital_ctrl` (
  `REMOTE_NO` decimal(10,0) DEFAULT NULL COMMENT '远动号',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `FES_RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '前置RTU号',
  `FES_RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签名',
  `OFFSET_NUM` decimal(10,0) DEFAULT NULL COMMENT '分量数',
  `FES_DOT_TAG_NAME1` varchar(48) DEFAULT NULL COMMENT '前置分量1',
  `FES_DOT_TAG_NAME2` varchar(48) DEFAULT NULL COMMENT '前置分量2',
  `FES_DOT_TAG_NAME3` varchar(48) DEFAULT NULL COMMENT '前置分量3',
  `FES_DOT_TAG_NAME4` varchar(48) DEFAULT NULL COMMENT '前置分量4',
  `FES_DOT_TAG_NAME5` varchar(48) DEFAULT NULL COMMENT '前置分量5',
  `PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '属性',
  `SRC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点来源',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '规约参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '规约参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '规约参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '规约参数4',
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL COMMENT '规约参数5',
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL COMMENT '规约参数6',
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL COMMENT '规约参数7',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '规约参数8',
  `RES_PARA_STR2` varchar(256) DEFAULT NULL COMMENT '规约参数9',
  `YK_NO` decimal(10,0) DEFAULT NULL COMMENT '遥控号',
  `ZF_YK_NO` decimal(10,0) DEFAULT NULL COMMENT '转发遥控号',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '转发RTU标签名',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '转发RTU号',
  `SRC_LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '源所属车站',
  `SRC_SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '源所属专业',
  `FES_DOT_NO1` decimal(10,0) DEFAULT NULL COMMENT '前置点号1',
  `FES_DOT_NO2` decimal(10,0) DEFAULT NULL COMMENT '前置点号2',
  `FES_DOT_NO3` decimal(10,0) DEFAULT NULL COMMENT '前置点号3',
  `FES_DOT_NO4` decimal(10,0) DEFAULT NULL COMMENT '前置点号4',
  `FES_DOT_NO5` decimal(10,0) DEFAULT NULL COMMENT '前置点号5',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='遥控转发表';

drop table if exists forward_digital_double;
CREATE TABLE `forward_digital_double` (
  `REMOTE_NO` decimal(10,0) DEFAULT NULL COMMENT '远动号',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `DP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '标签名',
  `FES_RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '前置RTU号',
  `FES_RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签名',
  `FES_DOT_NO` decimal(10,0) DEFAULT NULL COMMENT '前置点号',
  `DP_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '后台点号',
  `PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '属性',
  `SRC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点来源',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '规约参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '规约参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '规约参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '规约参数4',
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL COMMENT '规约参数5',
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL COMMENT '规约参数6',
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL COMMENT '规约参数7',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '规约参数8',
  `RES_PARA_STR2` varchar(256) DEFAULT NULL COMMENT '规约参数9',
  `YK_NO` decimal(10,0) DEFAULT NULL COMMENT '遥控号',
  `ZF_YK_NO` decimal(10,0) DEFAULT NULL COMMENT '转发遥控号',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '转发RTU标签名',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '转发RTU号',
  `SRC_LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '源所属车站',
  `SRC_SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '源所属专业',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='双点遥信转发表';

drop table if exists forward_digital_single;
CREATE TABLE `forward_digital_single` (
  `REMOTE_NO` decimal(10,0) DEFAULT NULL COMMENT '远动号',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `DP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '标签名',
  `FES_RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '前置RTU号',
  `FES_RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签名',
  `FES_DOT_NO` decimal(10,0) DEFAULT NULL COMMENT '前置点号',
  `DP_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '后台点号',
  `PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '属性',
  `SRC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点来源',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '规约参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '规约参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '规约参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '规约参数4',
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL COMMENT '规约参数5',
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL COMMENT '规约参数6',
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL COMMENT '规约参数7',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '规约参数8',
  `RES_PARA_STR2` varchar(256) DEFAULT NULL COMMENT '规约参数9',
  `YK_NO` decimal(10,0) DEFAULT NULL COMMENT '遥控号',
  `ZF_YK_NO` decimal(10,0) DEFAULT NULL COMMENT '转发遥控号',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '转发RTU标签名',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '转发RTU号',
  `SRC_LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '源所属车站',
  `SRC_SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '源所属专业',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='单点遥信转发表';

drop table if exists forward_mix;
CREATE TABLE `forward_mix` (
  `REMOTE_NO` decimal(10,0) DEFAULT NULL COMMENT '远动号',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `DP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '标签名',
  `FES_RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '前置RTU号',
  `FES_RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签名',
  `FES_DOT_NO` decimal(10,0) DEFAULT NULL COMMENT '前置点号',
  `DP_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '后台点号',
  `BASE` decimal(10,0) DEFAULT NULL COMMENT '基值',
  `COEFF` decimal(10,0) DEFAULT NULL COMMENT '系数',
  `PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '属性',
  `SRC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点来源',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '规约参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '规约参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '规约参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '规约参数4',
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL COMMENT '规约参数5',
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL COMMENT '规约参数6',
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL COMMENT '规约参数7',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '规约参数8',
  `RES_PARA_STR2` varchar(256) DEFAULT NULL COMMENT '规约参数9',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '转发RTU标签名',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '转发RTU号',
  `SRC_LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '源所属车站',
  `SRC_SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '源所属专业',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='混合量转发表';

drop table if exists forward_mix_ctrl;
CREATE TABLE `forward_mix_ctrl` (
  `REMOTE_NO` decimal(10,0) DEFAULT NULL COMMENT '远动号',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `FES_RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '前置RTU号',
  `FES_RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU标签名',
  `FES_DOT_NO` decimal(10,0) DEFAULT NULL COMMENT '前置点号',
  `DP_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '后台点号',
  `MAX_RANGE` decimal(10,0) DEFAULT NULL COMMENT '量程上限',
  `MIN_RANGE` decimal(10,0) DEFAULT NULL COMMENT '量程下限',
  `PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '属性',
  `SRC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点来源',
  `RES_PARA_INT1` decimal(10,0) DEFAULT NULL COMMENT '规约参数1',
  `RES_PARA_INT2` decimal(10,0) DEFAULT NULL COMMENT '规约参数2',
  `RES_PARA_INT3` decimal(10,0) DEFAULT NULL COMMENT '规约参数3',
  `RES_PARA_INT4` decimal(10,0) DEFAULT NULL COMMENT '规约参数4',
  `RES_PARA_INT5` decimal(10,0) DEFAULT NULL COMMENT '规约参数5',
  `RES_PARA_INT6` decimal(10,0) DEFAULT NULL COMMENT '规约参数6',
  `RES_PARA_INT7` decimal(10,0) DEFAULT NULL COMMENT '规约参数7',
  `RES_PARA_STR1` varchar(64) DEFAULT NULL COMMENT '规约参数8',
  `RES_PARA_STR2` varchar(256) DEFAULT NULL COMMENT '规约参数9',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '转发RTU标签名',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '转发RTU号',
  `SRC_LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '源所属车站',
  `SRC_SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '源所属专业',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='混合量控制转发表';

drop table if exists linkage_action;
CREATE TABLE `linkage_action` (
  `NAME` varchar(128) NOT NULL COMMENT '动作名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '动作描述',
  `LINKAGE_STRING` varchar(2048) DEFAULT NULL COMMENT '动作解析串',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `ACTION_TYPE` decimal(10,0) DEFAULT NULL COMMENT '动作类型',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属子系统',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '控制点所属车站',
  `TIMEFLAG` decimal(20,0) DEFAULT NULL COMMENT '时标',
  PRIMARY KEY (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='联动动作';

drop trigger if exists TI_LINKAGE_ACTION;
DELIMITER ;;
create trigger TI_LINKAGE_ACTION BEFORE INSERT on linkage_action for each ROW 
body:BEGIN
    #仅仅修改时标
    
    SET NEW.TIMEFLAG = unix_timestamp(now());
    
END;;
DELIMITER ;

drop trigger if exists TIA_LINKAGE_ACTION;
DELIMITER ;;
create trigger TIA_LINKAGE_ACTION AFTER INSERT on linkage_action for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'linkage_action', 'name', '', NEW.NAME, getTableSubSystem('linkage_action'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_LINKAGE_ACTION;
DELIMITER ;;
create trigger TU_LINKAGE_ACTION BEFORE UPDATE on linkage_action for each ROW 
body:BEGIN
    #修改对应功能
    SET NEW.TIMEFLAG = unix_timestamp(now());
    UPDATE linkage_function SET 
    TIMEFLAG = NEW.TIMEFLAG,
    ACTION_NAME = NEW.NAME
    WHERE ACTION_NAME = OLD.NAME;
END;;
DELIMITER ;

drop trigger if exists TUA_LINKAGE_ACTION;
DELIMITER ;;
create trigger TUA_LINKAGE_ACTION AFTER UPDATE on linkage_action for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'linkage_action', 'name', OLD.NAME, NEW.NAME, getTableSubSystem('linkage_action'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_LINKAGE_ACTION;
DELIMITER ;;
create trigger TD_LINKAGE_ACTION AFTER DELETE on linkage_action for each ROW 
body:BEGIN
    #删除关联了此动作的功能
    DELETE FROM linkage_function WHERE ACTION_NAME = OLD.NAME; 
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'linkage_action', 'name', OLD.NAME, '', getTableSubSystem('linkage_action'));
END IF;
END;;
DELIMITER ;

drop table if exists linkage_define;
CREATE TABLE `linkage_define` (
  `LINKAGE_NAME` varchar(128) NOT NULL COMMENT '联动名称',
  `FUNC_NAME` varchar(128) NOT NULL COMMENT '功能名',
  `FUNC_NO` decimal(10,0) DEFAULT NULL COMMENT '功能编号',
  `RELATION` decimal(10,0) DEFAULT NULL COMMENT '串/并',
  `TIMEFLAG` decimal(20,0) DEFAULT NULL COMMENT '时标',
  PRIMARY KEY (`LINKAGE_NAME`,`FUNC_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='联动定义';

drop trigger if exists TI_LINKAGE_DEFINE;
DELIMITER ;;
create trigger TI_LINKAGE_DEFINE BEFORE INSERT on linkage_define for each ROW 
body:BEGIN
    #仅仅修改时标
    #避免触发器循环
    SET @disableLinkageSettingTrigger = 1;
        
    IF COALESCE(@disableLinkageDefineTrigger,0) = 0 THEN
    BEGIN
        SET NEW.TIMEFLAG = unix_timestamp(now());
        UPDATE linkage_setting SET 
        TIMEFLAG = NEW.TIMEFLAG
        WHERE NAME = NEW.LINKAGE_NAME;
    END;
    END IF;
    
    SET @disableLinkageSettingTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TIA_LINKAGE_DEFINE;
DELIMITER ;;
create trigger TIA_LINKAGE_DEFINE AFTER INSERT on linkage_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'linkage_define', 'linkage_name,func_name', '', concat(NEW.LINKAGE_NAME,',',NEW.FUNC_NAME), getTableSubSystem('linkage_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_LINKAGE_DEFINE;
DELIMITER ;;
create trigger TU_LINKAGE_DEFINE BEFORE UPDATE on linkage_define for each ROW 
body:BEGIN
    #修改对应联动功能关系表
    #避免触发器循环
    SET @disableLinkageSettingTrigger = 1;
        
    IF COALESCE(@disableLinkageDefineTrigger,0) = 0 THEN
    BEGIN
        SET NEW.TIMEFLAG = unix_timestamp(now());
        UPDATE linkage_setting SET 
        TIMEFLAG = NEW.TIMEFLAG
        WHERE NAME = OLD.LINKAGE_NAME;
    END;
    END IF;
    
    SET @disableLinkageSettingTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TUA_LINKAGE_DEFINE;
DELIMITER ;;
create trigger TUA_LINKAGE_DEFINE AFTER UPDATE on linkage_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'linkage_define', 'linkage_name,func_name', concat(OLD.LINKAGE_NAME,',',OLD.FUNC_NAME), concat(NEW.LINKAGE_NAME,',',NEW.FUNC_NAME), getTableSubSystem('linkage_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_LINKAGE_DEFINE;
DELIMITER ;;
create trigger TD_LINKAGE_DEFINE AFTER DELETE on linkage_define for each ROW 
body:BEGIN
    #仅仅修改时标
    #避免触发器循环
    SET @disableLinkageSettingTrigger = 1;
        
    IF COALESCE(@disableLinkageDefineTrigger,0) = 0 THEN
    BEGIN
        UPDATE linkage_setting SET 
        TIMEFLAG = unix_timestamp(now())
        WHERE NAME = OLD.LINKAGE_NAME;
    END;
    END IF;
    
    SET @disableLinkageSettingTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'linkage_define', 'linkage_name,func_name', concat(OLD.LINKAGE_NAME,',',OLD.FUNC_NAME), '', getTableSubSystem('linkage_define'));
END IF;
END;;
DELIMITER ;

drop table if exists linkage_function;
CREATE TABLE `linkage_function` (
  `NAME` varchar(128) NOT NULL COMMENT '功能名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '功能描述',
  `ACTION_NO` decimal(10,0) DEFAULT NULL COMMENT '动作编号',
  `ACTION_NAME` varchar(128) NOT NULL COMMENT '动作名称',
  `ENABLE` decimal(10,0) DEFAULT NULL COMMENT '是否可用',
  `DELAYTIME` decimal(10,0) DEFAULT NULL COMMENT '延时时间',
  `CONDITION_TYPE` decimal(10,0) DEFAULT '0' COMMENT '执行条件',
  `CONDITION_INFO` varchar(256) DEFAULT NULL COMMENT '条件信息',
  `FAILSTRAGE` decimal(10,0) DEFAULT NULL COMMENT '失败处理',
  `RETRYTIMES` decimal(10,0) DEFAULT NULL COMMENT '重试次数',
  `TIMEFLAG` decimal(20,0) DEFAULT NULL COMMENT '时标',
  `FUNC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '功能类型',
  PRIMARY KEY (`NAME`,`ACTION_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='联动功能表';

drop trigger if exists TI_LINKAGE_FUNCTION;
DELIMITER ;;
create trigger TI_LINKAGE_FUNCTION BEFORE INSERT on linkage_function for each ROW 
body:BEGIN
    #仅仅修改时标
    SET NEW.TIMEFLAG = unix_timestamp(now());
    
END;;
DELIMITER ;

drop trigger if exists TIA_LINKAGE_FUNCTION;
DELIMITER ;;
create trigger TIA_LINKAGE_FUNCTION AFTER INSERT on linkage_function for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'linkage_function', 'name,action_name', '', concat(NEW.NAME,',',NEW.ACTION_NAME), getTableSubSystem('linkage_function'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_LINKAGE_FUNCTION;
DELIMITER ;;
create trigger TU_LINKAGE_FUNCTION BEFORE UPDATE on linkage_function for each ROW 
body:BEGIN
    #修改对应联动功能关系表
    SET NEW.TIMEFLAG = unix_timestamp(now());
    UPDATE linkage_define SET 
    TIMEFLAG = NEW.TIMEFLAG,
    FUNC_NAME = NEW.NAME
    WHERE FUNC_NAME = OLD.NAME;
END;;
DELIMITER ;

drop trigger if exists TUA_LINKAGE_FUNCTION;
DELIMITER ;;
create trigger TUA_LINKAGE_FUNCTION AFTER UPDATE on linkage_function for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'linkage_function', 'name,action_name', concat(OLD.NAME,',',OLD.ACTION_NAME), concat(NEW.NAME,',',NEW.ACTION_NAME), getTableSubSystem('linkage_function'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_LINKAGE_FUNCTION;
DELIMITER ;;
create trigger TD_LINKAGE_FUNCTION AFTER DELETE on linkage_function for each ROW 
body:BEGIN
    #删除关联了此功能的联动定义
    #检查功能是否被删除
    IF NOT EXISTS (SELECT * FROM linkage_function WHERE NAME = OLD.NAME) THEN 
    BEGIN
        DELETE FROM linkage_define WHERE FUNC_NAME = OLD.NAME; 
    END;
    ELSE 
    BEGIN
        #修改时标
        UPDATE linkage_define SET
        TIMEFLAG = unix_timestamp(now())
        WHERE FUNC_NAME = OLD.NAME;
    END; 
    END IF;
    
    
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'linkage_function', 'name,action_name', concat(OLD.NAME,',',OLD.ACTION_NAME), '', getTableSubSystem('linkage_function'));
END IF;
END;;
DELIMITER ;

drop table if exists linkage_setting;
CREATE TABLE `linkage_setting` (
  `NAME` varchar(128) NOT NULL COMMENT '联动名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `TRIGGER_TYPE` decimal(10,0) DEFAULT NULL COMMENT '触发方式',
  `EXEC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '执行方式',
  `LINK_TYPE` decimal(10,0) DEFAULT NULL COMMENT '联动类型',
  `TRIGGER_CONDITION` varchar(64) DEFAULT NULL COMMENT '触发条件',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '车站ID',
  `TIMEFLAG` decimal(20,0) DEFAULT NULL COMMENT '时标',
  `MUTEX_LIST` varchar(1280) DEFAULT NULL COMMENT '联动互斥列表',
  PRIMARY KEY (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='联动配置';

drop trigger if exists TI_LINKAGE_SETTING;
DELIMITER ;;
create trigger TI_LINKAGE_SETTING BEFORE INSERT on linkage_setting for each ROW 
body:BEGIN
    #仅仅修改时标
    #避免触发器循环
    SET @disableLinkageDefineTrigger = 1;
        
    IF COALESCE(@disableLinkageSettingTrigger,0) = 0 THEN
    BEGIN
        SET NEW.TIMEFLAG = unix_timestamp(now());
    END;
    END IF;
    
    SET @disableLinkageDefineTrigger = 0;
    
    
END;;
DELIMITER ;

drop trigger if exists TIA_LINKAGE_SETTING;
DELIMITER ;;
create trigger TIA_LINKAGE_SETTING AFTER INSERT on linkage_setting for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'linkage_setting', 'name', '', NEW.NAME, getTableSubSystem('linkage_setting'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_LINKAGE_SETTING;
DELIMITER ;;
create trigger TU_LINKAGE_SETTING BEFORE UPDATE on linkage_setting for each ROW 
body:BEGIN
    #修改对应联动功能关系表
    #避免触发器循环
    SET @disableLinkageDefineTrigger = 1;
        
    IF COALESCE(@disableLinkageSettingTrigger,0) = 0 THEN
    BEGIN
        SET NEW.TIMEFLAG = unix_timestamp(now());
        IF NEW.NAME<>OLD.NAME THEN 
            UPDATE linkage_define SET 
            LINKAGE_NAME = NEW.NAME
            WHERE LINKAGE_NAME = OLD.NAME;
       END IF;
    END;
    END IF;
    
    SET @disableLinkageDefineTrigger = 0;
    
   
END;;
DELIMITER ;

drop trigger if exists TUA_LINKAGE_SETTING;
DELIMITER ;;
create trigger TUA_LINKAGE_SETTING AFTER UPDATE on linkage_setting for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'linkage_setting', 'name', OLD.NAME, NEW.NAME, getTableSubSystem('linkage_setting'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_LINKAGE_SETTING;
DELIMITER ;;
create trigger TD_LINKAGE_SETTING AFTER DELETE on linkage_setting for each ROW 
body:BEGIN
    #删除关联了此联动的功能关联
    #避免触发器循环
    SET @disableLinkageDefineTrigger = 1;
        
    IF COALESCE(@disableLinkageSettingTrigger,0) = 0 THEN
    BEGIN
        DELETE FROM linkage_define WHERE LINKAGE_NAME = OLD.NAME;
    END;
    END IF;
    
    SET @disableLinkageDefineTrigger = 0;
     
   
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'linkage_setting', 'name', OLD.NAME, '', getTableSubSystem('linkage_setting'));
END IF;
END;;
DELIMITER ;

drop table if exists load_stat_define;
CREATE TABLE `load_stat_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `IS_ENABLED` decimal(10,0) NOT NULL DEFAULT '0' COMMENT '是否启用，0为不启用，1为启用',
  `TYPE` decimal(10,0) NOT NULL DEFAULT '1' COMMENT '设备类型，1为线路，2为馈线，3为变压器，4为配变',
  `LEVEL` decimal(10,0) NOT NULL DEFAULT '0' COMMENT '负荷等级 0为非一级，1为1级',
  `V_BASE` decimal(22,6) DEFAULT NULL COMMENT '电压基准值',
  `A_BASE` decimal(22,6) DEFAULT NULL COMMENT '电流基准值',
  `V_TAG` varchar(64) DEFAULT NULL COMMENT '电压遥测点标签',
  `A_TAG` varchar(64) DEFAULT NULL COMMENT '电流遥测点标签',
  `LOAD_TAG` varchar(64) DEFAULT NULL COMMENT '负载遥测点标签',
  `AVAIL_RATIO_TAG` varchar(64) DEFAULT NULL COMMENT '供电可靠率遥信点标签',
  `FAIL_RATIO_TAG` varchar(64) DEFAULT NULL COMMENT '线路故障率遥信点标签',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='负载统计定义表';

drop table if exists mix_control;
CREATE TABLE `mix_control` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `CTRL_ACT_NAME` varchar(64) DEFAULT NULL,
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL,
  `IS_TAGT_STATE` decimal(10,0) DEFAULT NULL COMMENT '是否等待目标状态',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `RESV_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制保留时间',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基数',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数',
  `CTRL_TOLERANCE` decimal(22,6) DEFAULT NULL COMMENT '控制目标误差-预留',
  `RANGE_UP` decimal(10,0) DEFAULT NULL COMMENT '最大量程 ',
  `RANGE_LOW` decimal(10,0) DEFAULT NULL COMMENT '最小量程 ',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT 'RTU标签',
  `OFFSET_NO` varchar(48) DEFAULT NULL COMMENT 'DAO点号',
  `CTRL_ALIAS` varchar(128) DEFAULT NULL COMMENT '遥控别名',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_MIX_CONTROL` (`RTU_TAG`,`OFFSET_NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='混合量输出参数表';

drop trigger if exists TIA_MIX_CONTROL;
DELIMITER ;;
create trigger TIA_MIX_CONTROL AFTER INSERT on mix_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'mix_control', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('mix_control'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_MIX_CONTROL;
DELIMITER ;;
create trigger TU_MIX_CONTROL BEFORE UPDATE on mix_control for each ROW 
body:BEGIN
    
    #修改功能名称
    IF COALESCE(NEW.RTU_TAG,'') = '' THEN
    BEGIN
        SET NEW.OFFSET_NO = '-1';
    END;
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_MIX_CONTROL;
DELIMITER ;;
create trigger TUA_MIX_CONTROL AFTER UPDATE on mix_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'mix_control', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('mix_control'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_MIX_CONTROL;
DELIMITER ;;
create trigger TDA_MIX_CONTROL AFTER DELETE on mix_control for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'mix_control', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('mix_control'));
END IF;
END;;
DELIMITER ;

drop table if exists mix_map_to_fes;
CREATE TABLE `mix_map_to_fes` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '标签点名（表名.测点标签）',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名称',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '前置RTU',
  `DOT_NO` varchar(48) DEFAULT NULL COMMENT '前置序号',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基值',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数-预留',
  `RANGE_UP` decimal(10,0) DEFAULT NULL COMMENT '最大量程',
  `RANGE_LOW` decimal(10,0) DEFAULT NULL COMMENT '最小量程',
  `IS_FILTER_ERROR` decimal(10,0) DEFAULT NULL COMMENT '是否过滤突变',
  `IS_FILTER_DISTURB` decimal(10,0) DEFAULT NULL COMMENT '是否过滤抖动',
  `DISTURB_TIME` decimal(10,0) DEFAULT NULL COMMENT '抖动时间限制',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模拟量前置映射表';

drop trigger if exists TI_MIX_MAP_TO_FES;
DELIMITER ;;
create trigger TI_MIX_MAP_TO_FES AFTER INSERT on mix_map_to_fes for each ROW 
BEGIN
        
    SET @disableFesMixTrigger = 1;
    
    IF COALESCE(@disableMixMapToFesTrigger,0) = 0 THEN
    BEGIN
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN
                UPDATE fes_mix SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME = concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    SET @disableFesMixTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'mix_map_to_fes', 'key_id_tag', '', NEW.KEY_ID_TAG, getTableSubSystem('mix_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_MIX_MAP_TO_FES;
DELIMITER ;;
create trigger TU_MIX_MAP_TO_FES BEFORE UPDATE on mix_map_to_fes for each ROW 
body_label:BEGIN
    
    IF NEW.RTU_TAG = OLD.RTU_TAG AND 
        NEW.DOT_NO = OLD.DOT_NO AND 
        NEW.KEY_ID_TAG = OLD.KEY_ID_TAG
        THEN LEAVE body_label;
    END IF;
    
    
    SET @disableFesMixTrigger = 1;
    
    IF COALESCE(@disableMixMapToFesTrigger,0) = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN
                UPDATE fes_mix SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME=concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
        
        
        IF COALESCE(NEW.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(NEW.DOT_NO,'') <> '' THEN
                UPDATE fes_mix SET 
                APP_TABLE_NAME=getBeforeFirstDot(NEW.KEY_ID_TAG), 
                APP_TAG_NAME=getAfterFirstDot(NEW.KEY_ID_TAG),
                APP_COLUMN_NAME='value' 
                WHERE TAG_NAME=concat(NEW.RTU_TAG,'.',NEW.DOT_NO);
            END IF;
            
        END;
        ELSE SET NEW.DOT_NO = '-1';
        END IF;
    END;
    END IF;
    
    SET @disableFesMixTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TUA_MIX_MAP_TO_FES;
DELIMITER ;;
create trigger TUA_MIX_MAP_TO_FES AFTER UPDATE on mix_map_to_fes for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'mix_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, NEW.KEY_ID_TAG, getTableSubSystem('mix_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_MIX_MAP_TO_FES;
DELIMITER ;;
create trigger TD_MIX_MAP_TO_FES BEFORE DELETE on mix_map_to_fes for each ROW 
BEGIN
        
    SET @disableFesMixTrigger = 1;
    
    IF COALESCE(@disableMixMapToFesTrigger,0) = 0 THEN
    BEGIN
        
        IF COALESCE(OLD.RTU_TAG,'') <> '' THEN
        BEGIN 
            IF COALESCE(OLD.DOT_NO,'') <> '' THEN
                UPDATE fes_mix SET 
                APP_TABLE_NAME='', 
                APP_TAG_NAME='',
                APP_COLUMN_NAME='' 
                WHERE TAG_NAME = concat(OLD.RTU_TAG,'.',OLD.DOT_NO);
            END IF;
            
        END;
        END IF;
    END;
    END IF;
    
    SET @disableFesMixTrigger = 0;
    
END;;
DELIMITER ;

drop trigger if exists TDA_MIX_MAP_TO_FES;
DELIMITER ;;
create trigger TDA_MIX_MAP_TO_FES AFTER DELETE on mix_map_to_fes for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'mix_map_to_fes', 'key_id_tag', OLD.KEY_ID_TAG, '', getTableSubSystem('mix_map_to_fes'));
END IF;
END;;
DELIMITER ;

drop table if exists mix_temp_define;
CREATE TABLE `mix_temp_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '点标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `DEV_TP_NAME` varchar(64) NOT NULL COMMENT '设备模板名称',
  `SEQ_NO` decimal(10,0) NOT NULL COMMENT '点序号',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类型',
  `POINT_CLASS` decimal(10,0) DEFAULT NULL COMMENT '点类别 ',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点属性（采集点/计算点/虚拟点...）',
  `POINT_SORT` decimal(10,0) DEFAULT NULL COMMENT ' 测点种类  ',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否采样',
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL COMMENT '是否统计',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL,
  `STATE_TEXT_NAME` varchar(64) DEFAULT NULL COMMENT 'RETURN_PERCENT',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '告警优先级',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延时时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回自动确认',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认后自动删除',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  `IS_FILTER_ERROR` decimal(10,0) DEFAULT NULL COMMENT '是否过滤MI错误',
  `IS_FILTER_DISTURB` decimal(10,0) DEFAULT NULL COMMENT '是否过滤抖动',
  `DISTURB_TIME` decimal(10,0) DEFAULT NULL COMMENT '抖动时限',
  `IS_CONTROL` decimal(10,0) DEFAULT NULL COMMENT '是否控制',
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL,
  `IS_TAGT_STATE` decimal(10,0) DEFAULT NULL COMMENT '是否等待目标状态',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `RESV_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制保留时间',
  `BASE` decimal(22,6) DEFAULT NULL COMMENT '基数-AO表',
  `COEFF` decimal(22,6) DEFAULT NULL COMMENT '系数-AO表',
  `RANGE_UP` decimal(10,0) DEFAULT NULL COMMENT '量程最大值 ',
  `RANGE_LOW` decimal(10,0) DEFAULT NULL COMMENT '量程最小值 ',
  `CTRL_TOLERANCE` decimal(22,6) DEFAULT NULL COMMENT '控制目标误差',
  `CTRL_ACT_NAME` varchar(64) DEFAULT NULL COMMENT '控制动作组名',
  `SEC_DEV_IN_GRP` decimal(10,0) DEFAULT NULL COMMENT '二次设备在设备组内序号',
  `SEC_DEVTP_NAME` varchar(64) DEFAULT NULL COMMENT '二次设备模板',
  `SEC_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '输入点号',
  `OUT_SEC_SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '输出点号',
  `KEY_VALUE` decimal(10,0) DEFAULT NULL COMMENT '关键数据标志',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='混合量模板定义表';

drop trigger if exists TI_MIX_TEMP_DEFINE;
DELIMITER ;;
create trigger TI_MIX_TEMP_DEFINE AFTER INSERT on mix_temp_define for each ROW 
BEGIN
   /*
    INSERT INTO mix(
    TAG_NAME,                                             DESCRIPTION,    DEVICE,  SEQ_NO,    LOCATION_ID, SUB_SYSTEM, RTU_TAG, IS_CONTROL,     STATE_TEXT_NAME,    GRAY, CONTRAST, QCOLOR, RAW_VALUE, VALUE, STATUS, LAST_UPDATE_TIME, LAST_CHANGE_TIME, METHOD, ALARM_PRIORITY,    PIC_NAME, SOUND_NAME1, SOUND_NAME2, SOUND_NAME3, ALARM_DELAY_TIME,    IS_WATER_ALM,    IS_ACK_ON_RTN,    DEL_ACT_ON_ACK,    IS_NEVER_ALM_ON_RTN,    REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TYPE, POINT_TP_NAME, IS_SAMPLE,    IS_STATISTICS,    SAMPLE_PERIOD,    POINT_TYPE,    POINT_CLASS,    POINT_PROPERTY,    POINT_SORT, CAMERA_TAG, PRESET_ID) SELECT   
    concat(TAG_NAME, '.' , getAfterLastDot(NEW.TAG_NAME)),NEW.DESCRIPTION,TAG_NAME,NEW.SEQ_NO,LOCATION_ID, SUB_SYSTEM, '',      NEW.IS_CONTROL, NEW.STATE_TEXT_NAME,GRAY, CONTRAST, QCOLOR, '0',       VALUE, STATUS, '0',              '0',             '',      NEW.ALARM_PRIORITY, '',       '',         '',          '',          NEW.ALARM_DELAY_TIME,NEW.IS_WATER_ALM,NEW.IS_ACK_ON_RTN,NEW.DEL_ACT_ON_ACK,NEW.IS_NEVER_ALM_ON_RTN,REGION_ID, OPT_HANDOVER_GROUP, IS_SUMMARY, SUMMARY_VALUE, SUMMARY_STATUS, DEV_TYPE, NEW.TAG_NAME,  NEW.IS_SAMPLE,NEW.IS_STATISTICS,NEW.SAMPLE_PERIOD,NEW.POINT_TYPE,NEW.POINT_CLASS,NEW.POINT_PROPERTY,NEW.POINT_SORT,'',      ''
    FROM dev_info WHERE DEV_TP_NAME = NEW.DEV_TP_NAME;
*/
    
    INSERT INTO temp_plugin_point_map_mix
    (PLUGIN_NAME, TEMP_NAME, TEMP_POINT_NAME, FES_TEMP_NAME, FES_TEMP_POINT_NAME, FES_TEMP_OUT_NAME)
    SELECT 
    PLUGIN_NAME, TEMP_NAME, getAfterLastDot(NEW.TAG_NAME),'','',''
    FROM temp_plugin_dev_map WHERE TEMP_NAME = NEW.DEV_TP_NAME;
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'mix_temp_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('mix_temp_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_MIX_TEMP_DEFINE;
DELIMITER ;;
create trigger TU_MIX_TEMP_DEFINE AFTER UPDATE on mix_temp_define for each ROW 
BEGIN
    /*
    UPDATE mix SET 
    TAG_NAME = concat(DEVICE, '.', getAfterLastDot(NEW.TAG_NAME)),
    DESCRIPTION = NEW.DESCRIPTION,
    SEQ_NO = NEW.SEQ_NO,
    IS_CONTROL = NEW.IS_CONTROL,
    STATE_TEXT_NAME = NEW.STATE_TEXT_NAME,
    ALARM_PRIORITY = NEW.ALARM_PRIORITY,
    ALARM_DELAY_TIME = NEW.ALARM_DELAY_TIME,
    IS_WATER_ALM = NEW.IS_WATER_ALM,
    IS_ACK_ON_RTN = NEW.IS_ACK_ON_RTN,
    DEL_ACT_ON_ACK = NEW.DEL_ACT_ON_ACK,
    IS_NEVER_ALM_ON_RTN = NEW.IS_NEVER_ALM_ON_RTN,
    POINT_TP_NAME = NEW.TAG_NAME,
    IS_SAMPLE = NEW.IS_SAMPLE,
    IS_STATISTICS = NEW.IS_STATISTICS,
    SAMPLE_PERIOD = NEW.SAMPLE_PERIOD,
    POINT_TYPE = NEW.POINT_TYPE,
    POINT_CLASS = NEW.POINT_CLASS,
    POINT_PROPERTY = NEW.POINT_PROPERTY,
    POINT_SORT = NEW.POINT_SORT
    WHERE POINT_TP_NAME=OLD.TAG_NAME;
    
    UPDATE mix_control SET
    CTRL_ACT_NAME = NEW.CTRL_ACT_NAME,
    CTRL_TYPE = NEW.CTRL_TYPE,
    IS_TAGT_STATE = NEW.IS_TAGT_STATE,
    CTRL_TIMEOUT = NEW.CTRL_TIMEOUT,
    RESV_TIMEOUT = NEW.RESV_TIMEOUT,
    BASE = NEW.BASE,
    COEFF = NEW.COEFF,
    CTRL_TOLERANCE = NEW.CTRL_TOLERANCE,
    RANGE_UP = NEW.RANGE_UP,
    RANGE_LOW = NEW.RANGE_LOW,
    POINT_TP_NAME = NEW.TAG_NAME
    WHERE POINT_TP_NAME = OLD.TAG_NAME;
    
    UPDATE mix_map_to_fes SET
    POINT_TP_NAME = NEW.TAG_NAME,
    BASE = NEW.BASE,
    COEFF = NEW.COEFF,
    RANGE_UP = NEW.RANGE_UP,
    RANGE_LOW = NEW.RANGE_LOW,
    IS_FILTER_ERROR = NEW.IS_FILTER_ERROR,
    IS_FILTER_DISTURB = NEW.IS_FILTER_DISTURB,
    DISTURB_TIME = NEW.DISTURB_TIME
    WHERE POINT_TP_NAME = OLD.TAG_NAME;
    */
    
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN
        UPDATE temp_plugin_point_map_mix
        SET TEMP_POINT_NAME = getAfterLastDot(NEW.TAG_NAME)
        WHERE TEMP_NAME = OLD.DEV_TP_NAME AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    END IF;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'mix_temp_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('mix_temp_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_MIX_TEMP_DEFINE;
DELIMITER ;;
create trigger TD_MIX_TEMP_DEFINE BEFORE DELETE on mix_temp_define for each ROW 
BEGIN
    DELETE FROM temp_plugin_point_map_mix WHERE TEMP_NAME = OLD.DEV_TP_NAME  AND TEMP_POINT_NAME = getAfterLastDot(OLD.TAG_NAME);
    /*
    DELETE FROM mix WHERE POINT_TP_NAME = OLD.TAG_NAME;
    */
END;;
DELIMITER ;

drop trigger if exists TDA_MIX_TEMP_DEFINE;
DELIMITER ;;
create trigger TDA_MIX_TEMP_DEFINE AFTER DELETE on mix_temp_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'mix_temp_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('mix_temp_define'));
END IF;
END;;
DELIMITER ;

drop table if exists opc_config_define;
CREATE TABLE `opc_config_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `DEVICE` varchar(64) DEFAULT NULL COMMENT '设备标签',
  `DEV_GROUP` varchar(64) NOT NULL COMMENT '设备组标签',
  `COEFF` decimal(22,6) NOT NULL COMMENT '系数',
  `BASE` decimal(22,6) NOT NULL COMMENT '基值',
  `POINT_TYPE` varchar(64) NOT NULL COMMENT '点类型',
  `VALUE_TYPE` varchar(64) NOT NULL COMMENT '值类型',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_OPC_CONFIG_DEFINE` (`DEVICE`,`DEV_GROUP`,`POINT_TYPE`,`VALUE_TYPE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='opc配置表';

drop trigger if exists TIA_OPC_CONFIG_DEFINE;
DELIMITER ;;
create trigger TIA_OPC_CONFIG_DEFINE AFTER INSERT on opc_config_define for each ROW 
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'opc_config_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('opc_config_define'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_OPC_CONFIG_DEFINE;
DELIMITER ;;
create trigger TUA_OPC_CONFIG_DEFINE AFTER UPDATE on opc_config_define for each ROW 
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,   KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'opc_config_define', 'tag_name',     OLD.TAG_NAME,  NEW.TAG_NAME, getTableSubSystem('opc_config_define'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_OPC_CONFIG_DEFINE;
DELIMITER ;;
create trigger TDA_OPC_CONFIG_DEFINE AFTER DELETE on opc_config_define for each ROW 
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'opc_config_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('opc_config_define'));
    END IF;
END;;
DELIMITER ;

drop table if exists opc_config_version;
CREATE TABLE `opc_config_version` (
  `DOMAIN_ID` decimal(10,0) NOT NULL COMMENT '所属域',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `VERSION` decimal(10,0) NOT NULL COMMENT '版本号',
  `UPDATE_TIME` varchar(64) DEFAULT NULL COMMENT '最后更新时间',
  PRIMARY KEY (`DOMAIN_ID`,`SUB_SYSTEM`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='opc配置版本记录表';

drop trigger if exists TIA_OPC_CONFIG_VERSION;
DELIMITER ;;
create trigger TIA_OPC_CONFIG_VERSION AFTER INSERT on opc_config_version for each ROW 
BEGIN
   
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES						(getMaxTriggerId(),getOpType('insert'),'opc_config_version', 'domain_id,sub_system', '', concat(NEW.DOMAIN_ID,',',NEW.SUB_SYSTEM), getTableSubSystem('opc_config_version'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_OPC_CONFIG_VERSION;
DELIMITER ;;
create trigger TUA_OPC_CONFIG_VERSION AFTER UPDATE on opc_config_version for each ROW 
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,   KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'opc_config_version', 'domain_id,sub_system', concat(OLD.DOMAIN_ID,',',OLD.SUB_SYSTEM), concat(NEW.DOMAIN_ID,',',NEW.SUB_SYSTEM), getTableSubSystem('opc_config_version'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_OPC_CONFIG_VERSION;
DELIMITER ;;
create trigger TDA_OPC_CONFIG_VERSION AFTER DELETE on opc_config_version for each ROW 
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                                            (getMaxTriggerId(),getOpType('delete'),'opc_config_version', 'domain_id,sub_system', concat(OLD.DOMAIN_ID,',',OLD.SUB_SYSTEM), '', getTableSubSystem('opc_config_version'));
    END IF;
END;;
DELIMITER ;

drop table if exists operation_order_model;
CREATE TABLE `operation_order_model` (
  `ID` decimal(10,0) NOT NULL COMMENT 'ID',
  `ORDER_NAME` varchar(256) NOT NULL COMMENT '操作票名',
  `ORDER_DESC` varchar(512) DEFAULT NULL COMMENT '操作票描述',
  `REMARKS` varchar(1024) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='典型操作票表';

drop table if exists operation_order_model_info;
CREATE TABLE `operation_order_model_info` (
  `ORDER_ID` decimal(10,0) NOT NULL COMMENT '操作票ID，关联OPERATION_ORDER_PRESTORE表的ID列',
  `SEQ_NO` decimal(10,0) NOT NULL COMMENT '序号',
  `OPT_DESC` varchar(512) NOT NULL COMMENT '操作描述',
  `KEY_ID_TAG` varchar(64) DEFAULT NULL COMMENT '测点标识',
  `CTRL_VALUE` decimal(10,0) DEFAULT NULL COMMENT '控制值',
  `REMARKS` varchar(512) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`ORDER_ID`,`SEQ_NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='典型操作票信息表';

drop table if exists p_dict;
CREATE TABLE `p_dict` (
  `DICT_ID` decimal(10,0) NOT NULL COMMENT '数据字典标识',
  `DICT_SORT_ID` decimal(10,0) DEFAULT NULL COMMENT '数据字典分类标识',
  `DICT_CODE` varchar(64) DEFAULT NULL COMMENT '数据字典代码',
  `DICT_NAME_ZH` varchar(64) DEFAULT NULL COMMENT '数据字典名称,用于中文语系',
  `DICT_NAME_EN` varchar(150) DEFAULT NULL COMMENT '数据字典名称，用于英文语系',
  `DISPLAY_SN` decimal(10,0) DEFAULT NULL COMMENT '显示顺序',
  `STATUS` decimal(10,0) DEFAULT NULL COMMENT '使用状态 0：未启用 1：启用',
  `TYPE` varchar(4) DEFAULT NULL COMMENT '类型',
  PRIMARY KEY (`DICT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='字典信息从表';

drop table if exists p_dict_sort;
CREATE TABLE `p_dict_sort` (
  `DICT_SORT_ID` decimal(10,0) NOT NULL COMMENT '数据字典分类标识',
  `DICT_SORT_CODE` varchar(16) DEFAULT NULL COMMENT '数据字典分类代码',
  `DICT_SORT_NAME` varchar(64) DEFAULT NULL COMMENT '数据字典分类名称',
  `SORT_ID` decimal(10,0) DEFAULT NULL COMMENT '排序编号',
  PRIMARY KEY (`DICT_SORT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='字典信息主表';

drop table if exists pa_area_info;
CREATE TABLE `pa_area_info` (
  `ID` decimal(10,0) NOT NULL COMMENT '区域ID',
  `NAME` varchar(64) NOT NULL COMMENT '广播区名',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `UN_PA_AREA_INFO` (`ID`,`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PA广播区列表';

drop trigger if exists TIA_PA_AREA_INFO;
DELIMITER ;;
create trigger TIA_PA_AREA_INFO AFTER INSERT on pa_area_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_area_info', 'id', '', NEW.ID, getTableSubSystem('pa_area_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_AREA_INFO;
DELIMITER ;;
create trigger TUA_PA_AREA_INFO AFTER UPDATE on pa_area_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_area_info', 'id', OLD.ID, NEW.ID, getTableSubSystem('pa_area_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_AREA_INFO;
DELIMITER ;;
create trigger TDA_PA_AREA_INFO AFTER DELETE on pa_area_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_area_info', 'id', OLD.ID, '', getTableSubSystem('pa_area_info'));
END IF;
END;;
DELIMITER ;

drop table if exists pa_audio_info;
CREATE TABLE `pa_audio_info` (
  `ID` decimal(10,0) NOT NULL COMMENT '预录语音ID',
  `NAME` varchar(64) NOT NULL COMMENT '预录语音名',
  `CONTENT` varchar(256) NOT NULL COMMENT '内容',
  `DURATION` decimal(10,0) NOT NULL COMMENT '时长，单位秒',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `UN_PA_AUDIO_INFO` (`ID`,`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='预录语音信息';

drop trigger if exists TIA_PA_AUDIO_INFO;
DELIMITER ;;
create trigger TIA_PA_AUDIO_INFO AFTER INSERT on pa_audio_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_audio_info', 'id', '', NEW.ID, getTableSubSystem('pa_audio_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_AUDIO_INFO;
DELIMITER ;;
create trigger TUA_PA_AUDIO_INFO AFTER UPDATE on pa_audio_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_audio_info', 'id', OLD.ID, NEW.ID, getTableSubSystem('pa_audio_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_AUDIO_INFO;
DELIMITER ;;
create trigger TDA_PA_AUDIO_INFO AFTER DELETE on pa_audio_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_audio_info', 'id', OLD.ID, '', getTableSubSystem('pa_audio_info'));
END IF;
END;;
DELIMITER ;

drop table if exists pa_broadcast_group;
CREATE TABLE `pa_broadcast_group` (
  `ID` decimal(10,0) NOT NULL COMMENT '广播组ID',
  `NAME` varchar(64) NOT NULL COMMENT '广播组名',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站/位置',
  `AREA_SEQ` varchar(1024) NOT NULL COMMENT '广播区详情',
  PRIMARY KEY (`ID`,`LOCATION_ID`),
  UNIQUE KEY `UN_PA_BROADCAST_GROUP` (`ID`,`NAME`,`LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='广播组';

drop trigger if exists TIA_PA_BROADCAST_GROUP;
DELIMITER ;;
create trigger TIA_PA_BROADCAST_GROUP AFTER INSERT on pa_broadcast_group for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_broadcast_group', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pa_broadcast_group'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_BROADCAST_GROUP;
DELIMITER ;;
create trigger TUA_PA_BROADCAST_GROUP AFTER UPDATE on pa_broadcast_group for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_broadcast_group', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pa_broadcast_group'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_BROADCAST_GROUP;
DELIMITER ;;
create trigger TDA_PA_BROADCAST_GROUP AFTER DELETE on pa_broadcast_group for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_broadcast_group', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('pa_broadcast_group'));
END IF;
END;;
DELIMITER ;

drop table if exists pa_ctrl_param;
CREATE TABLE `pa_ctrl_param` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `CTRL_TAG_NAME` varchar(64) NOT NULL COMMENT '测点名',
  PRIMARY KEY (`LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PA控制参数配置';

drop trigger if exists TIA_PA_CTRL_PARAM;
DELIMITER ;;
create trigger TIA_PA_CTRL_PARAM AFTER INSERT on pa_ctrl_param for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_ctrl_param', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('pa_ctrl_param'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_CTRL_PARAM;
DELIMITER ;;
create trigger TUA_PA_CTRL_PARAM AFTER UPDATE on pa_ctrl_param for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_ctrl_param', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('pa_ctrl_param'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_CTRL_PARAM;
DELIMITER ;;
create trigger TDA_PA_CTRL_PARAM AFTER DELETE on pa_ctrl_param for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_ctrl_param', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('pa_ctrl_param'));
END IF;
END;;
DELIMITER ;

drop table if exists pa_location_area_def;
CREATE TABLE `pa_location_area_def` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID，关联车站表',
  `AREA_ID` decimal(10,0) NOT NULL COMMENT '广播区ID，关联PA_AREA_INFO',
  PRIMARY KEY (`LOCATION_ID`,`AREA_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='车站广播区定义';

drop trigger if exists TIA_PA_LOCATION_AREA_DEF;
DELIMITER ;;
create trigger TIA_PA_LOCATION_AREA_DEF AFTER INSERT on pa_location_area_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_location_area_def', 'location_id,area_id', '', concat(NEW.LOCATION_ID,',',NEW.AREA_ID), getTableSubSystem('pa_location_area_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_LOCATION_AREA_DEF;
DELIMITER ;;
create trigger TUA_PA_LOCATION_AREA_DEF AFTER UPDATE on pa_location_area_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_location_area_def', 'location_id,area_id', concat(OLD.LOCATION_ID,',',OLD.AREA_ID), concat(NEW.LOCATION_ID,',',NEW.AREA_ID), getTableSubSystem('pa_location_area_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_LOCATION_AREA_DEF;
DELIMITER ;;
create trigger TDA_PA_LOCATION_AREA_DEF AFTER DELETE on pa_location_area_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_location_area_def', 'location_id,area_id', concat(OLD.LOCATION_ID,',',OLD.AREA_ID), '', getTableSubSystem('pa_location_area_def'));
END IF;
END;;
DELIMITER ;

drop table if exists pa_location_seq_def;
CREATE TABLE `pa_location_seq_def` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `SEQ` decimal(10,0) NOT NULL COMMENT '顺序号',
  PRIMARY KEY (`LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PA系统车站序号定义';

drop trigger if exists TIA_PA_LOCATION_SEQ_DEF;
DELIMITER ;;
create trigger TIA_PA_LOCATION_SEQ_DEF AFTER INSERT on pa_location_seq_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_location_seq_def', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('pa_location_seq_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_LOCATION_SEQ_DEF;
DELIMITER ;;
create trigger TUA_PA_LOCATION_SEQ_DEF AFTER UPDATE on pa_location_seq_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_location_seq_def', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('pa_location_seq_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_LOCATION_SEQ_DEF;
DELIMITER ;;
create trigger TDA_PA_LOCATION_SEQ_DEF AFTER DELETE on pa_location_seq_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_location_seq_def', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('pa_location_seq_def'));
END IF;
END;;
DELIMITER ;

drop table if exists pa_operator_def;
CREATE TABLE `pa_operator_def` (
  `ID` decimal(10,0) NOT NULL COMMENT '操作员ID',
  `DESCRIPTION` varchar(64) NOT NULL COMMENT '描述',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PA子系统操作员ID定义表';

drop trigger if exists TIA_PA_OPERATOR_DEF;
DELIMITER ;;
create trigger TIA_PA_OPERATOR_DEF AFTER INSERT on pa_operator_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_operator_def', 'id', '', NEW.ID, getTableSubSystem('pa_operator_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_OPERATOR_DEF;
DELIMITER ;;
create trigger TUA_PA_OPERATOR_DEF AFTER UPDATE on pa_operator_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_operator_def', 'id', OLD.ID, NEW.ID, getTableSubSystem('pa_operator_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_OPERATOR_DEF;
DELIMITER ;;
create trigger TDA_PA_OPERATOR_DEF AFTER DELETE on pa_operator_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_operator_def', 'id', OLD.ID, '', getTableSubSystem('pa_operator_def'));
END IF;
END;;
DELIMITER ;

drop table if exists pa_operator_map;
CREATE TABLE `pa_operator_map` (
  `USER_GRP_ID` decimal(10,0) NOT NULL COMMENT 'ISCS用户组ID',
  `PA_OPERATOR_ID` decimal(10,0) NOT NULL COMMENT 'PA操作员ID',
  PRIMARY KEY (`USER_GRP_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='ISCS操作员ID与PA操作员ID映射';

drop trigger if exists TIA_PA_OPERATOR_MAP;
DELIMITER ;;
create trigger TIA_PA_OPERATOR_MAP AFTER INSERT on pa_operator_map for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_operator_map', 'user_grp_id', '', NEW.USER_GRP_ID, getTableSubSystem('pa_operator_map'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_OPERATOR_MAP;
DELIMITER ;;
create trigger TUA_PA_OPERATOR_MAP AFTER UPDATE on pa_operator_map for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_operator_map', 'user_grp_id', OLD.USER_GRP_ID, NEW.USER_GRP_ID, getTableSubSystem('pa_operator_map'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_OPERATOR_MAP;
DELIMITER ;;
create trigger TDA_PA_OPERATOR_MAP AFTER DELETE on pa_operator_map for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_operator_map', 'user_grp_id', OLD.USER_GRP_ID, '', getTableSubSystem('pa_operator_map'));
END IF;
END;;
DELIMITER ;

drop table if exists pa_timed_audio;
CREATE TABLE `pa_timed_audio` (
  `ID` decimal(10,0) NOT NULL COMMENT '定时广播ID',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站/位置',
  `NAME` varchar(64) NOT NULL COMMENT '定时广播名',
  `AUDIO_ID_LIST` varchar(256) NOT NULL COMMENT '预录语音ID列表，英文逗号分隔',
  `AREA_SEQ` varchar(1024) NOT NULL COMMENT '广播区详情',
  `IS_ACTIVE` decimal(10,0) NOT NULL COMMENT '是否激活',
  `WEEK_INFO` varchar(8) NOT NULL COMMENT '7个字符，从周一到周日',
  `START_TIME` varchar(16) NOT NULL COMMENT '开始时间，时分秒08:00:00',
  `END_TIME` varchar(16) NOT NULL COMMENT '结束时间，时分秒',
  `INTERVAL_TIME` decimal(10,0) NOT NULL COMMENT '间隔时间',
  `USER_ID` decimal(10,0) NOT NULL COMMENT '操作员ID-ISCS系统中的操作员',
  `USER_GRP_ID` decimal(10,0) NOT NULL,
  `OPERATE_TIME` decimal(20,0) DEFAULT NULL COMMENT '操作时间',
  `CUR_AUDIO_DURATION` decimal(10,0) DEFAULT NULL COMMENT '当前正在播放的语音持续时间，单位秒',
  `NEXT_AUDIO_INDEX` decimal(10,0) DEFAULT NULL COMMENT '下一个要播放的语音的索引，AUDIO_ID_LIST中的索引',
  PRIMARY KEY (`ID`,`LOCATION_ID`),
  UNIQUE KEY `UN_PA_TIMED_AUDIO` (`ID`,`LOCATION_ID`,`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='定时广播';

drop trigger if exists TIA_PA_TIMED_AUDIO;
DELIMITER ;;
create trigger TIA_PA_TIMED_AUDIO AFTER INSERT on pa_timed_audio for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pa_timed_audio', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pa_timed_audio'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PA_TIMED_AUDIO;
DELIMITER ;;
create trigger TUA_PA_TIMED_AUDIO AFTER UPDATE on pa_timed_audio for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pa_timed_audio', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pa_timed_audio'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PA_TIMED_AUDIO;
DELIMITER ;;
create trigger TDA_PA_TIMED_AUDIO AFTER DELETE on pa_timed_audio for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pa_timed_audio', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('pa_timed_audio'));
END IF;
END;;
DELIMITER ;

drop table if exists pis_area_info;
CREATE TABLE `pis_area_info` (
  `ID` decimal(10,0) NOT NULL COMMENT '区域ID',
  `NAME` varchar(64) NOT NULL COMMENT '区域名',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PIS区域信息';

drop trigger if exists TIA_PIS_AREA_INFO;
DELIMITER ;;
create trigger TIA_PIS_AREA_INFO AFTER INSERT on pis_area_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_area_info', 'id', '', NEW.ID, getTableSubSystem('pis_area_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PIS_AREA_INFO;
DELIMITER ;;
create trigger TUA_PIS_AREA_INFO AFTER UPDATE on pis_area_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_area_info', 'id', OLD.ID, NEW.ID, getTableSubSystem('pis_area_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PIS_AREA_INFO;
DELIMITER ;;
create trigger TDA_PIS_AREA_INFO AFTER DELETE on pis_area_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_area_info', 'id', OLD.ID, '', getTableSubSystem('pis_area_info'));
END IF;
END;;
DELIMITER ;

drop table if exists pis_ctrl_param;
CREATE TABLE `pis_ctrl_param` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `CTRL_KEY_ID_TAG` varchar(64) NOT NULL COMMENT '测点名',
  PRIMARY KEY (`LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PIS控制参数配置';

drop trigger if exists TIA_PIS_CTRL_PARAM;
DELIMITER ;;
create trigger TIA_PIS_CTRL_PARAM AFTER INSERT on pis_ctrl_param for each ROW 
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_ctrl_param', 'location_id', '', NEW.LOCATION_ID, getTableSubSystem('pis_ctrl_param'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PIS_CTRL_PARAM;
DELIMITER ;;
create trigger TUA_PIS_CTRL_PARAM AFTER UPDATE on pis_ctrl_param for each ROW 
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_ctrl_param', 'location_id', OLD.LOCATION_ID, NEW.LOCATION_ID, getTableSubSystem('pis_ctrl_param'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PIS_CTRL_PARAM;
DELIMITER ;;
create trigger TDA_PIS_CTRL_PARAM AFTER DELETE on pis_ctrl_param for each ROW 
BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_ctrl_param', 'location_id', OLD.LOCATION_ID, '', getTableSubSystem('pis_ctrl_param'));
    END IF;
END;;
DELIMITER ;

drop table if exists pis_display_info;
CREATE TABLE `pis_display_info` (
  `ID` decimal(10,0) NOT NULL COMMENT '发布记录ID',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '位置ID',
  `MSG_ID` decimal(10,0) DEFAULT NULL COMMENT '消息ID',
  `MSG_CONTENT` varchar(512) DEFAULT NULL COMMENT '消息内容',
  `AREAS` varchar(50) DEFAULT NULL COMMENT 'PIS发布区',
  `DEST_LOCATION` decimal(10,0) DEFAULT NULL COMMENT '发布目的地',
  `BEGIN_TIME` varchar(8) DEFAULT NULL COMMENT '开始时间,例如08:00',
  `END_TIME` varchar(8) DEFAULT NULL COMMENT '结束时间,例如08:00',
  `MSG_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '消息优先级',
  `INTERVAL_TIME` decimal(10,0) DEFAULT NULL COMMENT '间隔时间，单位秒',
  `OPERATOR_ID` decimal(10,0) DEFAULT NULL COMMENT '操作员ID',
  `OPERATE_TIME` decimal(20,0) DEFAULT NULL COMMENT '操作时间，单位1970-1-1至今的秒数',
  PRIMARY KEY (`ID`,`LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PIS发布记录表';

drop trigger if exists TIA_PIS_DISPLAY_INFO;
DELIMITER ;;
create trigger TIA_PIS_DISPLAY_INFO AFTER INSERT on pis_display_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_display_info', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pis_display_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PIS_DISPLAY_INFO;
DELIMITER ;;
create trigger TUA_PIS_DISPLAY_INFO AFTER UPDATE on pis_display_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_display_info', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pis_display_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PIS_DISPLAY_INFO;
DELIMITER ;;
create trigger TDA_PIS_DISPLAY_INFO AFTER DELETE on pis_display_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_display_info', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('pis_display_info'));
END IF;
END;;
DELIMITER ;

drop table if exists pis_fes_location_info;
CREATE TABLE `pis_fes_location_info` (
  `PIS_STATION_ID` decimal(10,0) NOT NULL COMMENT 'ATS对应的站号,0~31',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT 'ISCS车站号',
  PRIMARY KEY (`PIS_STATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='前置PIS乘客信息配置表';

drop trigger if exists TIA_PIS_FES_LOCATION_INFO;
DELIMITER ;;
create trigger TIA_PIS_FES_LOCATION_INFO AFTER INSERT on pis_fes_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_fes_location_info', 'pis_station_id', '', NEW.PIS_STATION_ID, getTableSubSystem('pis_fes_location_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PIS_FES_LOCATION_INFO;
DELIMITER ;;
create trigger TUA_PIS_FES_LOCATION_INFO AFTER UPDATE on pis_fes_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_fes_location_info', 'pis_station_id', OLD.PIS_STATION_ID, NEW.PIS_STATION_ID, getTableSubSystem('pis_fes_location_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PIS_FES_LOCATION_INFO;
DELIMITER ;;
create trigger TDA_PIS_FES_LOCATION_INFO AFTER DELETE on pis_fes_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_fes_location_info', 'pis_station_id', OLD.PIS_STATION_ID, '', getTableSubSystem('pis_fes_location_info'));
END IF;
END;;
DELIMITER ;

drop table if exists pis_location_area_def;
CREATE TABLE `pis_location_area_def` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `AREA_ID` decimal(10,0) NOT NULL COMMENT '区域ID',
  PRIMARY KEY (`LOCATION_ID`,`AREA_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PIS车站发布区定义表';

drop trigger if exists TIA_PIS_LOCATION_AREA_DEF;
DELIMITER ;;
create trigger TIA_PIS_LOCATION_AREA_DEF AFTER INSERT on pis_location_area_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_location_area_def', 'location_id,area_id', '', concat(NEW.LOCATION_ID,',',NEW.AREA_ID), getTableSubSystem('pis_location_area_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PIS_LOCATION_AREA_DEF;
DELIMITER ;;
create trigger TUA_PIS_LOCATION_AREA_DEF AFTER UPDATE on pis_location_area_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_location_area_def', 'location_id,area_id', concat(OLD.LOCATION_ID,',',OLD.AREA_ID), concat(NEW.LOCATION_ID,',',NEW.AREA_ID), getTableSubSystem('pis_location_area_def'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PIS_LOCATION_AREA_DEF;
DELIMITER ;;
create trigger TDA_PIS_LOCATION_AREA_DEF AFTER DELETE on pis_location_area_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_location_area_def', 'location_id,area_id', concat(OLD.LOCATION_ID,',',OLD.AREA_ID), '', getTableSubSystem('pis_location_area_def'));
END IF;
END;;
DELIMITER ;

drop table if exists pis_message_info;
CREATE TABLE `pis_message_info` (
  `ID` decimal(10,0) NOT NULL COMMENT '信息ID',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '位置ID',
  `DESCRIPTION` varchar(64) NOT NULL COMMENT 'PIS信息描述',
  `CONTENT` varchar(512) NOT NULL COMMENT 'PIS信息内容',
  PRIMARY KEY (`ID`,`LOCATION_ID`),
  UNIQUE KEY `UN_PIS_MESSAGE_INFO` (`ID`,`LOCATION_ID`,`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='PIS信息模板表';

drop trigger if exists TIA_PIS_MESSAGE_INFO;
DELIMITER ;;
create trigger TIA_PIS_MESSAGE_INFO AFTER INSERT on pis_message_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'pis_message_info', 'id,location_id', '', concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pis_message_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_PIS_MESSAGE_INFO;
DELIMITER ;;
create trigger TUA_PIS_MESSAGE_INFO AFTER UPDATE on pis_message_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'pis_message_info', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), concat(NEW.ID,',',NEW.LOCATION_ID), getTableSubSystem('pis_message_info'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_PIS_MESSAGE_INFO;
DELIMITER ;;
create trigger TDA_PIS_MESSAGE_INFO AFTER DELETE on pis_message_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'pis_message_info', 'id,location_id', concat(OLD.ID,',',OLD.LOCATION_ID), '', getTableSubSystem('pis_message_info'));
END IF;
END;;
DELIMITER ;

drop table if exists point_lock_condition_info;
CREATE TABLE `point_lock_condition_info` (
  `tag_name` varchar(128) NOT NULL COMMENT '闭锁测点标签',
  `actual_value` decimal(10,0) NOT NULL COMMENT '实际值',
  `condition_tag_name` varchar(128) NOT NULL COMMENT '条件测点',
  `condition_desc` varchar(128) NOT NULL COMMENT '描述',
  `condition1` decimal(10,0) NOT NULL COMMENT '条件1',
  `value1` decimal(22,6) NOT NULL COMMENT '值1',
  `condition2` decimal(10,0) NOT NULL COMMENT '条件2',
  `value2` decimal(22,6) NOT NULL COMMENT '值2',
  `condition_table` varchar(128) NOT NULL COMMENT '表名',
  PRIMARY KEY (`tag_name`,`actual_value`,`condition_tag_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测点闭锁信息表';

drop table if exists point_lock_config_info;
CREATE TABLE `point_lock_config_info` (
  `tag_name` varchar(128) NOT NULL COMMENT '标签',
  `actual_value` decimal(10,0) NOT NULL COMMENT '实际值',
  `isEnable` decimal(10,0) NOT NULL COMMENT '启用',
  PRIMARY KEY (`tag_name`,`actual_value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测点闭锁配置信息表';

drop table if exists point_task_link;
CREATE TABLE `point_task_link` (
  `KEY_ID_TAG` varchar(80) NOT NULL COMMENT '点标签',
  `TASK_STANDARD_UUID` varchar(37) NOT NULL COMMENT '作业组模板uuid（唯一标识）',
  `TASK_DESC` varchar(128) NOT NULL COMMENT '作业组描述',
  `IS_AUTO` decimal(10,0) NOT NULL COMMENT '是否自动派单',
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测点作业组关联表';

drop table if exists psc3000_plugin;
CREATE TABLE `psc3000_plugin` (
  `DEV_GROUP_TAG` varchar(64) NOT NULL COMMENT '设备组名',
  `DEV_GROUP_DESC` varchar(128) DEFAULT NULL COMMENT '设备组描述',
  `PLUGIN_NAME` varchar(64) NOT NULL COMMENT '套件名',
  `FES_TEMP_NAME` varchar(64) NOT NULL COMMENT '对应套件中的前置模板名',
  `FES_DEV_INST_ID` decimal(10,0) NOT NULL COMMENT '对应前置模板的对应实例前置设备的ID',
  PRIMARY KEY (`DEV_GROUP_TAG`,`PLUGIN_NAME`,`FES_TEMP_NAME`,`FES_DEV_INST_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='计算导入PSC3000数据参与的表';

drop table if exists scl_dataset;
CREATE TABLE `scl_dataset` (
  `id` decimal(10,0) NOT NULL COMMENT '主键',
  `iedname` varchar(128) DEFAULT NULL COMMENT '实例装置的iedname',
  `LDInst` varchar(128) DEFAULT NULL COMMENT '数据集的LD名称',
  `LNName` varchar(128) DEFAULT NULL COMMENT '数据集的LN名称',
  `Name` varchar(128) DEFAULT NULL COMMENT '数据集的名称',
  `Describtion` varchar(128) DEFAULT NULL COMMENT '数据集的描述',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='实例装置的数据集信息';

drop trigger if exists TIA_SCL_DATASET;
DELIMITER ;;
create trigger TIA_SCL_DATASET AFTER INSERT on scl_dataset for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_dataset', 'id', '', NEW.id, getTableSubSystem('scl_dataset'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_SCL_DATASET;
DELIMITER ;;
create trigger TUA_SCL_DATASET AFTER UPDATE on scl_dataset for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_dataset', 'id', OLD.id, NEW.id, getTableSubSystem('scl_dataset'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_SCL_DATASET;
DELIMITER ;;
create trigger TDA_SCL_DATASET AFTER DELETE on scl_dataset for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_dataset', 'id', OLD.id, '', getTableSubSystem('scl_dataset'));
END IF;
END;;
DELIMITER ;

drop table if exists scl_fcda;
CREATE TABLE `scl_fcda` (
  `id` decimal(10,0) NOT NULL COMMENT '主键',
  `ldInst` varchar(128) DEFAULT NULL COMMENT 'fcda的LD',
  `prefix` varchar(128) DEFAULT NULL COMMENT 'fcda的prefix',
  `lninst` varchar(128) DEFAULT NULL COMMENT 'fcda的lninst',
  `lnclass` varchar(128) DEFAULT NULL COMMENT 'fcda的lnclass',
  `doname` varchar(128) DEFAULT NULL COMMENT 'fcda的doname',
  `daName` varchar(128) DEFAULT NULL COMMENT 'fcda的daname',
  `FC` varchar(128) DEFAULT NULL COMMENT 'fcda的fc',
  `Ix` varchar(128) DEFAULT NULL COMMENT 'fcda的lx',
  `DatasetID` decimal(10,0) DEFAULT NULL COMMENT 'fcda关联数据集ID',
  `TypeID` decimal(10,0) DEFAULT NULL COMMENT 'fcda的typeid',
  `IedName` varchar(128) DEFAULT NULL COMMENT '实例装置的iedname',
  `basictype` decimal(10,0) DEFAULT NULL COMMENT 'fcda的基本类型',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='实例装置的FCDA测点';

drop trigger if exists TIA_SCL_FCDA;
DELIMITER ;;
create trigger TIA_SCL_FCDA AFTER INSERT on scl_fcda for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_fcda', 'id', '', NEW.id, getTableSubSystem('scl_fcda'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_SCL_FCDA;
DELIMITER ;;
create trigger TUA_SCL_FCDA AFTER UPDATE on scl_fcda for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_fcda', 'id', OLD.id, NEW.id, getTableSubSystem('scl_fcda'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_SCL_FCDA;
DELIMITER ;;
create trigger TDA_SCL_FCDA AFTER DELETE on scl_fcda for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_fcda', 'id', OLD.id, '', getTableSubSystem('scl_fcda'));
END IF;
END;;
DELIMITER ;

drop table if exists scl_lcb;
CREATE TABLE `scl_lcb` (
  `id` decimal(10,0) NOT NULL COMMENT '主键',
  `name` varchar(128) DEFAULT NULL COMMENT '控制块名称',
  `lcbDesc` varchar(128) DEFAULT NULL COMMENT '控制块的描述',
  `dataset` varchar(128) DEFAULT NULL COMMENT '控制块的数据集名称',
  `intgPd` decimal(10,0) DEFAULT NULL COMMENT '控制块的intgPd',
  `TrgOps` decimal(10,0) DEFAULT NULL COMMENT '控制块的trgOps',
  `iedname` varchar(128) DEFAULT NULL COMMENT '实例装置的iedname',
  `LDInst` varchar(128) DEFAULT NULL COMMENT '报告控制块的ldinst',
  `LNName` varchar(128) DEFAULT NULL COMMENT '报告控制块的lnName',
  `logEna` decimal(10,0) DEFAULT NULL COMMENT '报告控制块是否使能',
  `logName` varchar(128) DEFAULT NULL COMMENT '报告控制块的logname',
  `ReasonCode` decimal(10,0) DEFAULT NULL COMMENT '报告控制块的reasoncode',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='实例装置的人日志控制块信息';

drop trigger if exists TIA_SCL_LCB;
DELIMITER ;;
create trigger TIA_SCL_LCB AFTER INSERT on scl_lcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_lcb', 'id', '', NEW.id, getTableSubSystem('scl_lcb'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_SCL_LCB;
DELIMITER ;;
create trigger TUA_SCL_LCB AFTER UPDATE on scl_lcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_lcb', 'id', OLD.id, NEW.id, getTableSubSystem('scl_lcb'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_SCL_LCB;
DELIMITER ;;
create trigger TDA_SCL_LCB AFTER DELETE on scl_lcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_lcb', 'id', OLD.id, '', getTableSubSystem('scl_lcb'));
END IF;
END;;
DELIMITER ;

drop table if exists scl_rcb;
CREATE TABLE `scl_rcb` (
  `id` decimal(10,0) NOT NULL COMMENT '主键',
  `rcbdesc` varchar(128) DEFAULT NULL COMMENT '控制块的描述',
  `name` varchar(128) DEFAULT NULL COMMENT '控制块的名称',
  `dataSet` varchar(128) DEFAULT NULL COMMENT '控制块的数据集名称',
  `intgPd` decimal(10,0) DEFAULT NULL COMMENT '控制块的IntgPd',
  `rptID` varchar(128) DEFAULT NULL COMMENT '控制块的rptid',
  `confRev` decimal(10,0) DEFAULT NULL COMMENT '控制块的confRev',
  `buffered` decimal(10,0) DEFAULT NULL COMMENT '控制块的buffered',
  `indexed` decimal(10,0) DEFAULT NULL COMMENT '控制块的indexed',
  `bufTime` decimal(10,0) DEFAULT NULL COMMENT '控制块的buftime',
  `TrgOps` decimal(10,0) DEFAULT NULL COMMENT '控制块的TrgOps',
  `OptFlds` decimal(10,0) DEFAULT NULL COMMENT '控制块的OptFlds',
  `maxClient` decimal(10,0) DEFAULT NULL COMMENT '控制块的maxclient',
  `iedname` varchar(128) DEFAULT NULL COMMENT '实例装置的Iedname',
  `LDInst` varchar(128) DEFAULT NULL COMMENT '控制块的ldinst',
  `LNName` varchar(128) DEFAULT NULL COMMENT '控制块的LnName',
  `giPeriod` decimal(10,0) DEFAULT NULL COMMENT '控制块的giPeriod',
  `enablePeriod` decimal(10,0) DEFAULT NULL COMMENT '控制块的enablePeriod',
  `rcbinst` decimal(10,0) DEFAULT NULL COMMENT '控制块的rcbinst',
  `rcbena` decimal(10,0) DEFAULT NULL COMMENT '控制块是否使能',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='实例装置的报告控制块信息';

drop trigger if exists TIA_SCL_RCB;
DELIMITER ;;
create trigger TIA_SCL_RCB AFTER INSERT on scl_rcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_rcb', 'id', '', NEW.id, getTableSubSystem('scl_rcb'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_SCL_RCB;
DELIMITER ;;
create trigger TUA_SCL_RCB AFTER UPDATE on scl_rcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_rcb', 'id', OLD.id, NEW.id, getTableSubSystem('scl_rcb'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_SCL_RCB;
DELIMITER ;;
create trigger TDA_SCL_RCB AFTER DELETE on scl_rcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_rcb', 'id', OLD.id, '', getTableSubSystem('scl_rcb'));
END IF;
END;;
DELIMITER ;

drop table if exists scl_runtime;
CREATE TABLE `scl_runtime` (
  `id` decimal(10,0) NOT NULL COMMENT '主键',
  `typeid` decimal(10,0) DEFAULT NULL COMMENT 'typeid的id',
  `typeindex` decimal(10,0) DEFAULT NULL COMMENT '类型索引号',
  `eltag` decimal(10,0) DEFAULT NULL COMMENT '类型标签',
  `pellen` decimal(10,0) DEFAULT NULL COMMENT '类型长度',
  `strnumrtblks` decimal(10,0) DEFAULT NULL COMMENT 'typeid的属性',
  `arrnumelmnts` decimal(10,0) DEFAULT NULL COMMENT 'typeid的属性',
  `arrnumrtblks` decimal(10,0) DEFAULT NULL COMMENT 'typeid的属性',
  `compnameptr` varchar(128) DEFAULT NULL COMMENT 'typeid的属性',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='实例测点的RT信息';

drop trigger if exists TIA_SCL_RUNTIME;
DELIMITER ;;
create trigger TIA_SCL_RUNTIME AFTER INSERT on scl_runtime for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_runtime', 'id', '', NEW.id, getTableSubSystem('scl_runtime'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_SCL_RUNTIME;
DELIMITER ;;
create trigger TUA_SCL_RUNTIME AFTER UPDATE on scl_runtime for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_runtime', 'id', OLD.id, NEW.id, getTableSubSystem('scl_runtime'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_SCL_RUNTIME;
DELIMITER ;;
create trigger TDA_SCL_RUNTIME AFTER DELETE on scl_runtime for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_runtime', 'id', OLD.id, '', getTableSubSystem('scl_runtime'));
END IF;
END;;
DELIMITER ;

drop table if exists scl_sgcb;
CREATE TABLE `scl_sgcb` (
  `id` decimal(10,0) NOT NULL COMMENT '主键',
  `iedname` varchar(128) DEFAULT NULL COMMENT '实例装置的iedname',
  `LDInst` varchar(128) DEFAULT NULL COMMENT '控制块的ldinst',
  `LNName` varchar(128) DEFAULT NULL COMMENT '控制块的lnName',
  `NumOfSG` decimal(10,0) DEFAULT NULL COMMENT '控制块的定值组个数',
  `ActSG` varchar(128) DEFAULT NULL COMMENT '控制块的ActSG',
  `ResvTms` decimal(10,0) DEFAULT NULL COMMENT '控制块的ResvTms',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='实例装置的定值控制块信息';

drop trigger if exists TIA_SCL_SGCB;
DELIMITER ;;
create trigger TIA_SCL_SGCB AFTER INSERT on scl_sgcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'scl_sgcb', 'id', '', NEW.id, getTableSubSystem('scl_sgcb'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_SCL_SGCB;
DELIMITER ;;
create trigger TUA_SCL_SGCB AFTER UPDATE on scl_sgcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'scl_sgcb', 'id', OLD.id, NEW.id, getTableSubSystem('scl_sgcb'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_SCL_SGCB;
DELIMITER ;;
create trigger TDA_SCL_SGCB AFTER DELETE on scl_sgcb for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'scl_sgcb', 'id', OLD.id, '', getTableSubSystem('scl_sgcb'));
END IF;
END;;
DELIMITER ;

drop table if exists sequence_action;
CREATE TABLE `sequence_action` (
  `NAME` varchar(128) NOT NULL COMMENT '动作名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '动作描述',
  `KEY_ID_TAG` varchar(128) DEFAULT NULL COMMENT '点名(表名+’.’+TAG_NAME)',
  `TIMEFLAG` decimal(20,0) DEFAULT NULL COMMENT '修改时标',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `TARGET_VALUE` decimal(10,0) DEFAULT NULL COMMENT '目标值',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='顺控动作表';

drop trigger if exists TI_SEQUENCE_ACTION;
DELIMITER ;;
create trigger TI_SEQUENCE_ACTION BEFORE INSERT on sequence_action for each ROW 
body:BEGIN
    SET NEW.TIMEFLAG = unix_timestamp(now());
END;;
DELIMITER ;

drop trigger if exists TIA_SEQUENCE_ACTION;
DELIMITER ;;
create trigger TIA_SEQUENCE_ACTION AFTER INSERT on sequence_action for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'sequence_action', 'name', '', NEW.NAME, getTableSubSystem('sequence_action'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_SEQUENCE_ACTION;
DELIMITER ;;
create trigger TU_SEQUENCE_ACTION BEFORE UPDATE on sequence_action for each ROW 
body:BEGIN
    #修改对应功能
    SET NEW.TIMEFLAG = unix_timestamp(now());
    UPDATE sequence_function SET 
    TIMEFLAG = NEW.TIMEFLAG,
    ACTION_NAME = NEW.NAME
    WHERE ACTION_NAME = OLD.NAME;
END;;
DELIMITER ;

drop trigger if exists TUA_SEQUENCE_ACTION;
DELIMITER ;;
create trigger TUA_SEQUENCE_ACTION AFTER UPDATE on sequence_action for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'sequence_action', 'name', OLD.NAME, NEW.NAME, getTableSubSystem('sequence_action'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_SEQUENCE_ACTION;
DELIMITER ;;
create trigger TD_SEQUENCE_ACTION AFTER DELETE on sequence_action for each ROW 
body:BEGIN
    #删除关联了此动作的功能
    DELETE FROM sequence_function WHERE ACTION_NAME = OLD.NAME; 
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'sequence_action', 'name', OLD.NAME, '', getTableSubSystem('sequence_action'));
END IF;
END;;
DELIMITER ;

drop table if exists sequence_define;
CREATE TABLE `sequence_define` (
  `SEQ_NAME` varchar(128) NOT NULL COMMENT '所属顺控',
  `FUNC_NAME` varchar(128) NOT NULL COMMENT '功能名',
  `FUNC_NO` decimal(10,0) NOT NULL COMMENT '功能编号',
  `RELATION` decimal(10,0) DEFAULT NULL COMMENT '串/并行',
  `TIMEFLAG` decimal(20,0) DEFAULT NULL COMMENT '时标',
  PRIMARY KEY (`SEQ_NAME`,`FUNC_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='顺控定义表';

drop trigger if exists TI_SEQUENCE_DEFINE;
DELIMITER ;;
create trigger TI_SEQUENCE_DEFINE BEFORE INSERT on sequence_define for each ROW 
body:BEGIN
    #仅仅修改时标
    #避免触发器循环
    SET @disableSequenceSettingTrigger = 1;
        
    IF COALESCE(@disableSequenceDefineTrigger,0) = 0 THEN
    BEGIN
        SET NEW.TIMEFLAG = unix_timestamp(now());
        UPDATE sequence_setting SET 
        TIMEFLAG = NEW.TIMEFLAG
        WHERE NAME = NEW.SEQ_NAME;
    END;
    END IF;
    
    SET @disableSequenceSettingTrigger = 0;
    
    
END;;
DELIMITER ;

drop trigger if exists TIA_SEQUENCE_DEFINE;
DELIMITER ;;
create trigger TIA_SEQUENCE_DEFINE AFTER INSERT on sequence_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'sequence_define', 'seq_name,func_name', '', concat(NEW.SEQ_NAME,',',NEW.FUNC_NAME), getTableSubSystem('sequence_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_SEQUENCE_DEFINE;
DELIMITER ;;
create trigger TU_SEQUENCE_DEFINE BEFORE UPDATE on sequence_define for each ROW 
body:BEGIN
    
    #修改对应联动功能关系表
    #避免触发器循环
    SET @disableSequenceSettingTrigger = 1;
        
    IF COALESCE(@disableSequenceDefineTrigger,0) = 0 THEN
    BEGIN
        SET NEW.TIMEFLAG = unix_timestamp(now());
        UPDATE sequence_setting SET 
        TIMEFLAG = NEW.TIMEFLAG
        WHERE NAME = OLD.SEQ_NAME;
    END;
    END IF;
    
    SET @disableSequenceSettingTrigger = 0;
END;;
DELIMITER ;

drop trigger if exists TUA_SEQUENCE_DEFINE;
DELIMITER ;;
create trigger TUA_SEQUENCE_DEFINE AFTER UPDATE on sequence_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'sequence_define', 'seq_name,func_name', concat(OLD.SEQ_NAME,',',OLD.FUNC_NAME), concat(NEW.SEQ_NAME,',',NEW.FUNC_NAME), getTableSubSystem('sequence_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_SEQUENCE_DEFINE;
DELIMITER ;;
create trigger TD_SEQUENCE_DEFINE AFTER DELETE on sequence_define for each ROW 
body:BEGIN
    #仅仅修改时标
    #避免触发器循环
    SET @disableSequenceSettingTrigger = 1;
        
    IF COALESCE(@disableSequenceDefineTrigger,0) = 0 THEN
    BEGIN
        UPDATE sequence_setting SET 
        TIMEFLAG = unix_timestamp(now())
        WHERE NAME = OLD.SEQ_NAME;
    END;
    END IF;
    
    SET @disableSequenceSettingTrigger = 0;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'sequence_define', 'seq_name,func_name', concat(OLD.SEQ_NAME,',',OLD.FUNC_NAME), '', getTableSubSystem('sequence_define'));
END IF;
END;;
DELIMITER ;

drop table if exists sequence_function;
CREATE TABLE `sequence_function` (
  `NAME` varchar(128) NOT NULL COMMENT '功能名称',
  `ACTION_NAME` varchar(128) NOT NULL COMMENT '动作名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '功能描述',
  `ACTION_NO` decimal(10,0) DEFAULT NULL COMMENT '动作编号',
  `ENABLE` decimal(10,0) DEFAULT NULL COMMENT '是否可用',
  `DELAYTIME` decimal(10,0) DEFAULT NULL COMMENT '延时时间',
  `TIMEFLAG` decimal(20,0) DEFAULT NULL COMMENT '时标',
  `FAILSTRAGE` decimal(10,0) DEFAULT NULL COMMENT '失败处理',
  `RETRYTIMES` decimal(10,0) DEFAULT NULL COMMENT '重试次数',
  PRIMARY KEY (`NAME`,`ACTION_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='顺控功能';

drop trigger if exists TI_SEQUENCE_FUNCTION;
DELIMITER ;;
create trigger TI_SEQUENCE_FUNCTION BEFORE INSERT on sequence_function for each ROW 
body:BEGIN
    #仅仅修改时标
    SET NEW.TIMEFLAG = unix_timestamp(now());
    
END;;
DELIMITER ;

drop trigger if exists TIA_SEQUENCE_FUNCTION;
DELIMITER ;;
create trigger TIA_SEQUENCE_FUNCTION AFTER INSERT on sequence_function for each ROW 
BEGIN
    UPDATE sequence_define SET 
    TIMEFLAG = NEW.TIMEFLAG
    WHERE FUNC_NAME = NEW.NAME;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'sequence_function', 'name,action_name', '', concat(NEW.NAME,',',NEW.ACTION_NAME), getTableSubSystem('sequence_function'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_SEQUENCE_FUNCTION;
DELIMITER ;;
create trigger TU_SEQUENCE_FUNCTION BEFORE UPDATE on sequence_function for each ROW 
body:BEGIN
    #修改对应联动功能关系表
    SET NEW.TIMEFLAG = unix_timestamp(now());
    UPDATE sequence_define SET 
    TIMEFLAG = NEW.TIMEFLAG,
    FUNC_NAME = NEW.NAME
    WHERE FUNC_NAME = OLD.NAME;
    
END;;
DELIMITER ;

drop trigger if exists TUA_SEQUENCE_FUNCTION;
DELIMITER ;;
create trigger TUA_SEQUENCE_FUNCTION AFTER UPDATE on sequence_function for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'sequence_function', 'name,action_name', concat(OLD.NAME,',',OLD.ACTION_NAME), concat(NEW.NAME,',',NEW.ACTION_NAME), getTableSubSystem('sequence_function'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_SEQUENCE_FUNCTION;
DELIMITER ;;
create trigger TD_SEQUENCE_FUNCTION AFTER DELETE on sequence_function for each ROW 
body:BEGIN
    #删除关联了此功能的联动定义
    #检查功能是否被删除
    IF NOT EXISTS (SELECT * FROM sequence_function WHERE NAME = OLD.NAME) THEN 
    BEGIN
        DELETE FROM sequence_define WHERE FUNC_NAME = OLD.NAME; 
    END;
    ELSE 
    BEGIN
        #修改时标
        UPDATE sequence_define SET
        TIMEFLAG = unix_timestamp(now())
        WHERE FUNC_NAME = OLD.NAME;
    END; 
    END IF;
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'sequence_function', 'name,action_name', concat(OLD.NAME,',',OLD.ACTION_NAME), '', getTableSubSystem('sequence_function'));
END IF;
END;;
DELIMITER ;

drop table if exists sequence_setting;
CREATE TABLE `sequence_setting` (
  `NAME` varchar(128) NOT NULL COMMENT '联动名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `SEQ_TYPE` decimal(10,0) DEFAULT NULL COMMENT '顺控类型',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '车站',
  `TIMEFLAG` decimal(20,0) DEFAULT NULL COMMENT '时标',
  PRIMARY KEY (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='顺控配置';

drop trigger if exists TI_SEQUENCE_SETTING;
DELIMITER ;;
create trigger TI_SEQUENCE_SETTING BEFORE INSERT on sequence_setting for each ROW 
body:BEGIN
    #仅仅修改时标
    #避免触发器循环
    SET @disableSequenceDefineTrigger = 1;
        
    IF COALESCE(@disableSequenceSettingTrigger,0) = 0 THEN
    BEGIN
        SET NEW.TIMEFLAG = unix_timestamp(now());
    END;
    END IF;
    
    SET @disableSequenceDefineTrigger = 0;
    
    
END;;
DELIMITER ;

drop trigger if exists TIA_SEQUENCE_SETTING;
DELIMITER ;;
create trigger TIA_SEQUENCE_SETTING AFTER INSERT on sequence_setting for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'sequence_setting', 'name', '', NEW.NAME, getTableSubSystem('sequence_setting'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TU_SEQUENCE_SETTING;
DELIMITER ;;
create trigger TU_SEQUENCE_SETTING BEFORE UPDATE on sequence_setting for each ROW 
body:BEGIN
    #修改对应联动功能关系表
    #避免触发器循环
    SET @disableSequenceDefineTrigger = 1;
        
    IF COALESCE(@disableSequenceSettingTrigger,0) = 0 THEN
    BEGIN
        SET NEW.TIMEFLAG = unix_timestamp(now());
        IF NEW.NAME<>OLD.NAME THEN 
            UPDATE sequence_define SET 
            SEQ_NAME = NEW.NAME
            WHERE SEQ_NAME = OLD.NAME;
       END IF;
    END;
    END IF;
    
    SET @disableSequenceDefineTrigger = 0;
    
    
END;;
DELIMITER ;

drop trigger if exists TUA_SEQUENCE_SETTING;
DELIMITER ;;
create trigger TUA_SEQUENCE_SETTING AFTER UPDATE on sequence_setting for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'sequence_setting', 'name', OLD.NAME, NEW.NAME, getTableSubSystem('sequence_setting'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TD_SEQUENCE_SETTING;
DELIMITER ;;
create trigger TD_SEQUENCE_SETTING AFTER DELETE on sequence_setting for each ROW 
body:BEGIN
    #删除关联了此联动的功能关联
    #避免触发器循环
    SET @disableSequenceDefineTrigger = 1;
        
    IF COALESCE(@disableSequenceSettingTrigger,0) = 0 THEN
    BEGIN
        DELETE FROM sequence_define WHERE SEQ_NAME = OLD.NAME;
    END;
    END IF;
    
    SET @disableSequenceDefineTrigger = 0;
    
    
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'sequence_setting', 'name', OLD.NAME, '', getTableSubSystem('sequence_setting'));
END IF;
END;;
DELIMITER ;

drop table if exists stat_inst_def;
CREATE TABLE `stat_inst_def` (
  `INST_TAG` varchar(64) NOT NULL COMMENT '统计实例标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '统计实例描述',
  `TEMPLATE_TAG` varchar(64) NOT NULL COMMENT '统计模板归属',
  `TRIGGER_METHOD` decimal(10,0) NOT NULL COMMENT '触发方式有两种，一种为周期触发，一种为定时触发。和不触发。 对应值为 0 不触发 1 周期触发 2 定时触发 选择周期触发 需要填充PERIOD列 该列不得为空 选择定时触发，需要选择定时模式 填充TIMER_MODE列，然后根据选择定时模式，填充对应MONTH DAY WEEKDAY HOUR MINUTE SECOND列 周期触发的初始触发时间为统计服务程序打开的时刻，可自定义触发周期。循环触发 下面讲解定时触发的设计。 【需要设计对应触发器】 模式可分为 每年/每月/每日/每周/每小时 每年模式时，需要定义 月日时分秒 具体日期和时间。 定义在每一年的具体某个日期时刻循环触发。 需要填充 MONTH DAY HOUR MINUTE SECOND列 这些列不得为空 每月模式时，需要定义 日时分秒 具体日期和时间。定义在每月的具体某个日期时刻循环触发。需要填充 DAY HOUR MINUTE SECOND列 这些列不得为空 每月模式时，DAY可为负数，意为从每月的倒数第几天，以防大小月。 每日模式时，需要定义 时分秒 具体时间。定义在每天具体某个时间循环触发。需要填充 HOUR MINUTE SECOND列 这些列不得为空 每周模式时，需要定义 时分秒 具体时间。定义在每周的星期几某个时间循环触发 需要填充 HOUR MINUTE SECOND列 这些列不得为空 每小时模式 需要定义分秒 具体时间 定义在每小时的具体时间循环触发 需要填充 MINUTE SECOND列 这些列不得为空',
  `PERIOD` decimal(3,0) DEFAULT NULL COMMENT '计算周期，计算触发周期，开始时间从统计服务开始运行起计算 当值为 0 不计算 1 每半小时 2 每小时 3 每天 4 每周 5 每月 6 每年',
  `TIMER_MODE` decimal(10,0) DEFAULT NULL COMMENT '定时触发的模式 对应的值为 每年模式 1 每月模式 2 每日模式 3 每周模式 4 每小时模式 5 下面讲解定时触发的设计。 【需要设计对应触发器】 模式可分为 每年/每月/每日/每周/每小时 每年模式时，需要定义 月日时分秒 具体日期和时间。 定义在每一年的具体某个日期时刻循环触发。 需要填充 MONTH DAY HOUR MINUTE SECOND列 这些列不得为空 每月模式时，需要定义 日时分秒 具体日期和时间。定义在每月的具体某个日期时刻循环触发。需要填充 DAY HOUR MINUTE SECOND列 这些列不得为空 每月模式时，DAY可为负数，意为从每月的倒数第几天，以防大小月。 每日模式时，需要定义 时分秒 具体时间。定义在每天具体某个时间循环触发。需要填充 HOUR MINUTE SECOND列 这些列不得为空 每周模式时，需要定义 时分秒 具体时间。定义在每周的星期几某个时间循环触发 需要填充 HOUR MINUTE SECOND列 这些列不得为空 每小时模式 需要定义分秒 具体时间 定义在每小时的具体时间循环触发 需要填充 MINUTE SECOND列 这些列不得为空',
  `MONTH` decimal(10,0) DEFAULT NULL COMMENT '正常填充 1-12',
  `DAY` decimal(10,0) DEFAULT NULL COMMENT '正常填充 1-31 仅在每月模式的时候可以启用下面的值，防止大小月无法确定月末。 -1 月末 -2 这个月的倒数第二天 -3 这个月的倒数第三天 -N 这个月的倒数第X天',
  `WEEKDAY` decimal(10,0) DEFAULT NULL COMMENT '1-7',
  `HOUR` decimal(10,0) DEFAULT NULL COMMENT '0-23',
  `MINUTE` decimal(10,0) DEFAULT NULL COMMENT '0-59',
  `SECOND` decimal(10,0) DEFAULT NULL COMMENT '0-59',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业ID',
  `IS_MOD` decimal(10,0) NOT NULL COMMENT '是否修改',
  PRIMARY KEY (`INST_TAG`),
  KEY `IN_STAT_INST_DEF` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计实例定义表';

drop trigger if exists TIA_STAT_INST_DEF;
DELIMITER ;;
create trigger TIA_STAT_INST_DEF AFTER INSERT on stat_inst_def for each ROW 
BEGIN
    
    #插入测点分组表
    #插入测点表
    #插入时间测点变
    #插入返回分组表
    #插入返回参数表
    INSERT INTO stat_inst_para_grp 
    (PARA_GRP_TAG,                                        DESCRIPTION,  INST_TAG,      SORT,LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG)) ,  DESCRIPTION,  NEW.INST_TAG,  SORT,NEW.LOCATION_ID,NEW.SUB_SYSTEM 
    FROM stat_template_para WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 1;
    INSERT INTO stat_inst_para_point 
    (PARA_TAG,                                         DESCRIPTION, `RANK`, POINT_TAG, SORT, PARA_GRP_TAG, INST_TAG,    LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG)),DESCRIPTION,  1,     '',        SORT, '',           NEW.INST_TAG,NEW.LOCATION_ID,NEW.SUB_SYSTEM
    FROM stat_template_para WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 0 AND COALESCE(`TYPE`,0) = 1;
    
    INSERT INTO stat_inst_para_time 
    (PARA_TAG,                                         DESCRIPTION, `RANK`, `TIMESTAMP`,           SORT, PARA_GRP_TAG, INST_TAG,    TIME_MODE, YEAR_R, MONTH_R, DAY_R, WEEKDAY_R, HOUR_R, MINUTE_R, SECOND_R,LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG)),DESCRIPTION, 1,       unix_timestamp(now()),SORT, '',           NEW.INST_TAG, 0,        0,      0,       0,      0,         0,      0,        0,      NEW.LOCATION_ID,NEW.SUB_SYSTEM
    FROM stat_template_para WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 0 AND COALESCE(`TYPE`,0) = 2;
    
    INSERT INTO stat_inst_ret_grp 
    (RET_GRP_TAG,                                     DESCRIPTION, INST_TAG,    SORT,LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(RET_TAG)),DESCRIPTION, NEW.INST_TAG,SORT,NEW.LOCATION_ID,NEW.SUB_SYSTEM
    FROM stat_template_ret WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 1;
    
    INSERT INTO stat_inst_ret_para 
    (RET_TAG,                                         DESCRIPTION, `RANK`, POINT_TAG, SORT, PARA_GRP_TAG, VALUE, IS_BIND, INST_TAG,   LOCATION_ID,    SUB_SYSTEM)
    SELECT 
    concat(NEW.INST_TAG,'.',getAfterLastDot(RET_TAG)),DESCRIPTION,  1,      '',       SORT,  '',          0,      0,      NEW.INST_TAG,NEW.LOCATION_ID,NEW.SUB_SYSTEM          
    FROM stat_template_ret WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(ISGRP,0) = 0;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_def', 'inst_tag', '', NEW.INST_TAG, getTableSubSystem('stat_inst_def'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_INST_DEF;
DELIMITER ;;
create trigger TUA_STAT_INST_DEF AFTER UPDATE on stat_inst_def for each ROW 
BEGIN
    IF NEW.INST_TAG <> OLD.INST_TAG THEN 
    BEGIN
        UPDATE stat_inst_para_grp
        SET INST_TAG = NEW.INST_TAG,
        PARA_GRP_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_GRP_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
        
        UPDATE stat_inst_para_point
        SET INST_TAG = NEW.INST_TAG,
        PARA_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
        
        UPDATE stat_inst_para_time
        SET INST_TAG = NEW.INST_TAG,
        PARA_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(PARA_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
    
        UPDATE stat_inst_ret_grp
        SET INST_TAG = NEW.INST_TAG,
        RET_GRP_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(RET_GRP_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
        
        UPDATE stat_inst_ret_para
        SET INST_TAG = NEW.INST_TAG,
        RET_TAG = concat(NEW.INST_TAG,'.',getAfterLastDot(RET_TAG))
        WHERE INST_TAG = OLD.INST_TAG;
    
    END;
    END IF;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_def', 'inst_tag', OLD.INST_TAG, NEW.INST_TAG, getTableSubSystem('stat_inst_def'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_INST_DEF;
DELIMITER ;;
create trigger TDA_STAT_INST_DEF AFTER DELETE on stat_inst_def for each ROW 
BEGIN
    DELETE FROM stat_inst_para_grp WHERE INST_TAG = OLD.INST_TAG;
    DELETE FROM stat_inst_para_point WHERE INST_TAG = OLD.INST_TAG;
    DELETE FROM stat_inst_para_time WHERE INST_TAG = OLD.INST_TAG;
    DELETE FROM stat_inst_ret_grp WHERE INST_TAG = OLD.INST_TAG;
    DELETE FROM stat_inst_ret_para WHERE INST_TAG = OLD.INST_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_def', 'inst_tag', OLD.INST_TAG, '', getTableSubSystem('stat_inst_def'));
    END IF;
END;;
DELIMITER ;

drop table if exists stat_inst_para_grp;
CREATE TABLE `stat_inst_para_grp` (
  `PARA_GRP_TAG` varchar(64) NOT NULL COMMENT '输入参数组标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `INST_TAG` varchar(64) NOT NULL COMMENT '归属实例',
  `SORT` decimal(3,0) NOT NULL COMMENT '参数排序，最大值为8',
  `LOCATION_ID` decimal(10,0) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  PRIMARY KEY (`PARA_GRP_TAG`),
  KEY `IN_STAT_INST_PARA_GRP` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计实例输入参数组定义表';

drop trigger if exists TIA_STAT_INST_PARA_GRP;
DELIMITER ;;
create trigger TIA_STAT_INST_PARA_GRP AFTER INSERT on stat_inst_para_grp for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,         KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_para_grp', 'para_grp_tag', '',        NEW.PARA_GRP_TAG, getTableSubSystem('stat_inst_para_grp'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_INST_PARA_GRP;
DELIMITER ;;
create trigger TUA_STAT_INST_PARA_GRP AFTER UPDATE on stat_inst_para_grp for each ROW 
body:BEGIN
    IF NEW.PARA_GRP_TAG = OLD.PARA_GRP_TAG
    THEN LEAVE body;
    END IF;
    
    UPDATE stat_inst_para_point
    SET PARA_GRP_TAG = NEW.PARA_GRP_TAG
    WHERE PARA_GRP_TAG = OLD.PARA_GRP_TAG;
    
    UPDATE stat_inst_para_time
    SET PARA_GRP_TAG = NEW.PARA_GRP_TAG
    WHERE PARA_GRP_TAG = OLD.PARA_GRP_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,     KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_para_grp', 'para_grp_tag', OLD.PARA_GRP_TAG, NEW.PARA_GRP_TAG, getTableSubSystem('stat_inst_para_grp'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_INST_PARA_GRP;
DELIMITER ;;
create trigger TDA_STAT_INST_PARA_GRP AFTER DELETE on stat_inst_para_grp for each ROW 
BEGIN
    DELETE FROM stat_inst_para_point WHERE PARA_GRP_TAG = OLD.PARA_GRP_TAG;
    DELETE FROM stat_inst_para_time WHERE PARA_GRP_TAG = OLD.PARA_GRP_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,         KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_para_grp', 'para_grp_tag', OLD.PARA_GRP_TAG, '', getTableSubSystem('stat_inst_para_grp'));
    END IF;
END;;
DELIMITER ;

drop table if exists stat_inst_para_point;
CREATE TABLE `stat_inst_para_point` (
  `PARA_TAG` varchar(64) NOT NULL COMMENT '参数标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '参数描述',
  `RANK` decimal(3,0) NOT NULL COMMENT '参数级别 值有1和2 1为顶层参数，2有归属参数组',
  `POINT_TAG` varchar(64) DEFAULT NULL COMMENT '输入参数测点标签',
  `SORT` decimal(10,0) NOT NULL COMMENT '排序 当参数级别为1时，该列最大值为8 当参数级别为2时，该列无最大值限制',
  `PARA_GRP_TAG` varchar(64) DEFAULT NULL COMMENT '若参数级别为1，归属参数组为空 若参数级别为2，归属参数组不得为空',
  `INST_TAG` varchar(64) NOT NULL COMMENT '归属实例',
  `LOCATION_ID` decimal(10,0) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  PRIMARY KEY (`PARA_TAG`),
  KEY `IN_STAT_INST_PARA_POINT` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计实例输入参数测点类定义表';

drop trigger if exists TIA_STAT_INST_PARA_POINT;
DELIMITER ;;
create trigger TIA_STAT_INST_PARA_POINT AFTER INSERT on stat_inst_para_point for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_para_point', 'para_tag', '',        NEW.PARA_TAG, getTableSubSystem('stat_inst_para_point'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_INST_PARA_POINT;
DELIMITER ;;
create trigger TUA_STAT_INST_PARA_POINT AFTER UPDATE on stat_inst_para_point for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_para_point', 'para_tag', OLD.PARA_TAG,   NEW.PARA_TAG, getTableSubSystem('stat_inst_para_point'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_INST_PARA_POINT;
DELIMITER ;;
create trigger TDA_STAT_INST_PARA_POINT AFTER DELETE on stat_inst_para_point for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_para_point', 'para_tag', OLD.PARA_TAG, '', getTableSubSystem('stat_inst_para_point'));
    END IF;
END;;
DELIMITER ;

drop table if exists stat_inst_para_time;
CREATE TABLE `stat_inst_para_time` (
  `PARA_TAG` varchar(64) NOT NULL COMMENT '参数标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '参数描述',
  `RANK` decimal(3,0) NOT NULL COMMENT '参数级别 有1和2 1为顶层参数，2是参数组下面的二级参数，归属参数组那列需要填值',
  `TIMESTAMP` decimal(20,0) DEFAULT NULL COMMENT '时间值，时间戳，unix时间戳，时间精度s级别 10位整型 时区为东八区 如当前时间为 2019年5月7日11点52分30秒 东八区 精度为：1557201150',
  `SORT` decimal(10,0) NOT NULL COMMENT '排序 当参数级别为1时，该列最大值为8 当参数级别为2时，该列无最大值限制',
  `PARA_GRP_TAG` varchar(64) DEFAULT NULL COMMENT '若参数级别为1，归属参数组为空 若参数级别为2，归属参数组不得为空',
  `INST_TAG` varchar(64) NOT NULL COMMENT '归属实例',
  `TIME_MODE` decimal(3,0) DEFAULT NULL COMMENT '绝对模式 1  相对年（2），月（3），日（4），周（5），时（6），分（7）模式对应 2-7。 相对秒模式是8',
  `YEAR_R` decimal(10,0) DEFAULT NULL COMMENT '只有相对模式，相对年：N年前',
  `MONTH_R` decimal(10,0) DEFAULT NULL COMMENT '相对模式下：0-N 对应 N月前  绝对模式下：1-12对应 1-12月',
  `DAY_R` decimal(10,0) DEFAULT NULL COMMENT '相对模式下：0-N 对应 N日前 绝对模式下：1-31 对应N日。其中32是月初，32是月末。',
  `WEEKDAY_R` decimal(3,0) DEFAULT NULL COMMENT '相对周，只有相对模式 1-7对应周一到周日 意为上一个周几 选择周X，为当前时间上一个周X，比如说今天是周二，选定周一，则计算时间为昨天，若选择为周二，则为上一个周二（七天前），若选择周日，则为三天前',
  `HOUR_R` decimal(10,0) DEFAULT NULL COMMENT '相对模式：N个小时之前  绝对模式：0-23时',
  `MINUTE_R` decimal(10,0) DEFAULT NULL COMMENT '相对模式：N个分钟之前 绝对模式：0-59分',
  `SECOND_R` decimal(20,0) DEFAULT NULL COMMENT '相对模式：N秒之前 绝对模式：0-59秒',
  `LOCATION_ID` decimal(10,0) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  PRIMARY KEY (`PARA_TAG`),
  KEY `IN_STAT_INST_PARA_TIME` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计实例输入参数时间类定义表';

drop trigger if exists TIA_STAT_INST_PARA_TIME;
DELIMITER ;;
create trigger TIA_STAT_INST_PARA_TIME AFTER INSERT on stat_inst_para_time for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_para_time', 'para_tag', '',        NEW.PARA_TAG, getTableSubSystem('stat_inst_para_time'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_INST_PARA_TIME;
DELIMITER ;;
create trigger TUA_STAT_INST_PARA_TIME AFTER UPDATE on stat_inst_para_time for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_para_time', 'para_tag', OLD.PARA_TAG,   NEW.PARA_TAG, getTableSubSystem('stat_inst_para_time'));
    END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_INST_PARA_TIME;
DELIMITER ;;
create trigger TDA_STAT_INST_PARA_TIME AFTER DELETE on stat_inst_para_time for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_para_time', 'para_tag', OLD.PARA_TAG, '', getTableSubSystem('stat_inst_para_time'));
    END IF;
END;;
DELIMITER ;

drop table if exists stat_inst_ret_grp;
CREATE TABLE `stat_inst_ret_grp` (
  `RET_GRP_TAG` varchar(64) NOT NULL COMMENT '输出参数组标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `INST_TAG` varchar(64) NOT NULL COMMENT '归属实例',
  `SORT` decimal(10,0) NOT NULL COMMENT '无最大值限制',
  `LOCATION_ID` decimal(10,0) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  PRIMARY KEY (`RET_GRP_TAG`),
  KEY `IN_STAT_INST_RET_GRP` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计实例输出参数组定义表';

drop trigger if exists TIA_STAT_INST_RET_GRP;
DELIMITER ;;
create trigger TIA_STAT_INST_RET_GRP AFTER INSERT on stat_inst_ret_grp for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_ret_grp', 'ret_grp_tag', '',        NEW.RET_GRP_TAG, getTableSubSystem('stat_inst_ret_grp'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_INST_RET_GRP;
DELIMITER ;;
create trigger TUA_STAT_INST_RET_GRP AFTER UPDATE on stat_inst_ret_grp for each ROW 
body:BEGIN
    
    IF NEW.RET_GRP_TAG = OLD.RET_GRP_TAG
    THEN LEAVE body;
    END IF;
    
    UPDATE stat_inst_ret_para 
    SET PARA_GRP_TAG = NEW.RET_GRP_TAG
    WHERE PARA_GRP_TAG = OLD.RET_GRP_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_ret_grp', 'ret_grp_tag', OLD.RET_GRP_TAG, NEW.RET_GRP_TAG, getTableSubSystem('stat_inst_ret_grp'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_INST_RET_GRP;
DELIMITER ;;
create trigger TDA_STAT_INST_RET_GRP AFTER DELETE on stat_inst_ret_grp for each ROW 
BEGIN
    DELETE FROM stat_inst_ret_para WHERE PARA_GRP_TAG = OLD.RET_GRP_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_ret_grp', 'ret_grp_tag', OLD.RET_GRP_TAG, '', getTableSubSystem('stat_inst_ret_grp'));
    END IF;
END;;
DELIMITER ;

drop table if exists stat_inst_ret_para;
CREATE TABLE `stat_inst_ret_para` (
  `RET_TAG` varchar(64) NOT NULL COMMENT '参数标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '参数描述',
  `RANK` decimal(3,0) NOT NULL COMMENT '函数体',
  `POINT_TAG` varchar(64) DEFAULT NULL COMMENT '输出参数标签，若IS_BIND列为否，该列无效，该列为空',
  `SORT` decimal(10,0) NOT NULL COMMENT '排序 当参数级别为1时，该列最大值为8 当参数级别为2时，该列无最大值限制',
  `PARA_GRP_TAG` varchar(64) DEFAULT NULL COMMENT '若参数级别为1，归属参数组为空 若参数级别为2，归属参数组不得为空',
  `VALUE` decimal(22,6) DEFAULT NULL COMMENT '参数的值为一个定点数（不是整型） 小数点左边20位，小数点右边10位',
  `IS_BIND` decimal(3,0) NOT NULL COMMENT '是否绑定测点，默认不绑定测点',
  `INST_TAG` varchar(64) NOT NULL COMMENT '归属实例',
  `STATUS` decimal(10,0) DEFAULT NULL COMMENT '1 有效值 0 无效值',
  `LOCATION_ID` decimal(10,0) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  PRIMARY KEY (`RET_TAG`),
  KEY `IN_STAT_INST_RET_PARA` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计实例输出测点参数定义表';

drop trigger if exists TIA_STAT_INST_RET_PARA;
DELIMITER ;;
create trigger TIA_STAT_INST_RET_PARA AFTER INSERT on stat_inst_ret_para for each ROW 
BEGIN
   
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_inst_ret_para', 'ret_tag', '',        NEW.RET_TAG, getTableSubSystem('stat_inst_ret_para'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_INST_RET_PARA;
DELIMITER ;;
create trigger TUA_STAT_INST_RET_PARA AFTER UPDATE on stat_inst_ret_para for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_inst_ret_para', 'ret_tag', OLD.RET_TAG, NEW.RET_TAG, getTableSubSystem('stat_inst_ret_para'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_INST_RET_PARA;
DELIMITER ;;
create trigger TDA_STAT_INST_RET_PARA AFTER DELETE on stat_inst_ret_para for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_inst_ret_para', 'ret_tag', OLD.RET_TAG, '', getTableSubSystem('stat_inst_ret_para'));
    END IF;
    
END;;
DELIMITER ;

drop table if exists stat_link_def;
CREATE TABLE `stat_link_def` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `STAT_TEMP_TAG` varchar(64) NOT NULL COMMENT '绑定模板标签',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_STAT_LINK_DEF` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计关联表';

drop trigger if exists TIA_STAT_LINK_DEF;
DELIMITER ;;
create trigger TIA_STAT_LINK_DEF AFTER INSERT on stat_link_def for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_link_def', 'tag_name', '',        NEW.TAG_NAME, getTableSubSystem('STAT_LINK_DEF'));
    END IF; 
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_LINK_DEF;
DELIMITER ;;
create trigger TUA_STAT_LINK_DEF AFTER UPDATE on stat_link_def for each ROW 
BEGIN
    IF NEW.TAG_NAME <> OLD.TAG_NAME THEN 
    BEGIN
         UPDATE stat_link_para
         SET LINK_TAG = NEW.TAG_NAME,
         TAG_NAME = concat(NEW.TAG_NAME,'.',getAfterLastDot(TAG_NAME))
         WHERE LINK_TAG = OLD.TAG_NAME;
        UPDATE stat_link_ret
        SET LINK_TAG = NEW.TAG_NAME,
        TAG_NAME = concat(NEW.TAG_NAME,'.',getAfterLastDot(TAG_NAME))
        WHERE LINK_TAG = OLD.TAG_NAME;
    
    END;
    END IF;
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_link_def', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('stat_link_def'));
    END IF;
    UPDATE stat_link_para
    SET STAT_PARA_TAG = concat(NEW.STAT_TEMP_TAG,'.',getAfterLastDot(STAT_PARA_TAG))
    WHERE LINK_TAG = OLD.TAG_NAME;
    UPDATE stat_link_ret
    SET STAT_RET_TAG = concat(NEW.STAT_TEMP_TAG,'.',getAfterLastDot(STAT_RET_TAG))
    WHERE LINK_TAG = OLD.TAG_NAME;
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_LINK_DEF;
DELIMITER ;;
create trigger TDA_STAT_LINK_DEF AFTER DELETE on stat_link_def for each ROW 
BEGIN
    #删除对应的模板点
    DELETE FROM stat_link_para WHERE LINK_TAG= OLD.TAG_NAME;
    DELETE FROM stat_link_ret WHERE LINK_TAG = OLD.TAG_NAME;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_link_def', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('stat_link_def'));
    END IF;
    
    
END;;
DELIMITER ;

drop table if exists stat_link_para;
CREATE TABLE `stat_link_para` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `LINK_TAG` varchar(64) NOT NULL COMMENT '统计关联模板',
  `STAT_PARA_TAG` varchar(64) NOT NULL COMMENT '模板输入参数标签',
  `TEMP_NAME` varchar(64) NOT NULL COMMENT '后台模板标签',
  `TEMP_TYPE` decimal(10,0) NOT NULL COMMENT '绑定点类型',
  `TEMP_POINT_NAME` varchar(64) NOT NULL COMMENT '绑定测点模板标签',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_STAT_LINK_PARA` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计关联输入参数表';

drop trigger if exists TIA_STAT_LINK_PARA;
DELIMITER ;;
create trigger TIA_STAT_LINK_PARA AFTER INSERT on stat_link_para for each ROW 
BEGIN
   
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_link_para', 'tag_name', '',        NEW.TAG_NAME, getTableSubSystem('stat_link_para'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_LINK_PARA;
DELIMITER ;;
create trigger TUA_STAT_LINK_PARA AFTER UPDATE on stat_link_para for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_link_para', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('stat_link_para'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_LINK_PARA;
DELIMITER ;;
create trigger TDA_STAT_LINK_PARA AFTER DELETE on stat_link_para for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_link_para', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('stat_link_para'));
    END IF;
    
END;;
DELIMITER ;

drop table if exists stat_link_ret;
CREATE TABLE `stat_link_ret` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `LINK_TAG` varchar(64) NOT NULL COMMENT '统计关联',
  `STAT_RET_TAG` varchar(64) NOT NULL COMMENT '模板输出参数标签',
  `TEMP_NAME` varchar(64) NOT NULL COMMENT '后台模板标签',
  `TEMP_TYPE` decimal(10,0) NOT NULL COMMENT '绑定点类型',
  `TEMP_POINT_NAME` varchar(64) NOT NULL COMMENT '绑定测点模板标签',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_STAT_LINK_RET` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计关联输出参数表';

drop trigger if exists TIA_STAT_LINK_RET;
DELIMITER ;;
create trigger TIA_STAT_LINK_RET AFTER INSERT on stat_link_ret for each ROW 
BEGIN
   
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_link_ret', 'tag_name', '',        NEW.TAG_NAME, getTableSubSystem('stat_link_ret'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_LINK_RET;
DELIMITER ;;
create trigger TUA_STAT_LINK_RET AFTER UPDATE on stat_link_ret for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_link_ret', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('stat_link_ret'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_LINK_RET;
DELIMITER ;;
create trigger TDA_STAT_LINK_RET AFTER DELETE on stat_link_ret for each ROW 
BEGIN
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_link_ret', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('stat_link_ret'));
    END IF;
    
END;;
DELIMITER ;

drop table if exists stat_template_def;
CREATE TABLE `stat_template_def` (
  `TEMPLATE_TAG` varchar(64) NOT NULL COMMENT '统计模板标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '统计模板描述',
  `FUNCTION_BODY` varchar(8192) DEFAULT NULL COMMENT '函数体',
  `FUNCTION_HEADER_CACHE` varchar(1024) DEFAULT NULL COMMENT '函数头，建模过程缓存加速用',
  `FUNCTION_RETURN_CACHE` varchar(2048) DEFAULT NULL COMMENT '函数返回值，建模过程缓存加速用',
  `IS_MOD` decimal(10,0) NOT NULL COMMENT '是否修改',
  PRIMARY KEY (`TEMPLATE_TAG`),
  KEY `IN_STAT_TEMPLATE_DEF` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计模板定义表';

drop trigger if exists TIA_STAT_TEMPLATE_DEF;
DELIMITER ;;
create trigger TIA_STAT_TEMPLATE_DEF AFTER INSERT on stat_template_def for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_template_def', 'template_tag', '',        NEW.TEMPLATE_TAG, getTableSubSystem('stat_template_def'));
    END IF;
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_TEMPLATE_DEF;
DELIMITER ;;
create trigger TUA_STAT_TEMPLATE_DEF AFTER UPDATE on stat_template_def for each ROW 
BEGIN
    UPDATE stat_template_para
    SET TEMPLATE_TAG = NEW.TEMPLATE_TAG,
    PARA_TAG = concat(NEW.TEMPLATE_TAG,'.',getAfterLastDot(PARA_TAG))
    WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    
    UPDATE stat_template_ret
    SET TEMPLATE_TAG = NEW.TEMPLATE_TAG,
    RET_TAG = concat(NEW.TEMPLATE_TAG,'.',getAfterLastDot(RET_TAG))
    WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    
    UPDATE stat_inst_def
    SET TEMPLATE_TAG = NEW.TEMPLATE_TAG
    WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    UPDATE stat_link_def
    SET STAT_TEMP_TAG = NEW.TEMPLATE_TAG
    WHERE STAT_TEMP_TAG = OLD.TEMPLATE_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_template_def', 'template_tag', OLD.TEMPLATE_TAG, NEW.TEMPLATE_TAG, getTableSubSystem('stat_template_def'));
    END IF;
    
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_TEMPLATE_DEF;
DELIMITER ;;
create trigger TDA_STAT_TEMPLATE_DEF AFTER DELETE on stat_template_def for each ROW 
BEGIN
    #删除对应关联
    DELETE FROM stat_link_def WHERE STAT_TEMP_TAG = OLD.TEMPLATE_TAG;
    
    #删除对应实例
    DELETE FROM stat_inst_def WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    
    #删除对应的模板点
    DELETE FROM stat_template_para WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    DELETE FROM stat_template_ret WHERE TEMPLATE_TAG = OLD.TEMPLATE_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_template_def', 'template_tag', OLD.TEMPLATE_TAG, '', getTableSubSystem('stat_template_def'));
    END IF;
    
    
END;;
DELIMITER ;

drop table if exists stat_template_para;
CREATE TABLE `stat_template_para` (
  `PARA_TAG` varchar(64) NOT NULL COMMENT '统计模板参数标签',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '统计模板参数描述',
  `TEMPLATE_TAG` varchar(64) NOT NULL COMMENT '归属统计模板标签',
  `ISGRP` decimal(3,0) NOT NULL COMMENT '是否为参数组，初始值为参数',
  `ARG_NAME` varchar(128) NOT NULL COMMENT '公式输入参数名，用来在公式中使用的参数',
  `TYPE` decimal(3,0) NOT NULL COMMENT '参数类型，类型有两种，一种测点，一种时间 当值为1为测点，2为时间',
  `SORT` decimal(3,0) NOT NULL COMMENT '从1开始递增，最多不超过8（可以为8）',
  PRIMARY KEY (`PARA_TAG`),
  KEY `IN_STAT_TEMPLATE_PARA` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计模板参数定义表';

drop trigger if exists TIA_STAT_TEMPLATE_PARA;
DELIMITER ;;
create trigger TIA_STAT_TEMPLATE_PARA AFTER INSERT on stat_template_para for each ROW 
BEGIN
    #插入对应的实例
    INSERT INTO stat_inst_para_grp 
    (PARA_GRP_TAG,                                        DESCRIPTION,  INST_TAG,      SORT,     LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)) ,  NEW.DESCRIPTION, INST_TAG,    NEW.SORT,LOCATION_ID,SUB_SYSTEM 
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 1;
    
    INSERT INTO stat_inst_para_point 
    (PARA_TAG,                                         DESCRIPTION,   `RANK`,  POINT_TAG,  SORT,    PARA_GRP_TAG, INST_TAG,LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION,  1,     '',        NEW.SORT, '',          INST_TAG,LOCATION_ID,SUB_SYSTEM
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.`TYPE`,0) = 1;
    
    INSERT INTO stat_inst_para_time 
    (PARA_TAG,                                         DESCRIPTION,    `RANK`, `TIMESTAMP`,            SORT,    PARA_GRP_TAG, INST_TAG,    TIME_MODE, YEAR_R, MONTH_R, DAY_R, WEEKDAY_R, HOUR_R, MINUTE_R, SECOND_R,LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION, 1,       unix_timestamp(now()),NEW.SORT, '',           INST_TAG,    0,        0,      0,       0,      0,         0,      0,        0,      LOCATION_ID,SUB_SYSTEM
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.`TYPE`,0) = 2;
    
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_template_para', 'para_tag', '',        NEW.PARA_TAG, getTableSubSystem('stat_template_para'));
    END IF;
    
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_TEMPLATE_PARA;
DELIMITER ;;
create trigger TUA_STAT_TEMPLATE_PARA AFTER UPDATE on stat_template_para for each ROW 
body:BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_template_para', 'para_tag', OLD.PARA_TAG, NEW.PARA_TAG, getTableSubSystem('stat_template_para'));
    END IF;
    
    UPDATE stat_link_para
    SET STAT_PARA_TAG = concat(OLD.TEMPLATE_TAG,'.',getAfterLastDot(NEW.PARA_TAG))
    WHERE STAT_PARA_TAG = concat(OLD.TEMPLATE_TAG,'.',getAfterLastDot(OLD.PARA_TAG));
    
    IF OLD.PARA_TAG = NEW.PARA_TAG AND OLD.DESCRIPTION = NEW.DESCRIPTION AND OLD.ISGRP = NEW.ISGRP AND OLD.`TYPE` = NEW.`TYPE`
    AND OLD.SORT = NEW.SORT
    THEN LEAVE body;
    END IF;
    
    
    #更新对应的实例点
    #考虑从分组到不分组,从测点类型到时间类型,需删除旧的数据
    IF OLD.ISGRP = 1 AND NEW.ISGRP = 0 THEN
    BEGIN   
        DELETE stat_inst_para_grp FROM stat_inst_def,stat_inst_para_grp 
        WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
        AND stat_inst_para_grp.INST_TAG = stat_inst_def.INST_TAG
        AND getAfterLastDot(stat_inst_para_grp.PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG);
        
        IF NEW.`TYPE` = 1 THEN
            INSERT INTO stat_inst_para_point 
            (PARA_TAG,                                         DESCRIPTION,   `RANK`,  POINT_TAG,  SORT,    PARA_GRP_TAG, INST_TAG,LOCATION_ID,SUB_SYSTEM)
            SELECT 
            concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION,  1,     '',        NEW.SORT, '',          INST_TAG,LOCATION_ID,SUB_SYSTEM
            FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.`TYPE`,0) = 1;
        ELSEIF NEW.`TYPE` = 2 THEN
            INSERT INTO stat_inst_para_time 
            (PARA_TAG,                                         DESCRIPTION,    `RANK`, `TIMESTAMP`,            SORT,    PARA_GRP_TAG, INST_TAG,    TIME_MODE, YEAR_R, MONTH_R, DAY_R, WEEKDAY_R, HOUR_R, MINUTE_R, SECOND_R,LOCATION_ID,SUB_SYSTEM)
            SELECT 
            concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION, 1,       unix_timestamp(now()),NEW.SORT, '',           INST_TAG,    0,        0,      0,       0,      0,         0,      0,        0,      LOCATION_ID,SUB_SYSTEM
            FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.`TYPE`,0) = 2;
        END IF; 
    END;
    ELSEIF OLD.ISGRP = 0 AND NEW.ISGRP = 1 THEN
    BEGIN
        IF OLD.`TYPE` = 1 THEN 
            DELETE stat_inst_para_point FROM stat_inst_def,stat_inst_para_point 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_para_point.INST_TAG = stat_inst_def.INST_TAG
            AND getAfterLastDot(stat_inst_para_point.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
        ELSEIF OLD.`TYPE` = 2 THEN
            DELETE stat_inst_para_time FROM stat_inst_def,stat_inst_para_time 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_para_time.INST_TAG = stat_inst_def.INST_TAG
            AND getAfterLastDot(stat_inst_para_time.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
        END IF;
        
        INSERT INTO stat_inst_para_grp 
        (PARA_GRP_TAG,                                        DESCRIPTION,    INST_TAG,      SORT,   LOCATION_ID,SUB_SYSTEM)
        SELECT 
        concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)) ,  NEW.DESCRIPTION, INST_TAG,    NEW.SORT,LOCATION_ID,SUB_SYSTEM 
        FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 1;
            
    END;
    ELSEIF OLD.ISGRP = NEW.ISGRP AND OLD.ISGRP = 0 THEN
    BEGIN
        IF OLD.`TYPE` = 1 AND NEW.`TYPE` = 2 THEN 
        BEGIN
            DELETE stat_inst_para_point FROM stat_inst_def,stat_inst_para_point 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_para_point.INST_TAG = stat_inst_def.INST_TAG
            AND getAfterLastDot(stat_inst_para_point.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
            
            INSERT INTO stat_inst_para_time 
            (PARA_TAG,                                         DESCRIPTION,    `RANK`, `TIMESTAMP`,            SORT,    PARA_GRP_TAG, INST_TAG,    TIME_MODE, YEAR_R, MONTH_R, DAY_R, WEEKDAY_R, HOUR_R, MINUTE_R, SECOND_R,LOCATION_ID,SUB_SYSTEM)
            SELECT 
            concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION, 1,       unix_timestamp(now()),NEW.SORT, '',           INST_TAG,    0,        0,      0,       0,      0,         0,      0,        0,      LOCATION_ID,SUB_SYSTEM
            FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.`TYPE`,0) = 2;
        END;
        ELSEIF OLD.`TYPE` = 2 AND NEW.`TYPE` = 1 THEN 
        BEGIN
            DELETE stat_inst_para_time FROM stat_inst_def,stat_inst_para_time 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_para_time.INST_TAG = stat_inst_def.INST_TAG
            AND getAfterLastDot(stat_inst_para_time.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
            
            INSERT INTO stat_inst_para_point 
            (PARA_TAG,                                         DESCRIPTION,   `RANK`,  POINT_TAG,  SORT,    PARA_GRP_TAG, INST_TAG,LOCATION_ID,SUB_SYSTEM)
            SELECT 
            concat(INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),NEW.DESCRIPTION,  1,     '',        NEW.SORT, '',          INST_TAG,LOCATION_ID,SUB_SYSTEM
            FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0 AND COALESCE(NEW.`TYPE`,0) = 1;
        END;
        ELSEIF OLD.`TYPE` = NEW.`TYPE` AND OLD.`TYPE` = 1 THEN 
           UPDATE stat_inst_para_point INNER JOIN stat_inst_def
           ON stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_para_point.INST_TAG 
           AND `RANK` = 1 AND getAfterLastDot(PARA_TAG) = getAfterLastDot(OLD.PARA_TAG)
           SET stat_inst_para_point.PARA_TAG = concat(stat_inst_para_point.INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),
               stat_inst_para_point.DESCRIPTION = NEW.DESCRIPTION,
               stat_inst_para_point.SORT = NEW.SORT;
       ELSEIF OLD.`TYPE` = NEW.`TYPE` AND OLD.`TYPE` = 2 THEN 
           UPDATE stat_inst_para_time INNER JOIN stat_inst_def
           ON stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_para_time.INST_TAG 
           AND `RANK` = 1 AND getAfterLastDot(PARA_TAG) = getAfterLastDot(OLD.PARA_TAG)
           SET stat_inst_para_time.PARA_TAG = concat(stat_inst_para_time.INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),
               stat_inst_para_time.DESCRIPTION = NEW.DESCRIPTION,
               stat_inst_para_time.SORT = NEW.SORT; 
       END IF;
    END;
    ELSEIF OLD.ISGRP = NEW.ISGRP AND OLD.ISGRP = 1 THEN 
    BEGIN
        IF OLD.`TYPE` = 1 AND NEW.`TYPE` = 2 THEN 
        BEGIN
            DELETE stat_inst_para_point FROM stat_inst_def,stat_inst_para_grp,stat_inst_para_point 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_def.INST_TAG = stat_inst_para_grp.INST_TAG
            AND getAfterLastDot(stat_inst_para_grp.PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG)
            AND stat_inst_para_point.PARA_GRP_TAG = stat_inst_para_grp.PARA_GRP_TAG;
        END;
        ELSEIF OLD.`TYPE` = 2 AND NEW.`TYPE` = 1 THEN
        BEGIN
            DELETE stat_inst_para_time FROM stat_inst_def,stat_inst_para_grp,stat_inst_para_time 
            WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
            AND stat_inst_def.INST_TAG = stat_inst_para_grp.INST_TAG
            AND getAfterLastDot(stat_inst_para_grp.PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG)
            AND stat_inst_para_time.PARA_GRP_TAG = stat_inst_para_grp.PARA_GRP_TAG;
        END;
        END IF;
       
        
        UPDATE stat_inst_para_grp INNER JOIN  stat_inst_def
        ON stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_para_grp.INST_TAG 
        AND getAfterLastDot(PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG)
        SET stat_inst_para_grp.PARA_GRP_TAG = concat(stat_inst_para_grp.INST_TAG,'.',getAfterLastDot(NEW.PARA_TAG)),
            stat_inst_para_grp.DESCRIPTION = NEW.DESCRIPTION,
            stat_inst_para_grp.SORT = NEW.SORT;
    END; 
    END IF;
    
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_TEMPLATE_PARA;
DELIMITER ;;
create trigger TDA_STAT_TEMPLATE_PARA AFTER DELETE on stat_template_para for each ROW 
BEGIN
    #删除相应的实例点
    DELETE stat_inst_para_grp FROM stat_inst_def,stat_inst_para_grp 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_para_grp.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_para_grp.PARA_GRP_TAG) = getAfterLastDot(OLD.PARA_TAG);
    
    DELETE stat_inst_para_point FROM stat_inst_def,stat_inst_para_point 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_para_point.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_para_point.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
    
    DELETE stat_inst_para_time FROM stat_inst_def,stat_inst_para_time 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_para_time.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_para_time.PARA_TAG) = getAfterLastDot(OLD.PARA_TAG);
    DELETE FROM stat_link_para 
    WHERE STAT_PARA_TAG = OLD.PARA_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_template_para', 'para_tag', OLD.PARA_TAG, '', getTableSubSystem('stat_template_para'));
    END IF;
    
 
    
END;;
DELIMITER ;

drop table if exists stat_template_ret;
CREATE TABLE `stat_template_ret` (
  `RET_TAG` varchar(64) NOT NULL COMMENT '统计模板输出参数定义',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `TEMPLATE_TAG` varchar(64) NOT NULL COMMENT '绑定所属的统计模板标签',
  `RET_NAME` varchar(64) NOT NULL COMMENT '公式返回值参数名',
  `ISGRP` decimal(3,0) NOT NULL COMMENT '是否为参数组，默认值为参数',
  `SORT` decimal(10,0) NOT NULL COMMENT '从1开始递增，无最大值限制',
  PRIMARY KEY (`RET_TAG`),
  KEY `IN_STAT_TEMPLATE_RET` (`DESCRIPTION`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='统计模板输出参数定义（返回值）';

drop trigger if exists TIA_STAT_TEMPLATE_RET;
DELIMITER ;;
create trigger TIA_STAT_TEMPLATE_RET AFTER INSERT on stat_template_ret for each ROW 
BEGIN
    #插入相应实例点
    INSERT INTO stat_inst_ret_grp 
    (RET_GRP_TAG,                                        DESCRIPTION,     INST_TAG,  SORT,      LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)) ,  NEW.DESCRIPTION, INST_TAG,    NEW.SORT,LOCATION_ID,SUB_SYSTEM 
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 1;
    
    INSERT INTO stat_inst_ret_para 
    (RET_TAG,                                          DESCRIPTION,    `RANK`, POINT_TAG, SORT,    PARA_GRP_TAG, VALUE, IS_BIND, INST_TAG,LOCATION_ID,SUB_SYSTEM)
    SELECT 
    concat(INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)) ,NEW.DESCRIPTION, 1,      '',       NEW.SORT,'',            0,     0,      INST_TAG,LOCATION_ID,SUB_SYSTEM   
    FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,  SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('insert'),'stat_template_ret', 'ret_tag', '',        NEW.RET_TAG, getTableSubSystem('stat_template_ret'));
    END IF;
    
    
    
END;;
DELIMITER ;

drop trigger if exists TUA_STAT_TEMPLATE_RET;
DELIMITER ;;
create trigger TUA_STAT_TEMPLATE_RET AFTER UPDATE on stat_template_ret for each ROW 
body:BEGIN
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,          KEY_COLUMN_NAME,KEY_OLD_VALUE,  KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('update'),'stat_template_ret', 'ret_tag', OLD.RET_TAG, NEW.RET_TAG, getTableSubSystem('stat_template_ret'));
    END IF;
    
    UPDATE stat_link_ret
    SET STAT_RET_TAG = concat(OLD.TEMPLATE_TAG,'.',getAfterLastDot(NEW.RET_TAG))
    WHERE STAT_RET_TAG = concat(OLD.TEMPLATE_TAG,'.',getAfterLastDot(OLD.RET_TAG));    
    
    IF OLD.RET_TAG = NEW.RET_TAG AND OLD.DESCRIPTION = NEW.DESCRIPTION AND OLD.ISGRP = NEW.ISGRP AND OLD.SORT = NEW.SORT
    THEN LEAVE body;
    END IF;
    
    #更新相应的实例点
    IF OLD.ISGRP = 1 AND NEW.ISGRP = 0 THEN
    BEGIN
        DELETE stat_inst_ret_grp FROM stat_inst_def,stat_inst_ret_grp 
        WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
        AND stat_inst_ret_grp.INST_TAG = stat_inst_def.INST_TAG
        AND getAfterLastDot(stat_inst_ret_grp.RET_GRP_TAG) = getAfterLastDot(OLD.RET_TAG);
        
        INSERT INTO stat_inst_ret_para 
        (RET_TAG,                                          DESCRIPTION,    `RANK`, POINT_TAG, SORT,    PARA_GRP_TAG, VALUE, IS_BIND, INST_TAG,LOCATION_ID,SUB_SYSTEM)
        SELECT 
        concat(INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)) ,NEW.DESCRIPTION, 1,      '',       NEW.SORT,'',            0,     0,      INST_TAG,LOCATION_ID,SUB_SYSTEM   
        FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 0;
    END;
    ELSEIF OLD.ISGRP = 0 AND NEW.ISGRP = 1 THEN
    BEGIN
        DELETE stat_inst_ret_para FROM stat_inst_def,stat_inst_ret_para 
        WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
        AND stat_inst_ret_para.INST_TAG = stat_inst_def.INST_TAG
        AND getAfterLastDot(stat_inst_ret_para.RET_TAG) = getAfterLastDot(OLD.RET_TAG);
        
        INSERT INTO stat_inst_ret_grp 
        (RET_GRP_TAG,                                        DESCRIPTION,     INST_TAG,  SORT,      LOCATION_ID,SUB_SYSTEM)
        SELECT 
        concat(INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)) ,  NEW.DESCRIPTION, INST_TAG,    NEW.SORT,LOCATION_ID,SUB_SYSTEM 
        FROM stat_inst_def WHERE TEMPLATE_TAG = NEW.TEMPLATE_TAG AND COALESCE(NEW.ISGRP,0) = 1;
    END;
    ELSEIF OLD.ISGRP = NEW.ISGRP AND OLD.ISGRP = 0 THEN
    BEGIN
        UPDATE stat_inst_ret_para INNER JOIN stat_inst_def
        ON stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_ret_para.INST_TAG
        AND `RANK` = 1 AND getAfterLastDot(RET_TAG) = getAfterLastDot(OLD.RET_TAG)
        SET stat_inst_ret_para.RET_TAG = concat(stat_inst_ret_para.INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)),
            stat_inst_ret_para.DESCRIPTION = NEW.DESCRIPTION,
            stat_inst_ret_para.SORT = NEW.SORT;
    END;
    ELSEIF OLD.ISGRP = NEW.ISGRP AND OLD.ISGRP = 1 THEN
    BEGIN
        UPDATE stat_inst_ret_grp INNER JOIN stat_inst_def
        ON stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG AND stat_inst_def.INST_TAG = stat_inst_ret_grp.INST_TAG
        AND getAfterLastDot(RET_GRP_TAG) = getAfterLastDot(OLD.RET_TAG)
        SET stat_inst_ret_grp.RET_GRP_TAG = concat(stat_inst_ret_grp.INST_TAG,'.',getAfterLastDot(NEW.RET_TAG)),
            stat_inst_ret_grp.DESCRIPTION = NEW.DESCRIPTION,
            stat_inst_ret_grp.SORT = NEW.SORT;
    END; 
    END IF;
    
    
    
END;;
DELIMITER ;

drop trigger if exists TDA_STAT_TEMPLATE_RET;
DELIMITER ;;
create trigger TDA_STAT_TEMPLATE_RET AFTER DELETE on stat_template_ret for each ROW 
BEGIN
    #删除相应的实例点
    DELETE stat_inst_ret_grp FROM stat_inst_def,stat_inst_ret_grp 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_ret_grp.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_ret_grp.RET_GRP_TAG) = getAfterLastDot(OLD.RET_TAG);
    
    DELETE stat_inst_ret_para FROM stat_inst_def,stat_inst_ret_para 
    WHERE stat_inst_def.TEMPLATE_TAG = OLD.TEMPLATE_TAG
    AND stat_inst_ret_para.INST_TAG = stat_inst_def.INST_TAG
    AND getAfterLastDot(stat_inst_ret_para.RET_TAG) = getAfterLastDot(OLD.RET_TAG);
    DELETE FROM stat_link_ret
    WHERE STAT_RET_TAG = OLD.RET_TAG;
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,            KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
    VALUES                      (getMaxTriggerId(),getOpType('delete'),'stat_template_ret', 'ret_tag', OLD.RET_TAG, '', getTableSubSystem('stat_template_ret'));
    END IF;
    
  
END;;
DELIMITER ;

drop table if exists switch_cfg_info;
CREATE TABLE `switch_cfg_info` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `NIC1_ADDRESS` varchar(32) DEFAULT NULL COMMENT 'IP地址',
  `NIC1_NETMASK` varchar(32) DEFAULT NULL COMMENT '子网掩码',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属位置',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='交换机配置定义表';

drop table if exists t_sys_table_file_tree;
CREATE TABLE `t_sys_table_file_tree` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `TREECODE` varchar(255) DEFAULT NULL COMMENT '节点编码',
  `TREEPCODE` varchar(255) DEFAULT NULL COMMENT '父节点编号',
  `NAME` varchar(255) DEFAULT NULL COMMENT '文件或者文件夹名称',
  `PATH` varchar(255) DEFAULT NULL COMMENT '文件或者文件夹路径',
  `TYPE` decimal(10,0) DEFAULT NULL COMMENT '类型',
  `CREATETIME` varchar(255) DEFAULT NULL COMMENT '创建时间',
  `CREATEOPERATOR` varchar(255) DEFAULT NULL COMMENT '创建操作人',
  `LASTUPDATETIME` varchar(255) DEFAULT NULL COMMENT '最后更新时间',
  `LASTUPDATEOPERATOR` varchar(255) DEFAULT NULL COMMENT '最后更新操作人',
  `startTime` varchar(255) DEFAULT NULL COMMENT '开始时间',
  `endTime` varchar(255) DEFAULT NULL COMMENT '结束时间',
  `timeType` varchar(255) DEFAULT NULL COMMENT '时间类型',
  `sourceType` varchar(255) DEFAULT NULL COMMENT '源类型',
  `devlist` varchar(255) DEFAULT NULL COMMENT '设备列表',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

drop table if exists t_w_inspection;
CREATE TABLE `t_w_inspection` (
  `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '巡检编号',
  `CODE` varchar(255) NOT NULL COMMENT '巡检设备代码',
  `NAME` varchar(255) NOT NULL COMMENT '巡检设备名称',
  `INSPECTION_TYPE` decimal(10,0) DEFAULT NULL COMMENT '巡检类型',
  `DESCRIPTION` varchar(255) DEFAULT NULL COMMENT '巡检内容',
  `IMAGEURL` varchar(512) DEFAULT NULL COMMENT '附图路径',
  `UPTIME` varchar(255) DEFAULT NULL COMMENT '上传时间',
  `AUTHOR` varchar(255) DEFAULT NULL COMMENT '上传者',
  `ISHANDLE` decimal(10,0) DEFAULT NULL COMMENT '处理结果',
  `HANDLER` varchar(255) DEFAULT NULL COMMENT '处理人',
  `CABINETTYPE` varchar(255) DEFAULT NULL COMMENT '柜子类型',
  `CABINETTYPENAME` varchar(255) DEFAULT NULL COMMENT '柜子名称',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备巡检表';

drop table if exists trigger_condition_define;
CREATE TABLE `trigger_condition_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '触发器标签',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `EXE_TYPE` decimal(10,0) DEFAULT NULL COMMENT '执行方式',
  `IS_REPEAT` decimal(10,0) DEFAULT NULL COMMENT '是否重复',
  `SPACE_TIME` decimal(10,0) DEFAULT NULL COMMENT '间隔时间',
  `FUNCTION_NAME` varchar(64) DEFAULT NULL COMMENT '触发函数名',
  `PARAM_COUNT` decimal(10,0) DEFAULT NULL COMMENT '参数个数',
  `PARAM_1` varchar(64) DEFAULT NULL COMMENT '点参数(车站.专业.表名.点名)',
  `PARAM_2` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_3` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_4` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_5` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_6` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_7` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_8` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_9` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_10` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_11` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_12` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_13` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_14` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_15` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_16` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_17` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_18` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_19` varchar(64) DEFAULT NULL COMMENT '点参数',
  `PARAM_20` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='条件触发定义表';

drop trigger if exists TIA_TRIGGER_CONDITION_DEFINE;
DELIMITER ;;
create trigger TIA_TRIGGER_CONDITION_DEFINE AFTER INSERT on trigger_condition_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'trigger_condition_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('trigger_condition_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_TRIGGER_CONDITION_DEFINE;
DELIMITER ;;
create trigger TUA_TRIGGER_CONDITION_DEFINE AFTER UPDATE on trigger_condition_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'trigger_condition_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('trigger_condition_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_TRIGGER_CONDITION_DEFINE;
DELIMITER ;;
create trigger TDA_TRIGGER_CONDITION_DEFINE AFTER DELETE on trigger_condition_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'trigger_condition_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('trigger_condition_define'));
END IF;
END;;
DELIMITER ;

drop table if exists trigger_function_define;
CREATE TABLE `trigger_function_define` (
  `FUNC_NAME` varchar(64) NOT NULL COMMENT '函数名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `FUNC_STR` varchar(4096) DEFAULT NULL COMMENT '函数内容',
  PRIMARY KEY (`FUNC_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='触发函数定义表';

drop trigger if exists TIA_TRIGGER_FUNCTION_DEFINE;
DELIMITER ;;
create trigger TIA_TRIGGER_FUNCTION_DEFINE AFTER INSERT on trigger_function_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'trigger_function_define', 'func_name', '', NEW.FUNC_NAME, getTableSubSystem('trigger_function_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_TRIGGER_FUNCTION_DEFINE;
DELIMITER ;;
create trigger TUA_TRIGGER_FUNCTION_DEFINE AFTER UPDATE on trigger_function_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'trigger_function_define', 'func_name', OLD.FUNC_NAME, NEW.FUNC_NAME, getTableSubSystem('trigger_function_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_TRIGGER_FUNCTION_DEFINE;
DELIMITER ;;
create trigger TDA_TRIGGER_FUNCTION_DEFINE AFTER DELETE on trigger_function_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'trigger_function_define', 'func_name', OLD.FUNC_NAME, '', getTableSubSystem('trigger_function_define'));
END IF;
END;;
DELIMITER ;

drop table if exists trigger_period_define;
CREATE TABLE `trigger_period_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '触发器名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `PERIOD_TYPE` decimal(10,0) DEFAULT NULL COMMENT '周期类型(每年每月每周每天每小时)',
  `START_TIME` decimal(20,0) DEFAULT NULL COMMENT '开始时间',
  `STOP_TIME` decimal(20,0) DEFAULT NULL COMMENT '结束时间',
  `PERIOD_STR` varchar(128) DEFAULT NULL COMMENT '周期内容串',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='周期触发器定义表';

drop trigger if exists TIA_TRIGGER_PERIOD_DEFINE;
DELIMITER ;;
create trigger TIA_TRIGGER_PERIOD_DEFINE AFTER INSERT on trigger_period_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'trigger_period_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('trigger_period_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_TRIGGER_PERIOD_DEFINE;
DELIMITER ;;
create trigger TUA_TRIGGER_PERIOD_DEFINE AFTER UPDATE on trigger_period_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'trigger_period_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('trigger_period_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_TRIGGER_PERIOD_DEFINE;
DELIMITER ;;
create trigger TDA_TRIGGER_PERIOD_DEFINE AFTER DELETE on trigger_period_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'trigger_period_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('trigger_period_define'));
END IF;
END;;
DELIMITER ;

drop table if exists trigger_timer_define;
CREATE TABLE `trigger_timer_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '触发器名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '描述',
  `TRIGGER_TIME` decimal(20,0) DEFAULT NULL COMMENT '触发时间',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='定时触发器定义表';

drop trigger if exists TIA_TRIGGER_TIMER_DEFINE;
DELIMITER ;;
create trigger TIA_TRIGGER_TIMER_DEFINE AFTER INSERT on trigger_timer_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('insert'),'trigger_timer_define', 'tag_name', '', NEW.TAG_NAME, getTableSubSystem('trigger_timer_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TUA_TRIGGER_TIMER_DEFINE;
DELIMITER ;;
create trigger TUA_TRIGGER_TIMER_DEFINE AFTER UPDATE on trigger_timer_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('update'),'trigger_timer_define', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('trigger_timer_define'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_TRIGGER_TIMER_DEFINE;
DELIMITER ;;
create trigger TDA_TRIGGER_TIMER_DEFINE AFTER DELETE on trigger_timer_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT INTO sys_trigger_info(ACT_ID,           OP_TYPE,            TABLE_NAME,KEY_COLUMN_NAME,KEY_OLD_VALUE,KEY_NEW_VALUE,SUBSYSTEM_FLAG)
VALUES                      (getMaxTriggerId(),getOpType('delete'),'trigger_timer_define', 'tag_name', OLD.TAG_NAME, '', getTableSubSystem('trigger_timer_define'));
END IF;
END;;
DELIMITER ;

drop table if exists accuml;
CREATE TABLE `accuml` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `DEVICE` varchar(64) DEFAULT NULL COMMENT '点设备名称',
  `SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '测点顺序号',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT 'RTU名',
  `GRAY` decimal(22,6) DEFAULT NULL COMMENT '灰度',
  `CONTRAST` decimal(22,6) DEFAULT NULL COMMENT '对比度',
  `QCOLOR` decimal(10,0) DEFAULT NULL COMMENT '拓扑着色',
  `RAW_VALUE` decimal(20,0) DEFAULT NULL COMMENT '原始值',
  `VALUE` decimal(22,6) DEFAULT NULL COMMENT '实时值double',
  `STATUS` decimal(10,0) DEFAULT NULL COMMENT '状态',
  `LAST_UPDATE_TIME` decimal(20,0) DEFAULT NULL COMMENT '最后更新时间',
  `LAST_CHANGE_TIME` decimal(20,0) DEFAULT NULL COMMENT '最后变化时间',
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位ID',
  `SOUND_NAME1` varchar(32) DEFAULT NULL COMMENT '告警声音文件1',
  `SOUND_NAME2` varchar(32) DEFAULT NULL COMMENT '告警声音文件2',
  `SOUND_NAME3` varchar(32) DEFAULT NULL COMMENT '告警声音文件3',
  `REGION_ID` decimal(10,0) DEFAULT NULL COMMENT '责任区',
  `OPT_HANDOVER_GROUP` varchar(64) DEFAULT NULL COMMENT '操作权限移交组',
  `IS_LIMIT` decimal(10,0) DEFAULT NULL COMMENT '是否越限告警',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类型',
  `POINT_CLASS` decimal(10,0) DEFAULT NULL COMMENT '测点类别',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点属性（采集|计算|虚拟点）',
  `POINT_SORT` decimal(10,0) DEFAULT NULL COMMENT '测点种类',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型ID',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否变化存盘',
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL COMMENT '是否统计',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL COMMENT '采用周期(分钟)',
  `SAMPLE_DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '采样死区',
  `CAMERA_TAG` varchar(64) DEFAULT NULL COMMENT '摄像头标签',
  `PRESET_ID` varchar(64) DEFAULT NULL COMMENT '预置点ID',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_ACCUML` (`DEVICE`,`RTU_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='累计量参数表';

drop trigger if exists TI_ACCUML;
DELIMITER ;;
create trigger TI_ACCUML AFTER INSERT on accuml for each ROW 
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;
END;;
DELIMITER ;

drop trigger if exists TU_ACCUML;
DELIMITER ;;
create trigger TU_ACCUML AFTER UPDATE on accuml for each ROW 
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 
        accuml_limit_info.TAG_NAME = NEW.TAG_NAME,
        accuml_limit_info.LOCATION_ID = NEW.LOCATION_ID,
        accuml_limit_info.SUB_SYSTEM = NEW.SUB_SYSTEM,
        accuml_limit_info.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 
        sample_define.TAG_NAME =   NEW.TAG_NAME ,
        sample_define.DESCRIPTION = NEW.DESCRIPTION ,    
        sample_define.LOCATION_ID = NEW.LOCATION_ID ,  
        sample_define.SUB_SYSTEM = NEW.SUB_SYSTEM ,
        sample_define.DEVICE_TAG = NEW.DEVICE,
        sample_define.POINT_TYPE = NEW.POINT_TYPE ,     
        sample_define.SAMPLE_PERIOD = NEW.SAMPLE_PERIOD , 
        sample_define.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;
END;;
DELIMITER ;

drop trigger if exists TD_ACCUML;
DELIMITER ;;
create trigger TD_ACCUML BEFORE DELETE on accuml for each ROW 
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);
END;;
DELIMITER ;

drop trigger if exists TDA_ACCUML;
DELIMITER ;;
create trigger TDA_ACCUML AFTER DELETE on accuml for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists acs_device_info;
CREATE TABLE `acs_device_info` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '设备唯一标识',
  `IP_ADDR` varchar(32) NOT NULL COMMENT '设备IP地址',
  `MNG_PORT` decimal(10,0) NOT NULL COMMENT '远程管理端口号',
  `MNG_USER` varchar(64) NOT NULL COMMENT '管理用户名',
  `MNG_PASSWD` varchar(32) NOT NULL COMMENT '用户名对应的密码',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '设备所属厂站',
  `DESCRIPTION` varchar(64) DEFAULT NULL COMMENT '描述，由用户输入',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='AccessControlSystem设备信息表';

drop trigger if exists TIA_ACS_DEVICE_INFO;
DELIMITER ;;
create trigger TIA_ACS_DEVICE_INFO AFTER INSERT on acs_device_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ACS_DEVICE_INFO;
DELIMITER ;;
create trigger TUA_ACS_DEVICE_INFO AFTER UPDATE on acs_device_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ACS_DEVICE_INFO;
DELIMITER ;;
create trigger TDA_ACS_DEVICE_INFO AFTER DELETE on acs_device_info for each ROW 
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;
END;;
DELIMITER ;

drop table if exists acs_device_node_map;
CREATE TABLE `acs_device_node_map` (
  `NODE_NAME` varchar(64) NOT NULL COMMENT '机器节点名称，关联sys_model_node_info中的NODE_NAME',
  `ACS_DEVICE_TAG` varchar(64) NOT NULL COMMENT 'ACS设备唯一标识，关联ACS_DEVICE_INFO中的TAG_NAME',
  PRIMARY KEY (`NODE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='机器节点与ACS设备的对应关系';

drop trigger if exists TIA_ACS_DEVICE_NODE_MAP;
DELIMITER ;;
create trigger TIA_ACS_DEVICE_NODE_MAP AFTER INSERT on acs_device_node_map for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ACS_DEVICE_NODE_MAP;
DELIMITER ;;
create trigger TUA_ACS_DEVICE_NODE_MAP AFTER UPDATE on acs_device_node_map for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ACS_DEVICE_NODE_MAP;
DELIMITER ;;
create trigger TDA_ACS_DEVICE_NODE_MAP AFTER DELETE on acs_device_node_map for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists alarm_action_define;
CREATE TABLE `alarm_action_define` (
  `ACTION_ID` decimal(10,0) NOT NULL COMMENT '动作ID',
  `ACTION_NAME` varchar(64) NOT NULL COMMENT '动作名称',
  `MACRO_NAME` varchar(64) DEFAULT NULL COMMENT '宏定义名称，与代码中宏定义对应，便于在代码中定位',
  PRIMARY KEY (`ACTION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警动作定义表';

drop trigger if exists TIA_ALARM_ACTION_DEFINE;
DELIMITER ;;
create trigger TIA_ALARM_ACTION_DEFINE AFTER INSERT on alarm_action_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_ACTION_DEFINE;
DELIMITER ;;
create trigger TUA_ALARM_ACTION_DEFINE AFTER UPDATE on alarm_action_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_ACTION_DEFINE;
DELIMITER ;;
create trigger TDA_ALARM_ACTION_DEFINE AFTER DELETE on alarm_action_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists alarm_color;
CREATE TABLE `alarm_color` (
  `ID` varchar(8) DEFAULT NULL COMMENT '告警颜色ID',
  `COLOR` varchar(16) DEFAULT NULL COMMENT '告警颜色值'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警颜色';

drop table if exists alarm_keyword_define;
CREATE TABLE `alarm_keyword_define` (
  `KEYWORD_ID` decimal(10,0) NOT NULL COMMENT '关键字ID，与告警消息中的关键字ID对应',
  `KEYWORD_NAME` varchar(64) NOT NULL COMMENT '关键字名称，在告警格式文本中匹配此文本进行替换',
  `MACRO_NAME` varchar(64) DEFAULT NULL COMMENT '宏定义名称，与代码中宏定义对应，便于在代码中定位',
  PRIMARY KEY (`KEYWORD_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警关键字定义表';

drop trigger if exists TIA_ALARM_KEYWORD_DEFINE;
DELIMITER ;;
create trigger TIA_ALARM_KEYWORD_DEFINE AFTER INSERT on alarm_keyword_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_KEYWORD_DEFINE;
DELIMITER ;;
create trigger TUA_ALARM_KEYWORD_DEFINE AFTER UPDATE on alarm_keyword_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_KEYWORD_DEFINE;
DELIMITER ;;
create trigger TDA_ALARM_KEYWORD_DEFINE AFTER DELETE on alarm_keyword_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists alarm_level_define;
CREATE TABLE `alarm_level_define` (
  `PRIORITY_ID` decimal(10,0) NOT NULL COMMENT '优先级ID',
  `PRIORITY_NAME` varchar(64) NOT NULL COMMENT '优先级名称',
  `ALARM_ACTIONS` decimal(10,0) DEFAULT NULL COMMENT '包含的告警动作，取位',
  `PRIORITY_ORDER` decimal(10,0) DEFAULT NULL COMMENT '优先级排序',
  `SOUND_FILE_NAME` varchar(64) DEFAULT NULL COMMENT '告警语音文件名',
  `IF_EVENT` decimal(10,0) DEFAULT NULL COMMENT '是否为事件',
  PRIMARY KEY (`PRIORITY_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警等级（优先级）定义表';

drop trigger if exists TIA_ALARM_LEVEL_DEFINE;
DELIMITER ;;
create trigger TIA_ALARM_LEVEL_DEFINE AFTER INSERT on alarm_level_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_LEVEL_DEFINE;
DELIMITER ;;
create trigger TUA_ALARM_LEVEL_DEFINE AFTER UPDATE on alarm_level_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_LEVEL_DEFINE;
DELIMITER ;;
create trigger TDA_ALARM_LEVEL_DEFINE AFTER DELETE on alarm_level_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists alarm_level_sound_def;
CREATE TABLE `alarm_level_sound_def` (
  `PRIORITY_ID` decimal(10,0) NOT NULL COMMENT '优先级ID',
  `REGION_ID` decimal(10,0) NOT NULL COMMENT '责任区',
  `SOUND_FILE_NAME` varchar(64) DEFAULT NULL COMMENT '语音文件名',
  PRIMARY KEY (`PRIORITY_ID`,`REGION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警等级声音定义表';

drop trigger if exists TIA_ALARM_LEVEL_SOUND_DEF;
DELIMITER ;;
create trigger TIA_ALARM_LEVEL_SOUND_DEF AFTER INSERT on alarm_level_sound_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_LEVEL_SOUND_DEF;
DELIMITER ;;
create trigger TUA_ALARM_LEVEL_SOUND_DEF AFTER UPDATE on alarm_level_sound_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_LEVEL_SOUND_DEF;
DELIMITER ;;
create trigger TDA_ALARM_LEVEL_SOUND_DEF AFTER DELETE on alarm_level_sound_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists alarm_manner_define;
CREATE TABLE `alarm_manner_define` (
  `MANNER_ID` decimal(10,0) NOT NULL COMMENT '告警类型',
  `MANNER_NAME` varchar(64) NOT NULL,
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '告警优先级',
  `IF_WATER_ALM` decimal(10,0) DEFAULT NULL,
  `IF_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回后自动确认',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认后删除动作',
  `IF_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  PRIMARY KEY (`MANNER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警方式定义表';

drop trigger if exists TIA_ALARM_MANNER_DEFINE;
DELIMITER ;;
create trigger TIA_ALARM_MANNER_DEFINE AFTER INSERT on alarm_manner_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_MANNER_DEFINE;
DELIMITER ;;
create trigger TUA_ALARM_MANNER_DEFINE AFTER UPDATE on alarm_manner_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_MANNER_DEFINE;
DELIMITER ;;
create trigger TDA_ALARM_MANNER_DEFINE AFTER DELETE on alarm_manner_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists alarm_status_define;
CREATE TABLE `alarm_status_define` (
  `ALARM_TYPE` decimal(10,0) NOT NULL COMMENT '所属告警类型，与ALAMR_TYPE_DEFINE表对应',
  `STATUS_VALUE` decimal(10,0) NOT NULL COMMENT '状态值，与告警消息中的status值对应，与代码中宏定义值对应',
  `ALARM_MANNER` decimal(10,0) DEFAULT NULL,
  `DISPLAY_NAME` varchar(64) NOT NULL COMMENT '显示名称',
  `MACRO_NAME` varchar(64) DEFAULT NULL COMMENT '宏定义名称，与代码中宏定义对应，便于在代码中定位',
  `ALARM_FORMAT` varchar(256) NOT NULL COMMENT '告警格式，其中的告警关键字会被告警服务替换',
  PRIMARY KEY (`ALARM_TYPE`,`STATUS_VALUE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警状态定义表';

drop trigger if exists TIA_ALARM_STATUS_DEFINE;
DELIMITER ;;
create trigger TIA_ALARM_STATUS_DEFINE AFTER INSERT on alarm_status_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_STATUS_DEFINE;
DELIMITER ;;
create trigger TUA_ALARM_STATUS_DEFINE AFTER UPDATE on alarm_status_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_STATUS_DEFINE;
DELIMITER ;;
create trigger TDA_ALARM_STATUS_DEFINE AFTER DELETE on alarm_status_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists alarm_status_dig_set;
CREATE TABLE `alarm_status_dig_set` (
  `POINT_TYPE` decimal(10,0) NOT NULL COMMENT '测点分类',
  `POINT_SORT` decimal(10,0) NOT NULL COMMENT '测点分类',
  `ALM_STATUS_CHANGE` decimal(10,0) DEFAULT NULL COMMENT '测点变位告警状态',
  `ALM_STATUS_SOE` decimal(10,0) DEFAULT NULL COMMENT '测点SOE告警状态',
  PRIMARY KEY (`POINT_TYPE`,`POINT_SORT`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='数字量默认告警状态设置';

drop table if exists alarm_type_define;
CREATE TABLE `alarm_type_define` (
  `TYPE_ID` decimal(10,0) NOT NULL COMMENT '类型ID，与代码中宏定义值对应',
  `TYPE_NAME` varchar(64) NOT NULL COMMENT '类型名称',
  `ALARM_SOUND_SOURCE` decimal(10,0) DEFAULT NULL COMMENT '语音告警来源',
  `MACRO_NAME` varchar(64) DEFAULT NULL COMMENT '宏定义名称，与代码中宏定义对应，便于在代码中定位',
  PRIMARY KEY (`TYPE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='告警类型定义表';

drop trigger if exists TIA_ALARM_TYPE_DEFINE;
DELIMITER ;;
create trigger TIA_ALARM_TYPE_DEFINE AFTER INSERT on alarm_type_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_ALARM_TYPE_DEFINE;
DELIMITER ;;
create trigger TUA_ALARM_TYPE_DEFINE AFTER UPDATE on alarm_type_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_ALARM_TYPE_DEFINE;
DELIMITER ;;
create trigger TDA_ALARM_TYPE_DEFINE AFTER DELETE on alarm_type_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists analog;
CREATE TABLE `analog` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '点描述',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT 'RTU标签',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型ID',
  `DEVICE` varchar(64) DEFAULT NULL COMMENT '点设备名称',
  `SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '测点顺序号',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `IS_CONTROL` decimal(10,0) DEFAULT NULL COMMENT '是否遥控',
  `GRAY` decimal(22,6) DEFAULT NULL COMMENT '灰度',
  `CONTRAST` decimal(22,6) DEFAULT NULL COMMENT '对比度',
  `QCOLOR` decimal(10,0) DEFAULT NULL COMMENT '拓扑着色',
  `VALUE` decimal(22,6) DEFAULT NULL COMMENT '值',
  `STATUS` decimal(10,0) DEFAULT NULL COMMENT '状态',
  `LAST_UPDATE_TIME` decimal(20,0) DEFAULT NULL COMMENT '最后更新时间',
  `LAST_CHANGE_TIME` decimal(20,0) DEFAULT NULL COMMENT '最后变化时间',
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位ID',
  `SOUND_NAME1` varchar(32) DEFAULT NULL COMMENT '告警声音文件1',
  `SOUND_NAME2` varchar(32) DEFAULT NULL COMMENT '告警声音文件2',
  `SOUND_NAME3` varchar(32) DEFAULT NULL COMMENT '告警声音文件3',
  `REGION_ID` decimal(10,0) DEFAULT NULL COMMENT '责任区',
  `OPT_HANDOVER_GROUP` varchar(64) DEFAULT NULL COMMENT '操作权限移交组',
  `IS_LIMIT` decimal(10,0) DEFAULT NULL COMMENT '是否越限告警',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类型',
  `POINT_CLASS` decimal(10,0) DEFAULT NULL COMMENT '点分类 ',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点属性',
  `POINT_SORT` decimal(10,0) DEFAULT NULL COMMENT '测点种类 ',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否变化存盘',
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL,
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL COMMENT '采用周期(分钟)',
  `SAMPLE_DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '采样死区',
  `CAMERA_TAG` varchar(64) DEFAULT NULL COMMENT '摄像头标签',
  `PRESET_ID` varchar(64) DEFAULT NULL COMMENT '预置点ID',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_ANALOG` (`RTU_TAG`,`DEVICE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模拟量参数表';

drop trigger if exists TI_ANALOG;
DELIMITER ;;
create trigger TI_ANALOG AFTER INSERT on analog for each ROW 
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;
END;;
DELIMITER ;

drop trigger if exists TU_ANALOG;
DELIMITER ;;
create trigger TU_ANALOG AFTER UPDATE on analog for each ROW 
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 
        analog_control.TAG_NAME = NEW.TAG_NAME,
        analog_control.LOCATION_ID = NEW.LOCATION_ID,
        analog_control.SUB_SYSTEM = NEW.SUB_SYSTEM,
        analog_control.POINT_TP_NAME = NEW.POINT_TP_NAME,
        analog_control.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 
        analog_limit_info.TAG_NAME = NEW.TAG_NAME ,
        analog_limit_info.LOCATION_ID = NEW.LOCATION_ID ,
        analog_limit_info.SUB_SYSTEM = NEW.SUB_SYSTEM ,
        analog_limit_info.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 
        sample_define.TAG_NAME =  NEW.TAG_NAME ,
        sample_define.DESCRIPTION = NEW.DESCRIPTION ,    
        sample_define.LOCATION_ID = NEW.LOCATION_ID ,  
        sample_define.SUB_SYSTEM = NEW.SUB_SYSTEM ,
        sample_define.DEVICE_TAG = NEW.DEVICE,
        sample_define.POINT_TYPE = NEW.POINT_TYPE ,     
        sample_define.SAMPLE_PERIOD = NEW.SAMPLE_PERIOD , 
        sample_define.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;
END;;
DELIMITER ;

drop trigger if exists TD_ANALOG;
DELIMITER ;;
create trigger TD_ANALOG BEFORE DELETE on analog for each ROW 
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) ;
END;;
DELIMITER ;

drop trigger if exists TDA_ANALOG;
DELIMITER ;;
create trigger TDA_ANALOG AFTER DELETE on analog for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists bas_craft_def;
CREATE TABLE `bas_craft_def` (
  `ID` decimal(10,0) NOT NULL COMMENT '工艺ID',
  `NAME` varchar(32) NOT NULL COMMENT '工艺名',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `UN_BAS_CRAFT_DEF` (`ID`,`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='工艺定义';

drop trigger if exists TIA_BAS_CRAFT_DEF;
DELIMITER ;;
create trigger TIA_BAS_CRAFT_DEF AFTER INSERT on bas_craft_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_BAS_CRAFT_DEF;
DELIMITER ;;
create trigger TUA_BAS_CRAFT_DEF AFTER UPDATE on bas_craft_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_BAS_CRAFT_DEF;
DELIMITER ;;
create trigger TDA_BAS_CRAFT_DEF AFTER DELETE on bas_craft_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists bas_mode_def;
CREATE TABLE `bas_mode_def` (
  `ID` decimal(10,0) NOT NULL COMMENT 'ID',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站ID',
  `CRAFT_ID` decimal(10,0) NOT NULL COMMENT '所属工艺',
  `MODE_NO` decimal(10,0) NOT NULL COMMENT '模式号',
  `MODE_NAME` varchar(32) NOT NULL COMMENT '模式名',
  `MODE_TYPE` decimal(10,0) DEFAULT NULL COMMENT '模式类型',
  PRIMARY KEY (`ID`,`LOCATION_ID`,`CRAFT_ID`,`MODE_NO`),
  UNIQUE KEY `UN_BAS_MODE_DEF` (`ID`,`LOCATION_ID`,`CRAFT_ID`,`MODE_NO`,`MODE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模式定义';

drop trigger if exists TIA_BAS_MODE_DEF;
DELIMITER ;;
create trigger TIA_BAS_MODE_DEF AFTER INSERT on bas_mode_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_BAS_MODE_DEF;
DELIMITER ;;
create trigger TUA_BAS_MODE_DEF AFTER UPDATE on bas_mode_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_BAS_MODE_DEF;
DELIMITER ;;
create trigger TDA_BAS_MODE_DEF AFTER DELETE on bas_mode_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists bas_mode_type;
CREATE TABLE `bas_mode_type` (
  `ID` decimal(10,0) NOT NULL COMMENT '类型ID',
  `NAME` varchar(32) NOT NULL COMMENT '类型名',
  PRIMARY KEY (`ID`),
  UNIQUE KEY `UN_BAS_MODE_TYPE` (`ID`,`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模式类型';

drop trigger if exists TIA_BAS_MODE_TYPE;
DELIMITER ;;
create trigger TIA_BAS_MODE_TYPE AFTER INSERT on bas_mode_type for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_BAS_MODE_TYPE;
DELIMITER ;;
create trigger TUA_BAS_MODE_TYPE AFTER UPDATE on bas_mode_type for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_BAS_MODE_TYPE;
DELIMITER ;;
create trigger TDA_BAS_MODE_TYPE AFTER DELETE on bas_mode_type for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists dbop_list;
CREATE TABLE `dbop_list` (
  `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '应该是自增主键，同步时用来标识是否同步',
  `STATEMENT` mediumtext NOT NULL COMMENT 'sql语句，可以直接执行的sql语句。',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=82184 DEFAULT CHARSET=utf8 COMMENT='关系库同步表';

drop table if exists dev_group;
CREATE TABLE `dev_group` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '设备名称',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '设备描述',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `RTU_NO` decimal(10,0) DEFAULT NULL COMMENT '关联前置RTU号',
  `PGROUP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '分组名称',
  `REGION_ID` decimal(10,0) NOT NULL COMMENT '区域ID',
  `IS_SUMMARY` decimal(10,0) DEFAULT NULL COMMENT '是否摘要',
  `SUMMARY_VALUE` decimal(10,0) DEFAULT NULL COMMENT '摘要值',
  `SUMMARY_STATUS` decimal(10,0) DEFAULT NULL COMMENT '摘要状态',
  `GROUP_TAG` varchar(64) DEFAULT NULL COMMENT '区域名称',
  `DEV_GROUP_NO` decimal(10,0) DEFAULT NULL COMMENT '设备组序号',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备分组表';

drop trigger if exists TIA_DEV_GROUP;
DELIMITER ;;
create trigger TIA_DEV_GROUP AFTER INSERT on dev_group for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_DEV_GROUP;
DELIMITER ;;
create trigger TUA_DEV_GROUP AFTER UPDATE on dev_group for each ROW 
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 <> COALESCE(OLD.RTU_NO,-1) THEN
    -- BEGIN
      --  DECLARE rtuTag varchar(64) DEFAULT '';
       -- SELECT TAG_NAME INTO rtuTag FROM fes_rtu_para WHERE fes_rtu_para.RTU_NO = NEW.RTU_NO;
       -- UPDATE accuml,dev_info SET accuml.RTU_TAG = rtuTag  WHERE dev_info.GROUP_TAG_NAME = NEW.TAG_NAME AND accuml.DEVICE = dev_info.TAG_NAME;
       -- UPDATE analog,dev_info SET analog.RTU_TAG = rtuTag  WHERE dev_info.GROUP_TAG_NAME = NEW.TAG_NAME AND analog.DEVICE = dev_info.TAG_NAME;
       -- UPDATE digital,dev_info SET digital.RTU_TAG = rtuTag  WHERE dev_info.GROUP_TAG_NAME = NEW.TAG_NAME AND digital.DEVICE = dev_info.TAG_NAME;
       -- UPDATE mix,dev_info SET mix.RTU_TAG = rtuTag  WHERE dev_info.GROUP_TAG_NAME = NEW.TAG_NAME AND mix.DEVICE = dev_info.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'),'dev_group', 'tag_name', OLD.TAG_NAME, NEW.TAG_NAME, getTableSubSystem('dev_group'));
END IF;
END;;
DELIMITER ;

drop trigger if exists TDA_DEV_GROUP;
DELIMITER ;;
create trigger TDA_DEV_GROUP AFTER DELETE on dev_group for each ROW 
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;
END;;
DELIMITER ;

drop table if exists dev_info;
CREATE TABLE `dev_info` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '设备名称',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '设备描述',
  `DEV_NAME` varchar(64) DEFAULT NULL COMMENT '设备编号',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '车站ID',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `GROUP_TAG_NAME` varchar(64) DEFAULT NULL COMMENT '设备分组',
  `VOL_LEVEL_ID` decimal(10,0) DEFAULT NULL COMMENT '电压等级ID',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型',
  `GRAY` decimal(22,6) DEFAULT NULL COMMENT '灰度',
  `CONTRAST` decimal(22,6) DEFAULT NULL COMMENT '对比度',
  `QCOLOR` decimal(10,0) DEFAULT NULL COMMENT '拓扑着色',
  `VALUE` decimal(10,0) DEFAULT NULL COMMENT '值',
  `STATUS` decimal(10,0) DEFAULT NULL COMMENT '状态',
  `NODE_NO1` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO1',
  `NODE_NO2` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO2',
  `NODE_NO3` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO3',
  `NODE_NO4` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO4',
  `NODE_NO5` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO5',
  `NODE_NO6` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO6',
  `NODE_NO7` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO7',
  `NODE_NO8` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO8',
  `NODE_NO9` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO9',
  `NODE_NO10` decimal(10,0) DEFAULT NULL COMMENT 'NODE_NO10',
  `REGION_ID` decimal(10,0) NOT NULL COMMENT '责任区',
  `OPT_HANDOVER_GROUP` varchar(64) DEFAULT NULL COMMENT '权限移交组',
  `IS_SUMMARY` decimal(10,0) DEFAULT NULL COMMENT '是否摘要',
  `SUMMARY_VALUE` decimal(10,0) DEFAULT NULL COMMENT '摘要值',
  `SUMMARY_STATUS` decimal(10,0) DEFAULT NULL COMMENT '摘要状态',
  `DEV_TP_NAME` varchar(128) DEFAULT NULL COMMENT '设备模板',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_DEV_INFO` (`GROUP_TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备信息表';

drop trigger if exists TI_DEV_INFO;
DELIMITER ;;
create trigger TI_DEV_INFO AFTER INSERT on dev_info for each ROW 
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;
END;;
DELIMITER ;

drop trigger if exists TU_DEV_INFO;
DELIMITER ;;
create trigger TU_DEV_INFO AFTER UPDATE on dev_info for each ROW 
label_body:BEGIN
    
IF getTriggerFlag() = TRUE THEN 
INSERT 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 LEAVE label_body;
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  ;
END;;
DELIMITER ;

drop trigger if exists TD_DEV_INFO;
DELIMITER ;;
create trigger TD_DEV_INFO BEFORE DELETE on dev_info for each ROW 
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;
END;;
DELIMITER ;

drop trigger if exists TDA_DEV_INFO;
DELIMITER ;;
create trigger TDA_DEV_INFO AFTER DELETE on dev_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists dev_topo_info;
CREATE TABLE `dev_topo_info` (
  `DEV_TAG_NAME` varchar(64) NOT NULL COMMENT '设备TAG',
  `ASSOCIATED_DEV_TAGS` varchar(3200) NOT NULL COMMENT '关联的设备TAG串，使用英文逗号分隔',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '设备所属厂站',
  PRIMARY KEY (`DEV_TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备拓扑信息表';

drop table if exists dev_topo_ver;
CREATE TABLE `dev_topo_ver` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '厂站ID',
  `LAST_CHG_TIME` decimal(20,0) NOT NULL COMMENT '最后一次拓扑信息修改的时间。POSIX时标标准，ms数。',
  PRIMARY KEY (`LOCATION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='描述厂站的拓扑信息版本';

drop trigger if exists TIA_DEV_TOPO_VER;
DELIMITER ;;
create trigger TIA_DEV_TOPO_VER AFTER INSERT on dev_topo_ver for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_DEV_TOPO_VER;
DELIMITER ;;
create trigger TUA_DEV_TOPO_VER AFTER UPDATE on dev_topo_ver for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_DEV_TOPO_VER;
DELIMITER ;;
create trigger TDA_DEV_TOPO_VER AFTER DELETE on dev_topo_ver for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists dev_type_def;
CREATE TABLE `dev_type_def` (
  `DEV_TYPE_ID` decimal(10,0) NOT NULL COMMENT '设备分类',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '分类名称',
  `DESCRIPTION` varchar(64) DEFAULT NULL COMMENT '分类描述',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  PRIMARY KEY (`DEV_TYPE_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备分类定义表';

drop trigger if exists TIA_DEV_TYPE_DEF;
DELIMITER ;;
create trigger TIA_DEV_TYPE_DEF AFTER INSERT on dev_type_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_DEV_TYPE_DEF;
DELIMITER ;;
create trigger TUA_DEV_TYPE_DEF AFTER UPDATE on dev_type_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_DEV_TYPE_DEF;
DELIMITER ;;
create trigger TDA_DEV_TYPE_DEF AFTER DELETE on dev_type_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists dict_menu_info;
CREATE TABLE `dict_menu_info` (
  `MENU_NAME` varchar(64) NOT NULL,
  `ACTUAL_VALUE` decimal(10,0) NOT NULL COMMENT '动作值',
  `DISPLAY_VALUE` varchar(64) NOT NULL COMMENT '显示值',
  `MENU_MACRO_NAME` varchar(64) DEFAULT NULL COMMENT '宏定义名称',
  PRIMARY KEY (`MENU_NAME`,`ACTUAL_VALUE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='菜单定义信息表';

drop trigger if exists TIA_DICT_MENU_INFO;
DELIMITER ;;
create trigger TIA_DICT_MENU_INFO AFTER INSERT on dict_menu_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_DICT_MENU_INFO;
DELIMITER ;;
create trigger TUA_DICT_MENU_INFO AFTER UPDATE on dict_menu_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_DICT_MENU_INFO;
DELIMITER ;;
create trigger TDA_DICT_MENU_INFO AFTER DELETE on dict_menu_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists dict_point_type;
CREATE TABLE `dict_point_type` (
  `TYPE_NAME` varchar(64) NOT NULL COMMENT '类型名',
  `POINT_CLASS` decimal(10,0) NOT NULL COMMENT '点分类',
  `SHORT_NAME` varchar(64) NOT NULL COMMENT '点名缩写 ',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类别',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点属性（采集点/计算点/虚拟点...）',
  `UNIT_ID` decimal(10,0) DEFAULT NULL COMMENT '单位ID',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否采样',
  `SAMPLE_DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '采样死区',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL,
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL COMMENT '是否统计',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '告警优先级',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延时时间',
  `INPUT_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '多位延迟时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回自动确认',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认后自动删除',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  `IS_CONTROL` decimal(10,0) DEFAULT NULL COMMENT '是否控制',
  `CTRL_ACT_NAME` varchar(64) DEFAULT NULL COMMENT '控制动作组名',
  `CTRL_TYPE` decimal(10,0) DEFAULT NULL COMMENT '是否遥控选择',
  `IS_TAGT_STATE` decimal(10,0) DEFAULT NULL COMMENT '是否等待目标状态',
  `CTRL_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制超时时间',
  `RESV_TIMEOUT` decimal(10,0) DEFAULT NULL COMMENT '控制保留时间',
  `OFFSET_NUM` decimal(10,0) DEFAULT NULL COMMENT 'DO点个数',
  `STATE_TEXT_NAME` varchar(64) DEFAULT NULL COMMENT '数字量文本名',
  `VALUE_NUM` decimal(10,0) DEFAULT NULL COMMENT '分量数',
  PRIMARY KEY (`TYPE_NAME`,`POINT_CLASS`,`SHORT_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测点类型表';

drop table if exists dict_state_text_info;
CREATE TABLE `dict_state_text_info` (
  `STATE_TEXT_NAME` varchar(64) NOT NULL COMMENT '文本名称',
  `ACTUAL_VALUE` decimal(10,0) NOT NULL COMMENT '动作值',
  `DISPLAY_VALUE` varchar(64) NOT NULL COMMENT '显示值',
  `ALARM_MANNER` decimal(10,0) NOT NULL COMMENT '告警方式',
  `ALARM_DESCRIPTION` varchar(64) NOT NULL COMMENT '告警描述',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  PRIMARY KEY (`STATE_TEXT_NAME`,`ACTUAL_VALUE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='状态文本信息表';

drop trigger if exists TIA_DICT_STATE_TEXT_INFO;
DELIMITER ;;
create trigger TIA_DICT_STATE_TEXT_INFO AFTER INSERT on dict_state_text_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_DICT_STATE_TEXT_INFO;
DELIMITER ;;
create trigger TUA_DICT_STATE_TEXT_INFO AFTER UPDATE on dict_state_text_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_DICT_STATE_TEXT_INFO;
DELIMITER ;;
create trigger TDA_DICT_STATE_TEXT_INFO AFTER DELETE on dict_state_text_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists dict_text_define;
CREATE TABLE `dict_text_define` (
  `CLASS_NAME` varchar(64) NOT NULL COMMENT '文本分类',
  `ACTUAL_VALUE` decimal(10,0) NOT NULL COMMENT '文本实际值 ',
  `DISPLAY_NAME` varchar(64) NOT NULL COMMENT '文本显示名(IA) ',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '文本描述(A相电流)',
  PRIMARY KEY (`CLASS_NAME`,`ACTUAL_VALUE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='文本定义表';

drop trigger if exists TIA_DICT_TEXT_DEFINE;
DELIMITER ;;
create trigger TIA_DICT_TEXT_DEFINE AFTER INSERT on dict_text_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_DICT_TEXT_DEFINE;
DELIMITER ;;
create trigger TUA_DICT_TEXT_DEFINE AFTER UPDATE on dict_text_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_DICT_TEXT_DEFINE;
DELIMITER ;;
create trigger TDA_DICT_TEXT_DEFINE AFTER DELETE on dict_text_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists dict_unit_info;
CREATE TABLE `dict_unit_info` (
  `UNIT_ID` decimal(10,0) NOT NULL COMMENT '单位ID',
  `UNIT_NAME` varchar(64) DEFAULT NULL COMMENT '单位名称',
  `UNIT_DESC` varchar(64) DEFAULT NULL COMMENT '单位描述',
  PRIMARY KEY (`UNIT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='单位信息表';

drop trigger if exists TIA_DICT_UNIT_INFO;
DELIMITER ;;
create trigger TIA_DICT_UNIT_INFO AFTER INSERT on dict_unit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_DICT_UNIT_INFO;
DELIMITER ;;
create trigger TUA_DICT_UNIT_INFO AFTER UPDATE on dict_unit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_DICT_UNIT_INFO;
DELIMITER ;;
create trigger TDA_DICT_UNIT_INFO AFTER DELETE on dict_unit_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists dict_vol_level;
CREATE TABLE `dict_vol_level` (
  `VL_ID` decimal(10,0) NOT NULL COMMENT '电压等级ID',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `VL_COLOR` decimal(10,0) NOT NULL COMMENT '等级颜色',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `H_LIMIT` decimal(22,6) DEFAULT NULL COMMENT '电压高限',
  `L_LIMIT` decimal(22,6) DEFAULT NULL COMMENT '电压低限',
  PRIMARY KEY (`VL_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='电压等级定义表';

drop trigger if exists TIA_DICT_VOL_LEVEL;
DELIMITER ;;
create trigger TIA_DICT_VOL_LEVEL AFTER INSERT on dict_vol_level for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_DICT_VOL_LEVEL;
DELIMITER ;;
create trigger TUA_DICT_VOL_LEVEL AFTER UPDATE on dict_vol_level for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_DICT_VOL_LEVEL;
DELIMITER ;;
create trigger TDA_DICT_VOL_LEVEL AFTER DELETE on dict_vol_level for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists digital;
CREATE TABLE `digital` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `DEVICE` varchar(64) DEFAULT NULL COMMENT '所属设备',
  `SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '点序号',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT 'RTU标签',
  `IS_CONTROL` decimal(10,0) DEFAULT NULL COMMENT '是否控制',
  `STATE_TEXT_NAME` varchar(64) DEFAULT NULL COMMENT '状态文本',
  `VALUE_NUM` decimal(10,0) DEFAULT NULL COMMENT '分量数',
  `GRAY` decimal(22,6) DEFAULT NULL COMMENT '灰度',
  `CONTRAST` decimal(22,6) DEFAULT NULL COMMENT '对比度',
  `QCOLOR` decimal(10,0) DEFAULT NULL COMMENT '拓扑着色',
  `VALUE` decimal(10,0) DEFAULT NULL COMMENT '值',
  `STATUS` decimal(10,0) DEFAULT NULL COMMENT '状态',
  `LAST_UPDATE_TIME` decimal(20,0) DEFAULT NULL COMMENT '最近更新时间',
  `VALUE1` decimal(10,0) DEFAULT NULL COMMENT '分量1值',
  `STATUS1` decimal(10,0) DEFAULT NULL COMMENT '分量1状态',
  `LAST_CHANGE_TIME1` decimal(20,0) DEFAULT NULL COMMENT '分量1最近变化时间',
  `VALUE2` decimal(10,0) DEFAULT NULL COMMENT '分量2值',
  `STATUS2` decimal(10,0) DEFAULT NULL COMMENT '分量2状态',
  `LAST_CHANGE_TIME2` decimal(20,0) DEFAULT NULL COMMENT '分量2最近变化时间',
  `VALUE3` decimal(10,0) DEFAULT NULL COMMENT '分量3值',
  `STATUS3` decimal(10,0) DEFAULT NULL COMMENT '分量3状态',
  `LAST_CHANGE_TIME3` decimal(20,0) DEFAULT NULL COMMENT '分量3最近变化时间',
  `VALUE4` decimal(10,0) DEFAULT NULL COMMENT '分量4值',
  `STATUS4` decimal(10,0) DEFAULT NULL COMMENT '分量4状态',
  `LAST_CHANGE_TIME4` decimal(20,0) DEFAULT NULL COMMENT '分量4最近变化时间',
  `VALUE5` decimal(10,0) DEFAULT NULL COMMENT '分量5值',
  `STATUS5` decimal(10,0) DEFAULT NULL COMMENT '分量5状态',
  `LAST_CHANGE_TIME5` decimal(20,0) DEFAULT NULL COMMENT '分量5最近变化时间',
  `IS_OBJ_ALM_MANNER` decimal(10,0) DEFAULT '0' COMMENT '是否自定义告警',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '变位告警优先级',
  `ALARM_PRIORITY_SOE` decimal(10,0) DEFAULT '0' COMMENT 'SOE告警优先级',
  `PIC_NAME` varchar(64) DEFAULT NULL COMMENT '联动推图画面',
  `SOUND_NAME1` varchar(32) DEFAULT NULL COMMENT '告警声音文件1',
  `SOUND_NAME2` varchar(32) DEFAULT NULL COMMENT '告警声音文件2',
  `SOUND_NAME3` varchar(32) DEFAULT NULL COMMENT '告警声音文件3',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延迟时间',
  `INPUT_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '多位延迟时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账--',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回自动确认',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认后删除动作',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗口',
  `REGION_ID` decimal(10,0) DEFAULT NULL COMMENT '责任区',
  `OPT_HANDOVER_GROUP` varchar(64) DEFAULT NULL COMMENT '控制权限转移组',
  `IS_SUMMARY` decimal(10,0) DEFAULT NULL COMMENT '是否摘要',
  `SUMMARY_VALUE` decimal(10,0) DEFAULT NULL COMMENT '摘要值',
  `SUMMARY_STATUS` decimal(10,0) DEFAULT NULL COMMENT '摘要状态',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类型',
  `POINT_CLASS` decimal(10,0) DEFAULT NULL COMMENT '点分类',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点属性 ',
  `POINT_SORT` decimal(10,0) DEFAULT NULL COMMENT '测点种类 ',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否采样',
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL COMMENT '是否统计',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL COMMENT '采样周期',
  `IS_ALARM_FREQ` decimal(10,0) DEFAULT NULL COMMENT '是否告警计数',
  `ALARM_FREQ` decimal(10,0) DEFAULT NULL COMMENT '设定告警计次',
  `ALARM_FREQ_DESC` varchar(64) DEFAULT NULL COMMENT '计次告警描述',
  `IS_ALARM_TIME` decimal(10,0) DEFAULT NULL COMMENT '是否告警计时',
  `ALARM_TIME` decimal(10,0) DEFAULT NULL COMMENT '设定告警计时(秒)',
  `ALARM_TIME_DESC` varchar(64) DEFAULT NULL COMMENT '计时告警描述',
  `ALARM_FREQ_RANGE` decimal(10,0) DEFAULT NULL COMMENT '计时告警时间范围（秒）',
  `INFER_NAME` varchar(64) DEFAULT NULL COMMENT '所属推理结果',
  `CAMERA_TAG` varchar(64) DEFAULT NULL COMMENT '摄像头标签',
  `PRESET_ID` varchar(64) DEFAULT NULL COMMENT '预置点ID',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_DIGITAL` (`DEVICE`,`RTU_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='数字量输入参数表';

drop trigger if exists TI_DIGITAL;
DELIMITER ;;
create trigger TI_DIGITAL AFTER INSERT on digital for each ROW 
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;
END;;
DELIMITER ;

drop trigger if exists TU_DIGITAL;
DELIMITER ;;
create trigger TU_DIGITAL AFTER UPDATE on digital for each ROW 
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 
        digital_control.TAG_NAME = NEW.TAG_NAME,
        digital_control.LOCATION_ID = NEW.LOCATION_ID,
        digital_control.SUB_SYSTEM = NEW.SUB_SYSTEM,
        digital_control.RTU_TAG = NEW.RTU_TAG ,
        digital_control.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 
        sample_define.TAG_NAME =  NEW.TAG_NAME,
        sample_define.DESCRIPTION = NEW.DESCRIPTION ,    
        sample_define.LOCATION_ID = NEW.LOCATION_ID ,  
        sample_define.SUB_SYSTEM = NEW.SUB_SYSTEM ,
        sample_define.DEVICE_TAG = NEW.DEVICE,
        sample_define.POINT_TYPE = NEW.POINT_TYPE,
        sample_define.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;
END;;
DELIMITER ;

drop trigger if exists TD_DIGITAL;
DELIMITER ;;
create trigger TD_DIGITAL BEFORE DELETE on digital for each ROW 
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);
END;;
DELIMITER ;

drop trigger if exists TDA_DIGITAL;
DELIMITER ;;
create trigger TDA_DIGITAL AFTER DELETE on digital for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists fbd_diagram;
CREATE TABLE `fbd_diagram` (
  `NAME` varchar(64) NOT NULL COMMENT '名称',
  `PART_NUMBER` decimal(10,0) NOT NULL COMMENT '图内序号',
  `CONTENT` mediumtext NOT NULL COMMENT '内容',
  `GROUP_ID` decimal(10,0) NOT NULL COMMENT '图组ID',
  `ENABLE` decimal(10,0) NOT NULL COMMENT '图是否运行',
  PRIMARY KEY (`NAME`,`PART_NUMBER`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='FBD图表';

drop table if exists fbd_group;
CREATE TABLE `fbd_group` (
  `ID` decimal(10,0) NOT NULL COMMENT '组ID',
  `NAME` varchar(64) NOT NULL COMMENT '组名',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '组描述',
  `BUSINESS_TYPE` decimal(10,0) NOT NULL COMMENT '业务类型',
  `REFRESH_TIME` decimal(10,0) NOT NULL COMMENT '刷新时间',
  `VERSION` decimal(10,0) NOT NULL COMMENT '版本号',
  `UPDATE_TIME` varchar(32) DEFAULT NULL COMMENT '组最后更新时间',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '位置ID',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业ID',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='FBD图组定义表';

drop trigger if exists TIA_FBD_GROUP;
DELIMITER ;;
create trigger TIA_FBD_GROUP AFTER INSERT on fbd_group for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_FBD_GROUP;
DELIMITER ;;
create trigger TUA_FBD_GROUP AFTER UPDATE on fbd_group for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_FBD_GROUP;
DELIMITER ;;
create trigger TDA_FBD_GROUP AFTER DELETE on fbd_group for each ROW 
BEGIN
    
    IF getTriggerFlag() = TRUE THEN 
    INSERT 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;
END;;
DELIMITER ;

drop table if exists fbd_template;
CREATE TABLE `fbd_template` (
  `NAME` varchar(64) NOT NULL COMMENT '名称',
  `PART_NUMBER` decimal(10,0) NOT NULL COMMENT '图内序号',
  `CONTENT` mediumtext NOT NULL COMMENT '内容',
  PRIMARY KEY (`NAME`,`PART_NUMBER`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='FBD模板表';

drop table if exists hmi_type_info;
CREATE TABLE `hmi_type_info` (
  `HMI_ID` decimal(10,0) NOT NULL,
  `HMI_NAME` varchar(64) NOT NULL,
  `HMI_DESC` varchar(100) DEFAULT NULL,
  `HMI_DEFINE` varchar(32) DEFAULT NULL,
  `HMI_TYPE` decimal(10,0) DEFAULT NULL,
  PRIMARY KEY (`HMI_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='画面信息定义表';

drop trigger if exists TIA_HMI_TYPE_INFO;
DELIMITER ;;
create trigger TIA_HMI_TYPE_INFO AFTER INSERT on hmi_type_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_HMI_TYPE_INFO;
DELIMITER ;;
create trigger TUA_HMI_TYPE_INFO AFTER UPDATE on hmi_type_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_HMI_TYPE_INFO;
DELIMITER ;;
create trigger TDA_HMI_TYPE_INFO AFTER DELETE on hmi_type_info for each ROW 
BEGIN
    #1是报表,0是HMI画面
    DECLARE num int;
    SET num =  (OLD.HMI_ID-1) DIV 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;
END;;
DELIMITER ;

drop table if exists ia_disposal_plan;
CREATE TABLE `ia_disposal_plan` (
  `KEY_ID_TAG` varchar(128) NOT NULL,
  `DISPOSAL_PLAN` varchar(512) DEFAULT NULL,
  `LOCATION_ID` decimal(10,0) NOT NULL,
  PRIMARY KEY (`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='处置预案表';

drop trigger if exists TIA_IA_DISPOSAL_PLAN;
DELIMITER ;;
create trigger TIA_IA_DISPOSAL_PLAN AFTER INSERT on ia_disposal_plan for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_IA_DISPOSAL_PLAN;
DELIMITER ;;
create trigger TUA_IA_DISPOSAL_PLAN AFTER UPDATE on ia_disposal_plan for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_IA_DISPOSAL_PLAN;
DELIMITER ;;
create trigger TDA_IA_DISPOSAL_PLAN AFTER DELETE on ia_disposal_plan for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists mix;
CREATE TABLE `mix` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '描述',
  `DEVICE` varchar(64) DEFAULT NULL COMMENT '设备',
  `SEQ_NO` decimal(10,0) DEFAULT NULL COMMENT '序号',
  `LOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '车站',
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL COMMENT '所属专业',
  `RTU_TAG` varchar(64) DEFAULT NULL COMMENT '所属RTU',
  `IS_CONTROL` decimal(10,0) DEFAULT NULL COMMENT '是否控制',
  `STATE_TEXT_NAME` varchar(64) DEFAULT NULL COMMENT '数字量文本',
  `GRAY` decimal(22,6) DEFAULT NULL COMMENT '灰度',
  `CONTRAST` decimal(22,6) DEFAULT NULL COMMENT '对比度',
  `QCOLOR` decimal(10,0) DEFAULT NULL COMMENT '拓扑着色',
  `VALUE` decimal(10,0) DEFAULT NULL COMMENT '实时值',
  `STATUS` decimal(10,0) DEFAULT NULL COMMENT '状态',
  `LAST_UPDATE_TIME` decimal(20,0) DEFAULT NULL COMMENT '最进更新时间',
  `LAST_CHANGE_TIME` decimal(20,0) DEFAULT NULL COMMENT '最近变位时间',
  `RAW_VALUE` decimal(22,6) DEFAULT NULL COMMENT '原始值',
  `METHOD` varchar(64) DEFAULT NULL COMMENT '策略',
  `IS_OBJ_ALM_MANNER` decimal(10,0) DEFAULT '0' COMMENT '是否自定义告警',
  `ALARM_PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '告警优先级',
  `PIC_NAME` varchar(64) DEFAULT NULL COMMENT '推图名',
  `SOUND_NAME1` varchar(32) DEFAULT NULL COMMENT '告警声音文件1',
  `SOUND_NAME2` varchar(32) DEFAULT NULL COMMENT '告警声音文件2',
  `SOUND_NAME3` varchar(32) DEFAULT NULL COMMENT '告警声音文件3',
  `ALARM_DELAY_TIME` decimal(10,0) DEFAULT NULL COMMENT '告警延迟时间',
  `IS_WATER_ALM` decimal(10,0) DEFAULT NULL COMMENT '是否流水账',
  `IS_ACK_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回自动确认',
  `DEL_ACT_ON_ACK` decimal(10,0) DEFAULT NULL COMMENT '确认后删除动作',
  `IS_NEVER_ALM_ON_RTN` decimal(10,0) DEFAULT NULL COMMENT '是否返回不上窗',
  `REGION_ID` decimal(10,0) DEFAULT NULL COMMENT '责任区',
  `OPT_HANDOVER_GROUP` varchar(64) DEFAULT NULL COMMENT '控制权限转移组',
  `IS_SUMMARY` decimal(10,0) DEFAULT NULL COMMENT '是否摘要',
  `SUMMARY_VALUE` decimal(10,0) DEFAULT NULL COMMENT '摘要值',
  `SUMMARY_STATUS` decimal(10,0) DEFAULT NULL COMMENT '摘要状态',
  `POINT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '点类型',
  `POINT_CLASS` decimal(10,0) DEFAULT NULL COMMENT '点类别 ',
  `POINT_PROPERTY` decimal(10,0) DEFAULT NULL COMMENT '点属性',
  `POINT_SORT` decimal(10,0) DEFAULT NULL COMMENT '测点种类 ',
  `DEV_TYPE` decimal(10,0) DEFAULT NULL COMMENT '设备类型',
  `POINT_TP_NAME` varchar(64) DEFAULT NULL COMMENT '点模板名',
  `IS_SAMPLE` decimal(10,0) DEFAULT NULL COMMENT '是否采样',
  `IS_STATISTICS` decimal(10,0) DEFAULT NULL COMMENT '是否统计',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL COMMENT '采样周期 ',
  `CAMERA_TAG` varchar(64) DEFAULT NULL COMMENT '摄像头TAG',
  `PRESET_ID` varchar(64) DEFAULT NULL COMMENT '预置点ID',
  PRIMARY KEY (`TAG_NAME`),
  KEY `IN_MIX` (`DEVICE`,`RTU_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='混合量输入参数表';

drop trigger if exists TI_MIX;
DELIMITER ;;
create trigger TI_MIX AFTER INSERT on mix for each ROW 
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;
END;;
DELIMITER ;

drop trigger if exists TU_MIX;
DELIMITER ;;
create trigger TU_MIX AFTER UPDATE on mix for each ROW 
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 
        sample_define.TAG_NAME = NEW.TAG_NAME ,
        sample_define.DESCRIPTION = NEW.DESCRIPTION ,    
        sample_define.LOCATION_ID = NEW.LOCATION_ID ,  
        sample_define.SUB_SYSTEM = NEW.SUB_SYSTEM ,
        sample_define.DEVICE_TAG = NEW.DEVICE,
        sample_define.POINT_TYPE = NEW.POINT_TYPE,
        sample_define.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;
END;;
DELIMITER ;

drop trigger if exists TD_MIX;
DELIMITER ;;
create trigger TD_MIX BEFORE DELETE on mix for each ROW 
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);
END;;
DELIMITER ;

drop trigger if exists TDA_MIX;
DELIMITER ;;
create trigger TDA_MIX AFTER DELETE on mix for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_ctrl_act_define;
CREATE TABLE `opt_ctrl_act_define` (
  `CTRL_GRP_NAME` varchar(64) NOT NULL COMMENT '控制动作组名',
  `CTRL_GRP_NO` decimal(10,0) NOT NULL COMMENT '控制动作组号',
  `CTRL_ACT_NAME` varchar(64) NOT NULL COMMENT '动作名',
  `CTRL_ACT_TYPE` decimal(10,0) DEFAULT NULL COMMENT '控制类型',
  `TARGET_VALUE` decimal(10,0) DEFAULT NULL COMMENT '目标值',
  `ACTION_ALLOW` decimal(10,0) DEFAULT NULL COMMENT '控制动作 允许',
  `VALUE_NUM` decimal(10,0) DEFAULT NULL COMMENT '分量数',
  `CTRL_INDEX` decimal(10,0) DEFAULT NULL COMMENT '遥控点号索引',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业',
  PRIMARY KEY (`CTRL_GRP_NAME`,`CTRL_GRP_NO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='控制动作定义表';

drop trigger if exists TIA_OPT_CTRL_ACT_DEFINE;
DELIMITER ;;
create trigger TIA_OPT_CTRL_ACT_DEFINE AFTER INSERT on opt_ctrl_act_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_CTRL_ACT_DEFINE;
DELIMITER ;;
create trigger TUA_OPT_CTRL_ACT_DEFINE AFTER UPDATE on opt_ctrl_act_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_CTRL_ACT_DEFINE;
DELIMITER ;;
create trigger TDA_OPT_CTRL_ACT_DEFINE AFTER DELETE on opt_ctrl_act_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_handover_grp;
CREATE TABLE `opt_handover_grp` (
  `TAG_NAME` varchar(64) NOT NULL,
  `DESCRIPTION` varchar(128) NOT NULL,
  `LOCATION_ID` decimal(10,0) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  `EXTER_KEY_ID_TAG` varchar(128) DEFAULT NULL,
  `PGROUP_NAME` varchar(64) DEFAULT NULL,
  `DEV_TYPE` decimal(10,0) DEFAULT NULL,
  `REGION_ID` decimal(10,0) DEFAULT NULL,
  `RESV_TIMEOUT` decimal(10,0) DEFAULT NULL,
  `TARGET_VALUE` decimal(10,0) DEFAULT NULL,
  `GROUP_TAG` varchar(64) NOT NULL,
  `LOCATION` decimal(10,0) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='权限转移组表';

drop trigger if exists TIA_OPT_HANDOVER_GRP;
DELIMITER ;;
create trigger TIA_OPT_HANDOVER_GRP AFTER INSERT on opt_handover_grp for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_HANDOVER_GRP;
DELIMITER ;;
create trigger TUA_OPT_HANDOVER_GRP AFTER UPDATE on opt_handover_grp for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_HANDOVER_GRP;
DELIMITER ;;
create trigger TDA_OPT_HANDOVER_GRP AFTER DELETE on opt_handover_grp for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_interlock_define;
CREATE TABLE `opt_interlock_define` (
  `TAG_NAME` varchar(64) NOT NULL,
  `SUB_SYSTEM` decimal(10,0) NOT NULL,
  `DESCRIPTION` varchar(128) NOT NULL,
  `INTERLOCK_STR` varchar(4096) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='闭锁公式定义表';

drop trigger if exists TIA_OPT_INTERLOCK_DEFINE;
DELIMITER ;;
create trigger TIA_OPT_INTERLOCK_DEFINE AFTER INSERT on opt_interlock_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_INTERLOCK_DEFINE;
DELIMITER ;;
create trigger TUA_OPT_INTERLOCK_DEFINE AFTER UPDATE on opt_interlock_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_INTERLOCK_DEFINE;
DELIMITER ;;
create trigger TDA_OPT_INTERLOCK_DEFINE AFTER DELETE on opt_interlock_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_interlock_para;
CREATE TABLE `opt_interlock_para` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT 'DO标签点,遥控的时候通过该标签匹配',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT 'DO描述',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT 'DO所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `INTERLOCK_TAG` varchar(64) DEFAULT NULL COMMENT '闭锁函数-对应闭锁定义表',
  `INTERLOCK_NUM` decimal(10,0) DEFAULT NULL COMMENT '闭锁参数数量',
  `INTERLOCK_IN_TAG1` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG2` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG3` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG4` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG5` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG6` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG7` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG8` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG9` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG10` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG11` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG12` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG13` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG14` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG15` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG16` varchar(64) DEFAULT NULL COMMENT '闭锁输入参数',
  `INTERLOCK_IN_TAG17` varchar(64) DEFAULT NULL,
  `INTERLOCK_IN_TAG18` varchar(64) DEFAULT NULL,
  `INTERLOCK_IN_TAG19` varchar(64) DEFAULT NULL,
  `INTERLOCK_IN_TAG20` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='闭锁参数表';

drop trigger if exists TIA_OPT_INTERLOCK_PARA;
DELIMITER ;;
create trigger TIA_OPT_INTERLOCK_PARA AFTER INSERT on opt_interlock_para for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_INTERLOCK_PARA;
DELIMITER ;;
create trigger TUA_OPT_INTERLOCK_PARA AFTER UPDATE on opt_interlock_para for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_INTERLOCK_PARA;
DELIMITER ;;
create trigger TDA_OPT_INTERLOCK_PARA AFTER DELETE on opt_interlock_para for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_shield_define;
CREATE TABLE `opt_shield_define` (
  `SHIELD_ID` decimal(10,0) NOT NULL COMMENT '标志牌ID',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '屏蔽描述（遥控屏蔽/检修屏蔽）',
  `PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '屏蔽等级',
  `SHIELD_TYPE` decimal(10,0) DEFAULT NULL COMMENT '屏蔽类型',
  `SHIELD_PROP` decimal(10,0) DEFAULT NULL COMMENT '屏蔽属性(禁止控制/刷新/告警)',
  `SHIELD_SET` decimal(10,0) DEFAULT NULL,
  `SHIELD_CANCEL` decimal(10,0) DEFAULT NULL COMMENT '屏蔽取消值',
  `ICON_NAME` varchar(128) DEFAULT NULL COMMENT '图元名称',
  PRIMARY KEY (`SHIELD_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='屏蔽定义表';

drop trigger if exists TIA_OPT_SHIELD_DEFINE;
DELIMITER ;;
create trigger TIA_OPT_SHIELD_DEFINE AFTER INSERT on opt_shield_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_SHIELD_DEFINE;
DELIMITER ;;
create trigger TUA_OPT_SHIELD_DEFINE AFTER UPDATE on opt_shield_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_SHIELD_DEFINE;
DELIMITER ;;
create trigger TDA_OPT_SHIELD_DEFINE AFTER DELETE on opt_shield_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_shield_info;
CREATE TABLE `opt_shield_info` (
  `SHIELD_ID` decimal(10,0) NOT NULL COMMENT '屏蔽id',
  `SHIELD_NAME` varchar(128) NOT NULL COMMENT '屏蔽事由',
  `SHIELD_TYPE` decimal(10,0) NOT NULL COMMENT '屏蔽类型',
  `SHIELD_STATUS` decimal(10,0) NOT NULL COMMENT '屏蔽状态',
  `SHIELD_ENABLE` decimal(10,0) NOT NULL COMMENT '屏蔽使能',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `SHIELD_TIME` decimal(20,0) NOT NULL COMMENT '屏蔽更新时间',
  `TIME_ID` decimal(10,0) NOT NULL COMMENT '时段定义',
  `HOST_NAME` varchar(64) DEFAULT NULL COMMENT '操作主机名',
  `USER_ID` decimal(10,0) DEFAULT NULL COMMENT '用户ID',
  `USER_GROUP_ID` decimal(10,0) DEFAULT NULL COMMENT '用户组ID',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '屏蔽描述',
  PRIMARY KEY (`SHIELD_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='屏蔽信息表';

drop trigger if exists TIA_OPT_SHIELD_INFO;
DELIMITER ;;
create trigger TIA_OPT_SHIELD_INFO AFTER INSERT on opt_shield_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_SHIELD_INFO;
DELIMITER ;;
create trigger TUA_OPT_SHIELD_INFO AFTER UPDATE on opt_shield_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_SHIELD_INFO;
DELIMITER ;;
create trigger TDA_OPT_SHIELD_INFO AFTER DELETE on opt_shield_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_shield_obj;
CREATE TABLE `opt_shield_obj` (
  `SHIELD_ID` decimal(10,0) NOT NULL COMMENT '屏蔽id',
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '屏蔽key标签',
  `SHIELD_MODE` decimal(10,0) NOT NULL COMMENT '屏蔽模式',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `SHIELD_OBJECT` varchar(64) DEFAULT NULL COMMENT '屏蔽对象',
  PRIMARY KEY (`SHIELD_ID`,`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='屏蔽对象表';

drop trigger if exists TIA_OPT_SHIELD_OBJ;
DELIMITER ;;
create trigger TIA_OPT_SHIELD_OBJ AFTER INSERT on opt_shield_obj for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_SHIELD_OBJ;
DELIMITER ;;
create trigger TUA_OPT_SHIELD_OBJ AFTER UPDATE on opt_shield_obj for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_SHIELD_OBJ;
DELIMITER ;;
create trigger TDA_OPT_SHIELD_OBJ AFTER DELETE on opt_shield_obj for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_shield_time;
CREATE TABLE `opt_shield_time` (
  `TIME_ID` decimal(10,0) NOT NULL COMMENT '时段id',
  `TIME_TYPE` decimal(10,0) NOT NULL COMMENT '时段类型',
  `START_TIME` varchar(64) NOT NULL COMMENT '开始时间',
  `END_TIME` varchar(64) NOT NULL COMMENT '结束时间',
  `TIME_OPTION` decimal(10,0) NOT NULL COMMENT '时间选项',
  PRIMARY KEY (`TIME_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='时段定义表';

drop trigger if exists TIA_OPT_SHIELD_TIME;
DELIMITER ;;
create trigger TIA_OPT_SHIELD_TIME AFTER INSERT on opt_shield_time for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_SHIELD_TIME;
DELIMITER ;;
create trigger TUA_OPT_SHIELD_TIME AFTER UPDATE on opt_shield_time for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_SHIELD_TIME;
DELIMITER ;;
create trigger TDA_OPT_SHIELD_TIME AFTER DELETE on opt_shield_time for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_tag_info;
CREATE TABLE `opt_tag_info` (
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT 'KEY标签',
  `TAG_TYPE` decimal(10,0) NOT NULL COMMENT '标签类型',
  `OPT_TIME` decimal(20,0) DEFAULT NULL COMMENT '操作时间',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '专业',
  `SET_VALUE` decimal(22,6) DEFAULT NULL COMMENT '设置值',
  `STATE_TEXT` varchar(64) DEFAULT NULL COMMENT '状态文本',
  `HOST_NAME` varchar(64) DEFAULT NULL COMMENT '主机',
  `USER_NAME` varchar(64) DEFAULT NULL COMMENT '用户',
  `USER_GROUP` varchar(64) DEFAULT NULL COMMENT '点标签一栏表',
  PRIMARY KEY (`KEY_ID_TAG`,`TAG_TYPE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='标签信息表';

drop trigger if exists TIA_OPT_TAG_INFO;
DELIMITER ;;
create trigger TIA_OPT_TAG_INFO AFTER INSERT on opt_tag_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_TAG_INFO;
DELIMITER ;;
create trigger TUA_OPT_TAG_INFO AFTER UPDATE on opt_tag_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_TAG_INFO;
DELIMITER ;;
create trigger TDA_OPT_TAG_INFO AFTER DELETE on opt_tag_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_token_define;
CREATE TABLE `opt_token_define` (
  `TOKEN_ID` decimal(10,0) NOT NULL COMMENT '标志牌ID',
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '标志牌描述',
  `PRIORITY` decimal(10,0) DEFAULT NULL COMMENT '标志牌等级',
  `TOKEN_TYPE` decimal(10,0) DEFAULT NULL COMMENT '标志牌类型',
  `TOKEN_PROP` decimal(10,0) DEFAULT NULL COMMENT '标志牌属性',
  `TOKEN_SOURCE` decimal(10,0) DEFAULT NULL COMMENT '标志牌来源',
  `ICON_NAME` varchar(128) DEFAULT NULL COMMENT '图元名称',
  `TOKEN_SET` decimal(10,0) DEFAULT NULL COMMENT '标志牌挂牌值',
  `TOKEN_CANCEL` decimal(10,0) DEFAULT NULL COMMENT '标志牌摘牌值',
  `SYNC_P_TAG` varchar(64) DEFAULT NULL COMMENT '同步点标签',
  `TOKEN_POS_X` decimal(10,0) DEFAULT NULL COMMENT '图像中X坐标',
  `TOKEN_POS_Y` decimal(10,0) DEFAULT NULL COMMENT '图像中Y坐标',
  PRIMARY KEY (`TOKEN_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='挂牌定义表';

drop trigger if exists TIA_OPT_TOKEN_DEFINE;
DELIMITER ;;
create trigger TIA_OPT_TOKEN_DEFINE AFTER INSERT on opt_token_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_TOKEN_DEFINE;
DELIMITER ;;
create trigger TUA_OPT_TOKEN_DEFINE AFTER UPDATE on opt_token_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_TOKEN_DEFINE;
DELIMITER ;;
create trigger TDA_OPT_TOKEN_DEFINE AFTER DELETE on opt_token_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists opt_token_info;
CREATE TABLE `opt_token_info` (
  `TOKEN_ID` decimal(10,0) NOT NULL COMMENT '标志牌ID',
  `KEY_ID_TAG` varchar(128) NOT NULL COMMENT '对象KEYID',
  `OPT_TIME` decimal(20,0) DEFAULT NULL COMMENT '操作时间',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `DEVICE` varchar(64) DEFAULT NULL COMMENT '挂牌设备',
  `TOKEN_POS_X` decimal(10,0) DEFAULT NULL COMMENT '图形中X坐标',
  `TOKEN_POS_Y` decimal(10,0) DEFAULT NULL COMMENT '图形中Y坐标',
  `IS_DISPLAY` decimal(10,0) DEFAULT NULL COMMENT '是否显示',
  `HOST_NAME` varchar(64) DEFAULT NULL COMMENT '操作主机名',
  `USER_NAME` varchar(64) DEFAULT NULL COMMENT '操作用户名',
  `USER_GROUP` varchar(64) DEFAULT NULL COMMENT '操作用户组名',
  `GRAPH_NAME` varchar(64) DEFAULT NULL COMMENT '图形名称',
  `NOTE` varchar(128) DEFAULT NULL COMMENT '注释',
  PRIMARY KEY (`TOKEN_ID`,`KEY_ID_TAG`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='挂牌信息表';

drop trigger if exists TIA_OPT_TOKEN_INFO;
DELIMITER ;;
create trigger TIA_OPT_TOKEN_INFO AFTER INSERT on opt_token_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_OPT_TOKEN_INFO;
DELIMITER ;;
create trigger TUA_OPT_TOKEN_INFO AFTER UPDATE on opt_token_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_OPT_TOKEN_INFO;
DELIMITER ;;
create trigger TDA_OPT_TOKEN_INFO AFTER DELETE on opt_token_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists region_info;
CREATE TABLE `region_info` (
  `REGION_ID` decimal(10,0) NOT NULL,
  `TAG_NAME` varchar(64) NOT NULL,
  `REGION_NO` decimal(10,0) DEFAULT NULL,
  `DESCRIPTION` varchar(128) DEFAULT NULL,
  `SUB_SYSTEM` decimal(10,0) DEFAULT NULL,
  PRIMARY KEY (`REGION_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设备责任区信息表';

drop trigger if exists TIA_REGION_INFO;
DELIMITER ;;
create trigger TIA_REGION_INFO AFTER INSERT on region_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_REGION_INFO;
DELIMITER ;;
create trigger TUA_REGION_INFO AFTER UPDATE on region_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_REGION_INFO;
DELIMITER ;;
create trigger TDA_REGION_INFO AFTER DELETE on region_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists report_type_info;
CREATE TABLE `report_type_info` (
  `REPORT_ID` decimal(10,0) NOT NULL COMMENT '报表ID ',
  `REPORT_NAME` varchar(64) DEFAULT NULL COMMENT '报表名称 ',
  `REPORT_DESC` varchar(128) DEFAULT NULL COMMENT '报表描述 ',
  `REPORT_DEFINE` varchar(32) DEFAULT NULL COMMENT '报表定义 ',
  PRIMARY KEY (`REPORT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='报表类型表';

drop trigger if exists TIA_REPORT_TYPE_INFO;
DELIMITER ;;
create trigger TIA_REPORT_TYPE_INFO AFTER INSERT on report_type_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_REPORT_TYPE_INFO;
DELIMITER ;;
create trigger TUA_REPORT_TYPE_INFO AFTER UPDATE on report_type_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_REPORT_TYPE_INFO;
DELIMITER ;;
create trigger TDA_REPORT_TYPE_INFO AFTER DELETE on report_type_info for each ROW 
BEGIN
    #PERM_TYPE = 1是报表,0是HMI画面
    DECLARE num int;
    SET num =  (OLD.REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop table if exists rm_dept_def;
CREATE TABLE `rm_dept_def` (
  `DEPT_ID` decimal(10,0) NOT NULL COMMENT '部门ID号',
  `DEPT_NAME` varchar(64) NOT NULL COMMENT '部门名称',
  `PERM_DEPT` decimal(10,0) DEFAULT NULL COMMENT '所属部门',
  `DEPT_DESC` varchar(100) DEFAULT NULL COMMENT '部门描述',
  PRIMARY KEY (`DEPT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='部门定义表';

drop trigger if exists TIA_RM_DEPT_DEF;
DELIMITER ;;
create trigger TIA_RM_DEPT_DEF AFTER INSERT on rm_dept_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_DEPT_DEF;
DELIMITER ;;
create trigger TUA_RM_DEPT_DEF AFTER UPDATE on rm_dept_def for each ROW 
BEGIN
    DECLARE oldNum int;
    DECLARE newNum int;
    SET oldNum =  (OLD.DEPT_ID-1) DIV 32;
    SET newNum =  (NEW.DEPT_ID-1) DIV 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_DEPT_DEF;
DELIMITER ;;
create trigger TDA_RM_DEPT_DEF AFTER DELETE on rm_dept_def for each ROW 
BEGIN
#删除引用了该部门的用户
    DECLARE num int;
    SET num =  (OLD.DEPT_ID-1) DIV 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;
END;;
DELIMITER ;

drop table if exists rm_norm_func_def;
CREATE TABLE `rm_norm_func_def` (
  `FUNC_ID` decimal(10,0) NOT NULL COMMENT '功能ID',
  `FUNC_NAME` varchar(64) NOT NULL COMMENT '功能名称',
  `FUNC_DEFINE` varchar(32) NOT NULL COMMENT '功能宏定义',
  `FUNC_DESC` varchar(100) DEFAULT NULL COMMENT '功能描述',
  PRIMARY KEY (`FUNC_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='通用功能定义表';

drop trigger if exists TIA_RM_NORM_FUNC_DEF;
DELIMITER ;;
create trigger TIA_RM_NORM_FUNC_DEF AFTER INSERT on rm_norm_func_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_NORM_FUNC_DEF;
DELIMITER ;;
create trigger TUA_RM_NORM_FUNC_DEF AFTER UPDATE on rm_norm_func_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_NORM_FUNC_DEF;
DELIMITER ;;
create trigger TDA_RM_NORM_FUNC_DEF AFTER DELETE on rm_norm_func_def for each ROW 
BEGIN
#删除引用了该功能的角色
    DECLARE num int;
    SET num =  (OLD.FUNC_ID-1) DIV 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;
END;;
DELIMITER ;

drop table if exists rm_role_db_info;
CREATE TABLE `rm_role_db_info` (
  `PERM_ROLE` decimal(10,0) NOT NULL COMMENT '权限角色',
  `PERM_ATTRIBUTE` decimal(10,0) DEFAULT NULL COMMENT '权限属性',
  `PERM_TABLE_NAME` varchar(32) NOT NULL COMMENT '数据库表名',
  PRIMARY KEY (`PERM_ROLE`,`PERM_TABLE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色数据表权限信息表';

drop trigger if exists TIA_RM_ROLE_DB_INFO;
DELIMITER ;;
create trigger TIA_RM_ROLE_DB_INFO AFTER INSERT on rm_role_db_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_ROLE_DB_INFO;
DELIMITER ;;
create trigger TUA_RM_ROLE_DB_INFO AFTER UPDATE on rm_role_db_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_ROLE_DB_INFO;
DELIMITER ;;
create trigger TDA_RM_ROLE_DB_INFO AFTER DELETE on rm_role_db_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists rm_role_def;
CREATE TABLE `rm_role_def` (
  `PERM_ID` decimal(10,0) NOT NULL COMMENT '角色ID',
  `PERM_NAME` varchar(64) NOT NULL COMMENT '角色名称',
  `PERM_DESC` varchar(100) DEFAULT NULL COMMENT '角色描述',
  `PERM_NOM_FUNC1` decimal(10,0) DEFAULT NULL COMMENT '角色普通功能1',
  `PERM_NOM_FUNC2` decimal(10,0) DEFAULT NULL COMMENT '角色普通功能2',
  `PERM_NOM_FUNC3` decimal(10,0) DEFAULT NULL COMMENT '角色普通功能3',
  `PERM_NOM_FUNC4` decimal(10,0) DEFAULT NULL COMMENT '角色普通功能4',
  `PERM_NOM_FUNC5` decimal(10,0) DEFAULT NULL COMMENT '角色普通功能5',
  `PERM_NOM_FUNC6` decimal(10,0) DEFAULT NULL COMMENT '角色普通功能6',
  `PERM_NOM_FUNC7` decimal(10,0) DEFAULT NULL COMMENT '角色普通功能7',
  `PERM_NOM_FUNC8` decimal(10,0) DEFAULT NULL COMMENT '角色普通功能8',
  `PERM_SPE_FUNC1` decimal(10,0) DEFAULT NULL COMMENT '角色特殊功能1',
  `PERM_SPE_FUNC2` decimal(10,0) DEFAULT NULL COMMENT '角色特殊功能2',
  `PERM_SPE_FUNC3` decimal(10,0) DEFAULT NULL COMMENT '角色特殊功能3',
  `PERM_SPE_FUNC4` decimal(10,0) DEFAULT NULL COMMENT '角色特殊功能4',
  `PERM_SPE_FUNC5` decimal(10,0) DEFAULT NULL COMMENT '角色特殊功能5',
  `PERM_SPE_FUNC6` decimal(10,0) DEFAULT NULL COMMENT '角色特殊功能6',
  `PERM_SPE_FUNC7` decimal(10,0) DEFAULT NULL COMMENT '角色特殊功能7',
  `PERM_SPE_FUNC8` decimal(10,0) DEFAULT NULL COMMENT '角色特殊功能8',
  PRIMARY KEY (`PERM_ID`),
  UNIQUE KEY `UN_RM_ROLE_DEF` (`PERM_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色定义表';

drop trigger if exists TIA_RM_ROLE_DEF;
DELIMITER ;;
create trigger TIA_RM_ROLE_DEF AFTER INSERT on rm_role_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_ROLE_DEF;
DELIMITER ;;
create trigger TUA_RM_ROLE_DEF AFTER UPDATE on rm_role_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_ROLE_DEF;
DELIMITER ;;
create trigger TDA_RM_ROLE_DEF AFTER DELETE on rm_role_def for each ROW 
BEGIN
#删除引用了该角色的数据
#用户组
    DECLARE num int;
    SET num =  (OLD.PERM_ID-1) DIV 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;
END;;
DELIMITER ;

drop table if exists rm_role_hmi_info;
CREATE TABLE `rm_role_hmi_info` (
  `PERM_ROLE` decimal(10,0) NOT NULL COMMENT '所属角色',
  `PERM_TYPE` decimal(10,0) NOT NULL COMMENT '权限类型',
  `PERM_INFO1` decimal(10,0) DEFAULT NULL COMMENT '权限信息1',
  `PERM_INFO2` decimal(10,0) DEFAULT NULL COMMENT '权限信息2',
  `PERM_INFO3` decimal(10,0) DEFAULT NULL COMMENT '权限信息3',
  `PERM_INFO4` decimal(10,0) DEFAULT NULL COMMENT '权限信息4',
  `PERM_INFO5` decimal(10,0) DEFAULT NULL COMMENT '权限信息5',
  `PERM_INFO6` decimal(10,0) DEFAULT NULL COMMENT '权限信息6',
  `PERM_INFO7` decimal(10,0) DEFAULT NULL COMMENT '权限信息7',
  `PERM_INFO8` decimal(10,0) DEFAULT NULL COMMENT '权限信息8',
  `PERM_INFO9` decimal(10,0) DEFAULT NULL COMMENT '权限信息9',
  `PERM_INFO10` decimal(10,0) DEFAULT NULL COMMENT '权限信息10',
  `PERM_INFO11` decimal(10,0) DEFAULT NULL COMMENT '权限信息11',
  `PERM_INFO12` decimal(10,0) DEFAULT NULL COMMENT '权限信息12',
  `PERM_INFO13` decimal(10,0) DEFAULT NULL COMMENT '权限信息13',
  `PERM_INFO14` decimal(10,0) DEFAULT NULL COMMENT '权限信息14',
  `PERM_INFO15` decimal(10,0) DEFAULT NULL COMMENT '权限信息15',
  `PERM_INFO16` decimal(10,0) DEFAULT NULL COMMENT '权限信息16',
  PRIMARY KEY (`PERM_ROLE`,`PERM_TYPE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色画面权限信息表';

drop trigger if exists TIA_RM_ROLE_HMI_INFO;
DELIMITER ;;
create trigger TIA_RM_ROLE_HMI_INFO AFTER INSERT on rm_role_hmi_info for each ROW 
BEGIN
    #PERM_TYPE = 1是报表,0是HMI画面
    #先清除,后插入rm_userg_report_info
    DECLARE num int;
    DECLARE pos int;
    SET num =  (NEW.PERM_ROLE-1) DIV 32;
    SET 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( ((REPORT_ID-1) DIV 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 IGNORE 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( ((PERM_ROLE-1) DIV 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( ((REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_ROLE_HMI_INFO;
DELIMITER ;;
create trigger TUA_RM_ROLE_HMI_INFO AFTER UPDATE on rm_role_hmi_info for each ROW 
BEGIN
    #PERM_TYPE = 1是报表,0是HMI画面
    #先清除,后插入rm_userg_report_info
    DECLARE oldNum int;
    DECLARE oldPos int;
    DECLARE newNum int;
    DECLARE newPos int;
    SET oldNum =  (OLD.PERM_ROLE-1) DIV 32;
    SET oldPos = IF(OLD.PERM_ROLE%32=0,32,OLD.PERM_ROLE%32);
    SET newNum =  (NEW.PERM_ROLE-1) DIV 32;
    SET 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( ((REPORT_ID-1) DIV 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 IGNORE 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( ((PERM_ROLE-1) DIV 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( ((REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_ROLE_HMI_INFO;
DELIMITER ;;
create trigger TDA_RM_ROLE_HMI_INFO AFTER DELETE on rm_role_hmi_info for each ROW 
BEGIN
    #PERM_TYPE = 1是报表,0是HMI画面
    #先清除,后插入rm_userg_report_info
    DECLARE num int;
    DECLARE pos int;
    SET num =  (OLD.PERM_ROLE-1) DIV 32;
    SET 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( ((REPORT_ID-1) DIV 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 IGNORE 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( ((PERM_ROLE-1) DIV 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( ((REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop table if exists rm_role_spef_reg;
CREATE TABLE `rm_role_spef_reg` (
  `PERM_ROLE` decimal(10,0) NOT NULL COMMENT '所属角色',
  `PERM_SPEFUNC` decimal(10,0) NOT NULL COMMENT '特殊功能',
  `PERM_REGION1` decimal(10,0) DEFAULT NULL COMMENT '责任区1',
  `PERM_REGION2` decimal(10,0) DEFAULT NULL COMMENT '责任区2',
  `PERM_REGION3` decimal(10,0) DEFAULT NULL COMMENT '责任区3',
  `PERM_REGION4` decimal(10,0) DEFAULT NULL COMMENT '责任区4',
  PRIMARY KEY (`PERM_ROLE`,`PERM_SPEFUNC`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色特殊功能责任区权限信息表';

drop trigger if exists TIA_RM_ROLE_SPEF_REG;
DELIMITER ;;
create trigger TIA_RM_ROLE_SPEF_REG AFTER INSERT on rm_role_spef_reg for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_ROLE_SPEF_REG;
DELIMITER ;;
create trigger TUA_RM_ROLE_SPEF_REG AFTER UPDATE on rm_role_spef_reg for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_ROLE_SPEF_REG;
DELIMITER ;;
create trigger TDA_RM_ROLE_SPEF_REG AFTER DELETE on rm_role_spef_reg for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists rm_security_def;
CREATE TABLE `rm_security_def` (
  `PERM_ID` decimal(10,0) NOT NULL COMMENT '安全策略ID号',
  `PERM_MIN_LENGTH` decimal(10,0) DEFAULT NULL COMMENT '密码最小长度',
  `PERM_MIN_LETR` decimal(10,0) DEFAULT NULL COMMENT '密码最少字符数',
  `PERM_MIN_NUM` decimal(10,0) DEFAULT NULL COMMENT '密码最少数字式',
  `PERM_MAX_LOCK` decimal(10,0) DEFAULT NULL COMMENT '账户锁定阀值',
  `PERM_LOCK_TIMER` decimal(10,0) DEFAULT NULL COMMENT '账户锁定时长',
  PRIMARY KEY (`PERM_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='权限安全策略表';

drop trigger if exists TIA_RM_SECURITY_DEF;
DELIMITER ;;
create trigger TIA_RM_SECURITY_DEF AFTER INSERT on rm_security_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_SECURITY_DEF;
DELIMITER ;;
create trigger TUA_RM_SECURITY_DEF AFTER UPDATE on rm_security_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_SECURITY_DEF;
DELIMITER ;;
create trigger TDA_RM_SECURITY_DEF AFTER DELETE on rm_security_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists rm_spcl_func_def;
CREATE TABLE `rm_spcl_func_def` (
  `PERM_ID` decimal(10,0) NOT NULL COMMENT '功能ID号',
  `PERM_NAME` varchar(64) NOT NULL COMMENT '功能名称',
  `PERM_DEFINE` varchar(32) NOT NULL COMMENT '功能宏定义',
  `PERM_DESC` varchar(100) DEFAULT NULL COMMENT '功能描述',
  PRIMARY KEY (`PERM_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='权限特殊功能定义表';

drop trigger if exists TIA_RM_SPCL_FUNC_DEF;
DELIMITER ;;
create trigger TIA_RM_SPCL_FUNC_DEF AFTER INSERT on rm_spcl_func_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_SPCL_FUNC_DEF;
DELIMITER ;;
create trigger TUA_RM_SPCL_FUNC_DEF AFTER UPDATE on rm_spcl_func_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_SPCL_FUNC_DEF;
DELIMITER ;;
create trigger TDA_RM_SPCL_FUNC_DEF AFTER DELETE on rm_spcl_func_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists rm_user_def;
CREATE TABLE `rm_user_def` (
  `PERM_ID` decimal(10,0) NOT NULL COMMENT '用户ID号',
  `PERM_NAME` varchar(64) NOT NULL COMMENT '用户名称',
  `PERM_ALIAS` varchar(64) DEFAULT NULL COMMENT '用户姓名',
  `PERM_PASSWORD` varchar(64) NOT NULL COMMENT '用户口令',
  `PERM_CREATE_DATE` decimal(20,0) DEFAULT NULL COMMENT '用户创建时间',
  `PERM_EXPIRE_DATE` decimal(20,0) DEFAULT NULL COMMENT '用户失效时间',
  `PERM_DESC` varchar(128) DEFAULT NULL COMMENT '用户描述',
  `PERM_IF_LOCK` decimal(10,0) DEFAULT NULL COMMENT '账户是否锁定',
  `PERM_LOCK_TIME` decimal(20,0) DEFAULT NULL COMMENT '账户锁定时间',
  `PERM_USER_GROUP1` decimal(10,0) DEFAULT NULL COMMENT '所属用户组1',
  `PERM_USER_GROUP2` decimal(10,0) DEFAULT NULL COMMENT '所属用户组2',
  `PERM_USER_GROUP3` decimal(10,0) DEFAULT NULL COMMENT '所属用户组3',
  `PERM_USER_GROUP4` decimal(10,0) DEFAULT NULL COMMENT '所属用户组4',
  `PERM_USER_GROUP5` decimal(10,0) DEFAULT NULL COMMENT '所属用户组5',
  `PERM_USER_GROUP6` decimal(10,0) DEFAULT NULL COMMENT '所属用户组6',
  `PERM_USER_GROUP7` decimal(10,0) DEFAULT NULL COMMENT '所属用户组7',
  `PERM_USER_GROUP8` decimal(10,0) DEFAULT NULL COMMENT '所属用户组8',
  `PERM_USER_GROUP9` decimal(10,0) DEFAULT NULL COMMENT '所属用户组9',
  `PERM_USER_GROUP10` decimal(10,0) DEFAULT NULL COMMENT '所属用户组10',
  `PERM_USER_GROUP11` decimal(10,0) DEFAULT NULL COMMENT '所属用户组11',
  `PERM_USER_GROUP12` decimal(10,0) DEFAULT NULL COMMENT '所属用户组12',
  `PERM_USER_GROUP13` decimal(10,0) DEFAULT NULL COMMENT '所属用户组13',
  `PERM_USER_GROUP14` decimal(10,0) DEFAULT NULL COMMENT '所属用户组14',
  `PERM_USER_GROUP15` decimal(10,0) DEFAULT NULL COMMENT '所属用户组15',
  `PERM_USER_GROUP16` decimal(10,0) DEFAULT NULL COMMENT '所属用户组16',
  `PERM_GROUP_LEADER1` decimal(10,0) DEFAULT NULL COMMENT '组长所属组1',
  `PERM_GROUP_LEADER2` decimal(10,0) DEFAULT NULL COMMENT '组长所属组2',
  `PERM_GROUP_LEADER3` decimal(10,0) DEFAULT NULL COMMENT '组长所属组3',
  `PERM_GROUP_LEADER4` decimal(10,0) DEFAULT NULL COMMENT '组长所属组4',
  `PERM_GROUP_LEADER5` decimal(10,0) DEFAULT NULL COMMENT '组长所属组5',
  `PERM_GROUP_LEADER6` decimal(10,0) DEFAULT NULL COMMENT '组长所属组6',
  `PERM_GROUP_LEADER7` decimal(10,0) DEFAULT NULL COMMENT '组长所属组7',
  `PERM_GROUP_LEADER8` decimal(10,0) DEFAULT NULL COMMENT '组长所属组8',
  `PERM_GROUP_LEADER9` decimal(10,0) DEFAULT NULL COMMENT '组长所属组9',
  `PERM_GROUP_LEADER10` decimal(10,0) DEFAULT NULL COMMENT '组长所属组10',
  `PERM_GROUP_LEADER11` decimal(10,0) DEFAULT NULL COMMENT '组长所属组11',
  `PERM_GROUP_LEADER12` decimal(10,0) DEFAULT NULL COMMENT '组长所属组12',
  `PERM_GROUP_LEADER13` decimal(10,0) DEFAULT NULL COMMENT '组长所属组13',
  `PERM_GROUP_LEADER14` decimal(10,0) DEFAULT NULL COMMENT '组长所属组14',
  `PERM_GROUP_LEADER15` decimal(10,0) DEFAULT NULL COMMENT '组长所属组15',
  `PERM_GROUP_LEADER16` decimal(10,0) DEFAULT NULL COMMENT '组长所属组16',
  `PERM_DEPT1` decimal(10,0) DEFAULT NULL COMMENT '所属部门1',
  `PERM_DEPT2` decimal(10,0) DEFAULT NULL COMMENT '所属部门2',
  `PERM_DEPT3` decimal(10,0) DEFAULT NULL COMMENT '所属部门3',
  `PERM_DEPT4` decimal(10,0) DEFAULT NULL COMMENT '所属部门4',
  `MOBILE_PHONE` varchar(32) DEFAULT NULL COMMENT '手机号',
  `EMAIL` varchar(64) DEFAULT NULL COMMENT '邮箱地址',
  `ADDRESS` varchar(128) DEFAULT NULL COMMENT '通讯地址',
  `OPEN_ID` varchar(255) DEFAULT NULL COMMENT 'APP会话ID',
  `ALLOW_DURATION` decimal(10,0) DEFAULT NULL COMMENT '允许持续时间',
  PRIMARY KEY (`PERM_ID`),
  UNIQUE KEY `UN_RM_USER_DEF` (`PERM_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户定义表';

drop trigger if exists TIA_RM_USER_DEF;
DELIMITER ;;
create trigger TIA_RM_USER_DEF AFTER INSERT on rm_user_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_USER_DEF;
DELIMITER ;;
create trigger TUA_RM_USER_DEF AFTER UPDATE on rm_user_def for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_USER_DEF;
DELIMITER ;;
create trigger TDA_RM_USER_DEF AFTER DELETE on rm_user_def for each ROW 
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;
END;;
DELIMITER ;

drop table if exists rm_user_face_info;
CREATE TABLE `rm_user_face_info` (
  `USER_ID` decimal(10,0) NOT NULL COMMENT '用户ID，关联用户',
  `DESCRIPTION` varchar(64) DEFAULT NULL COMMENT '描述',
  `DATA_BASE64` mediumtext NOT NULL COMMENT '人脸照片，BASE64编码',
  PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储用户的人脸信息';

drop table if exists rm_user_finger_print_info;
CREATE TABLE `rm_user_finger_print_info` (
  `USER_ID` decimal(10,0) NOT NULL COMMENT '用户ID，关联用户',
  `FP_ID` decimal(10,0) NOT NULL COMMENT '指纹ID，有效值1至10',
  `DESCRIPTION` varchar(64) DEFAULT NULL COMMENT '描述，用户可填写“食指”等',
  `DATA_BASE64` varchar(2048) NOT NULL COMMENT '指纹数据，BASE64编码',
  PRIMARY KEY (`USER_ID`,`FP_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储用户的指纹信息';

drop table if exists rm_user_signature_info;
CREATE TABLE `rm_user_signature_info` (
  `USER_ID` decimal(10,0) NOT NULL COMMENT '用户ID，关联用户表RM_USER_DEF',
  `DATA_BASE64` mediumtext NOT NULL COMMENT '电子签章照片，BASE64编码',
  PRIMARY KEY (`USER_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='存储用户电子签章信息';

drop table if exists rm_userg_db_info;
CREATE TABLE `rm_userg_db_info` (
  `PERM_USER_GROUP` decimal(10,0) NOT NULL COMMENT '所属用户组',
  `PERM_ATTRIBUTE` decimal(10,0) NOT NULL COMMENT '权限属性',
  `PERM_TABLE_NAME` varchar(32) NOT NULL COMMENT '数据库表名',
  PRIMARY KEY (`PERM_USER_GROUP`,`PERM_TABLE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户组数据表权限信息表';

drop trigger if exists TIA_RM_USERG_DB_INFO;
DELIMITER ;;
create trigger TIA_RM_USERG_DB_INFO AFTER INSERT on rm_userg_db_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_USERG_DB_INFO;
DELIMITER ;;
create trigger TUA_RM_USERG_DB_INFO AFTER UPDATE on rm_userg_db_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_USERG_DB_INFO;
DELIMITER ;;
create trigger TDA_RM_USERG_DB_INFO AFTER DELETE on rm_userg_db_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists rm_userg_def;
CREATE TABLE `rm_userg_def` (
  `PERM_ID` decimal(10,0) NOT NULL COMMENT '用户组ID号',
  `PERM_NAME` varchar(64) NOT NULL COMMENT '用户组名称',
  `PERM_LEVEL` decimal(10,0) DEFAULT NULL COMMENT '用户组级别',
  `PERM_ROLE1` decimal(10,0) DEFAULT NULL COMMENT '用户组角色1',
  `PERM_ROLE2` decimal(10,0) DEFAULT NULL COMMENT '用户组角色2',
  `PERM_ROLE3` decimal(10,0) DEFAULT NULL COMMENT '用户组角色3',
  `PERM_ROLE4` decimal(10,0) DEFAULT NULL COMMENT '用户组角色4',
  `PERM_ROLE5` decimal(10,0) DEFAULT NULL COMMENT '用户组角色5',
  `PERM_ROLE6` decimal(10,0) DEFAULT NULL COMMENT '用户组角色6',
  `PERM_ROLE7` decimal(10,0) DEFAULT NULL COMMENT '用户组角色7',
  `PERM_ROLE8` decimal(10,0) DEFAULT NULL COMMENT '用户组角色8',
  `PERM_LOCATION1` decimal(10,0) DEFAULT NULL COMMENT '可管理车站1',
  `PERM_LOCATION2` decimal(10,0) DEFAULT NULL COMMENT '可管理车站2',
  `PERM_LOCATION3` decimal(10,0) DEFAULT NULL COMMENT '可管理车站3',
  `PERM_LOCATION4` decimal(10,0) DEFAULT NULL COMMENT '可管理车站4',
  `PERM_LOCATION5` decimal(10,0) DEFAULT NULL COMMENT '可管理车站5',
  `PERM_LOCATION6` decimal(10,0) DEFAULT NULL COMMENT '可管理车站6',
  `PERM_LOCATION7` decimal(10,0) DEFAULT NULL COMMENT '可管理车站7',
  `PERM_LOCATION8` decimal(10,0) DEFAULT NULL COMMENT '可管理车站8',
  `PERM_DESC` varchar(100) DEFAULT NULL COMMENT '用户组描述',
  `PERM_PIC1` varchar(64) DEFAULT NULL COMMENT '屏登陆画面1',
  `PERM_PIC2` varchar(64) DEFAULT NULL COMMENT '屏登陆画面2',
  `PERM_PIC3` varchar(64) DEFAULT NULL COMMENT '屏登陆画面3',
  `PERM_PIC4` varchar(64) DEFAULT NULL COMMENT '屏登陆画面4',
  `PERM_PIC5` varchar(64) DEFAULT NULL COMMENT '屏登陆画面5',
  `PERM_PIC6` varchar(64) DEFAULT NULL COMMENT '屏登陆画面6',
  PRIMARY KEY (`PERM_ID`),
  UNIQUE KEY `UN_RM_USERG_DEF` (`PERM_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户组定义表';

drop trigger if exists TIA_RM_USERG_DEF;
DELIMITER ;;
create trigger TIA_RM_USERG_DEF AFTER INSERT on rm_userg_def for each ROW 
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( ((REPORT_ID-1) DIV 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 IGNORE 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( ((PERM_ROLE-1) DIV 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( ((REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_USERG_DEF;
DELIMITER ;;
create trigger TUA_RM_USERG_DEF AFTER UPDATE on rm_userg_def for each ROW 
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( ((REPORT_ID-1) DIV 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 IGNORE 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( ((PERM_ROLE-1) DIV 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( ((REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_USERG_DEF;
DELIMITER ;;
create trigger TDA_RM_USERG_DEF AFTER DELETE on rm_userg_def for each ROW 
BEGIN
#删除用户中的用户组
    DECLARE num int;
    SET num =  (OLD.PERM_ID-1) DIV 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;
END;;
DELIMITER ;

drop table if exists rm_userg_hmi_info;
CREATE TABLE `rm_userg_hmi_info` (
  `PERM_USER_GROUP` decimal(10,0) NOT NULL COMMENT '所属用户组',
  `PERM_TYPE` decimal(10,0) NOT NULL COMMENT '权限类型',
  `PERM_INFO1` decimal(10,0) DEFAULT NULL COMMENT '权限信息1',
  `PERM_INFO2` decimal(10,0) DEFAULT NULL COMMENT '权限信息2',
  `PERM_INFO3` decimal(10,0) DEFAULT NULL COMMENT '权限信息3',
  `PERM_INFO4` decimal(10,0) DEFAULT NULL COMMENT '权限信息4',
  `PERM_INFO5` decimal(10,0) DEFAULT NULL COMMENT '权限信息5',
  `PERM_INFO6` decimal(10,0) DEFAULT NULL COMMENT '权限信息6',
  `PERM_INFO7` decimal(10,0) DEFAULT NULL COMMENT '权限信息7',
  `PERM_INFO8` decimal(10,0) DEFAULT NULL COMMENT '权限信息8',
  `PERM_INFO9` decimal(10,0) DEFAULT NULL COMMENT '权限信息9',
  `PERM_INFO10` decimal(10,0) DEFAULT NULL COMMENT '权限信息10',
  `PERM_INFO11` decimal(10,0) DEFAULT NULL COMMENT '权限信息11',
  `PERM_INFO12` decimal(10,0) DEFAULT NULL COMMENT '权限信息12',
  `PERM_INFO13` decimal(10,0) DEFAULT NULL COMMENT '权限信息13',
  `PERM_INFO14` decimal(10,0) DEFAULT NULL COMMENT '权限信息14',
  `PERM_INFO15` decimal(10,0) DEFAULT NULL COMMENT '权限信息15',
  `PERM_INFO16` decimal(10,0) DEFAULT NULL COMMENT '权限信息16',
  `PERM_ATTRIBUTE` decimal(10,0) NOT NULL COMMENT '权限属性',
  PRIMARY KEY (`PERM_USER_GROUP`,`PERM_TYPE`,`PERM_ATTRIBUTE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户组画面权限信息表';

drop trigger if exists TIA_RM_USERG_HMI_INFO;
DELIMITER ;;
create trigger TIA_RM_USERG_HMI_INFO AFTER INSERT on rm_userg_hmi_info for each ROW 
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( ((REPORT_ID-1) DIV 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 IGNORE 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( ((PERM_ROLE-1) DIV 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( ((REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_USERG_HMI_INFO;
DELIMITER ;;
create trigger TUA_RM_USERG_HMI_INFO AFTER UPDATE on rm_userg_hmi_info for each ROW 
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( ((REPORT_ID-1) DIV 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 IGNORE 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( ((PERM_ROLE-1) DIV 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( ((REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_USERG_HMI_INFO;
DELIMITER ;;
create trigger TDA_RM_USERG_HMI_INFO AFTER DELETE on rm_userg_hmi_info for each ROW 
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 IGNORE 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( ((PERM_ROLE-1) DIV 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( ((REPORT_ID-1) DIV 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;
END;;
DELIMITER ;

drop table if exists rm_userg_report_info;
CREATE TABLE `rm_userg_report_info` (
  `PERM_USER_GROUP` decimal(10,0) NOT NULL,
  `REPORT_CODE` varchar(32) NOT NULL,
  PRIMARY KEY (`PERM_USER_GROUP`,`REPORT_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

drop table if exists rm_userg_rsp_info;
CREATE TABLE `rm_userg_rsp_info` (
  `PERM_USER_GROUP` decimal(10,0) NOT NULL COMMENT '所属用户组',
  `PERM_ROLE` decimal(10,0) NOT NULL COMMENT '所属角色',
  `PERM_SPEFUNC` decimal(10,0) NOT NULL COMMENT '特殊功能',
  `PERM_REGION1` decimal(10,0) DEFAULT NULL COMMENT '责任区1',
  `PERM_REGION2` decimal(10,0) DEFAULT NULL COMMENT '责任区2',
  `PERM_REGION3` decimal(10,0) DEFAULT NULL COMMENT '责任区3',
  `PERM_REGION4` decimal(10,0) DEFAULT NULL COMMENT '责任区4',
  `PERM_LOCATION1` decimal(10,0) DEFAULT NULL COMMENT '车站1',
  `PERM_LOCATION2` decimal(10,0) DEFAULT NULL COMMENT '车站2',
  `PERM_LOCATION3` decimal(10,0) DEFAULT NULL COMMENT '车站3',
  `PERM_LOCATION4` decimal(10,0) DEFAULT NULL COMMENT '车站4',
  `PERM_LOCATION5` decimal(10,0) DEFAULT NULL COMMENT '车站5',
  `PERM_LOCATION6` decimal(10,0) DEFAULT NULL COMMENT '车站6',
  `PERM_LOCATION7` decimal(10,0) DEFAULT NULL COMMENT '车站7',
  `PERM_LOCATION8` decimal(10,0) DEFAULT NULL COMMENT '车站8',
  PRIMARY KEY (`PERM_USER_GROUP`,`PERM_ROLE`,`PERM_SPEFUNC`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户组特殊权限信息表';

drop trigger if exists TIA_RM_USERG_RSP_INFO;
DELIMITER ;;
create trigger TIA_RM_USERG_RSP_INFO AFTER INSERT on rm_userg_rsp_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_RM_USERG_RSP_INFO;
DELIMITER ;;
create trigger TUA_RM_USERG_RSP_INFO AFTER UPDATE on rm_userg_rsp_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_RM_USERG_RSP_INFO;
DELIMITER ;;
create trigger TDA_RM_USERG_RSP_INFO AFTER DELETE on rm_userg_rsp_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists safety_day;
CREATE TABLE `safety_day` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '标签名',
  `BEGIN_TIME` decimal(20,0) NOT NULL COMMENT '起始时间，UTC秒数',
  `DAY_DIFF` decimal(10,0) NOT NULL COMMENT '安全运行天数',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='安全天数';

drop trigger if exists TIA_SAFETY_DAY;
DELIMITER ;;
create trigger TIA_SAFETY_DAY AFTER INSERT on safety_day for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SAFETY_DAY;
DELIMITER ;;
create trigger TUA_SAFETY_DAY AFTER UPDATE on safety_day for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SAFETY_DAY;
DELIMITER ;;
create trigger TDA_SAFETY_DAY AFTER DELETE on safety_day for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sample_define;
CREATE TABLE `sample_define` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '点标签',
  `POINT_TYPE` decimal(10,0) NOT NULL COMMENT '点类型',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '点描述',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站ID',
  `SUB_SYSTEM` decimal(10,0) NOT NULL COMMENT '所属专业',
  `DEVICE_TAG` varchar(64) DEFAULT NULL COMMENT '设备标签',
  `SAMPLE_PERIOD` decimal(10,0) DEFAULT NULL COMMENT '采样周期(分钟)',
  `SAMPLE_DEADBAND` decimal(22,6) DEFAULT NULL COMMENT '采样死区值',
  `VALUE` decimal(22,6) DEFAULT NULL COMMENT '值',
  `STATUS` decimal(10,0) DEFAULT NULL COMMENT '状态',
  `UPDATE_TIME` decimal(20,0) DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`TAG_NAME`,`POINT_TYPE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='采样定义表';

drop trigger if exists TIA_SAMPLE_DEFINE;
DELIMITER ;;
create trigger TIA_SAMPLE_DEFINE AFTER INSERT on sample_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SAMPLE_DEFINE;
DELIMITER ;;
create trigger TUA_SAMPLE_DEFINE AFTER UPDATE on sample_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SAMPLE_DEFINE;
DELIMITER ;;
create trigger TDA_SAMPLE_DEFINE AFTER DELETE on sample_define for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_app_info;
CREATE TABLE `sys_model_app_info` (
  `APP_ID` decimal(10,0) NOT NULL COMMENT '应用ID-下标1开始',
  `TAG_NAME` varchar(32) NOT NULL COMMENT '应用标签名',
  `DESCRIPTION` varchar(64) NOT NULL COMMENT '应用描述',
  `SUB_SYSTEM` varchar(128) NOT NULL COMMENT '应用包含专业',
  `ORDER_NO` decimal(10,0) NOT NULL COMMENT '应用启动顺序',
  PRIMARY KEY (`APP_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-应用信息表';

drop trigger if exists TIA_SYS_MODEL_APP_INFO;
DELIMITER ;;
create trigger TIA_SYS_MODEL_APP_INFO AFTER INSERT on sys_model_app_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_APP_INFO;
DELIMITER ;;
create trigger TUA_SYS_MODEL_APP_INFO AFTER UPDATE on sys_model_app_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_APP_INFO;
DELIMITER ;;
create trigger TDA_SYS_MODEL_APP_INFO AFTER DELETE on sys_model_app_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_dbconfig_domain;
CREATE TABLE `sys_model_dbconfig_domain` (
  `DOMAIN_ID` decimal(10,0) NOT NULL,
  `FIRST_CONNECT` varchar(48) NOT NULL COMMENT '首链接',
  `NUM1` decimal(10,0) NOT NULL COMMENT '本地数据库连接个',
  `LOCAL1` varchar(48) DEFAULT NULL COMMENT '节点1',
  `LOCAL2` varchar(48) DEFAULT NULL COMMENT '节点2',
  `LOCAL3` varchar(48) DEFAULT NULL COMMENT '节点3',
  `LOCAL4` varchar(48) DEFAULT NULL COMMENT '节点4',
  `NUM2` decimal(10,0) DEFAULT NULL COMMENT '远程数据库连接个数',
  `REMOTE1` decimal(10,0) DEFAULT NULL COMMENT '远程域1,主要用于同时写远程库',
  `REMOTE2` decimal(10,0) DEFAULT NULL COMMENT '远程域2',
  `REMOTE3` decimal(10,0) DEFAULT NULL COMMENT '远程域3',
  `REMOTE4` decimal(10,0) DEFAULT NULL COMMENT '远程域4',
  PRIMARY KEY (`DOMAIN_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-数据库域配置';

drop trigger if exists TIA_SYS_MODEL_DBCONFIG_DOMAIN;
DELIMITER ;;
create trigger TIA_SYS_MODEL_DBCONFIG_DOMAIN AFTER INSERT on sys_model_dbconfig_domain for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_DBCONFIG_DOMAIN;
DELIMITER ;;
create trigger TUA_SYS_MODEL_DBCONFIG_DOMAIN AFTER UPDATE on sys_model_dbconfig_domain for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_DBCONFIG_DOMAIN;
DELIMITER ;;
create trigger TDA_SYS_MODEL_DBCONFIG_DOMAIN AFTER DELETE on sys_model_dbconfig_domain for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_dbconfig_node;
CREATE TABLE `sys_model_dbconfig_node` (
  `NODE_NAME` varchar(64) NOT NULL,
  `DB_TYPE` decimal(10,0) NOT NULL COMMENT '数据库类型',
  `DB_SERVICE_NAME` varchar(16) NOT NULL COMMENT '数据库服务名',
  `USER_NAME` varchar(16) NOT NULL COMMENT '数据库用户名',
  `USER_PASSWORD` varchar(16) NOT NULL COMMENT '数据库密码',
  `NIC_CUR_ID` decimal(10,0) DEFAULT NULL COMMENT '活动网卡ID',
  `DB_NAME` varchar(16) DEFAULT NULL COMMENT '数据库标识（主数据库/备数据库）',
  `DB_HOST_PRIOR` decimal(10,0) DEFAULT NULL COMMENT '域内优先级',
  `IS_DB_SWITCH` decimal(10,0) DEFAULT NULL COMMENT '是否可以切换',
  `IS_DIRECT_ACCESS` decimal(10,0) NOT NULL COMMENT '是否可以直连',
  `DB_PORT` decimal(10,0) DEFAULT NULL,
  PRIMARY KEY (`NODE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模数据库节点配置';

drop trigger if exists TIA_SYS_MODEL_DBCONFIG_NODE;
DELIMITER ;;
create trigger TIA_SYS_MODEL_DBCONFIG_NODE AFTER INSERT on sys_model_dbconfig_node for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_DBCONFIG_NODE;
DELIMITER ;;
create trigger TUA_SYS_MODEL_DBCONFIG_NODE AFTER UPDATE on sys_model_dbconfig_node for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_DBCONFIG_NODE;
DELIMITER ;;
create trigger TDA_SYS_MODEL_DBCONFIG_NODE AFTER DELETE on sys_model_dbconfig_node for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_deploy_info;
CREATE TABLE `sys_model_deploy_info` (
  `DOMAIN_ID` decimal(10,0) NOT NULL COMMENT '域名',
  `APP_ID` decimal(10,0) NOT NULL COMMENT '应用ID',
  `DESCRIPTION` varchar(64) DEFAULT NULL COMMENT '部署描述',
  `NUM` decimal(10,0) DEFAULT NULL COMMENT '部署的服务器数量',
  `NODE1` varchar(64) NOT NULL COMMENT '部署应用的机器节点1',
  `NODE2` varchar(64) DEFAULT NULL COMMENT '部署应用的机器节点2',
  `NODE3` varchar(64) DEFAULT NULL COMMENT '部署应用的机器节点3',
  `NODE4` varchar(64) DEFAULT NULL COMMENT '部署应用的机器节点4',
  `NODE5` varchar(64) DEFAULT NULL COMMENT '部署应用的机器节点5',
  `NODE6` varchar(64) DEFAULT NULL COMMENT '部署应用的机器节点6',
  `NODE7` varchar(64) DEFAULT NULL COMMENT '部署应用的机器节点7',
  `NODE8` varchar(64) DEFAULT NULL COMMENT '部署应用的机器节点8',
  PRIMARY KEY (`DOMAIN_ID`,`APP_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-部署信息表';

drop trigger if exists TIA_SYS_MODEL_DEPLOY_INFO;
DELIMITER ;;
create trigger TIA_SYS_MODEL_DEPLOY_INFO AFTER INSERT on sys_model_deploy_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_DEPLOY_INFO;
DELIMITER ;;
create trigger TUA_SYS_MODEL_DEPLOY_INFO AFTER UPDATE on sys_model_deploy_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_DEPLOY_INFO;
DELIMITER ;;
create trigger TDA_SYS_MODEL_DEPLOY_INFO AFTER DELETE on sys_model_deploy_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_domain_info;
CREATE TABLE `sys_model_domain_info` (
  `DOMAIN_ID` decimal(10,0) NOT NULL COMMENT '域ID',
  `TAG_NAME` varchar(32) DEFAULT NULL COMMENT '域名称',
  `IS_CENTER_DOMAIN` decimal(10,0) NOT NULL COMMENT '是否中心域',
  `DESCRIPTION` varchar(32) DEFAULT NULL COMMENT '域描述',
  PRIMARY KEY (`DOMAIN_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-域信息表';

drop trigger if exists TIA_SYS_MODEL_DOMAIN_INFO;
DELIMITER ;;
create trigger TIA_SYS_MODEL_DOMAIN_INFO AFTER INSERT on sys_model_domain_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_DOMAIN_INFO;
DELIMITER ;;
create trigger TUA_SYS_MODEL_DOMAIN_INFO AFTER UPDATE on sys_model_domain_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_DOMAIN_INFO;
DELIMITER ;;
create trigger TDA_SYS_MODEL_DOMAIN_INFO AFTER DELETE on sys_model_domain_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_location_info;
CREATE TABLE `sys_model_location_info` (
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '车站ID',
  `TAG_NAME` varchar(32) NOT NULL COMMENT '车站标签-唯一',
  `DESCRIPTION` varchar(128) NOT NULL COMMENT '车站描述-中文名',
  `DOMAIN_ID` decimal(10,0) NOT NULL,
  `LOCATION_NO` decimal(10,0) DEFAULT NULL COMMENT '车站位置号',
  `KILOMETERS` decimal(10,0) DEFAULT NULL COMMENT '站中心公里标-备用',
  `IS_CENTRAL` decimal(10,0) DEFAULT NULL COMMENT '是否中心站',
  `OCC_NO` decimal(10,0) DEFAULT NULL COMMENT '中心站号-备用',
  `CONTROL_TYPE` decimal(10,0) DEFAULT NULL COMMENT '控制模式-备用',
  `AUTO_TRIGGER` decimal(10,0) DEFAULT NULL COMMENT '自动触发模式-备用',
  `CBI_NO` decimal(10,0) DEFAULT NULL COMMENT '连锁编号-备用',
  `ZC_NO` decimal(10,0) DEFAULT NULL COMMENT '控制区域编号-备用',
  `LOCATION_TYPE` decimal(10,0) DEFAULT NULL COMMENT '位置类型-备用',
  `PLOCATION_ID` decimal(10,0) DEFAULT NULL COMMENT '父位置ID-备用',
  `LOCATION_LEVEL` decimal(10,0) DEFAULT NULL COMMENT '位置等级-车站',
  PRIMARY KEY (`LOCATION_ID`),
  UNIQUE KEY `UN_SYS_MODEL_LOCATION_INFO` (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-车站信息表';

drop trigger if exists TIA_SYS_MODEL_LOCATION_INFO;
DELIMITER ;;
create trigger TIA_SYS_MODEL_LOCATION_INFO AFTER INSERT on sys_model_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_LOCATION_INFO;
DELIMITER ;;
create trigger TUA_SYS_MODEL_LOCATION_INFO AFTER UPDATE on sys_model_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_LOCATION_INFO;
DELIMITER ;;
create trigger TDA_SYS_MODEL_LOCATION_INFO AFTER DELETE on sys_model_location_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_msg_relay;
CREATE TABLE `sys_model_msg_relay` (
  `SRC_DOMAIN_ID` decimal(10,0) NOT NULL COMMENT '源域ID',
  `DST_DOMAIN_ID` decimal(10,0) NOT NULL COMMENT '目标域ID',
  `SUB_APP_ID` decimal(10,0) NOT NULL COMMENT '消息主题，应用ID',
  `SUB_CHANNEL_ID` varchar(512) DEFAULT NULL COMMENT '消息主题，通道ID，半角逗号“,”分割。允许为空，表示不订阅转发，但建立转发连接，允许定向发送给目标域的peer、host、domain',
  PRIMARY KEY (`SRC_DOMAIN_ID`,`DST_DOMAIN_ID`,`SUB_APP_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-消息转发通道配置';

drop trigger if exists TIA_SYS_MODEL_MSG_RELAY;
DELIMITER ;;
create trigger TIA_SYS_MODEL_MSG_RELAY AFTER INSERT on sys_model_msg_relay for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_MSG_RELAY;
DELIMITER ;;
create trigger TUA_SYS_MODEL_MSG_RELAY AFTER UPDATE on sys_model_msg_relay for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_MSG_RELAY;
DELIMITER ;;
create trigger TDA_SYS_MODEL_MSG_RELAY AFTER DELETE on sys_model_msg_relay for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_node_info;
CREATE TABLE `sys_model_node_info` (
  `NODE_NAME` varchar(64) NOT NULL COMMENT '机器节点名称',
  `LOCATION_ID` decimal(10,0) NOT NULL COMMENT '所属车站',
  `DESCRIPTION` varchar(64) NOT NULL COMMENT '节点描述-中文名称',
  `NODE_TYPE` decimal(10,0) DEFAULT NULL COMMENT '节点类型-（工作站/实时服务器/数据服务器）',
  `IS_USED` decimal(10,0) DEFAULT NULL COMMENT '是否使用',
  `NIC_NUM` decimal(10,0) DEFAULT NULL COMMENT '网卡数',
  `NIC1_NAME` varchar(64) DEFAULT NULL COMMENT '网卡1名称',
  `NIC1_ADDRESS` varchar(32) DEFAULT NULL COMMENT '网卡1IP地址',
  `NIC1_NETMASK` varchar(32) DEFAULT NULL COMMENT '子网掩码',
  `NIC1_GATEWAY` varchar(32) DEFAULT NULL COMMENT '网卡1网关',
  `NIC2_NAME` varchar(64) DEFAULT NULL COMMENT '网卡2名称',
  `NIC2_ADDRESS` varchar(32) DEFAULT NULL COMMENT '网卡2IP地址',
  `NIC2_NETMASK` varchar(32) DEFAULT NULL COMMENT '网卡2子网掩码',
  `NIC2_GATEWAY` varchar(32) DEFAULT NULL COMMENT '网卡2网关',
  PRIMARY KEY (`NODE_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-节点配置';

drop trigger if exists TIA_SYS_MODEL_NODE_INFO;
DELIMITER ;;
create trigger TIA_SYS_MODEL_NODE_INFO AFTER INSERT on sys_model_node_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_NODE_INFO;
DELIMITER ;;
create trigger TUA_SYS_MODEL_NODE_INFO AFTER UPDATE on sys_model_node_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_NODE_INFO;
DELIMITER ;;
create trigger TDA_SYS_MODEL_NODE_INFO AFTER DELETE on sys_model_node_info for each ROW 
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;
END;;
DELIMITER ;

drop table if exists sys_model_process_info;
CREATE TABLE `sys_model_process_info` (
  `PROC_ALIAS` varchar(48) NOT NULL COMMENT '进程别名',
  `APP_ID` decimal(10,0) DEFAULT NULL COMMENT '应用ID',
  `PROC_NAME` varchar(64) NOT NULL COMMENT '进程名称',
  `PROC_PARA` varchar(96) DEFAULT NULL COMMENT '进程参数',
  `INSTANCE_NUM` decimal(10,0) DEFAULT NULL COMMENT '运行实例数-备用',
  `NOT_USED` decimal(10,0) DEFAULT NULL COMMENT '是否停用',
  `START_TYPE` decimal(10,0) DEFAULT NULL COMMENT '启动类型-（启动一次/常驻内存）',
  `RUN_ORDER` decimal(10,0) DEFAULT NULL COMMENT '启动顺序',
  `IS_AUTO_RUN` decimal(10,0) DEFAULT NULL COMMENT '是否自动运行-备用',
  `IS_CYCLE_START` decimal(10,0) DEFAULT NULL COMMENT '是否周期启动-备用',
  `IS_CYCLE_PERIOD` decimal(10,0) DEFAULT NULL COMMENT '是否过段时间运行-备用',
  `IS_OFFLIEN_RUN` decimal(10,0) DEFAULT NULL COMMENT '网络故障是否运行-备用',
  `IS_DUTY_RUN` decimal(10,0) DEFAULT NULL COMMENT '是否值机态运行-备用',
  `IS_STANDBY_RUN` decimal(10,0) DEFAULT NULL COMMENT '是否备态下运行-备用',
  `REPORT_TYPE` decimal(10,0) NOT NULL COMMENT '报告类型-备用',
  `OCCUPY_CONTEXT` decimal(10,0) DEFAULT NULL COMMENT '所属态-备用',
  `FILE_PATH` varchar(96) DEFAULT NULL COMMENT '文件路径-备用',
  `DESCRIPTION` varchar(96) DEFAULT NULL COMMENT '进程描述',
  `RUN_STYLE` decimal(10,0) DEFAULT NULL COMMENT '运行类型-备用',
  `PROC_TYPE` decimal(10,0) DEFAULT NULL COMMENT '进程类型-是否单域单进程',
  PRIMARY KEY (`PROC_ALIAS`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-应用进程信息';

drop trigger if exists TIA_SYS_MODEL_PROCESS_INFO;
DELIMITER ;;
create trigger TIA_SYS_MODEL_PROCESS_INFO AFTER INSERT on sys_model_process_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_PROCESS_INFO;
DELIMITER ;;
create trigger TUA_SYS_MODEL_PROCESS_INFO AFTER UPDATE on sys_model_process_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_PROCESS_INFO;
DELIMITER ;;
create trigger TDA_SYS_MODEL_PROCESS_INFO AFTER DELETE on sys_model_process_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_model_sub_system_info;
CREATE TABLE `sys_model_sub_system_info` (
  `SUB_SYSTEM_ID` decimal(10,0) NOT NULL COMMENT '专业ID',
  `TAG_NAME` varchar(32) NOT NULL COMMENT '标签名',
  `DESCRIPTION` varchar(64) NOT NULL COMMENT '专业描述',
  PRIMARY KEY (`SUB_SYSTEM_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统建模-专业分类信息表';

drop trigger if exists TIA_SYS_MODEL_SUB_SYSTEM_INFO;
DELIMITER ;;
create trigger TIA_SYS_MODEL_SUB_SYSTEM_INFO AFTER INSERT on sys_model_sub_system_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TUA_SYS_MODEL_SUB_SYSTEM_INFO;
DELIMITER ;;
create trigger TUA_SYS_MODEL_SUB_SYSTEM_INFO AFTER UPDATE on sys_model_sub_system_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop trigger if exists TDA_SYS_MODEL_SUB_SYSTEM_INFO;
DELIMITER ;;
create trigger TDA_SYS_MODEL_SUB_SYSTEM_INFO AFTER DELETE on sys_model_sub_system_info for each ROW 
BEGIN
IF getTriggerFlag() = TRUE THEN 
INSERT 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;
END;;
DELIMITER ;

drop table if exists sys_trigger_flag;
CREATE TABLE `sys_trigger_flag` (
  `FLAG_ID` decimal(10,0) NOT NULL,
  PRIMARY KEY (`FLAG_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='触发器标记表';

drop table if exists temp_plugin;
CREATE TABLE `temp_plugin` (
  `TAG_NAME` varchar(64) NOT NULL COMMENT '套件名称',
  `DESCRIPTION` varchar(128) DEFAULT NULL COMMENT '套件描述',
  PRIMARY KEY (`TAG_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='套件定义表';

drop trigger if exists TU_TEMP_PLUGIN;
DELIMITER ;;
create trigger TU_TEMP_PLUGIN AFTER UPDATE on temp_plugin for each ROW 
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;
    
END;;
DELIMITER ;

drop trigger if exists TD_TEMP_PLUGIN;
DELIMITER ;;
create trigger TD_TEMP_PLUGIN AFTER DELETE on temp_plugin for each ROW 
BEGIN
    DELETE FROM temp_plugin_dev_map WHERE PLUGIN_NAME = OLD.TAG_NAME;
    
END;;
DELIMITER ;

drop table if exists temp_plugin_dev_map;
CREATE TABLE `temp_plugin_dev_map` (
  `PLUGIN_NAME` varchar(64) NOT NULL COMMENT '套件名',
  `TEMP_NAME` varchar(64) NOT NULL COMMENT '后台模板名',
  PRIMARY KEY (`PLUGIN_NAME`,`TEMP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='套件与后台设备的映射';

drop trigger if exists TI_TEMP_PLUGIN_DEV_MAP;
DELIMITER ;;
create trigger TI_TEMP_PLUGIN_DEV_MAP AFTER INSERT on temp_plugin_dev_map for each ROW 
body:BEGIN
     IF myTriggerIsDisable() = 1 THEN LEAVE body;
     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;
END;;
DELIMITER ;

drop trigger if exists TD_TEMP_PLUGIN_DEV_MAP;
DELIMITER ;;
create trigger TD_TEMP_PLUGIN_DEV_MAP AFTER DELETE on temp_plugin_dev_map for each ROW 
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;
    
END;;
DELIMITER ;

drop table if exists temp_plugin_fes_dev_map;
CREATE TABLE `temp_plugin_fes_dev_map` (
  `PLUGIN_NAME` varchar(64) NOT NULL COMMENT '套件名',
  `TEMP_NAME` varchar(64) NOT NULL COMMENT '后台模板名',
  `FES_TEMP_NAME` varchar(64) NOT NULL COMMENT '前置模板名',
  PRIMARY KEY (`PLUGIN_NAME`,`TEMP_NAME`,`FES_TEMP_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='套件中前置设备与后台设备的映射';

drop trigger if exists TU_TEMP_PLUGIN_FES_DEV_MAP;
DELIMITER ;;
create trigger TU_TEMP_PLUGIN_FES_DEV_MAP AFTER UPDATE on temp_plugin_fes_dev_map for each ROW 
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;
    
END;;
DELIMITER ;

drop trigger if exists TD_TEMP_PLUGIN_FES_DEV_MAP;
DELIMITER ;;
create trigger TD_TEMP_PLUGIN_FES_DEV_MAP AFTER DELETE on temp_plugin_fes_dev_map for each ROW 
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;
    
END;;
DELIMITER ;

drop table if exists temp_plugin_point_map_acc;
CREATE TABLE `temp_plugin_point_map_acc` (
  `PLUGIN_NAME` varchar(64) NOT NULL COMMENT '套件名',
  `TEMP_NAME` varchar(64) NOT NULL COMMENT '后台模板名',
  `TEMP_POINT_NAME` varchar(64) NOT NULL COMMENT '后台模板测点',
  `FES_TEMP_NAME` varchar(64) DEFAULT NULL COMMENT '前置模板名',
  `FES_TEMP_POINT_NAME` varchar(64) DEFAULT NULL COMMENT '前置模板测点名',
  PRIMARY KEY (`PLUGIN_NAME`,`TEMP_NAME`,`TEMP_POINT_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='acc套件定义表';

drop table if exists temp_plugin_point_map_ana;
CREATE TABLE `temp_plugin_point_map_ana` (
  `PLUGIN_NAME` varchar(64) NOT NULL COMMENT '套件名',
  `TEMP_NAME` varchar(64) NOT NULL COMMENT '后台模板名',
  `TEMP_POINT_NAME` varchar(64) NOT NULL COMMENT '后台模板测点',
  `FES_TEMP_NAME` varchar(64) DEFAULT NULL COMMENT '前置模板名',
  `FES_TEMP_POINT_NAME` varchar(64) DEFAULT NULL COMMENT '前置模板测点名',
  `FES_TEMP_OUT_NAME` varchar(64) DEFAULT NULL COMMENT '前置输出测点名',
  PRIMARY KEY (`PLUGIN_NAME`,`TEMP_NAME`,`TEMP_POINT_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='ana套件定义表';

drop table if exists temp_plugin_point_map_dig;
CREATE TABLE `temp_plugin_point_map_dig` (
  `PLUGIN_NAME` varchar(64) NOT NULL COMMENT '套件名',
  `TEMP_NAME` varchar(64) NOT NULL COMMENT '后台模板名',
  `TEMP_POINT_NAME` varchar(64) NOT NULL COMMENT '后台模板测点',
  `FES_TEMP_NAME` varchar(64) DEFAULT NULL COMMENT '前置模板名',
  `FES_TEMP_POINT_NAME1` varchar(64) DEFAULT NULL COMMENT '前置模板测点名1',
  `FES_TEMP_POINT_NAME2` varchar(64) DEFAULT NULL COMMENT '前置模板测点名2',
  `FES_TEMP_POINT_NAME3` varchar(64) DEFAULT NULL COMMENT '前置模板测点名3',
  `FES_TEMP_POINT_NAME4` varchar(64) DEFAULT NULL COMMENT '前置模板测点名4',
  `FES_TEMP_POINT_NAME5` varchar(64) DEFAULT NULL COMMENT '前置模板测点名5',
  `FES_TEMP_OUT_NAME1` varchar(64) DEFAULT NULL COMMENT '前置输出测点名1',
  `FES_TEMP_OUT_NAME2` varchar(64) DEFAULT NULL COMMENT '前置输出测点名2',
  `FES_TEMP_OUT_NAME3` varchar(64) DEFAULT NULL COMMENT '前置输出测点名3',
  `FES_TEMP_OUT_NAME4` varchar(64) DEFAULT NULL COMMENT '前置输出测点名4',
  `FES_TEMP_OUT_NAME5` varchar(64) DEFAULT NULL COMMENT '前置输出测点名5',
  PRIMARY KEY (`PLUGIN_NAME`,`TEMP_NAME`,`TEMP_POINT_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='dig套件定义表';

drop table if exists temp_plugin_point_map_mix;
CREATE TABLE `temp_plugin_point_map_mix` (
  `PLUGIN_NAME` varchar(64) NOT NULL COMMENT '套件名',
  `TEMP_NAME` varchar(64) NOT NULL COMMENT '后台模板名',
  `TEMP_POINT_NAME` varchar(64) NOT NULL COMMENT '后台模板测点',
  `FES_TEMP_NAME` varchar(64) DEFAULT NULL COMMENT '前置模板名',
  `FES_TEMP_POINT_NAME` varchar(64) DEFAULT NULL COMMENT '前置模板测点名',
  `FES_TEMP_OUT_NAME` varchar(64) DEFAULT NULL COMMENT '前置输出测点名',
  PRIMARY KEY (`PLUGIN_NAME`,`TEMP_NAME`,`TEMP_POINT_NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='mix套件定义表';

drop table if exists trend_collection;
CREATE TABLE `trend_collection` (
  `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id 主键，唯一字段',
  `USERNO` varchar(16) DEFAULT NULL COMMENT '用户编号',
  `SNAME` varchar(2000) DEFAULT NULL COMMENT '测点',
  `NAME` varchar(32) DEFAULT NULL COMMENT '收藏名称',
  `BACKUP` varchar(2000) DEFAULT NULL COMMENT '备用1',
  `BACKUP1` varchar(2000) DEFAULT NULL COMMENT '备用2',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='趋势分析分组表';

drop table if exists web_files;
CREATE TABLE `web_files` (
  `id` varchar(24) NOT NULL COMMENT '22位随机ID',
  `name` varchar(125) NOT NULL COMMENT '名称',
  `file_group` varchar(125) NOT NULL COMMENT '组名',
  `ext` varchar(32) DEFAULT NULL COMMENT '扩展名',
  `upload_time` decimal(20,0) NOT NULL COMMENT '上传时间戳',
  `revision` decimal(10,0) NOT NULL COMMENT '版本',
  `status` decimal(10,0) NOT NULL COMMENT '状态 0-存在 1-删除',
  `comment` varchar(1024) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `IN_WEB_FILES` (`name`,`file_group`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='web文件';

drop table if exists web_page;
CREATE TABLE `web_page` (
  `id` varchar(24) NOT NULL COMMENT '22位随机ID',
  `category` decimal(3,0) NOT NULL COMMENT '0区域页面，1自定义页面，2根节点',
  `page_type` decimal(3,0) NOT NULL COMMENT '0表示2D页面，1表示3D页面',
  `page_name` varchar(64) NOT NULL COMMENT '页面名称，与page_type组成联合唯一索引',
  `content_path` varchar(32) DEFAULT NULL COMMENT '页面内容文件路径（仅保存文件名即可）',
  `icon_info` varchar(256) DEFAULT NULL COMMENT '图标信息，前端传来的json',
  `parent_code` varchar(24) DEFAULT NULL COMMENT '父节点编码，前端传入，22位随机ID',
  `create_time` decimal(20,0) NOT NULL COMMENT '创建时间',
  `show_area` decimal(3,0) NOT NULL COMMENT '是否展示区域树，0不展示，1展示',
  `model_info` varchar(256) DEFAULT NULL COMMENT '模板信息，前端传来的json',
  `area_code` varchar(80) DEFAULT NULL COMMENT '区域页面（type为0）所绑定的区域编码',
  PRIMARY KEY (`id`),
  UNIQUE KEY `UN_WEB_PAGE` (`page_type`,`page_name`),
  KEY `IN_WEB_PAGE` (`category`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='web组态页面配置';

drop table if exists web_primitive;
CREATE TABLE `web_primitive` (
  `id` varchar(24) NOT NULL COMMENT '22位随机ID',
  `category` decimal(3,0) NOT NULL COMMENT '类别：0-BI模板库；1-BI页面库；2-组态图元库；3-组合块库；4-组态模板库；5-组态页面库\n',
  `primitive_name` varchar(64) NOT NULL COMMENT '图元名称，与category组成联合唯一索引，category在前',
  `content_path` varchar(32) DEFAULT NULL COMMENT '图元文件路径（仅保存文件名即可），为空（null或empty）的记录是层级',
  `icon_info` varchar(256) DEFAULT NULL COMMENT '图标信息，前端传来的json',
  `parent_code` varchar(24) DEFAULT NULL COMMENT '父节点编码，前端传入，22位随机ID',
  `param` varchar(256) DEFAULT NULL COMMENT '存放前端使用的参数',
  `create_time` decimal(20,0) NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `UN_WEB_PRIMITIVE` (`category`,`primitive_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='web组态图元、模板配置';

drop procedure if exists autoRelationByPlugin;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `autoRelationByPlugin`(pluginName varchar(64)) 
body: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 INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_acc.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_acc.FES_TEMP_POINT_NAME = temp_plugin_point_map_acc.TEMP_POINT_NAME;
    
    #analog
    UPDATE 
    (temp_plugin_point_map_ana INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_ana.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_ana.FES_TEMP_POINT_NAME = temp_plugin_point_map_ana.TEMP_POINT_NAME;
    #analog ctrl,只关联第一个分量
    UPDATE 
    (temp_plugin_point_map_ana INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_ana.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_ana.FES_TEMP_OUT_NAME = temp_plugin_point_map_ana.TEMP_POINT_NAME;
    #digital,只关联第一个分量
    UPDATE 
    (temp_plugin_point_map_dig INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_dig.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_dig.FES_TEMP_POINT_NAME1 = temp_plugin_point_map_dig.TEMP_POINT_NAME;
    
    #digital ctrl,只关联第一个分量
    UPDATE 
    (temp_plugin_point_map_dig INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_dig.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_dig.FES_TEMP_OUT_NAME1 = temp_plugin_point_map_dig.TEMP_POINT_NAME;
    UPDATE 
    (temp_plugin_point_map_mix INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_mix.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_mix.FES_TEMP_POINT_NAME = temp_plugin_point_map_mix.TEMP_POINT_NAME;
    UPDATE 
    (temp_plugin_point_map_mix INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_mix.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_mix.FES_TEMP_OUT_NAME = temp_plugin_point_map_mix.TEMP_POINT_NAME;
        /*以下操作转到代码实现
    #经过以上操作后台和前置同名的测点已经关联,下面处理特殊的点 wz-->posOn,posOff   
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpAutoRelationPluginTbl(
        PLUGIN_NAME varchar(64),
        TEMP_NAME varchar(64),
        TEMP_POINT_NAME varchar(64),
        FES_TEMP_NAME varchar(64),
        FES_POINT1 varchar(64),
        FES_POINT2 varchar(64)
    );
    DELETE FROM tmpAutoRelationPluginTbl;
        
    INSERT INTO tmpAutoRelationPluginTbl(PLUGIN_NAME,TEMP_NAME,TEMP_POINT_NAME,FES_TEMP_NAME,FES_POINT1,FES_POINT2)
    SELECT A.PLUGIN_NAME,A.TEMP_NAME,A.TEMP_POINT_NAME,temp_plugin_fes_dev_map.FES_TEMP_NAME,'','' FROM 
    (SELECT PLUGIN_NAME,TEMP_NAME,TEMP_POINT_NAME FROM temp_plugin_point_map_dig WHERE PLUGIN_NAME = pluginName AND TEMP_POINT_NAME = 'pos') AS A
    LEFT JOIN temp_plugin_fes_dev_map
    ON A.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME AND A.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME;
    
    UPDATE tmpAutoRelationPluginTbl INNER JOIN fes_dev_pt_temp_def 
    ON fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') 
    AND tmpAutoRelationPluginTbl.FES_TEMP_NAME = fes_dev_pt_temp_def.DEV_TP_NAME
    AND getAfterLastDot(fes_dev_pt_temp_def.TAG_NAME) = 'posOn'
    SET tmpAutoRelationPluginTbl.FES_POINT1 = getAfterLastDot(fes_dev_pt_temp_def.TAG_NAME);
    
    UPDATE tmpAutoRelationPluginTbl INNER JOIN fes_dev_pt_temp_def 
    ON fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') 
    AND tmpAutoRelationPluginTbl.FES_TEMP_NAME = fes_dev_pt_temp_def.DEV_TP_NAME
    AND getAfterLastDot(fes_dev_pt_temp_def.TAG_NAME) = 'posOff'
    SET tmpAutoRelationPluginTbl.FES_POINT2 = getAfterLastDot(fes_dev_pt_temp_def.TAG_NAME);
    
    UPDATE tmpAutoRelationPluginTbl INNER JOIN temp_plugin_point_map_dig
    ON tmpAutoRelationPluginTbl.PLUGIN_NAME = temp_plugin_point_map_dig.PLUGIN_NAME
    AND tmpAutoRelationPluginTbl.TEMP_NAME = temp_plugin_point_map_dig.TEMP_NAME
    AND tmpAutoRelationPluginTbl.TEMP_POINT_NAME = temp_plugin_point_map_dig.TEMP_POINT_NAME
    SET 
    temp_plugin_point_map_dig.FES_TEMP_NAME = tmpAutoRelationPluginTbl.FES_TEMP_NAME,
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME1 = tmpAutoRelationPluginTbl.FES_POINT1,
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME2 = tmpAutoRelationPluginTbl.FES_POINT2;
    
    DELETE FROM tmpAutoRelationPluginTbl;
    */
    
END;;
DELIMITER ;

drop procedure if exists autoRelationByPluginDev;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `autoRelationByPluginDev`(pluginName varchar(64),devTag varchar(64)) 
body: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 INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_acc.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_acc.FES_TEMP_POINT_NAME = temp_plugin_point_map_acc.TEMP_POINT_NAME;
    
    #analog
    UPDATE 
    (temp_plugin_point_map_ana INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_ana.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_ana.FES_TEMP_POINT_NAME = temp_plugin_point_map_ana.TEMP_POINT_NAME;
    
    UPDATE 
    (temp_plugin_point_map_ana INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_ana.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_ana.FES_TEMP_OUT_NAME = temp_plugin_point_map_ana.TEMP_POINT_NAME;
    #digital,只关联第一个分量
    UPDATE 
    (temp_plugin_point_map_dig INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_dig.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_dig.FES_TEMP_POINT_NAME1 = temp_plugin_point_map_dig.TEMP_POINT_NAME;
    UPDATE 
    (temp_plugin_point_map_dig INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_dig.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_dig.FES_TEMP_OUT_NAME1 = temp_plugin_point_map_dig.TEMP_POINT_NAME;
        
    UPDATE 
    (temp_plugin_point_map_mix INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_mix.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_mix.FES_TEMP_POINT_NAME = temp_plugin_point_map_mix.TEMP_POINT_NAME;
    UPDATE 
    (temp_plugin_point_map_mix INNER JOIN temp_plugin_fes_dev_map 
    ON 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
    )INNER JOIN fes_dev_pt_temp_def 
    ON 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)
    SET temp_plugin_point_map_mix.FES_TEMP_NAME = temp_plugin_fes_dev_map.FES_TEMP_NAME,
        temp_plugin_point_map_mix.FES_TEMP_OUT_NAME = temp_plugin_point_map_mix.TEMP_POINT_NAME;
    
        /*以下操作转代码实现
    #经过以上操作后台和前置同名的测点已经关联,下面处理特殊的点 wz-->posOn,posOff   
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpAutoRelationPluginTbl(
        PLUGIN_NAME varchar(64),
        TEMP_NAME varchar(64),
        TEMP_POINT_NAME varchar(64),
        FES_TEMP_NAME varchar(64),
        FES_POINT1 varchar(64),
        FES_POINT2 varchar(64)
    );
    DELETE FROM tmpAutoRelationPluginTbl;
        
    INSERT INTO tmpAutoRelationPluginTbl(PLUGIN_NAME,TEMP_NAME,TEMP_POINT_NAME,FES_TEMP_NAME,FES_POINT1,FES_POINT2)
    SELECT A.PLUGIN_NAME,A.TEMP_NAME,A.TEMP_POINT_NAME,temp_plugin_fes_dev_map.FES_TEMP_NAME,'','' FROM 
    (SELECT PLUGIN_NAME,TEMP_NAME,TEMP_POINT_NAME FROM temp_plugin_point_map_dig WHERE PLUGIN_NAME = pluginName 
    AND TEMP_NAME = devTag 
    AND TEMP_POINT_NAME = 'pos') AS A
    LEFT JOIN temp_plugin_fes_dev_map
    ON A.PLUGIN_NAME = temp_plugin_fes_dev_map.PLUGIN_NAME AND A.TEMP_NAME = temp_plugin_fes_dev_map.TEMP_NAME;
    
    UPDATE tmpAutoRelationPluginTbl INNER JOIN fes_dev_pt_temp_def 
    ON fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') 
    AND tmpAutoRelationPluginTbl.FES_TEMP_NAME = fes_dev_pt_temp_def.DEV_TP_NAME
    AND getAfterLastDot(fes_dev_pt_temp_def.TAG_NAME) = 'posOn'
    SET tmpAutoRelationPluginTbl.FES_POINT1 = getAfterLastDot(fes_dev_pt_temp_def.TAG_NAME);
    
    UPDATE tmpAutoRelationPluginTbl INNER JOIN fes_dev_pt_temp_def 
    ON fes_dev_pt_temp_def.POINT_TYPE = getFesPointType('FES_DIG') 
    AND tmpAutoRelationPluginTbl.FES_TEMP_NAME = fes_dev_pt_temp_def.DEV_TP_NAME
    AND getAfterLastDot(fes_dev_pt_temp_def.TAG_NAME) = 'posOff'
    SET tmpAutoRelationPluginTbl.FES_POINT2 = getAfterLastDot(fes_dev_pt_temp_def.TAG_NAME);
    
    UPDATE tmpAutoRelationPluginTbl INNER JOIN temp_plugin_point_map_dig
    ON tmpAutoRelationPluginTbl.PLUGIN_NAME = temp_plugin_point_map_dig.PLUGIN_NAME
    AND tmpAutoRelationPluginTbl.TEMP_NAME = temp_plugin_point_map_dig.TEMP_NAME
    AND tmpAutoRelationPluginTbl.TEMP_POINT_NAME = temp_plugin_point_map_dig.TEMP_POINT_NAME
    SET 
    temp_plugin_point_map_dig.FES_TEMP_NAME = tmpAutoRelationPluginTbl.FES_TEMP_NAME,
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME1 = tmpAutoRelationPluginTbl.FES_POINT1,
    temp_plugin_point_map_dig.FES_TEMP_POINT_NAME2 = tmpAutoRelationPluginTbl.FES_POINT2;
    
    DELETE FROM tmpAutoRelationPluginTbl;
    */
    
END;;
DELIMITER ;

drop procedure if exists bindStatDev;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `bindStatDev`(statTag varchar(64),devTag varchar(64)) 
body:BEGIN
    /*
     * 绑定统计分析中的实例,使用对应的后台设备
     * statTag 统计分析实例  
     * devTag 后台设备  
     */
    
    #清空
    UPDATE stat_inst_para_point SET POINT_TAG = '' WHERE INST_TAG = statTag;
    
    UPDATE stat_inst_ret_para SET POINT_TAG = '' WHERE INST_TAG = statTag;
    
    #填写
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpBinStatDevTbl(
        PARA_TAG  varchar(64) NOT NULL,
        POINT_TAG varchar(64) NOT NULL
    );
    DELETE FROM tmpBinStatDevTbl;
    
    #查找对应的测点
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT PARA_TAG,concat('analog.',analog.TAG_NAME) AS A 
    FROM stat_inst_para_point,analog 
    WHERE INST_TAG = statTag AND analog.DEVICE = devTag 
    AND getAfterLastDot(PARA_TAG) = getAfterLastDot(analog.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT PARA_TAG,concat('accuml.',accuml.TAG_NAME) AS A 
    FROM stat_inst_para_point,accuml 
    WHERE INST_TAG = statTag AND accuml.DEVICE = devTag 
    AND getAfterLastDot(PARA_TAG) = getAfterLastDot(accuml.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT PARA_TAG,concat('digital.',digital.TAG_NAME) AS A 
    FROM stat_inst_para_point,digital 
    WHERE INST_TAG = statTag AND digital.DEVICE = devTag 
    AND getAfterLastDot(PARA_TAG) = getAfterLastDot(digital.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT PARA_TAG,concat('mix.',mix.TAG_NAME) AS A 
    FROM stat_inst_para_point,mix 
    WHERE INST_TAG = statTag AND mix.DEVICE = devTag 
    AND getAfterLastDot(PARA_TAG) = getAfterLastDot(mix.TAG_NAME);
    
    UPDATE stat_inst_para_point INNER JOIN tmpBinStatDevTbl ON stat_inst_para_point.PARA_TAG = tmpBinStatDevTbl.PARA_TAG
    SET stat_inst_para_point.POINT_TAG = tmpBinStatDevTbl.POINT_TAG;
    
    DELETE FROM tmpBinStatDevTbl;
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT RET_TAG,concat('analog.',analog.TAG_NAME,'.value') AS A 
    FROM stat_inst_ret_para,analog 
    WHERE INST_TAG = statTag AND analog.DEVICE = devTag 
    AND getAfterLastDot(RET_TAG) = getAfterLastDot(analog.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT RET_TAG,concat('digital.',digital.TAG_NAME,'.value') AS A 
    FROM stat_inst_ret_para,digital 
    WHERE INST_TAG = statTag AND digital.DEVICE = devTag 
    AND getAfterLastDot(RET_TAG) = getAfterLastDot(digital.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT RET_TAG,concat('accuml.',accuml.TAG_NAME,'.value') AS A 
    FROM stat_inst_ret_para,accuml 
    WHERE INST_TAG = statTag AND accuml.DEVICE = devTag 
    AND getAfterLastDot(RET_TAG) = getAfterLastDot(accuml.TAG_NAME);
    
    INSERT INTO tmpBinStatDevTbl(PARA_TAG,POINT_TAG)
    SELECT RET_TAG,concat('mix.',mix.TAG_NAME,'.value') AS A 
    FROM stat_inst_ret_para,mix 
    WHERE INST_TAG = statTag AND mix.DEVICE = devTag 
    AND getAfterLastDot(RET_TAG) = getAfterLastDot(mix.TAG_NAME);
    
    UPDATE stat_inst_ret_para INNER JOIN tmpBinStatDevTbl ON stat_inst_ret_para.RET_TAG = tmpBinStatDevTbl.PARA_TAG
    SET stat_inst_ret_para.POINT_TAG = tmpBinStatDevTbl.POINT_TAG,
    stat_inst_ret_para.IS_BIND = '1'
    ;
    
END;;
DELIMITER ;

drop procedure if exists bingRtu;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `bingRtu`(devTagName varchar(64), rtuTagName varchar(64)) 
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;;
DELIMITER ;

drop procedure if exists clearTempRelation;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `clearTempRelation`(devTempName varchar(64)) 
body:BEGIN
    /*
     * 清除指定模板的所有测点与前置模板的关系
     */
    
    UPDATE accuml_temp_define 
    SET SEC_DEVTP_NAME = '',
        SEC_SEQ_NO = -1
    WHERE DEV_TP_NAME = devTempName;
    
    UPDATE analog_temp_define 
    SET SEC_DEVTP_NAME = '',
        SEC_SEQ_NO = -1,
        OUT_SEC_SEQ_NO = -1
    WHERE DEV_TP_NAME = devTempName;
    
    UPDATE digital_temp_define 
    SET SEC_DEVTP_NAME = '',
        SEC_SEQ_NO1 = -1,
        SEC_SEQ_NO2 = -1,
        SEC_SEQ_NO3 = -1,
        SEC_SEQ_NO4 = -1,
        SEC_SEQ_NO5 = -1,
        OUT_SEC_SEQ_NO1 = -1,
        OUT_SEC_SEQ_NO2 = -1,
        OUT_SEC_SEQ_NO3 = -1,
        OUT_SEC_SEQ_NO4 = -1,
        OUT_SEC_SEQ_NO5 = -1
    WHERE DEV_TP_NAME = devTempName;
    
    UPDATE mix_temp_define 
    SET SEC_DEVTP_NAME = '',
        SEC_SEQ_NO = -1,
        OUT_SEC_SEQ_NO = -1
    WHERE DEV_TP_NAME = devTempName;
    
END;;
DELIMITER ;

drop procedure if exists closeMyTrigger;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `closeMyTrigger`(closeFlag int) 
body:BEGIN
    /*
     * 设置某些触发器的开关,获取开关状态使用myTriggerIsDisable
     * 目前支持本触发器开关的触发器有TI_TEMP_PLUGIN_DEV_MAP
     * 一般本存储过程需要成对使用时
     */
    
    IF closeFlag = 1 THEN 
       SET @disableMyTrrigger = 1;
    ELSE
       SET @disableMyTrrigger = 0;
    END IF;
END;;
DELIMITER ;

drop procedure if exists copyDevGroup;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `copyDevGroup`(srcGroupName varchar(64),dstGroupName varchar(64),dstGroupDesc varchar(64)) 
body:BEGIN
    /*
     * 复制整个设备组
     */
    DECLARE totalInst int DEFAULT 1;
    
    IF COALESCE(srcGroupName,'') = '' OR COALESCE(dstGroupName,'')='' OR COALESCE(dstGroupDesc,'') = ''
    THEN LEAVE body;
    END IF;
    
    IF EXISTS (SELECT * FROM dev_group WHERE TAG_NAME = dstGroupName)
    THEN LEAVE body;
    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%';
    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(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;;
DELIMITER ;

drop procedure if exists copyDevInst;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `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)
) 
pro_body:BEGIN
    IF EXISTS (SELECT * FROM dev_info WHERE TAG_NAME=dstDevTagName) THEN LEAVE pro_body;
    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;;
DELIMITER ;

drop procedure if exists copyDevTemp;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `copyDevTemp`( srcTagName varchar(64),  dstTagName varchar(64), dstDesc varchar(64)) 
pro_body:BEGIN
    
    
    IF EXISTS (SELECT * FROM dev_temp_def WHERE TAG_NAME=dstTagName) THEN LEAVE pro_body;
    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;;
DELIMITER ;

drop procedure if exists copyFesTemp;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `copyFesTemp`(srcFesTempTag varchar(64),dstFesTempTag varchar(64),dstDesc varchar(128)) 
body:BEGIN
    /*
     * 复制前置模板
     */
    IF EXISTS (SELECT * FROM fes_dev_temp_def WHERE TAG_NAME=dstFesTempTag) THEN LEAVE body;
    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;;
DELIMITER ;

drop procedure if exists copyPlugin;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `copyPlugin`( srcPluginName varchar(64),dstPluginName varchar(64),dstPluginDesc varchar(128)) 
body: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;;
DELIMITER ;

drop procedure if exists createOptLock;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `createOptLock`(tableName varchar(64),pointTempTag varchar(64),funcTag varchar(64),argNum int) 
body:BEGIN
    /*
     * 依据指定的模板点,用于创建闭锁出口,除了参数列表中的参数,还有部分参数在export_seq表中
     * tableName:表名,指出pointTempTag是属于那张表的,目前取值范围有analog,digital,mix
     * pointTempTag:测点模板的标签名
     * funcTag:闭锁出口选用的函数
     * argNum:闭锁函数的参数个数
     * export_seq表中保存了参数1-20
     */
    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;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET hasDone = 1;
    
    IF COALESCE(tableName,'') = '' OR COALESCE(pointTempTag,'')=''
    THEN LEAVE body;
    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;
    
    WHILE hasDone != 1 DO
        FETCH cur_args INTO argTag,argNo;
        #argTag中的格式是表名.测点模板名
        SET tableName = getBeforeFirstDot(argTag);
        SET pointTempTagName = getAfterFirstDot(argTag);
        
        IF tableName = 'analog' THEN 
           UPDATE (analog INNER JOIN dev_info ON analog.POINT_TP_NAME = pointTempTagName AND analog.DEVICE = dev_info.TAG_NAME)
           INNER JOIN tmpCreateOptLockTbl ON tmpCreateOptLockTbl.GROUP_TAG_NAME = dev_info.GROUP_TAG_NAME
           SET tmpCreateOptLockTbl.ARGS_TAG_NAME = analog.TAG_NAME;
        ELSEIF tableName = 'digital' THEN
           UPDATE (digital INNER JOIN dev_info ON digital.POINT_TP_NAME = pointTempTagName AND digital.DEVICE = dev_info.TAG_NAME)
           INNER JOIN tmpCreateOptLockTbl ON tmpCreateOptLockTbl.GROUP_TAG_NAME = dev_info.GROUP_TAG_NAME
           SET tmpCreateOptLockTbl.ARGS_TAG_NAME = digital.TAG_NAME;
        ELSEIF tableName = 'mix' THEN
           UPDATE (mix INNER JOIN dev_info ON mix.POINT_TP_NAME = pointTempTagName AND mix.DEVICE = dev_info.TAG_NAME)
           INNER JOIN tmpCreateOptLockTbl ON tmpCreateOptLockTbl.GROUP_TAG_NAME = dev_info.GROUP_TAG_NAME
           SET tmpCreateOptLockTbl.ARGS_TAG_NAME = mix.TAG_NAME;
        END IF;
    
        #更新闭锁输入参数
        UPDATE opt_interlock_para INNER JOIN tmpCreateOptLockTbl
        ON opt_interlock_para.TAG_NAME = tmpCreateOptLockTbl.POINT_TAG_NAME
        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)
        ;
        
        
    END WHILE;
    
    CLOSE cur_args;
    
END;;
DELIMITER ;

drop procedure if exists custom_log;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `custom_log`(info1 varchar(100), info2 varchar(100) ) 
BEGIN
DECLARE enableDebug int DEFAULT 1;
SET enableDebug = (SELECT PRINT_DEBUG_INFO_FLAG FROM custom_debug_flag LIMIT 1);
IF enableDebug=1 THEN 
INSERT INTO custom_debug_info(col1,col2,col3) VALUES (info1,info2,now());
END IF;
END;;
DELIMITER ;

drop procedure if exists db_sync;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `db_sync`(in src_hostname varchar(255),
                                                         in src_last_id bigint(20),
                                                         in src_last_statement mediumtext,
                                                         in new_id bigint(20),
                                                         in new_statement mediumtext,
                                                         out db_sync_return int) 
lable:
begin
    /*
    * 数据库同步服务 db_sync_server 调用，代码中开启事务、锁表
    */
    declare local_last_id bigint(20) default 0;
    declare local_last_statement mediumtext;
    set db_sync_return = 1;
    if lower(@@global.hostname) = lower(src_hostname) then
        -- 如果源主机就是本主机，则无需同步
        set db_sync_return = 10;
        leave lable;
    end if;
    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
        -- 表为空
        set db_sync_return = 20;
        leave lable;
    end if;
    if src_last_id < local_last_id then
        -- 数据不一致
        set db_sync_return = 21;
        leave lable;
    end if;
    if src_last_id > local_last_id then
        -- 数据不一致
        set db_sync_return = 22;
        leave lable;
    end if;
    if src_last_statement <> local_last_statement then
        -- 数据不一致
        set db_sync_return = 23;
        leave lable;
    end if;
    begin
        declare exit handler for sqlexception begin
            set db_sync_return = 100;
        end;
        -- 执行需要同步的语句
        set @db_sync_sql4exec = new_statement;
        prepare stmt from @db_sync_sql4exec;
        execute stmt;
        deallocate prepare stmt;
        -- 插入同步语句到本地表
        set @db_sync_sql4exec = 'insert into dbop_list(ID, STATEMENT) values (?, ?)';
        prepare stmt from @db_sync_sql4exec;
        set @db_sync_new_id = new_id;
        set @db_sync_new_statement = new_statement;
        execute stmt using @db_sync_new_id , @db_sync_new_statement;
        deallocate prepare stmt;
        -- 执行成功
        set db_sync_return = 0;
    end;
END;;
DELIMITER ;

drop procedure if exists exportBgXFes;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `exportBgXFes`( ) 
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 INNER JOIN fes_accuml ON
    fes_accuml.TAG_NAME = concat(background_x_fes_accuml.RTU_TAG,'.',background_x_fes_accuml.RTU_DOT_NO)
    SET background_x_fes_accuml.RTU_DOT_DESC = fes_accuml.DESCRIPTION,
        background_x_fes_accuml.RTU_DEV_TAG = fes_accuml.DEV_TAG,
        background_x_fes_accuml.RTU_RES_PARA_INT1 = fes_accuml.RES_PARA_INT1,
        background_x_fes_accuml.RTU_RES_PARA_INT2 = fes_accuml.RES_PARA_INT2,
        background_x_fes_accuml.RTU_RES_PARA_INT3 = fes_accuml.RES_PARA_INT3,
        background_x_fes_accuml.RTU_RES_PARA_INT4 = fes_accuml.RES_PARA_INT4;
    
    #插入所有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 INNER JOIN fes_analog ON 
    fes_analog.TAG_NAME = concat(background_x_fes_analog.RTU_TAG,'.',background_x_fes_analog.RTU_DOT_NO)
    SET background_x_fes_analog.RTU_DOT_DESC = fes_analog.DESCRIPTION,
        background_x_fes_analog.RTU_DEV_TAG = fes_analog.DEV_TAG,
        background_x_fes_analog.RTU_RES_PARA_INT1 = fes_analog.RES_PARA_INT1,
        background_x_fes_analog.RTU_RES_PARA_INT2 = fes_analog.RES_PARA_INT2,
        background_x_fes_analog.RTU_RES_PARA_INT3 = fes_analog.RES_PARA_INT3,
        background_x_fes_analog.RTU_RES_PARA_INT4 = fes_analog.RES_PARA_INT4;
    
    #插入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 INNER JOIN fes_analog_ctrl ON 
    fes_analog_ctrl.TAG_NAME = concat(background_x_fes_ana_ctrl.RTU_TAG,'.',background_x_fes_ana_ctrl.RTU_DOT_NO)
    SET background_x_fes_ana_ctrl.RTU_DOT_DESC = fes_analog_ctrl.DESCRIPTION,
        background_x_fes_ana_ctrl.RTU_DEV_TAG = fes_analog_ctrl.DEV_TAG,
        background_x_fes_ana_ctrl.RTU_RES_PARA_INT1 = fes_analog_ctrl.RES_PARA_INT1,
        background_x_fes_ana_ctrl.RTU_RES_PARA_INT2 = fes_analog_ctrl.RES_PARA_INT2,
        background_x_fes_ana_ctrl.RTU_RES_PARA_INT3 = fes_analog_ctrl.RES_PARA_INT3,
        background_x_fes_ana_ctrl.RTU_RES_PARA_INT4 = fes_analog_ctrl.RES_PARA_INT4;
    
    #对于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 INNER JOIN fes_digital ON  
    fes_digital.TAG_NAME = concat(background_x_fes_digtal.RTU_TAG,'.',background_x_fes_digtal.RTU_DOT_NO) 
    SET background_x_fes_digtal.RTU_DOT_DESC = fes_digital.DESCRIPTION,
        background_x_fes_digtal.RTU_DEV_TAG = fes_digital.DEV_TAG,
        background_x_fes_digtal.RTU_RES_PARA_INT1 = fes_digital.RES_PARA_INT1,
        background_x_fes_digtal.RTU_RES_PARA_INT2 = fes_digital.RES_PARA_INT2,
        background_x_fes_digtal.RTU_RES_PARA_INT3 = fes_digital.RES_PARA_INT3,
        background_x_fes_digtal.RTU_RES_PARA_INT4 = fes_digital.RES_PARA_INT4;
            
    #对于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 INNER JOIN fes_digital_ctrl ON 
    fes_digital_ctrl.TAG_NAME = concat(background_x_fes_dig_ctrl.RTU_TAG,'.',background_x_fes_dig_ctrl.RTU_DOT_NO) 
    SET background_x_fes_dig_ctrl.RTU_DOT_DESC = fes_digital_ctrl.DESCRIPTION,
        background_x_fes_dig_ctrl.RTU_DEV_TAG = fes_digital_ctrl.DEV_TAG,
        background_x_fes_dig_ctrl.RTU_RES_PARA_INT1 = fes_digital_ctrl.RES_PARA_INT1,
        background_x_fes_dig_ctrl.RTU_RES_PARA_INT2 = fes_digital_ctrl.RES_PARA_INT2,
        background_x_fes_dig_ctrl.RTU_RES_PARA_INT3 = fes_digital_ctrl.RES_PARA_INT3,
        background_x_fes_dig_ctrl.RTU_RES_PARA_INT4 = fes_digital_ctrl.RES_PARA_INT4;
    
    #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 INNER JOIN fes_mix ON 
    fes_mix.TAG_NAME = concat(background_x_fes_mix.RTU_TAG,'.',background_x_fes_mix.RTU_DOT_NO)
    SET background_x_fes_mix.RTU_DOT_DESC = fes_mix.DESCRIPTION,
        background_x_fes_mix.RTU_DEV_TAG = fes_mix.DEV_TAG,
        background_x_fes_mix.RTU_RES_PARA_INT1 = fes_mix.RES_PARA_INT1,
        background_x_fes_mix.RTU_RES_PARA_INT2 = fes_mix.RES_PARA_INT2,
        background_x_fes_mix.RTU_RES_PARA_INT3 = fes_mix.RES_PARA_INT3,
        background_x_fes_mix.RTU_RES_PARA_INT4 = fes_mix.RES_PARA_INT4;
    
    #插入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 INNER JOIN fes_mix_ctrl ON 
    fes_mix_ctrl.TAG_NAME=concat(background_x_fes_mix_ctrl.RTU_TAG,'.',background_x_fes_mix_ctrl.RTU_DOT_NO)
    SET background_x_fes_mix_ctrl.RTU_DOT_DESC = fes_mix_ctrl.DESCRIPTION,
        background_x_fes_mix_ctrl.RTU_DEV_TAG = fes_mix_ctrl.DEV_TAG,
        background_x_fes_mix_ctrl.RTU_RES_PARA_INT1 = fes_mix_ctrl.RES_PARA_INT1,
        background_x_fes_mix_ctrl.RTU_RES_PARA_INT2 = fes_mix_ctrl.RES_PARA_INT2,
        background_x_fes_mix_ctrl.RTU_RES_PARA_INT3 = fes_mix_ctrl.RES_PARA_INT3,
        background_x_fes_mix_ctrl.RTU_RES_PARA_INT4 = fes_mix_ctrl.RES_PARA_INT4;
      
END;;
DELIMITER ;

drop procedure if exists generateBatchDev;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `generateBatchDev`(devTagName varchar(64), batchNum decimal(10,0),batchStart decimal(10,0), formatTagName varchar(64),formatDesc varchar(128),fesRtuTagName varchar(64)) 
body:BEGIN
    #批量生成后台设备,当fesRtuTagName不为空时,同时批量生成对应的前置设备
    #devTagName 后台设备名
    #batchNum 批量生成的数量
    #batchStart 批量的起始数字
    #formatTagName TagName字段的批量格式
    #formatDesc 描述字段的批量格式
    #fesRtuTagName 前置RTU的标签名,为空表示不生成对应的前置设备
    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;
    
    #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;
    
    SET locationTagName = getBeforeLastDot(devTagName);
    #根据批量规则生成一系列标签名和描述,
    SET stepNum = batchStart;
    WHILE stepNum < (batchStart + batchNum) DO
        INSERT INTO tmpGenerateBatchDevTbl(BAT_TAG_NAME,BAT_DESC)
        VALUES (replace(formatTagName,'%1',stepNum), replace(formatDesc,'%1',stepNum));
        SET stepNum = stepNum + 1;
    END WHILE;
    
    #第一个设备在调用本存储过程之前已经生成,这里不必再重新生成
    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 LEAVE body;
        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
        SET stepNum = batchStart;
        WHILE stepNum < (batchStart + batchNum) DO
            SET tempStr = replace(formatTagName,'%1',stepNum);
            SET tmpDevTag = concat(locationTagName,'.',tempStr);
            SET 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
                    SET accSize = 0;
                END IF;
                IF COALESCE(anaSize,0) = 0 THEN
                    SET anaSize = 0;
                END IF;
                IF COALESCE(digSize,0) = 0 THEN
                    SET digSize = 0;
                END IF;
                IF COALESCE(mixSize,0) = 0 THEN
                    SET 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 INNER JOIN tmpFesTblAcc 
            ON accuml_map_to_fes.KEY_ID_TAG = tmpFesTblAcc.KEY_ID_TAG
            SET accuml_map_to_fes.DOT_NO = tmpFesTblAcc.DOT_NO;
            #ana
            UPDATE analog SET RTU_TAG = fesRtuTagName WHERE DEVICE = tmpDevTag;
            UPDATE analog_map_to_fes INNER JOIN tmpFesTblAna 
            ON analog_map_to_fes.KEY_ID_TAG = tmpFesTblAna.KEY_ID_TAG
            SET analog_map_to_fes.DOT_NO = tmpFesTblAna.DOT_NO;
            #digital
            UPDATE digital SET RTU_TAG = fesRtuTagName WHERE DEVICE = tmpDevTag;
    
            UPDATE digital_map_to_fes INNER JOIN tmpFesTblDig 
            ON digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG AND tmpFesTblDig.OFFSET_NO = 1
            SET digital_map_to_fes.DOT_NO1 = tmpFesTblDig.DOT_NO; 
            
            UPDATE digital_map_to_fes INNER JOIN tmpFesTblDig 
            ON digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG AND tmpFesTblDig.OFFSET_NO = 2
            SET digital_map_to_fes.DOT_NO2 = tmpFesTblDig.DOT_NO; 
           
            UPDATE digital_map_to_fes INNER JOIN tmpFesTblDig 
            ON digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG AND tmpFesTblDig.OFFSET_NO = 3
            SET digital_map_to_fes.DOT_NO3 = tmpFesTblDig.DOT_NO; 
           
            UPDATE digital_map_to_fes INNER JOIN tmpFesTblDig 
            ON digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG AND tmpFesTblDig.OFFSET_NO = 4
            SET digital_map_to_fes.DOT_NO4 = tmpFesTblDig.DOT_NO; 
            
            UPDATE digital_map_to_fes INNER JOIN tmpFesTblDig 
            ON digital_map_to_fes.KEY_ID_TAG = tmpFesTblDig.KEY_ID_TAG AND tmpFesTblDig.OFFSET_NO = 5
            SET digital_map_to_fes.DOT_NO5 = tmpFesTblDig.DOT_NO; 
            #mix
            UPDATE mix SET RTU_TAG = fesRtuTagName WHERE DEVICE = tmpDevTag;
            UPDATE mix_map_to_fes INNER JOIN tmpFesTblMix 
            ON mix_map_to_fes.KEY_ID_TAG = tmpFesTblMix.KEY_ID_TAG
            SET mix_map_to_fes.DOT_NO = tmpFesTblMix.DOT_NO; 
            
            #每次循环结束加1
            SET stepNum = stepNum + 1;
            
        END WHILE;
        
        
    END; 
    END IF;
    
END;;
DELIMITER ;

drop procedure if exists generateFesDevTemp;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `generateFesDevTemp`(devTagName varchar(64), fesDevTagName varchar(64)) 
body:BEGIN
    #生成与后台设备模板一样的前置模板的对话框
    #devTagName 后台设备模板名
    #fesDevTagName 前置设备模板名,该前置设备模板应已存在,且该设备下无任何点
    IF EXISTS (SELECT TAG_NAME FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME= fesDevTagName ) 
    THEN LEAVE body;
    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 int NOT NULL AUTO_INCREMENT,
        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;
    ALTER TABLE tmpGenerateFesDevTempTbl auto_increment = 1;
   
    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 INNER JOIN  tmpGenerateFesDevTempTbl 
    ON TAG_NAME = DIG_TAG_NAME AND DEV_TP_NAME = devTagName AND HouTai_OFF_SET = 1
    SET SEC_SEQ_NO1 = FES_SEQ_NO;
    
    UPDATE digital_temp_define INNER JOIN  tmpGenerateFesDevTempTbl 
    ON TAG_NAME = DIG_TAG_NAME AND DEV_TP_NAME = devTagName AND HouTai_OFF_SET = 2
    SET SEC_SEQ_NO2 = FES_SEQ_NO;
    
    UPDATE digital_temp_define INNER JOIN  tmpGenerateFesDevTempTbl 
    ON TAG_NAME = DIG_TAG_NAME AND DEV_TP_NAME = devTagName AND HouTai_OFF_SET = 3
    SET SEC_SEQ_NO3 = FES_SEQ_NO;
    
    UPDATE digital_temp_define INNER JOIN  tmpGenerateFesDevTempTbl 
    ON TAG_NAME = DIG_TAG_NAME AND DEV_TP_NAME = devTagName AND HouTai_OFF_SET = 4
    SET SEC_SEQ_NO4 = FES_SEQ_NO;
    
    UPDATE digital_temp_define INNER JOIN  tmpGenerateFesDevTempTbl 
    ON TAG_NAME = DIG_TAG_NAME AND DEV_TP_NAME = devTagName AND HouTai_OFF_SET = 5
    SET SEC_SEQ_NO5 = FES_SEQ_NO;
    
END;;
DELIMITER ;

drop procedure if exists importbgxfes;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `importbgxfes`(openTriggerInfo decimal(10,0), importFesPointDesc decimal(10,0), fesDescToBG decimal(10,0) ) 
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 INNER JOIN background_x_fes_accuml
    ON concat('accuml','.',accuml.TAG_NAME)  = background_x_fes_accuml.BG_POINT_TAG
    SET accuml.RTU_TAG = '';
    
    UPDATE analog INNER JOIN background_x_fes_analog
    ON concat('analog','.',analog.TAG_NAME)  = background_x_fes_analog.BG_POINT_TAG
    SET analog.RTU_TAG = '';
    
    UPDATE digital INNER JOIN background_x_fes_digtal
    ON concat('digital','.',digital.TAG_NAME)  = background_x_fes_digtal.BG_POINT_TAG
    SET digital.RTU_TAG = '';
    
    UPDATE mix INNER JOIN background_x_fes_mix
    ON concat('mix','.',mix.TAG_NAME)  = background_x_fes_mix.BG_POINT_TAG
    SET mix.RTU_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 INNER JOIN accuml_map_to_fes 
    ON 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 
    ) INNER JOIN accuml_temp_define ON accuml_map_to_fes.POINT_TP_NAME = accuml_temp_define.TAG_NAME
    ) INNER JOIN fes_accuml ON fes_accuml.DEV_TAG = background_x_fes_accuml.RTU_DEV_TAG AND fes_accuml.SEQ_NO = accuml_temp_define.SEC_SEQ_NO
    SET 
    background_x_fes_accuml.RTU_TAG = fes_accuml.RTU_TAG,
    background_x_fes_accuml.RTU_DOT_NO = fes_accuml.DOT_NO;
    #ana
    UPDATE 
    (
    ( background_x_fes_analog INNER JOIN analog_map_to_fes 
    ON 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 
    ) INNER JOIN analog_temp_define ON analog_map_to_fes.POINT_TP_NAME = analog_temp_define.TAG_NAME
    ) INNER JOIN fes_analog ON fes_analog.DEV_TAG = background_x_fes_analog.RTU_DEV_TAG AND fes_analog.SEQ_NO = analog_temp_define.SEC_SEQ_NO
    SET 
    background_x_fes_analog.RTU_TAG = fes_analog.RTU_TAG,
    background_x_fes_analog.RTU_DOT_NO = fes_analog.DOT_NO;
    #dig的5个分量,需要判断是哪个分量,即判断background_x_fes_digtal.OFFSET字段,可枚举该字段的值,只有5个,1,2,3,4,5
    UPDATE 
    (
    (background_x_fes_digtal INNER JOIN digital_map_to_fes 
    ON 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 
    ) INNER JOIN digital_temp_define ON digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    ) INNER JOIN fes_digital ON fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO1
    SET 
    background_x_fes_digtal.RTU_TAG = fes_digital.RTU_TAG,
    background_x_fes_digtal.RTU_DOT_NO = fes_digital.DOT_NO;
    
    UPDATE 
    (
    (background_x_fes_digtal INNER JOIN digital_map_to_fes 
    ON 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 
    ) INNER JOIN digital_temp_define ON digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    ) INNER JOIN fes_digital ON fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO2
    SET 
    background_x_fes_digtal.RTU_TAG = fes_digital.RTU_TAG,
    background_x_fes_digtal.RTU_DOT_NO = fes_digital.DOT_NO;
    
    UPDATE 
    (
    (background_x_fes_digtal INNER JOIN digital_map_to_fes 
    ON 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 
    ) INNER JOIN digital_temp_define ON digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    ) INNER JOIN fes_digital ON fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO3
    SET 
    background_x_fes_digtal.RTU_TAG = fes_digital.RTU_TAG,
    background_x_fes_digtal.RTU_DOT_NO = fes_digital.DOT_NO;
    
    UPDATE 
    (
    (background_x_fes_digtal INNER JOIN digital_map_to_fes 
    ON 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 
    ) INNER JOIN digital_temp_define ON digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    ) INNER JOIN fes_digital ON fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO4
    SET 
    background_x_fes_digtal.RTU_TAG = fes_digital.RTU_TAG,
    background_x_fes_digtal.RTU_DOT_NO = fes_digital.DOT_NO;
    
    UPDATE 
    (
    (background_x_fes_digtal INNER JOIN digital_map_to_fes 
    ON 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 
    ) INNER JOIN digital_temp_define ON digital_map_to_fes.POINT_TP_NAME = digital_temp_define.TAG_NAME
    ) INNER JOIN fes_digital ON fes_digital.DEV_TAG = background_x_fes_digtal.RTU_DEV_TAG AND fes_digital.SEQ_NO = digital_temp_define.SEC_SEQ_NO5
    SET 
    background_x_fes_digtal.RTU_TAG = fes_digital.RTU_TAG,
    background_x_fes_digtal.RTU_DOT_NO = fes_digital.DOT_NO;
    #mix
    UPDATE 
    (
    ( background_x_fes_mix INNER JOIN mix_map_to_fes 
    ON 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 
    ) INNER JOIN mix_temp_define ON mix_map_to_fes.POINT_TP_NAME = mix_temp_define.TAG_NAME
    ) INNER JOIN fes_mix ON fes_mix.DEV_TAG = background_x_fes_mix.RTU_DEV_TAG AND fes_mix.SEQ_NO = mix_temp_define.SEC_SEQ_NO
    SET 
    background_x_fes_mix.RTU_TAG = fes_mix.RTU_TAG,
    background_x_fes_mix.RTU_DOT_NO = fes_mix.DOT_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 background_x_fes_accuml.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 background_x_fes_analog.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 background_x_fes_digtal.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 background_x_fes_mix.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 background_x_fes_ana_ctrl.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 background_x_fes_dig_ctrl.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 background_x_fes_mix_ctrl.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 fes_accuml 
    FROM (
    fes_accuml INNER JOIN  fes_dev_info 
    ON fes_accuml.DEV_TAG = fes_dev_info.TAG_NAME AND fes_dev_info.TAG_NAME = concat(fes_accuml.RTU_TAG,'.', 'defaultDev')
    ) INNER JOIN background_x_fes_accuml ON fes_accuml.RTU_TAG = background_x_fes_accuml.RTU_TAG;
    
    DELETE fes_analog 
    FROM (
    fes_analog INNER JOIN  fes_dev_info 
    ON fes_analog.DEV_TAG = fes_dev_info.TAG_NAME AND fes_dev_info.TAG_NAME = concat(fes_analog.RTU_TAG,'.', 'defaultDev')
    ) INNER JOIN background_x_fes_analog ON fes_analog.RTU_TAG = background_x_fes_analog.RTU_TAG;
    
    DELETE fes_digital 
    FROM (
    fes_digital INNER JOIN  fes_dev_info 
    ON fes_digital.DEV_TAG = fes_dev_info.TAG_NAME AND fes_dev_info.TAG_NAME = concat(fes_digital.RTU_TAG,'.', 'defaultDev')
    ) INNER JOIN background_x_fes_digtal ON fes_digital.RTU_TAG = background_x_fes_digtal.RTU_TAG;
    DELETE fes_mix 
    FROM (
    fes_mix INNER JOIN  fes_dev_info 
    ON fes_mix.DEV_TAG = fes_dev_info.TAG_NAME AND fes_dev_info.TAG_NAME = concat(fes_mix.RTU_TAG,'.', 'defaultDev')
    ) INNER JOIN background_x_fes_mix ON fes_mix.RTU_TAG = background_x_fes_mix.RTU_TAG;
    
    DELETE fes_analog_ctrl 
    FROM (
    fes_analog_ctrl INNER JOIN  fes_dev_info 
    ON fes_analog_ctrl.DEV_TAG = fes_dev_info.TAG_NAME AND fes_dev_info.TAG_NAME = concat(fes_analog_ctrl.RTU_TAG,'.', 'defaultDev')
    ) INNER JOIN background_x_fes_ana_ctrl ON fes_analog_ctrl.RTU_TAG = background_x_fes_ana_ctrl.RTU_TAG;
    
    DELETE fes_digital_ctrl 
    FROM (
    fes_digital_ctrl INNER JOIN  fes_dev_info 
    ON fes_digital_ctrl.DEV_TAG = fes_dev_info.TAG_NAME AND fes_dev_info.TAG_NAME = concat(fes_digital_ctrl.RTU_TAG,'.', 'defaultDev')
    ) INNER JOIN background_x_fes_dig_ctrl ON fes_digital_ctrl.RTU_TAG = background_x_fes_dig_ctrl.RTU_TAG;
    
    DELETE fes_mix_ctrl 
    FROM (
    fes_mix_ctrl INNER JOIN  fes_dev_info 
    ON fes_mix_ctrl.DEV_TAG = fes_dev_info.TAG_NAME AND fes_dev_info.TAG_NAME = concat(fes_mix_ctrl.RTU_TAG,'.', 'defaultDev')
    ) INNER JOIN background_x_fes_mix_ctrl ON fes_mix_ctrl.RTU_TAG = background_x_fes_mix_ctrl.RTU_TAG;
        
    #对于非默认设备将所有FES与后台的联系清除
    UPDATE fes_accuml INNER JOIN background_x_fes_accuml ON 
    fes_accuml.TAG_NAME = concat(background_x_fes_accuml.RTU_TAG,'.',background_x_fes_accuml.RTU_DOT_NO)
    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;
    
    UPDATE fes_analog INNER JOIN background_x_fes_analog ON 
    fes_analog.TAG_NAME = concat(background_x_fes_analog.RTU_TAG,'.',background_x_fes_analog.RTU_DOT_NO)
    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;
    
    UPDATE fes_digital INNER JOIN background_x_fes_digtal ON 
    fes_digital.TAG_NAME = concat(background_x_fes_digtal.RTU_TAG,'.',background_x_fes_digtal.RTU_DOT_NO)
    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;
    
    UPDATE fes_mix INNER JOIN background_x_fes_mix ON 
    fes_mix.TAG_NAME = concat(background_x_fes_mix.RTU_TAG,'.',background_x_fes_mix.RTU_DOT_NO)
    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;
    
    UPDATE fes_analog_ctrl INNER JOIN background_x_fes_ana_ctrl ON
    fes_analog_ctrl.TAG_NAME = concat(background_x_fes_ana_ctrl.RTU_TAG,'.',background_x_fes_ana_ctrl.RTU_DOT_NO)
    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;
    
    UPDATE fes_digital_ctrl INNER JOIN background_x_fes_dig_ctrl ON
    fes_digital_ctrl.TAG_NAME = concat(background_x_fes_dig_ctrl.RTU_TAG,'.',background_x_fes_dig_ctrl.RTU_DOT_NO)
    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;
    
    UPDATE fes_mix_ctrl INNER JOIN background_x_fes_mix_ctrl ON
    fes_mix_ctrl.TAG_NAME = concat(background_x_fes_mix_ctrl.RTU_TAG,'.',background_x_fes_mix_ctrl.RTU_DOT_NO)
    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;
    #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 INNER JOIN background_x_fes_accuml
    ON concat('accuml','.',accuml.TAG_NAME)  = background_x_fes_accuml.BG_POINT_TAG
    SET accuml.RTU_TAG = background_x_fes_accuml.RTU_TAG;
   
    UPDATE analog INNER JOIN background_x_fes_analog
    ON concat('analog','.',analog.TAG_NAME)  = background_x_fes_analog.BG_POINT_TAG
    SET analog.RTU_TAG = background_x_fes_analog.RTU_TAG;
    
    UPDATE digital INNER JOIN background_x_fes_digtal
    ON concat('digital','.',digital.TAG_NAME)  = background_x_fes_digtal.BG_POINT_TAG
    SET digital.RTU_TAG = background_x_fes_digtal.RTU_TAG;
    
    UPDATE mix INNER JOIN background_x_fes_mix
    ON concat('mix','.',mix.TAG_NAME)  = background_x_fes_mix.BG_POINT_TAG
    SET mix.RTU_TAG = background_x_fes_mix.RTU_TAG;
    
    UPDATE accuml_map_to_fes INNER JOIN background_x_fes_accuml 
    ON 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
    SET accuml_map_to_fes.DOT_NO = background_x_fes_accuml.RTU_DOT_NO;
    
    UPDATE analog_map_to_fes INNER JOIN background_x_fes_analog 
    ON 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
    SET analog_map_to_fes.DOT_NO = background_x_fes_analog.RTU_DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN background_x_fes_digtal 
    ON  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
    SET  digital_map_to_fes.DOT_NO1 = background_x_fes_digtal.RTU_DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN background_x_fes_digtal 
    ON  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
    SET  digital_map_to_fes.DOT_NO2 = background_x_fes_digtal.RTU_DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN background_x_fes_digtal 
    ON  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
    SET  digital_map_to_fes.DOT_NO3 = background_x_fes_digtal.RTU_DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN background_x_fes_digtal 
    ON  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
    SET  digital_map_to_fes.DOT_NO4 = background_x_fes_digtal.RTU_DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN background_x_fes_digtal 
    ON  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
    SET  digital_map_to_fes.DOT_NO5 = background_x_fes_digtal.RTU_DOT_NO;
    
    UPDATE mix_map_to_fes INNER JOIN background_x_fes_mix
    ON 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
    SET mix_map_to_fes.DOT_NO = background_x_fes_mix.RTU_DOT_NO;
        
    #写入ctrl表,注意此时background_x_fes__ctrl中的RTU必须和analog_control中的RTU保持一致,确保该点是同一个RTU
    UPDATE analog_control INNER JOIN background_x_fes_ana_ctrl
    ON analog_control.TAG_NAME = background_x_fes_ana_ctrl.BG_POINT_TAG AND analog_control.RTU_TAG = background_x_fes_ana_ctrl.RTU_TAG
    SET analog_control.OFFSET_NO = background_x_fes_ana_ctrl.RTU_DOT_NO;
    
    UPDATE digital_control INNER JOIN background_x_fes_dig_ctrl
    ON 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
    SET digital_control.OFFSET_NO1 = background_x_fes_dig_ctrl.RTU_DOT_NO;
    
    UPDATE digital_control INNER JOIN background_x_fes_dig_ctrl
    ON 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
    SET digital_control.OFFSET_NO2 = background_x_fes_dig_ctrl.RTU_DOT_NO;
    
    UPDATE digital_control INNER JOIN background_x_fes_dig_ctrl
    ON 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
    SET digital_control.OFFSET_NO3 = background_x_fes_dig_ctrl.RTU_DOT_NO;
    
    UPDATE digital_control INNER JOIN background_x_fes_dig_ctrl
    ON 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
    SET digital_control.OFFSET_NO4 = background_x_fes_dig_ctrl.RTU_DOT_NO;
    
    UPDATE digital_control INNER JOIN background_x_fes_dig_ctrl
    ON 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
    SET digital_control.OFFSET_NO5 = background_x_fes_dig_ctrl.RTU_DOT_NO;
    
    UPDATE mix_control INNER JOIN background_x_fes_mix_ctrl
    ON mix_control.TAG_NAME = background_x_fes_mix_ctrl.BG_POINT_TAG AND mix_control.RTU_TAG = background_x_fes_mix_ctrl.RTU_TAG
    SET mix_control.OFFSET_NO = background_x_fes_mix_ctrl.RTU_DOT_NO;
    #写入完毕,下面为描述的处理
    
    #可选,保持实例前置设备(非默认设备)和excel文件中的一致
    IF importFesPointDesc = 1 THEN
    BEGIN
        
        UPDATE fes_accuml INNER JOIN background_x_fes_accuml 
        ON 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')
        SET fes_accuml.DESCRIPTION = background_x_fes_accuml.RTU_DOT_DESC ;
        
        UPDATE fes_analog INNER JOIN background_x_fes_analog 
        ON 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')
        SET fes_analog.DESCRIPTION = background_x_fes_analog.RTU_DOT_DESC ;
        
        UPDATE fes_digital INNER JOIN background_x_fes_digtal 
        ON 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')
        SET fes_digital.DESCRIPTION = background_x_fes_digtal.RTU_DOT_DESC ;
        
        UPDATE fes_mix INNER JOIN background_x_fes_mix 
        ON 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')
        SET fes_mix.DESCRIPTION = background_x_fes_mix.RTU_DOT_DESC ;
        
        UPDATE fes_analog_ctrl INNER JOIN background_x_fes_ana_ctrl
        ON 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')
        SET fes_analog_ctrl.DESCRIPTION = background_x_fes_ana_ctrl.RTU_DOT_DESC;
        
        UPDATE fes_digital_ctrl INNER JOIN background_x_fes_dig_ctrl
        ON 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')
        SET fes_digital_ctrl.DESCRIPTION = background_x_fes_dig_ctrl.RTU_DOT_DESC;
        
        UPDATE fes_mix_ctrl INNER JOIN background_x_fes_mix_ctrl
        ON 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')
        SET fes_mix_ctrl.DESCRIPTION = background_x_fes_mix_ctrl.RTU_DOT_DESC;
        
    END;
    END IF;
    
    #把前置描述导入后台,检索map_to_fes表,查找联系
    IF fesDescToBG = 1 THEN 
    BEGIN 
        UPDATE (accuml INNER JOIN accuml_map_to_fes 
        ON 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,'')<>'')
        INNER JOIN fes_accuml ON fes_accuml.TAG_NAME = concat( accuml_map_to_fes.RTU_TAG ,'.', accuml_map_to_fes.DOT_NO) 
        SET accuml.DESCRIPTION = fes_accuml.DESCRIPTION;
        
        UPDATE (analog INNER JOIN analog_map_to_fes 
        ON 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,'')<>'')
        INNER JOIN fes_analog ON fes_analog.TAG_NAME = concat(analog_map_to_fes.RTU_TAG ,'.',  analog_map_to_fes.DOT_NO) 
        SET analog.DESCRIPTION = fes_analog.DESCRIPTION;
        #更新mix的
        UPDATE (mix INNER JOIN mix_map_to_fes 
        ON 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,'')<>'')
        INNER JOIN fes_mix ON fes_mix.TAG_NAME = concat(mix_map_to_fes.RTU_TAG ,'.', mix_map_to_fes.DOT_NO) 
        SET mix.DESCRIPTION = fes_mix.DESCRIPTION;
        #更新dig的,以第一个点为准,其他点的描述不考虑
        UPDATE (digital INNER JOIN digital_map_to_fes 
        ON 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,'')<>'')
        INNER JOIN fes_digital ON fes_digital.TAG_NAME = concat(digital_map_to_fes.RTU_TAG ,'.', digital_map_to_fes.DOT_NO1) 
        SET digital.DESCRIPTION = fes_digital.DESCRIPTION;
        
    END;
    END IF;
    
    #把同步实时库的功能重新打开
    IF openTriggerInfo <> 1 THEN 
    UPDATE sys_trigger_flag SET FLAG_ID = 1;
    END IF;
    
END;;
DELIMITER ;

drop procedure if exists importPsc3000;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `importPsc3000`(rtuTag varchar(64)) 
body:BEGIN
    /*
     * 导入PSC3000的数据,数据在调用本存储过程前已经把excel表格中的数据存入PSC3000开头的表格中
     * 1.对于设备的处理,以excel表中设备ID和设备模板为判断基准,
               数据库中不存在该ID和模板一样的设备,则往数据库中增加该设备
                对于数据库中存在,而excel表中不存在的设备,删除数据库中的设备.
                对于数据库中存在ID和模板与excel一样的设备,对设备的名称和TAG_NAME和描述,这三个字段进行更新
        2.对于测点的处理,以设备ID和点名缩写为判断基准
                对于转发信息表中存在,而数据库中不存在的测点,进行添加操作
                对于转发信息表和数据库中都存在的测点,进行更新操作,更新字段规约参数1,点描述,变比系数,修正值,值描述文件(定值)
     * 遥控由于没有点名缩写,所以使用描述为判断基准
     */
    DECLARE count1 int DEFAULT 0;
    DECLARE count2 int DEFAULT 0;
    
    IF COALESCE(rtuTag,'') = '' THEN LEAVE body;
    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 INNER JOIN psc3000_dev 
    ON  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
    SET fes_dev_info.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;
    
    #筛选出要插入的设备  
    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 ( tmpImportPsc3000Tbl INNER JOIN fes_accuml ON tmpImportPsc3000Tbl.TAG_NAME = fes_accuml.TAG_NAME )
    INNER JOIN psc3000_acc ON tmpImportPsc3000Tbl.DEV_ID = psc3000_acc.DEV_ID 
    AND tmpImportPsc3000Tbl.SHORT_NAME = psc3000_acc.TAG_NAME
    SET fes_accuml.DESCRIPTION = psc3000_acc.DESCRIPTION,
    fes_accuml.RES_PARA_INT1 = psc3000_acc.REMOTE_NO,
    fes_accuml.COEFF = psc3000_acc.RATIO;
    
    #添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    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 ( tmpImportPsc3000Tbl INNER JOIN fes_analog ON tmpImportPsc3000Tbl.TAG_NAME = fes_analog.TAG_NAME )
    INNER JOIN psc3000_ana ON tmpImportPsc3000Tbl.DEV_ID = psc3000_ana.DEV_ID 
    AND tmpImportPsc3000Tbl.SHORT_NAME = psc3000_ana.TAG_NAME
    SET fes_analog.DESCRIPTION = psc3000_ana.DESCRIPTION,
    fes_analog.RES_PARA_INT1 = psc3000_ana.REMOTE_NO,
    fes_analog.COEFF = psc3000_ana.RATIO;
    
    #添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    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 ( tmpImportPsc3000Tbl INNER JOIN fes_digital ON tmpImportPsc3000Tbl.TAG_NAME = fes_digital.TAG_NAME )
    INNER JOIN psc3000_dig ON tmpImportPsc3000Tbl.DEV_ID = psc3000_dig.DEV_ID 
    AND tmpImportPsc3000Tbl.SHORT_NAME = psc3000_dig.TAG_NAME
    SET fes_digital.DESCRIPTION = psc3000_dig.DESCRIPTION,
    fes_digital.RES_PARA_INT1 = psc3000_dig.REMOTE_NO;
    
    #添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    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 ( tmpImportPsc3000Tbl INNER JOIN fes_digital_ctrl ON tmpImportPsc3000Tbl.TAG_NAME = fes_digital_ctrl.TAG_NAME )
    INNER JOIN psc3000_dig_ctrl ON tmpImportPsc3000Tbl.DEV_ID = psc3000_dig_ctrl.DEV_ID 
    AND tmpImportPsc3000Tbl.SHORT_NAME = psc3000_dig_ctrl.DESCRIPTION
    SET fes_digital_ctrl.DESCRIPTION = psc3000_dig_ctrl.DESCRIPTION,
    fes_digital_ctrl.RES_PARA_INT1 = psc3000_dig_ctrl.REMOTE_NO;
    
    #添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    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 ( tmpImportPsc3000Tbl INNER JOIN fes_const ON tmpImportPsc3000Tbl.TAG_NAME = fes_const.TAG_NAME )
    INNER JOIN psc3000_const ON tmpImportPsc3000Tbl.DEV_ID = psc3000_const.DEV_ID 
    AND tmpImportPsc3000Tbl.SEQ_NO = psc3000_const.SEQ_NO
    SET fes_const.DESCRIPTION = psc3000_const.DESCRIPTION,
    fes_const.VALUE_RATIO = psc3000_const.RATIO,
    fes_const.VALUE_ADDED = psc3000_const.REVISE,
    fes_const.VALUE_TEXT = psc3000_const.VALUE_TEXT
    ;
    
    #添加数据库中不存在的测点,一般来说不需要添加,出现添加的情况是因为前置模板导入前和导入后不一致才会造成
    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;;
DELIMITER ;

drop procedure if exists importPsc3000ByPlugin;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `importPsc3000ByPlugin`(rtuTag varchar(64),regionId decimal(10,0)) 
body:BEGIN
    /*
     * 导入PSC3000数据通过构建套件的形式
     * rtuTag 要导入的RTU
     * regionId 责任区ID
     * PSC3000_plugin表中存着要导入的套件
     * PSC3000_DEV 表中存着导入的前置设备
     * PSC3000_acc,ana,dig,mix,dig_ctrl,const存着对应的测点数据
     */
    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 hasDone int DEFAULT 0;
    DECLARE myDevTag varchar(64) DEFAULT '';
    
    #在此游标中临时表的第一列是套件名,第二列是设备组名,游标获取4列,分别是设备组,套件名,后台模板名,前置模板名
    DECLARE cur_1 CURSOR 
    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;
    
    
    DECLARE cur_2 CURSOR
    FOR
    SELECT PLUGIN_NAME
    FROM tmpImportPsc3000PluginTbl WHERE tmpImportPsc3000PluginTbl.DEV_GRP = '';
    
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET hasDone = 1;
    
    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 GROUP BY psc3000_plugin.DEV_GROUP_TAG;
    
    #第二步 实例化后台和前置的设备,对于后台设备 一个设备组对应一个套件
    DELETE FROM tmpImportPsc3000PluginTbl;
    #此时临时表中第一列是套件名,第二列是设备组名
    INSERT INTO tmpImportPsc3000PluginTbl (PLUGIN_NAME , DEV_GRP) 
    SELECT PLUGIN_NAME,DEV_GROUP_TAG
    FROM psc3000_plugin GROUP BY PLUGIN_NAME,DEV_GROUP_TAG;
    
    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 LEFT JOIN psc3000_plugin ON DEV_ID = FES_DEV_INST_ID
    SET psc3000_dev.DEV_MODEL = IF(COALESCE(FES_TEMP_NAME,DEV_MODEL) != DEV_MODEL,COALESCE(FES_TEMP_NAME,DEV_MODEL), DEV_MODEL);
    
    DELETE psc3000_dev FROM psc3000_dev LEFT JOIN psc3000_plugin ON psc3000_dev.DEV_ID = psc3000_plugin.FES_DEV_INST_ID 
    WHERE FES_DEV_INST_ID IS NULL;
    
    DELETE tmpPsc3000PluginFesTempTbl FROM tmpPsc3000PluginFesTempTbl LEFT JOIN psc3000_plugin 
    ON tmpPsc3000PluginFesTempTbl.DEV_ID = psc3000_plugin.FES_DEV_INST_ID 
    WHERE FES_DEV_INST_ID IS NULL;
    
    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 GROUP BY PLUGIN_NAME,DEV_GROUP_TAG;
    
    #创建临时表,存储用于第五步骤的信息
    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;
    FETCH cur_1 INTO devGrpTag,pluginName,tempName,fesTempName;
    
    WHILE hasDone != 1 DO
        
        SET 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;
        
        FETCH cur_1 INTO devGrpTag,pluginName,tempName,fesTempName;
        
    END WHILE;
    CLOSE cur_1;
    
	#更新设备组编号
	UPDATE dev_group,psc3000_plugin SET dev_group.DEV_GROUP_NO = psc3000_plugin.FES_DEV_INST_ID 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 ( tblImportPsc3000Point INNER JOIN fes_accuml ON tblImportPsc3000Point.TAG_NAME = fes_accuml.TAG_NAME )
    INNER JOIN psc3000_acc ON tblImportPsc3000Point.DEV_ID = psc3000_acc.DEV_ID 
    AND tblImportPsc3000Point.SHORT_NAME = psc3000_acc.TAG_NAME
    SET fes_accuml.DESCRIPTION = psc3000_acc.DESCRIPTION,
    fes_accuml.RES_PARA_INT1 = psc3000_acc.REMOTE_NO,
	fes_accuml.SEQ_NO = psc3000_acc.REMOTE_NO,
    fes_accuml.COEFF = psc3000_acc.RATIO,
	fes_accuml.FES_POINT_TP_NAME = concat(getBeforeFirstDot(fes_accuml.FES_POINT_TP_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 ( tblImportPsc3000Point INNER JOIN fes_analog ON tblImportPsc3000Point.TAG_NAME = fes_analog.TAG_NAME )
    INNER JOIN psc3000_ana ON tblImportPsc3000Point.DEV_ID = psc3000_ana.DEV_ID 
    AND tblImportPsc3000Point.SHORT_NAME = psc3000_ana.TAG_NAME
    SET fes_analog.DESCRIPTION = psc3000_ana.DESCRIPTION,
    fes_analog.RES_PARA_INT1 = psc3000_ana.REMOTE_NO,
	fes_analog.SEQ_NO = psc3000_ana.REMOTE_NO,
    fes_analog.COEFF = psc3000_ana.RATIO,
	fes_analog.FES_POINT_TP_NAME = concat(getBeforeFirstDot(fes_analog.FES_POINT_TP_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 ( tblImportPsc3000Point INNER JOIN fes_digital ON tblImportPsc3000Point.TAG_NAME = fes_digital.TAG_NAME )
    INNER JOIN psc3000_dig ON tblImportPsc3000Point.DEV_ID = psc3000_dig.DEV_ID 
    AND tblImportPsc3000Point.SHORT_NAME = psc3000_dig.TAG_NAME
    SET fes_digital.DESCRIPTION = psc3000_dig.DESCRIPTION,
    fes_digital.RES_PARA_INT1 = psc3000_dig.REMOTE_NO,
	fes_digital.SEQ_NO = psc3000_dig.REMOTE_NO,
	fes_digital.FES_POINT_TP_NAME = concat(getBeforeFirstDot(fes_digital.FES_POINT_TP_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 ( tblImportPsc3000Point INNER JOIN fes_digital_ctrl ON tblImportPsc3000Point.TAG_NAME = fes_digital_ctrl.TAG_NAME )
    INNER JOIN psc3000_dig_ctrl ON tblImportPsc3000Point.DEV_ID = psc3000_dig_ctrl.DEV_ID 
    AND tblImportPsc3000Point.SHORT_NAME = psc3000_dig_ctrl.TAG_NAME
    SET fes_digital_ctrl.DESCRIPTION = psc3000_dig_ctrl.DESCRIPTION,
    fes_digital_ctrl.RES_PARA_INT1 = psc3000_dig_ctrl.REMOTE_NO,
	fes_digital_ctrl.SEQ_NO = psc3000_dig_ctrl.REMOTE_NO,
	fes_digital_ctrl.POINT_TP_NAME = concat(getBeforeFirstDot(fes_digital_ctrl.POINT_TP_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 IGNORE 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 INNER JOIN tblPsc3000PointToAdd 
    ON accuml.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET accuml.RTU_TAG = rtuTag;
    
    UPDATE accuml_map_to_fes INNER JOIN tblPsc3000PointToAdd
    ON accuml_map_to_fes.KEY_ID_TAG = concat('accuml.',tblPsc3000PointToAdd.TAG_NAME)
    SET accuml_map_to_fes.DOT_NO = tblPsc3000PointToAdd.DOT_NO;
    
    
    #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 IGNORE 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 INNER JOIN tblPsc3000PointToAdd 
    ON analog.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET analog.RTU_TAG = rtuTag;
    
    UPDATE analog_map_to_fes INNER JOIN tblPsc3000PointToAdd
    ON analog_map_to_fes.KEY_ID_TAG = concat('analog.',tblPsc3000PointToAdd.TAG_NAME)
    SET analog_map_to_fes.DOT_NO = tblPsc3000PointToAdd.DOT_NO;
    
    #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 IGNORE 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 INNER JOIN tblPsc3000PointToAdd 
    ON digital.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET digital.RTU_TAG = rtuTag;
    
    UPDATE digital_map_to_fes INNER JOIN tblPsc3000PointToAdd
    ON digital_map_to_fes.KEY_ID_TAG = concat('digital.',tblPsc3000PointToAdd.TAG_NAME)
    SET digital_map_to_fes.DOT_NO1 = tblPsc3000PointToAdd.DOT_NO;
   
    
    
    #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 IGNORE 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 INNER JOIN tblPsc3000PointToAdd 
    ON mix.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET mix.RTU_TAG = rtuTag;
    
    UPDATE mix_map_to_fes INNER JOIN tblPsc3000PointToAdd
    ON mix_map_to_fes.KEY_ID_TAG = concat('mix.',tblPsc3000PointToAdd.TAG_NAME)
    SET mix_map_to_fes.DOT_NO = tblPsc3000PointToAdd.DOT_NO;
   
    #第六步,应施建全和王重生的要求,对于后台生成的设备组中未关联的测点,删除,对于前置中不存在与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) GROUP BY dev_info.TAG_NAME;
    
    DELETE accuml FROM accuml INNER JOIN tmpImportPsc3000PluginTbl ON accuml.DEVICE = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    AND COALESCE(accuml.RTU_TAG,'') = '';
    
    DELETE analog FROM analog INNER JOIN tmpImportPsc3000PluginTbl ON analog.DEVICE = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    AND COALESCE(analog.RTU_TAG,'') = '';
    
    DELETE digital FROM digital INNER JOIN tmpImportPsc3000PluginTbl ON digital.DEVICE = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    AND COALESCE(digital.RTU_TAG,'') = '';
    
    DELETE mix FROM mix INNER JOIN tmpImportPsc3000PluginTbl ON 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 accuml FROM accuml INNER JOIN tblPsc3000PointToAdd ON accuml.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    DELETE fes_accuml FROM fes_accuml INNER JOIN tblPsc3000PointToAdd ON 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 analog FROM analog INNER JOIN tblPsc3000PointToAdd ON analog.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    DELETE fes_analog FROM fes_analog INNER JOIN tblPsc3000PointToAdd ON 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 fes_digital FROM fes_digital INNER JOIN tblPsc3000PointToAdd ON fes_digital.TAG_NAME = tblPsc3000PointToAdd.FES_TAG_NAME;
    
    #先标记要删除的点
    UPDATE tblPsc3000PointToAdd INNER JOIN digital_map_to_fes 
    ON 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'
    SET tblPsc3000PointToAdd.DOT_NO = -1;
    
    DELETE digital FROM digital INNER JOIN tblPsc3000PointToAdd 
    ON 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 mix FROM mix INNER JOIN tblPsc3000PointToAdd ON mix.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    DELETE fes_mix FROM fes_mix INNER JOIN tblPsc3000PointToAdd ON fes_mix.TAG_NAME = tblPsc3000PointToAdd.FES_TAG_NAME;
    
    #ana_ctrl
    DELETE FROM tblPsc3000PointToAdd;
    
    DELETE fes_analog_ctrl 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 analog_control FROM analog_control INNER JOIN tblPsc3000PointToAdd ON analog_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME;
    
    
    #dig_ctrl 要考虑五个分量
    DELETE FROM tblPsc3000PointToAdd;
    
    DELETE fes_digital_ctrl 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 INNER JOIN tblPsc3000PointToAdd 
    ON digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET digital_control.OFFSET_NO1 = -1;
    
    #第二个分量
    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 INNER JOIN tblPsc3000PointToAdd 
    ON digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET digital_control.OFFSET_NO2 = -1;
    
    #第三个分量
    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 INNER JOIN tblPsc3000PointToAdd 
    ON digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET digital_control.OFFSET_NO3 = -1;
    
    #第四个分量
    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 INNER JOIN tblPsc3000PointToAdd 
    ON digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET digital_control.OFFSET_NO4 = -1;
    
    #第五个分量
    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 INNER JOIN tblPsc3000PointToAdd 
    ON digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET digital_control.OFFSET_NO5 = -1;
    
    #设置未关联的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 fes_mix_ctrl 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 mix_control FROM mix_control INNER JOIN tblPsc3000PointToAdd ON 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 INNER JOIN tblPsc3000PointToAdd ON concat('digital.',digital.TAG_NAME) = tblPsc3000PointToAdd.TAG_NAME
    SET digital.VALUE_NUM = 1,
        digital.STATE_TEXT_NAME = IF(position('(' IN STATE_TEXT_NAME) = 0,STATE_TEXT_NAME,left(STATE_TEXT_NAME,position('(' IN STATE_TEXT_NAME) - 1));
    
    #双分量属性
    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 INNER JOIN tblPsc3000PointToAdd ON concat('digital.',digital.TAG_NAME) = tblPsc3000PointToAdd.TAG_NAME
    SET digital.VALUE_NUM = 2;
    
    #对于单控制
    
    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 INNER JOIN tblPsc3000PointToAdd ON digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET digital_control.OFFSET_NUM = 1;
    
    
    #对于双控制
    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 INNER JOIN tblPsc3000PointToAdd ON digital_control.TAG_NAME = tblPsc3000PointToAdd.TAG_NAME
    SET digital_control.OFFSET_NUM = 2;
    
    #*****************************赵勇新增功能(给后台添加控制点与前置未关联的控制点进行关联)******************************
	#临时表 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 not in
	(SELECT OFFSET_NO1 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO1 <> -1) 
	and fes_digital_ctrl.DOT_NO not in
	(SELECT OFFSET_NO2 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO2 <> -1) 
	and fes_digital_ctrl.DOT_NO not in
	(SELECT OFFSET_NO3 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO3 <> -1) 
	and fes_digital_ctrl.DOT_NO not in
	(SELECT OFFSET_NO4 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO4 <> -1) 
	and fes_digital_ctrl.DOT_NO not in
	(SELECT OFFSET_NO5 FROM DIGITAL_CONTROL WHERE RTU_TAG = rtuTag AND OFFSET_NO5 <> -1)
	;
		
	#执行插入操作
	INSERT IGNORE 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 INNER JOIN tmpRelationControlTbl ON digital_control.TAG_NAME = tmpRelationControlTbl.TAG_NAME
    SET digital_control.OFFSET_NUM = 1,OFFSET_NO1 = tmpRelationControlTbl.DOT_NO;
	#analog_control
		
	#*****************************赵勇添加结束****************************************************************************
    
	#第八步,删除空设备,临时表tmpImportPsc3000PluginTbl中存储设备信息
    UPDATE tmpImportPsc3000PluginTbl INNER JOIN digital ON tmpImportPsc3000PluginTbl.PLUGIN_NAME = digital.DEVICE
    SET tmpImportPsc3000PluginTbl.DEV_GRP = '';
    
    UPDATE tmpImportPsc3000PluginTbl INNER JOIN accuml ON tmpImportPsc3000PluginTbl.PLUGIN_NAME = accuml.DEVICE
    SET tmpImportPsc3000PluginTbl.DEV_GRP = '';
    
    UPDATE tmpImportPsc3000PluginTbl INNER JOIN analog ON tmpImportPsc3000PluginTbl.PLUGIN_NAME = analog.DEVICE
    SET tmpImportPsc3000PluginTbl.DEV_GRP = '';
    
    UPDATE tmpImportPsc3000PluginTbl INNER JOIN mix ON tmpImportPsc3000PluginTbl.PLUGIN_NAME = mix.DEVICE
    SET tmpImportPsc3000PluginTbl.DEV_GRP = '';
    
    #此时空设备的DEV_GRP不为空,删除
    DELETE dev_info FROM dev_info INNER JOIN tmpImportPsc3000PluginTbl ON dev_info.TAG_NAME = tmpImportPsc3000PluginTbl.PLUGIN_NAME
    WHERE tmpImportPsc3000PluginTbl.DEV_GRP != '';
    
    #第九步,要求自动同步描述
    /*SET hasDone = 0;
    OPEN cur_2;
    FETCH cur_2 INTO myDevTag;
    
    WHILE hasDone != 1 DO
        
        IF myDevTag IS NOT NULL THEN 
        BEGIN
            CALL updateDescByFes(myDevTag);
        END; 
        END IF;
        
        FETCH cur_2 INTO myDevTag;
        
    END WHILE;
    CLOSE cur_2;*/
    
    #第十步，先删除保护定值和数据块，再导入模板里的保护定值和数据块,此时tmpPsc3000PluginFesTempTbl表中是导入的前置设备
    DELETE fes_data_block FROM fes_data_block INNER JOIN 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((DEV_ID DIV 10000) = 0,DEV_ID,(DEV_ID DIV 10000)) WHERE RTU_TAG = rtuTag;
END;;
DELIMITER ;

drop procedure if exists InsertForwardAcc;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `InsertForwardAcc`(rtuTagName varchar(64), startNum decimal(10,0)) 
body:BEGIN
    #使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    #批量插入前置表,
    #rtuTagName 指明插入哪个转发RTU
    #startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    DECLARE countNum int DEFAULT 0;
    SELECT count(*) INTO countNum FROM export_seq; 
    
    #相当于对要空出要插入的位置
    UPDATE fes_forwarding_accuml 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum ORDER BY ID DESC;
    
    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;;
DELIMITER ;

drop procedure if exists InsertForwardAna;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `InsertForwardAna`(rtuTagName varchar(64), startNum decimal(10,0)) 
body:BEGIN
    #使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    #批量插入前置表,
    #rtuTagName 指明插入哪个转发RTU
    #startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    DECLARE countNum int DEFAULT 0;
    SELECT count(*) INTO countNum FROM export_seq; 
    
    #相当于对要空出要插入的位置
    UPDATE fes_forwarding_anaolg 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum ORDER BY ID DESC;
    
    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;;
DELIMITER ;

drop procedure if exists InsertForwardAnaCtrl;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `InsertForwardAnaCtrl`(rtuTagName varchar(64), startNum decimal(10,0)) 
body:BEGIN
    #使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    #批量插入前置表,
    #rtuTagName 指明插入哪个转发RTU
    #startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    DECLARE countNum int DEFAULT 0;
    SELECT count(*) INTO countNum FROM export_seq; 
    
    #相当于对要空出要插入的位置
    UPDATE fes_forwarding_anaolg_ctrl 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum ORDER BY ID DESC;
    
    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;;
DELIMITER ;

drop procedure if exists InsertForwardDig;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `InsertForwardDig`(rtuTagName varchar(64), startNum decimal(10,0)) 
body:BEGIN
    #使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    #批量插入前置表,
    #rtuTagName 指明插入哪个转发RTU
    #startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    DECLARE countNum int DEFAULT 0;
    SELECT count(*) INTO countNum FROM export_seq; 
    
    #相当于对要空出要插入的位置
    UPDATE fes_forwarding_digital 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum ORDER BY ID DESC;
    
    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;;
DELIMITER ;

drop procedure if exists InsertForwardDigCtrl;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `InsertForwardDigCtrl`(rtuTagName varchar(64), startNum decimal(10,0)) 
body:BEGIN
    #使用本方法前,先把要插入的点,放在EXPORT_SEQ表中
    #批量插入前置表,
    #rtuTagName 指明插入哪个转发RTU
    #startNum 指明插入位置,当startNum等于指定rtuTagName的最大ID时,相当于末尾添加
    
    DECLARE countNum int DEFAULT 0;
    SELECT count(*) INTO countNum FROM export_seq; 
    
    #相当于对要空出要插入的位置
    UPDATE fes_forwarding_digital_ctrl 
    SET ID = ID + countNum
    WHERE RTU_TAG = rtuTagName AND ID > startNum ORDER BY ID DESC;
    
    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;;
DELIMITER ;

drop procedure if exists instDevTempGroup;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `instDevTempGroup`(srcTempGroupName varchar(64), dstDevGroupName varchar(64)) 
body:BEGIN
    /*
     * 实例化整个模板组
     */
    DECLARE locationTagName varchar(64) DEFAULT '';
   # DECLARE totalInst int DEFAULT 1;
    
    IF COALESCE(srcTempGroupName,'') = '' OR COALESCE(dstDevGroupName,'')=''
    THEN LEAVE body;
    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 LEAVE body;
    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;;
DELIMITER ;

drop procedure if exists instPlugin;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `instPlugin`(pluginName varchar(64),rtuTag varchar(64),devGroupName varchar(64), isBatchInGrp int,startNo int,countNo int) 
body:BEGIN
    #先实例化后台和前置设备,再关联
    #pluginName  套件名
    #rtuTag rtu名
    #devGroupName 设备组名
    #isBatchInGrp 是否在组内批量实例化
    #startNo 批量化的起始序号
    #countNo 批量的数量
    
    #前置的命名规则更后台要有一一对应,批量时有编号,不批量时无编号
    #后台是设备组_模板名+编号
    #前置也是设备组_模板+编号
    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 hasDone 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;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET hasDone = 1;
    
    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;
    SET sampleGrpName = getAfterLastDot(devGroupName);
	SET 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;
        
        #循环调用存储过程进行关联
        OPEN cur_relation;
        FETCH cur_relation INTO tempName,fesTempName;
            WHILE hasDone != 1 DO
               
                CALL relationByPlugin(pluginName,concat(devGroupName,'_',tempName), concat(rtuTag,'.',sampleGrpName,'_',fesTempName));
                FETCH cur_relation INTO tempName,fesTempName;
            END WHILE;
        
        CLOSE cur_relation;
    END;
    ELSE
    BEGIN
        #批量时,设备都带上编号
        #实例化后台设备,注意TAG_NAME的命名规则
        SET stepCount = startNo;
        WHILE stepCount < (startNo + countNo) DO
            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;
            
            #循环调用存储过程进行关联
            OPEN cur_relation;
            FETCH cur_relation INTO tempName,fesTempName;
            WHILE hasDone != 1 DO
                
                CALL relationByPlugin(pluginName,concat(devGroupName,'_',tempName,stepCount), concat(rtuTag,'.',sampleGrpName,'_',fesTempName,stepCount));
                FETCH cur_relation INTO tempName,fesTempName;
            END WHILE;
        
            CLOSE cur_relation;  
        
            SET stepCount = stepCount + 1;
            SET hasDone = 0;
        END WHILE;
        
    END; 
    END IF;
    
    
END;;
DELIMITER ;

drop procedure if exists instPluginByFesDev;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `instPluginByFesDev`(pluginName varchar(64),rtuTag varchar(64),devGroupName varchar(64), fesDevTag varchar(64),fesDevDes varchar(64)) 
body:BEGIN
    #先实例化后台和前置设备,再关联
    #pluginName  套件名
    #rtuTag rtu名
    #devGroupName 设备组名
    #fesDevTag 前置设备组标签
    #fesDevDes 前置设备组描述
    
    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 hasDone int DEFAULT 0;
    DECLARE fesDevCount int DEFAULT 0;
    DECLARE cur_relation CURSOR 
    FOR 
    SELECT TEMP_NAME FROM temp_plugin_fes_dev_map WHERE PLUGIN_NAME = pluginName;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET hasDone = 1;
    
    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;
    SET sampleGrpName = getAfterLastDot(devGroupName);
    SET 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;
        
        #循环调用存储过程进行关联
        OPEN cur_relation;
        FETCH cur_relation INTO tempName;
            WHILE hasDone != 1 DO
               
                CALL relationByPlugin(pluginName,concat(devGroupName,'_',tempName), concat(rtuTag,'.',fesDevTag));
                FETCH cur_relation INTO tempName;
            END WHILE;
        
        CLOSE cur_relation;
    END; 
END;;
DELIMITER ;

drop procedure if exists relationByDevGroup;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `relationByDevGroup`(groupName varchar(64)) 
body: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 INNER JOIN tmpRelationGroupTbl ON 
    fes_accuml.TAG_NAME = concat(tmpRelationGroupTbl.RTU_TAG,'.', tmpRelationGroupTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    
    #清除后台的
    UPDATE accuml INNER JOIN dev_info 
    ON dev_info.GROUP_TAG_NAME = groupName AND accuml.DEVICE = dev_info.TAG_NAME
    SET RTU_TAG = '';
    
    #模拟手动操作,先关联对应RTU,再关联对应点
    UPDATE accuml INNER JOIN tmpRelationGroupTbl 
    ON accuml.TAG_NAME = getAfterFirstDot(tmpRelationGroupTbl.KEY_ID_TAG) 
    SET accuml.RTU_TAG = tmpRelationGroupTbl.RTU_TAG;
    
    UPDATE accuml_map_to_fes INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON accuml_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG 
    SET accuml_map_to_fes.DOT_NO = t1.DOT_NO; 
    
    #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 INNER JOIN tmpRelationGroupTbl ON 
    fes_analog.TAG_NAME = concat(tmpRelationGroupTbl.RTU_TAG,'.', tmpRelationGroupTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    #清除后台的
    UPDATE analog INNER JOIN dev_info 
    ON dev_info.GROUP_TAG_NAME = groupName AND analog.DEVICE = dev_info.TAG_NAME
    SET RTU_TAG = '';
    
    UPDATE analog INNER JOIN tmpRelationGroupTbl 
    ON analog.TAG_NAME = getAfterFirstDot(tmpRelationGroupTbl.KEY_ID_TAG) 
    SET analog.RTU_TAG = tmpRelationGroupTbl.RTU_TAG;
    
    UPDATE analog_map_to_fes INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON analog_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG 
    SET analog_map_to_fes.DOT_NO = t1.DOT_NO;
    
    #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 INNER JOIN tmpRelationGroupTbl ON 
    fes_digital.TAG_NAME = concat(tmpRelationGroupTbl.RTU_TAG,'.', tmpRelationGroupTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    #清除后台的
    UPDATE digital INNER JOIN dev_info 
    ON dev_info.GROUP_TAG_NAME = groupName AND digital.DEVICE = dev_info.TAG_NAME
    SET RTU_TAG = '';
    
    UPDATE digital INNER JOIN tmpRelationGroupTbl 
    ON digital.TAG_NAME = getAfterFirstDot(tmpRelationGroupTbl.KEY_ID_TAG) 
    SET digital.RTU_TAG = tmpRelationGroupTbl.RTU_TAG;
    
    UPDATE digital_map_to_fes INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 1
    SET digital_map_to_fes.DOT_NO1 = t1.DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 2
    SET digital_map_to_fes.DOT_NO2 = t1.DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 3
    SET digital_map_to_fes.DOT_NO3 = t1.DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 4
    SET digital_map_to_fes.DOT_NO4 = t1.DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG AND t1.OFFSET_NO = 5
    SET digital_map_to_fes.DOT_NO5 = t1.DOT_NO;
    
    #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 INNER JOIN tmpRelationGroupTbl ON 
    fes_mix.TAG_NAME = concat(tmpRelationGroupTbl.RTU_TAG,'.', tmpRelationGroupTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    #清除后台的
    UPDATE mix INNER JOIN dev_info 
    ON dev_info.GROUP_TAG_NAME = groupName AND mix.DEVICE = dev_info.TAG_NAME
    SET RTU_TAG = '';
    
    UPDATE mix INNER JOIN tmpRelationGroupTbl 
    ON mix.TAG_NAME = getAfterFirstDot(tmpRelationGroupTbl.KEY_ID_TAG) 
    SET mix.RTU_TAG = tmpRelationGroupTbl.RTU_TAG;
    
    UPDATE mix_map_to_fes INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON mix_map_to_fes.KEY_ID_TAG = t1.KEY_ID_TAG 
    SET mix_map_to_fes.DOT_NO = t1.DOT_NO;
    
    #控制点的关联,因为有控制点,肯定有前置点,所以控制表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 INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON analog_control.TAG_NAME = t1.KEY_ID_TAG 
    SET analog_control.OFFSET_NO = t1.DOT_NO;
    
    #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 INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 1
    SET digital_control.OFFSET_NO1 = t1.DOT_NO;
    
    UPDATE digital_control INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 2
    SET digital_control.OFFSET_NO2 = t1.DOT_NO;
    
    UPDATE digital_control INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 3
    SET digital_control.OFFSET_NO3 = t1.DOT_NO;
    
    UPDATE digital_control INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 4
    SET digital_control.OFFSET_NO4 = t1.DOT_NO;
    
    UPDATE digital_control INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO ,OFFSET_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON digital_control.TAG_NAME = t1.KEY_ID_TAG AND t1.OFFSET_NO = 5
    SET digital_control.OFFSET_NO5 = t1.DOT_NO;
    
    #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 INNER JOIN 
    (SELECT KEY_ID_TAG,DOT_NO
    FROM tmpRelationGroupTbl GROUP BY RTU_TAG,DOT_NO ) t1 
    ON mix_control.TAG_NAME = t1.KEY_ID_TAG 
    SET mix_control.OFFSET_NO = t1.DOT_NO;
    
END;;
DELIMITER ;

drop procedure if exists relationByPlugin;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `relationByPlugin`(pluginName varchar(64), devTag varchar(64),fesDevTag varchar(64)) 
body:BEGIN
    #使用套件中的关联关系,关联前置和后台设备,仅关联套件中指定了关联关系的测点,需注意,为了加快速度,默认此时的后台设备的RTU标签为空,若不为空,调用此存储过程前,请先置空
    #pluginName 套件名
    #devTag 后台设备名
    #fesDevTag 前置设备名
    DECLARE rtuTag varchar(64) DEFAULT '';
    DECLARE tempName varchar(64) DEFAULT '';
    DECLARE fesTempName varchar(64) DEFAULT '';
    
    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 
    LEAVE body; 
    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 INNER JOIN tmpRelationByPluginTbl ON 
    fes_accuml.TAG_NAME = concat(rtuTag,'.', tmpRelationByPluginTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    
    #模拟手动操作,先关联对应RTU,再关联对应点
    UPDATE accuml INNER JOIN tmpRelationByPluginTbl ON TAG_NAME = mid(KEY_ID_TAG,8,char_length(KEY_ID_TAG) - 7) SET RTU_TAG = rtuTag ;
    
    UPDATE accuml_map_to_fes INNER JOIN tmpRelationByPluginTbl 
    ON accuml_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG
    SET accuml_map_to_fes.DOT_NO = tmpRelationByPluginTbl.DOT_NO; 
    
    #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 INNER JOIN tmpRelationByPluginTbl ON 
    fes_analog.TAG_NAME = concat(rtuTag,'.', tmpRelationByPluginTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    
    #模拟手动操作,先关联对应RTU,再关联对应点
    UPDATE analog INNER JOIN tmpRelationByPluginTbl ON TAG_NAME = mid(KEY_ID_TAG,8,char_length(KEY_ID_TAG) - 7) SET RTU_TAG = rtuTag ;
    
    UPDATE analog_map_to_fes INNER JOIN tmpRelationByPluginTbl 
    ON analog_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG
    SET analog_map_to_fes.DOT_NO = tmpRelationByPluginTbl.DOT_NO; 
    
    #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 INNER JOIN tmpRelationByPluginTbl ON 
    fes_mix.TAG_NAME = concat(rtuTag,'.', tmpRelationByPluginTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    
    #模拟手动操作,先关联对应RTU,再关联对应点
    UPDATE mix INNER JOIN tmpRelationByPluginTbl ON TAG_NAME = mid(KEY_ID_TAG,5,char_length(KEY_ID_TAG) - 4) SET RTU_TAG = rtuTag ;
    
    UPDATE mix_map_to_fes INNER JOIN tmpRelationByPluginTbl 
    ON mix_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG
    SET mix_map_to_fes.DOT_NO = tmpRelationByPluginTbl.DOT_NO; 
    
    #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 INNER JOIN tmpRelationByPluginTbl ON 
    fes_digital.TAG_NAME = concat(rtuTag,'.', tmpRelationByPluginTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    
    #模拟手动操作,先关联对应RTU,再关联对应点
    UPDATE digital INNER JOIN tmpRelationByPluginTbl ON TAG_NAME = mid(KEY_ID_TAG,9,char_length(KEY_ID_TAG) - 8) SET RTU_TAG = rtuTag ;
    
    UPDATE digital_map_to_fes INNER JOIN tmpRelationByPluginTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 1
    SET digital_map_to_fes.DOT_NO1 = tmpRelationByPluginTbl.DOT_NO; 
    
    UPDATE digital_map_to_fes INNER JOIN tmpRelationByPluginTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 2
    SET digital_map_to_fes.DOT_NO2 = tmpRelationByPluginTbl.DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN tmpRelationByPluginTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 3
    SET digital_map_to_fes.DOT_NO3 = tmpRelationByPluginTbl.DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN tmpRelationByPluginTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 4
    SET digital_map_to_fes.DOT_NO4 = tmpRelationByPluginTbl.DOT_NO;
    
    UPDATE digital_map_to_fes INNER JOIN tmpRelationByPluginTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 5
    SET digital_map_to_fes.DOT_NO5 = tmpRelationByPluginTbl.DOT_NO;
    
    #对于控制点
    #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 INNER JOIN tmpRelationByPluginTbl 
    ON TAG_NAME = KEY_ID_TAG SET RTU_TAG = rtuTag ;
    
    UPDATE analog_control INNER JOIN tmpRelationByPluginTbl 
    ON analog_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG
    SET analog_control.OFFSET_NO = tmpRelationByPluginTbl.DOT_NO;
    
    #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 INNER JOIN tmpRelationByPluginTbl 
    ON TAG_NAME = KEY_ID_TAG SET RTU_TAG = rtuTag ;
    
    UPDATE mix_control INNER JOIN tmpRelationByPluginTbl 
    ON mix_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG
    SET mix_control.OFFSET_NO = tmpRelationByPluginTbl.DOT_NO;
    
    #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 INNER JOIN tmpRelationByPluginTbl 
    ON TAG_NAME = KEY_ID_TAG AND OFFSET_NO = 1 SET RTU_TAG = rtuTag ;
    
    UPDATE digital_control INNER JOIN tmpRelationByPluginTbl 
    ON digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 1
    SET digital_control.OFFSET_NO1 = tmpRelationByPluginTbl.DOT_NO;
    
    UPDATE digital_control INNER JOIN tmpRelationByPluginTbl 
    ON digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 2
    SET digital_control.OFFSET_NO2 = tmpRelationByPluginTbl.DOT_NO;
    
    UPDATE digital_control INNER JOIN tmpRelationByPluginTbl 
    ON digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 3
    SET digital_control.OFFSET_NO3 = tmpRelationByPluginTbl.DOT_NO;
    
    UPDATE digital_control INNER JOIN tmpRelationByPluginTbl 
    ON digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 4
    SET digital_control.OFFSET_NO4 = tmpRelationByPluginTbl.DOT_NO;
    
    UPDATE digital_control INNER JOIN tmpRelationByPluginTbl 
    ON digital_control.TAG_NAME = tmpRelationByPluginTbl.KEY_ID_TAG AND tmpRelationByPluginTbl.OFFSET_NO = 5
    SET digital_control.OFFSET_NO5 = tmpRelationByPluginTbl.DOT_NO;
    
END;;
DELIMITER ;

drop procedure if exists relationByTempPointName;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `relationByTempPointName`(devTempName varchar(64),fesDevTempName varchar(64)) 
body:BEGIN
    /*
     * 按照模板下的测点名称进行,后台模板与前置模板的关联
     */
    #获取设备测点名称
    
    CALL clearTempRelation(devTempName);
    #创建临时表,
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpRelationNameTbl(
        TAG_NAME varchar(64) NOT NULL,
        FES_SEQ_NO decimal(10,0) NOT NULL
    );
    
    #accuml
    DELETE FROM tmpRelationNameTbl;
    
    INSERT INTO tmpRelationNameTbl(TAG_NAME,FES_SEQ_NO)
    SELECT A.TAG_NAME,B.SEQ_NO
    FROM (SELECT TAG_NAME FROM accuml_temp_define WHERE DEV_TP_NAME = devTempName) A 
    INNER JOIN (SELECT TAG_NAME,SEQ_NO FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = fesDevTempName 
    AND POINT_TYPE = getFesPointType('FES_ACC')) B 
    ON getAfterLastDot(A.TAG_NAME) = getAfterLastDot(B.TAG_NAME);
    
    #进行关联
    UPDATE accuml_temp_define INNER JOIN tmpRelationNameTbl ON 
    accuml_temp_define.TAG_NAME = tmpRelationNameTbl.TAG_NAME 
    SET 
        SEC_DEVTP_NAME = fesDevTempName,
        SEC_SEQ_NO = tmpRelationNameTbl.FES_SEQ_NO;
    
    #analog
    DELETE FROM tmpRelationNameTbl;
    
    INSERT INTO tmpRelationNameTbl(TAG_NAME,FES_SEQ_NO)
    SELECT A.TAG_NAME,B.SEQ_NO
    FROM (SELECT TAG_NAME FROM analog_temp_define WHERE DEV_TP_NAME = devTempName) A 
    INNER JOIN (SELECT TAG_NAME,SEQ_NO FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = fesDevTempName 
    AND POINT_TYPE = getFesPointType('FES_ANA')) B 
    ON getAfterLastDot(A.TAG_NAME) = getAfterLastDot(B.TAG_NAME);
    
    #进行关联
    UPDATE analog_temp_define INNER JOIN tmpRelationNameTbl ON 
    analog_temp_define.TAG_NAME = tmpRelationNameTbl.TAG_NAME 
    SET 
        SEC_DEVTP_NAME = fesDevTempName,
        SEC_SEQ_NO = tmpRelationNameTbl.FES_SEQ_NO;
    
    #digital只判断第一个分量
    DELETE FROM tmpRelationNameTbl;
    
    INSERT INTO tmpRelationNameTbl(TAG_NAME,FES_SEQ_NO)
    SELECT A.TAG_NAME,B.SEQ_NO
    FROM (SELECT TAG_NAME FROM digital_temp_define WHERE DEV_TP_NAME = devTempName) A 
    INNER JOIN (SELECT TAG_NAME,SEQ_NO FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = fesDevTempName 
    AND POINT_TYPE = getFesPointType('FES_DIG')) B 
    ON getAfterLastDot(A.TAG_NAME) = getAfterLastDot(B.TAG_NAME);
    
    #进行关联
    UPDATE digital_temp_define INNER JOIN tmpRelationNameTbl ON 
    digital_temp_define.TAG_NAME = tmpRelationNameTbl.TAG_NAME 
    SET 
        SEC_DEVTP_NAME = fesDevTempName,
        SEC_SEQ_NO1 = tmpRelationNameTbl.FES_SEQ_NO;
        
    #mix
    DELETE FROM tmpRelationNameTbl;
    
    INSERT INTO tmpRelationNameTbl(TAG_NAME,FES_SEQ_NO)
    SELECT A.TAG_NAME,B.SEQ_NO
    FROM (SELECT TAG_NAME FROM mix_temp_define WHERE DEV_TP_NAME = devTempName) A 
    INNER JOIN (SELECT TAG_NAME,SEQ_NO FROM fes_dev_pt_temp_def WHERE DEV_TP_NAME = fesDevTempName 
    AND POINT_TYPE = getFesPointType('FES_MIX')) B 
    ON getAfterLastDot(A.TAG_NAME) = getAfterLastDot(B.TAG_NAME);
    
    #进行关联
    UPDATE mix_temp_define INNER JOIN tmpRelationNameTbl ON 
    mix_temp_define.TAG_NAME = tmpRelationNameTbl.TAG_NAME 
    SET 
        SEC_DEVTP_NAME = fesDevTempName,
        SEC_SEQ_NO = tmpRelationNameTbl.FES_SEQ_NO;
    
END;;
DELIMITER ;

drop procedure if exists relationFesByName;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `relationFesByName`(devTag varchar(64),fesDevTag varchar(64)) 
body:BEGIN
    /*
     * 后台和FES的两个设备之间名称相同的测点做关联
     */
    DECLARE rtuTag varchar(64);
    
    IF COALESCE(devTag,'') = '' OR COALESCE(fesDevTag,'')=''
    THEN LEAVE body;
    END IF;
    
    SELECT RTU_TAG INTO rtuTag FROM fes_dev_info WHERE TAG_NAME = fesDevTag;
    
    IF COALESCE(rtuTag,'') = ''
    THEN LEAVE body;
    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 INNER JOIN  tmpRelationFesByNameTbl
    ON accuml_map_to_fes.KEY_ID_TAG = tmpRelationFesByNameTbl.KEY_ID_TAG
    SET accuml_map_to_fes.RTU_TAG = rtuTag,accuml_map_to_fes.DOT_NO = tmpRelationFesByNameTbl.DOT_NO;
    
    #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 INNER JOIN  tmpRelationFesByNameTbl
    ON analog_map_to_fes.KEY_ID_TAG = tmpRelationFesByNameTbl.KEY_ID_TAG
    SET analog_map_to_fes.RTU_TAG = rtuTag,analog_map_to_fes.DOT_NO = tmpRelationFesByNameTbl.DOT_NO;
    
    #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 INNER JOIN  tmpRelationFesByNameTbl
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationFesByNameTbl.KEY_ID_TAG
    SET digital_map_to_fes.RTU_TAG = rtuTag,digital_map_to_fes.DOT_NO1 = tmpRelationFesByNameTbl.DOT_NO;
    
    #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 INNER JOIN  tmpRelationFesByNameTbl
    ON mix_map_to_fes.KEY_ID_TAG = tmpRelationFesByNameTbl.KEY_ID_TAG
    SET mix_map_to_fes.RTU_TAG = rtuTag,mix_map_to_fes.DOT_NO = tmpRelationFesByNameTbl.DOT_NO;
    
    
END;;
DELIMITER ;

drop procedure if exists relationToFes;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `relationToFes`(devTagName varchar(64), fesDevTagName varchar(64)) 
body:BEGIN
    /*
     * 后台和FES的两个设备之间做关联,使用前,先检查后台设备的模板和前置的模板是否有所关联,已经关联的测点不会再关联,若想关联,先清除
     */
    DECLARE rtuTagName varchar(64);
    DECLARE fesDevTempTagName varchar(64);
    
    IF COALESCE(devTagName,'') = '' OR COALESCE(fesDevTagName,'')=''
    THEN LEAVE body;
    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 LEAVE body;
    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 INNER JOIN tmpRelationToFesTbl ON 
    fes_accuml.TAG_NAME = concat(rtuTagName,'.', tmpRelationToFesTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    #清除后台的
    UPDATE accuml INNER JOIN tmpRelationToFesTbl ON TAG_NAME = getAfterFirstDot(tmpRelationToFesTbl.KEY_ID_TAG) SET RTU_TAG = '';
    
    #模拟手动操作,先关联对应RTU,再关联对应点
    UPDATE accuml INNER JOIN tmpRelationToFesTbl ON TAG_NAME = getAfterFirstDot(tmpRelationToFesTbl.KEY_ID_TAG) SET RTU_TAG = rtuTagName ;
    
    UPDATE accuml_map_to_fes INNER JOIN tmpRelationToFesTbl 
    ON accuml_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG
    SET accuml_map_to_fes.DOT_NO = tmpRelationToFesTbl.DOT_NO; 
    
    #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 INNER JOIN tmpRelationToFesTbl ON 
    fes_analog.TAG_NAME = concat(rtuTagName,'.', tmpRelationToFesTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    #清除后台的
    UPDATE analog INNER JOIN tmpRelationToFesTbl ON TAG_NAME = getAfterFirstDot(tmpRelationToFesTbl.KEY_ID_TAG) SET RTU_TAG = '';
    
    UPDATE analog INNER JOIN tmpRelationToFesTbl ON TAG_NAME = getAfterFirstDot(tmpRelationToFesTbl.KEY_ID_TAG) SET RTU_TAG = rtuTagName;
    
    UPDATE analog_map_to_fes INNER JOIN tmpRelationToFesTbl 
    ON analog_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG
    SET analog_map_to_fes.DOT_NO = tmpRelationToFesTbl.DOT_NO; 
    
    #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 INNER JOIN tmpRelationToFesTbl ON 
    fes_digital.TAG_NAME = concat(rtuTagName,'.', tmpRelationToFesTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    #清除后台的
    UPDATE digital INNER JOIN tmpRelationToFesTbl ON TAG_NAME = getAfterFirstDot(tmpRelationToFesTbl.KEY_ID_TAG)  SET RTU_TAG = '' ;
    
    UPDATE digital INNER JOIN tmpRelationToFesTbl ON TAG_NAME = getAfterFirstDot(tmpRelationToFesTbl.KEY_ID_TAG)  SET RTU_TAG = rtuTagName;
    
    UPDATE digital_map_to_fes INNER JOIN tmpRelationToFesTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 1
    SET digital_map_to_fes.DOT_NO1 = tmpRelationToFesTbl.DOT_NO; 
    
    UPDATE digital_map_to_fes INNER JOIN tmpRelationToFesTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 2
    SET digital_map_to_fes.DOT_NO2 = tmpRelationToFesTbl.DOT_NO; 
   
    UPDATE digital_map_to_fes INNER JOIN tmpRelationToFesTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 3
    SET digital_map_to_fes.DOT_NO3 = tmpRelationToFesTbl.DOT_NO; 
   
    UPDATE digital_map_to_fes INNER JOIN tmpRelationToFesTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 4
    SET digital_map_to_fes.DOT_NO4 = tmpRelationToFesTbl.DOT_NO; 
    
    UPDATE digital_map_to_fes INNER JOIN tmpRelationToFesTbl 
    ON digital_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 5
    SET digital_map_to_fes.DOT_NO5 = tmpRelationToFesTbl.DOT_NO; 
    
    
    #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 INNER JOIN tmpRelationToFesTbl ON 
    fes_mix.TAG_NAME = concat(rtuTagName,'.', tmpRelationToFesTbl.DOT_NO)
    SET APP_TABLE_NAME='',APP_TAG_NAME='',APP_COLUMN_NAME='';
    #清除后台的
    UPDATE mix INNER JOIN tmpRelationToFesTbl ON TAG_NAME = getAfterFirstDot(tmpRelationToFesTbl.KEY_ID_TAG) SET RTU_TAG = '' ;
    
    UPDATE mix INNER JOIN tmpRelationToFesTbl ON TAG_NAME = getAfterFirstDot(tmpRelationToFesTbl.KEY_ID_TAG) SET RTU_TAG = rtuTagName;
    
    UPDATE mix_map_to_fes INNER JOIN tmpRelationToFesTbl 
    ON mix_map_to_fes.KEY_ID_TAG = tmpRelationToFesTbl.KEY_ID_TAG
    SET mix_map_to_fes.DOT_NO = tmpRelationToFesTbl.DOT_NO; 
    
    
    #最后只剩下对应控制点,没有关联
    #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 INNER JOIN tmpRelationToFesTbl 
    ON analog_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG
    SET analog_control.RTU_TAG = rtuTagName,
    analog_control.OFFSET_NO = tmpRelationToFesTbl.DOT_NO;
       
    #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 INNER JOIN tmpRelationToFesTbl 
    ON digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 1
    SET digital_control.RTU_TAG = rtuTagName, 
    digital_control.OFFSET_NO1 = tmpRelationToFesTbl.DOT_NO;
    
    UPDATE digital_control INNER JOIN tmpRelationToFesTbl 
    ON digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 2
    SET digital_control.RTU_TAG = rtuTagName,
    digital_control.OFFSET_NO2 = tmpRelationToFesTbl.DOT_NO;
    
    UPDATE digital_control INNER JOIN tmpRelationToFesTbl 
    ON digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 3
    SET digital_control.RTU_TAG = rtuTagName, 
    digital_control.OFFSET_NO3 = tmpRelationToFesTbl.DOT_NO;
    
    UPDATE digital_control INNER JOIN tmpRelationToFesTbl 
    ON digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 4
    SET digital_control.RTU_TAG = rtuTagName,
    digital_control.OFFSET_NO4 = tmpRelationToFesTbl.DOT_NO;
    
    UPDATE digital_control INNER JOIN tmpRelationToFesTbl 
    ON digital_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG AND tmpRelationToFesTbl.OFFSET_NO = 5
    SET digital_control.RTU_TAG = rtuTagName,
    digital_control.OFFSET_NO5 = tmpRelationToFesTbl.DOT_NO;
    
    
    #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 INNER JOIN tmpRelationToFesTbl 
    ON mix_control.TAG_NAME = tmpRelationToFesTbl.KEY_ID_TAG
    SET mix_control.RTU_TAG = rtuTagName,
    mix_control.OFFSET_NO = tmpRelationToFesTbl.DOT_NO;
    
    
    
END;;
DELIMITER ;

drop procedure if exists sortResPara1;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `sortResPara1`(rtuTag varchar(64), startNum decimal(10,0)) 
body:BEGIN
    /*
     * 依据设备ID重排规约参数1
     * 
     */
    
    
    #acc
    set @tmpResParaVar = startNum;
    UPDATE (
    SELECT fes_accuml.TAG_NAME,fes_accuml.DESCRIPTION,fes_dev_info.DEV_DESC, fes_accuml.RES_PARA_INT1
    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,'.%')
    ORDER BY fes_dev_info.DEV_ID,fes_accuml.SEQ_NO) temp
    LEFT JOIN fes_accuml ON temp.TAG_NAME = fes_accuml.TAG_NAME
    set fes_accuml.RES_PARA_INT1 = (@tmpResParaVar := @tmpResParaVar + 1);
    
    #ana
    set @tmpResParaVar = startNum;
    
    UPDATE (
    SELECT fes_analog.TAG_NAME,fes_analog.DESCRIPTION,fes_dev_info.DEV_DESC, fes_analog.RES_PARA_INT1
    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,'.%')
    ORDER BY fes_dev_info.DEV_ID,fes_analog.SEQ_NO) temp
    LEFT JOIN fes_analog ON temp.TAG_NAME = fes_analog.TAG_NAME
    set fes_analog.RES_PARA_INT1 = (@tmpResParaVar := @tmpResParaVar + 1);
    
    #dig
    set @tmpResParaVar = startNum;
    
    UPDATE (
    SELECT fes_digital.TAG_NAME,fes_digital.DESCRIPTION,fes_dev_info.DEV_DESC, fes_digital.RES_PARA_INT1
    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,'.%')
    ORDER BY fes_dev_info.DEV_ID,fes_digital.SEQ_NO) temp
    LEFT JOIN fes_digital ON temp.TAG_NAME = fes_digital.TAG_NAME
    set fes_digital.RES_PARA_INT1 = (@tmpResParaVar := @tmpResParaVar + 1);
    
    #mix
    set @tmpResParaVar = startNum;
    
    UPDATE (
    SELECT fes_mix.TAG_NAME,fes_mix.DESCRIPTION,fes_dev_info.DEV_DESC, fes_mix.RES_PARA_INT1
    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,'.%')
    ORDER BY fes_dev_info.DEV_ID,fes_mix.SEQ_NO) temp
    LEFT JOIN fes_mix ON temp.TAG_NAME = fes_mix.TAG_NAME
    set fes_mix.RES_PARA_INT1 = (@tmpResParaVar := @tmpResParaVar + 1);
    
    #anaCtrl
    set @tmpResParaVar = startNum;
    
    UPDATE (
    SELECT fes_analog_ctrl.TAG_NAME,fes_analog_ctrl.DESCRIPTION,fes_dev_info.DEV_DESC, fes_analog_ctrl.RES_PARA_INT1
    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,'.%')
    ORDER BY fes_dev_info.DEV_ID,fes_analog_ctrl.SEQ_NO) temp
    LEFT JOIN fes_analog_ctrl ON temp.TAG_NAME = fes_analog_ctrl.TAG_NAME
    set fes_analog_ctrl.RES_PARA_INT1 = (@tmpResParaVar := @tmpResParaVar + 1);
    
    #digCtrl
    set @tmpResParaVar = startNum;
    
    UPDATE (
    SELECT fes_digital_ctrl.TAG_NAME,fes_digital_ctrl.DESCRIPTION,fes_dev_info.DEV_DESC, fes_digital_ctrl.RES_PARA_INT1
    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,'.%')
    ORDER BY fes_dev_info.DEV_ID,fes_digital_ctrl.SEQ_NO) temp
    LEFT JOIN fes_digital_ctrl ON temp.TAG_NAME = fes_digital_ctrl.TAG_NAME
    set fes_digital_ctrl.RES_PARA_INT1 = (@tmpResParaVar := @tmpResParaVar + 1);
    #mixCtrl
    set @tmpResParaVar = startNum;
    
    UPDATE (
    SELECT fes_mix_ctrl.TAG_NAME,fes_mix_ctrl.DESCRIPTION,fes_dev_info.DEV_DESC, fes_mix_ctrl.RES_PARA_INT1
    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,'.%')
    ORDER BY fes_dev_info.DEV_ID,fes_mix_ctrl.SEQ_NO) temp
    LEFT JOIN fes_mix_ctrl ON temp.TAG_NAME = fes_mix_ctrl.TAG_NAME
    set fes_mix_ctrl.RES_PARA_INT1 = (@tmpResParaVar := @tmpResParaVar + 1);
END;;
DELIMITER ;

drop procedure if exists updateDescByFes;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `updateDescByFes`(devTagName varchar(64)) 
body: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 INNER JOIN tmpUpdateDescByFesTbl
    ON accuml.TAG_NAME = tmpUpdateDescByFesTbl.TAG_NAME
    SET accuml.DESCRIPTION = tmpUpdateDescByFesTbl.DESCRIPTION;
    #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 INNER JOIN tmpUpdateDescByFesTbl
    ON analog.TAG_NAME = tmpUpdateDescByFesTbl.TAG_NAME
    SET analog.DESCRIPTION = tmpUpdateDescByFesTbl.DESCRIPTION;
    #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 INNER JOIN tmpUpdateDescByFesTbl
    ON digital.TAG_NAME = tmpUpdateDescByFesTbl.TAG_NAME
    SET digital.DESCRIPTION = tmpUpdateDescByFesTbl.DESCRIPTION;
    #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 INNER JOIN tmpUpdateDescByFesTbl
    ON mix.TAG_NAME = tmpUpdateDescByFesTbl.TAG_NAME
    SET mix.DESCRIPTION = tmpUpdateDescByFesTbl.DESCRIPTION;
    
    
END;;
DELIMITER ;

drop procedure if exists updateFesResPara;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `updateFesResPara`(tableName varchar(64),startNum decimal(10,0)) 
body:BEGIN
    /*
     * 批量修改设备的规约参数1,要批量修改的设备在export_seq中
     * tableName 表名
     * startNum 修改的起始数字
     */
    
    SET @fesResPara := startNum - 1;
    
    #创建临时表
    CREATE TEMPORARY TABLE IF NOT EXISTS tmpUpdateFesResParaTbl(
        TAG_NAME varchar(64) NOT NULL,
        RES_PARA decimal(10,0) NOT NULL
    );
    DELETE FROM tmpUpdateFesResParaTbl;
    
    IF lower(tableName) = 'fes_accuml' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_accuml,export_seq WHERE fes_accuml.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.`NO`,fes_accuml.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (@fesResPara := @fesResPara + 1);
        
        UPDATE fes_accuml INNER JOIN tmpUpdateFesResParaTbl ON fes_accuml.TAG_NAME = tmpUpdateFesResParaTbl.TAG_NAME
        SET fes_accuml.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA;
    END;
    ELSEIF lower(tableName) = 'fes_analog' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_analog,export_seq WHERE fes_analog.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.`NO`,fes_analog.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (@fesResPara := @fesResPara + 1);
        
        UPDATE fes_analog INNER JOIN tmpUpdateFesResParaTbl ON fes_analog.TAG_NAME = tmpUpdateFesResParaTbl.TAG_NAME
        SET fes_analog.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA;
    END;
    ELSEIF lower(tableName) = 'fes_analog_ctrl' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_analog_ctrl,export_seq WHERE fes_analog_ctrl.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.`NO`,fes_analog_ctrl.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (@fesResPara := @fesResPara + 1);
        
        UPDATE fes_analog_ctrl INNER JOIN tmpUpdateFesResParaTbl ON fes_analog_ctrl.TAG_NAME = tmpUpdateFesResParaTbl.TAG_NAME
        SET fes_analog_ctrl.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA;
    END;
    ELSEIF lower(tableName) = 'fes_digital' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_digital,export_seq WHERE fes_digital.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.`NO`,fes_digital.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (@fesResPara := @fesResPara + 1);
        
        UPDATE fes_digital INNER JOIN tmpUpdateFesResParaTbl ON fes_digital.TAG_NAME = tmpUpdateFesResParaTbl.TAG_NAME
        SET fes_digital.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA;
    END;
    ELSEIF lower(tableName) = 'fes_digital_ctrl' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_digital_ctrl,export_seq WHERE fes_digital_ctrl.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.`NO`,fes_digital_ctrl.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (@fesResPara := @fesResPara + 1);
        
        UPDATE fes_digital_ctrl INNER JOIN tmpUpdateFesResParaTbl ON fes_digital_ctrl.TAG_NAME = tmpUpdateFesResParaTbl.TAG_NAME
        SET fes_digital_ctrl.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA;
    END;
    ELSEIF lower(tableName) = 'fes_mix' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_mix,export_seq WHERE fes_mix.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.`NO`,fes_mix.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (@fesResPara := @fesResPara + 1);
        
        UPDATE fes_mix INNER JOIN tmpUpdateFesResParaTbl ON fes_mix.TAG_NAME = tmpUpdateFesResParaTbl.TAG_NAME
        SET fes_mix.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA;
    END;
    ELSEIF lower(tableName) = 'fes_mix_ctrl' THEN
    BEGIN     
        INSERT INTO tmpUpdateFesResParaTbl(TAG_NAME,RES_PARA)
        SELECT TAG_NAME,1
        FROM fes_mix_ctrl,export_seq WHERE fes_mix_ctrl.DEV_TAG = export_seq.DEV_TAG 
        ORDER BY export_seq.`NO`,fes_mix_ctrl.SEQ_NO;
        
        UPDATE tmpUpdateFesResParaTbl SET RES_PARA = (@fesResPara := @fesResPara + 1);
        
        UPDATE fes_mix_ctrl INNER JOIN tmpUpdateFesResParaTbl ON fes_mix_ctrl.TAG_NAME = tmpUpdateFesResParaTbl.TAG_NAME
        SET fes_mix_ctrl.RES_PARA_INT1 = tmpUpdateFesResParaTbl.RES_PARA;
    END;
    END IF; 
    
    
    
END;;
DELIMITER ;

drop procedure if exists updateForwardBase;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `updateForwardBase`(tableName varchar(64), rtuTagName varchar(64), bs decimal(22,6)) 
body: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;;
DELIMITER ;

drop procedure if exists updateForwardCoeff;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `updateForwardCoeff`( tableName varchar(64), rtuTagName varchar(64), coe decimal(22,6)) 
body: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;;
DELIMITER ;

drop procedure if exists updateForwardId;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `updateForwardId`(tableName varchar(64), rtuTagName varchar(64), remoteStartNo decimal(10,0)) 
body:BEGIN
    #修改前置编号
    #tableName 表名
    #rtuTagName 指定RTU
    #remoteStartNo 修改的起始数字
    SET @forwardId := remoteStartNo - 1;
    IF lower(tableName) = 'fes_forwarding_accuml' THEN
        UPDATE fes_forwarding_accuml SET ID = (@forwardId := @forwardId + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    ELSEIF lower(tableName) = 'fes_forwarding_anaolg' THEN 
        UPDATE fes_forwarding_anaolg SET ID = (@forwardId := @forwardId + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    ELSEIF lower(tableName) = 'fes_forwarding_anaolg_ctrl' THEN 
        UPDATE fes_forwarding_anaolg_ctrl SET ID = (@forwardId := @forwardId + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    ELSEIF lower(tableName) = 'fes_forwarding_digital' THEN 
        UPDATE fes_forwarding_digital SET ID = (@forwardId := @forwardId + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    ELSEIF lower(tableName) = 'fes_forwarding_digital_ctrl' THEN 
        UPDATE fes_forwarding_digital_ctrl SET ID = (@forwardId := @forwardId + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    END IF; 
END;;
DELIMITER ;

drop procedure if exists updateForwardRemote;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `updateForwardRemote`( tableName varchar(64), rtuTagName varchar(64), remoteStartNo decimal(10,0)) 
body:BEGIN
    #修改转发表的远动号
    
    #因为后面会自动加1,这里先减1
    SET @forwardRemoteNum := remoteStartNo - 1;
    IF lower(tableName) = 'fes_forwarding_accuml' THEN
        UPDATE fes_forwarding_accuml SET REMOTE_NO = (@forwardRemoteNum := @forwardRemoteNum + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    ELSEIF lower(tableName) = 'fes_forwarding_anaolg' THEN 
        UPDATE fes_forwarding_anaolg SET REMOTE_NO = (@forwardRemoteNum := @forwardRemoteNum + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    ELSEIF lower(tableName) = 'fes_forwarding_anaolg_ctrl' THEN 
        UPDATE fes_forwarding_anaolg_ctrl SET REMOTE_NO = (@forwardRemoteNum := @forwardRemoteNum + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    ELSEIF lower(tableName) = 'fes_forwarding_digital' THEN 
        UPDATE fes_forwarding_digital SET REMOTE_NO = (@forwardRemoteNum := @forwardRemoteNum + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    ELSEIF lower(tableName) = 'fes_forwarding_digital_ctrl' THEN 
        UPDATE fes_forwarding_digital_ctrl SET REMOTE_NO = (@forwardRemoteNum := @forwardRemoteNum + 1) WHERE RTU_TAG = rtuTagName ORDER BY ID;
    END IF;
        
    
END;;
DELIMITER ;

drop procedure if exists updateUsergRegion;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `updateUsergRegion`(roleId decimal(10,0)) 
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 INNER JOIN tmpUURegion
    ON 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
    SET 
    rm_userg_rsp_info.PERM_LOCATION1 = tmpUURegion.PERM_LOCATION1,
    rm_userg_rsp_info.PERM_LOCATION2 = tmpUURegion.PERM_LOCATION2,
    rm_userg_rsp_info.PERM_LOCATION3 = tmpUURegion.PERM_LOCATION3,
    rm_userg_rsp_info.PERM_LOCATION4 = tmpUURegion.PERM_LOCATION4,
    rm_userg_rsp_info.PERM_LOCATION5 = tmpUURegion.PERM_LOCATION5,
    rm_userg_rsp_info.PERM_LOCATION6 = tmpUURegion.PERM_LOCATION6,
    rm_userg_rsp_info.PERM_LOCATION7 = tmpUURegion.PERM_LOCATION7,
    rm_userg_rsp_info.PERM_LOCATION8 = tmpUURegion.PERM_LOCATION8;
    
  
END;;
DELIMITER ;

drop procedure if exists updateUsergRsp;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `updateUsergRsp`(userGId decimal(10,0)) 
BEGIN
    #更新rm_userg_rsp_info表
    UPDATE rm_userg_rsp_info INNER JOIN rm_role_spef_reg
    ON 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
    SET 
    rm_userg_rsp_info.PERM_REGION1 = rm_role_spef_reg.PERM_REGION1,
    rm_userg_rsp_info.PERM_REGION2 = rm_role_spef_reg.PERM_REGION2,
    rm_userg_rsp_info.PERM_REGION3 = rm_role_spef_reg.PERM_REGION3,
    rm_userg_rsp_info.PERM_REGION4 = rm_role_spef_reg.PERM_REGION4;
    
       
END;;
DELIMITER ;

drop procedure if exists userGRoleModify;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `userGRoleModify`(userGroupId decimal(10,0), roleIdList varchar(100)) 
BEGIN
    #针对用户组所属角色有所更改的情况
    #userGroupId 用户组ID,不可为空
    #roleIdList 该用户组所属的角色的ID号列表,以逗号分隔
    
    #删除不在指定roleIdList表中的角色
    
    DELETE FROM rm_userg_rsp_info WHERE PERM_USER_GROUP = userGroupId AND find_in_set(PERM_ROLE,roleIdList) = 0;
    
    
        
    
END;;
DELIMITER ;

drop function if exists findPsc3000Tag;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `findPsc3000Tag`(pointDesc varchar(128),pointRemoteNo decimal(10,0), pointDevId decimal(10,0),tableName varchar(64)) RETURNS varchar(64) CHARSET utf8 
BEGIN 
    #在对应的PSC3000的测点表中查找对应测点的TAG
    
    DECLARE tempVar varchar(64) DEFAULT '';
    
    IF tableName='psc_acc' THEN  
    SET tempVar =(SELECT TAG_NAME FROM psc3000_acc WHERE DESCRIPTION = pointDesc AND pointRemoteNo = REMOTE_NO AND pointDevId = DEV_ID);
    ELSEIF tableName='psc_ana' THEN
    SET tempVar =(SELECT TAG_NAME FROM psc3000_ana WHERE DESCRIPTION = pointDesc AND pointRemoteNo = REMOTE_NO AND pointDevId = DEV_ID);
    ELSEIF tableName='psc_dig' THEN
    SET 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 SET tempVar = '';
    END IF;
    #自增
    RETURN tempVar;
    
END;;
DELIMITER ;

drop function if exists getAfterFirstDot;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getAfterFirstDot`( ds varchar(128)) RETURNS varchar(128) CHARSET utf8 
BEGIN
    /*
     * 返回第一个.号之后的字符串
     */
    DECLARE tempVar varchar(128);
    DECLARE countStr int;
    DECLARE countFirstStr int;
    SET tempVar = ds;
    
    IF locate('.',ds) = 0 THEN RETURN tempVar;
    ELSE
    BEGIN
        SET tempVar = substring_index( ds, '.' , 1);
        SET countStr = char_length(ds);
        SET countFirstStr = char_length(tempVar);
        SET countFirstStr = countFirstStr + 1;
        SET tempVar = right(ds,countStr - countFirstStr);
        RETURN tempVar;
    END;   
    END IF;
END;;
DELIMITER ;

drop function if exists getAfterLastDot;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getAfterLastDot`( ds varchar(128)) RETURNS varchar(128) CHARSET utf8 
BEGIN
    
 /*
 * 返回最后一个.号之后的数据
 */
    DECLARE tempVar varchar(128);
    SET tempVar = '';
    
    IF locate('.',ds) = 0 THEN RETURN tempVar;
    ELSE RETURN substring_index( ds, '.' , -1);
    END IF;
END;;
DELIMITER ;

drop function if exists getBeforeFirstDot;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getBeforeFirstDot`( ds varchar(128)) RETURNS varchar(128) CHARSET utf8 
BEGIN
    /*
     * 返回第一个.号之前的字符串
     */
    
    IF locate('.',ds) = 0 THEN RETURN '';
    ELSE
    BEGIN
        RETURN substring_index( ds, '.' , 1);
    END;   
    END IF;
END;;
DELIMITER ;

drop function if exists getBeforeLastDot;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getBeforeLastDot`( ds varchar(128)) RETURNS varchar(128) CHARSET utf8 
BEGIN
    /*
     * 返回最后一个.之前的字符串
     */
    DECLARE tempVar varchar(128);
    DECLARE countStr int;
    DECLARE countLastStr int;
    SET tempVar = ds;
    
    IF locate('.',ds) = 0 THEN RETURN tempVar;
    ELSE
    BEGIN
        SET tempVar = substring_index( ds, '.' , -1);
        SET countStr = char_length(ds);
        SET countLastStr = char_length(tempVar);
        SET countLastStr = countLastStr + 1;
        SET tempVar = left(ds,countStr - countLastStr);
        RETURN tempVar;
    END;   
    END IF;
END;;
DELIMITER ;

drop function if exists getFesPointType;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getFesPointType`( flag varchar(64)) RETURNS decimal(10,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
	 * FES_CONST:7
     */
	 
    RETURN 
	CASE flag 
	WHEN 'FES_ACC' THEN 0 
    WHEN 'FES_ANA' THEN 1
    WHEN 'FES_DIG' THEN 2
    WHEN 'FES_MIX' THEN 3
    WHEN 'FES_ANA_CTRL' THEN 4
    WHEN 'FES_DIG_CTRL' THEN 5
    WHEN 'FES_MIX_CTRL' THEN 6
    WHEN 'FES_CONST' THEN 7
    ELSE  0
    END;
	
END;;
DELIMITER ;

drop function if exists getMaxDefaultDevSeqNo;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getMaxDefaultDevSeqNo`( defaultDevTagName varchar(64), dotType decimal(10,0)) RETURNS decimal(10,0) 
BEGIN
    /*
     * 返回指定defaultDevTagName的最大SeqNo
     */
    DECLARE tempVar decimal(10,0) DEFAULT 0;
    
    IF dotType=getFesPointType('FES_ACC') THEN  SET tempVar =(SELECT max(SEQ_NO) FROM fes_accuml WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_ANA') THEN  SET tempVar =(SELECT max(SEQ_NO) FROM fes_analog WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_DIG') THEN  SET tempVar =(SELECT max(SEQ_NO) FROM fes_digital WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_MIX') THEN  SET tempVar =(SELECT max(SEQ_NO) FROM fes_mix WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_ANA_CTRL') THEN  SET tempVar =(SELECT max(SEQ_NO) FROM fes_analog_ctrl WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_DIG_CTRL') THEN  SET tempVar =(SELECT max(SEQ_NO) FROM fes_digital_ctrl WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    IF dotType=getFesPointType('FES_MIX_CTRL') THEN  SET tempVar =(SELECT max(SEQ_NO) FROM fes_mix_ctrl WHERE DEV_TAG=defaultDevTagName);
    END IF;
    
    
    IF tempVar IS NULL THEN SET tempVar = 0;
    END IF;
    
    RETURN tempVar + 1;
END;;
DELIMITER ;

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

drop function if exists getMaxSeq;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getMaxSeq`(devTag varchar(64), tableName varchar(64)) RETURNS decimal(10,0) 
BEGIN 
    #获取指定设备的最大索引号
    DECLARE temp decimal(10) DEFAULT 0;
    
    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;;
DELIMITER ;

drop function if exists getMaxTriggerId;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getMaxTriggerId`() RETURNS decimal(10,0) 
BEGIN 
    #分割字符串,返回第strCount个子串
    DECLARE maxId decimal(10,0) DEFAULT 0;
    
    SELECT (max(ACT_ID)) INTO maxId FROM sys_trigger_info;
    
    IF COALESCE(maxId,0) = 0 THEN
    SET maxId = 0;
    END IF;
    
    SET maxId = maxId + 1;
    RETURN maxId;
    
END;;
DELIMITER ;

drop function if exists getOpType;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getOpType`(opName varchar(32)) RETURNS decimal(10,0) 
BEGIN 
    #获取操作类型
    DECLARE flag decimal(10,0) DEFAULT -1;
    
    IF opName = 'insert' THEN
    SET flag = 0;
    ELSEIF opName = 'delete' THEN
    SET flag = 1;
    ELSEIF opName = 'update' THEN
    SET flag = 2;
    ELSE
    SET flag = -1;
    END IF;
    
    RETURN flag;
END;;
DELIMITER ;

drop function if exists getTableSubSystem;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getTableSubSystem`( tableName varchar(100)) RETURNS decimal(10,0) 
BEGIN 
    
    DECLARE subSystemFlag decimal(10,0) DEFAULT 0; 
    
    RETURN subSystemFlag;
END;;
DELIMITER ;

drop function if exists getTriggerFlag;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `getTriggerFlag`() RETURNS tinyint(1) 
BEGIN 
    #分割字符串,返回第strCount个子串
    DECLARE flag boolean DEFAULT FALSE;
    
    SELECT FLAG_ID INTO flag FROM sys_trigger_flag WHERE FLAG_ID = 1;
    
    IF COALESCE(flag,FALSE) = FALSE THEN
    SET flag = FALSE;
    END IF;
    
    RETURN flag;
    
END;;
DELIMITER ;

drop function if exists myTriggerIsDisable;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `myTriggerIsDisable`() RETURNS int(11) 
BEGIN 
    #触发器开关状态,返回1时
     IF COALESCE(@disableMyTrrigger,0) = 0 THEN
        RETURN 0;
     ELSE
        RETURN 1;
     END IF;
END;;
DELIMITER ;

drop function if exists resetBit;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `resetBit`( num int ,pos int) RETURNS int(11) 
BEGIN 
    #设置num的pos位为0,pos从1开始数
    #要求输入和输出都是Int范围的数
    DECLARE strNum varchar(64);
    
    SET strNum = bin(num);
    SET strNum = lpad(strNum,64,'0');
    SET strNum = concat(left(strNum,64-pos),'0',right(strNum,pos-1));
    SET strNum = right(strNum,32);
    #周工那边是用Int,而不是uint,所以这里控制范围
    SET strNum = lpad(strNum,64,left(strNum,1));
    
    RETURN CAST(CONV(CAST(strNum AS binary),2,10) AS signed);
END;;
DELIMITER ;

drop function if exists setBit;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `setBit`( num int ,pos int) RETURNS int(11) 
BEGIN 
    #设置num的pos位为1,pos从1开始数
    #要求输入和输出都是Int范围的数
    DECLARE strNum varchar(64);
    SET strNum = bin(num);
    SET strNum = lpad(strNum,64,'0');
    SET strNum = concat(left(strNum,64-pos),'1',right(strNum,pos-1));
    SET strNum = right(strNum,32);
    SET strNum = lpad(strNum,64,left(strNum,1));
    RETURN CAST(CONV(CAST(strNum AS binary),2,10) AS signed);
END;;
DELIMITER ;

drop function if exists splitList;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `splitList`( strList varchar(100), split_char varchar(1) , strCount int) RETURNS varchar(64) CHARSET utf8 
BEGIN 
    #分割字符串,返回第strCount个子串
    DECLARE splitCount int;
    DECLARE splitIndex int DEFAULT 1;
    DECLARE str1 varchar(100) DEFAULT '';
    DECLARE str2 varchar(100) DEFAULT '';
    
    SET splitCount = char_length(strList)-char_length(replace(strList,split_char,'')) + 1;
    IF strCount > splitCount OR strCount <=0 THEN RETURN '';
    END IF;
    
    IF strCount = 1 THEN RETURN substring_index(strList,split_char,strCount);
    ELSE
    BEGIN
        SET str1 = substring_index(strList,split_char,strCount-1);
        SET str2 = substring_index(strList,split_char,strCount);
        RETURN right(str2,LENGTH(str2) - LENGTH(str1) - 1);
    END;
    END IF;
    
END;;
DELIMITER ;

drop function if exists testNumber;
DELIMITER ;;
CREATE DEFINER=`root`@`%` FUNCTION `testNumber`( num decimal(10,0), pos int) RETURNS tinyint(1) 
BEGIN 
    #返回num的第pos位是否为1,pos从1开始数
    DECLARE tempStr varchar(64) DEFAULT '';
    
    SET tempStr = bin(num);
    
    IF pos > length(tempStr) THEN RETURN FALSE;
    END IF;
    
    IF pos < 1 THEN RETURN FALSE;
    END IF;
    
    IF mid(tempStr,length(tempStr) - pos + 1,1)='1' THEN RETURN TRUE;
    ELSE RETURN FALSE;
    END IF;
    
    
END;;
DELIMITER ;


