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

    <resultMap type="ProWorkOrderProcessVo" id="ProWorkOrderProcessResult">
        <result property="workorderProcessId" column="workorder_process_id"/>
        <result property="workorderId" column="workorder_id"/>
        <result property="workorderCode" column="workorder_code"/>
        <result property="processId" column="process_id"/>
        <result property="workstationId" column="workstation_id"/>
        <result property="stdWorkingTime" column="std_working_time"/>
        <result property="keyFlag" column="key_flag"/>
        <result property="isCheck" column="is_check"/>
        <result property="remark" column="remark"/>
        <result property="processRef" column="process_ref"/>
        <result property="enCode" column="en_code"/>
        <result property="controlCode" column="control_code"/>
        <result property="x" column="x"/>
        <result property="processName" column="process_name"/>
        <result property="quantity" column="quantity"/>
        <result property="lineBreakTime" column="line_break_time"/>
        <result property="workstationName" column="workstation_name"/>
        <result property="stdWorkingTimeUom" column="std_working_time_uom"/>
        <result property="idx" column="idx"/>
        <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="selectProWorkOrderProcessVo">
        select pwop.workorder_process_id,
               pwop.workorder_id,
               pwop.workorder_code,
               pwop.process_id,
               pwop.workstation_id,
               pwop.idx,
               pwop.std_working_time,
               pwop.std_working_time_uom,
               pwop.key_flag,
               pwop.is_check,
               pwop.remark,
               pwop.process_ref,
               pwop.en_code,
               pwop.control_code,
               pwop.x,
               pwop.quantity,
               pwop.line_break_time,
               pwop.default_pre_time,
               pwop.default_suf_time,
               pwop.idx,
               pp.process_code,
               pp.process_name,
               mw.workstation_name,
               mw.workstation_code
        from pro_work_order_process pwop
                 left join pro_process pp on pwop.process_id = pp.process_id
                 left join md_workstation mw on pwop.workstation_id = mw.workstation_id
    </sql>

    <select id="selectProWorkOrderProcessList" parameterType="ProWorkOrderProcess"
            resultMap="ProWorkOrderProcessResult">
        <include refid="selectProWorkOrderProcessVo"/>
        <where>
            <if test="workorderId != null ">and pwop.workorder_id = #{workorderId}</if>
            <if test="workorderProcessId != null ">and pwop.workorder_process_id = #{workorderProcessId}</if>
            <if test="processId != null ">and pwop.process_id = #{processId}</if>
            <if test="workstationId != null ">and pwop.workstation_id = #{workstationId}</if>
            <if test="stdWorkingTime != null ">and pwop.std_working_time = #{stdWorkingTime}</if>
            <if test="keyFlag != null  and keyFlag != ''">and pwop.key_flag = #{keyFlag}</if>
            <if test="isCheck != null  and isCheck != ''">and pwop.is_check = #{isCheck}</if>
            <if test="processRef != null  and processRef != ''">and pwop.process_ref = #{processRef}</if>
            <if test="enCode != null  and enCode != ''">and pwop.en_code = #{enCode}</if>
            <if test="controlCode != null  and controlCode != ''">and pwop.control_code = #{controlCode}</if>
            <if test="stdWorkingTimeUom != null  and stdWorkingTimeUom != ''">and pwop.std_working_time_uom =
                #{stdWorkingTimeUom}
            </if>
            <if test="x != null  and x != ''">and pwop.x = #{x}</if>
            <if test="processName != null  and processName != ''">and pwop.process_name like concat('%', #{processName},
                '%')
            </if>
            <if test="quantity != null ">and pwop.quantity = #{quantity}</if>
            <if test="lineBreakTime != null ">and pwop.line_break_time = #{lineBreakTime}</if>
        </where>
    </select>

    <select id="selectProWorkOrderProcessByWorkOrderId"
            parameterType="Long" resultMap="ProWorkOrderProcessResult">
        <include refid="selectProWorkOrderProcessVo"/>
        where pwop.workorder_id = #{workorderId}
    </select>

    <select id="selectProWorkOrderProcessByWorkorderProcessId" parameterType="Long"
            resultMap="ProWorkOrderProcessResult">
        <include refid="selectProWorkOrderProcessVo"/>
        where pwop.workorder_process_id = #{workorderProcessId}
    </select>

    <insert id="insertProWorkOrderProcess" parameterType="ProWorkOrderProcess" useGeneratedKeys="true"
            keyProperty="workorderProcessId">
        insert into pro_work_order_process
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workorderId != null">workorder_id,</if>
            <if test="workorderCode != null">workorder_code,</if>
            <if test="processId != null">process_id,</if>
            <if test="workstationId != null">workstation_id,</if>
            <if test="stdWorkingTime != null">std_working_time,</if>
            <if test="keyFlag != null">key_flag,</if>
            <if test="stdWorkingTimeUom != null">std_working_time_uom,</if>
            <if test="isCheck != null">is_check,</if>
            <if test="remark != null">remark,</if>
            <if test="processRef != null">process_ref,</if>
            <if test="enCode != null">en_code,</if>
            <if test="controlCode != null">control_code,</if>
            <if test="x != null">x,</if>
            <if test="processName != null">process_name,</if>
            <if test="quantity != null">quantity,</if>
            <if test="lineBreakTime != null">line_break_time,</if>
            <if test="idx != null">idx,</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="workorderId != null">#{workorderId},</if>
            <if test="workorderCode != null">#{workorderCode},</if>
            <if test="processId != null">#{processId},</if>
            <if test="workstationId != null">#{workstationId},</if>
            <if test="stdWorkingTime != null">#{stdWorkingTime},</if>
            <if test="keyFlag != null">#{keyFlag},</if>
            <if test="stdWorkingTimeUom != null">#{stdWorkingTimeUom},</if>
            <if test="isCheck != null">#{isCheck},</if>
            <if test="remark != null">#{remark},</if>
            <if test="processRef != null">#{processRef},</if>
            <if test="enCode != null">#{enCode},</if>
            <if test="controlCode != null">#{controlCode},</if>
            <if test="x != null">#{x},</if>
            <if test="processName != null">#{processName},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="lineBreakTime != null">#{lineBreakTime},</if>
            <if test="idx != null">#{idx},</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="updateProWorkOrderProcess" parameterType="ProWorkOrderProcess">
        update pro_work_order_process
        <trim prefix="SET" suffixOverrides=",">
            <if test="workorderId != null">workorder_id = #{workorderId},</if>
            <if test="workorderCode != null">workorder_code = #{workorderCode},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="workstationId != null">workstation_id = #{workstationId},</if>
            <if test="stdWorkingTime != null">std_working_time = #{stdWorkingTime},</if>
            <if test="keyFlag != null">key_flag = #{keyFlag},</if>
            <if test="isCheck != null">is_check = #{isCheck},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="processRef != null">process_ref = #{processRef},</if>
            <if test="enCode != null">en_code = #{enCode},</if>
            <if test="controlCode != null">control_code = #{controlCode},</if>
            <if test="x != null">x = #{x},</if>
            <if test="processName != null">process_name = #{processName},</if>
            <if test="stdWorkingTimeUom != null">std_working_time_uom = #{stdWorkingTimeUom},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="lineBreakTime != null">line_break_time = #{lineBreakTime},</if>
            <if test="idx != null">idx = #{idx},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>

        </trim>
        where workorder_process_id = #{workorderProcessId}
    </update>

    <delete id="deleteProWorkOrderProcessByWorkorderProcessId" parameterType="Long">
        delete
        from pro_work_order_process
        where workorder_process_id = #{workorderProcessId}
    </delete>

    <delete id="deleteProWorkOrderProcessByWorkorderProcessIds" parameterType="String">
        delete from pro_work_order_process where workorder_process_id in
        <foreach item="workorderProcessId" collection="array" open="(" separator="," close=")">
            #{workorderProcessId}
        </foreach>
    </delete>

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