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

    <resultMap type="ProWorkOrderProcessItem" id="ProWorkOrderProcessItemResult">
        <result property="workorderProcessItemId" column="workorder_process_item_id"/>
        <result property="workorderProcessId" column="workorder_process_id"/>
        <result property="itemId" column="item_id"/>
        <result property="quantity" column="quantity"/>
        <result property="sapItemCode" column="sap_item_code"/>
        <result property="remark" column="remark"/>
        <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="itemName" column="item_name"/>
        <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="selectProWorkOrderProcessItemVo">
        select pwopi.workorder_process_item_id,
               pwopi.workorder_process_id,
               pwopi.item_id,
               pwopi.quantity,
               pwopi.sap_item_code,
               pwopi.remark,
               pwopi.requirement_number,
               pwopi.requirement_project_number,
               pwopi.store_addr,
               pwopi.scrap_percentage,
               pwopi.unit_usage,
               pwopi.virtual_project_log,
               pwopi.deputy_unit_of_measure,
               pwopi.unit_convert,
               pwopi.store_addr2,
               pwopi.process_ref,
               pwopi.is_backflush,
               mi.item_name
        from pro_work_order_process_item pwopi
                 left join md_item mi on pwopi.item_id = mi.item_id
    </sql>

    <select id="selectProWorkOrderProcessItemList" parameterType="ProWorkOrderProcessItem"
            resultMap="ProWorkOrderProcessItemResult">
        <include refid="selectProWorkOrderProcessItemVo"/>
        <where>
            <if test="workorderProcessId != null ">and pwopi.workorder_process_id = #{workorderProcessId}</if>
            <if test="itemId != null ">and pwopi.item_id = #{itemId}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and pwopi.unit_of_measure = #{unitOfMeasure}</if>
            <if test="quantity != null ">and pwopi.quantity = #{quantity}</if>
        </where>
    </select>

    <select id="selectProWorkOrderProcessItemByWorkorderProcessId"
            parameterType="Long" resultMap="ProWorkOrderProcessItemResult">
        <include refid="selectProWorkOrderProcessItemVo"/>
        where pwopi.workorder_process_id = #{workorderProcessId}
    </select>

    <select id="selectProWorkOrderProcessItemByWorkorderProcessItemId" parameterType="Long"
            resultMap="ProWorkOrderProcessItemResult">
        <include refid="selectProWorkOrderProcessItemVo"/>
        where pwopi.workorder_process_item_id = #{workorderProcessItemId}
    </select>

    <insert id="insertProWorkOrderProcessItem" parameterType="ProWorkOrderProcessItem" useGeneratedKeys="true"
            keyProperty="workorderProcessItemId">
        insert into pro_work_order_process_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workorderProcessId != null">workorder_process_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="quantity != null">quantity,</if>
            <if test="sapItemCode != null">sap_item_code,</if>
            <if test="remark != null">remark,</if>
            <if test="requirementNumber != null">requirement_number,</if>
            <if test="requirementProjectNumber != null">requirement_project_number,</if>
            <if test="unitOfMeasure != null">unit_of_measure,</if>
            <if test="storeAddr != null">store_addr,</if>
            <if test="scrapPercentage != null">scrap_percentage,</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="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="workorderProcessId != null">#{workorderProcessId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="sapItemCode != null">#{sapItemCode},</if>
            <if test="remark != null">#{remark},</if>
            <if test="requirementNumber != null">#{requirementNumber},</if>
            <if test="requirementProjectNumber != null">#{requirementProjectNumber},</if>
            <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
            <if test="storeAddr != null">#{storeAddr},</if>
            <if test="scrapPercentage != null">#{scrapPercentage},</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="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="updateProWorkOrderProcessItem" parameterType="ProWorkOrderProcessItem">
        update pro_work_order_process_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="workorderProcessId != null">workorder_process_id = #{workorderProcessId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="sapItemCode != null">sap_item_code = #{sapItemCode},</if>
            <if test="remark != null">remark = #{remark},</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="scrapPercentage != null">scrap_percentage = #{scrapPercentage},</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="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>

        </trim>
        where workorder_process_item_id = #{workorderProcessItemId}
    </update>

    <delete id="deleteProWorkOrderProcessItemByWorkorderProcessItemId" parameterType="Long">
        delete
        from pro_work_order_process_item
        where workorder_process_item_id = #{workorderProcessItemId}
    </delete>

    <delete id="deleteProWorkOrderProcessItemByWorkorderProcessItemIds" parameterType="String">
        delete from pro_work_order_process_item where workorder_process_item_id in
        <foreach item="workorderProcessItemId" collection="array" open="(" separator="," close=")">
            #{workorderProcessItemId}
        </foreach>
    </delete>
</mapper>