<?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.proWorkOrder.ProWorkOrderBomMapper">

    <resultMap type="ProWorkOrderBom" id="ProWorkOrderBomResult">
        <result property="bomItemId" column="bom_item_id"/>
        <result property="workorderId" column="workorder_id"/>
        <result property="workorderCode" column="workorder_code"/>
        <result property="alternativeBill" column="alternative_bill"/>
        <result property="itemId" column="item_id"/>
        <result property="quantity" column="quantity"/>
        <result property="unitOfMeasure" column="unit_of_measure"/>
        <result property="itemName" column="item_name"/>
        <result property="itemCode" column="item_code"/>
        <result property="requirementNumber" column="requirement_number"/>
        <result property="requirementProjectNumber" column="requirement_project_number"/>
        <result property="storeAddr" column="store_addr"/>
        <result property="scrapPercentage" column="scrap_percentage"/>
        <result property="unitUsage" column="unit_usage"/>
        <result property="virtualProjectLog" column="virtual_project_log"/>
        <result property="deputyUnitOfMeasure" column="deputy_unit_of_measure"/>
        <result property="unitConvert" column="unit_convert"/>
        <result property="storeAddr2" column="store_addr2"/>
        <result property="processRef" column="process_ref"/>
        <result property="isBackflush" column="is_backflush"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="opName" column="op_name"/>
        <result property="erpCreateTime" column="erp_create_time"/>
        <result property="erpUpdateTime" column="erp_update_time"/>
    </resultMap>

    <sql id="selectProWorkOrderBomVo">
        select pwob.bom_item_id,
               pwob.workorder_id,
               pwob.workorder_code,
               pwob.item_id,
               pwob.quantity,
               pwob.unit_of_measure,
               pwob.scrap_percentage,
               pwob.alternative_bill,
               mi.item_name,
               pwob.item_code,
               pwob.requirement_number,
               pwob.requirement_project_number,
               pwob.store_addr,
               pwob.unit_usage,
               pwob.virtual_project_log,
               pwob.deputy_unit_of_measure,
               pwob.unit_convert,
               pwob.store_addr2,
               pwob.process_ref,
               pwob.is_backflush
        from pro_work_order_bom pwob
                 left join md_item mi on pwob.item_id = mi.item_id

    </sql>

    <select id="selectBomByWorkorderCode"
            resultMap="ProWorkOrderBomResult">
        <include refid="selectProWorkOrderBomVo"/>
        where pwob.workorder_code = #{workorderCode}
    </select>

    <select id="selectProWorkOrderBomList" parameterType="ProWorkOrderBom" resultMap="ProWorkOrderBomResult">
        <include refid="selectProWorkOrderBomVo"/>
        <where>
            <if test="workorderId != null ">and pwob.workorder_id = #{workorderId}</if>
            <if test="itemId != null ">and pwob.item_id = #{itemId}</if>
            <if test="quantity != null ">and pwob.quantity = #{quantity}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and pwob.unit_of_measure = #{unitOfMeasure}</if>
            <if test="scrapPercentage != null ">and pwob.scrap_percentage = #{scrapPercentage}</if>
        </where>
    </select>

    <select id="selectProWorkOrderBomByBomItemId" parameterType="Long" resultMap="ProWorkOrderBomResult">
        <include refid="selectProWorkOrderBomVo"/>
        where pwob.bom_item_id = #{bomItemId}
    </select>

    <insert id="insertProWorkOrderBom" parameterType="ProWorkOrderBom" useGeneratedKeys="true" keyProperty="bomItemId">
        insert into pro_work_order_bom
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workorderId != null">workorder_id,</if>
            <if test="workorderCode != null">workorder_code,</if>
            <if test="alternativeBill != null">alternative_bill,</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="itemCode != null">item_code,</if>
            <if test="requirementNumber != null">requirement_number,</if>
            <if test="requirementProjectNumber != null">requirement_project_number,</if>
            <if test="storeAddr != null">store_addr,</if>
            <if test="unitUsage != null">unit_usage,</if>
            <if test="virtualProjectLog != null">virtual_project_log,</if>
            <if test="deputyUnitOfMeasure != null">deputy_unit_of_measure,</if>
            <if test="unitConvert != null">unit_convert,</if>
            <if test="storeAddr2 != null">store_addr2,</if>
            <if test="processRef != null">process_ref,</if>
            <if test="isBackflush != null">is_backflush,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="opName != null">op_name,</if>
            <if test="erpCreateTime !=null">erp_create_time,</if>
            <if test="erpUpdateTime !=null">erp_update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workorderId != null">#{workorderId},</if>
            <if test="workorderCode != null">#{workorderCode},</if>
            <if test="alternativeBill != null">#{alternativeBill},</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="itemCode != null">#{itemCode},</if>
            <if test="requirementNumber != null">#{requirementNumber},</if>
            <if test="requirementProjectNumber != null">#{requirementProjectNumber},</if>
            <if test="storeAddr != null">#{storeAddr},</if>
            <if test="unitUsage != null">#{unitUsage},</if>
            <if test="virtualProjectLog != null">#{virtualProjectLog},</if>
            <if test="deputyUnitOfMeasure != null">#{deputyUnitOfMeasure},</if>
            <if test="unitConvert != null">#{unitConvert},</if>
            <if test="storeAddr2 != null">#{storeAddr2},</if>
            <if test="processRef != null">#{processRef},</if>
            <if test="isBackflush != null">#{isBackflush},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="opName != null">#{opName},</if>
            <if test="erpCreateTime != null">#{erpCreateTime},</if>
            <if test="erpUpdateTime != null">#{erpUpdateTime},</if>
        </trim>
    </insert>

    <update id="updateProWorkOrderBom" parameterType="ProWorkOrderBom">
        update pro_work_order_bom
        <trim prefix="SET" suffixOverrides=",">
            <if test="workorderId != null">workorder_id = #{workorderId},</if>
            <if test="workorderCode != null">workorder_code = #{workorderCode},</if>
            <if test="alternativeBill != null">alternative_bill = #{alternativeBill},</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="itemCode != null">item_code = #{itemCode},</if>
            <if test="requirementNumber != null">requirement_number = #{requirementNumber},</if>
            <if test="requirementProjectNumber != null">requirement_project_number = #{requirementProjectNumber},</if>
            <if test="storeAddr != null">store_addr = #{storeAddr},</if>
            <if test="unitUsage != null">unit_usage = #{unitUsage},</if>
            <if test="virtualProjectLog != null">virtual_project_log = #{virtualProjectLog},</if>
            <if test="deputyUnitOfMeasure != null">deputy_unit_of_measure = #{deputyUnitOfMeasure},</if>
            <if test="unitConvert != null">unit_convert = #{unitConvert},</if>
            <if test="storeAddr2 != null">store_addr2 = #{storeAddr2},</if>
            <if test="processRef != null">process_ref = #{processRef},</if>
            <if test="isBackflush != null">is_backflush = #{isBackflush},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="opName != null">op_name = #{opName},</if>
            <if test="erpUpdateTime != null">erp_update_time = #{erpUpdateTime},</if>
        </trim>
        where bom_item_id = #{bomItemId}
    </update>

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

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

    <delete id="deleteByWorkorderId" parameterType="Long">
        delete
        from pro_work_order_bom
        where workorder_id = #{workorderId}
    </delete>
</mapper>