<?xml version="1.0" encoding="UTF-8" ?>
<queruMap desc="报检实绩">
    <query id="FrmBaoJianResult.QueryMain" desc="查询报检主信息">
        SELECT
            T.INSPECTION_NO,
            T.INSPECTION_NUM,
            T.HEAT_PLAN_NO,
            T.ZY_BATCH_ID,
            T.JUDGE_STOVE_NO,
            T.BATCH_NO,
            DECODE(T.INSPECTION_FLAG,'0','待抽','1','待检','2','已检','3','免检') INSPECTION_FLAG,
            T.INSPECTION_RESULT,
            T.PRODUCCODE,
            T.PRODUCNAME,
            T.STEELCODE,
            T.STEELNAME,
            T.GRADECODE,
            T.GRADENAME,
            T.SPEC_CODE,
            T.SPEC_NAME,
            T.MODEL_CODE,
            T.MODEL_DESC,
            T.STD_CODE,
            T.STD_NAME,
            T.STD_STYLE,
            T.STD_STYLE_DESC,
            T.CRAFT_FILE_NO,
            T.CUSTOMER_NM,
            T.ACT_COUNT,
            T.ACT_WEIGHT,
            TO_CHAR(T.CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
            TO_CHAR(T.LOAD_TIME, 'YYYY-MM-DD HH24:MI:SS') LOAD_TIME,
            TO_CHAR(T.INSPECTION_TIME, 'YYYY-MM-DD HH24:MI:SS') INSPECTION_TIME,
            T.INSPECTION_NAME,
            DECODE(T.APPLY_FLAG,'0','待送判','1','已送判') APPLY_FLAG,
            T.PROCESS_CODE,
            T.UNQUALIFIED_REASONS,
            T.DISPOSAL_METHOD,
            DECODE(T.DISPOSAL_FLAG,'0','待完成','1','已完成') DISPOSAL_FLAG,
            T.PLINE_CODE,
            T.PLINE_NAME,
            T.DISPOSAL_TIME,
            T.DISPOSAL_NAME,
            T.IDCODE,
            DECODE(T.VALIDFLAG,'1','有效','0','无效') VALIDFLAG,
            DECODE(T.RECHECK,'0','不需要','1','需要','2','已重检') RECHECK,
            T.ORDER_NO,
            T.ORDER_SEQ,
            T.SCRAP_NUM,
            T.PROC_DATE,
            T.SEQ,
            T.SCRAP_WT,
            T.CREATE_NAME,
            (SELECT MAX(C.CRAFT_PATH) FROM CRAFT_ORD_DESIGN C WHERE C.CRAFT_NO = T.CRAFT_FILE_NO) CRAFT_PATH,
            (SELECT MAX(T2.STORAGE_NAME)
            FROM YDM_BC_BUTTRESS T1, YDM_BS_STORAGE T2
            WHERE T1.JUDGE_STOVE_NO = T.JUDGE_STOVE_NO
            AND T1.BATCH_NO = T.BATCH_NO
            AND T1.PLINE_CODE = T.PLINE_CODE
            AND T1.STORAGE_NO = T2.STORAGE_NO)STORAGE_NAME,
            (SELECT MAX(T2.LOCATION_NAME)
            FROM YDM_BC_BUTTRESS T1,YDM_BS_LOCATION T2
            WHERE T1.JUDGE_STOVE_NO = T.JUDGE_STOVE_NO
            AND T1.BATCH_NO = T.BATCH_NO
            AND T1.PLINE_CODE = T.PLINE_CODE
            AND T1.LOCATION_NO = T2.LOCATION_NO)LOCATION_NAME
        FROM MIL_INSPECTION T
        WHERE
            1 = 1
    </query>

    <query desc="查询报检详细信息" id="FrmBaoJianResult.QueryDetail">
        SELECT
            T.INSPECTION_NO,
            T.INSPECTION_NUM,
            DECODE(T.PROJECT_CATEGORY,'1','表面质量','2','外包装质量','3','几何尺寸')PROJECT_CATEGORY,
            T.PROJECT_NAME,
            T.RESULT,
            T.REASONS,
            T.MOME,
            T.GUID,
            T.DETECTION_CODE
        FROM
            (SELECT
            T1 .INSPECTION_NO,
            T1 .INSPECTION_NUM,
            DECODE(T1 .PROJECT_CATEGORY,'表面质量','1','外包装质量','2','几何尺寸','3') PROJECT_CATEGORY,
            T1 .PROJECT_NAME,
            T1.RESULT,
            T1.REASONS,
            T1.MOME,
            T1.GUID,
            T1.DETECTION_CODE
            FROM
            MIL_INSPECTION_DETAIL T1
            ORDER BY
            PROJECT_CATEGORY) T
        WHERE
            1 = 1
    </query>

    <query desc="报检子表字段信息修改" id="FrmBaoJianResult.savedetail">
        UPDATE MIL_INSPECTION_DETAIL T
        SET
            T.RESULT = ?,
            T.REASONS = ?,
            T.MOME = ?,
            T.DETECTION_CODE = ?
        WHERE
            1 = 1
    </query>

    <query desc="更新主表" id="FrmBaoJianResult.UpdateMain">
        UPDATE MIL_INSPECTION T
        SET
            T.INSPECTION_RESULT = ?,
            T.UNQUALIFIED_REASONS = ?,
            T.DISPOSAL_FLAG = ?,
            T.INSPECTION_TIME = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),
            T.INSPECTION_NAME = ?,
            T.RECHECK = ?,
            T.INSPECTION_FLAG = 2
        WHERE
            1 = 1
    </query>

    <query desc="处置" id="FrmBaoJianResult.Chuzhi">
        UPDATE MIL_INSPECTION T
        SET
            T.DISPOSAL_METHOD = ?,
            T.DISPOSAL_FLAG = ?,
            T.DISPOSAL_TIME = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),
            T.DISPOSAL_NAME = ?
        WHERE
            1 = 1
    </query>

    <query desc="撤销处置" id="FrmBaoJianResult.RevokeChuzhi">
        UPDATE MIL_INSPECTION T
        SET
            T.DISPOSAL_METHOD = ?,
            T.DISPOSAL_FLAG = ?,
            T.DISPOSAL_TIME = TO_DATE(?,'yyyy-mm-dd hh24:mi:ss'),
            T.DISPOSAL_NAME = ?,
            T.RECHECK = 0
        WHERE
            1 = 1
     </query>

    <query desc="废除" id="FrmBaoJianResult.DelMain">
     <![CDATA[
        DECLARE
  V_INSPECTION_NO  MIL_INSPECTION.INSPECTION_NO%type := ?;
  V_INSPECTION_NUM MIL_INSPECTION.INSPECTION_NUM%type := ?;
BEGIN
  UPDATE MIL_INSPECTION T
     SET T.VALIDFLAG = '0'
   WHERE T.INSPECTION_NO = V_INSPECTION_NO
     AND T.INSPECTION_NUM = V_INSPECTION_NUM;
  UPDATE MIL_INSPECTION T
     SET T.RECHECK = '1'
   WHERE T.RECHECK = '2'
     AND T.INSPECTION_NO = V_INSPECTION_NO
     AND T.INSPECTION_NUM = V_INSPECTION_NUM - 1;
END;
 ]]>
    </query>

    <query desc="撤销废除" id="FrmBaoJianResult.RevokeDel">
        <![CDATA[
        DECLARE
  V_INSPECTION_NO  MIL_INSPECTION.INSPECTION_NO%type := ?;
  V_INSPECTION_NUM MIL_INSPECTION.INSPECTION_NUM%type := ?;
BEGIN
  UPDATE MIL_INSPECTION T
     SET T.VALIDFLAG = '1'
   WHERE T.INSPECTION_NO = V_INSPECTION_NO
     AND T.INSPECTION_NUM = V_INSPECTION_NUM;
  UPDATE MIL_INSPECTION T
     SET T.RECHECK = '2'
   WHERE T.RECHECK = '1'
     AND T.INSPECTION_NO = V_INSPECTION_NO
     AND T.INSPECTION_NUM = V_INSPECTION_NUM - 1;
END;
 ]]>
    </query>
    <query desc="抽检" id="FrmBaoJianResult.Chouajian">
        UPDATE MIL_INSPECTION T
        SET
            T.INSPECTION_FLAG = ?
        WHERE
            1 = 1
    </query>

    <query desc="普通免检的自动送判" id="FrmBaoJianResult.SongPanPT">
        UPDATE MIL_INSPECTION T
        SET
            T.APPLY_FLAG = ?
        WHERE
            1 = 1
    </query>

    <query desc="修改支数" id="FrmBaoJianResult.UpdateActCount">
        UPDATE MIL_INSPECTION T
        SET
            T.ACT_COUNT = ?,
            T.ACT_WEIGHT = ?,
            T.SCRAP_NUM = ?,
            T.SCRAP_WT = ?
        WHERE
            1 = 1
    </query>

    <query desc="查询产线" id="FrmBaoJianResult.doQueryPlineCode">
        <![CDATA[
	SELECT T.PLINE_CODE   plineCode,
       T.PLINE_NAME   plineName,
       T.PROCESS_DESC processDesc,
       T2.BASENAME    factoryDesc,
       T3.BASENAME    checkStationDesc
  FROM COM_BASE_PLINE T
  LEFT JOIN COM_BASE_INFO T2
    ON T2.BASECODE = T.FACTORY_CODE
  LEFT JOIN COM_BASE_INFO T3
    ON T3.BASECODE = T.CHECK_STATION
 WHERE T.VALIDFLAG = '1'
   AND T.PROCESS_CODE IN (?)
 ORDER BY T.PROCESS_CODE, T.FACTORY_CODE, T.PLINE_NAME
 ]]>
    </query>

    <query desc="查询产线" id="FrmBaoJianResult.QueryPlineCode">
        SELECT
            T.PLINE_CODE
        FROM
            COM_BASE_PLINE T
        WHERE 1 = 1
    </query>

    <query desc="待抽改 待检" id="FrmBaoJianResult.UpdateInsflag1">
        UPDATE MIL_INSPECTION T
        SET
            T.INSPECTION_FLAG = ?
        WHERE
            T.INSPECTION_NO = ? AND INSPECTION_NUM = ?
    </query>

    <query desc="待抽改 免检" id="FrmBaoJianResult.UpdateInsflag2">
        UPDATE MIL_INSPECTION T
        SET
        T.INSPECTION_FLAG = ?,
        T.APPLY_FLAG = ?
        WHERE
        T.INSPECTION_NO = ? AND INSPECTION_NUM = ?
    </query>
    <query desc="抽检" id="FrmBaoJianResult.Chouajian2">
    <![CDATA[
  DECLARE
BEGIN
  --轧管10炉抽1炉
  FOR MYROW4 IN (SELECT DISTINCT T.PLINE_CODE
                   FROM MIL_INSPECTION T
                  WHERE T.INSPECTION_FLAG = '0'
                    AND T.PROCESS_CODE = 'D') LOOP
    FOR MYROW IN (SELECT *
                    FROM (SELECT T.*
                            FROM MIL_INSPECTION T
                           WHERE T.INSPECTION_FLAG = '0'
                             AND T.PROCESS_CODE = 'D'
                             AND T.PLINE_CODE = MYROW4.PLINE_CODE
                           order by DBMS_RANDOM.random)
                   WHERE ROWNUM <=
                         CEIL((SELECT COUNT(1)
                                 FROM MIL_INSPECTION T
                                WHERE T.INSPECTION_FLAG = '0'
                                  AND T.PLINE_CODE = MYROW4.PLINE_CODE
                                  AND T.PROCESS_CODE = 'D') / 10)) LOOP
    
      UPDATE MIL_INSPECTION T
         SET T.INSPECTION_FLAG = '1'
       WHERE T.IDCODE = MYROW.IDCODE;
    END LOOP;
    UPDATE MIL_INSPECTION T
       SET T.INSPECTION_FLAG = '3'
     WHERE T.INSPECTION_FLAG = '0'
       AND T.PROCESS_CODE = 'D';
  END LOOP;

  --热处理，加工每炉都抽，每炉10抽1
  FOR MYROW1 IN (SELECT DISTINCT T.JUDGE_STOVE_NO
                   FROM MIL_INSPECTION T
                  WHERE T.INSPECTION_FLAG = '0'
                    AND T.PROCESS_CODE <> 'D') LOOP
    FOR MYROW2 IN (SELECT *
                     FROM (SELECT T.*
                             FROM MIL_INSPECTION T
                            WHERE T.INSPECTION_FLAG = '0'
                              AND T.PROCESS_CODE <> 'D'
                              AND T.JUDGE_STOVE_NO = MYROW1.JUDGE_STOVE_NO
                            order by DBMS_RANDOM.random)
                    WHERE ROWNUM <=
                          CEIL((SELECT COUNT(1)
                                  FROM MIL_INSPECTION T
                                 WHERE T.INSPECTION_FLAG = '0'
                                   AND T.JUDGE_STOVE_NO =
                                       MYROW1.JUDGE_STOVE_NO
                                   AND T.PROCESS_CODE <> 'D') / 10)) LOOP
      UPDATE MIL_INSPECTION T
         SET T.INSPECTION_FLAG = '1'
       WHERE T.IDCODE = MYROW2.IDCODE
         AND T.JUDGE_STOVE_NO = MYROW1.JUDGE_STOVE_NO;
    END LOOP;
    UPDATE MIL_INSPECTION T
       SET T.INSPECTION_FLAG = '3'
     WHERE T.INSPECTION_FLAG = '0'
       AND T.JUDGE_STOVE_NO = MYROW1.JUDGE_STOVE_NO
       AND T.PROCESS_CODE <> 'D';
  END LOOP;

END;
]]>
    </query>
</queruMap>