<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yeejoin.equipmanage.mapper.BuildingMapper">
    <resultMap id="BuildingLists" type="com.yeejoin.equipmanage.common.entity.vo.BuildingListVO">
        <result column="full_name" property="warehouseName"></result>
        <result column="name" property="equipmentName"></result>
        <result column="code" property="code"></result>
        <result column="category_name" property="categoryName"></result>
        <result column="amount" property="amount"></result>
        <result column="unit_name" property="unitName"></result>
    </resultMap>
    <resultMap id="EquiplistBySystemId" type="com.yeejoin.equipmanage.common.entity.vo.EquiplistSpecificBySystemVO">
        <result property="id" column="id"></result>
        <result property="equipmentName" column="equipment_name"></result>
        <result property="code" column="code"></result>
        <result property="categoryName" column="category_name"></result>
    </resultMap>

    <resultMap id="TreeAndEquipVO" type="com.yeejoin.equipmanage.common.vo.BuildingTreeAndEquipVO">
        <result property="parentId" column="parentId"></result>
        <result property="name" column="name"></result>
        <result property="id" column="id"></result>
        <result property="type" column="type"></result>
        <collection property="children" select="com.yeejoin.equipmanage.mapper.BuildingMapper.getSonBuildingTree" column="id">
            <result property="parentId" column="parentId"></result>
            <result property="name" column="name"></result>
            <result property="id" column="id"></result>
            <result property="type" column="type"></result>
        </collection>
    </resultMap>

    <resultMap id="BuildingTreeSon" type="com.yeejoin.equipmanage.common.vo.BuildingTreeAndEquipVO">
        <result property="parentId" column="parentId"></result>
        <result property="name" column="name"></result>
        <result property="id" column="id"></result>
        <result property="type" column="type"></result>
        <collection property="children" select="com.yeejoin.equipmanage.mapper.BuildingMapper.getSonBuildingTree1" column="id">
            <result property="parentId" column="parentId"></result>
            <result property="name" column="name"></result>
            <result property="id" column="id"></result>
            <result property="type" column="type"></result>
        </collection>
    </resultMap>

    <resultMap id="BuildingTreeSon1" type="com.yeejoin.equipmanage.common.vo.BuildingTreeAndEquipVO">
        <result property="parentId" column="parentId"></result>
        <result property="name" column="name"></result>
        <result property="id" column="id"></result>
        <result property="type" column="type"></result>
        <collection property="children" select="com.yeejoin.equipmanage.mapper.BuildingMapper.getSonBuildingTree2" column="id">
            <result property="parentId" column="parentId"></result>
            <result property="name" column="name"></result>
            <result property="id" column="id"></result>
            <result property="type" column="type"></result>
        </collection>
    </resultMap>

    <resultMap id="BuildingTreeSon2" type="com.yeejoin.equipmanage.common.vo.BuildingTreeAndEquipVO">
        <result property="parentId" column="parentId"></result>
        <result property="name" column="name"></result>
        <result property="id" column="id"></result>
        <result property="type" column="type"></result>
    </resultMap>

    <select id="getFireArchitectureEnumList"
            resultType="com.yeejoin.equipmanage.common.entity.EquipmentCategory">
        SELECT
            id,
            `code`,
            `name`
        FROM
            wl_equipment_category
        WHERE
            parent_id = (
            SELECT
                id
            FROM
                wl_equipment_category
        WHERE
            CODE = #{code})
    </select>
    <select id="getExamineList" resultMap="BuildingLists">
        SELECT
            wlws.full_name ,
            wled.`name`,
            wlesp.code,
            wlec.`name` category_name,
            wlsd.amount,
          wlu.name unit_name
        FROM
            wl_warehouse_structure wlws
            LEFT JOIN wl_stock_detail wlsd ON wlsd.warehouse_structure_id = wlws.id
            left join wl_stock wls on wls.id=wlsd.stock_id
            left join wl_equipment_detail wled on wlsd.equipment_detail_id = wled.id
            left join wl_equipment_specific wlesp on wlesp.equipment_detail_id = wled.id
            left join wl_equipment wlem on wled.equipment_id = wlem.id
            left join wl_equipment_category wlec on wlec.id=wlem.category_id
            left join wl_unit wlu on wlem.unit_id=wlu.id
        WHERE
            wlws.source_id = #{regionId}
    </select>
    <insert id="insetSourceScene">
            insert into wl_source_scene
             (id,
             source_id,
             source_name,
             project_id,
             scene_id,
             point_in_scene,
             creator_id,
             create_date,
             video_in_scene)
             VALUES(
             #{id},
             #{instanceId},
             #{name},
             #{projectId},
             #{sceneId},
             null,
             #{creatorId},
             now(),
             null)
    </insert>
    <update id="setSceneId">
            UPDATE
              wl_source_scene
              SET point_in_scene=#{pointInScene},
                     video_in_scene = #{videoInScene}
              where scene_id =#{sceneId}
    </update>
    <select id="getPointInScene" resultType="string">
        select point_in_scene from wl_source_scene where scene_id =#{id}
    </select>
    <select id="getVPointInScene" resultType="string">
        select video_in_scene from wl_source_scene where scene_id =#{id}
    </select>

    <select id="getEquipBySceneId" resultMap="EquiplistBySystemId">
        SELECT
                spe.id,
                det.NAME equipment_name,
                spe.CODE,
                wlec.NAME AS category_name
                FROM
                wl_equipment_specific AS spe
                LEFT JOIN wl_equipment_detail AS det ON spe.equipment_detail_id = det.id
                LEFT JOIN wl_equipment AS wle ON wle.id = det.equipment_id
                LEFT JOIN wl_equipment_category AS wlec ON wlec.id = wle.category_id
                WHERE
                spe.id in
                <foreach item="item" collection="ids" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
    </select>
    <select id="getVideoBySceneId" resultMap="EquiplistBySystemId">
            SELECT
            id,
            name equipment_name,
            code,
            name AS category_name
            FROM
            wl_video
            WHERE
            id in
            <foreach item="item" collection="ids" separator="," open="(" close=")" index="">
                #{item}
            </foreach>
    </select>
    <select id="getScenesList" resultType="com.yeejoin.equipmanage.common.entity.SourceScene">
        select
        ins.instance_id as id,
        ins.field_value as sourceName,
        sou.scene_id as sceneId
        from
        wl_form_instance  as ins
        left join wl_source_scene as sou on sou.source_id = ins.instance_id
        where ins.field_name ='name'
        and instance_id in (
        <if test="groupType == 'building' ">
            select
            instance_id
            from
            wl_form_instance
            where field_name ='parentId'
            and group_type ='floor'
            and field_value = #{id}
        </if>
        <if test="groupType == 'floor' ">
            #{id}
        </if>
        <if test="groupType == 'storageTank' ">
            #{id}
        </if>
        <if test="groupType == 'room' ">
            select
            field_value
            from
            wl_form_instance
            where field_name ='parentId'
            and instance_id = #{id}
        </if>
        )
        group by ins.instance_id
    </select>
    <select id="getBuildList" resultType="com.yeejoin.equipmanage.common.entity.vo.PointTreeVo">
        SELECT
            t.*
        FROM
            (
            SELECT
                id AS sequenceNbr,
                group_name AS displayName,
                - 1 AS parentId,
                NULL AS equipCode,
                NULL AS bizOrgCode
            FROM
                wl_form_group
            WHERE
                id = 0 UNION ALL
            SELECT
                ins.instance_id AS sequenceNbr,
                nam.field_value AS displayName,
                par.field_value AS parentId,
                cd.field_value AS equipCode,
                bzc.field_value AS bizOrgCode
            FROM
                wl_form_instance AS ins
                LEFT JOIN wl_form_instance AS nam ON nam.instance_id = ins.instance_id
                AND nam.field_name = 'name'
                LEFT JOIN wl_form_instance AS par ON par.instance_id = ins.instance_id
                AND par.field_name = 'parentId'
                LEFT JOIN wl_form_instance AS cd ON cd.instance_id = ins.instance_id
                AND cd.field_name = 'code'
                LEFT JOIN wl_form_instance AS bzc ON bzc.instance_id = ins.instance_id
                AND bzc.field_name = 'bizOrgCode'
                <where>
                    <if test="bizOrgCode != null and bizOrgCode != ''">
                        bzc.field_value = #{bizOrgCode}
                    </if>
                </where>
            GROUP BY
                ins.instance_id
            ORDER BY
                parentId
            ) t

    </select>
    <select id="get3dBuildList" resultType="com.yeejoin.equipmanage.common.entity.vo.PointTreeVo">
        select
            id as sequenceNbr,
            group_name as displayName,
            -1 as parentId
            from wl_form_group where id = 0
    union all
        select
            ins.instance_id as sequenceNbr,
            nam.field_value as displayName,
            par.field_value as parentId
            from
            wl_form_instance as ins
            left join wl_form_instance as nam on  nam.instance_id = ins.instance_id and nam.field_name ='name'
            left join wl_form_instance as par on  par.instance_id = ins.instance_id and par.field_name ='parentId'
            group by ins.instance_id
            order by parentId
    </select>
    <select id="isBuildingRoom" resultType="Long">
        select
            (case
            when group_type ='building'
                 then instance_id
                 else null
                 end ) as id
            from wl_form_instance
            where instance_id =(
            select field_value from wl_form_instance where field_name ='parentId'  and instance_id = #{id})
            group by instance_id
    </select>
    <select id="getEquipmentCount" resultType="Long">
         select
         if(sum(sto.amount) is null,0,sum(sto.amount))  as count from
         wl_stock_detail as sto
         left join wl_warehouse_structure as war on  sto.warehouse_structure_id  = war.id
         where war.source_id in(
        <if test="groupType == 'building' ">
            select source_id from wl_warehouse_structure where parent_id = #{id} or source_id = #{id}
            union all
            select source_id from wl_warehouse_structure where parent_id in(
            select source_id from wl_warehouse_structure where parent_id = #{id}
            )

        </if>
        <if test="groupType == 'floor' ">
            select instance_id from wl_form_instance where field_name ='parentId' and field_value = #{id} or instance_id = #{id}
            group by instance_id
        </if>
        <if test="groupType == 'room' ">
            #{id}
        </if>
         )
    </select>
    <select id="getFloorOrRoomCount" resultType="Long">
         select count(1) from
         wl_form_instance
         where field_name = 'parentId'
         and field_value = #{id}
        <if test="groupType == 'building' ">
            and group_type = 'floor'
        </if>
    </select>
    <select id="getRoomCount" resultType="Long">
         select count(1) from (
              select *  from wl_form_instance  where field_name = 'parentId'  and group_type = 'room' and field_value = #{id}
              union
              select *  from wl_form_instance  where field_name = 'parentId' and
              field_value in (
                select instance_id from wl_form_instance  where field_name = 'parentId'  and group_type = 'floor' and field_value = #{id}
              )) co
    </select>
    <select id="getAllfloorCount" resultType="com.yeejoin.equipmanage.common.entity.FormInstance">
         select
         war.source_id as instanceId ,
         sum(sto.amount ) as fieldValue
         from
         wl_warehouse_structure as war
         left join wl_stock_detail as sto on war.id = sto.warehouse_structure_id
         where war.source_id in (
         select
         instance_id
         from
         wl_form_instance
         where field_name ='parentId'
         and group_type = 'floor'
         and field_value =#{id})
         group by war.id
    </select>
    <select id="selectParentId" resultType="Long">
          select field_value from wl_form_instance
         where field_name ='parentId'
         and instance_id = (
         select source_id from wl_source_scene  where scene_id = #{id}
         )
    </select>
    <select id="selectSystemId" resultType="String">
         select system_id from wl_equipment_specific where id = #{id}
    </select>
    <select id="getSpeids" resultType="String">
         select point_in_scene from wl_source_scene where scene_id =#{id}
    </select>
    <select id="selectWare" resultType="com.yeejoin.equipmanage.common.entity.WarehouseStructure">
         select war.* from
         wl_stock_detail as det
         left join wl_warehouse_structure as war on war.id = det.warehouse_structure_id
         where equipment_specific_id =#{id}
    </select>
    <select id="getBuildingtreeAndEquip" resultMap="TreeAndEquipVO">
         SELECT
                *
                FROM
                (
                SELECT
                a.instance_id as id,
                MAX( CASE WHEN a.field_name = 'parentId' THEN a.field_value END ) AS parentId,
                MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS name,
                        'building' as type
                FROM
                `wl_form_instance` a
                LEFT JOIN wl_source_scene wlss ON wlss.source_id = a.instance_id
                GROUP BY
                a.instance_id
                ) sa
				where sa.parentId=0
					union all
				select id,null as parentId,fei.`name`,'equipment' as type from  f_equipment fei where fei.risk_source_id=0
    </select>

    <select id="getSonBuildingTree" resultMap="BuildingTreeSon">
            SELECT
            *
            FROM
            (
            SELECT
            a.instance_id as id,
            MAX( CASE WHEN a.field_name = 'parentId' THEN a.field_value END ) AS parentId,
            MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS name,
            'building' as type
            FROM
            `wl_form_instance` a
            LEFT JOIN wl_source_scene wlss ON wlss.source_id = a.instance_id
            GROUP BY
            a.instance_id
            ) sa
            where sa.parentId=#{id}
            union all
            select id,null as parentId,fei.`name`,'equipment' as type from  f_equipment fei where fei.risk_source_id=#{id}
    </select>

    <select id="getSonBuildingTree1" resultMap="BuildingTreeSon">
            SELECT
            *
            FROM
            (
            SELECT
            a.instance_id as id,
            MAX( CASE WHEN a.field_name = 'parentId' THEN a.field_value END ) AS parentId,
            MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS name,
            'building' as type
            FROM
            `wl_form_instance` a
            LEFT JOIN wl_source_scene wlss ON wlss.source_id = a.instance_id
            GROUP BY
            a.instance_id
            ) sa
            where sa.parentId=#{id}
            union all
            select id,null as parentId,fei.`name`,'equipment' as type from  f_equipment fei where fei.risk_source_id=#{id}
    </select>

    <select id="getSonBuildingTree2" resultMap="BuildingTreeSon">
            SELECT
            *
            FROM
            (
            SELECT
            a.instance_id as id,
            MAX( CASE WHEN a.field_name = 'parentId' THEN a.field_value END ) AS parentId,
            MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS name,
            'building' as type
            FROM
            `wl_form_instance` a
            LEFT JOIN wl_source_scene wlss ON wlss.source_id = a.instance_id
            GROUP BY
            a.instance_id
            ) sa
            where sa.parentId=#{id}
            union all
            select id,null as parentId,fei.`name`,'equipment' as type from  f_equipment fei where fei.risk_source_id=#{id}
    </select>
    <select id="getCountByTypeCode" resultType="java.lang.Long">
        SELECT
            count(1)
        FROM
            wl_form_instance
        WHERE
            field_name = 'bizOrgCode'
        AND field_value LIKE  CONCAT(#{bizOrgCode},'%')
        AND group_type = #{type}
    </select>
    <select id="getEquipCount" resultType="java.lang.Long">
        SELECT
            IF (
                sum(sto.amount) IS NULL,
                0,
                sum(sto.amount)
            ) AS count
        FROM
            wl_stock_detail AS sto
        LEFT JOIN wl_warehouse_structure AS war ON sto.warehouse_structure_id = war.id
        LEFT JOIN wl_form_instance wfi ON wfi.instance_id = war.source_id
        WHERE
         wfi.field_name = 'bizOrgCode'
         AND wfi.field_value LIKE  CONCAT(#{bizOrgCode},'%')
    </select>
</mapper>
