<?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.ximai.mes.pro.mapper.prototypeRequest.ProPrototypeRequestProcessItemMapper">

    <resultMap type="ProPrototypeRequestProcessItem" id="ProPrototypeRequestProcessItemResult">
        <result property="prototypeRequestProcessItemId" column="prototype_request_process_item_id"/>
        <result property="prototypeRequestProcessId" column="prototype_request_process_id"/>
        <result property="itemId" column="item_id"/>
        <result property="itemName" column="item_name"/>
        <result property="unitOfMeasure" column="unit_of_measure"/>
        <result property="quantity" column="quantity"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectProPrototypeRequestProcessItemVo">
        select pprpi.prototype_request_process_item_id,
               pprpi.prototype_request_process_id,
               pprpi.item_id,
               pprpi.unit_of_measure,
               pprpi.quantity,
               pprpi.remark,
               mi.item_name
        from pro_prototype_request_process_item pprpi
left join md_item mi on pprpi.item_id = mi.item_id
    </sql>

    <select id="selectProPrototypeRequestProcessItemList" parameterType="ProPrototypeRequestProcessItem"
            resultMap="ProPrototypeRequestProcessItemResult">
        <include refid="selectProPrototypeRequestProcessItemVo"/>
        <where>
            <if test="prototypeRequestProcessId != null ">and pprpi.prototype_request_process_id =
                #{prototypeRequestProcessId}
            </if>
            <if test="itemId != null ">and pprpi.item_id = #{itemId}</if>
            <if test="itemName != null and itemName != ''">and mi.item_name = #{itemName}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and pprpi.unit_of_measure = #{unitOfMeasure}</if>
            <if test="quantity != null ">and pprpi.quantity = #{quantity}</if>
        </where>
    </select>

    <select id="selectProPrototypeRequestProcessItemByPrototypeRequestProcessItemId" parameterType="Long"
            resultMap="ProPrototypeRequestProcessItemResult">
        <include refid="selectProPrototypeRequestProcessItemVo"/>
        where pprpi.prototype_request_process_item_id = #{prototypeRequestProcessItemId}
    </select>
    <select id="selectProPrototypeRequestProcessItemByPrototypeRequestProcessId"
            parameterType="Long" resultMap="ProPrototypeRequestProcessItemResult">
        <include refid="selectProPrototypeRequestProcessItemVo"/>
        where pprpi.prototype_request_process_id = #{prototypeRequestProcessId}
    </select>

    <insert id="insertProPrototypeRequestProcessItem" parameterType="ProPrototypeRequestProcessItem"
            useGeneratedKeys="true" keyProperty="prototypeRequestProcessItemId">
        insert into pro_prototype_request_process_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="prototypeRequestProcessId != null">prototype_request_process_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="unitOfMeasure != null">unit_of_measure,</if>
            <if test="quantity != null">quantity,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="prototypeRequestProcessId != null">#{prototypeRequestProcessId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateProPrototypeRequestProcessItem" parameterType="ProPrototypeRequestProcessItem">
        update pro_prototype_request_process_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="prototypeRequestProcessId != null">prototype_request_process_id = #{prototypeRequestProcessId},
            </if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where prototype_request_process_item_id = #{prototypeRequestProcessItemId}
    </update>

    <delete id="deleteProPrototypeRequestProcessItemByPrototypeRequestProcessItemId" parameterType="Long">
        delete
        from pro_prototype_request_process_item
        where prototype_request_process_item_id = #{prototypeRequestProcessItemId}
    </delete>

    <delete id="deleteProPrototypeRequestProcessItemByPrototypeRequestProcessItemIds" parameterType="String">
        delete from pro_prototype_request_process_item where prototype_request_process_item_id in
        <foreach item="prototypeRequestProcessItemId" collection="array" open="(" separator="," close=")">
            #{prototypeRequestProcessItemId}
        </foreach>
    </delete>
</mapper>
