<?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="suhuiguang" id="1623223065754-1"  runAlways="true">
        <comment>`getBuildingNames`</comment>
        <sql endDelimiter="#">
            DROP FUNCTION IF EXISTS `getBuildingNames`#
            CREATE FUNCTION `getBuildingNames` (`rootId` VARCHAR(100))
            RETURNS varchar(1000)
            BEGIN
            DECLARE ptemp VARCHAR(1000);
            DECLARE ctemp VARCHAR(1000);
            DECLARE ctempName VARCHAR(1000);
            SET ptemp ='';
            SET ctemp = rootId;
            WHILE ctemp != '0' DO
            SET ptemp = concat(ptemp,',',ctemp);
            SELECT GROUP_CONCAT(parentId) INTO ctemp
            FROM get_building_tree
            WHERE instanceId = ctemp;
            END WHILE;
            SET ptemp = concat(ptemp,',',rootId);
            SELECT GROUP_CONCAT(instanceName) INTO ctempName
            FROM get_building_tree
            WHERE FIND_IN_SET(instanceId, ptemp);
            RETURN REPLACE(ctempName,',','');
            END#
        </sql>
    </changeSet>
    <changeSet author="suhuiguang" id="1623223065755-2"  runAlways="true">
        <comment>`getChildrend`</comment>
        <sql endDelimiter="#">
            DROP FUNCTION IF EXISTS `getChildrend`#
            CREATE FUNCTION `getChildrend` (`p_id` VARCHAR(1000)) RETURNS varchar(1000)
            BEGIN
            DECLARE sTemp TEXT;
            DECLARE sTempChd VARCHAR(5000);
            SET sTemp='$';
            SET sTempChd = CAST(p_id AS CHAR);
            WHILE sTempChd IS NOT NULL DO
            SET sTemp = CONCAT(sTemp,',',sTempChd);
            SELECT GROUP_CONCAT(distinct id) INTO sTempChd FROM wl_equipment_category WHERE FIND_IN_SET(parent_id,sTempChd) > 0;
            END WHILE;
            RETURN sTemp;
            END#
        </sql>
    </changeSet>
    <changeSet author="suhuiguang" id="1623223065756-3"  runAlways="true">
        <comment>`getChildrenIdsByRootId`</comment>
        <sql endDelimiter="#">
            DROP FUNCTION IF EXISTS `getChildrenIdsByRootId`#
            CREATE FUNCTION `getChildrenIdsByRootId` (`rootId` VARCHAR(100)) RETURNS text
            BEGIN
            DECLARE ids text;
            DECLARE ptemp text;
            SELECT
            GROUP_CONCAT(c.instanceId) INTO ids
            FROM
            (
            SELECT
            a.instanceId,
            IF (
            FIND_IN_SET(a.parentId ,@pids) > 0,

            IF (
            length(@pids) - length(
            REPLACE (@pids, a.parentId, '')
            ) > 1,

            IF (
            length(@pids) - length(REPLACE(@pids, a.instanceId, '')) > 1 ,@pids ,@pids := concat(@pids, ',', a.instanceId)
            ) ,@pids := concat(@pids, ',', a.instanceId)
            ),
            0
            ) AS 'plist',

            IF (
            FIND_IN_SET(a.parentId ,@pids) > 0,
            @pids,
            0
            ) AS ischild
            FROM
            (
            SELECT
            r.instanceId,
            r.parentId
            FROM
            get_building_tree r
            ) a,
            (SELECT @pids := rootId) b
            ) c
            WHERE
            c.ischild != 0;
            IF ISNULL(ids) || LENGTH(trim(ids)) <![CDATA[ < 1 ]]> THEN
            SET ptemp = rootId;
            ELSE
            SET ptemp = concat(rootId,',',ids);
            END IF;
            RETURN ptemp;
            END#
        </sql>
    </changeSet>
    <changeSet author="suhuiguang" id="1623223065754-4"  runAlways="true">
        <comment>`getParent`</comment>
        <sql endDelimiter="#">
            DROP FUNCTION IF EXISTS `getParent`#
            CREATE  FUNCTION `getParent` (`rootId` VARCHAR(1000)) RETURNS varchar(1000)
            BEGIN
            DECLARE ptemp VARCHAR(1000);
            DECLARE ctemp VARCHAR(1000);
            SET ptemp ='#';
            SET ctemp =rootId;
            WHILE ctemp IS NOT NULL DO
            SET ptemp = concat(ptemp,',',ctemp);
            SELECT GROUP_CONCAT(id) INTO ctemp
            FROM wl_equipment_category
            WHERE id = ( SELECT parent_id FROM wl_equipment_category WHERE FIND_IN_SET(id,ctemp) > 0);
            END WHILE;
            RETURN ptemp;
            END#
        </sql>
    </changeSet>
    <changeSet author="suhuiguang" id="1623223065754-5"  runAlways="true">
        <comment>`queryChildrenAreaInfo`</comment>
        <sql endDelimiter="#">
            DROP FUNCTION IF EXISTS `queryChildrenAreaInfo`#
            CREATE FUNCTION `queryChildrenAreaInfo` (areaId INT) RETURNS varchar(4000)
            BEGIN
            DECLARE sTemp VARCHAR(4000);
            DECLARE sTempChd VARCHAR(4000);
            SET sTemp='$';
            SET sTempChd = CAST(areaId AS CHAR);
            WHILE sTempChd IS NOT NULL DO
            SET sTemp= CONCAT(sTemp,',',sTempChd);
            SELECT GROUP_CONCAT(id) INTO sTempChd FROM wl_equipment_category WHERE FIND_IN_SET(parentId,sTempChd)>0;
            END WHILE;
            RETURN sTemp;
            END#
        </sql>
    </changeSet>
    <changeSet author="gaojianqiang" id="16270125660001-1"  runAlways="true">
        <comment>`getMaintenanceResourceTreeById`</comment>
        <sql endDelimiter="#">
            DROP FUNCTION IF EXISTS `getMaintenanceResourceTreeById`#
            CREATE DEFINER=`root`@`%` FUNCTION `getMaintenanceResourceTreeById`(`rootId` bigint) RETURNS varchar(1000) CHARSET utf8
            BEGIN

            DECLARE sTemp TEXT;
            DECLARE sTempChd VARCHAR(4000);

            SET sTemp='$';
            SET sTempChd = CAST(rootId AS CHAR);

            WHILE sTempChd IS NOT NULL DO
            SET sTemp= CONCAT(sTemp,',',sTempChd);
            SELECT GROUP_CONCAT(distinct id) INTO sTempChd FROM mt_maintenance_resource WHERE FIND_IN_SET(parent_id,sTempChd)>0;
            END WHILE;
            RETURN sTemp;
            END#
        </sql>
    </changeSet>
    <!--注释掉 不再自动刷脚本 因为现场可能调整编号或者指标编号 自动跑 会覆盖-->
