<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc="排产信息">

	<query id="CoreSchedulingInfomation.getSchedulingInfo" desc="查询排产信息">
		<![CDATA[
SELECT T1.ORDER_NO || '/' || T1.ORDER_SEQ ORDER_NO_ALL,
       T1.ORDER_NO,
       T1.ORDER_SEQ,
       T1.DELIVERY_NO,
       T1.ROLL_MONTH,
       T1.ROLL_WELL_MONTH,
       MIN(ROLL_MONTH) OVER(PARTITION BY T1.ORD_LN_PK) FRIST_ROLL_MONTH,
			 CASE
			WHEN T1.ORDER_NO LIKE 'TH%' THEN
				(SELECT SALE_AREA_DESC FROM SLM_ORDER_HEAD WHERE ORDER_NO = (
				SELECT CUSTM_ORDER_NO FROM SLM_ORDER_HEAD WHERE ORDER_NO = T1.ORDER_NO and VALIDFLAG = '1'
				) and VALIDFLAG = '1')
			ELSE
				Q.SALE_AREA_DESC
			END SALE_AREA_DESC,
        (SELECT case when T4.ABRVITION is not null then T4.ABRVITION else t4.customer_nm end ABRVITION
          FROM SLM_BASE_CUSTOMER T4
         WHERE T4.CUSTOMER_NO = T1.FINAL_USER) FINAL_USER_DESC,
       T1.PRODUCNAME,
       T1.STEELNAME,
       T1.SPEC_NAME,
       T1.MODEL_DESC,
       T1.STD_NAME,
       T1.LENGTHDISC,
       T1.SUPPLY_UNIT,
       Q.CUSTOMER_NM,
       Q.RECEIV_NM,
       Q.STLMT_UNIT_NM,
       Q.AW,
       (SELECT BASENAME  FROM COM_BASE_INFO WHERE BASECODE = Q.TRANSIT_TYP ) TRANSIT_TYP,
        (SELECT (SELECT STATION_NM  FROM SLM_BASE_STATION WHERE STATION_NO =K.STATION_NO )
          FROM SLM_ORDER_DELIVERY_TRANSIT K
         WHERE K.ORD_LN_DLY_PK = Q.ORD_LN_DLY_PK
           AND K.VALIDFLAG = '1'
           AND ROWNUM = 1) TRAINS_STATION,
       TO_CHAR(T1.DELVRY_EDATE,'YYYY-MM-DD') DELVRY_EDATE,
       (SELECT A.BASENAME
          FROM COM_BASE_INFO A
         WHERE T1.OUTS_BILLET_FL = A.BASECODE) OUTS_BILLET_FL,
       (SELECT A.BASENAME
          FROM COM_BASE_INFO A
         WHERE T1.OUTS_STL_PIPE_FL = A.BASECODE) OUTS_STL_PIPE_FL,
				CASE
					WHEN T1.ORDER_NO LIKE 'TH%' THEN
						(SELECT SALE_ORG_DESC FROM SLM_ORDER_HEAD WHERE ORDER_NO = (
						SELECT CUSTM_ORDER_NO FROM SLM_ORDER_HEAD WHERE ORDER_NO = T1.ORDER_NO and VALIDFLAG = '1'
						) and VALIDFLAG = '1')
					ELSE
						(SELECT A.BASENAME
						FROM COM_BASE_INFO A
					 WHERE T1.ORDERFROM = A.BASECODE
							OR T1.ORDERFROM = A.BASENAME)
				END ORDERFROM,
       DECODE(T1.PORDERSTATUS,
              '0',
              '待接收',
              '1',
              '已接收',
              '2',
              '已下发',
              '3',
              '生产中',
              '4',
              '生产完成',
              '5',
              '缴库完成',
              '6',
              '发运完毕',
              '7',
              '结算完毕',
              '8',
              '合同关闭') DELIVERY_STATUS,
       DECODE(T1.DELVRY_RANGE_TPE,
              '%',
              '(-' || T1.DELVRY_RANGE_MIN || '%' || ',' ||
              T1.DELVRY_RANGE_MAX || '%)',
              '(-' || T1.DELVRY_RANGE_MIN || T1.ORDER_UNIT || ',' ||
              T1.DELVRY_RANGE_MAX || T1.ORDER_UNIT || ')') DELVRY_RANGE_TPE,
       T1.CREATE_NAME,
       T1.ORDER_ADD_DESC,
       TO_CHAR(T1.CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
       T1.ORDER_UNIT,
       TO_CHAR(T1.ORDER_QTY, 'FM9999990.000') ORDER_QTY,
       TO_CHAR(Q1.PLANED_WEIGHT, 'FM9999990.000') PLANED_WEIGHT,
       NVL(Q1.PLANED_COUNT, 0) AS PLANED_COUNT,
       NVL(PAYDOWN_NUM, 0) AS AMOUNT_COUNT,
       TO_CHAR(NVL(T1.PAYDOWNWEIGHT, 0), 'FM9999990.000') AMOUNT_WEIGHT,
       NVL(T5.PLAN_NUM, 0) AS BILL_COUNT,
       NVL(T5.PLAN_WT, 0) AS BILL_WEIGHT,
       NVL(T6.ACT_COUNT, 0) AS OUT_COUNT,
       NVL(T6.ACT_WEIGHT, 0) AS OUT_WEIGHT,
       (NVL(T5.PLAN_NUM, 0) - NVL(T6.ACT_COUNT, 0)) AS ZT_COUNT,
       (NVL(T5.PLAN_WT, 0) - NVL(T6.ACT_WEIGHT, 0)) AS ZT_WEIGHT,
       (SELECT SIGN_PERSON FROM SLM_ORDER_HEAD WHERE ORD_PK = T1.ORD_PK) ORDER_CREATE_NAME
  FROM PLN_SALEORD T1
  LEFT JOIN (SELECT A.ORD_LN_DLY_PK,
                    SUM(A.PLAN_NUM) PLAN_NUM,
                    SUM(A.PLAN_WT) PLAN_WT
               FROM YDM_ZC_BILL_M A
              WHERE A.VALIDFLAG = '1'
                AND A.RETURN_FLAG = '0'
              GROUP BY A.ORD_LN_DLY_PK) T5
    ON T1.ORD_LN_DLY_PK = T5.ORD_LN_DLY_PK
  LEFT JOIN (SELECT SUM(T.ACT_COUNT) ACT_COUNT,
                    SUM(T.ACT_WEIGHT) ACT_WEIGHT,
                    T.ORD_LN_DLY_PK
               FROM YDM_ZC_OUTLIST T
              WHERE T.VALIDFLAG = '1'
                AND T.OUTSTOCK_TYPE_CODE = '800805'
              GROUP BY T.ORD_LN_DLY_PK) T6
    ON T6.ORD_LN_DLY_PK = T1.ORD_LN_DLY_PK
  LEFT JOIN (SELECT MAX(T2.SALE_AREA_DESC) SALE_AREA_DESC,
                    MAX(T2.CUSTOMER_NM) CUSTOMER_NM,
                    MAX(T2.STLMT_UNIT_NM) STLMT_UNIT_NM,
                    MAX(T8.RECEIV_NM) RECEIV_NM,
                    MAX(T9.AW) AW,
                    MAX(T8.TRANSIT_TYP) TRANSIT_TYP,
                    T8.ORD_LN_DLY_PK
               FROM SLM_ORDER_HEAD          T2,
                    SLM_ORDER_LINE          T9,
                    SLM_ORDER_LINE_DELIVERY T8
             
              WHERE T2.ORD_PK = T8.ORD_PK
                AND T2.ORD_PK = T9.ORD_PK
                AND T9.ORD_LN_PK = T8.ORD_LN_PK
                AND T2.VALIDFLAG = '1'
                AND T8.VALIDFLAG = '1'
                AND T2.CREATE_NAME IS NOT NULL
                AND T9.CREATE_NAME IS NOT NULL
                AND T9.VALIDFLAG = '1'
                -- #CONDITION#
              GROUP BY T8.ORD_LN_DLY_PK) Q
    ON Q.ORD_LN_DLY_PK = T1.ORD_LN_DLY_PK
  LEFT JOIN (SELECT SUM(T.PLAN_P_MAIN) PLANED_WEIGHT,
                    SUM(T.PLAN_NUM_D) PLANED_COUNT,
                    SUBSTR(ORD_LN_DLY_PK, 1, 35) ORD_LN_PK,
                    T.ORD_LN_DLY_PK
               FROM PLN_SALEORD_PROC T
              WHERE T.ORDER_SOURCE != '备料需求'
              GROUP BY SUBSTR(ORD_LN_DLY_PK, 1, 35),ORD_LN_DLY_PK) Q1
    ON T1.ORD_LN_PK = Q1.ORD_LN_PK AND T1.ORD_LN_DLY_PK = Q1.ORD_LN_DLY_PK

 WHERE UPPER(T1.ORDER_NO) || '&' LIKE UPPER('%' || ? || '%')
   AND T1.CREATE_NAME IS NOT NULL
   AND T1.PORDERSTATUS >= '0'
		]]>
	</query>
	
	
	<query id="CoreSchedulingInfomation.getSchedulingInfoByOrdLnPk" desc="查询排产信息">
		<![CDATA[
	    SELECT T1.ORDER_NO || '/' || T1.ORDER_SEQ ORDER_NO_ALL,
       T1.ORDER_NO,
       T1.ORDER_SEQ,
       T1.DELIVERY_NO,
       T1.ROLL_MONTH,
       T1.ROLL_WELL_MONTH,
       MIN(ROLL_MONTH) OVER(PARTITION BY ?) FRIST_ROLL_MONTH,
       Q.SALE_AREA_DESC,
       (SELECT T4.ABRVITION
          FROM SLM_BASE_CUSTOMER T4
         WHERE T4.CUSTOMER_NO = T1.FINAL_USER) FINAL_USER_DESC,
       T1.PRODUCNAME,
       T1.STEELNAME,
       T1.SPEC_NAME,
       T1.MODEL_DESC,
       T1.STD_NAME,
       T1.LENGTHDISC,
       Q.CUSTOMER_NM,
       Q.RECEIV_NM,
       Q.STLMT_UNIT_NM,
       Q.AW,
       T1.TRANSIT_TYP,
       T1.STATION_NM TRAINS_STATION,
       TO_CHAR(T1.DELVRY_EDATE,'YYYY-MM-DD') DELVRY_EDATE,
       (SELECT A.BASENAME
          FROM COM_BASE_INFO A
         WHERE T1.OUTS_BILLET_FL = A.BASECODE) OUTS_BILLET_FL,
       (SELECT A.BASENAME
          FROM COM_BASE_INFO A
         WHERE T1.OUTS_STL_PIPE_FL = A.BASECODE) OUTS_STL_PIPE_FL,
       (SELECT A.BASENAME
          FROM COM_BASE_INFO A
         WHERE T1.ORDERFROM = A.BASECODE
            OR T1.ORDERFROM = A.BASENAME) ORDERFROM,
       DECODE(T1.PORDERSTATUS,
              '0',
              '待接收',
              '1',
              '已接收',
              '2',
              '已下发',
              '3',
              '生产中',
              '4',
              '生产完成',
              '5',
              '缴库完成',
              '6',
              '发运完毕',
              '7',
              '结算完毕',
              '8',
              '合同关闭') DELIVERY_STATUS,
       DECODE(T1.DELVRY_RANGE_TPE,
              '%',
              '(-' || T1.DELVRY_RANGE_MIN || '%' || ',' ||
              T1.DELVRY_RANGE_MAX || '%)',
              '(-' || T1.DELVRY_RANGE_MIN || T1.ORDER_UNIT || ',' ||
              T1.DELVRY_RANGE_MAX || T1.ORDER_UNIT || ')') DELVRY_RANGE_TPE,
       T1.CREATE_NAME,
       T1.ORDER_ADD_DESC,
       TO_CHAR(T1.CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
       T1.ORDER_UNIT,
       TO_CHAR(T1.ORDER_QTY, 'FM9999990.000') ORDER_QTY,
       TO_CHAR(Q1.PLANED_WEIGHT, 'FM9999990.000') PLANED_WEIGHT,
       NVL(Q1.PLANED_COUNT, 0) AS PLANED_COUNT,
       NVL(PAYDOWN_NUM, 0) AS AMOUNT_COUNT,
       TO_CHAR(NVL(T1.PAYDOWNWEIGHT, 0), 'FM9999990.000') AMOUNT_WEIGHT,
       NVL(T5.PLAN_NUM, 0) AS BILL_COUNT,
       NVL(T5.PLAN_WT, 0) AS BILL_WEIGHT,
       NVL(T6.ACT_COUNT, 0) AS OUT_COUNT,
       NVL(T6.ACT_WEIGHT, 0) AS OUT_WEIGHT,
       (NVL(T5.PLAN_NUM, 0) - NVL(T6.ACT_COUNT, 0)) AS ZT_COUNT,
       (NVL(T5.PLAN_WT, 0) - NVL(T6.ACT_WEIGHT, 0)) AS ZT_WEIGHT
  FROM PLN_SALEORD T1
  LEFT JOIN (SELECT A.ORD_LN_DLY_PK,
                    SUM(A.PLAN_NUM) PLAN_NUM,
                    SUM(A.PLAN_WT) PLAN_WT
               FROM YDM_ZC_BILL_M A
              WHERE A.ORD_LN_PK  = ?
                AND A.RETURN_FLAG = '0'
                AND A.VALIDFLAG = '1'
              GROUP BY A.ORD_LN_DLY_PK) T5
    ON T1.ORD_LN_DLY_PK = T5.ORD_LN_DLY_PK
  LEFT JOIN (SELECT SUM(T.ACT_COUNT) ACT_COUNT,
                    SUM(T.ACT_WEIGHT) ACT_WEIGHT,
                    T.ORD_LN_DLY_PK
               FROM YDM_ZC_OUTLIST T
              WHERE T.ORD_LN_PK = ?
                AND T.VALIDFLAG = '1'
                AND T.OUTSTOCK_TYPE_CODE = '800805'
              GROUP BY T.ORD_LN_DLY_PK) T6
    ON T6.ORD_LN_DLY_PK = T1.ORD_LN_DLY_PK
  LEFT JOIN (SELECT MAX(T2.SALE_AREA_DESC) SALE_AREA_DESC,
                    MAX(T2.CUSTOMER_NM) CUSTOMER_NM,
                    MAX(T2.STLMT_UNIT_NM) STLMT_UNIT_NM,
                    MAX(T8.RECEIV_NM) RECEIV_NM,
                    MAX(T9.AW) AW,
                    T8.ORD_LN_DLY_PK
               FROM SLM_ORDER_HEAD T2,
                    SLM_ORDER_LINE T9,
                    SLM_ORDER_LINE_DELIVERY T8
                    
              WHERE T2.ORD_PK = T8.ORD_PK
                AND T2.ORD_PK = T9.ORD_PK
                AND T9.ORD_LN_PK = T8.ORD_LN_PK
                AND T9.ORD_LN_PK = ?
                AND T2.VALIDFLAG = '1'
                AND T8.VALIDFLAG = '1'
                AND T2.CREATE_NAME IS NOT NULL
                AND T9.CREATE_NAME IS NOT NULL
                AND T9.VALIDFLAG = '1'

              GROUP BY T8.ORD_LN_DLY_PK) Q
    ON Q.ORD_LN_DLY_PK = T1.ORD_LN_DLY_PK
   LEFT JOIN (SELECT SUM(T.PLAN_P_MAIN) PLANED_WEIGHT,
                    SUM(T.PLAN_NUM_D) PLANED_COUNT,
                    SUBSTR(ORD_LN_DLY_PK, 1, 35) ORD_LN_PK
               FROM PLN_SALEORD_PROC T
              WHERE T.ORDER_SOURCE != '备料需求'
                AND SUBSTR(ORD_LN_DLY_PK, 1, 35) = ?
              GROUP BY SUBSTR(ORD_LN_DLY_PK, 1, 35)) Q1
    ON T1.ORD_LN_PK = Q1.ORD_LN_PK

 WHERE T1.ORD_LN_PK =  ?
   AND T1.CREATE_NAME IS NOT NULL
		]]>
	</query>

</queryMap>