<?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.cim.idm.dao.MesToWmsDao">
    <!--    查询完工报告-->
    <select id="getEndReportList" resultType="com.cim.idm.model.MaterialPackingSubDto">
        SELECT
            M.RECEIVEREQUESTNAME,
            M.RECEIVEREQUESTDETAILNAME,
            M.QTY
        FROM
            MATERIALPACKINGSUB M
        WHERE
            M.MATERIALPACKINGNAME = #{materialPackingName}
        UNION
        SELECT
            M.RECEIVEREQUESTNAME,
            M.RECEIVEREQUESTDETAILNAME,
            M.QTY
        FROM
            MATERIALPACKINGSUBHISTORY M
        WHERE
            M.MATERIALPACKINGNAME = #{materialPackingName}
    </select>
    <select id="getMaterialPackingList"
            resultType="com.cim.idm.data.changeOrder.changeOrderDto"
            parameterType="arraylist">
        SELECT
        T.MATERIALSPECNAME MATNR, T.ERPFACTORY WERKS,T.ERPLOCATION LGORT,
        T.MATERIALQUANTITY MENGE,T.MATERIALUNIT  MEINS, '' KOSTL, '' GRUND, '261' BWART,
        '' ZLLORDER, '' ZLLITEM, T.WO AUFNR
        FROM
        MATERIALPACKINGMDC T
        WHERE
        T.MATERIALSPECNAME IN
        <foreach collection="materialSpecnameList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND T.CHARGE IN
        <foreach collection="chargeList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND (CHANGEORDERFLAG !=  'T' OR CHANGEORDERFLAG IS NULL)
    </select>
    <select id="getMaterialPackingListByRK" resultType="com.cim.idm.data.changeOrder.changeOrderDto">
        SELECT
        T.MATERIALSPECNAME MATNR, T.ERPFACTORY WERKS,T.ERPLOCATION LGORT,
        T.MATERIALQUANTITY MENGE,T.MATERIALUNIT  MEINS, '' KOSTL, '' GRUND, '261' BWART,
        '' ZLLORDER, '' ZLLITEM, T.WO AUFNR
        FROM
        MATERIALPACKINGMDC T
        WHERE
        T.MATERIALSPECNAME IN
        <foreach collection="materialSpecnameList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND T.CHARGE IN
        <foreach collection="chargeList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND CHANGEORDERFLAG =  'T'
    </select>
    <!--    更新-->
    <update id="updateEndReport"  parameterType="map">
        UPDATE
            MATERIALRECEIVEREQUESTDETAIL T
        SET
            T.RECEIVEDQUANTITY = T.RECEIVEDQUANTITY - (
                SELECT
                    NVL(SUM(M.QTY), 0)
                FROM
                    MATERIALPACKINGSUB M
                WHERE
                    M.MATERIALPACKINGNAME = #{materialPackingName}
                  AND M.RECEIVEREQUESTNAME = T.RECEIVEREQUESTNAME
                  AND M.RECEIVEREQUESTDETAILNAME = T.RECEIVEREQUESTDETAILNAME
        WHERE
            T.RECEIVEREQUESTNAME = #{receiveRequestName}
          AND T.RECEIVEREQUESTDETAILNAME = #{receiveRequestDetailName}
    </update>

    <!--    更新-->
    <update id="updateEndReport2"  parameterType="map">
        UPDATE MATERIALRECEIVEREQUESTDETAIL T
        SET T.RECEIVEDQUANTITY = T.RECEIVEDQUANTITY - (
            SELECT NVL(SUM(M.QTY),0)
            FROM MATERIALPACKINGSUBHISTORY M
            WHERE M.MATERIALPACKINGNAME = #{materialPackingName}
              AND M.RECEIVEREQUESTNAME = T.RECEIVEREQUESTNAME
              AND M.RECEIVEREQUESTDETAILNAME = T.RECEIVEREQUESTDETAILNAME
        )
        WHERE T.RECEIVEREQUESTNAME = #{receiveRequestName}
          AND T.RECEIVEREQUESTDETAILNAME = #{receiveRequestDetailName}
    </update>

    <delete id="deleteSub" parameterType="map">
        DELETE
        FROM
            MATERIALPACKINGSUB T
        WHERE
            T.RECEIVEREQUESTNAME = #{receiveRequestName}
          AND
            T.RECEIVEREQUESTDETAILNAME = #{receiveRequestDetailName}
          AND T.MATERIALPACKINGNAME = #{materialPackingName}
    </delete>

    <!--    更新-->
    <update id="updateBsMesShipped"  parameterType="map">
        UPDATE BS_MES_SHIPPED T SET T.RECEIVE_FLAG = 'N' WHERE T.LOTNAME = #{materialPackingName}
    </update>

    <update id="updateUnDoId"  parameterType="map">
        UPDATE IF_ERPUNDO T SET T.FLAG = 'Y' WHERE  T.UNDOID = #{undoId}
    </update>

    <update id="updateMDCFlag">
        UPDATE MATERIALPACKINGMDC T SET T.CHANGEORDERFLAG = 'T' WHERE T.MATERIALSPECNAME in
        <foreach collection="materialSpecnameList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND T.CHARGE in
        <foreach collection="chargeList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>

    </update>
    <update id="updateWO">
        UPDATE MATERIALPACKINGMDC T SET T.WO = #{t} WHERE T.MATERIALSPECNAME in
        <foreach collection="materialSpecnameList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND T.CHARGE in
        <foreach collection="chargeList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

</mapper>