<!--    <changeSet author="suhuiguang" id="1623219730755-1" runOnChange="true">-->
<!--        <comment>init view 所有的视图维护到此处view_all</comment>-->
<!--        <sqlFile path="./init/view_all.sql" relativeToChangelogFile="true"/>-->
<!--    </changeSet>-->
<!--    <changeSet author="suhuiguang" id="1623994514562-1" runOnChange="true">-->
<!--        <comment>机场 view 机场使用的视图维护到此处view_jcs</comment>-->
<!--        <sqlFile path="./init/view_jcs.sql" relativeToChangelogFile="true"/>-->
<!--    </changeSet>-->
<!--BUG 2935 初始化消防水源信息 by kongfm 2021-09-17-->
    <changeSet author="kongfm" id="2021-09-17-kongfm-1">
        <preConditions onFail="MARK_RAN">
            <tableExists tableName="wl_equipment_category" />
            <not>
            <primaryKeyExists primaryKeyName="id" tableName="wl_equipment_category"/>
            </not>
        </preConditions>
        <comment>init firewater info</comment>
        <sql>
            INSERT INTO `wl_equipment_category` (`id`, `parent_id`, `code`, `name`, `is_consumptive`, `description`, `remark`, `create_date`, `industry_code`) VALUES ('1438771726480601090', '1326407483682672641', '93060000', '消防水源', '0', NULL, NULL, '2021-09-17 15:48:05', '2');
            INSERT INTO `wl_equipment_category` (`id`, `parent_id`, `code`, `name`, `is_consumptive`, `description`, `remark`, `create_date`, `industry_code`) VALUES ('1438771952830410753', '1438771726480601090', '93060100', '消防水池', '0', NULL, NULL, '2021-09-17 15:48:59', '2');
            INSERT INTO `wl_equipment_category` (`id`, `parent_id`, `code`, `name`, `is_consumptive`, `description`, `remark`, `create_date`, `industry_code`) VALUES ('1438772365205991426', '1438771726480601090', '93060200', '消防水鹤', '0', NULL, NULL, '2021-09-17 15:50:38', '2');
            INSERT INTO `wl_equipment_category` (`id`, `parent_id`, `code`, `name`, `is_consumptive`, `description`, `remark`, `create_date`, `industry_code`) VALUES ('1438772459552665602', '1438771726480601090', '93060300', '天然水源', '0', NULL, NULL, '2021-09-17 15:51:00', '2');
            INSERT INTO `wl_equipment_category` (`id`, `parent_id`, `code`, `name`, `is_consumptive`, `description`, `remark`, `create_date`, `industry_code`) VALUES ('1438772584828137473', '1438771726480601090', '93060400', '水源-消火栓', '0', NULL, NULL, '2021-09-17 15:51:30', '2');
        </sql>
    </changeSet>
    
      <changeSet author="chenhao" id="2021-11-03-1">
        <preConditions onFail="MARK_RAN">
             <not>
                <columnExists tableName="wl_equipment_specific" columnName="agency_name"/>
            </not>
        </preConditions>
        <comment>update data wl_equipment_specific</comment>
        <sql>
 			 ALTER TABLE wl_equipment_specific ADD agency_name varchar(100)  NULL COMMENT '所属单位名称(来源于平台)';
         </sql>
    </changeSet>
    <changeSet author="suhuiguang" id="1638172796697-3"  runAlways="true">
        <comment>`alarm_equip_day_insert`</comment>
        <sql endDelimiter="#">
            DROP event  IF EXISTS `alarm_equip_day_insert`#
            CREATE DEFINER=`root`@`%` EVENT `alarm_equip_day_insert` ON SCHEDULE EVERY 1 DAY STARTS '2021-11-29 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
            TRUNCATE table wl_equipment_alarm_equip_day;
            INSERT into `wl_equipment_alarm_equip_day` (
            `report_date`,
            `system_id`,
            `equipment_code`,
            `equipment_name`,
            `index_key`,
            `index_name`,
            `report_equip_num`)
            SELECT
            r.report_date,
            f.id as system_id,
            r.equipment_code,
            '' as equipment_name,
            r.index_type,
            r.index_name,
            count(DISTINCT(r.equipment_specific_id)) as `report_equip_num`
            FROM
            `wl_equipment_alarm_report_day` r,
            f_fire_fighting_system f
            where
            r.index_true_num > 0
            and FIND_IN_SET(f.id,r.system_ids)
            GROUP BY
            r.report_date,
            r.system_ids,
            r.equipment_code,
            r.index_type,
            r.index_name;
            END#
        </sql>
    </changeSet>
    <changeSet author="gaojianqiang" id="1641307260-20220104-01"  runAlways="true">
        <comment>`getBuildChildIdsByRootId`</comment>
        <sql endDelimiter="#">
            DROP FUNCTION IF EXISTS `getBuildChildIdsByRootId`#
            CREATE DEFINER=`root`@`%` FUNCTION `getBuildChildIdsByRootId`(`rootId` VARCHAR(100)) RETURNS varchar(4000) CHARSET utf8mb4 COLLATE utf8mb4_general_ci
            BEGIN
            DECLARE sTemp TEXT;
            DECLARE sTempChd VARCHAR(4000);
            SET group_concat_max_len=102400;

            SET sTemp='$';
            SET sTempChd = CAST(rootId AS CHAR);

            WHILE sTempChd IS NOT NULL DO
            SET sTemp= CONCAT(sTemp,',',sTempChd);
            SELECT GROUP_CONCAT(distinct instanceId) INTO sTempChd FROM get_building_tree WHERE FIND_IN_SET(parentId,sTempChd)>0;
            END WHILE;
            RETURN sTemp;
            END#
        </sql>
    </changeSet>
    
    <changeSet author="maoying" id="1661505446-01"  runAlways="true">
        <comment>`create v_running_days`</comment>
        <sql>
           DROP VIEW IF EXISTS `v_running_days`;
			CREATE ALGORITHM = UNDEFINED DEFINER = `root`@`%` SQL SECURITY DEFINER VIEW `v_running_days` AS SELECT
			(
				to_days(now()) - to_days('20160823')
			) AS `runningDays`
        </sql>
    </changeSet>
</databaseChangeLog>