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

    <resultMap type="ProWorkOrderProcessTool" id="ProWorkOrderProcessToolResult">
        <result property="workorderProcessToolId" column="workorder_process_tool_id"/>
        <result property="workorderProcessId" column="workorder_process_id"/>
        <result property="toolId" column="tool_id"/>
        <result property="quantity" column="quantity"/>
        <result property="remark" column="remark"/>
        <result property="itemCode" column="item_code"/>
        <result property="sapItemCode" column="sap_item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="specification" column="specification"/>
        <result property="itemTypeName" column="item_type_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="selectProWorkOrderProcessToolVo">
        select pwopt.workorder_process_tool_id,
               pwopt.workorder_process_id,
               pwopt.tool_id,
               pwopt.work_order_id,
               pwopt.quantity,
               pwopt.remark,
               im.item_code,
               pwopt.sap_item_code,
               pwopt.item_name,
               im.specification,
               im.item_type_name
        from pro_work_order_process_tool pwopt
                 left join md_item im
                           on im.item_id = pwopt.tool_id
    </sql>

    <select id="selectProWorkOrderProcessToolList" parameterType="ProWorkOrderProcessTool"
            resultMap="ProWorkOrderProcessToolResult">
        <include refid="selectProWorkOrderProcessToolVo"/>
        <where>
            <if test="workorderProcessId != null ">and workorder_process_id = #{workorderProcessId}</if>
            <if test="toolId != null ">and tool_id = #{toolId}</if>
            <if test="workOrderId != null ">and work_order_id = #{workOrderId}</if>
            <if test="quantity != null ">and quantity = #{quantity}</if>
        </where>
    </select>

    <select id="selectProWorkOrderProcessToolByWorkorderProcessToolId" parameterType="Long"
            resultMap="ProWorkOrderProcessToolResult">
        <include refid="selectProWorkOrderProcessToolVo"/>
        where workorder_process_tool_id = #{workorderProcessToolId}
    </select>
    <select id="selectProWorkOrderProcessToolByWorkorderProcessId"
            parameterType="Long" resultMap="ProWorkOrderProcessToolResult">
        <include refid="selectProWorkOrderProcessToolVo"/>
        where workorder_process_id = #{workorderProcessId}
    </select>

    <insert id="insertProWorkOrderProcessTool" parameterType="ProWorkOrderProcessTool" useGeneratedKeys="true"
            keyProperty="workorderProcessToolId">
        insert into pro_work_order_process_tool
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workorderProcessId != null">workorder_process_id,</if>
            <if test="toolId != null">tool_id,</if>
            <if test="itemName != null">item_name,</if>
            <if test="sapItemCode != null">sap_item_code,</if>
            <if test="quantity != null">quantity,</if>
            <if test="remark != null">remark,</if>
            <if test="workOrderId != null">work_order_id,</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="toolId != null">#{toolId},</if>
            <if test="itemName != null">#{itemName},</if>
            <if test="sapItemCode != null">#{sapItemCode},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="remark != null">#{remark},</if>
            <if test="workOrderId != null">#{workOrderId},</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="updateProWorkOrderProcessTool" parameterType="ProWorkOrderProcessTool">
        update pro_work_order_process_tool
        <trim prefix="SET" suffixOverrides=",">
            <if test="workorderProcessId != null">workorder_process_id = #{workorderProcessId},</if>
            <if test="toolId != null">tool_id = #{toolId},</if>
            <if test="itemName != null">item_name = #{itemName},</if>
            <if test="sapItemCode != null">sap_item_code = #{sapItemCode},</if>
            <if test="workOrderId != null">work_order_id = #{workOrderId},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>

        </trim>
        where workorder_process_tool_id = #{workorderProcessToolId}
    </update>

    <delete id="deleteProWorkOrderProcessToolByWorkorderProcessToolId" parameterType="Long">
        delete
        from pro_work_order_process_tool
        where workorder_process_tool_id = #{workorderProcessToolId}
    </delete>

    <delete id="deleteProWorkOrderProcessToolByWorkorderProcessToolIds" parameterType="String">
        delete from pro_work_order_process_tool where workorder_process_tool_id in
        <foreach item="workorderProcessToolId" collection="array" open="(" separator="," close=")">
            #{workorderProcessToolId}
        </foreach>
    </delete>

    <select id="selectProWorkOrderProcessToolByWorkorderIds"
            parameterType="String" resultMap="ProWorkOrderProcessToolResult">
        <include refid="selectProWorkOrderProcessToolVo"/>
        where work_order_id in
        <foreach item="workOrderId" collection="array" open="(" separator="," close=")">
            #{workOrderId}
        </foreach>
    </select>
</mapper>
