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

    <insert id="createDelivery">
        INSERT INTO MATERIALDELIVERY(SITENAME, DELIVERYNAME, DELIVERYTYPE, DELIVERYSTATE)
        VALUES (#{dto.siteName}, #{dto.deliveryName}, #{dto.deliveryType}, #{state})
    </insert>

    <insert id="createRelation">
        INSERT ALL
        <foreach item="item" index="index" collection="receiveList">
            INTO MATERIALDELIVERYRECEIVE (RECEIVEREQUESTNAME, DELIVERYNAME, DELIVERYNUM, MATERIALSPECNAME, RECEIVEREQUESTDETAILNAME) VALUES (#{item.receiveRequestName}, #{item.deliveryName}, #{item.deliveryNum}, #{item.materialSpecName}, #{item.receiveRequestDetailName})
        </foreach>
        SELECT * FROM dual
    </insert>

    <resultMap id="MaterialReceiveRequestVo" type="com.cim.idm.model.vo.MaterialReceiveRequestVo">
        <result property="siteName" column="SITENAME" />
        <result property="receiveRequestName" column="RECEIVEREQUESTNAME" />
        <result property="receiveRequestType" column="RECEIVEREQUESTTYPE" />
        <result property="orderDate" column="ORDERDATE" />
        <result property="supplierNo" column="SUPPLIERNO" />
        <result property="lastEventName" column="LASTEVENTNAME" />
        <result property="lastEventTime" column="LASTEVENTTIME" />
        <result property="lastEventUser" column="LASTEVENTUSER" />
        <result property="lastEventComment" column="LASTEVENTCOMMENT" />
        <result property="supplierName" column="SUPPLIERNAME" />
        <result property="erpFactory" column="ERPFACTORY" />
        <result property="orgName" column="ORGNAME" />
<!--        <result property="materialSpecName" column="MATERIALSPECNAME" />-->
<!--        <result property="descCn" column="DESC_CN" />-->
        <result property="receiveRequestState" column="RECEIVEREQUESTSTATE" />
        <result property="oemSiteName" column="OEMSITENAME" />
        <result property="oemSiteInCharge" column="OEMSITEINCHARGE" />
        <result property="lastEventTimeKey" column="LASTEVENTTIMEKEY" />
        <result property="systemType" column="SYSTEMTYPE" />
        <result property="purchaseOrderGroup" column="PURCHASEORDERGROUP" />
    </resultMap>

    <select id="getReceiveList" resultMap="MaterialReceiveRequestVo">
        SELECT DISTINCT m.SITENAME, m.RECEIVEREQUESTNAME, m.RECEIVEREQUESTTYPE, m.ORDERDATE, m.SUPPLIERNO,
                        m.LASTEVENTNAME, m.LASTEVENTTIME, m.LASTEVENTUSER, m.LASTEVENTCOMMENT, s.SUPPLIERNAME,
                        STOCKORGNO ERPFACTORY, o.ORGNAME,
                        CASE WHEN m.RECEIVEREQUESTSTATE = 'Completed' THEN '完成' ELSE '未完成' END RECEIVEREQUESTSTATE,
                        m.OEMSITENAME, m.OEMSITEINCHARGE, m.LASTEVENTTIMEKEY, m.SYSTEMTYPE, m.PURCHASEORDERGROUP
        FROM MATERIALRECEIVEREQUEST m
        LEFT JOIN SUPPLIER s ON s.SUPPLIERNO = m.SUPPLIERNO
        LEFT JOIN MATERIALRECEIVEREQUESTDETAIL d ON m.RECEIVEREQUESTNAME = d.RECEIVEREQUESTNAME
        LEFT JOIN MATERIALSPEC ms ON d.MATERIALSPECNAME = ms.MATERIALSPECNAME
        LEFT JOIN ORG o ON STOCKORGNO = o.ORGNO
        WHERE m.SITENAME = #{dto.siteName}
        ANd ms.is_batch = #{isBatch}
        AND m.RECEIVEREQUESTTYPE IN
        <foreach item="type" index="index" collection="typeList" open="(" separator="," close=")">
            #{type}
        </foreach>
        <if test="dto.erpFactory != null and dto.erpFactory != ''">
            AND m.STOCKORGNO = #{dto.erpFactory}
        </if>
        <if test="dto.receiveRequestName != null and dto.receiveRequestName != ''">
            AND m.RECEIVEREQUESTNAME LIKE '%'||#{dto.receiveRequestName}||'%'
        </if>
        <if test="dto.receiveRequestState != null and dto.receiveRequestState != ''">
            AND m.RECEIVEREQUESTSTATE = #{dto.receiveRequestState}
        </if>
        <if test="dto.materialSpecName != null and dto.materialSpecName != ''">
            AND ms.MATERIALSPECNAME LIKE '%'||#{dto.materialSpecName}||'%'
        </if>
        <if test="dto.descCn != null and dto.descCn != ''">
            AND ms.DESC_CN LIKE '%'||#{dto.descCn}||'%'
        </if>
        <if test="dto.fromDate != null and dto.fromDate != '' and dto.toDate != null and dto.toDate != ''">
            AND m.ORDERDATE BETWEEN TO_DATE( #{dto.fromDate}, 'YYYY/MM/DDHH24MISS' )
            AND TO_DATE( #{dto.toDate}, 'YYYY/MM/DDHH24MISS' )
        </if>
        <if test="dto.supplierNo != null and dto.supplierNo != ''">
            AND m.SUPPLIERNO LIKE '%'||#{dto.supplierNo}||'%'
        </if>
        AND ms.IS_BATCH = #{isBatch}
    </select>

    <!-- GetInvoiceDetailInformationRow Z10006_SAP -->
    <select id="getStoreDetail" resultType="map">
        WITH AA AS (
            SELECT m5.RECEIVEREQUESTNAME, m5.MATERIALSPECNAME, m5.SDK_ID, m5.phase, SUM( m5.MATERIALQUANTITY ) QTY
            FROM MATERIALPACKING m5
            WHERE m5.STOCKSTATE IN ( 'Stocked', 'StockOut' )
                AND m5.RECEIVEREQUESTNAME in
                <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
                    #{name}
                </foreach>
            GROUP BY m5.MATERIALSPECNAME, m5.RECEIVEREQUESTNAME, m5.SDK_ID, m5.phase
        ),
        BB AS (
            SELECT m5.RECEIVEREQUESTNAME, m5.MATERIALSPECNAME, m5.SDK_ID, m5.phase, SUM( m5.MATERIALQUANTITY ) QTY
            FROM MATERIALPACKING m5
            WHERE m5.STOCKSTATE = 'Created'
                AND m5.RECEIVEREQUESTNAME in
                <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
                    #{name}
                </foreach>
            GROUP BY m5.MATERIALSPECNAME, m5.RECEIVEREQUESTNAME, m5.SDK_ID, m5.phase
        ),
        MAIN AS (
            SELECT m.MATERIALSPECNAME,
            SUM( m.REQUESTQUANTITY ) REQUESTQUANTITY,
            SUM( m.RECEIVEDQUANTITY ) RECEIVEDQUANTITY,
            SUM( m.REQUESTQUANTITY - m.RECEIVEDQUANTITY ) AS RECEIVEDQUANTITY2,
            SUM( m.REQUESTQUANTITY - m.RECEIVEDQUANTITY ) AS MINUSQTY
            FROM MATERIALRECEIVEREQUESTDETAIL m
            WHERE m.RECEIVEREQUESTNAME in
            <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
                #{name}
            </foreach>
            GROUP BY m.MATERIALSPECNAME
        )
        <if test="deliveryName != null">
            ,DELIVERY AS (
                SELECT sum(md.DELIVERYNUM) DELIVERYNUM, md.MATERIALSPECNAME MATERIALSPECNAME
                FROM MATERIALDELIVERYRECEIVE md
                WHERE md.DELIVERYNAME = #{deliveryName}
                and md.RECEIVEREQUESTNAME IN
                <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
                    #{name}
                </foreach>
                GROUP BY md.MATERIALSPECNAME
            )
        </if>
        SELECT distinct MAIN.RECEIVEDQUANTITY, MAIN.MINUSQTY,
        <if test="deliveryName != null">
            DELIVERY.DELIVERYNUM REQUESTQUANTITY,
        </if>
        <if test="deliveryName == null">
            MAIN.REQUESTQUANTITY REQUESTQUANTITY,
        </if>
        TO_CHAR( SYSDATE, 'YYYY-MM-DD' ) RECEIVETIME, TO_CHAR( SYSDATE, 'YYYY-MM-DD' ) MAKEDATE, '1' RECEIVEREQUESTDETAILNAME, '是' IS_BATCH,
        m.MATERIALSPECNAME, m.SITENAME, m.ERPLOCATION, m.LOCATIONNAME, m.MATERIALUNIT, m.SDK_ID,
        m.REMARK REMARK, m.SALESMAN SALESPERSON, m.SALESASSISTANT CAREER_ASSISTANCE, m.BUSINESSDIVISION BUSINESS_UNIT,
--         m2.STOCKORGNO AS ERPFACTORY,
        COALESCE(m2.STOCKORGNO, m.ERPFACTORY) AS ERPFACTORY,
        m2.SUPPLIERNO,m.POCATEGORY,
        m3.WIDTH AS DESCRIPTION, m3.MATERIALUNITDESC, m3.WIDTH SPECNAME, m3.PHASE, m3.DESC_CN, DECODE( m3.IS_BATCH, 'Y', '是' , '否' ) IS_BATCH,
        CASE WHEN m3.EXPIRATION_RULES = '0' THEN '入厂保质期' WHEN m3.EXPIRATION_RULES = '1' THEN '出厂保质期' ELSE '未定义' END AS REPLACED_VALUE,
        DECODE( m4.IQCFLAG, 'Y', '是', '否' ) IQCFLAG,
        be.DESCRIPTION AS ERPLOCATION2, o.ORGNAME AS ERPFACTORY2, sss.DESCRIPTION AS LOCATIONNAME2,
        NVL( BB.QTY, 0 ) CREATEQTY, NVL( AA.QTY, 0 ) STOCKINQTY
        FROM MAIN
        LEFT JOIN MATERIALRECEIVEREQUESTDETAIL m ON MAIN.MATERIALSPECNAME = m.MATERIALSPECNAME
        LEFT JOIN MATERIALRECEIVEREQUEST m2 ON m.RECEIVEREQUESTNAME = m2.RECEIVEREQUESTNAME
        LEFT JOIN MATERIALSPEC m3 ON m.MATERIALSPECNAME = m3.MATERIALSPECNAME
        LEFT JOIN BS_MATERIALFACTORY m4 ON m.MATERIALSPECNAME = m4.MATERIALSPECNAME AND m.ERPFACTORY = m4.ERPFACTORY
        LEFT JOIN SUPPLIER S ON m2.SUPPLIERNO = S.SUPPLIERNO
        LEFT JOIN BS_ERPLOCATION be ON m.ERPLOCATION = be.ERPLOCATIONNAME
        LEFT JOIN ORG o ON o.ORGNO = m2.STOCKORGNO
        LEFT JOIN STORAGESPEC sss ON m.LOCATIONNAME = sss.STORAGENAME
        LEFT JOIN AA ON AA.RECEIVEREQUESTNAME = m.RECEIVEREQUESTNAME
            AND AA.MATERIALSPECNAME = m.MATERIALSPECNAME
            AND AA.SDK_ID = m.SDK_ID AND AA.PHASE = m3.PHASE
        LEFT JOIN BB ON BB.RECEIVEREQUESTNAME = m.RECEIVEREQUESTNAME
            AND BB.MATERIALSPECNAME = m.MATERIALSPECNAME
            AND COALESCE(BB.SDK_ID, 'N/A') = COALESCE(m.SDK_ID, 'N/A')
            AND COALESCE(BB.PHASE, 'N/A') = COALESCE(m3.PHASE, 'N/A')
        <if test="deliveryName != null">
            INNER JOIN DELIVERY ON DELIVERY.MATERIALSPECNAME = m.MATERIALSPECNAME
        </if>
        WHERE ( m2.RETURNFLAG IS NULL OR m2.RETURNFLAG != 'Y' )
            AND m.RECEIVEREQUESTNAME in
            <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
                #{name}
            </foreach>
            AND m.SITENAME = #{siteName}
            AND (m.DELETEDETAILFLAG IS NULL OR m.DELETEDETAILFLAG !='L')
            AND m3.IS_BATCH != 'N'
<!--            AND m2.RECEIVEREQUESTTYPE IN-->
<!--            <foreach item="type" index="index" collection="typeList" open="(" separator="," close=")">-->
<!--                #{type}-->
<!--            </foreach>-->
--         ZLR1 销售退货类型
    </select>

    <!-- GetNoBatchPOStockIn 00001 -->
    <select id="getMaterialDetail" resultType="map">
        WITH MAIN AS (
            SELECT m.MATERIALSPECNAME,
            SUM( m.REQUESTQUANTITY ) REQUESTQUANTITY,
            SUM( m.RECEIVEDQUANTITY ) RECEIVEDQUANTITY,
            SUM( m.REQUESTQUANTITY - m.RECEIVEDQUANTITY)  AS ThisAcceptNumber
            FROM MATERIALRECEIVEREQUESTDETAIL m
            WHERE m.RECEIVEREQUESTNAME in
            <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
                #{name}
            </foreach>
            GROUP BY m.MATERIALSPECNAME
        )
        <if test="deliveryName != null">
            ,DELIVERY AS (
                SELECT sum(md.DELIVERYNUM) DELIVERYNUM, md.MATERIALSPECNAME MATERIALSPECNAME
                FROM MATERIALDELIVERYRECEIVE md
                WHERE md.DELIVERYNAME = #{deliveryName}
                and md.RECEIVEREQUESTNAME IN
                <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
                    #{name}
                </foreach>
                GROUP BY md.MATERIALSPECNAME
            )
        </if>
        SELECT distinct MAIN.RECEIVEDQUANTITY, MAIN.ThisAcceptNumber,
        <if test="deliveryName != null">
            DELIVERY.DELIVERYNUM REQUESTQUANTITY,
        </if>
        <if test="deliveryName == null">
            MAIN.REQUESTQUANTITY REQUESTQUANTITY,
        </if>
        m3.DESCRIPTION, be.DESCRIPTION AS ERPLOCATION2, m.ERPLOCATION, o.ORGNAME AS ERPFACTORY2,
        m2.STOCKORGNO AS ERPFACTORY, sss.DESCRIPTION AS LOCATIONNAME2, m.LOCATIONNAME, m2.SUPPLIERNO,
        ss.SPECNAME, m.PHASE, m3.DESC_CN, m.MATERIALSPECNAME,
        m.MATERIALUNIT, MM.FNAME MATERIALUNITDESC, m.SDK_ID, m.PHASE, m.SITENAME,
        DECODE(m3.IS_BATCH, 'Y', '是' , '否') IS_BATCH, DECODE(m3.IQCFLAG, 'Y', '是' ,'否') IQCFLAG,
        CASE WHEN m3.EXPIRATION_RULES = '0' THEN '不遵守有效期管理'
            WHEN m3.EXPIRATION_RULES = '1' THEN '遵守有效期管理'
            ELSE '未定义'
        END AS REPLACED_VALUE
        FROM MAIN
        LEFT JOIN MATERIALRECEIVEREQUESTDETAIL m ON MAIN.MATERIALSPECNAME = m.MATERIALSPECNAME
        LEFT JOIN MATERIALRECEIVEREQUEST m2 ON m.RECEIVEREQUESTNAME = m2.RECEIVEREQUESTNAME
        LEFT JOIN MATERIALSPEC m3 ON m.MATERIALSPECNAME = m3.MATERIALSPECNAME
        LEFT JOIN SDK_SPEC ss ON m.SDK_ID = ss.SDK_ID
        LEFT JOIN MATERIALUNIT MM ON MM.FNUMBER = m.MATERIALUNIT
        LEFT JOIN BS_ERPLOCATION be ON m.ERPLOCATION = be.ERPLOCATIONNAME
        LEFT JOIN ORG o ON o.ORGNO = m2.STOCKORGNO
        LEFT JOIN STORAGESPEC sss ON m.LOCATIONNAME = sss.STORAGENAME
        <if test="deliveryName != null">
            INNER JOIN DELIVERY ON DELIVERY.MATERIALSPECNAME = m.MATERIALSPECNAME
        </if>
        WHERE m.RECEIVEREQUESTNAME in
        <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
            #{name}
        </foreach>
        AND m.SITENAME = #{siteName}
        AND (m.REQUESTQUANTITY - m.RECEIVEDQUANTITY) > 0
<!--        AND m2.RECEIVEREQUESTTYPE IN-->
<!--        <foreach item="type" index="index" collection="typeList" open="(" separator="," close=")">-->
<!--            #{type}-->
<!--        </foreach>-->
        AND (m2.RETURNFLAG IS NULL OR m2.RETURNFLAG != 'Y')
        AND (m3.IS_BATCH IS NULL OR m3.IS_BATCH != 'Y')
    </select>

    <!-- GetPackingRow  xh002 -->
    <select id="getStoreCharge" resultType="map">
        SELECT ROWNUM  SEQ, TTT.*
        FROM (
            SELECT A.RECEIVEREQUESTNAME, A.SITENAME, A.MATERIALPACKINGNAME, A.MATERIALSPECNAME, A.ERPLOCATION, A.ERPFACTORY,
                A.LOCATIONNAME, A.CHARGE, A.REMARK, B.DESC_CN, B.DESCRIPTION, A.SDK_ID, SS.SPECNAME, A.PHASE,
                A.MATERIALQUANTITY, M.FNAME UNIT, BE.DESCRIPTION ERPLOCATIONDESC, S.DESCRIPTION LOCATIONNAMEDESC,
                A.ERPFACTORY ERPFACTROY, o.ORGNAME ERPFACTROYDESC, A.SUPPLIERNAME SUPPLIERNO
            FROM MATERIALPACKING A
                LEFT JOIN MATERIALSPEC B ON A.MATERIALSPECNAME = B.MATERIALSPECNAME
                LEFT JOIN SDK_SPEC SS ON A.SDK_ID = SS.SDK_ID
                LEFT JOIN BS_ERPLOCATION BE ON BE.ERPLOCATIONNAME = A.ERPLOCATION
                LEFT JOIN STORAGESPEC S ON S.STORAGENAME = A.LOCATIONNAME
                LEFT JOIN ORG o ON o.ORGNO = A.ERPFACTORY
                LEFT JOIN MATERIALUNIT M ON M.FNUMBER = A.UNIT
            WHERE A.SITENAME = #{siteName}
            AND A.RECEIVEREQUESTNAME = #{documentName}
            AND A.STOCKSTATE = 'Created'
            ORDER BY A.MATERIALSPECNAME, A.SDK_ID, A.PHASE
        ) TTT
    </select>

    <select id="getByKey" resultType="com.cim.idm.model.po.delivery.MaterialDelivery">
        select SITENAME, DELIVERYNAME, DELIVERYTYPE, DELIVERYSTATE
        from MATERIALDELIVERY
        WHERE SITENAME = #{dto.siteName}
        AND DELIVERYNAME = #{dto.documentName}
    </select>

    <select id="getReceiveByKey" resultType="com.cim.idm.model.po.MaterialReceiveRequest">
        SELECT SITENAME, RECEIVEREQUESTNAME, RECEIVEREQUESTTYPE, PURCHASEORDERGROUP, RECEIVEREQUESTSTATE, SUPPLIERNO,
               OEMSITENAME, OEMSITEINCHARGE, SYSTEMTYPE, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,
               LASTEVENTCOMMENT, ERPPONAME, SAVEREASON, RESPPERSON, RESPDEPART, SOLVETIME, PASSSTATE, WORKER, SDK_ID,
               ERPID, REMARK, PURCHASER, PURORGNO, ORDERDATE, CLOSERUSER, CLOSEDATE, STOCKORGNO, RETURNFLAG,
               RECEIVEREQUESTDETAILTYPE, ERPLOCATION, PRODUCTDEPTNO, SOURCEBILLID, EFFECTIVESTARTTIME, EFFECTIVEENDTIME
        FROM MATERIALRECEIVEREQUEST
        WHERE SITENAME = #{dto.siteName}
        AND RECEIVEREQUESTNAME = #{dto.documentName}
    </select>

    <select id="getRelByDelivery" resultType="com.cim.idm.model.po.delivery.MaterialDeliveryReceive">
        SELECT md.DELIVERYNAME, md.DELIVERYNUM, md.RECEIVEREQUESTNAME, md.MATERIALSPECNAME, md.RECEIVEREQUESTDETAILNAME
        FROM MATERIALDELIVERYRECEIVE md
        WHERE md.DELIVERYNAME = #{dto.documentName}
    </select>

    <select id="getReceiveByDelivery" resultType="com.cim.idm.model.po.MaterialReceiveRequest">
        SELECT m.SITENAME, m.RECEIVEREQUESTNAME, m.RECEIVEREQUESTTYPE, m.PURCHASEORDERGROUP, m.RECEIVEREQUESTSTATE, m.SUPPLIERNO,
               m.OEMSITENAME, m.OEMSITEINCHARGE, m.SYSTEMTYPE, m.LASTEVENTNAME, m.LASTEVENTTIMEKEY, m.LASTEVENTTIME, m.LASTEVENTUSER,
               m.LASTEVENTCOMMENT, m.ERPPONAME, m.SAVEREASON, m.RESPPERSON, m.RESPDEPART, m.SOLVETIME, m.PASSSTATE, m.WORKER, m.SDK_ID,
               m.ERPID, m.REMARK, m.PURCHASER, m.PURORGNO, m.ORDERDATE, m.CLOSERUSER, m.CLOSEDATE, m.STOCKORGNO, m.RETURNFLAG,
               m.RECEIVEREQUESTDETAILTYPE, m.ERPLOCATION, m.PRODUCTDEPTNO, m.SOURCEBILLID, m.EFFECTIVESTARTTIME, m.EFFECTIVEENDTIME
        FROM MATERIALRECEIVEREQUEST m, MATERIALDELIVERYRECEIVE md
        WHERE m.SITENAME = #{dto.siteName}
        AND md.DELIVERYNAME = #{dto.documentName}
        AND m.RECEIVEREQUESTNAME = md.RECEIVEREQUESTNAME
    </select>

    <select id="getReceiveByMaterialSpec" resultType="com.cim.idm.model.vo.DeliveryReceiveDetailVo">
        SELECT receiveRequestName, receiveRequestDetailName
        FROM MATERIALRECEIVEREQUESTDETAIL
        WHERE RECEIVEREQUESTNAME = #{documentName}
        AND MATERIALSPECNAME = #{materialSpec}
    </select>

    <select id="getDeliveryByMaterialSpec" resultType="com.cim.idm.model.vo.DeliveryReceiveDetailVo">
        SELECT receiveRequestName, receiveRequestDetailName
        FROM MATERIALDELIVERYRECEIVE
        WHERE DELIVERYNAME = #{documentName}
        AND MATERIALSPECNAME = #{materialSpec}
    </select>

    <select id="getReceiveDetail" resultType="com.cim.idm.model.vo.DeliveryReceiveDetailVo">
        SELECT receiveRequestName, materialSpecName, requestQuantity, requestQuantity as deliveryNum, RECEIVEREQUESTDETAILNAME
        FROM MATERIALRECEIVEREQUESTDETAIL
        WHERE RECEIVEREQUESTNAME in
        <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
            #{name}
        </foreach>
        order by RECEIVEREQUESTDETAILNAME
    </select>

    <select id="getDeliveryList" resultType="com.cim.idm.model.po.delivery.MaterialDelivery">
        SELECT SITENAME, DELIVERYNAME, DELIVERYTYPE, DELIVERYSTATE
        FROM MATERIALDELIVERY
        WHERE SITENAME = #{dto.siteName}
        <if test="dto.deliveryName != null and dto.deliveryName != ''">
            AND DELIVERYNAME LIKE '%'||#{dto.deliveryName}||'%'
        </if>
        <if test="dto.deliveryType != null and dto.deliveryType != ''">
            AND DELIVERYTYPE = #{dto.deliveryType}
        </if>
        <if test="dto.deliveryState != null and dto.deliveryState != ''">
            AND DELIVERYSTATE = #{dto.deliveryState}
        </if>
        order by DELIVERYNAME
    </select>

    <delete id="delDelivery">
        delete from MATERIALDELIVERY
        where SITENAME = #{dto.siteName}
        and DELIVERYNAME = #{dto.deliveryName}
    </delete>

    <delete id="delDeliveryRel">
        delete from MATERIALDELIVERYRECEIVE
        where DELIVERYNAME = #{dto.deliveryName}
    </delete>

    <delete id="editDelivery">
        UPDATE MATERIALDELIVERY SET DELIVERYSTATE = #{dto.deliveryState}
        WHERE SITENAME = #{dto.siteName} AND DELIVERYNAME = #{dto.deliveryName}
    </delete>

    <select id="getDeliveryStoreDetail" resultType="map">
        WITH
            AA AS (
                SELECT
                    m5.RECEIVEREQUESTNAME,
                    m5.MATERIALSPECNAME,
                    SUM( m5.MATERIALQUANTITY ) QTY
                FROM
                    MATERIALPACKING m5
                WHERE
                    m5.STOCKSTATE IN ( 'Stocked', 'StockOut' )
                  AND m5.RECEIVEREQUESTNAME IN ( #{deliveryName} )
                GROUP BY
                    m5.MATERIALSPECNAME,
                    m5.RECEIVEREQUESTNAME
            ),


            BB AS (
                SELECT
                    m5.RECEIVEREQUESTNAME,
                    m5.MATERIALSPECNAME,
                    SUM( m5.MATERIALQUANTITY ) QTY
                FROM
                    MATERIALPACKING m5
                WHERE
                    m5.STOCKSTATE = 'Created'
                  AND m5.RECEIVEREQUESTNAME IN ( #{deliveryName} )
                GROUP BY
                    m5.MATERIALSPECNAME,
                    m5.RECEIVEREQUESTNAME
            ),

            CC AS (
                SELECT
                    m5.DELIVERYNAME RECEIVEREQUESTNAME,
                    m5.MATERIALSPECNAME ,
                    SUM( m5.DELIVERYNUM ) QTY
                FROM
                    MATERIALDELIVERYRECEIVE m5
                WHERE
                    m5.DELIVERYNAME IN ( #{deliveryName} )
                GROUP BY
                    m5.DELIVERYNAME,
                    m5.MATERIALSPECNAME
            ),

            DELIVERY AS (
                SELECT DISTINCT DELIVERYNAME, md.MATERIALSPECNAME,
                                TO_CHAR( SYSDATE, 'YYYY-MM-DD' ) RECEIVETIME,
                                TO_CHAR( SYSDATE, 'YYYY-MM-DD' ) MAKEDATE,
                                '1' RECEIVEREQUESTDETAILNAME,
                                m.SITENAME,
                                m.ERPLOCATION,
                                m.LOCATIONNAME,
                                m.MATERIALUNIT,
                                m.SDK_ID,
                                m.REMARK REMARK,
                                m.SALESMAN SALESPERSON,
                                m.SALESASSISTANT CAREER_ASSISTANCE,
                                m.BUSINESSDIVISION BUSINESS_UNIT,
                                COALESCE( m2.STOCKORGNO, m.ERPFACTORY ) AS ERPFACTORY,
                                m2.SUPPLIERNO,
                                --m.POCATEGORY,
                                m3.WIDTH AS DESCRIPTION,
                                m3.MATERIALUNITDESC,
                                m3.WIDTH SPECNAME,
                                m3.PHASE,
                                m3.DESC_CN,
                                DECODE( m3.IS_BATCH, 'Y', '是', '否' ) IS_BATCH,
                                CASE WHEN m3.EXPIRATION_RULES = '0' THEN
                                         '入厂保质期'
                                     WHEN m3.EXPIRATION_RULES = '1' THEN
                                         '出厂保质期' ELSE '未定义'
                                    END AS REPLACED_VALUE,
                                DECODE( m4.IQCFLAG, 'Y', '是', '否' ) IQCFLAG,
                                be.DESCRIPTION AS ERPLOCATION2,
                                o.ORGNAME AS ERPFACTORY2,
                                sss.DESCRIPTION AS LOCATIONNAME2 FROM MATERIALDELIVERYRECEIVE md
                                                                          LEFT JOIN MATERIALRECEIVEREQUEST m2 ON md.RECEIVEREQUESTNAME = m2.RECEIVEREQUESTNAME
                                                                          LEFT JOIN MATERIALRECEIVEREQUESTDETAIL m ON md.RECEIVEREQUESTNAME = m.RECEIVEREQUESTNAME
                    AND md.RECEIVEREQUESTDETAILNAME = m.RECEIVEREQUESTDETAILNAME
                                                                          LEFT JOIN MATERIALSPEC m3 ON md.MATERIALSPECNAME = m3.MATERIALSPECNAME
                                                                          LEFT JOIN BS_MATERIALFACTORY m4 ON m3.MATERIALSPECNAME = m4.MATERIALSPECNAME AND m2.ERPFACTORY = m4.ERPFACTORY
                                                                          LEFT JOIN BS_ERPLOCATION be ON m.ERPLOCATION = be.ERPLOCATIONNAME
                                                                          LEFT JOIN ORG o ON m2.STOCKORGNO  = o.ORGNO
                                                                          LEFT JOIN STORAGESPEC sss ON m.LOCATIONNAME = sss.STORAGENAME
                WHERE md.DELIVERYNAME = #{deliveryName}  AND m2.RECEIVEREQUESTTYPE IN ('Z001', 'Z002', 'Z003','Z004', 'Z005', 'Z006', 'Z007', 'Z008'))
        SELECT M.DELIVERYNAME, M.MATERIALSPECNAME,
               M.RECEIVETIME,
               M.MAKEDATE,
               M.RECEIVEREQUESTDETAILNAME,
               M.SITENAME,
               M.ERPLOCATION,
               M.LOCATIONNAME,
               M.MATERIALUNIT,
               M.SDK_ID,
               M.REMARK,
               M.SALESPERSON,
               M.CAREER_ASSISTANCE,
               M.BUSINESS_UNIT,
               M.ERPFACTORY,
               M.SUPPLIERNO,
               M.DESCRIPTION,
               M.MATERIALUNITDESC,
               M.SPECNAME,
               M.PHASE,
               M.DESC_CN,
               M.IS_BATCH,
               M.REPLACED_VALUE,
               M.IQCFLAG,
               M.ERPLOCATION2,
               M.ERPFACTORY2,
               M.LOCATIONNAME2,AA.QTY RECEIVEDQUANTITY2,BB.QTY RECEIVEDQUANTITY, CC.QTY REQUESTQUANTITY, (CC.QTY - BB.QTY) MINUSQTY, AA.QTY STOCKINQTY, BB.QTY CREATEQTY  FROM DELIVERY M
                                                                                      LEFT JOIN AA ON M.DELIVERYNAME = AA.RECEIVEREQUESTNAME AND M.MATERIALSPECNAME = AA.MATERIALSPECNAME
                                                                                      LEFT JOIN BB ON M.DELIVERYNAME = BB.RECEIVEREQUESTNAME AND M.MATERIALSPECNAME = BB.MATERIALSPECNAME
                                                                                      LEFT JOIN CC ON M.DELIVERYNAME = CC.RECEIVEREQUESTNAME AND M.MATERIALSPECNAME = CC.MATERIALSPECNAME
        WHERE M.DELIVERYNAME = #{deliveryName}
    </select>
    <select id="getStoreChargeSort" resultType="map">
        SELECT ROWNUM  SEQ, TTT.*
        FROM (
                 SELECT A.RECEIVEREQUESTNAME, A.SITENAME, A.MATERIALPACKINGNAME, A.MATERIALSPECNAME, A.ERPLOCATION, A.ERPFACTORY,
                        A.LOCATIONNAME, A.CHARGE, A.REMARK, B.DESC_CN, B.DESCRIPTION, A.SDK_ID, SS.SPECNAME, A.PHASE,
                        A.MATERIALQUANTITY, M.FNAME UNIT, BE.DESCRIPTION ERPLOCATIONDESC, S.DESCRIPTION LOCATIONNAMEDESC,
                        A.ERPFACTORY ERPFACTROY, o.ORGNAME ERPFACTROYDESC, A.SUPPLIERNAME SUPPLIERNO
                 FROM MATERIALPACKING A
                          LEFT JOIN MATERIALSPEC B ON A.MATERIALSPECNAME = B.MATERIALSPECNAME
                          LEFT JOIN SDK_SPEC SS ON A.SDK_ID = SS.SDK_ID
                          LEFT JOIN BS_ERPLOCATION BE ON BE.ERPLOCATIONNAME = A.ERPLOCATION
                          LEFT JOIN STORAGESPEC S ON S.STORAGENAME = A.LOCATIONNAME
                          LEFT JOIN ORG o ON o.ORGNO = A.ERPFACTORY
                          LEFT JOIN MATERIALUNIT M ON M.FNUMBER = A.UNIT
                 WHERE A.SITENAME = #{siteName}
                   AND A.RECEIVEREQUESTNAME = #{documentName}
                   AND A.STOCKSTATE = 'Created'
                 ORDER BY A.CHARGE
             ) TTT
    </select>

    <update id="updateMaterialReceiveRequestDetailQty">
        UPDATE MATERIALRECEIVEREQUESTDETAIL
        SET RECEIVEDQUANTITY = RECEIVEDQUANTITY + #{dto.deliveryNum}
        WHERE RECEIVEREQUESTNAME = #{dto.receiveRequestName}
          AND RECEIVEREQUESTDETAILNAME = #{dto.receiveRequestDetailName}
          AND MATERIALSPECNAME = #{dto.materialSpecName}
    </update>

    <select id="getDeliveryReceiveDetail" resultType="com.cim.idm.model.vo.DeliveryReceiveDetailVo">
        SELECT RD.receiveRequestName, RD.materialSpecName, RD.requestQuantity, RD.requestQuantity - RD.receivedQuantity as deliveryNum, RD.RECEIVEREQUESTDETAILNAME
        ,MS.DESCRIPTION AS description
        FROM MATERIALRECEIVEREQUESTDETAIL RD
        LEFT JOIN MATERIALSPEC MS ON RD.MATERIALSPECNAME = MS.MATERIALSPECNAME
        WHERE RD.RECEIVEREQUESTNAME in
        <foreach item="name" index="index" collection="nameList" open="(" separator="," close=")">
            #{name}
        </foreach>
        order by RD.RECEIVEREQUESTDETAILNAME
    </select>
</mapper>