<?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.dv.mapper.devBrandQuotation.DvBrandQuotationBomMapper">

    <resultMap type="DvBrandQuotationBom" id="DevBrandQuotationBomResult">
        <result property="bomItemId"    column="bom_item_id"    />
        <result property="brandQuotationId"    column="brand_quotation_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"    />
    </resultMap>

    <sql id="selectDevBrandQuotationBomVo">
        select
            a.bom_item_id,
            a.brand_quotation_id,
            a.item_id,
            c.item_name,
            a.quantity,
            a.unit_of_measure,
            a.scrap_percentage,
            a.alternative_group,
            a.alternative_priorities,
            a. alternative_strategy,
            a.alternative_probability,
            a.bom_item_remark,
            a.item_remark
        from dev_brand_quotation_bom a
                 left join md_item c on a.item_id= c.item_id
    </sql>

    <select id="selectDevBrandQuotationBomList" parameterType="DvBrandQuotationBom" resultMap="DevBrandQuotationBomResult">
        <include refid="selectDevBrandQuotationBomVo"/>
        <where>
            <if test="brandQuotationId != null "> and a.brand_quotation_id = #{brandQuotationId}</if>
            <if test="itemId != null "> and a.item_id = #{itemId}</if>
            <if test="quantity != null "> and a.quantity = #{quantity}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''"> and a.unit_of_measure = #{unitOfMeasure}</if>
            <if test="scrapPercentage != null "> and a.scrap_percentage = #{scrapPercentage}</if>
            <if test="alternativeGroup != null  and alternativeGroup != ''"> and a.alternative_group = #{alternativeGroup}</if>
            <if test="alternativePriorities != null "> and a.alternative_priorities = #{alternativePriorities}</if>
            <if test="alternativeStrategy != null  and alternativeStrategy != ''"> and a.alternative_strategy = #{alternativeStrategy}</if>
            <if test="alternativeProbability != null "> and a.alternative_probability = #{alternativeProbability}</if>
            <if test="bomItemRemark != null  and bomItemRemark != ''"> and a.bom_item_remark = #{bomItemRemark}</if>
            <if test="itemRemark != null  and itemRemark != ''"> and a.item_remark = #{itemRemark}</if>
        </where>
    </select>

    <select id="seletDvBrandQuotationBomBybrandQuotationId"
            parameterType="Long"
            resultMap="DevBrandQuotationBomResult">
        <include refid="selectDevBrandQuotationBomVo"/>
        where a.brand_quotation_id = #{brandQuotationId}
    </select>

    <select id="selectDevBrandQuotationBomByBomItemId" parameterType="Long" resultMap="DevBrandQuotationBomResult">
        <include refid="selectDevBrandQuotationBomVo"/>
        where a.bom_item_id = #{bomItemId}
    </select>

    <insert id="insertDevBrandQuotationBom" parameterType="DvBrandQuotationBom">
        insert into dev_brand_quotation_bom
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bomItemId != null">bom_item_id,</if>
            <if test="brandQuotationId != null">brand_quotation_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="bomItemId != null">#{bomItemId},</if>
            <if test="brandQuotationId != null">#{brandQuotationId},</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="updateDevBrandQuotationBom" parameterType="DvBrandQuotationBom">
        update dev_brand_quotation_bom
        <trim prefix="SET" suffixOverrides=",">
            <if test="brandQuotationId != null">brand_quotation_id = #{brandQuotationId},</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="deleteDevBrandQuotationBomByBomItemId" parameterType="Long">
        delete from dev_brand_quotation_bom where bom_item_id = #{bomItemId}
    </delete>

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