<?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.EquipmentSpecificMapper">

    <insert id="saveBatch" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.fas.dao.entity.FmeaEquipmentPoint"
            useGeneratedKeys="true">
        insert into f_fmea_equipment_point
        (
        fmea_id,
        important_equipment_id,
        equipment_point_id,
        create_date
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.fmeaId},
            #{item.importantEquipmentId},
            #{item.equipmentPointId},
            now()
            )
        </foreach>
    </insert>
    <delete id="deletPointBuFmeaId">
        delete from f_fmea_equipment_point where fmea_id = #{fmeaId} and important_equipment_id =#{importantEquipId}
    </delete>
    <select id="getEquipmentBySpe" resultType="com.yeejoin.amos.fas.business.vo.EquipmentSpecificVo">
        select
        spe.id as id ,
        equ.code as code,
        det.name as name,
        cate.name as type,
        spe.is_binding as isbind,
        concat_ws('-',ware.full_name, det.area) as address,
        sto.amount as amount,
        spe.single as single,
        spe.iot_code,
        spe.code as specific_code,
        spe.system_id,
        (select group_concat(manage.name) FROM `f_fire_fighting_system` as manage where find_in_set(manage.id, spe.system_id)) as system_name
        from
        (
            SELECT amount,
            (SELECT equipment_specific_id FROM wl_stock_detail WHERE stock_id = stk.id limit 1) AS equipment_specific_id,
            (SELECT warehouse_structure_id FROM wl_stock_detail WHERE stock_id = stk.id limit 1) AS warehouse_structure_id,
            (SELECT equipment_detail_id FROM wl_stock_detail WHERE stock_id = stk.id limit 1) AS equipment_detail_id
            FROM wl_stock stk
        ) AS sto
        left join wl_equipment_specific as spe on sto.equipment_specific_id = spe.id
        <if test="equipmentId != null and equipmentId!='null' ">
            left join f_equipment_fire_equipment as fire on spe.id = fire.fire_equipment_id
        </if>
        left join wl_warehouse_structure as ware on sto.warehouse_structure_id  = ware.source_id
        left join wl_equipment_detail as det on sto.equipment_detail_id = det.id
        left join wl_equipment as equ on det.equipment_id = equ.id
        left join wl_equipment_category as cate on equ.category_id = cate.id
        left join wl_stock_detail as wsd on wsd.equipment_detail_id = det.id
        where sto.amount <![CDATA[>]]> 0 and wsd.status != 7
        and  spe.id is not null
        <if test="name != null and name!='null' ">
            and (det.name like CONCAT('%',#{name},'%') or ware.full_name like CONCAT('%',#{name},'%') or spe.iot_code like CONCAT('%',#{name},'%') or spe.code like CONCAT('%',#{name},'%'))
        </if>
        <if test="code != null and code!='null' ">
            and cate.code  like #{code}
        </if>
        <if test="equipmentId != null and equipmentId!='null' ">
            and fire.equipment_id  = #{equipmentId}
        </if>
        <if test="equipmentIdList != null and equipmentIdList.size > 0 ">
            and equ.category_id  in
            <foreach collection="equipmentIdList" item="equipmentId" open="(" close=")" separator=",">
                #{equipmentId}
            </foreach>
        </if>
         limit #{pageNumber},#{pageSize}
    </select>

    <select id="getEquipmentBySpeCount" resultType="int">select
        count(1)
        from
        (
            SELECT amount,
            (SELECT equipment_specific_id FROM wl_stock_detail WHERE stock_id = stk.id limit 1) AS equipment_specific_id,
            (SELECT warehouse_structure_id FROM wl_stock_detail WHERE stock_id = stk.id limit 1) AS warehouse_structure_id,
            (SELECT equipment_detail_id FROM wl_stock_detail WHERE stock_id = stk.id limit 1) AS equipment_detail_id
            FROM wl_stock stk
        ) AS sto
        left join wl_equipment_specific as spe on sto.equipment_specific_id = spe.id
        <if test="equipmentId != null and equipmentId!='null' ">
            left join f_equipment_fire_equipment as fire on spe.id = fire.fire_equipment_id
        </if>
        left join wl_warehouse_structure as ware on sto.warehouse_structure_id  = ware.source_id
        left join wl_equipment_detail as det on sto.equipment_detail_id = det.id
        left join wl_equipment as equ on det.equipment_id = equ.id
        left join wl_equipment_category as cate on equ.category_id = cate.id
        left join wl_stock_detail as wsd on wsd.equipment_detail_id = det.id
        where sto.amount <![CDATA[>]]> 0 and wsd.status != 7
        and spe.id is not null
        <if test="name != null and name!='null' ">
            and (det.name like CONCAT('%',#{name},'%') or ware.full_name like CONCAT('%',#{name},'%') or spe.iot_code like CONCAT('%',#{name},'%') or spe.code like CONCAT('%',#{name},'%'))
        </if>
        <if test="code != null and code!='null' ">
            and cate.code  like #{code}
        </if>
        <if test="equipmentId != null and equipmentId!='null' ">
            and fire.equipment_id  = #{equipmentId}
        </if>
        <if test="equipmentIdList != null and equipmentIdList.size > 0 ">
            and equ.category_id  in
            <foreach collection="equipmentIdList" item="equipmentId" open="(" close=")" separator=",">
                #{equipmentId}
            </foreach>
        </if>
    </select>
    <select id="getFireEquiments" resultType="com.yeejoin.amos.fas.business.vo.EquipmentSpeVo">
        select DISTINCT
        sto.equipment_specific_id as id,
        spec.code as f_code,
        det.name as f_name
        from
        wl_stock_detail as sto
        left join wl_equipment_detail as det on sto.equipment_detail_id = det.id
        left join wl_equipment_specific  as spec on spec.equipment_detail_id = det.id
        left join wl_equipment as equ on det.equipment_id = equ.id
        left join f_equipment_fire_equipment as fire on sto.equipment_specific_id = fire.fire_equipment_id
        where sto.amount <![CDATA[>]]> 0
        <if test="fname != null and fname!='null' ">
            and det.name like CONCAT('%',#{fname},'%' )
        </if>
        <if test="equipmentId != null and equipmentId!='null' ">
            and fire.equipment_id  = #{equipmentId}
        </if>
    </select>
    <select id="getBindEquipment" resultType="com.yeejoin.amos.fas.business.vo.EquipmentPointVo">
        select
            speind.id,
            equindex.name,
            (
            select
            case when count(1) > 0 then 'bound' else 'noBound' end
            from
            f_fmea_equipment_point as d
            where
            d.equipment_point_id = speind.id
            and
            d.fmea_id = #{fmeaId}
            and
            d.important_equipment_id = #{importantEquipId}  ) isBound
            from
            f_equipment_fire_equipment as fire
            left join wl_stock_detail as sto on fire.fire_equipment_id = sto.equipment_specific_id
            left join wl_equipment_specific as spe on sto.qr_code  = spe.qr_code
            left join wl_equipment_specific_index as speind on sto.equipment_specific_id = speind.equipment_specific_id
            left join wl_equipment_index as equindex on speind.equipment_index_id = equindex.id
            where equindex.type_code ='BREAKDOWN'
            <if test="importantEquipId != null  ">
                and fire.equipment_id = #{importantEquipId}
            </if>
            <if test="equimentId != null  ">
                and fire.fire_equipment_id =#{equimentId}
            </if>
            <if test="equipmentPointName != null and equipmentPointName!='null' ">
                and equindex.name like  CONCAT('%',#{equipmentPointName},'%' )
            </if>
    </select>
    <select id="getAssoEquips" resultType="com.yeejoin.amos.fas.business.vo.AssoEquipsVo">
             select DISTINCT
                 sto.id as equipmentId,
                 spe.code as equipmentCode,
                 det.name as equipmentName ,
                 fqe.name as importantEquipName ,
                 (select group_concat(concat( name, '##', state )) from  wl_equipment_index as equindex where equip.id = equindex.equipment_id and type_code = 'BREAKDOWN'
                 <if test="indexIds.size > 0">
                     AND equindex.id IN
                         <foreach collection="indexIds" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                         </foreach>
                 </if>
                 group by equindex.equipment_id) as equipmentPointNames,
                 poin.important_equipment_id as importantEquipId,
                 fmea_id as  fmeaId
             from
                 f_fmea_equipment_point as poin
                 left join f_equipment as fqe on poin.important_equipment_id = fqe.id
                 left join wl_equipment_specific_index as speind on poin.equipment_point_id = speind.id
                 left join wl_equipment_specific as spe on speind.equipment_specific_id = spe.id
                 left join wl_stock_detail as sto on sto.qr_code = spe.qr_code
                 left join wl_equipment_detail as det on  spe.equipment_detail_id = det.id
                 left join wl_equipment as equip on det.equipment_id = equip.id
                <if test="fmeaId != null  ">
                    where fmea_id = #{fmeaId}
                </if>
                 limit #{pageNumber},#{pageSize}
    </select>

    <select id="findEquipNameById" resultType="String">
        select
            wed.name
        from
            wl_equipment_specific wes
        left join
            wl_equipment_detail wed ON wed.id = wes.equipment_detail_id
        where
            wes.id=#{id}
    </select>

    <select id="getOneById" resultType="com.yeejoin.amos.fas.business.vo.EquipmentSpecificForRiskVo">
        select
            wes.id as id,
            wed.name as name,
            wes.equipment_detail_id as equipmentDetailId,
            wes.qr_code as qrCode,
            wes.code as code,
            wes.org_code as orgCode
        from
            wl_equipment_specific wes
        left join
            wl_equipment_detail wed ON wed.id = wes.equipment_detail_id
        where
            wes.id=#{id}
    </select>
    <select id="queryFireMonitor" resultType="map">
        SELECT
            GROUP_CONCAT( es.id ) ids,
            GROUP_CONCAT( es.`code` ) codes
        FROM
            `f_equipment_fire_equipment` fe
        LEFT JOIN wl_equipment_specific es ON fe.fire_equipment_id = es.id
        LEFT JOIN wl_equipment_detail ed ON es.equipment_detail_id = ed.id
        <where>
            <if test="equipmentId !=null and equipmentId != ''">
                AND fe.equipment_id = #{equipmentId}
            </if>
            <if test="code !=null and code != ''">
                AND ed.`code` LIKE CONCAT(#{code},'%')
            </if>
        </where>
    </select>

    <select id="getOneByQrcode" resultType="java.lang.Integer">
        select count(1)
        from
            wl_scrap_detail as wsd
        left join wl_stock_detail as stdet on wsd.stock_detail_id = stdet.id
        where
        stdet.qr_code = #{qrCode}
        and
        stdet.status = 7
    </select>

    <select id="getSpecificById" resultType="com.yeejoin.amos.fas.dao.entity.EquipmentSpecific">
        SELECT
        *
        FROM
        wl_equipment_specific wes
        WHERE wes.id=#{id}
    </select>

    <select id="getIndexByIndexId" resultType="Long">
        SELECT
        equipment_index_id
        FROM
        wl_equipment_specific_index wesi
        WHERE wesi.id=#{indexId}
        limit 1
    </select>
</mapper>