<?xml version="1.0" encoding="UTF-8"?>

<queryMap desc = "合同认证">

   <query id = "CoreOrderAuthentication.QueryAuthedOrd" desc = "是否存在数据">
   
   <![CDATA[
   
     SELECT T.ORD_PK,
       T.ORD_LN_PK,
       T.ORDER_SEQ,
       T.CUSTOMER_NO,
       T.CUSTOMER_NM,
       T.PRODUCCODE,
       T.PRODUCNAME,
       T.STD_CODE,
       T.STD_NAME,
       T.STEELCODE,
       T.STEELNAME,
       T.SPEC_CODE,
       T.SPEC_NAME,
       T.MODEL_CODE,
       T.MODEL_DESC,
       TO_CHAR(T.SIGN_DATE, 'YYYY-MM-DD') SIGN_DATE,
       TO_CHAR(T.DELVRY_DATE, 'YYYY-MM-DD') DELVRY_DATE,
       TO_CHAR(T.DELVRY_BDATE, 'YYYY-MM-DD') DELVRY_BDATE,
       TO_CHAR(T.DELVRY_EDATE, 'YYYY-MM-DD') DELVRY_EDATE,
       T.ORDER_QTY,
       T.ORDER_UNIT,
       T.DELVRY_RANGE_TPE,
       T.SEND_QTY,
       DECODE(T.CHANGE_STATUS,
              '1',
              'OK',
              '2',
              '量不合',
              '3',
              '交货期不合',
              '4',
              '量不合及交货期不合') CHANGE_STATUS,
       T.CUSTM_ORDER_NO,
       TO_CHAR(T.CUSTM_SIGN_DATE, 'YYYY-MM-DD') CUSTM_SIGN_DATE,
       TO_CHAR(T.CUSTM_DELVRY_DATE, 'YYYY-MM-DD') CUSTM_DELVRY_DATE,
       T.CHANGE_NO,
       T.PRINT_COUNT,
       T.STLMT_UNIT_NO,
       T.STLMT_UNIT_NM,
       T.FINAL_USER,
       T.FINAL_USER_DESC,
       (SELECT BASENAME
          FROM COM_BASE_INFO A
         WHERE A.BASECODE = T.ORDER_LN_STATUS) ORD_LN_STATUS_DESC,
       T.BALNCE_PRICE_STS,
       T.SCH_APPROVE_NO,
       T.ORD_APPROVE_NO,
       T.PRICE_NO,
       T.MEMO,
       T.SALE_AREA_NO,
       T.SALE_AREA_NM,
       T.SALE_ORG,
       (SELECT BASENAME FROM COM_BASE_INFO A WHERE A.BASECODE = T.SALE_ORG) SALE_ORG_DESC,
       DECODE(T.IS_RETURN,'1','已返回','未返回') IS_RETURN,
       TO_CHAR(T.RETURN_TIME, 'YYYY-MM-DD HH24:MI:SS') RETURN_TIME,
       T.SEND_COUNT,
       TO_CHAR(T.UPDATE_TIME, 'YYYY-MM-DD HH24:MI:SS') UPDATE_TIME,
       T.BAL_QTY,
       T.SCH_QTY,
       T.STORE_QTY,
       T.SCH_COUNT,
       DECODE(T.ORDER_FILE,NULL,'无合同文本','有合同文本') ORDER_FILE,
       DECODE(T.USERORDER_FILE,NULL,'无合同文本','有合同文本') USERORDER_FILE,
       T.RETURN_NAME,
       T.ORDER_NO,
       T1.CREATE_NAME,
       T1.CREATE_NAME_OLD,
       T1.SALE_AREA_NO_OLD,
       T1.SALE_AREA_DESC_OLD,
       (SELECT BASENAME
          FROM COM_BASE_INFO A
         WHERE A.BASECODE = T.SUPPLY_UNIT) SUPPLY_UNIT,
       (SELECT BASENAME
          FROM COM_BASE_INFO A
         WHERE A.BASECODE = T1.FST_DRAWEE_UNIT) FST_DRAWEE_UNIT
  FROM SLM_ORDER_AUTHENTICATION T
  LEFT JOIN SLM_ORDER_HEAD T1 ON T.ORD_PK = T1.ORD_PK
  --LEFT JOIN SLM_ORDER_LOGOUT_REVIEW S ON T.ORD_LN_PK = S.ORD_LN_PK
  WHERE T.ORDER_SEQ LIKE '%' || ?|| '%' AND T.SALE_AREA_NM LIKE '%' || ?|| '%'
   ]]>
   
   </query>
   
   <query id = "CoreOrderAuthentication.ReturnTime" desc = "是否存在数据">
   
   <![CDATA[
   
     UPDATE SLM_ORDER_AUTHENTICATION T
     SET T.IS_RETURN = '1',
         T.RETURN_TIME = SYSDATE,
         T.RETURN_NAME = ?
     WHERE T.ORD_LN_PK = ?
   
   ]]>
   
   </query>
   
    <query id = "CoreOrderAuthentication.UpdateAuthOrdWrite" desc = "更新数据">
       <![CDATA[
   
        UPDATE SLM_ORDER_AUTHENTICATION T
        SET  
           T.CUSTM_SIGN_DATE   = TO_DATE('#CUSTM_SIGN_DATE#', 'YYYY-MM-DD'),
           T.CUSTM_DELVRY_DATE = TO_DATE('#CUSTM_DELVRY_DATE#', 'YYYY-MM-DD'),
           T.SCH_APPROVE_NO    = '#SCH_APPROVE_NO#',
           T.ORD_APPROVE_NO    = '#ORD_APPROVE_NO#',
           T.MEMO              = '#MEMO#',
           T.CUSTM_ORDER_NO    = '#CUSTM_ORDER_NO#'
           
        WHERE T.ORD_LN_PK = '#ORD_LN_PK#'
        ]]>
     </query>
     
      <query id = "CoreOrderAuthentication.UpdateData" desc = "更新数据">
       <![CDATA[
       
         DECLARE
  V_COUNT NUMBER := 0;

  BEGIN

  FOR DROW IN (SELECT B.ORDER_NO || '/' || A.ORDER_SEQ ORDER_SEQ,
                      A.ORD_PK,
                      A.ORD_LN_PK,
                      B.CUSTOMER_NO,
                      B.CUSTOMER_NM,
                      A.PRODUCCODE,
                      A.PRODUCNAME,
                      A.STD_CODE,
                      A.STD_NAME,
                      A.STEELCODE,
                      A.STEELNAME,
                      A.SPEC_CODE,
                      A.SPEC_NAME,
                      A.MODEL_CODE,
                      A.MODEL_DESC,
                      TO_CHAR(B.SIGN_DATE, 'YYYY-MM-DD') SIGN_DATE,
                      SLM.DELVRY_EDATE DELVRY_DATE,
                      (SELECT TO_CHAR(MIN(M.SEN_TIME), 'YYYY-MM-DD') DELVRY_BDATE                                  
                             FROM YDM_ZC_LOADVEHICLE_MAT M
                            WHERE M.RETURN_FLAG = '0'
                              AND M.ORD_LN_PK =  A.ORD_LN_PK
                              AND M.VALIDFLAG >'1'
                            GROUP BY M.ORD_LN_PK) DELVRY_BDATE,                            
                        (SELECT TO_CHAR(MAX(M.SEN_TIME), 'YYYY-MM-DD') DELVRY_EDATE                                  
                             FROM YDM_ZC_LOADVEHICLE_MAT M
                            WHERE M.RETURN_FLAG = '0'
                              AND M.ORD_LN_PK =  A.ORD_LN_PK
                              AND M.VALIDFLAG >'1'
                            GROUP BY M.ORD_LN_PK) DELVRY_EDATE,                        
                       (SELECT  NVL(SUM(CASE
                                        WHEN M.PONDER_NO IS NOT NULL AND NVL(M.PONDER_GROSS_WT,0)>0 THEN
                                         M.PONDER_GROSS_WT
                                        ELSE
                                         M.ACT_WEIGHT
                                      END),0) SEND_QTY
                             FROM YDM_ZC_LOADVEHICLE_MAT M
                            WHERE M.RETURN_FLAG = '0'
                              AND M.ORD_LN_PK =  A.ORD_LN_PK
                              AND M.VALIDFLAG >'1'
                            GROUP BY M.ORD_LN_PK ) SEND_QTY,                            
                      (SELECT   SUM(NVL(M.ACT_COUNT,0)) SEND_COUNT
                             FROM YDM_ZC_LOADVEHICLE_MAT M
                            WHERE M.RETURN_FLAG = '0'
                              AND M.ORD_LN_PK =  A.ORD_LN_PK
                              AND M.VALIDFLAG >'1'
                            GROUP BY M.ORD_LN_PK) SEND_COUNT,
                      A.ORDER_QTY,
                      A.ORDER_UNIT,
                      ('(-' || A.DELVRY_RANGE_MIN || '%' || ',' ||
                      A.DELVRY_RANGE_MAX || '%)') DELVRY_RANGE_TPE,
                      NVL(A.ORDER_QTY,0)-(NVL(A.DELVRY_RANGE_MIN,0)/100)*A.ORDER_QTY DELVRY_RANGE_MIN,
                      NVL(A.ORDER_QTY,0)+(NVL(A.DELVRY_RANGE_MAX,0)/100)*A.ORDER_QTY DELVRY_RANGE_MAX,
                      B.CUSTM_ORDER_NO,
                      R.CHANGE_NO,
                      B.PRINT_COUNT,
                      B.STLMT_UNIT_NO,
                      B.STLMT_UNIT_NM,
                      B.SUPPLY_UNIT,
                      A.FINAL_USER,
                      A.FINAL_USER_DESC,
                      A.ORDER_LN_STATUS,
                      COM.BASENAME ORD_LN_STATUS_DESC,
                      DECODE(A.BALNCE_PRICE_STS, '1', '确认', '未确认') BALNCE_PRICE_STS,
                      B.PRICE_NO,
                      B.SALE_ORG,
                      B.SALE_AREA_NO,
                      E.SALE_AREA_DESC SALE_AREA_NM,
                      Q1.BAL_QTY,
                      Q2.SCH_QTY,
                      Q2.SCH_COUNT,
                      Q3.STORE_QTY
               
                 FROM SLM_ORDER_LINE A
                 LEFT JOIN SLM_ORDER_HEAD B
                   ON A.ORD_PK = B.ORD_PK
                 LEFT JOIN (SELECT TO_CHAR(MAX(D.DELVRY_EDATE), 'YYYY-MM-DD') DELVRY_EDATE,
                                  D.ORD_LN_PK
                             FROM SLM_ORDER_LINE_DELIVERY D
                            WHERE D.VALIDFLAG = '1'
                            GROUP BY D.ORD_LN_PK) SLM
                   ON SLM.ORD_LN_PK = A.ORD_LN_PK
                 LEFT JOIN (SELECT LISTAGG(C.ORDER_CHGE_NO, ',') WITHIN GROUP(ORDER BY C.ORD_LN_PK) CHANGE_NO,
                                  C.ORD_LN_PK
                             FROM CHGE_SLM_APPLY_M C
                            WHERE C.CHGE_STATUS = 'S'
                            GROUP BY C.ORD_LN_PK) R
                   ON R.ORD_LN_PK = A.ORD_LN_PK
                 LEFT JOIN (SELECT SUM(F.WEIGHT) BAL_QTY, F.ORD_LN_PK
                             FROM SLM_BALANCEINFO F
                            WHERE F.BALANCESTATUS = '3'
                              AND F.REALINVOICENO IS NOT NULL
                            GROUP BY F.ORD_LN_PK) Q1
                   ON Q1.ORD_LN_PK = A.ORD_LN_PK
                 LEFT JOIN (SELECT SUM(T.PLAN_P_MAIN) SCH_QTY,
                                  SUM(T.PLAN_NUM_D) SCH_COUNT,
                                  SUBSTR(ORD_LN_DLY_PK, 1, 35) ORD_LN_PK
                             FROM PLN_SALEORD_PROC T
                            WHERE T.ORDER_SOURCE != '备料需求'
                            GROUP BY SUBSTR(ORD_LN_DLY_PK, 1, 35)) Q2
                   ON Q2.ORD_LN_PK = A.ORD_LN_PK
                 LEFT JOIN (SELECT SUM(T.ACT_WEIGHT) STORE_QTY, T.ORD_LN_PK
                             FROM MAT_ZC_M T
                            WHERE T.MAT_STATUS = '80150301'
                            GROUP BY T.ORD_LN_PK) Q3
                   ON Q3.ORD_LN_PK = A.ORD_LN_PK
                 LEFT JOIN COM_BASE_INFO COM
                   ON COM.BASECODE = A.ORDER_LN_STATUS
                 LEFT JOIN SLM_BASE_SALE_AREA_TYP E
                   ON B.SALE_AREA_NO = E.SALE_AREA_NO
               
                WHERE A.CREATE_NAME IS NOT NULL
                  AND B.CREATE_NAME IS NOT NULL
                  AND A.VALIDFLAG = '1'
                  AND B.VALIDFLAG = '1'
                  AND B.ORDER_STS >= '12020301'
                  AND B.SALE_ORG IN ('100101', '100103')
                  #ORDARRAY#) LOOP
  
    SELECT NVL(COUNT(1), 0)
      INTO V_COUNT
      FROM SLM_ORDER_AUTHENTICATION TA
     WHERE TA.ORD_LN_PK = DROW.ORD_LN_PK;
    IF (V_COUNT = 0) THEN
      INSERT INTO SLM_ORDER_AUTHENTICATION
        (ORD_PK,
         ORD_LN_PK,
         ORDER_SEQ,
         CUSTOMER_NO,
         CUSTOMER_NM,
         PRODUCCODE,
         PRODUCNAME,
         STD_CODE,
         STD_NAME,
         STEELCODE,
         STEELNAME,
         SPEC_CODE,
         SPEC_NAME,
         MODEL_CODE,
         MODEL_DESC,
         SIGN_DATE,
         DELVRY_DATE,
         DELVRY_BDATE,
         DELVRY_EDATE,
         ORDER_QTY,
         ORDER_UNIT,
         DELVRY_RANGE_TPE,
         SEND_QTY,
         CHANGE_STATUS,
         CUSTM_ORDER_NO,
         CHANGE_NO,
         PRINT_COUNT,
         STLMT_UNIT_NO,
         STLMT_UNIT_NM,
         SUPPLY_UNIT,
         FINAL_USER,
         FINAL_USER_DESC,
         ORDER_LN_STATUS,
         BALNCE_PRICE_STS,
         PRICE_NO,
         SALE_AREA_NO,
         SALE_AREA_NM,
         SALE_ORG,
         SEND_COUNT,
         UPDATE_TIME,
         BAL_QTY,
         SCH_QTY,
         STORE_QTY,
         SCH_COUNT)
      VALUES
        (DROW.ORD_PK,
         DROW.ORD_LN_PK,
         DROW.ORDER_SEQ,
         DROW.CUSTOMER_NO,
         DROW.CUSTOMER_NM,
         DROW.PRODUCCODE,
         DROW.PRODUCNAME,
         DROW.STD_CODE,
         DROW.STD_NAME,
         DROW.STEELCODE,
         DROW.STEELNAME,
         DROW.SPEC_CODE,
         DROW.SPEC_NAME,
         DROW.MODEL_CODE,
         DROW.MODEL_DESC,
         TO_DATE(DROW.SIGN_DATE, 'YYYY-MM-DD'),
         TO_DATE(DROW.DELVRY_DATE, 'YYYY-MM-DD'),
         TO_DATE(DROW.DELVRY_BDATE, 'YYYY-MM-DD'),
         TO_DATE(DROW.DELVRY_EDATE, 'YYYY-MM-DD'),
         DROW.ORDER_QTY,
         DROW.ORDER_UNIT,
         DROW.DELVRY_RANGE_TPE,
         DROW.SEND_QTY,
          (CASE
           WHEN DROW.ORDER_UNIT = '支' THEN
            (CASE
              WHEN (DROW.DELVRY_RANGE_MAX < DROW.SEND_COUNT OR DROW.DELVRY_RANGE_MIN > DROW.SEND_COUNT) AND (DROW.DELVRY_EDATE > DROW.DELVRY_DATE) THEN
               '4'
              WHEN DROW.DELVRY_EDATE > DROW.DELVRY_DATE THEN
               '3'
              WHEN (DROW.DELVRY_RANGE_MAX < DROW.SEND_COUNT OR DROW.DELVRY_RANGE_MIN > DROW.SEND_COUNT) THEN
               '2'
              WHEN (DROW.DELVRY_RANGE_MAX > DROW.SEND_COUNT AND DROW.DELVRY_RANGE_MIN < DROW.SEND_COUNT) THEN
               '1'
              ELSE
               '0'
            END)
           ELSE
            (CASE
              WHEN (DROW.DELVRY_RANGE_MAX < DROW.SEND_QTY OR DROW.DELVRY_RANGE_MIN > DROW.SEND_QTY) AND (DROW.DELVRY_EDATE > DROW.DELVRY_DATE) THEN
               '4'
              WHEN DROW.DELVRY_EDATE > DROW.DELVRY_DATE THEN
               '3'
              WHEN (DROW.DELVRY_RANGE_MAX < DROW.SEND_QTY OR DROW.DELVRY_RANGE_MIN > DROW.SEND_QTY) THEN
               '2'
              WHEN (DROW.DELVRY_RANGE_MAX > DROW.SEND_QTY AND DROW.DELVRY_RANGE_MIN < DROW.SEND_QTY) THEN
               '1'
              ELSE
               '0'
            END)
         END),
         DROW.CUSTM_ORDER_NO,
         DROW.CHANGE_NO,
         DROW.PRINT_COUNT,
         DROW.STLMT_UNIT_NO,
         DROW.STLMT_UNIT_NM,
         DROW.SUPPLY_UNIT,
         DROW.FINAL_USER,
         DROW.FINAL_USER_DESC,
         DROW.ORDER_LN_STATUS,
         DROW.BALNCE_PRICE_STS,
         DROW.PRICE_NO,
         DROW.SALE_AREA_NO,
         DROW.SALE_AREA_NM,
         DROW.SALE_ORG,
         DROW.SEND_COUNT,
         SYSDATE,
         DROW.BAL_QTY,
         DROW.SCH_QTY,
         DROW.STORE_QTY,
         DROW.SCH_COUNT
         
         );
    ELSE
      UPDATE SLM_ORDER_AUTHENTICATION T
         SET T.ORD_PK           = DROW.ORD_PK,
             T.ORD_LN_PK        = DROW.ORD_LN_PK,
             T.ORDER_SEQ        = DROW.ORDER_SEQ,
             T.CUSTOMER_NO      = DROW.CUSTOMER_NO,
             T.CUSTOMER_NM      = DROW.CUSTOMER_NM,
             T.PRODUCCODE       = DROW.PRODUCCODE,
             T.PRODUCNAME       = DROW.PRODUCNAME,
             T.STD_CODE         = DROW.STD_CODE,
             T.STD_NAME         = DROW.STD_NAME,
             T.STEELCODE        = DROW.STEELCODE,
             T.STEELNAME        = DROW.STEELNAME,
             T.SPEC_CODE        = DROW.SPEC_CODE,
             T.SPEC_NAME        = DROW.SPEC_NAME,
             T.MODEL_CODE       = DROW.MODEL_CODE,
             T.MODEL_DESC       = DROW.MODEL_DESC,
             T.SIGN_DATE        = TO_DATE(DROW.SIGN_DATE, 'YYYY-MM-DD'),
             T.DELVRY_DATE      = TO_DATE(DROW.DELVRY_DATE, 'YYYY-MM-DD'),
             T.DELVRY_BDATE     = TO_DATE(DROW.DELVRY_BDATE, 'YYYY-MM-DD'),
             T.DELVRY_EDATE     = TO_DATE(DROW.DELVRY_EDATE, 'YYYY-MM-DD'),
             T.ORDER_QTY        = DROW.ORDER_QTY,
             T.ORDER_UNIT       = DROW.ORDER_UNIT,
             T.DELVRY_RANGE_TPE = DROW.DELVRY_RANGE_TPE,
             T.SEND_QTY         = DROW.SEND_QTY,
             T.CHANGE_STATUS = (CASE
                                 WHEN DROW.ORDER_UNIT = '支' THEN
                                  (CASE
                                    WHEN (DROW.DELVRY_RANGE_MAX < DROW.SEND_COUNT OR DROW.DELVRY_RANGE_MIN > DROW.SEND_COUNT) AND
                                         (DROW.DELVRY_EDATE > DROW.DELVRY_DATE) THEN
                                     '4'
                                    WHEN DROW.DELVRY_EDATE > DROW.DELVRY_DATE THEN
                                     '3'
                                    WHEN (DROW.DELVRY_RANGE_MAX < DROW.SEND_COUNT OR DROW.DELVRY_RANGE_MIN > DROW.SEND_COUNT) THEN
                                     '2'
                                    WHEN (DROW.DELVRY_RANGE_MAX > DROW.SEND_COUNT AND DROW.DELVRY_RANGE_MIN < DROW.SEND_COUNT) THEN
                                     '1'
                                    ELSE
                                     '0'
                                  END)
                                 ELSE
                                  (CASE
                                    WHEN (DROW.DELVRY_RANGE_MAX < DROW.SEND_QTY OR DROW.DELVRY_RANGE_MIN > DROW.SEND_QTY) AND
                                         (DROW.DELVRY_EDATE > DROW.DELVRY_DATE) THEN
                                     '4'
                                    WHEN (DROW.DELVRY_EDATE > DROW.DELVRY_DATE) THEN
                                     '3'
                                    WHEN (DROW.DELVRY_RANGE_MAX < DROW.SEND_QTY OR DROW.DELVRY_RANGE_MIN > DROW.SEND_QTY) THEN
                                     '2'
                                    WHEN (DROW.DELVRY_RANGE_MAX > DROW.SEND_QTY AND DROW.DELVRY_RANGE_MIN < DROW.SEND_QTY) THEN
                                     '1'
                                    ELSE
                                     '0'
                                  END)
                               END),
             T.CHANGE_NO        = DROW.CHANGE_NO,
             T.PRINT_COUNT      = DROW.PRINT_COUNT,
             T.STLMT_UNIT_NO    = DROW.STLMT_UNIT_NO,
             T.STLMT_UNIT_NM    = DROW.STLMT_UNIT_NM,
             T.SUPPLY_UNIT      = DROW.SUPPLY_UNIT,
             T.FINAL_USER       = DROW.FINAL_USER,
             T.FINAL_USER_DESC  = DROW.FINAL_USER_DESC,
             T.ORDER_LN_STATUS  = DROW.ORDER_LN_STATUS,
             T.BALNCE_PRICE_STS = DROW.BALNCE_PRICE_STS,
             T.PRICE_NO         = DROW.PRICE_NO,
             T.SALE_AREA_NO     = DROW.SALE_AREA_NO,
             T.SALE_AREA_NM     = DROW.SALE_AREA_NM,
             T.SALE_ORG         = DROW.SALE_ORG,
             T.SEND_COUNT       = DROW.SEND_COUNT,
             T.UPDATE_TIME      = SYSDATE,
             T.BAL_QTY          = DROW.BAL_QTY,
             T.SCH_QTY          = DROW.SCH_QTY,
             T.STORE_QTY        = DROW.STORE_QTY,
             T.SCH_COUNT        = DROW.SCH_COUNT
       WHERE T.ORD_LN_PK = DROW.ORD_LN_PK;
    END IF;
  END LOOP;
  
  DELETE FROM SLM_ORDER_AUTHENTICATION T
  WHERE T.ORD_LN_PK NOT IN
       (SELECT A.ORD_LN_PK FROM SLM_ORDER_LINE A WHERE A.VALIDFLAG = '1');
  
END;
          
        ]]>
     </query>
     
     <query id = "CoreOrderAuthentication.getOrdData" desc = "获取合同数据至合同认证表">
       <![CDATA[
       
  DECLARE
  	V_COUNT NUMBER := 0;
  BEGIN
    FOR DROW IN (SELECT B.ORDER_NO || '/' || A.ORDER_SEQ ORDER_SEQ,
                          A.ORD_PK,
                          A.ORD_LN_PK,
                          B.CUSTOMER_NO,
                          B.CUSTOMER_NM,
                          A.PRODUCCODE,
                          A.PRODUCNAME,
                          A.STD_CODE,
                          A.STD_NAME,
                          A.STEELCODE,
                          A.STEELNAME,
                          A.SPEC_CODE,
                          A.SPEC_NAME,
                          A.MODEL_CODE,
                          A.MODEL_DESC,
                          TO_CHAR(B.SIGN_DATE, 'YYYY-MM-DD') SIGN_DATE,
                          SLM.DELVRY_EDATE DELVRY_DATE,
                          A.ORDER_QTY,
                          A.ORDER_UNIT,
                          ('(-' || A.DELVRY_RANGE_MIN || '%' || ',' ||
                          A.DELVRY_RANGE_MAX || '%)') DELVRY_RANGE_TPE,
                          NVL(A.ORDER_QTY,0)-(NVL(A.DELVRY_RANGE_MIN,0)/100)*A.ORDER_QTY DELVRY_RANGE_MIN,
                          NVL(A.ORDER_QTY,0)+(NVL(A.DELVRY_RANGE_MAX,0)/100)*A.ORDER_QTY DELVRY_RANGE_MAX,
                          B.CUSTM_ORDER_NO,                      
                          B.PRINT_COUNT,
                          B.STLMT_UNIT_NO,
                          B.STLMT_UNIT_NM,
                          B.SUPPLY_UNIT,
                          A.FINAL_USER,
                          A.FINAL_USER_DESC,
                          A.ORDER_LN_STATUS,
                          COM.BASENAME ORD_LN_STATUS_DESC,
                          DECODE(A.BALNCE_PRICE_STS, '1', '确认', '未确认') BALNCE_PRICE_STS,
                          B.PRICE_NO,
                          B.SALE_ORG,
                          B.SALE_AREA_NO,
                          E.SALE_AREA_DESC SALE_AREA_NM,
                          B.ORDER_NO          
                     FROM SLM_ORDER_LINE A
                     LEFT JOIN SLM_ORDER_HEAD B
                       ON A.ORD_PK = B.ORD_PK
                     LEFT JOIN (SELECT TO_CHAR(MAX(D.DELVRY_EDATE), 'YYYY-MM-DD') DELVRY_EDATE,
                                      D.ORD_LN_PK
                                 FROM SLM_ORDER_LINE_DELIVERY D
                                WHERE D.VALIDFLAG = '1'
                                GROUP BY D.ORD_LN_PK) SLM
                       ON SLM.ORD_LN_PK = A.ORD_LN_PK
                     LEFT JOIN COM_BASE_INFO COM
                       ON COM.BASECODE = A.ORDER_LN_STATUS
                     LEFT JOIN SLM_BASE_SALE_AREA_TYP E
                       ON B.SALE_AREA_NO = E.SALE_AREA_NO               
                    WHERE A.CREATE_NAME IS NOT NULL
                      AND B.CREATE_NAME IS NOT NULL
                      AND A.VALIDFLAG = '1'
                      AND B.VALIDFLAG = '1'
                      --AND B.ORDER_STS >= '12020301'
                      AND B.SALE_ORG IN ('100101', '100103')
                      AND NOT EXISTS (SELECT 1 FROM SLM_ORDER_AUTHENTICATION T WHERE T.ORDER_SEQ=B.ORDER_NO || '/' || A.ORDER_SEQ)) LOOP
                    
       SELECT NVL(COUNT(1), 0) INTO V_COUNT  FROM SLM_ORDER_AUTHENTICATION TA WHERE TA.ORD_LN_PK = DROW.ORD_LN_PK;
       IF (V_COUNT = 0) THEN
          INSERT INTO SLM_ORDER_AUTHENTICATION
            (ORD_PK,
             ORD_LN_PK,
             ORDER_SEQ,
             CUSTOMER_NO,
             CUSTOMER_NM,
             PRODUCCODE,
             PRODUCNAME,
             STD_CODE,
             STD_NAME,
             STEELCODE,
             STEELNAME,
             SPEC_CODE,
             SPEC_NAME,
             MODEL_CODE,
             MODEL_DESC,
             SIGN_DATE,
             DELVRY_DATE,
             ORDER_QTY,
             ORDER_UNIT,
             DELVRY_RANGE_TPE,
             CUSTM_ORDER_NO,
             PRINT_COUNT,
             STLMT_UNIT_NO,
             STLMT_UNIT_NM,
             SUPPLY_UNIT,
             FINAL_USER,
             FINAL_USER_DESC,
             ORDER_LN_STATUS,
             BALNCE_PRICE_STS,
             PRICE_NO,
             SALE_AREA_NO,
             SALE_AREA_NM,
             SALE_ORG,
             UPDATE_TIME,
             ORDER_NO)
          VALUES
            (DROW.ORD_PK,
             DROW.ORD_LN_PK,
             DROW.ORDER_SEQ,
             DROW.CUSTOMER_NO,
             DROW.CUSTOMER_NM,
             DROW.PRODUCCODE,
             DROW.PRODUCNAME,
             DROW.STD_CODE,
             DROW.STD_NAME,
             DROW.STEELCODE,
             DROW.STEELNAME,
             DROW.SPEC_CODE,
             DROW.SPEC_NAME,
             DROW.MODEL_CODE,
             DROW.MODEL_DESC,
             TO_DATE(DROW.SIGN_DATE, 'YYYY-MM-DD'),
             TO_DATE(DROW.DELVRY_DATE, 'YYYY-MM-DD'),
             DROW.ORDER_QTY,
             DROW.ORDER_UNIT,
             DROW.DELVRY_RANGE_TPE,
             DROW.CUSTM_ORDER_NO,
             DROW.PRINT_COUNT,
             DROW.STLMT_UNIT_NO,
             DROW.STLMT_UNIT_NM,
             DROW.SUPPLY_UNIT,
             DROW.FINAL_USER,
             DROW.FINAL_USER_DESC,
             DROW.ORDER_LN_STATUS,
             DROW.BALNCE_PRICE_STS,
             DROW.PRICE_NO,
             DROW.SALE_AREA_NO,
             DROW.SALE_AREA_NM,
             DROW.SALE_ORG,
             SYSDATE,
             DROW.ORDER_NO);
    ELSE
      UPDATE SLM_ORDER_AUTHENTICATION T
         SET T.ORD_PK           = DROW.ORD_PK,
             T.ORD_LN_PK        = DROW.ORD_LN_PK,
             T.ORDER_SEQ        = DROW.ORDER_SEQ,
             T.CUSTOMER_NO      = DROW.CUSTOMER_NO,
             T.CUSTOMER_NM      = DROW.CUSTOMER_NM,
             T.PRODUCCODE       = DROW.PRODUCCODE,
             T.PRODUCNAME       = DROW.PRODUCNAME,
             T.STD_CODE         = DROW.STD_CODE,
             T.STD_NAME         = DROW.STD_NAME,
             T.STEELCODE        = DROW.STEELCODE,
             T.STEELNAME        = DROW.STEELNAME,
             T.SPEC_CODE        = DROW.SPEC_CODE,
             T.SPEC_NAME        = DROW.SPEC_NAME,
             T.MODEL_CODE       = DROW.MODEL_CODE,
             T.MODEL_DESC       = DROW.MODEL_DESC,
             T.SIGN_DATE        = TO_DATE(DROW.SIGN_DATE, 'YYYY-MM-DD'),
             T.DELVRY_DATE      = TO_DATE(DROW.DELVRY_DATE, 'YYYY-MM-DD'),
             T.ORDER_QTY        = DROW.ORDER_QTY,
             T.ORDER_UNIT       = DROW.ORDER_UNIT,
             T.DELVRY_RANGE_TPE = DROW.DELVRY_RANGE_TPE,
             T.PRINT_COUNT      = DROW.PRINT_COUNT,
             T.STLMT_UNIT_NO    = DROW.STLMT_UNIT_NO,
             T.STLMT_UNIT_NM    = DROW.STLMT_UNIT_NM,
             T.SUPPLY_UNIT      = DROW.SUPPLY_UNIT,
             T.FINAL_USER       = DROW.FINAL_USER,
             T.FINAL_USER_DESC  = DROW.FINAL_USER_DESC,
             T.ORDER_LN_STATUS  = DROW.ORDER_LN_STATUS,
             T.BALNCE_PRICE_STS = DROW.BALNCE_PRICE_STS,
             T.PRICE_NO         = DROW.PRICE_NO,
             T.SALE_AREA_NO     = DROW.SALE_AREA_NO,
             T.SALE_AREA_NM     = DROW.SALE_AREA_NM,
             T.SALE_ORG         = DROW.SALE_ORG,
             T.UPDATE_TIME      = SYSDATE,
             T.ORDER_NO         = DROW.ORDER_NO
       WHERE T.ORD_LN_PK = DROW.ORD_LN_PK;
    END IF;
  END LOOP;
  
END;
          
        ]]>
     </query>
     
      <query id = "CoreOrderAuthentication.QueryOrdNo" desc = "更新数据">
       <![CDATA[
   
                  SELECT DISTINCT A.ORDER_NO , 'FALSE' CHK
          FROM SLM_ORDER_HEAD A, SLM_ORDER_LINE B
        
         WHERE A.ORD_PK = B.ORD_PK
           AND A.CREATE_NAME IS NOT NULL
           AND A.VALIDFLAG = '1'
           AND B.CREATE_NAME IS NOT NULL
           AND B.VALIDFLAG = '1'
           AND A.ORDER_STS >= '12020301'
           AND A.SALE_ORG IN ('100101', '100103')

        ]]>
     </query>
     
      <query id = "CoreOrderAuthentication.updateOrdFile" desc = "更新数据">
       <![CDATA[
   		
   		DECLARE
    BEGIN

       UPDATE SLM_ORDER_AUTHENTICATION T
        SET T.ORDER_FILE = ?
       WHERE T.ORD_PK = ?;

       UPDATE SLM_ORDER_HEAD T SET T.ORDER_FILE = ? WHERE T.ORD_PK = ?;
    END;
   		
        ]]>
     </query>
     
      <query id = "CoreOrderAuthentication.updateUserOrdFile" desc = "更新数据">
       <![CDATA[
   		
   		DECLARE
    BEGIN

       UPDATE SLM_ORDER_AUTHENTICATION T
        SET T.USERORDER_FILE = ?
       WHERE T.ORD_PK = ?;

       UPDATE SLM_ORDER_HEAD T SET T.USERORDER_FILE = ? WHERE T.ORD_PK = ?;
    END;
   		
        ]]>
     </query>

</queryMap>
