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

    <delete id="deleteByFmeaIdAndPointInputitemIds">
        delete from
            f_fmea_point_inputitem
        where
            fmea_id = #{fmeaId}
        and
            point_inputitem_id in
        <foreach collection="pointInputitemIds" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>

    <delete id="deleteByFmeaIds">
        delete from f_fmea_point_inputitem 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.FmeaPointInputitem"
            useGeneratedKeys="true">
        insert into f_fmea_point_inputitem
        (
        fmea_id,
        point_inputitem_id,
        state
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.fmeaId},
            #{item.pointInputitemId},
            #{item.state}
            )
        </foreach>
    </insert>

    <select id="countByFmeaId" resultType="long">
        select
            count(1)
        from
        (
            select
                b.point_id
            from
                f_fmea_point_inputitem a
            inner join
                p_point_inputitem as b on b.id = a.point_inputitem_id
            left join
                p_input_item as pii on pii.id = b.input_item_id
            left join
                p_point as pp on pp.id = b.point_id
            where
                a.fmea_id = #{fmeaId}
            group by b.point_id
        ) as d
    </select>

    <select id="listPointInputitemIdsByPointId" resultType="java.lang.Long">
        select id from p_point_inputitem where point_id = #{pointId}
    </select>

    <select id="listByFmeaId" resultType="java.util.Map">
        select
            a.fmea_id as fmeaId,
            pp.id as pointId,
            pp.point_no as pointNo,
            pp.name as pointName,
            pp.charge_person_id as userId,
            pp.charge_dept_id as deptId,
            group_concat(concat(pii.name, '##', a.state) SEPARATOR <![CDATA[ '\n' ]]>) as inputItems
        from
            f_fmea_point_inputitem a
        inner join
            p_point_inputitem as b on b.id = a.point_inputitem_id
        left join
            p_input_item as pii on pii.id = b.input_item_id
        left join
            p_point as pp on pp.id = b.point_id
        where
            a.fmea_id = #{fmeaId}
        group by b.point_id
        limit #{offset},#{limit}
    </select>

    <select id="listByPointIdAndInputItemIds" resultType="com.yeejoin.amos.fas.dao.entity.FmeaPointInputitem">
        select
            a.id,
            a.fmea_id as fmeaId,
            a.point_inputitem_id as pointInputitemId,
            a.state,
            b.input_item_id as inputItemId,
            c.risk_source_id
        from
            f_fmea_point_inputitem as a
        left join
            p_point_inputitem as b on a.point_inputitem_id = b.id
        left join
            f_fmea as c on a.fmea_id = c.id
        where
            b.point_id = #{pointId}
        and
            b.id in
            <foreach collection="inputIds" item="item" index="index" separator="," open="(" close=")">
                #{item}
            </foreach>
    </select>

    <select id="listFmeaByFmeaId" resultType="com.yeejoin.amos.fas.dao.entity.FmeaPointInputitem">
        select
            id,
            fmea_id as fmeaId,
            point_inputitem_id as pointInputitemId,
            state
        from
            f_fmea_point_inputitem
        where
            fmea_id = #{fmeaId}
    </select>

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

    <select id="listByPointInputItemId" resultType="com.yeejoin.amos.fas.dao.entity.FmeaPointInputitem">
        SELECT
            *
            FROM
                f_fmea_point_inputitem ffpi
            WHERE ffpi.point_inputitem_id = #{pointInputItemId}
    </select>
</mapper>