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

    <resultMap type="ProBom" id="ProBomResult">
        <result property="bomId" column="bom_id"/>
        <result property="bomCode" column="bom_code"/>
        <result property="itemId" column="item_id"/>
        <result property="itemName" column="item_name"/>
        <result property="usageName" column="usage_name"/>
        <result property="usageId" column="usage_id"/>
        <result property="quantity" column="quantity"/>
        <result property="unitOfMeasure" column="unit_of_measure"/>
        <result property="expirationDateStart" column="expiration_date_start"/>
        <result property="expirationDateEnd" column="expiration_date_end"/>
        <result property="ecnCode" column="ecn_code"/>
        <result property="singleLayerThickness" column="single_layer_thickness"/>
        <result property="singleWeight" column="single_weight"/>
        <result property="rowNum" column="row_num"/>
        <result property="dispatchMultiple" column="dispatch_multiple"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="remark" column="remark"/>
        <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="selectProBomVo">
        select pb.bom_id,
               pb.bom_code,
               pb.item_id,
               mi.item_name,
               pb.usage_id,
               pu.usage_name,
               pb.quantity,
               pb.unit_of_measure,
               pb.expiration_date_start,
               pb.expiration_date_end,
               pb.ecn_code,
               pb.single_layer_thickness,
               pb.single_weight,
               pb.row_num,
               pb.dispatch_multiple,
               pb.enable_flag,
               pb.remark,
               pb.create_by,
               pb.create_time,
               pb.update_by,
               pb.update_time
        from pro_bom pb
                 left join  md_item mi on pb.item_id = mi.item_id
                 left join pro_usage pu on pu.usage_id = pb.usage_id
    </sql>

    <select id="selectProBomList" parameterType="ProBom" resultMap="ProBomResult">
        <include refid="selectProBomVo"/>
        <where>
            <if test="bomCode != null  and bomCode != ''">and bom_code = #{bomCode}</if>
            <if test="itemId != null ">and item_id = #{itemId}</if>
            <if test="itemName != null ">and mi.item_name = #{itemName}</if>
            <if test="usageId != null ">and usage_id = #{usageId}</if>
            <if test="usageName != null ">and pu.usage_name = #{usageName}</if>
            <if test="quantity != null ">and quantity = #{quantity}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and unit_of_measure = #{unitOfMeasure}</if>
            <if test="expirationDateStart != null ">and expiration_date_start = #{expirationDateStart}</if>
            <if test="expirationDateEnd != null ">and expiration_date_end = #{expirationDateEnd}</if>
            <if test="ecnCode != null  and ecnCode != ''">and ecn_code = #{ecnCode}</if>
            <if test="singleLayerThickness != null ">and single_layer_thickness = #{singleLayerThickness}</if>
            <if test="singleWeight != null ">and single_weight = #{singleWeight}</if>
            <if test="rowNum != null ">and row_num = #{rowNum}</if>
            <if test="dispatchMultiple != null ">and dispatch_multiple = #{dispatchMultiple}</if>
            <if test="enableFlag != null  and enableFlag != ''">and enable_flag = #{enableFlag}</if>
        </where>
    </select>

    <select id="selectProBomByBomId" parameterType="Long" resultMap="ProBomResult">
        <include refid="selectProBomVo"/>
        where bom_id = #{bomId}
    </select>

    <insert id="insertProBom" parameterType="ProBom" useGeneratedKeys="true" keyProperty="bomId">
        insert into pro_bom
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bomCode != null">bom_code,</if>
            <if test="itemId != null">item_id,</if>
            <if test="usageId != null">usage_id,</if>
            <if test="quantity != null">quantity,</if>
            <if test="unitOfMeasure != null">unit_of_measure,</if>
            <if test="expirationDateStart != null">expiration_date_start,</if>
            <if test="expirationDateEnd != null">expiration_date_end,</if>
            <if test="ecnCode != null">ecn_code,</if>
            <if test="singleLayerThickness != null">single_layer_thickness,</if>
            <if test="singleWeight != null">single_weight,</if>
            <if test="rowNum != null">row_num,</if>
            <if test="dispatchMultiple != null">dispatch_multiple,</if>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="remark != null">remark,</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="bomCode != null">#{bomCode},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="usageId != null">#{usageId},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
            <if test="expirationDateStart != null">#{expirationDateStart},</if>
            <if test="expirationDateEnd != null">#{expirationDateEnd},</if>
            <if test="ecnCode != null">#{ecnCode},</if>
            <if test="singleLayerThickness != null">#{singleLayerThickness},</if>
            <if test="singleWeight != null">#{singleWeight},</if>
            <if test="rowNum != null">#{rowNum},</if>
            <if test="dispatchMultiple != null">#{dispatchMultiple},</if>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="remark != null">#{remark},</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="updateProBom" parameterType="ProBom">
        update pro_bom
        <trim prefix="SET" suffixOverrides=",">
            <if test="bomCode != null">bom_code = #{bomCode},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="usageId != null">usage_id = #{usageId},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
            <if test="expirationDateStart != null">expiration_date_start = #{expirationDateStart},</if>
            <if test="expirationDateEnd != null">expiration_date_end = #{expirationDateEnd},</if>
            <if test="ecnCode != null">ecn_code = #{ecnCode},</if>
            <if test="singleLayerThickness != null">single_layer_thickness = #{singleLayerThickness},</if>
            <if test="singleWeight != null">single_weight = #{singleWeight},</if>
            <if test="rowNum != null">row_num = #{rowNum},</if>
            <if test="dispatchMultiple != null">dispatch_multiple = #{dispatchMultiple},</if>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="remark != null">remark = #{remark},</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_id = #{bomId}
    </update>

    <delete id="deleteProBomByBomId" parameterType="Long">
        delete
        from pro_bom
        where bom_id = #{bomId}
    </delete>

    <delete id="deleteProBomByBomIds" parameterType="String">
        delete from pro_bom where bom_id in
        <foreach item="bomId" collection="array" open="(" separator="," close=")">
            #{bomId}
        </foreach>
    </delete>
</mapper>
