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

    <delete id="deleteByFmeaIdAndEquipmentPointIds">
        delete from
        f_fmea_equipment_point
        where
        fmea_id = #{fmeaId}
        and important_equipment_id = #{importantEquipId}
        and
        equipment_point_id in
        <foreach collection="equipmentPointIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>

    <delete id="deleteByFmeaIds">
        delete from f_fmea_equipment_point where fmea_id in
        <foreach collection="array" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>

    <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
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.fmeaId},
            #{item.importantEquipmentId},
            #{item.equipmentPointId}
            )
        </foreach>
    </insert>

    <select id="countByFmeaId" resultType="long">
        select
            count(1)
        from
        (
            select
                c.fmea_id as fmeaId
            from
                wl_equipment_specific as a
            left join
                wl_equipment_specific_index as b on b.equipment_specific_id = a.id
            left join
                f_fmea_equipment_point as c on c.equipment_point_id = b.id
            where
                c.fmea_id = #{fmeaId}
            group by b.equipment_specific_id
        ) as d
    </select>

    <select id="listByFmeaId" resultType="java.util.Map">
        select
            c.fmea_id as fmeaId,
            a.id as equipmentId,
            group_concat(concat( ei.name, '##', c.state ) SEPARATOR <![CDATA[ '\n' ]]> ) as equipmentPointNames,
            ed.name as equipmentName,
            a.code as equipmentCode,
            d.name AS importantEquipName,
            d.id AS importantEquipId
        from
            wl_equipment_specific as a
        left join
            wl_equipment_detail as ed on ed.id = a.equipment_detail_id
        left join
            wl_equipment_specific_index as b on b.equipment_specific_id = a.id
        left join
        	wl_equipment_index as ei on b.equipment_index_id = ei.id
        left join
            f_fmea_equipment_point as c on c.equipment_point_id = b.id
        LEFT JOIN f_equipment AS d ON d.id = c.important_equipment_id
        where
            c.fmea_id = #{fmeaId}
        group by b.equipment_specific_id,c.important_equipment_id
        limit #{offset},#{limit}
    </select>

    <select id="countEquipmentPoint" resultType="long">
        select
            count(1)
        from
            wl_equipment_specific_index as a
        where
            a.equipment_specific_id = #{equipmentId}
    </select>

    <select id="listEquipmentPointIdsByEquipmentId" resultType="java.lang.Long">
        select id from wl_equipment_specific_index where equipment_specific_id = #{equipmentId}
    </select>

    <select id="listByEquipmentPointId" resultType="com.yeejoin.amos.fas.dao.entity.FmeaEquipmentPoint">
     SELECT
        point.id,
        point.fmea_id AS fmeaId,
        point.equipment_point_id AS equipmentPointId,
        point.state
    FROM
	    f_fmea_equipment_point point
	LEFT JOIN wl_equipment_specific_index equipment ON equipment.id = point.equipment_point_id
    where
        equipment.equipment_index_id = #{equipmentPointId}
    </select>

    <select id="listEquipmentPoint" resultType="java.util.Map">
        select
            a.id,
            ei.name,
            ei.name_key as code,
            (
				select
					case when count(1) > 0 then 'bound' else 'noBound' end
				from
					f_fmea_equipment_point d
				where
					d.equipment_point_id = a.id
				and
					d.fmea_id = #{fmeaId}
                and
                    d.important_equipment_id = #{importantEquipId}
			) isBound
        from
            wl_equipment_specific_index as a
            left join wl_equipment_index ei on ei.id = a.equipment_index_id
        <where>
        	ei.type_code = 'BREAKDOWN'
            <if test="equipmentId!=null">
                and a.equipment_specific_id = #{equipmentId}
            </if>
            <if test="equipmentPointName!=null and equipmentPointName!=''">
                and ei.name like concat("%",#{equipmentPointName},"%")
            </if>
        </where>
    </select>

    <select id="listFmeaByFmeaId" resultType="com.yeejoin.amos.fas.dao.entity.FmeaEquipmentPoint">
        select
            id,
            fmea_id as fmeaId,
            equipment_point_id as equipmentPointId,
            state
        from
            f_fmea_equipment_point
        where
            fmea_id = #{fmeaId}
    </select>

    <update id="updateStateByIds">
        update
            f_fmea_equipment_point
        set
            state = #{state}
        where
            id in
            <foreach collection="ids" item="item" index="index" separator="," open="(" close=")">
                #{item}
            </foreach>
    </update>

    <select id="listFmeaByEquipIndexIdAndEquipId" resultType="com.yeejoin.amos.fas.dao.entity.FmeaEquipmentPoint">
        select
            id,
            fmea_id as fmeaId,
            equipment_point_id as equipmentPointId,
            state,
            important_equipment_id as importantEquipmentId
        from
            f_fmea_equipment_point
        where
            equipment_point_id = #{specificIndexId} and important_equipment_id = #{equipId}
    </select>

    <select id="getOneByFmeaId" resultType="com.yeejoin.amos.fas.dao.entity.FmeaEquipmentPoint">
        select
            id,
            fmea_id as fmeaId,
            equipment_point_id as equipmentPointId,
            state,
            important_equipment_id as importantEquipmentId
        from
            f_fmea_equipment_point
        where
            fmea_id = #{fmeaId}
    </select>

</mapper>