<?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.FormInstanceMapper">
    <resultMap id="resultMapDTO" type="com.yeejoin.equipmanage.common.entity.dto.BuildSearchDTO">
        <result column="instanceId" property="instanceId"/>
        <result column="fieldValue" property="fieldValue"/>
        <result column="groupType" property="groupType"/>
    </resultMap>
    <select id="queryInstancePage" resultType="map">
        select
        d.*
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode,
        <foreach collection="fieldNames" item="value" index="key" separator=",">
            MAX(CASE WHEN i.FIELD_NAME = #{key} THEN i.FIELD_VALUE END) as ${key}
        </foreach>
        from
        wl_form_instance i
        where i.GROUP_CODE = #{groupCode}
        GROUP by
        i.INSTANCE_ID)d
        <if test="params != null and  params.size() > 0">
            where
            1=1
            <foreach collection="params" index="key" item="value" separator="">
                <choose>
                    <when test="fieldNames[key] == 'like' and value !=null and value !=''">
                        and d.${key} like concat('%',#{value},'%')
                    </when>
                    <when test="fieldNames[key] == 'eq' and value !=null and value !=''">
                        and d.${key} = #{value}
                    </when>
                </choose>
            </foreach>
        </if>
        order by instanceId desc
    </select>

    <select id="getChildListdate" resultType="map">
	SELECT getChildListdate ( #{id} ) ids
    </select>

    <update id="updateListChild" >

        UPDATE wl_form_instance  set field_value =#{value}
        where wl_form_instance.group_type in('building','room','floor')
        <if test ="ids != null and ids.size()>0">
         and wl_form_instance.instance_id  in
        <foreach collection="ids" index="index" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        </if>
       and wl_form_instance.field_name=#{fieldName}
    </update>



    <select id="queryForMapList" resultType="map">
        select
        d.*
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode
        <foreach collection="fieldNames" item="value" open="," index="key" separator=",">
            MAX( CASE WHEN i.FIELD_NAME = #{key} THEN i.FIELD_VALUE END) ${key}
        </foreach>
        from
        wl_form_instance i
        where i.GROUP_CODE = #{groupCode}
        GROUP by
        i.INSTANCE_ID)d
        <if test="params != null and  params.size() > 0">
            where
            1=1
            <foreach collection="params" index="key" item="value" separator="">
                <choose>
                    <when test="fieldNames[key] == 'like' and value !=null and value !=''">
                        AND d.${key} like concat('%',#{value},'%')
                    </when>
                    <when test="fieldNames[key] == 'eq' and value !=null and value !=''">
                        AND d.${key} = #{value}
                    </when>
                </choose>

            </foreach>
        </if>
        order by d.instanceId desc
    </select>
    <select id="querySpecialChildrenList" resultType="hashmap">
        SELECT
        *
        FROM
        (
        SELECT
        a.instance_id as id,
        a.instance_id AS instanceId,
        a.group_code AS groupCode,
        a.group_type AS groupType,
        wlss.scene_id as sceneId,
        IFNULL(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 instanceName,
        MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS name,
        MAX( CASE WHEN a.field_name = 'code' THEN a.field_value END ) AS code,
        MAX( CASE WHEN a.field_name = 'isRisk' THEN a.field_value END ) AS isRisk,
        MAX( CASE WHEN a.field_name = 'address' THEN a.field_value END ) AS address,
        MAX( CASE WHEN a.field_name = 'bizOrgCode' THEN a.field_value END ) AS bizOrgCode,
        MAX( CASE WHEN a.field_name = 'bizOrgName' THEN a.field_value END ) AS bizOrgName
        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>
            <if test="parentId != null and parentId !=''">
                sa.parentId = #{parentId}
            </if>
            <if test="bizOrgCode != null and bizOrgCode !=''">
                sa.bizOrgCode like CONCAT(#{bizOrgCode},'%')
            </if>
        </where>
    </select>

    <select id="querySpecialChildrenListByName" resultType="hashmap">
        SELECT
        *
        FROM
        (
        SELECT
        a.instance_id as id,
        a.instance_id AS instanceId,
        a.group_code AS groupCode,
        a.group_type AS groupType,
        wlss.scene_id as sceneId,
        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 instanceName,
        MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS name,
        MAX( CASE WHEN a.field_name = 'code' THEN a.field_value END ) AS code,
        MAX( CASE WHEN a.field_name = 'isRisk' THEN a.field_value END ) AS isRisk,
        MAX( CASE WHEN a.field_name = 'address' THEN a.field_value END ) AS address,
        MAX( CASE WHEN a.field_name = 'bizOrgCode' THEN a.field_value END ) AS bizOrgCode,
        MAX( CASE WHEN a.field_name = 'bizOrgName' THEN a.field_value END ) AS bizOrgName
        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>
            <if test="parentId != null and parentId !=''">
                and sa.parentId = #{parentId}
            </if>
            <if test="bizOrgCode != null and bizOrgCode !=''">
                and sa.bizOrgCode like CONCAT(#{bizOrgCode},'%')
            </if>
            <if test="name != null and name !=''">
                and sa.instanceName like CONCAT('%', #{name}, '%')
            </if>
        </where>
    </select>

    <select id="queryPage" resultType="hashmap">
        select * from (SELECT
        a.instance_id AS instanceId,
        a.group_code AS groupCode,
        a.group_type AS groupType,
        a.field_value AS fieldValue,
        a.field_label AS fieldLabel,
        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 buildName,
        MAX(CASE WHEN a.field_name = 'isRisk' THEN a.field_value END)AS isRisk,
        MAX(CASE WHEN a.field_name = 'code' THEN a.field_value END)AS buildCode,
        MAX(CASE WHEN a.field_name = 'certificatesPersonNum' THEN a.field_value END)AS certificatesPersonNum,
        MAX(CASE WHEN a.field_name = 'dutyUser' THEN a.field_value END)AS dutyUser,
        MAX(CASE WHEN a.field_name = 'hasFirePlan' THEN a.field_value END)AS hasFirePlan,
        MAX(CASE WHEN a.field_name = 'installLoc' THEN a.field_value END)AS installLoc,
        MAX(CASE WHEN a.field_name = 'inwhichBuild' THEN a.field_value END)AS inwhichBuild,
        MAX(CASE WHEN a.field_name = 'maintenanceUnit' THEN a.field_value END)AS maintenanceUnit,
        MAX(CASE WHEN a.field_name = 'bizOrgCode' THEN a.field_value END)AS bizOrgCode,
        MAX(CASE WHEN a.field_name = 'bizOrgName' THEN a.field_value END)AS bizOrgName
        FROM
        `wl_form_instance` a
        GROUP BY
        a.instance_id) sa
        <where>
            sa.parentId in
            <foreach item="item" index="index" collection="query.instanceIds" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="query.fieldValue != null and query.fieldValue !=''">
                AND sa.fieldValue = #{query.fieldValue}
            </if>
            <if test="query.bizOrgCode != null and query.bizOrgCode !=''">
                AND sa.bizOrgCode LIKE CONCAT(#{query.bizOrgCode},'%')
            </if>
        </where>
        ORDER BY sa.instanceId DESC
    </select>
    <select id="roomPage" resultType="hashmap">
        select * from (SELECT
        a.instance_id AS instanceId,
        a.group_code AS groupCode,
        a.group_type AS groupType,
        a.field_value AS fieldValue,
        a.field_label AS fieldLabel,
        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 buildName,
        MAX(CASE WHEN a.field_name = 'isRisk' THEN a.field_value END)AS isRisk
        FROM
        `wl_form_instance` a
        GROUP BY
        a.instance_id) sa
        <where>
            sa.instanceId in
            <foreach item="item" index="index" collection="query.instanceIds" open="(" separator="," close=")">
                #{item}
            </foreach>
            <if test="query.fieldValue != null and query.fieldValue !=''">
                AND sa.fieldValue = #{query.fieldValue}
            </if>
        </where>
        ORDER BY sa.instanceId DESC
    </select>
    <select id="queryFloorRoom" resultType="hashmap">
        SELECT * FROM (SELECT
        a.instance_id AS instanceId,
        a.group_type AS groupType,
        MAX(
        CASE
        WHEN a.field_name = 'parentId' THEN
        a.field_value
        END
        ) AS parentId
        FROM
        `wl_form_instance` a
        GROUP BY
        a.instance_id) sa
        WHERE sa.parentId in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="getBuildFloorList" resultMap="resultMapDTO">
        SELECT
        sa.instanceId,
        sa.groupType,
        sa.fieldValue
        FROM
        (
        SELECT
        a.instance_id AS instanceId,
        a.group_type AS groupType,
        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 fieldValue
        FROM
        `wl_form_instance` a
        GROUP BY
        a.instance_id
        ) sa
        <where>
            sa.parentId = #{query.instanceId}
            <if test="query.groupType != null and query.groupType !=''">
                AND sa.groupType = #{query.groupType}
            </if>
        </where>
        ORDER BY
        sa.instanceId DESC
    </select>
    <select id="findDetailByInstanceId" resultType="java.util.LinkedHashMap">
        SELECT
            a.field_label AS `key`,
            a.field_value AS `value`
        FROM
            `wl_form_instance` a
        WHERE
            a.instance_id = #{instanceId}
        AND
            a.field_label != '父级id'
        ORDER BY a.group_column_id
    </select>
    <select id="findByInstanceId" resultType="java.util.LinkedHashMap">
        SELECT
            fi.NAME AS '建筑名称',
            fi.dutyUser AS '负责人',
            fi.buildType AS '建筑结构',
            fi.dutyUserPhone AS '负责人电话',
            fi.buildUseType AS '使用性质',
            fi.structureType AS '结构类型',
            fi.address AS '详细地址'
        FROM
            (
        SELECT
            a.instance_id,
            MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS NAME,
            MAX( CASE WHEN a.field_name = 'dutyUser' THEN a.field_value END ) AS dutyUser,
            MAX( CASE WHEN a.field_name = 'buildType' THEN a.field_value END ) AS buildType,
            MAX( CASE WHEN a.field_name = 'dutyUserPhone' THEN a.field_value END ) AS dutyUserPhone,
            MAX( CASE WHEN a.field_name = 'buildUseType' THEN a.field_value END ) AS buildUseType,
            MAX( CASE WHEN a.field_name = 'structureType' THEN a.field_value END ) AS structureType,
            MAX( CASE WHEN a.field_name = 'address' THEN a.field_value END ) AS address
        FROM
            `wl_form_instance` a
        WHERE
            a.instance_id = #{instanceId}
        GROUP BY
            a.instance_id
            ) fi
    </select>
    <select id="getFormInstanceById" resultType="java.util.Map">
        SELECT
            a.instance_id AS instanceId,
            MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS `name`,
            MAX( CASE WHEN a.field_name = 'code' THEN a.field_value END ) AS `code`,
            MAX( CASE WHEN a.field_name = 'address' THEN a.field_value END ) AS address,
            MAX( CASE WHEN a.field_name = 'riskPointId' THEN a.field_value END ) AS riskSourceId
        FROM
            `wl_form_instance` a
        <where>
            <if test="instanceId != null">
                a.instance_id = #{instanceId}
            </if>
        </where>
    </select>
    <!--  消防建筑视屏监控统计树  -->
    <select id="getBuildVideoListCount" resultType="hashmap">
        SELECT  *  FROM(
        SELECT
            sa.*,
            b.total
        FROM
            (
            SELECT
                a.instance_id AS instanceId,
                a.group_code AS groupCode,
                a.group_type AS groupType,
                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 instanceName,
                MAX( CASE WHEN a.field_name = 'biz_org_code' THEN a.field_value END ) AS bizOrgCode
            FROM
                `wl_form_instance` a
            GROUP BY
                a.instance_id
            ) sa
            LEFT JOIN ( SELECT wlv.source_id, count( wlv.source_id ) AS total FROM `wl_video_source` wlv GROUP BY wlv.source_id ) b ON b.source_id = sa.instanceId
            union all
            select
            123456789123456789 as instanceId,
            'else' as groupCode,
            'else' as groupType,
             0 as parentId,
            '其他' as instanceName,
            NULL AS bizOrgCode,
            count(1) as total
            from
            wl_video where id not in (select video_id from wl_video_source)
            ) tmp
            <where>
                <if test="list != null and list.size() >0">
                    AND tmp.bizOrgCode IN
                    <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
            </where>
    </select>

    <select id="getColaBuildVideoListCount" resultType="hashmap">
        SELECT
        sa.*,
        b.total
        FROM
        (
        SELECT
        wws.id AS instanceId,
        CONCAT(wws.parent_id,'') AS parentId,
        wws.name AS instanceName
        FROM
        `wl_warehouse_structure` wws
        GROUP BY
        wws.id
        ) sa
        LEFT JOIN (
        SELECT
        wws.id AS buildId,
        COUNT(wws.id) AS total
        FROM
        wl_stock_detail wsd,
        wl_equipment_detail wed,
        wl_equipment we,
        wl_equipment_category wec,
        wl_warehouse_structure wws
        WHERE
        wsd.equipment_detail_id = wed.id
        AND wed.equipment_id = we.id
        AND wec.id = we.category_id
        AND wws.id = wsd.warehouse_structure_id
        and wec.code in
        <foreach collection="dto.monitorCodes" item="dto.monitorCodes" index="index" open="(" close=")" separator=",">
            #{dto.monitorCodes}
        </foreach>
        GROUP BY wws.id
        ) b ON b.buildId = sa.instanceId
    </select>

    <insert id="saveStucture">
        insert into wl_warehouse_structure
             (id,
             code,
             name,
             full_name,
             parent_id,
             warehouse_id,
             create_date,
             biz_org_code,
             biz_org_name,
             source_id)
             VALUES(
             #{instanceId},
             #{code},
             #{name},
             #{address},
             #{parentId},
             0,
             now(),
             #{bizOrgCode},
             #{bizOrgName},
             #{instanceId})
    </insert>
    <select id="getStuctureName" resultType="String">
        select full_name from wl_warehouse_structure
        where id = #{id}
    </select>
    <select id="getStuName" resultType="String">
       select name from wl_warehouse_structure
        where id = #{id}
    </select>
    <select id="getStructureParentId" resultType="Long">
        select parent_id from wl_warehouse_structure
        where id = #{id}
    </select>
    <delete id="deleteStuById">
        DELETE  FROM wl_warehouse_structure WHERE  id = #{id}
    </delete>
    <update id="updateStr">
        update wl_warehouse_structure
        set  name = #{name} ,
             full_name = #{fullName},
             parent_id = #{parentId},
             code = #{code},
             biz_org_code = #{bizOrgCode},
             biz_org_name= #{bizOrgName}
        where id = #{id}
    </update>
    <update id="updateFormFieldValue">
        update wl_form_instance
            set
            field_value = #{value}
            where instance_id = #{id} AND field_name = #{name}
    </update>
    <update id="updateStrFullName">
         update wl_warehouse_structure
         set full_name=replace(full_name,#{name},#{fullName})
         where
         id in (
            select instance_id from wl_form_instance  where field_name = 'parentId' and field_value  = #{id}
            union all
            select instance_id from wl_form_instance where field_name = 'parentId' and field_value in (
                select instance_id from wl_form_instance  where field_name = 'parentId' and field_value  = #{id}
            )
         )
    </update>
    <update id="clearSystemId">
      update wl_form_instance
     set field_value = null
     where field_name like '%system%'
     and field_value = #{id}
    </update>

    <select id="getChildrenNotIsRiskList" resultType="com.yeejoin.equipmanage.common.entity.dto.BuildIsRiskDTO">
        select * from (SELECT
          a.instance_id AS instanceId,
          MAX(
            CASE
            WHEN a.field_name = 'parentId' THEN
              a.field_value
            END
          ) AS parentId,
          MAX(
            CASE
            WHEN a.field_name = 'isRisk' THEN
              a.field_value
            END
          ) AS isRisk
        FROM
          `wl_form_instance` a
        GROUP BY
          a.instance_id) sa where sa.parentId = #{instanceId} and sa.isRisk = 'false' and sa.isRisk is not null
    </select>
    <update id="clearRegionBind">
        UPDATE
            wl_form_instance
        SET field_value = (
        CASE field_name
        WHEN 'isRisk' THEN
            'false'
        WHEN 'riskPointId' THEN
            ''
        END
        )
        WHERE
	        instance_id = #{instanceId} and (field_name = 'isRisk' or field_name = 'riskPointId')
    </update>
    <select id="getParentId" resultType="map">
        select
            *
        from
            wl_warehouse_structure
        where
            parent_id = #{parentId}

    </select>
    <select id="getIdAndType" resultType="hashmap">
         select
             ins.field_value as id,
             inst.group_code as type,
             str.full_name as address
             from
             wl_form_instance as ins
             left join wl_form_instance as inst on ins.field_value = inst.instance_id
             left join wl_warehouse_structure as str on ins.instance_id = str.source_id
             where ins.instance_id =#{id}
             and ins.field_name ='parentId'
             group by ins.field_value
    </select>

    <!-- // 需求958 导出模板需要列表获取建筑 by kongfm 2021-09-15-->
    <select id="getAllBuilding" resultType="hashmap">
         select * from (SELECT
        a.instance_id AS instanceId,
        a.group_code AS groupCode,
        a.group_type AS groupType,
        a.field_value AS fieldValue,
        a.field_label AS fieldLabel,
        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 buildName
        FROM
        `wl_form_instance` a
				where a.group_type = 'building'
        GROUP BY
        a.instance_id) sa

        ORDER BY sa.instanceId DESC
    </select>
    
<select id="getBuildingToLongitudeAndLatitude" resultType="hashmap">
SELECT
	max(CASe WHEN field_name = 'latitude' THEN field_value end ) AS latitude,
	max(CASe WHEN field_name = 'longitude' THEN field_value end ) AS longitude
FROM
	wl_form_instance
WHERE
	instance_id = #{instanceId} 
</select>
<!--<select id="getBuildVideoCount" resultType="hashmap">-->
<!--    SELECT-->
<!--        wlv.source_id AS buildId,-->
<!--        count( wlv.source_id ) AS total-->
<!--    FROM-->
<!--        `wl_video_source` wlv-->
<!--    GROUP BY-->
<!--        wlv.source_id-->
<!--</select>-->
<select id="getBuildVideoCount" resultType="hashmap">
 SELECT
        b.instanceId AS buildId,
        COUNT( vs.video_id ) AS total
    FROM
        get_building_tree b
        JOIN wl_video_source vs ON FIND_IN_SET( b.instanceId, vs.parent_source_ids )
        JOIN wl_video v on v.id = vs.video_id
    GROUP BY
        b.instanceId
</select>
<update id ="updateFormInstanceByInstanceAndFieldName">

UPDATE wl_form_instance
SET field_value = #{value}
WHERE
	instance_id =#{instanceId}
AND field_name =#{name}


</update>

    <select id="selectChildListdate" resultType="java.lang.String">
        SELECT distinct a.instance_id FROM

        (select instance_id ,field_value from wl_form_instance  where group_type in('building','room','floor') and field_name='parentId') a
        WHERE  a.field_value = #{id}   union all SELECT distinct b.instance_id FROM

        (select instance_id ,field_value from wl_form_instance  where group_type in('building','room','floor') and field_name='parentId') b
        WHERE  b.field_value in (SELECT distinct c.instance_id FROM

        (select instance_id ,field_value from wl_form_instance  where group_type in('building','room','floor') and field_name='parentId') c
        WHERE  c.field_value = #{id})

    </select>

    <select id="querySpecialChildrenListByBizOrgCode" resultType="java.util.Map">
        SELECT
        *
        FROM
        (
        SELECT
        a.instance_id as id,
        a.instance_id AS instanceId,
        a.group_code AS groupCode,
        a.group_type AS groupType,
        wlss.scene_id as sceneId,
        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 instanceName,
        MAX( CASE WHEN a.field_name = 'name' THEN a.field_value END ) AS name,
        MAX( CASE WHEN a.field_name = 'code' THEN a.field_value END ) AS code,
        MAX( CASE WHEN a.field_name = 'isRisk' THEN a.field_value END ) AS isRisk,
        MAX( CASE WHEN a.field_name = 'address' THEN a.field_value END ) AS address,
        MAX( CASE WHEN a.field_name = 'bizOrgCode' THEN a.field_value END ) AS bizOrgCode,
        MAX( CASE WHEN a.field_name = 'bizOrgName' THEN a.field_value END ) AS bizOrgName
        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>
            <if test="parentId != null and parentId !=''">
                sa.parentId = #{parentId}
            </if>
            <if test="bizOrgCode != null and bizOrgCode !=''">
                and sa.bizOrgCode LIKE CONCAT(#{bizOrgCode},'%')
            </if>
        </where>
    </select>


    <select id="queryVideoCountByBizOrgCode" resultType="java.lang.Long">
        select count(1) from wl_video where biz_org_code like concat(#{bizOrgCode}, '%')
    </select>
    <select id="selectParentBuildId" resultType="java.lang.String">
        SELECT instance_id FROM `wl_form_instance` f  where   f.field_name = 'code'  and f.field_value = #{code}
    </select>
</mapper>
