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

<queryMap desc = "调整结转年月">

<query id = "FrmUpdateBalYearMonth.getBalYearMonth" desc = "查询数据  ">
<![CDATA[
select T.PLAN_ID,
       T.LOT_NO,
       T.JUDGE_STOVE_NO,
       T.PLINE_CODE,
       T.BAL_YEAR_MONTH,
       T.BAL_YEAR_MONTH BAL_YEAR_MONTH1,
       (SELECT C.PLINE_NAME
          FROM COM_BASE_PLINE C
         WHERE C.PLINE_CODE = T.PLINE_CODE) PLINE_NAME,
       SUM(T.ACT_COUNT) ACT_COUNT,
       SUM(T.ACT_COUNT) ACT_COUNT1,
       SUM(T.ACT_THEORY_WEIGHT) ACT_THEORY_WEIGHT,
       T1.VARIETIES,
       T1.STEELNAME,
       T1.PIPE_SIZE,
       T1.BUCKLE_TYPE,
       T1.TARGET
  from PORT_MIL_BATCH_SAMPLE_RESULT t, MIL_PLAN T1
 WHERE T.PLAN_ID = T1.ID(+)
   AND T.JUDGE_STOVE_NO = T1.JUDGE_STOVE_NO(+)
    #something#
 GROUP BY T.PLAN_ID,
          T.LOT_NO,
          T.JUDGE_STOVE_NO,
          T.PLINE_CODE,
          T.BAL_YEAR_MONTH,
          T1.VARIETIES,
          T1.STEELNAME,
          T1.PIPE_SIZE,
          T1.BUCKLE_TYPE,
          T1.TARGET
 ORDER BY T.PLINE_CODE, T.LOT_NO, T.JUDGE_STOVE_NO

]]>
</query>


<query id = "FrmUpdateBalYearMonth.doUpdate" desc = "调整结转年月  ">
<![CDATA[
DECLARE
  V_JUDGE_STOVE_NO PORT_MIL_BATCH_SAMPLE_RESULT.JUDGE_STOVE_NO%TYPE := ?;
  V_PLINE_CODE     PORT_MIL_BATCH_SAMPLE_RESULT.PLINE_CODE%TYPE := ?;
  V_ACT_COUNT      PORT_MIL_BATCH_SAMPLE_RESULT.ACT_COUNT%TYPE := ?;
  V_BAL_YEAR_MONTH PORT_MIL_BATCH_SAMPLE_RESULT.BAL_YEAR_MONTH%TYPE := ?;
BEGIN
  --修改进程表结转年月
  FOR MYROW IN (SELECT *
                  FROM (SELECT *
                          FROM PORT_MIL_BATCH_SAMPLE_RESULT T
                         WHERE T.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO
                           AND T.PLINE_CODE = V_PLINE_CODE
                           AND T.BAL_YEAR_MONTH != V_BAL_YEAR_MONTH
                         ORDER BY T.MAT_NO DESC)
                 WHERE ROWNUM <= V_ACT_COUNT) LOOP
  
    UPDATE PORT_MIL_BATCH_SAMPLE_RESULT T
       SET T.BAL_YEAR_MONTH = V_BAL_YEAR_MONTH
     WHERE T.MAT_NO = MYROW.MAT_NO
       AND T.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO;
  END LOOP;

  --修改入库表结转年月
  FOR MYROW1 IN (SELECT *
                   FROM (SELECT *
                           FROM YDM_BC_INLIST T
                          WHERE T.JUDGE_STOVE_NO =
                                REGEXP_REPLACE(V_JUDGE_STOVE_NO, '-.*', '')
                            AND T.PLINE_CODE = V_PLINE_CODE
                            AND T.BAL_YEAR_MONTH != V_BAL_YEAR_MONTH
                          ORDER BY T.MAT_NO DESC)
                  WHERE ROWNUM <= V_ACT_COUNT) LOOP
  
    UPDATE YDM_BC_INLIST T
       SET T.BAL_YEAR_MONTH = V_BAL_YEAR_MONTH
     WHERE T.MAT_NO = MYROW1.MAT_NO
       AND T.INSTOCK_TYPE_CODE = '800701'
       AND T.JUDGE_STOVE_NO = REGEXP_REPLACE(V_JUDGE_STOVE_NO, '-.*', '');
  END LOOP;

END;
]]>
</query>

