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

    <resultMap type="ProProductionMakeBom" id="ProProductionMakeBomResult">
        <result property="bomItemId"    column="bom_item_id"    />
        <result property="productionMakeId"    column="production_make_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="quantity"    column="quantity"    />
        <result property="unitOfMeasure"    column="unit_of_measure"    />
        <result property="scrapPercentage"    column="scrap_percentage"    />
        <result property="alternativeGroup"    column="alternative_group"    />
        <result property="alternativePriorities"    column="alternative_priorities"    />
        <result property="alternativeStrategy"    column="alternative_strategy"    />
        <result property="alternativeProbability"    column="alternative_probability"    />
        <result property="bomItemRemark"    column="bom_item_remark"    />
        <result property="itemRemark"    column="item_remark"    />
        <result property="itemName"    column="item_name"    />
    </resultMap>

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

    <select id="selectProProductionMakeBomList" parameterType="ProProductionMakeBom" resultMap="ProProductionMakeBomResult">
        <include refid="selectProProductionMakeBomVo"/>
        <where>
            <if test="productionMakeId != null "> and production_make_id = #{productionMakeId}</if>
            <if test="itemId != null "> and item_id = #{itemId}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''"> and unit_of_measure = #{unitOfMeasure}</if>
            <if test="scrapPercentage != null "> and scrap_percentage = #{scrapPercentage}</if>
            <if test="alternativeGroup != null  and alternativeGroup != ''"> and alternative_group = #{alternativeGroup}</if>
            <if test="alternativePriorities != null "> and alternative_priorities = #{alternativePriorities}</if>
            <if test="alternativeStrategy != null  and alternativeStrategy != ''"> and alternative_strategy = #{alternativeStrategy}</if>
            <if test="alternativeProbability != null "> and alternative_probability = #{alternativeProbability}</if>
            <if test="bomItemRemark != null  and bomItemRemark != ''"> and bom_item_remark = #{bomItemRemark}</if>
            <if test="itemRemark != null  and itemRemark != ''"> and item_remark = #{itemRemark}</if>
        </where>
    </select>

    <select id="selectProProductionMakeBomByBomItemId" parameterType="Long" resultMap="ProProductionMakeBomResult">
        <include refid="selectProProductionMakeBomVo"/>
        where bom_item_id = #{bomItemId}
    </select>

    <insert id="insertProProductionMakeBom" parameterType="ProProductionMakeBom" useGeneratedKeys="true" keyProperty="bomItemId">
        insert into pro_production_make_bom
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productionMakeId != null">production_make_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="quantity != null">quantity,</if>
            <if test="unitOfMeasure != null">unit_of_measure,</if>
            <if test="scrapPercentage != null">scrap_percentage,</if>
            <if test="alternativeGroup != null">alternative_group,</if>
            <if test="alternativePriorities != null">alternative_priorities,</if>
            <if test="alternativeStrategy != null">alternative_strategy,</if>
            <if test="alternativeProbability != null">alternative_probability,</if>
            <if test="bomItemRemark != null">bom_item_remark,</if>
            <if test="itemRemark != null">item_remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productionMakeId != null">#{productionMakeId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
            <if test="scrapPercentage != null">#{scrapPercentage},</if>
            <if test="alternativeGroup != null">#{alternativeGroup},</if>
            <if test="alternativePriorities != null">#{alternativePriorities},</if>
            <if test="alternativeStrategy != null">#{alternativeStrategy},</if>
            <if test="alternativeProbability != null">#{alternativeProbability},</if>
            <if test="bomItemRemark != null">#{bomItemRemark},</if>
            <if test="itemRemark != null">#{itemRemark},</if>
         </trim>
    </insert>

    <update id="updateProProductionMakeBom" parameterType="ProProductionMakeBom">
        update pro_production_make_bom
        <trim prefix="SET" suffixOverrides=",">
            <if test="productionMakeId != null">production_make_id = #{productionMakeId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
            <if test="scrapPercentage != null">scrap_percentage = #{scrapPercentage},</if>
            <if test="alternativeGroup != null">alternative_group = #{alternativeGroup},</if>
            <if test="alternativePriorities != null">alternative_priorities = #{alternativePriorities},</if>
            <if test="alternativeStrategy != null">alternative_strategy = #{alternativeStrategy},</if>
            <if test="alternativeProbability != null">alternative_probability = #{alternativeProbability},</if>
            <if test="bomItemRemark != null">bom_item_remark = #{bomItemRemark},</if>
            <if test="itemRemark != null">item_remark = #{itemRemark},</if>
        </trim>
        where bom_item_id = #{bomItemId}
    </update>

    <delete id="deleteProProductionMakeBomByBomItemId" parameterType="Long">
        delete from pro_production_make_bom where bom_item_id = #{bomItemId}
    </delete>

    <delete id="deleteProProductionMakeBomByProductionMakeId" parameterType="Long">
        delete from pro_production_make_bom where production_make_id = #{productionMakeId}
    </delete>


    <delete id="deleteProProductionMakeBomByBomItemIds" parameterType="String">
        delete from pro_production_make_bom where bom_item_id in
        <foreach item="bomItemId" collection="array" open="(" separator="," close=")">
            #{bomItemId}
        </foreach>
    </delete>
</mapper>
