<?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.productionMake.ProProductionMakeProcessItemMapper">

    <resultMap type="ProProductionMakeProcessItem" id="ProProductionMakeProcessItemResult">
        <result property="productionMakeProcessItemId"    column="production_make_process_item_id"    />
        <result property="productionMakeProcessId"    column="production_make_process_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="unitOfMeasure"    column="unit_of_measure"    />
        <result property="quantity"    column="quantity"    />
        <result property="remark"    column="remark"    />
        <result property="itemName"    column="item_name"    />
    </resultMap>

    <sql id="selectProProductionMakeProcessItemVo">
        select t1.*,t2.item_name from pro_production_make_process_item t1 left join md_item t2 on t1.item_id = t2.item_id
    </sql>

    <select id="selectProProductionMakeProcessItemList" parameterType="ProProductionMakeProcessItem" resultMap="ProProductionMakeProcessItemResult">
        <include refid="selectProProductionMakeProcessItemVo"/>
        <where>
            <if test="productionMakeProcessId != null "> and production_make_process_id = #{productionMakeProcessId}</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="selectProProductionMakeProcessItemByProductionMakeProcessItemId" parameterType="Long" resultMap="ProProductionMakeProcessItemResult">
        <include refid="selectProProductionMakeProcessItemVo"/>
        where production_make_process_item_id = #{productionMakeProcessItemId}
    </select>

    <insert id="insertProProductionMakeProcessItem" parameterType="ProProductionMakeProcessItem" useGeneratedKeys="true" keyProperty="productionMakeProcessItemId">
        insert into pro_production_make_process_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productionMakeProcessId != null">production_make_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="productionMakeProcessId != null">#{productionMakeProcessId},</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="updateProProductionMakeProcessItem" parameterType="ProProductionMakeProcessItem">
        update pro_production_make_process_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="productionMakeProcessId != null">production_make_process_id = #{productionMakeProcessId},</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_make_process_item_id = #{productionMakeProcessItemId}
    </update>

    <delete id="deleteProProductionMakeProcessItemByProductionMakeProcessItemId" parameterType="Long">
        delete from pro_production_make_process_item where production_make_process_item_id = #{productionMakeProcessItemId}
    </delete>

    <delete id="deleteProProductionMakeProcessItemByProductionMakeProcessItemIds" parameterType="String">
        delete from pro_production_make_process_item where production_make_process_item_id in
        <foreach item="productionMakeProcessItemId" collection="array" open="(" separator="," close=")">
            #{productionMakeProcessItemId}
        </foreach>
    </delete>

    <delete id="deleteProProductionMakeProcessItemByProductionMakeId" parameterType="Long">
        delete from pro_production_make_process_item where production_make_process_id in
        (select production_make_process_id from pro_production_make_process where production_make_id = #{productionMakeId})
    </delete>
</mapper>