<query id = "FrmUpdateBalYearMonth.getApplyBalYearMonth" desc = "查询数据  ">
<![CDATA[
select decode(t.act_weight, null, t.act_theory_weight, t.act_weight) ACT_WEIGHT1,
       t.jugde_apply_code||t.JUGDE_APPLY_SEQUENO AS jugde_apply_code,
       t.judge_stove_no,
       t.batch_no,
       t.batch_groud_no,
       t.process_code,
       t.pline_code,
       t.pline_name,
       t.act_count,
       t.pro_order_id,
       t.quality_group,
       t.material_no,
       t.material_name,
       t.producname,
       t.steelname,
       t.gradename,
       t.spec_name,
       t.model_desc,
       t.std_name,
       t.std_style_desc,
       t.craf_no,
       t.lot_no,
       t.pro_order_no,
       t.target,
       t.mat_gowhere_name,
       t.judge_result_time,
       t.judge_maker,
       t.belong_name_jugde,
       t.gx_plan_no,
       t.bal_year_month bal_year_month1,
       to_char(to_date(t.BAL_YEAR_MONTH, 'YYYY-MM'), 'yyyy-MM') as bal_year_month,
       DECODE(T.INSTOCK_FLAG, '0', '未确认', '1', '已确认') INSTOCK_FLAG,
       decode(t.IS_CARRY_VOER, '0', '未结转', '1', '已结转') IS_CARRY_VOER
  from QCM_ZG_JUGDE_APPLY t
 where t.IS_FLAG = '0'
   and t.APPLY_FLAG = '0'
   and t.VALIDFLAG = '20'
   and t.PLINE_CODE not in ('C065', 'C066')
]]>
</query>

<query id = "FrmUpdateBalYearMonth.doUpdateYearMonth" desc = "调整判定表结转年月  ">
<![CDATA[
DECLARE
  V_BAL_YEAR_MONTH   QCM_ZG_JUGDE_APPLY.BAL_YEAR_MONTH%type := ?;
  V_JUGDE_APPLY_CODE QCM_ZG_JUGDE_APPLY.JUGDE_APPLY_CODE%type := ?;
  V_JUDGE_STOVE_NO   QCM_ZG_JUGDE_APPLY.JUDGE_STOVE_NO%type := ?;
  V_BATCH_NO         QCM_ZG_JUGDE_APPLY.BATCH_NO%type := ?;
  V_BATCH_GROUD_NO   QCM_ZG_JUGDE_APPLY.BATCH_GROUD_NO%type := ?;
  V_QUALITY_GROUP    QCM_ZG_JUGDE_APPLY.QUALITY_GROUP%type := ?;
  V_MAT_GOWHERE_NAME QCM_ZG_JUGDE_APPLY.MAT_GOWHERE_NAME%type := ?;
  V_TARGET           QCM_ZG_JUGDE_APPLY.TARGET%type := ?;
  V_PLINE_CODE       QCM_ZG_JUGDE_APPLY.PLINE_CODE%type := ?;

BEGIN

  UPDATE QCM_ZG_JUGDE_APPLY T
     SET T.BAL_YEAR_MONTH = V_BAL_YEAR_MONTH
   WHERE T.JUGDE_APPLY_CODE||T.JUGDE_APPLY_SEQUENO = V_JUGDE_APPLY_CODE
     AND T.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO
     AND T.BATCH_NO = V_BATCH_NO
     AND T.BATCH_GROUD_NO = V_BATCH_GROUD_NO
     AND T.QUALITY_GROUP = V_QUALITY_GROUP
     AND NVL(T.MAT_GOWHERE_NAME, '/') = NVL(V_MAT_GOWHERE_NAME, '/')
     AND T.PLINE_CODE = V_PLINE_CODE;

  IF V_TARGET = 'A' THEN
    UPDATE YDM_ZC_INLIST B
       SET B.BAL_YEAR_MONTH = V_BAL_YEAR_MONTH,
            B.INSTOCK_TIME = TO_DATE(?,'YYYY-MM-DD HH24:MI:SS')
     WHERE B.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO
       AND B.BATCH_NO = V_BATCH_NO
       AND B.BATCH_GROUD_NO = V_BATCH_GROUD_NO
       AND B.JUGDE_APPLY_CODE = V_JUGDE_APPLY_CODE
       AND B.INSTOCK_TYPE_CODE = '800701' --产出入库
       AND B.PLINE_CODE = V_PLINE_CODE
       AND B.VALIDFLAG = '1';
  
  ELSE
    UPDATE YDM_BC_INLIST B
       SET B.BAL_YEAR_MONTH = V_BAL_YEAR_MONTH,
        B.INSTOCK_TIME = TO_DATE(?,'YYYY-MM-DD HH24:MI:SS')
     WHERE B.JUDGE_STOVE_NO = V_JUDGE_STOVE_NO
       AND B.BATCH_NO = V_BATCH_NO
       AND B.BATCH_GROUD_NO = V_BATCH_GROUD_NO
       AND B.INSTOCK_TYPE_CODE = '800709' --产权入库
       AND B.PLINE_CODE = V_PLINE_CODE
       AND B.VALIDFLAG = '1';
  END IF;

END;

]]>
</query>

