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

    <resultMap type="com.ximai.mes.pro.domain.proWorkOrder.ProWorkorder" id="ProWorkorderResult">
        <result property="workorderId" column="workorder_id"/>
        <result property="workorderCode" column="workorder_code"/>
        <result property="workorderName" column="workorder_name"/>
        <result property="workorderType" column="workorder_type"/>
        <result property="orderSource" column="order_source"/>
        <result property="sourceCode" column="source_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="unitOfMeasure" column="unit_of_measure"/>
        <result property="batchCode" column="batch_code"/>
        <result property="arrangeCode" column="arrange_code"/>
        <result property="quantity" column="quantity"/>
        <result property="quantityProduced" column="quantity_produced"/>
        <result property="quantityChanged" column="quantity_changed"/>
        <result property="quantityScheduled" column="quantity_scheduled"/>
        <result property="clientId" column="client_id"/>
        <result property="clientCode" column="client_code"/>
        <result property="clientName" column="client_name"/>
        <result property="vendorId" column="vendor_id"/>
        <result property="vendorCode" column="vendor_code"/>
        <result property="vendorName" column="vendor_name"/>
        <result property="salesVoucherItem" column="sales_voucher_item"/>
        <result property="salesVoucher" column="sales_voucher"/>
        <result property="requestDate" column="request_date"/>
        <result property="parentId" column="parent_id"/>
        <result property="ancestors" column="ancestors"/>
        <result property="finishDate" column="finish_date"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <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="combinationCode" column="combination_code"/>
        <result property="factoryCode" column="factory_code"/>
        <result property="startDate" column="start_date"/>
        <result property="productionManagerId" column="production_manager_id"/>
        <result property="storageLocation" column="storage_location"/>
        <result property="dispatchMultiple" column="dispatch_multiple"/>
        <result property="rowNum" column="row_num"/>
        <result property="singleWeight" column="single_weight"/>
        <result property="thickness" column="thickness"/>
        <result property="materialsGroupBill" column="materials_group_bill"/>
        <result property="alternativeBill" column="alternative_bill"/>
        <result property="packNum" column="pack_num"/>
        <result property="packType" column="pack_type"/>
        <result property="packUnitOfMeasure" column="pack_unit_of_measure"/>
        <result property="lossIndividuallyWrap" column="loss_individually_wrap"/>
        <result property="epItemCode" column="ep_item_code"/>
        <result property="usagePackNum" column="usage_pack_num"/>
        <result property="usageEncasementNum" column="usage_encasement_num"/>
        <result property="mergePack" column="merge_pack"/>
        <result property="itemTypeName" column="item_type_name"/>
        <result property="batchSerial" column="batch_serial"/>
        <result property="productionSolutionId" column="production_solution_id"/>
        <result property="productionSolutionCode" column="production_solution_code"/>
        <result property="productionSolutionName" column="production_solution_name"/>

    </resultMap>

    <sql id="selectProWorkorderVo">
        select pw.*,
               pc.combination_code,
               pa.arrange_code,
               md.item_type_name
        from pro_workorder pw
                 left join pro_combination pc on pw.workorder_id = pc.workorder_id
                 left join pro_arrange pa on pw.workorder_id = pa.workorder_id
                 left join md_item md on md.item_id = pw.product_id
    </sql>

    <select id="selectProWorkorderList" parameterType="ProWorkorderQuery" resultMap="ProWorkorderResult">
        <include refid="selectProWorkorderVo"/>
        <where>
            <if test="workorderCode != null  and workorderCode != ''">and pw.workorder_code like
                concat('%',#{workorderCode}
                , '%')
            </if>
            <if test="workorderName != null  and workorderName != ''">and pw.workorder_name like concat('%',
                #{workorderName}, '%')
            </if>
            <if test="workorderType != null and workorderType !=''">and pw.workorder_type = #{workorderType}</if>
            <if test="orderSource != null  and orderSource != ''">and pw.order_source = #{orderSource}</if>
            <if test="sourceCode != null  and sourceCode != ''">and pw.source_code = #{sourceCode}</if>
            <if test="productId != null ">and pw.product_id = #{productId}</if>
            <if test="productCode != null  and productCode != ''">and pw.product_code = #{productCode}</if>
            <if test="productName != null  and productName != ''">and pw.product_name like concat('%', #{productName},
                '%')
            </if>
            <if test="productSpc != null  and productSpc != ''">and pw.product_spc = #{productSpc}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and pw.unit_of_measure = #{unitOfMeasure}</if>
            <if test="batchCode !=null and batchCode !=''">and pw.batch_code = #{batchCode}</if>
            <if test="quantity != null ">and pw.quantity = #{quantity}</if>
            <if test="quantityProduced != null ">and pw.quantity_produced = #{quantityProduced}</if>
            <if test="quantityChanged != null ">and pw.quantity_changed = #{quantityChanged}</if>
            <if test="quantityScheduled != null ">and pw.quantity_scheduled = #{quantityScheduled}</if>
            <if test="clientId != null ">and pw.client_id = #{clientId}</if>
            <if test="clientCode != null  and clientCode != ''">and pw.client_code = #{clientCode}</if>
            <if test="clientName != null  and clientName != ''">and pw.client_name like concat('%', #{clientName},
                '%')
            </if>
            <if test="vendorId != null">and pw.vendor_id = #{vendorId}</if>
            <if test="itemTypeName != null">and md.item_type_name like concat('%',#{itemTypeName},'%')</if>
            <if test="vendorCode != null and vendorCode !=''">and pw.vendor_code = #{vendorCode}</if>
            <if test="vendorName != null and vendorName !=''">and pw.vendor_name like concat('%',#{vendorName},'%')</if>
            <if test="params.beginPurchaseDate != null and params.beginPurchaseDate != '' and params.endPurchaseDate != null and params.endPurchaseDate != ''">
                and pw.request_date between #{params.beginPurchaseDate} and #{params.endPurchaseDate}
            </if>
            <if test="requestDateFrom != null ">and pw.request_date &gt;= #{requestDateFrom}</if>
            <if test="requestDateTo != null ">and pw.request_date &lt; #{requestDateTo}</if>
            <if test="finishDate != null ">and pw.finish_date = #{finishDate}</if>
            <if test="parentId != null ">and pw.parent_id = #{parentId}</if>
            <if test="status != null  and status != ''">and pw.status = #{status}</if>
            <if test="arrangeCode != null and arrangeCode !=''">and pw.arrange_code = #{arrangeCode}</if>
            <if test="statusArr != null">and pw.status in
                <foreach item="status" collection="statusArr" open="(" separator="," close=")">
                    #{status}
                </foreach>
            </if>
        </where>
        order by pw.create_time desc
    </select>

    <select id="selectWorkorderById" parameterType="Long" resultMap="ProWorkorderResult">
        <include refid="selectProWorkorderVo"/>
        where pw.workorder_id = #{workorderId}
    </select>

    <select id="selectProWorkorderGorupArrangeByWorkorderIds" parameterType="String" resultMap="ProWorkorderResult">
        <include refid="selectProWorkorderVo"/>
        where pw.workorder_id IN
        (SELECT DISTINCT t1.workorder_id
        FROM pro_arrange t1
        LEFT JOIN pro_arrange t2 ON t1.arrange_code = t2.arrange_code
        WHERE t2.workorder_id IN
        <foreach item="workorderId" collection="array" open="(" separator="," close=")">
            #{workorderId}
        </foreach>
        )
        ORDER BY pa.arrange_code
    </select>


    <select id="selectProWorkorderByTaskId" parameterType="ProWorkorder" resultMap="ProWorkorderResult">
        SELECT
        w.workorder_id,
        w.workorder_code,
        w.workorder_name,
        w.quantity,
        tw.idx,
        IFNULL(SUM(fb.quantity_feedback), 0) quantity_produced
        FROM
        pro_task_workorder tw
        LEFT JOIN pro_workorder w ON tw.workorder_id = w.workorder_id
        LEFT JOIN pro_feedback fb on w.workorder_id =fb.workorder_id and fb.task_id=tw.task_id
        <where>
            <if test="taskId != null">and tw.task_id = #{taskId}</if>
        </where>
        GROUP BY w.workorder_id,
        w.workorder_code,
        w.workorder_name,
        w.quantity,
        tw.idx
        ORDER BY tw.idx ASC
    </select>

    <insert id="insertProWorkorder" parameterType="ProWorkorder" useGeneratedKeys="true" keyProperty="workorderId">
        insert into pro_workorder
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workorderCode != null and workorderCode != ''">workorder_code,</if>
            <if test="workorderName != null and workorderName != ''">workorder_name,</if>
            <if test="workorderType != null and workorderType != ''">workorder_type,</if>
            <if test="salesVoucher != null and salesVoucher != ''">sales_voucher,</if>
            <if test="salesVoucherItem != null and salesVoucherItem != ''">sales_voucher_item,</if>
            <if test="orderSource != null and orderSource != ''">order_source,</if>
            <if test="sourceCode != null">source_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="unitOfMeasure != null and unitOfMeasure != ''">unit_of_measure,</if>
            <if test="batchCode !=null and batchCode !=''">batch_code,</if>
            <if test="quantity != null">quantity,</if>
            <if test="quantityProduced != null">quantity_produced,</if>
            <if test="quantityQualify != null">quantity_qualify,</if>
            <if test="quantityUnqualify != null">quantity_unqualify,</if>
            <if test="quantityChanged != null">quantity_changed,</if>
            <if test="quantityScheduled != null">quantity_scheduled,</if>
            <if test="clientId != null">client_id,</if>
            <if test="clientCode != null">client_code,</if>
            <if test="clientName != null">client_name,</if>
            <if test="vendorId !=null">vendor_id,</if>
            <if test="vendorCode !=null">vendor_code,</if>
            <if test="vendorName !=null">vendor_name,</if>
            <if test="requestDate != null">request_date,</if>
            <if test="finishDate != null">finish_date,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null and ancestors != ''">ancestors,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="factoryCode != null">factory_code,</if>
            <if test="startDate != null">start_date,</if>
            <if test="productionManagerId != null">production_manager_id,</if>
            <if test="storageLocation != null">storage_location,</if>
            <if test="dispatchMultiple != null">dispatch_multiple,</if>
            <if test="rowNum != null">row_num,</if>
            <if test="singleWeight != null">single_weight,</if>
            <if test="thickness != null">thickness,</if>
            <if test="materialsGroupBill != null">materials_group_bill,</if>
            <if test="alternativeBill != null">alternative_bill,</if>
            <if test="packNum != null">pack_num,</if>
            <if test="packType != null">pack_type,</if>
            <if test="packUnitOfMeasure != null">pack_unit_of_measure,</if>
            <if test="lossIndividuallyWrap != null">loss_individually_wrap,</if>
            <if test="epItemCode != null">ep_item_code,</if>
            <if test="sapItemCode != null">sap_item_code,</if>
            <if test="usagePackNum != null">usage_pack_num,</if>
            <if test="usageEncasementNum != null">usage_encasement_num,</if>
            <if test="mergePack != null">merge_pack,</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="startSerial != null">start_serial,</if>
            <if test="endSerial != null">end_serial,</if>
            <if test="batchSerial != null">batch_serial,</if>
            <if test="productionSolutionId != null">production_solution_id,</if>
            <if test="productionSolutionCode != null">production_solution_code,</if>
            <if test="productionSolutionName != null">production_solution_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workorderCode != null and workorderCode != ''">#{workorderCode},</if>
            <if test="workorderName != null and workorderName != ''">#{workorderName},</if>
            <if test="workorderType != null and workorderType != ''">#{workorderType},</if>
            <if test="salesVoucher != null and salesVoucher != ''">#{salesVoucher},</if>
            <if test="salesVoucherItem != null and salesVoucherItem != ''">#{salesVoucherItem},</if>
            <if test="orderSource != null and orderSource != ''">#{orderSource},</if>
            <if test="sourceCode != null">#{sourceCode},</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="unitOfMeasure != null and unitOfMeasure != ''">#{unitOfMeasure},</if>
            <if test="batchCode !=null and batchCode !=''">#{batchCode},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="quantityProduced != null">#{quantityProduced},</if>
            <if test="quantityQualify != null">#{quantityQualify},</if>
            <if test="quantityUnqualify != null">#{quantityUnqualify},</if>
            <if test="quantityChanged != null">#{quantityChanged},</if>
            <if test="quantityScheduled != null">#{quantityScheduled},</if>
            <if test="clientId != null">#{clientId},</if>
            <if test="clientCode != null">#{clientCode},</if>
            <if test="clientName != null">#{clientName},</if>
            <if test="vendorId !=null">#{vendorId},</if>
            <if test="vendorCode !=null">#{vendorCode},</if>
            <if test="vendorName !=null">#{vendorName},</if>
            <if test="requestDate != null">#{requestDate},</if>
            <if test="finishDate != null">#{finishDate},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="factoryCode != null">#{factoryCode},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="productionManagerId != null">#{productionManagerId},</if>
            <if test="storageLocation != null">#{storageLocation},</if>
            <if test="dispatchMultiple != null">#{dispatchMultiple},</if>
            <if test="rowNum != null">#{rowNum},</if>
            <if test="singleWeight != null">#{singleWeight},</if>
            <if test="thickness != null">#{thickness},</if>
            <if test="materialsGroupBill != null">#{materialsGroupBill},</if>
            <if test="alternativeBill != null">#{alternativeBill},</if>
            <if test="packNum != null">#{packNum},</if>
            <if test="packType != null">#{packType},</if>
            <if test="packUnitOfMeasure != null">#{packUnitOfMeasure},</if>
            <if test="lossIndividuallyWrap != null">#{lossIndividuallyWrap},</if>
            <if test="epItemCode != null">#{epItemCode},</if>
            <if test="sapItemCode != null">#{sapItemCode},</if>
            <if test="usagePackNum != null">#{usagePackNum},</if>
            <if test="usageEncasementNum != null">#{usageEncasementNum},</if>
            <if test="mergePack != null">#{mergePack},</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="startSerial != null">#{startSerial},</if>
            <if test="endSerial != null">#{endSerial},</if>
            <if test="batchSerial != null">#{batchSerial},</if>
            <if test="productionSolutionId != null">#{productionSolutionId},</if>
            <if test="productionSolutionCode != null">#{productionSolutionCode},</if>
            <if test="productionSolutionName != null">#{productionSolutionName},</if>
        </trim>
    </insert>


    <update id="updateWoStatusByArrangeCode" parameterType="ProWorkorder">
        update pro_workorder
        <trim prefix="SET" suffixOverrides=",">
            <if test="updateBy != null">update_by=#{updateBy},</if>
            <if test="updateTime != null">update_time=#{updateTime},</if>
            <if test="status != null and status != ''">status = #{status},</if>
        </trim>
        where arrange_code = #{arrangeCode}
    </update>


    <update id="updateProWorkorder" parameterType="ProWorkorder">
        update pro_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="workorderType != null and workorderType != ''">workorder_type = #{workorderType},</if>
            <if test="salesVoucher != null and salesVoucher != ''">sales_voucher=#{salesVoucher},</if>
            <if test="salesVoucherItem != null and salesVoucherItem != ''">sales_voucher_item=#{salesVoucherItem},</if>
            <if test="orderSource != null and orderSource != ''">order_source = #{orderSource},</if>
            <if test="sourceCode != null">source_code = #{sourceCode},</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="unitOfMeasure != null and unitOfMeasure != ''">unit_of_measure = #{unitOfMeasure},</if>
            <if test="batchCode !=null and batchCode !=''">batch_code = #{batchCode},</if>
            <if test="arrangeCode !=null">arrange_code = #{arrangeCode},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="quantityProduced != null">quantity_produced = #{quantityProduced},</if>
            <if test="quantityQualify != null">quantity_qualify = #{quantityQualify},</if>
            <if test="quantityUnqualify != null">quantity_unqualify = #{quantityUnqualify},</if>
            <if test="quantityChanged != null">quantity_changed = #{quantityChanged},</if>
            <if test="quantityScheduled != null">quantity_scheduled = #{quantityScheduled},</if>
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="clientCode != null">client_code = #{clientCode},</if>
            <if test="epItemCode != null">ep_item_code = #{epItemCode},</if>
            <if test="sapItemCode != null">sap_item_code = #{sapItemCode},</if>
            <if test="clientName != null">client_name = #{clientName},</if>
            <if test="vendorId !=null">vendor_id = #{vendorId},</if>
            <if test="vendorCode !=null">vendor_code = #{vendorCode},</if>
            <if test="vendorName !=null">vendor_name = #{vendorName},</if>
            <if test="requestDate != null">request_date = #{requestDate},</if>
            <if test="finishDate != null">finish_date = #{finishDate},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</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="factoryCode != null">factory_code = #{factoryCode},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="productionManagerId != null">production_manager_id = #{productionManagerId},</if>
            <if test="storageLocation != null">storage_location = #{storageLocation},</if>
            <if test="dispatchMultiple != null">dispatch_multiple = #{dispatchMultiple},</if>
            <if test="rowNum != null">row_num = #{rowNum},</if>
            <if test="singleWeight != null">single_weight = #{singleWeight},</if>
            <if test="thickness != null">thickness = #{thickness},</if>
            <if test="materialsGroupBill != null">materials_group_bill = #{materialsGroupBill},</if>
            <if test="alternativeBill != null">alternative_bill = #{alternativeBill},</if>
            <if test="packNum != null">pack_num = #{packNum},</if>
            <if test="packType != null">pack_type = #{packType},</if>
            <if test="packUnitOfMeasure != null">pack_unit_of_measure = #{packUnitOfMeasure},</if>
            <if test="lossIndividuallyWrap != null">loss_individually_wrap=#{lossIndividuallyWrap},</if>
            <if test="usagePackNum != null">usage_pack_num=#{usagePackNum},</if>
            <if test="usageEncasementNum != null">usage_encasement_num=#{usageEncasementNum},</if>
            <if test="mergePack != null">merge_pack=#{mergePack},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="batchSerial != null">batch_serial=#{batchSerial},</if>
            <if test="productionSolutionId != null">production_solution_id=#{productionSolutionId},</if>
            <if test="productionSolutionCode != null">production_solution_code = #{productionSolutionCode},</if>
            <if test="productionSolutionName != null">production_solution_name = #{productionSolutionName},</if>
        </trim>
        where workorder_id = #{workorderId}
    </update>


    <update id="updateStatusByCode" parameterType="ProWorkorder">
        update pro_workorder
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">`status` = #{status},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where workorder_code = #{workorderCode}
    </update>
    <update id="updateWorkorderStatus">
        update pro_workorder pr
        set pr.status = #{status}
        where pr.workorder_id = #{workorderId}
    </update>

    <delete id="deleteProWorkorderByWorkorderId" parameterType="Long">
        delete
        from pro_workorder
        where workorder_id = #{workorderId}
    </delete>

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