<?xml version="1.0" encoding="utf-8"?>

<databaseChangeLog     
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog 
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
    <changeSet author="suhg" id="1561454393001-1"  runAlways="true">
        <comment>update f_fmea_equipment_point 更新触发器创建</comment>
        <sql endDelimiter="#">
           DROP TRIGGER IF EXISTS `trigger_fmea_equipment_point_update`;
		   CREATE TRIGGER trigger_fmea_equipment_point_update AFTER UPDATE on f_fmea_equipment_point FOR EACH ROW
			BEGIN 
			DECLARE riskSourceId BIGINT (20);
			DECLARE num1 int (20);
			DECLARE num2 int (20);
			SELECT fm.risk_source_id INTO riskSourceId FROM f_fmea fm WHERE fm.id = (select fmea_id from f_fmea_equipment_point fep where fep.id = new.id);
			IF new.state = '1' then 
				UPDATE f_risk_source rs  set rs.status = 'ANOMALY' where rs.id = riskSourceId;
			ELSEIF new.state = '0' then 
				SELECT count(fep.id) INTO num1 FROM f_fmea_equipment_point fep 
				LEFT JOIN f_fmea ff ON ff.id = fep.fmea_id
				WHERE fep.state = '1' AND ff.risk_source_id = riskSourceId; 
				
				SELECT count(fpi.id) INTO num2 FROM f_fmea_point_inputitem fpi 
				LEFT JOIN f_fmea ff ON ff.id = fpi.fmea_id
				WHERE state = '1' AND ff.risk_source_id = riskSourceId; 
				update f_risk_source  rs set rs.`status` = (case when (num1+num2) >0 then 'ANOMALY' else 'NORMAL' END)  where rs.id = riskSourceId;
			END IF;
			END#
        </sql>
    </changeSet>
