<?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.prototypeMake.ProPrototypeMakeBomMapper">

    <resultMap type="ProPrototypeMakeBom" id="ProPrototypeMakeBomResult">
        <result property="bomItemId" column="bom_item_id"/>
        <result property="prototypeMakeId" column="prototype_make_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="selectProPrototypeMakeBomVo">
        select ppmb.bom_item_id,
               ppmb.prototype_make_id,
               ppmb.item_id,
               ppmb.quantity,
               ppmb.unit_of_measure,
               ppmb.scrap_percentage,
               ppmb.alternative_group,
               ppmb.alternative_priorities,
               ppmb.alternative_strategy,
               ppmb.alternative_probability,
               ppmb.bom_item_remark,
               ppmb.item_remark,
               mi.item_name
        from pro_prototype_make_bom ppmb
                 left join md_item mi on mi.item_id = ppmb.item_id
    </sql>

    <select id="selectProPrototypeMakeBomList" parameterType="ProPrototypeMakeBom"
            resultMap="ProPrototypeMakeBomResult">
        <include refid="selectProPrototypeMakeBomVo"/>
        <where>
            <if test="prototypeMakeId != null ">and ppmb.prototype_make_id = #{prototypeMakeId}</if>
            <if test="itemId != null ">and ppmb.item_id = #{itemId}</if>
            <if test="quantity != null ">and ppmb.quantity = #{quantity}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and ppmb.unit_of_measure = #{unitOfMeasure}</if>
            <if test="scrapPercentage != null ">and ppmb.scrap_percentage = #{scrapPercentage}</if>
            <if test="alternativeGroup != null  and alternativeGroup != ''">and ppmb.alternative_group =
                #{alternativeGroup}
            </if>
            <if test="alternativePriorities != null ">and ppmb.alternative_priorities = #{alternativePriorities}</if>
            <if test="alternativeStrategy != null  and alternativeStrategy != ''">and ppmb.alternative_strategy =
                #{alternativeStrategy}
            </if>
            <if test="alternativeProbability != null ">and ppmb.alternative_probability = #{alternativeProbability}</if>
            <if test="bomItemRemark != null  and bomItemRemark != ''">and ppmb.bom_item_remark = #{bomItemRemark}</if>
            <if test="itemRemark != null  and itemRemark != ''">and ppmb.item_remark = #{itemRemark}</if>
        </where>
    </select>

    <select id="selectProPrototypeMakeBomByBomItemId" parameterType="Long" resultMap="ProPrototypeMakeBomResult">
        <include refid="selectProPrototypeMakeBomVo"/>
        where ppmb.bom_item_id = #{bomItemId}
    </select>
    <select id="selectProPrototypeMakeBomByPrototypeMakeId"
            parameterType="Long" resultMap="ProPrototypeMakeBomResult">
        <include refid="selectProPrototypeMakeBomVo"/>
        where ppmb.prototype_make_id = #{prototypeMakeId}
    </select>

    <insert id="insertProPrototypeMakeBom" parameterType="ProPrototypeMakeBom" useGeneratedKeys="true"
            keyProperty="bomItemId">
        insert into pro_prototype_make_bom
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="prototypeMakeId != null">prototype_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="prototypeMakeId != null">#{prototypeMakeId},</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="updateProPrototypeMakeBom" parameterType="ProPrototypeMakeBom">
        update pro_prototype_make_bom
        <trim prefix="SET" suffixOverrides=",">
            <if test="prototypeMakeId != null">prototype_make_id = #{prototypeMakeId},</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="deleteProPrototypeMakeBomByBomItemId" parameterType="Long">
        delete
        from pro_prototype_make_bom
        where bom_item_id = #{bomItemId}
    </delete>

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