<?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.op.mes.mapper.ProOrderWorkorderMapper">

    <resultMap type="ProOrderWorkorder" id="ProOrderWorkorderResult">
        <result property="workorderId"	column="workorder_id"	/>
        <result property="workorderCode"	column="workorder_code"	/>
        <result property="workorderName"	column="workorder_name"	/>
        <result property="orderId"	column="order_id"	/>
        <result property="orderCode"	column="order_code"	/>
        <result property="productId"	column="product_id"	/>
        <result property="productCode"	column="product_code"	/>
        <result property="productName"	column="product_name"	/>
        <result property="productSpc"	column="product_spc"	/>
        <result property="unit"	column="unit"	/>
        <result property="quantityProduced"	column="quantity_produced"	/>
        <result property="quantitySplit"	column="quantity_split"	/>
        <result property="routeCode"	column="route_code"	/>
        <result property="prodLineCode"	column="prod_line_code"	/>
        <result property="productDate"	column="product_date"	/>
        <result property="shiftId"	column="shift_id"	/>
        <result property="parentOrder"	column="parent_order"	/>
        <result property="ancestors"	column="ancestors"	/>
        <result property="status"	column="status"	/>
        <result property="remark"	column="remark"	/>
        <result property="attr1"	column="attr1"	/>
        <result property="attr2"	column="attr2"	/>
        <result property="attr3"	column="attr3"	/>
        <result property="attr4"	column="attr4"	/>
        <result property="createBy"	column="create_by"	/>
        <result property="createTime"	column="create_time"	/>
        <result property="updateBy"	column="update_by"	/>
        <result property="updateTime"	column="update_time"	/>
        <result property="prodType"	column="prod_type"	/>
        <result property="factoryCode"	column="factory_code"	/>
    </resultMap>

    <sql id="selectProOrderWorkorderVo">
		select workorder_id, workorder_code, workorder_name, order_id, order_code, product_id, product_code, product_name, product_spc, unit, quantity_produced, quantity_split, route_code, prod_line_code, product_date, shift_id, parent_order, ancestors, status, remark, attr1, attr2, attr3, attr4, create_by, create_time, update_by, update_time, prod_type, factory_code from pro_order_workorder
	</sql>

    <select id="selectProOrderWorkorderList" parameterType="ProOrderWorkorder" resultMap="ProOrderWorkorderResult">
        <include refid="selectProOrderWorkorderVo"/>
        <where>
            <if test="workorderCode != null  and workorderCode != ''"> and workorder_code = #{workorderCode}</if>
            <if test="workorderName != null  and workorderName != ''"> and workorder_name like concat('%', #{workorderName}, '%')</if>
            <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>
            <if test="orderCode != null  and orderCode != ''"> and order_code = #{orderCode}</if>
            <if test="productId != null  and productId != ''"> and product_id = #{productId}</if>
            <if test="productCode != null  and productCode != ''"> and product_code = #{productCode}</if>
            <if test="productName != null  and productName != ''"> and product_name like concat('%', #{productName}, '%')</if>
            <if test="productSpc != null  and productSpc != ''"> and product_spc = #{productSpc}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="quantityProduced != null "> and quantity_produced = #{quantityProduced}</if>
            <if test="quantitySplit != null "> and quantity_split = #{quantitySplit}</if>
            <if test="routeCode != null  and routeCode != ''"> and route_code = #{routeCode}</if>
            <if test="prodLineCode != null  and prodLineCode != ''"> and prod_line_code = #{prodLineCode}</if>
            <if test="productDate != null "> and product_date = #{productDate}</if>
            <if test="shiftId != null  and shiftId != ''"> and shift_id = #{shiftId}</if>
            <if test="parentOrder != null  and parentOrder != ''"> and parent_order = #{parentOrder}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="attr1 != null  and attr1 != ''"> and attr1 = #{attr1}</if>
            <if test="attr2 != null  and attr2 != ''"> and attr2 = #{attr2}</if>
            <if test="attr3 != null  and attr3 != ''"> and attr3 = #{attr3}</if>
            <if test="attr4 != null  and attr4 != ''"> and attr4 = #{attr4}</if>
            <if test="prodType != null  and prodType != ''"> and prod_type = #{prodType}</if>
            <if test="factoryCode != null  and factoryCode != ''"> and factory_code = #{factoryCode}</if>
        </where>
    </select>

    <select id="selectProOrderWorkorderByWorkorderId" parameterType="String" resultMap="ProOrderWorkorderResult">
        <include refid="selectProOrderWorkorderVo"/>
        where workorder_id = #{workorderId}
    </select>
    <select id="selectFirWorkOrder" parameterType="String" resultMap="ProOrderWorkorderResult">
        SELECT pow.*
        FROM pro_order_workorder pow LEFT JOIN pro_order po
        ON pow.order_id = po.id
        WHERE po.id = #{id}
    </select>
    <select id="selectChildWorkOrder" parameterType="String" resultMap="ProOrderWorkorderResult">
        <include refid="selectProOrderWorkorderVo"/>
        WHERE parent_order = #{workorderCode}
    </select>

    <insert id="insertProOrderWorkorder" parameterType="ProOrderWorkorder">
        insert into pro_order_workorder
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workorderId != null">workorder_id,</if>
            <if test="workorderCode != null and workorderCode != ''">workorder_code,</if>
            <if test="workorderName != null and workorderName != ''">workorder_name,</if>
            <if test="orderId != null and orderId != ''">order_id,</if>
            <if test="orderCode != null">order_code,</if>
            <if test="productId != null">product_id,</if>
            <if test="productCode != null and productCode != ''">product_code,</if>
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="productSpc != null">product_spc,</if>
            <if test="unit != null and unit != ''">unit,</if>
            <if test="quantityProduced != null">quantity_produced,</if>
            <if test="quantitySplit != null">quantity_split,</if>
            <if test="routeCode != null">route_code,</if>
            <if test="prodLineCode != null">prod_line_code,</if>
            <if test="productDate != null">product_date,</if>
            <if test="shiftId != null">shift_id,</if>
            <if test="parentOrder != null">parent_order,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="attr1 != null">attr1,</if>
            <if test="attr2 != null">attr2,</if>
            <if test="attr3 != null">attr3,</if>
            <if test="attr4 != null">attr4,</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>
            <if test="prodType != null">prod_type,</if>
            <if test="factoryCode != null">factory_code,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workorderId != null">#{workorderId},</if>
            <if test="workorderCode != null and workorderCode != ''">#{workorderCode},</if>
            <if test="workorderName != null and workorderName != ''">#{workorderName},</if>
            <if test="orderId != null and orderId != ''">#{orderId},</if>
            <if test="orderCode != null">#{orderCode},</if>
            <if test="productId != null">#{productId},</if>
            <if test="productCode != null and productCode != ''">#{productCode},</if>
            <if test="productName != null and productName != ''">#{productName},</if>
            <if test="productSpc != null">#{productSpc},</if>
            <if test="unit != null and unit != ''">#{unit},</if>
            <if test="quantityProduced != null">#{quantityProduced},</if>
            <if test="quantitySplit != null">#{quantitySplit},</if>
            <if test="routeCode != null">#{routeCode},</if>
            <if test="prodLineCode != null">#{prodLineCode},</if>
            <if test="productDate != null">#{productDate},</if>
            <if test="shiftId != null">#{shiftId},</if>
            <if test="parentOrder != null">#{parentOrder},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="attr1 != null">#{attr1},</if>
            <if test="attr2 != null">#{attr2},</if>
            <if test="attr3 != null">#{attr3},</if>
            <if test="attr4 != null">#{attr4},</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>
            <if test="prodType != null">#{prodType},</if>
            <if test="factoryCode != null">#{factoryCode},</if>
        </trim>
    </insert>

    <update id="updateProOrderWorkorder" parameterType="ProOrderWorkorder">
        update pro_order_workorder
        <trim prefix="SET" suffixOverrides=",">
            <if test="workorderCode != null and workorderCode != ''">workorder_code = #{workorderCode},</if>
            <if test="workorderName != null and workorderName != ''">workorder_name = #{workorderName},</if>
            <if test="orderId != null and orderId != ''">order_id = #{orderId},</if>
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="productCode != null and productCode != ''">product_code = #{productCode},</if>
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="productSpc != null">product_spc = #{productSpc},</if>
            <if test="unit != null and unit != ''">unit = #{unit},</if>
            <if test="quantityProduced != null">quantity_produced = #{quantityProduced},</if>
            <if test="quantitySplit != null">quantity_split = #{quantitySplit},</if>
            <if test="routeCode != null">route_code = #{routeCode},</if>
            <if test="prodLineCode != null">prod_line_code = #{prodLineCode},</if>
            <if test="productDate != null">product_date = #{productDate},</if>
            <if test="shiftId != null">shift_id = #{shiftId},</if>
            <if test="parentOrder != null">parent_order = #{parentOrder},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="attr1 != null">attr1 = #{attr1},</if>
            <if test="attr2 != null">attr2 = #{attr2},</if>
            <if test="attr3 != null">attr3 = #{attr3},</if>
            <if test="attr4 != null">attr4 = #{attr4},</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>
            <if test="prodType != null">prod_type = #{prodType},</if>
            <if test="factoryCode != null">factory_code = #{factoryCode},</if>
        </trim>
        where workorder_id = #{workorderId}
    </update>

    <delete id="deleteProOrderWorkorderByWorkorderId" parameterType="String">
		delete from pro_order_workorder where workorder_id = #{workorderId}
	</delete>

    <delete id="deleteProOrderWorkorderByWorkorderIds" parameterType="String">
        delete from pro_order_workorder where workorder_id in
        <foreach item="workorderId" collection="array" open="(" separator="," close=")">
            #{workorderId}
        </foreach>
    </delete>
</mapper>