<!--    <changeSet author="suhg" id="1561454393001-2"  runAlways="true">-->
<!--        <comment>f_risk_source_point_inputitem  更新触发器创建</comment>-->
<!--        <sql endDelimiter="#">-->
<!--            DROP TRIGGER IF EXISTS `trigger_fmea_point_inputitem_update`;-->
<!--			CREATE TRIGGER trigger_fmea_point_inputitem_update AFTER UPDATE on f_fmea_point_inputitem FOR EACH ROW-->
<!--			BEGIN -->
<!--			DECLARE riskSourceId BIGINT (20);-->
<!--			DECLARE num1 int (20);-->
<!--			DECLARE num2 int (20);-->
<!--			-->
<!--			SELECT fm.risk_source_id INTO riskSourceId FROM f_fmea fm WHERE fm.id = (select fmea_id from f_fmea_point_inputitem fpi where fpi.id = new.id);-->
<!--				IF new.state = '1' then -->
<!--				UPDATE f_risk_source rs set rs.status = 'ANOMALY' where rs.id= riskSourceId;-->
<!--			ELSE-->
<!--				SELECT count(fep.id) INTO num1 FROM f_fmea_equipment_point fep -->
<!--				LEFT JOIN f_fmea ff ON ff.id = fep.fmea_id-->
<!--				WHERE fep.state = '1' AND ff.risk_source_id = riskSourceId; -->
<!--				-->
<!--				SELECT count(fpi.id) INTO num2 FROM f_fmea_point_inputitem fpi -->
<!--				LEFT JOIN f_fmea ff ON ff.id = fpi.fmea_id-->
<!--				WHERE state = '1' AND ff.risk_source_id = riskSourceId;-->
<!--			update f_risk_source  rs set rs.`status` = (case when (num1+num2)>0 then 'ANOMALY' else 'NORMAL' END)  where rs.id = riskSourceId;-->
<!--			END IF;-->
<!--		END#-->
<!--        </sql>-->
<!--    </changeSet>-->
    <changeSet author="shanqiyun" id="1588901126339-1"  runAlways="true">
        <comment>f_risk_source 查询所有子节点id</comment>
        <sql endDelimiter="#">
            DROP FUNCTION IF EXISTS queryRiskSourceChildrenIds;
			CREATE FUNCTION queryRiskSourceChildrenIds(id INT)
			RETURNS VARCHAR(4000)
			BEGIN
				DECLARE sTemp VARCHAR(4000);
				DECLARE sTempChd VARCHAR(4000);
				
				SET sTemp='$';
				SET sTempChd = CAST(id AS char);
				
				WHILE sTempChd IS NOT NULL DO
					SET sTemp= CONCAT(sTemp,',',sTempChd);
					SELECT GROUP_CONCAT(r.id) INTO sTempChd FROM f_risk_source r WHERE FIND_IN_SET(parent_id,sTempChd)>0;
				END WHILE;
				RETURN sTemp;
			END#
        </sql>
    </changeSet>

	<changeSet author="maoying" id="1589954524001-1"  runAlways="true">
        <comment>updatePointConfig 触发器创建</comment>
        <sql endDelimiter="#">
	        DROP TRIGGER IF EXISTS `updatePointConfig`;
			CREATE TRIGGER `updatePointConfig` AFTER UPDATE ON `p_point` FOR EACH ROW
			BEGIN
            IF (new.name != old.name)
			THEN
			 UPDATE p_point_config pc SET pc.`name` = CONCAT(new.name,'合格') WHERE pc.name LIKE '%合格' AND pc.point_id = new.id;
			 UPDATE p_point_config pc SET pc.`name` = CONCAT(new.name,'漏检')  WHERE pc.name LIKE '%漏检' AND pc.point_id = new.id;
			END IF;
			END#
        </sql>
    </changeSet>
    <changeSet author="shanqiyun" id="1597831211781-1"  runAlways="true">
        <comment>create view toip_biz_message</comment>
        <sql>
            DROP VIEW IF EXISTS toip_biz_message;
            CREATE ALGORITHM = UNDEFINED DEFINER = `root` @`%` SQL SECURITY DEFINER VIEW `toip_biz_message` AS SELECT
				`m`.`id` AS `id`,
				`m`.`id` AS `originId`,
				`m`.`time` AS `time`,
				`m`.`content` AS `content`,
				`m`.`title` AS `title`,
				`m`.`type` AS `type`,
				`m`.`sender` AS `sender`,
				`m`.`receiver` AS `receiver`,
				`m`.`reader` AS `reader`,
				`m`.`biz_id` AS `biz_id`,
				`m`.`bizclass_name` AS `bizclass_name`,
				`n`.`org_code` AS `org_code` 
				FROM
					( `toip_sys_message` `m` JOIN `f_equipment` `n` ) 
				WHERE
					( ( `n`.`id` = `m`.`biz_id` ) AND ( `m`.`bizclass_name` = 'class com.yeejoin.amos.fas.business.service.model.FireEquimentDataRo' ) ) UNION ALL
				SELECT
					`m`.`id` AS `id`,
					`m`.`id` AS `originId`,
					`m`.`time` AS `time`,
					`m`.`content` AS `content`,
					`m`.`title` AS `title`,
					`m`.`type` AS `type`,
					`m`.`sender` AS `sender`,
					`m`.`receiver` AS `receiver`,
					`m`.`reader` AS `reader`,
					`m`.`biz_id` AS `biz_id`,
					`m`.`bizclass_name` AS `bizclass_name`,
					`n`.`org_code` AS `org_code` 
				FROM
					( `toip_sys_message` `m` JOIN `p_point` `n` ) 
				WHERE
					( ( `n`.`id` = `m`.`biz_id` ) AND ( `m`.`bizclass_name` = 'class com.yeejoin.amos.fas.business.service.model.ProtalDataRo' ) ) UNION ALL
				SELECT
					`m`.`id` AS `id`,
					`m`.`id` AS `originId`,
					`m`.`time` AS `time`,
					`m`.`content` AS `content`,
					`m`.`title` AS `title`,
					`m`.`type` AS `type`,
					`m`.`sender` AS `sender`,
					`m`.`receiver` AS `receiver`,
					`m`.`reader` AS `reader`,
					`m`.`biz_id` AS `biz_id`,
					`m`.`bizclass_name` AS `bizclass_name`,
					`n`.`org_code` AS `org_code` 
				FROM
					( `toip_sys_message` `m` JOIN `f_risk_source` `n` ) 
				WHERE
					( ( `n`.`id` = `m`.`biz_id` ) AND ( `m`.`bizclass_name` = 'class com.yeejoin.amos.fas.business.service.model.RiskSourceRuleRo' ) );
       </sql>
    </changeSet>
</databaseChangeLog>