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

    <resultMap type="ProBomItem" id="ProBomItemResult">
        <id property="bomItemId" column="bom_item_id"/>
        <result property="bomId" column="bom_id"/>
        <result property="itemName" column="item_name"/>
        <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="enableFlag" column="enable_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectProBomItemVo">
        select p.bom_item_id,
               p.bom_id,
               p.item_id,
               m.item_name,
               p.quantity,
               p.unit_of_measure,
               p.scrap_percentage,
               p.alternative_group,
               p.alternative_priorities,
               p.alternative_strategy,
               p.alternative_probability,
               p.bom_item_remark,
               p.item_remark,
               p.enable_flag,
               p.create_by,
               p.create_time,
               p.update_by,
               p.update_time
        from pro_bom_item p
                 left join md_item m on p.item_id = m.item_id
    </sql>

    <select id="selectProBomItemList" parameterType="ProBomItem" resultMap="ProBomItemResult">
        <include refid="selectProBomItemVo"/>
        <where>
            <if test="bomId != null ">and bom_id = #{bomId}</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>
            <if test="enableFlag != null  and enableFlag != ''">and enable_flag = #{enableFlag}</if>
        </where>
    </select>

    <select id="selectProBomItemByBomItemId" parameterType="Long" resultMap="ProBomItemResult">
        <include refid="selectProBomItemVo"/>
        where bom_item_id = #{bomItemId}
    </select>

    <insert id="insertProBomItem" parameterType="ProBomItem" useGeneratedKeys="true" keyProperty="bomItemId">
        insert into pro_bom_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bomId != null">bom_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>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="bomId != null">#{bomId},</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>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateProBomItem" parameterType="ProBomItem">
        update pro_bom_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="bomId != null">bom_id = #{bomId},</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>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where bom_item_id = #{bomItemId}
    </update>

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

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