<?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.amos.fas.business.dao.mapper.FireEquipMapper">
    <!--统计-->
    <select id="countPageData" resultType="long">
        SELECT
        count(1) AS total_num
        FROM
        f_fire_equipment a
        left join
        (select b.id,c.fire_equipment_id from f_equipment b, f_equipment_fire_equipment c where c.equipment_id = b.id
        GROUP BY c.fire_equipment_id) sa
        on a.id = sa.fire_equipment_id
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="name!=null">and a.name like concat(concat("%",#{name}),"%")</if>
            <if test="equipType!=null">and a.equip_type = #{equipType}</if>
            <if test="productArea!=null">and a.production_area like concat(concat("%",#{productArea}),"%")</if>
            <if test="code!=null">and a.code = #{code}</if>
            <if test="equipClassify!=null">and a.equip_classify = #{equipClassify}</if>
            <if test="protectObj!=null">and sa.id = #{protectObj}</if>
        </trim>
    </select>
    <!--分页查询 -->
    <select id="getFireEquipPage" resultType="java.util.HashMap">
        SELECT
        a.id,
        a.code,
        a.equip_classify as equipClassify,
        a.equip_type AS equipType,
        a.equip_code AS equipCode,
        a.name,
        a.model,
        a.maintenance_cycle AS maintenanceCycle,
        a.manufacturer,
        a.brand,
        a.production_date AS productionDate,
        a.effective_date AS effectiveDate,
        a.number,
        a.unit,
        a.production_area AS productionArea,
        a.floor3d,
        a.room,
        CASE
        WHEN sa.fire_equipment_id > 0 THEN
        '已绑定设备'
        ELSE
        ''
        END AS isBind
        FROM
        f_fire_equipment a
        LEFT JOIN (
        SELECT
        c.fire_equipment_id
        FROM
        f_equipment b,
        f_equipment_fire_equipment c
        WHERE
        c.equipment_id = b.id
        GROUP BY
        c.fire_equipment_id
        ) sa ON a.id = sa.fire_equipment_id
        <trim prefix="WHERE" prefixOverrides="AND ">
            <if test="name!=null">and a.name like concat(concat("%",#{name}),"%")</if>
            <if test="equipType!=null">and a.equip_type = #{equipType}</if>
            <if test="productArea!=null">and a.production_area like concat(concat("%",#{productArea}),"%")</if>
            <if test="code!=null">and a.code = #{code}</if>
            <if test="equipClassify!=null">and a.equip_classify = #{equipClassify}</if>
            <if test="protectObj!=null">and sa.id = #{protectObj}</if>
        </trim>
        order by a.id
        <choose>
            <when test="pageSize==-1"></when>
            <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
        </choose>
    </select>
    <!--统计-->
    <select id="countFireEquipByEquipIdPageData" resultType="long">
		select
			count(1) AS total_num
		from
		(
		select
		 b.fire_equipment_id,c.`name`
		from f_equipment_fire_equipment b,f_equipment c
		where  b.equipment_id = #{id} and c.id = b.equipment_id
		) sa,
		f_fire_equipment a
		where a.id = sa.fire_equipment_id
	</select>
    <!--分页查询 -->
    <select id="getFFireEquipByEquipIdPage" resultType="java.util.HashMap">
        select
        a.id,
        a.code ,
        a.name ,
        a.manufacturer,
        a.model,
        sa.name as proObj
        from
        (
        select
        b.fire_equipment_id,c.`name`
        from f_equipment_fire_equipment b,f_equipment c
        where b.equipment_id = #{id} and c.id = b.equipment_id
        ) sa,
        f_fire_equipment a
        where a.id = sa.fire_equipment_id
        order by a.id
        <choose>
            <when test="pageSize==-1"></when>
            <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
        </choose>
    </select>

    <select id="queryForFireEquipmentHistoryCount" resultType="long">
        SELECT
        COUNT(1)
        FROM
        (
        SELECT
        det.`name` as fireEquipmentName,
        spe.`code` as fireEquipmentCode,
        (
        SELECT
        group_concat(DISTINCT f.`name`)
        FROM
        f_equipment_fire_equipment efe
        JOIN f_equipment f ON efe.equipment_id = f.id
        WHERE
        efe.fire_equipment_id = ind.equipment_specific_id
        ) equipmentName,
        ind.update_date
        FROM
        wl_equipment_specific_index as ind
        left join wl_equipment_specific as spe on spe.id = ind.equipment_specific_id
        left join wl_equipment_detail as det on spe.equipment_detail_id = det.id
       	WHERE ind.`value` IS NOT NULL AND ind.`value` != ''
        ) d
        <where>
	        <if test="fireEquipmentName!=null">
	            AND d.fireEquipmentName LIKE '%${fireEquipmentName}%'
	        </if>
	        <if test="equipmentName!=null">
	            AND d.equipmentName LIKE '%${equipmentName}%'
	        </if>
	        <if test="startTime!=null">
	            AND d.update_date  BETWEEN '${startTime}' AND '${endTime}'
	        </if>
        </where>
    </select>


    <select id="queryForFireEquipmentHistory" resultType="map">
        SELECT
        d.*
        FROM
        (
        SELECT
        det.`name` as fireEquipmentName,
        spe.`code` as fireEquipmentCode,
        ein.name as eq_point_name,
        CASE ind.value WHEN 'true' THEN '是'
        WHEN 'false' THEN '否'
        ELSE ind.value END 'e_value',
        ein.unit as eq_point_unit,
        CONCAT(IFNULL(str.full_name,''),' ',IFNULL(sto.description,'')) as source_name,
        (
        SELECT
        group_concat(DISTINCT f.`name`)
        FROM
        f_equipment_fire_equipment efe
        JOIN f_equipment f ON efe.equipment_id = f.id
        WHERE
        efe.fire_equipment_id = ind.equipment_specific_id
        ) equipmentName,
        ind.update_date as create_date
        FROM
        wl_equipment_specific_index as ind
        left join wl_equipment_specific as spe on spe.id = ind.equipment_specific_id
        left join wl_equipment_detail as det on spe.equipment_detail_id = det.id
        left join wl_equipment_index as ein on ind.equipment_index_id = ein.id
        left join wl_stock_detail as sto on  sto.equipment_specific_id = spe.id
        left join wl_warehouse_structure as str on sto.warehouse_structure_id = str.source_id
        WHERE ind.`value` IS NOT NULL AND ind.`value` != '' AND ein.is_iot = 1
        ) d
      <where>
	      <if test="fireEquipmentName!=null">
	            AND d.fireEquipmentName LIKE '%${fireEquipmentName}%'
	        </if>
	        <if test="equipmentName!=null">
	            AND d.equipmentName LIKE '%${equipmentName}%'
	        </if>
	        <if test="startTime!=null">
	            AND d.create_date BETWEEN '${startTime}' AND '${endTime}'
	        </if>
      </where>
        ORDER BY d.create_date desc
        LIMIT ${start},${length}
    </select>


<!--    <select id="queryForEquipmentPageCount" resultType="long">-->
<!--        select-->
<!--        count(1)-->
<!--        from f_fire_equipment fe-->
<!--        where-->
<!--        1=1-->
<!--        <if test="equipClassify!=null">-->
<!--            and fe.equip_classify in ( ${equipClassify} )-->
<!--        </if>-->

<!--        <if test="code!=null">-->
<!--            and fe.`code` like '%${code}%'-->
<!--        </if>-->
<!--        <if test="name!=null">-->
<!--            and (fe.`name` like '%${name}%' or fe.`code` like '%${name}%')-->
<!--        </if>-->
<!--        &lt;!&ndash; 筛选未绑定的配套设施-->
<!--         &ndash;&gt;-->
<!--        <if test="bindStation != null and bindStation == 'false'">-->
<!--        	AND (select true from-->
<!--	        	f_fire_station_equipment fse-->
<!--		        left join f_fire_equipment e on fse.fire_equipment_id = e.id-->
<!--	        where fse.fire_equipment_id = fe.id AND e.equip_classify != 1   limit 0,1) is not true-->
<!--        </if>-->
<!--    </select>-->

<!--    <select id="queryForEquipmentPage" resultType="map">-->
<!--        select * from (-->
<!-- select-->
<!--        fe.*, (-->
<!--        SELECT-->
<!--        'YES'-->
<!--        FROM-->
<!--        f_equipment_fire_equipment efe-->
<!--        WHERE-->
<!--        efe.fire_equipment_id = fe.id limit 0,1-->
<!--        ) isBind,-->
<!--        frs.name riskSourceName,-->
<!--        (select true from-->
<!--        	f_fire_station_equipment fse-->
<!--        	left join f_fire_equipment e on fse.fire_equipment_id = e.id-->
<!--        where fse.fire_equipment_id = fe.id AND e.equip_classify != 1  limit 0,1) as bindStation-->
<!--        from f_fire_equipment fe-->
<!--        left join f_risk_source frs on frs.id = fe.risk_source_id-->
<!--				where 1=1) tmp-->
<!--        <where>-->
<!--        <if test="equipClassify!=null">-->
<!--            and tmp.equip_classify in ( ${equipClassify} )-->
<!--        </if>-->

<!--        <if test="code!=null">-->
<!--            and tmp.`code` like '%${code}%'-->
<!--        </if>-->
<!--        <if test="name!=null">-->
<!--            and (tmp.`name` like '%${name}%' or tmp.`code` like '%${name}%')-->
<!--        </if>-->
<!--        &lt;!&ndash; 筛选未绑定的配套设施-->
<!--         &ndash;&gt;-->
<!--        <if test="bindStation != null and bindStation == 'false'">-->
<!--        	AND tmp.bindStation is not true-->
<!--        </if>-->
<!--        </where>-->
<!--        LIMIT ${start},${length}-->
<!--    </select>-->


<!--    <select id="queryForGroupCount" resultType="map">-->
<!--		SELECT-->
<!--		fe.`name`,-->
<!--		 fe.unit,-->
<!--		 count(1) fireEquipmentCount-->
<!--		FROM-->
<!--			f_fire_equipment fe-->
<!--		WHERE-->
<!--			EXISTS (-->
<!--				SELECT-->
<!--					1-->
<!--				FROM-->
<!--					f_fire_station_equipment se-->
<!--				WHERE-->
<!--					se.fire_station_id = ${fireStationId}-->
<!--				AND se.fire_equipment_id = fe.id-->
<!--			)-->
<!--	</select>-->
	
	

<!--    <select id="findEquipDetailsById" resultType="com.yeejoin.amos.fas.core.common.response.EquipDetailsResponse">-->
<!--        SELECT-->
<!--        eq.`name`,-->
<!--        eq.`code`,-->
<!--        eq.address,-->
<!--        fs.`name` stationName,-->
<!--        eq.charge_user_id userId-->
<!--        FROM-->
<!--        f_equipment eq-->
<!--        LEFT JOIN f_fire_station fs ON fs.id = eq.fire_station_id-->
<!--        <where>-->
<!--        <if test="id!=null">AND eq.id = #{id}</if>-->
<!--        </where>-->
<!--    </select>-->
 
    <select id="queryStorageEquips" resultType="map">
        SELECT
        wed.id,
        wed.`name`,
        u.`name` AS unit,
        sum(wsd.amount) fireEquipmentCount
        FROM
        wl_stock_detail wsd
        LEFT JOIN wl_equipment_detail wed ON wed.id = wsd.equipment_detail_id
        LEFT JOIN wl_equipment we ON we.id = wed.equipment_id
        LEFT JOIN wl_unit u ON u.id = we.unit_id
        LEFT JOIN wl_warehouse_structure ws ON ws.id = wsd.warehouse_structure_id
        <where>
            <if test="riskSourceId != null">
                AND ws.source_id = #{riskSourceId}
            </if>
        </where>
        GROUP BY
        wsd.equipment_detail_id,
        wed.`name`
    </select>

<!--    <select id="findEquipDetailsById" resultType="com.yeejoin.amos.fas.core.common.response.EquipDetailsResponse">-->
<!--        SELECT-->
<!--        eq.`name`,-->
<!--        eq.`code`,-->
<!--        eq.address,-->
<!--        fs.`name` stationName,-->
<!--        eq.charge_user_id userId-->
<!--        FROM-->
<!--        f_equipment eq-->
<!--        LEFT JOIN f_fire_station fs ON fs.id = eq.fire_station_id-->
<!--        <where>-->
<!--        <if test="id!=null">AND eq.id = #{id}</if>-->
<!--        </where>-->
<!--    </select>-->


<!--    <select id="findOneByPointCode" resultType="com.yeejoin.amos.fas.business.vo.EquipCommunicationData">-->
<!--	   SELECT-->
<!--			p.`code` pointCode,-->
<!--			p.`name` pointName,-->
<!--			e.id fireEquipmentId,-->
<!--			e.`name` fireEquipmentName,-->
<!--			e.code fireEquipmentCode,-->
<!--			p.type,-->
<!--			d.dict_value alarmType,-->
<!--			p.unit-->
<!--		FROM-->
<!--			f_fire_equipment_point p-->
<!--		LEFT JOIN f_fire_equipment e ON e.id = p.fire_equipment_id-->
<!--		LEFT JOIN f_dict d on d.id = p.alarm_type-->
<!--		where p.`code` = #{code}-->
<!--	</select>-->

    <select id="listByCodes" resultType="com.yeejoin.amos.fas.business.entity.mybatis.FireEquipmentEntity">
        select
        id,
        code,
        name
        from
        f_fire_equipment
        where
        code in
        <foreach collection="codes" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="listByType" resultType="com.yeejoin.amos.fas.business.entity.mybatis.FireEquipmentEntity">
        select
        id,
        code,
        name
        from
        f_fire_equipment
        <where>
            <if test="type!=null">
                equip_classify = #{type}
            </if>
        </where>
    </select>

    <insert id="saveBatch" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.fas.business.param.FireEquipmentParam"
            useGeneratedKeys="true">
        insert into f_fire_equipment
        (
        org_code,
        code,
        equip_classify,
        equip_type,
        position3d,
        floor3d,
        is_indoor,
        name,
        maintenance_cycle,
        manufacturer,
        number,
        unit,
        room,
        equip_status,
        weight,
        risk_source_id
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.orgCode},
            #{item.code},
            #{item.equipClassify},
            #{item.equipType},
            #{item.position3d},
            #{item.floor3d},
            #{item.isIndoor},
            #{item.name},
            #{item.maintenanceCycle},
            #{item.manufacturer},
            #{item.number},
            #{item.unit},
            #{item.room},
            #{item.equipStatus},
            #{item.weight},
            #{item.riskSourceId}
            )
        </foreach>
    </insert>

    <update id="updateBatch">
        <foreach collection="list" item="item" index="index" separator=";">
            update f_fire_equipment
            <set>
                <if test="item.orgCode != null">
                    org_code = #{item.orgCode},
                </if>
                <if test="item.code != null">
                    code = #{item.code},
                </if>
                <if test="item.equipClassify != null">
                    equip_classify = #{item.equipClassify},
                </if>
                <if test="item.equipType != null">
                    equip_type = #{item.equipType},
                </if>
                <if test="item.position3d != null">
                    position3d = #{item.position3d},
                </if>
                <if test="item.floor3d != null">
                    floor3d = #{item.floor3d},
                </if>
                <if test="item.isIndoor != null">
                    is_indoor = #{item.isIndoor},
                </if>
                <if test="item.name != null">
                    name = #{item.name},
                </if>
                <if test="item.maintenanceCycle != null">
                    maintenance_cycle = #{item.maintenanceCycle},
                </if>
                <if test="item.manufacturer != null">
                    manufacturer = #{item.manufacturer},
                </if>
                <if test="item.number != null">
                    number = #{item.number},
                </if>
                <if test="item.unit != null">
                    unit = #{item.unit},
                </if>
                <if test="item.room != null">
                    room = #{item.room},
                </if>
                <if test="item.equipStatus != null">
                    equip_status = #{item.equipStatus},
                </if>
                <if test="item.weight != null">
                    weight = #{item.weight},
                </if>
                <if test="item.riskSourceId != null">
                    risk_source_id = #{item.riskSourceId},
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <select id="queryEquimentRelation" resultType="java.util.Map">
        SELECT
        fe.id,
        fe.`code` fCode,
        fe.`name` fName
        FROM
        f_equipment_fire_equipment efe
        JOIN f_fire_equipment fe ON efe.fire_equipment_id = fe.id
        WHERE
        efe.equipment_id = ${equipmentId}
        <if test="fname != null">
            AND fe.`name` like '%${fname}%'
        </if>
        <if test="length > 0">
            LIMIT ${start},${length} ;
        </if>

    </select>


    <select id="queryEquimentRelationCount" resultType="long">
        SELECT
        count(1)
        FROM
        f_equipment_fire_equipment efe
        JOIN wl_equipment_specific fe ON efe.fire_equipment_id = fe.id
        WHERE
        efe.equipment_id = ${equipmentId}
        <if test="fname != null">
            AND fe.`name` like '%${fname}%'
        </if>
    </select>

    <select id="findById" resultType="com.yeejoin.amos.fas.business.entity.mybatis.FireEquipmentEntity">
        select name from f_fire_equipment where id= #{id}
    </select>

    <select id="removeIfmeaPointByFireEquipIdAndEquipId" resultType="long">
        DELETE fmep FROM `f_fmea_equipment_point` fmep
			LEFT JOIN wl_equipment_specific_index esi ON esi.id = fmep.equipment_point_id
			WHERE esi.equipment_specific_id = #{fireEquipmentId} AND fmep.important_equipment_id = #{importantEquipmentId}
    </select>

    <select id="findFireEquipById" resultType="com.yeejoin.amos.fas.business.bo.FireEquipmentBo">
        SELECT
        spec.id AS id,
        spec.org_code AS org_code,
        spec.CODE AS CODE,
        detail.CODE AS equip_code,
        detail.NAME AS NAME,
        detail.standard AS model,
        detail.maintenance_cycle AS maintenance_cycle,
        manufatcturer.NAME AS manufacturer,
        detail.brand AS detail,
        detail.production_date AS production_date,
        sd.amount AS number,
        unit.NAME AS unit,
        '' AS position3d,
        '' AS floor3d,
        '' AS is_indoor,
        '' AS room,
        detail.remark,
        detail.creator_id AS create_by,
        spec.create_date AS create_date,
        '1' equip_status,
        detail.video AS video_url,
        warehouse.source_id AS risk_source_id,
        warehouse.full_name AS production_area,
        (
        SELECT
        GROUP_CONCAT( fe.NAME )
        FROM
        f_equipment_fire_equipment ffe
        LEFT JOIN f_equipment fe ON fe.id = ffe.equipment_id
        WHERE
        ffe.fire_equipment_id = spec.id
        ) protectObjNames
        FROM
        wl_equipment_specific spec
        LEFT JOIN wl_equipment_detail detail ON detail.id = spec.equipment_detail_id
        LEFT JOIN wl_stock_detail sd ON sd.equipment_specific_id = spec.id
        LEFT JOIN wl_manufacturer_info manufatcturer ON manufatcturer.id = detail.manufacturer_id
        LEFT JOIN wl_equipment equipment ON equipment.id = detail.equipment_id
        LEFT JOIN wl_unit unit ON equipment.unit_id = unit.id
        LEFT JOIN wl_warehouse_structure warehouse ON warehouse.id = sd.warehouse_structure_id
        WHERE
        <if test="id != null">
            spec.id = #{id}
        </if>
    </select>
    <select id="findVideoById" resultType="com.yeejoin.amos.fas.business.bo.FireEquipmentBo">
        SELECT
        risk.*,
        video.id AS id,
        video.org_code AS org_code,
        video.CODE AS CODE,
        video.CODE AS equip_code,
        video.NAME AS NAME,
        '' AS model,
        '' AS maintenance_cycle,
        '' AS manufatcturer,
        '' AS detail,
        video.create_date AS production_date,
        '' AS number,
        '' AS unit,
        video.position3d AS position3d,
        risk.floor3d AS floor3d,
        risk.is_indoor AS is_indoor,
        '' AS room,
        video.remark,
        video.creator_id AS create_by,
        video.create_date AS create_date,
        '1' equip_status,
        video.url AS video_url,
        source.source_id AS risk_source_id,
        risk.name AS production_area,
        (
        SELECT
        GROUP_CONCAT( fe.NAME )
        FROM
        wl_video_important_equipment ffe
        LEFT JOIN f_equipment fe ON fe.id = ffe.important_equipment_id
        WHERE
        ffe.video_id = video.id
        ) protectObjNames
        FROM wl_video video
        LEFT JOIN wl_video_source source ON source.video_id =video.id
        LEFT JOIN f_risk_source risk ON risk.id = source.source_id
        WHERE
        <if test="id != null">
            video.id = #{id}
        </if>
    </select>
    
<!--    <select id="findFireStationById" resultType="com.yeejoin.amos.fas.dao.entity.FireStation">-->
<!--        SELECT-->
<!--            *-->
<!--        FROM-->
<!--            (-->
<!--                SELECT-->
<!--              a.instance_id AS id,-->
<!--                    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-->
<!--                FROM-->
<!--                    `wl_form_instance` a-->
<!--        WHERE-->
<!--            a.group_code IN ('pool','r_pool','fireFoamRoom', 'r_fireFoamRoom','fireRoom','r_fireRoom')-->
<!--                GROUP BY-->
<!--                    a.instance_id-->
<!--            ) s,-->
<!--        f_risk_source r-->
<!--        where s.id = r.source_id and s.id = #{id}-->
<!--    </select>-->

    <!-- <select id="findFireStationById" resultType="java.util.Map">
       SELECT
			wb.`name`,
			wb.`code`,
			wb.address,
			wb.region_id AS riskSourceId,
			wb.height,
            wb.low,
            wb.design_capacity
		FROM
			wl_building wb
		WHERE
			wb.id = #{id}
    </select> -->
</mapper>