<query id = "FrmUpdateBalYearMonth.doUpdateBelongName" desc = "调整所属权  ">
<![CDATA[
UPDATE QCM_ZG_JUGDE_APPLY T
   SET T.BELONG_CODE_JUGDE = ?, T.BELONG_NAME_JUGDE = ?
 WHERE T.JUGDE_APPLY_CODE = ?
   AND T.JUDGE_STOVE_NO = ?
   AND T.BATCH_NO = ?
   AND T.BATCH_GROUD_NO = ?
   AND T.QUALITY_GROUP = ?
   AND T.MAT_GOWHERE_NAME = ?
]]>
</query>

<query id = "FrmUpdateBalYearMonth.doQueryIsCarryVoer" desc = "查询是否结转  ,是否缴库">
<![CDATA[
select t.is_carry_voer, T.INSTOCK_FLAG
  from QCM_ZG_JUGDE_APPLY t
 where t.jugde_apply_code || t.JUGDE_APPLY_SEQUENO = ?
   and t.judge_stove_no = ?
   and t.pline_code = ?
]]>
</query>

<query id = "FrmUpdateBalYearMonth.doQueryBalYearMonth" desc = "查询期初表最大结转年月 ">
<![CDATA[
select max(t.bal_year_month) bal_year_month,
       MIN(t.bal_year_month) bal_year_month1
  from YDM_ZC_INITIAL_ZG t
]]>
</query>

<query id = "FrmUpdateBalYearMonth.doQueryAPPLY" desc = "查询判定表结转年月 ">
<![CDATA[
SELECT MAX(T.BAL_YEAR_MONTH) BAL_YEAR_MONTH
  FROM QCM_ZG_JUGDE_APPLY T
 WHERE T.JUGDE_APPLY_CODE||T.JUGDE_APPLY_SEQUENO = ?
   AND T.VALIDFLAG = '20'
   AND t.IS_FLAG = '0'
   and t.APPLY_FLAG = '0'
]]>
</query>

<query id = "FrmUpdateBalYearMonth.doQueryBalYearMonth3" desc = "产权出库的最小结转年月 ">
<![CDATA[
select nvl(min(t.bal_year_month),0) bal_year_month
  from ydm_bc_outlist t
 where t.outstock_type_code = '800809'
   and t.judge_stove_no = ?
   and t.batch_no = ?
   and t.batch_groud_no = ?
   and t.pline_code = ?
]]>
</query>

</queryMap>