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

	<query id="FrmJgxSInfo.getPlanInfo" desc="查询排产信息">
             <![CDATA[
SELECT Q4.BASECODE GROUP_ID,
       Q4.BASENAME GROUP_PLAN,
       Q4.PLINE_CODE,
       Q4.PLINE_NAME,
       TO_CHAR(Q1.DIFFER_TIME, '99D9') REMAIN_DAY1,
       ROUND(Q1.WEIGTH_S, 0) WGT1,
       ROUND(Q1.PLAN_WGT, 0) PLAN_WGT1,
       TO_CHAR(Q2.DIFFER_TIME, '99D9') REMAIN_DAY2,
       ROUND(Q2.WEIGTH_S, 0) WGT2,
       ROUND(Q2.PLAN_WGT, 0) PLAN_WGT2,
       TO_CHAR(Q3.DIFFER_TIME, '99D9') REMAIN_DAY3,
       ROUND(Q3.WEIGTH_S, 0) WGT3,
       ROUND(Q3.PLAN_WGT, 0) PLAN_WGT3,
       TO_CHAR(Q5.PLAN_TIME_B, 'YYYY-MM-DD HH24:MI:SS') PLAN_TIME_B,
       TO_CHAR(Q5.PLAN_TIME_E, 'YYYY-MM-DD HH24:MI:SS') PLAN_TIME_E,
       Q5.REPAIR_TYPE
  FROM (SELECT NVL(SUM(T3.WEIGTH_S), 0) WEIGTH_S,
               SUM(CASE
                     WHEN T3.EXE_STATUS = '0' THEN
                      T3.WEIGTH_S
                     WHEN T3.EXE_STATUS = '1' THEN
                      T3.WEIGTH_S
                     ELSE
                      0
                   END) PLAN_WGT,
               (CASE
                 WHEN (TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') - --最大时间
                      MAX(T3.PLAN_TIME_E)) < 0 THEN
                  0
                 ELSE
                  (TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') - MAX(T3.PLAN_TIME_E)) --最大时间
               END) DIFFER_TIME,
               T3.PLINE_CODE
          FROM (SELECT (CASE
                         WHEN T5.PLAN_TIME_B <
                              TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') THEN --最小时间
                          CEIL((T5.PLAN_TIME_E -
                               TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60) / --最小时间
                          (CASE WHEN  TO_NUMBER(T5.PLAN_USETIME)=0 THEN CEIL((T5.PLAN_TIME_E - T5.PLAN_TIME_B) * 24 * 60) ELSE TO_NUMBER(T5.PLAN_USETIME) END) * T5.WEIGTH_S
                         WHEN T5.PLAN_TIME_E >
                              TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') THEN --最大时间
                          CEIL((TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') - --最大时间
                               T5.PLAN_TIME_B) * 24 * 60) /
                          (CASE WHEN  TO_NUMBER(T5.PLAN_USETIME)=0 THEN CEIL((T5.PLAN_TIME_E - T5.PLAN_TIME_B) * 24 * 60) ELSE TO_NUMBER(T5.PLAN_USETIME) END) * T5.WEIGTH_S
                         ELSE
                          T5.WEIGTH_S
                       END) WEIGTH_S,
                       T5.PLAN_TIME_B,
                       T5.PLAN_TIME_E,
                       T5.EXE_STATUS,
                       T5.PLINE_CODE
                  FROM PLN_ORDER_JGX_S T5
                 WHERE T5.PLAN_TIME_E >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') --最小时间
                   AND T5.PLAN_TIME_B <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')) T3 --最大时间
         GROUP BY T3.PLINE_CODE) Q1,
       (SELECT NVL(SUM(T3.WEIGTH_S), 0) WEIGTH_S,
               SUM(CASE
                     WHEN T3.EXE_STATUS = '0' THEN
                      T3.WEIGTH_S
                     WHEN T3.EXE_STATUS = '1' THEN
                      T3.WEIGTH_S
                     ELSE
                      0
                   END) PLAN_WGT,
               (CASE
                 WHEN (TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') -
                      MAX(T3.PLAN_TIME_E)) < 0 THEN
                  0
                 ELSE
                  (TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') - MAX(T3.PLAN_TIME_E))
               END) DIFFER_TIME,
               T3.PLINE_CODE
          FROM (SELECT (CASE
                         WHEN T5.PLAN_TIME_B <
                              TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') THEN --最小时间
                          CEIL((T5.PLAN_TIME_E -
                               TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60) / --最小时间
                          (CASE WHEN  TO_NUMBER(T5.PLAN_USETIME)=0 THEN CEIL((T5.PLAN_TIME_E - T5.PLAN_TIME_B) * 24 * 60) ELSE TO_NUMBER(T5.PLAN_USETIME) END) * T5.WEIGTH_S
                         WHEN T5.PLAN_TIME_E >
                              TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') THEN --最大时间
                          CEIL((TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') - --最大时间
                               T5.PLAN_TIME_B) * 24 * 60) /
                          (CASE WHEN  TO_NUMBER(T5.PLAN_USETIME)=0 THEN CEIL((T5.PLAN_TIME_E - T5.PLAN_TIME_B) * 24 * 60) ELSE TO_NUMBER(T5.PLAN_USETIME) END) * T5.WEIGTH_S
                         ELSE
                          T5.WEIGTH_S
                       END) WEIGTH_S,
                       T5.PLAN_TIME_B,
                       T5.PLAN_TIME_E,
                       T5.EXE_STATUS,
                       T5.PLINE_CODE
                  FROM PLN_ORDER_JGX_S T5
                 WHERE T5.PLAN_TIME_E >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') --最小时间
                   AND T5.PLAN_TIME_B <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')) T3
         GROUP BY T3.PLINE_CODE) Q2,
       (SELECT NVL(SUM(T3.WEIGTH_S), 0) WEIGTH_S,
               SUM(CASE
                     WHEN T3.EXE_STATUS = '0' THEN
                      T3.WEIGTH_S
                     WHEN T3.EXE_STATUS = '1' THEN
                      T3.WEIGTH_S
                     ELSE
                      0
                   END) PLAN_WGT,
               (CASE
                 WHEN (TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') -
                      MAX(T3.PLAN_TIME_E)) < 0 THEN
                  0
                 ELSE
                  (TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') - MAX(T3.PLAN_TIME_E))
               END) DIFFER_TIME,
               T3.PLINE_CODE
          FROM (SELECT (CASE
                         WHEN T5.PLAN_TIME_B <
                              TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') THEN --最小时间
                          CEIL((T5.PLAN_TIME_E -
                               TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60) / --最小时间
                          (CASE WHEN  TO_NUMBER(T5.PLAN_USETIME)=0 THEN CEIL((T5.PLAN_TIME_E - T5.PLAN_TIME_B) * 24 * 60) ELSE TO_NUMBER(T5.PLAN_USETIME) END) * T5.WEIGTH_S
                         WHEN T5.PLAN_TIME_E >
                              TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') THEN --最大时间
                          CEIL((TO_DATE(?, 'yyyy-mm-dd hh24-mi-ss') - --最大时间
                               T5.PLAN_TIME_B) * 24 * 60) /
                          (CASE WHEN  TO_NUMBER(T5.PLAN_USETIME)=0 THEN CEIL((T5.PLAN_TIME_E - T5.PLAN_TIME_B) * 24 * 60) ELSE TO_NUMBER(T5.PLAN_USETIME) END) * T5.WEIGTH_S
                         ELSE
                          T5.WEIGTH_S
                       END) WEIGTH_S,
                       T5.PLAN_TIME_B,
                       T5.PLAN_TIME_E,
                       T5.EXE_STATUS,
                       T5.PLINE_CODE
                  FROM PLN_ORDER_JGX_S T5
                 WHERE T5.PLAN_TIME_E >= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') --最小时间
                   AND T5.PLAN_TIME_B <= TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')) T3
         GROUP BY T3.PLINE_CODE) Q3,
       (SELECT T1.BASECODE,
               T1.BASENAME,
               T2.PLINE_CODE,
               T2.PLINE_NAME,
               T2.DEPARTMENT_CODE
          FROM COM_BASE_PLINE T2,
               (SELECT T.BASECODE, T.BASENAME
                  FROM COM_BASE_INFO T
                 WHERE T.SORTCODE = '2004') T1
         WHERE T2.GROUP_PLAN = T1.BASECODE
         ORDER BY T1.BASECODE, T2.PLINE_CODE) Q4,
       (SELECT T5.PLAN_TIME_E,
               T5.PLAN_TIME_B,
               NVL(T5.REPAIR_TYPE, '合同') REPAIR_TYPE,
               T5.PLINE_CODE
          FROM PLN_ORDER_JGX_S T5
         WHERE T5.EXE_STATUS = '1') Q5
 WHERE Q4.PLINE_CODE = Q1.PLINE_CODE(+)
   AND Q4.PLINE_CODE = Q2.PLINE_CODE(+)
   AND Q4.PLINE_CODE = Q3.PLINE_CODE(+)
   AND Q4.PLINE_CODE = Q5.PLINE_CODE(+)
                 ]]>
	</query>

	<query id="FrmJgxSInfo.getRepairPlan" desc="查询计划分组">
             <![CDATA[
                 SELECT T.BASECODE, T.BASENAME
  FROM COM_BASE_INFO T
 WHERE T.SORTCODE = ?
   AND T.VALIDFLAG = '1'
                 ]]>
	</query>

</queryMap>