<?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.productionRequest.ProProductionRequestProcessItemMapper">

    <resultMap type="ProProductionRequestProcessItem" id="ProProductionRequestProcessItemResult">
        <result property="productionRequestProcessItemId"    column="production_request_process_item_id"    />
        <result property="productionRequestProcessId"    column="production_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="selectProProductionRequestProcessItemVo">
        select production_request_process_item_id, production_request_process_id, t1.item_id, t1.unit_of_measure, quantity, t1.remark , t2.item_name
        from pro_production_request_process_item t1 left join md_item t2 on t1.item_id = t2.item_id
    </sql>

    <select id="selectProProductionRequestProcessItemList" parameterType="ProProductionRequestProcessItem" resultMap="ProProductionRequestProcessItemResult">
        <include refid="selectProProductionRequestProcessItemVo"/>
        <where>
            <if test="productionRequestProcessId != null "> and production_request_process_id = #{productionRequestProcessId}</if>
            <if test="itemId != null "> and item_id = #{itemId}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''"> and unit_of_measure = #{unitOfMeasure}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
        </where>
    </select>

    <select id="selectProProductionRequestProcessItemByProductionRequestProcessItemId" parameterType="Long" resultMap="ProProductionRequestProcessItemResult">
        <include refid="selectProProductionRequestProcessItemVo"/>
        where production_request_process_item_id = #{productionRequestProcessItemId}
    </select>

    <insert id="insertProProductionRequestProcessItem" parameterType="ProProductionRequestProcessItem" useGeneratedKeys="true" keyProperty="productionRequestProcessItemId">
        insert into pro_production_request_process_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productionRequestProcessId != null">production_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="productionRequestProcessId != null">#{productionRequestProcessId},</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="updateProProductionRequestProcessItem" parameterType="ProProductionRequestProcessItem">
        update pro_production_request_process_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="productionRequestProcessId != null">production_request_process_id = #{productionRequestProcessId},</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 production_request_process_item_id = #{productionRequestProcessItemId}
    </update>

    <delete id="deleteProProductionRequestProcessItemByProductionRequestProcessItemId" parameterType="Long">
        delete from pro_production_request_process_item where production_request_process_item_id = #{productionRequestProcessItemId}
    </delete>

    <delete id="deleteProProductionRequestProcessItemByProductionRequestProcessId" parameterType="Long">
        delete from pro_production_request_process_item where production_request_process_id = #{productionRequestProcessId}
    </delete>

    <delete id="deleteProProductionRequestProcessItemByProductionRequestId" parameterType="Long">
        delete from pro_production_request_process_item where production_request_process_id in
        (select production_request_process_id from pro_production_request_process where production_request_id = #{productionRequestId})
    </delete>

    <delete id="deleteProProductionRequestProcessItemByProductionRequestProcessItemIds" parameterType="String">
        delete from pro_production_request_process_item where production_request_process_item_id in
        <foreach item="productionRequestProcessItemId" collection="array" open="(" separator="," close=")">
            #{productionRequestProcessItemId}
        </foreach>
    </delete>
</mapper>
