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

    <resultMap type="ProProductionRequestBom" id="ProProductionRequestBomResult">
        <result property="bomItemId"    column="bom_item_id"    />
        <result property="productionRequestId"    column="production_request_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="itemName"    column="item_name"    />
        <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"    />
    </resultMap>

    <sql id="selectProProductionRequestBomVo">
        select bom_item_id, production_request_id, t1.item_id, quantity, t1.unit_of_measure, scrap_percentage, alternative_group, alternative_priorities,
        alternative_strategy, alternative_probability, bom_item_remark, item_remark, t2.item_name
        from pro_production_request_bom t1 left join md_item t2 on t1.item_id = t2.item_id
    </sql>

    <select id="selectProProductionRequestBomList" parameterType="ProProductionRequestBom" resultMap="ProProductionRequestBomResult">
        <include refid="selectProProductionRequestBomVo"/>
        <where>
            <if test="productionRequestId != null "> and production_request_id = #{productionRequestId}</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="selectProProductionRequestBomByBomItemId" parameterType="Long" resultMap="ProProductionRequestBomResult">
        <include refid="selectProProductionRequestBomVo"/>
        where bom_item_id = #{bomItemId}
    </select>

    <insert id="insertProProductionRequestBom" parameterType="ProProductionRequestBom" useGeneratedKeys="true" keyProperty="bomItemId">
        insert into pro_production_request_bom
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productionRequestId != null">production_request_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="productionRequestId != null">#{productionRequestId},</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="updateProProductionRequestBom" parameterType="ProProductionRequestBom">
        update pro_production_request_bom
        <trim prefix="SET" suffixOverrides=",">
            <if test="productionRequestId != null">production_request_id = #{productionRequestId},</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="deleteProProductionRequestBomByBomItemId" parameterType="Long">
        delete from pro_production_request_bom where bom_item_id = #{bomItemId}
    </delete>

    <delete id="deleteProProductionRequestBomByProductionRequestId" parameterType="Long">
        delete from pro_production_request_bom where production_request_id = #{productionRequestId}
    </delete>

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